# Complex

Inherits from: CAObject

Maturity Index: Relatively immature

## Class Description

A complex object consists of two real objects, a real and imaginary part. Real objects could be arbitrary Computer Algebra Kit objects, but the class has been used so far only for the Gaussian integers i.e., complex numbers over the integers. The imaginary unit is represented by the symbol I.

## Methods

### real:

+real:aReal
Returns a new complex object with real part set to aReal and the imaginary part set to the real zero.

### real:imaginary:

+real:aRealimaginary:anotherReal
Returns a new complex object with real part set to aReal and the imaginary part set to anotherReal.

### copy

-copy
Returns a new copy of the complex object.

### deepCopy

-deepCopy
Returns a new copy of the complex object; sends deepCopy messages to real and imaginary part.

### real

-real
Returns the real part of a complex number.

### imaginary

-imaginary
Returns the imaginary part of a complex number.

### isReal

- (BOOL)isReal
Whether the imaginary part is equal to zero.

### notReal

- (BOOL)notReal
Whether the isReal returns NO.

### hash

- (unsigned)hash
Returns a small integer that is the same for objects that are equal in the sense of isEqual:.

### isEqual:

- (BOOL)isEqual:b
Whether real and imaginary parts are equal to each other.

### conjugate

-conjugate
Returns a new reference to the object if it is real. Otherwise, returns a new object that is equal to the complex conjugate of the object. The conjugate of a + I b is a - I b.

### norm

-norm
Returns a new real object, the norm of the complex object i.e., the object multiplied by its conjugate.

### asReal

-asReal
If the imaginary part of the object is zero, this method returns a new reference to the real part of the object.

### zero

-zero
Returns a new complex object with real and imaginary part equal to zero.

### isZero

- (BOOL)isZero
Whether real and imaginary part are equal to zero.

### isOpposite:

- (BOOL)isOpposite:b
Whether real and imaginary parts are opposite to each other.

### negate

-negate
Returns a new complex number, by negating real and imaginary parts.

### double

-double
Returns a new complex number, by doubling real and imaginary parts.

Returns the sum of the complex numbers; adds real and imaginary parts together.

### subtract:

-subtract:b
Returns the difference of the complex numbers; subtracts real and imaginary parts from each other.

### I

-I
Returns a new reference to the imaginary unit.

### one

-one
Returns a new object with real part equal to one and imaginary part equal to zero.

### isOne

- (BOOL)isOne
Whether the complex object is a multiplicative unit. The complex units consist of the real units and the real units multiplied by the imaginary unit i.

### isMinusOne

- (BOOL)isMinusOne
Whether the complex object is a multiplicative unit. The complex units consist of the real units and the real units multiplied by the imaginary unit i.

### isUnit

- (BOOL)isUnit
Whether the complex object is a multiplicative unit. The complex units consist of the real units and the real units multiplied by the imaginary unit i.

### square

-square
Returns the complex number multiplied by itself, computed by the rule :

```(a+I b)^2 == (a^2 - b^2) + I (2 a b)
```

### multiply:

-multiply:b
Returns the product of the two complex numbers, computed by the rule :

```(a+I b)(c+I d) == (ac-bd) + I (ad+bc)
```

### power:

-power:(int)n
Returns the complex number raised to the n-th power. If the object is real, simply computes the n-th power as a real. Otherwise, computes the power by the binary exponentation algorithm.

### divide:

-divide:b
Returns the exact quotient of the complex number by the complex number b. Multiplies by the conjugate of b and then divides by the norm of b, or returns nil if the latter division is not exact.

### inverse

-inverse
Returns the conjugate of the complex number divided by its norm, or nil if the latter division is not exact.

### quotient:

-quotient:b
Returns the quotient on division by b.

Note: I'm not sure about the algorithm I'm using here !

### remainder:quotient:

-remainder:bquotient:(id *)q
Computes the remainder, and if q is non-NULL pointer, the quotient on division by b.

### gcd:

-gcd:b
If both objects are real, returns a complex object with real part set to the (real) gcd of the objects. Otherwise, computes the greatest common divisor by the Euclidean algorithm.

### printOn:

-printOn:(IOD)aFile
Prints the number to aStream.
{"ft_lang_label":"__label__en","ft_lang_prob":0.8047958,"math_prob":0.89413905,"size":3658,"snap":"2023-40-2023-50","text_gpt3_token_len":840,"char_repetition_ratio":0.22632731,"word_repetition_ratio":0.21827412,"special_character_ratio":0.21268453,"punctuation_ratio":0.11891118,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9962219,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-10-01T01:18:07Z\",\"WARC-Record-ID\":\"<urn:uuid:d3762770-c77e-4cf2-8e5b-f84e87a88a57>\",\"Content-Length\":\"8812\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:c7cf5a4f-9d24-44a2-b2f5-02f85df1a80e>\",\"WARC-Concurrent-To\":\"<urn:uuid:519e06ee-51f4-4cda-bbb5-47bcf0d66198>\",\"WARC-IP-Address\":\"73.86.242.53\",\"WARC-Target-URI\":\"http://updates.gnu-darwin.org/ProgramDocuments/cakit/Complex.html.body\",\"WARC-Payload-Digest\":\"sha1:G2AHN5YKJRT5LU4YEWLFKGYE3VMQF6D5\",\"WARC-Block-Digest\":\"sha1:AIM3DQEXLDM2GHUHH3XZQDTLR6RFU6AM\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-40/CC-MAIN-2023-40_segments_1695233510734.55_warc_CC-MAIN-20231001005750-20231001035750-00711.warc.gz\"}"}