com.waltermilner.complex

## Class Complex

• java.lang.Object
• com.waltermilner.complex.Complex

• ```public class Complex
extends java.lang.Object```
This class models a complex variable. The real and imaginary parts are double values, so range and precision correspond to the range and precision of double (we don't use BigDecimal for speed) Complex numbers are not ordered, so there is no compareTo method
Since:
14-Dec-2014
• ### Constructor Summary

Constructors
Constructor and Description
`Complex()`
Default no-arg constructor.
`Complex(Complex other)`
Copy constructor.
```Complex(double r, double i)```
Construct a new complex number.
• ### Method Summary

Methods
Modifier and Type Method and Description
`Complex` `add(Complex other)`
`double` `arg()`
Argument.
`Complex` `conjugate()`
Complex conjugate
`static Complex` `cos(Complex z)`
Cosine of a complex number.
`Complex` `div(Complex other)`
Divide complex numbers.
`Complex` `div(double d)`
Divide complex number by a real.
`boolean` `equals(java.lang.Object obj)`
equals.
`static Complex` `exp(Complex z)`
e to the power z.
`int` `hashCode()`
hashCode.
`double` `im()`
Imaginary part
`static Complex` `log(Complex c)`
Natural logarithm.
`double` `mod()`
Modulus
`Complex` `mul(Complex other)`
Multiply complex numbers.
`static Complex` ```polar(double r, double theta)```
Create a complex number from polar values.
`Complex` `pow(int n)`
Exponentiation to an integer power.
`double` `re()`
Real part
`static Complex` `sin(Complex z)`
Sine of a complex number.
`Complex` `sqrt()`
Square root of a complex number.
`Complex` `sub(Complex other)`
Subtract complex numbers.
`java.lang.String` `toString()`
A string representation of the complex number.
• ### Methods inherited from class java.lang.Object

`clone, finalize, getClass, notify, notifyAll, wait, wait, wait`
• ### Constructor Detail

• #### Complex

```public Complex(double r,
double i)```
Construct a new complex number.
Parameters:
`r` - real part
`i` - imaginary part
• #### Complex

`public Complex(Complex other)`
Copy constructor. Construct a new complex number with real and imaginary parts same as another
Parameters:
`other` - the copied complex number
• #### Complex

`public Complex()`
Default no-arg constructor. Real part = imaginary part = 0
• ### Method Detail

• #### toString

`public java.lang.String toString()`
A string representation of the complex number. Real and imaginary parts are shown with up to 3 decimal places
Overrides:
`toString` in class `java.lang.Object`
Returns:
A string representation of the complex number
• #### conjugate

`public Complex conjugate()`
Complex conjugate
Returns:
a-ib for complex number a+ib
• #### mod

`public double mod()`
Modulus
Returns:
The modulus
• #### re

`public double re()`
Real part
Returns:
real part
• #### im

`public double im()`
Imaginary part
Returns:
Imaginary part
• #### sqrt

`public Complex sqrt()`
Square root of a complex number. This is one square root. The other is -this
Returns:
Square root
• #### hashCode

`public int hashCode()`
hashCode. For use in hash tables and similar
Overrides:
`hashCode` in class `java.lang.Object`
Returns:
a hash value
• #### equals

`public boolean equals(java.lang.Object obj)`
equals.
Overrides:
`equals` in class `java.lang.Object`
Parameters:
`obj` - something else
Returns:
true iff obj is another complex number whose real and imaginary parts are equal to this one's
• #### polar

```public static Complex polar(double r,
double theta)```
Create a complex number from polar values.
Parameters:
`r` - modulus
`theta` - argument
Returns:
a Complex number = r cos(theta) + i r sin(theta)
• #### arg

`public double arg()`
Argument. In range -pi to pi
Returns:
the argument of the number in polar form

`public Complex add(Complex other)`
Parameters:
`other` - another complex number
Returns:
A new complex number, the sum of this number and the other
• #### sub

`public Complex sub(Complex other)`
Subtract complex numbers.
Parameters:
`other` - another complex number
Returns:
A new complex number, the difference between this number and the other
• #### mul

`public Complex mul(Complex other)`
Multiply complex numbers.
Parameters:
`other` - another complex number
Returns:
A new complex number, the product of this number and the other
• #### div

`public Complex div(double d)`
Divide complex number by a real. Throws a ComplexException if the divisor is zero
Parameters:
`d` - a real number
Returns:
A new complex number, this divided by the scalar
• #### div

`public Complex div(Complex other)`
Divide complex numbers. This throws a ComplexException if the other number is zero
Parameters:
`other` - another complex number
Returns:
A new complex number, the quotient of this number and the other
• #### pow

`public Complex pow(int n)`
Exponentiation to an integer power.
Parameters:
`n` - The power
Returns:
A new number, this raised to the given power
• #### exp

`public static Complex exp(Complex z)`
e to the power z. This works by evaluating a power series with 20 terms
Parameters:
`z` - A complex number
Returns:
e to the power z
• #### sin

`public static Complex sin(Complex z)`
Sine of a complex number. This evaluates the truncated power series We could have used (e^iz-e^-iz)/2i but this would be faster
Parameters:
`z` - the complex number to find the sine of
Returns:
the sine
• #### log

`public static Complex log(Complex c)`
Natural logarithm.
Parameters:
`c` - The complex number
Returns:
The principal value of the natural log of c