edu.princeton.cs.algs4

## Class Complex

• Object
• edu.princeton.cs.algs4.Complex

• ```public class Complex
extends Object```
The `Complex` class represents a complex number. Complex numbers are immutable: their values cannot be changed after they are created. It includes methods for addition, subtraction, multiplication, division, conjugation, and other common functions on complex numbers.

For additional documentation, see Section 9.9 of Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne.

Author:
Robert Sedgewick, Kevin Wayne
• ### Constructor Summary

Constructors
Constructor and Description
```Complex(double real, double imag)```
Initializes a complex number from the specified real and imaginary parts.
• ### Method Summary

Methods
Modifier and Type Method and Description
`double` `abs()`
Returns the absolute value of this complex number.
`Complex` `conjugate()`
Returns the complex conjugate of this complex number.
`Complex` `cos()`
Returns the complex cosine of this complex number.
`Complex` `divides(Complex that)`
Returns the result of dividing the specified complex number into this complex number.
`Complex` `exp()`
Returns the complex exponential of this complex number.
`double` `im()`
Returns the imaginary part of this complex number.
`static void` `main(String[] args)`
Unit tests the `Complex` data type.
`Complex` `minus(Complex that)`
Returns the result of subtracting the specified complex number from this complex number.
`double` `phase()`
Returns the phase of this complex number.
`Complex` `plus(Complex that)`
Returns the sum of this complex number and the specified complex number.
`double` `re()`
Returns the real part of this complex number.
`Complex` `reciprocal()`
Returns the reciprocal of this complex number.
`Complex` `scale(double alpha)`
Returns the product of this complex number and the specified scalar.
`Complex` `sin()`
Returns the complex sine of this complex number.
`Complex` `tan()`
Returns the complex tangent of this complex number.
`Complex` `times(Complex that)`
Returns the product of this complex number and the specified complex number.
`Complex` `times(double alpha)`
Deprecated.
`String` `toString()`
Returns a string representation of this complex number.
• ### Methods inherited from class Object

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

• #### Complex

```public Complex(double real,
double imag)```
Initializes a complex number from the specified real and imaginary parts.
Parameters:
`real` - the real part
`imag` - the imaginary part
• ### Method Detail

• #### toString

`public String toString()`
Returns a string representation of this complex number.
Overrides:
`toString` in class `Object`
Returns:
a string representation of this complex number, of the form 34 - 56i.
• #### abs

`public double abs()`
Returns the absolute value of this complex number. This quantity is also known as the modulus or magnitude.
Returns:
the absolute value of this complex number
• #### phase

`public double phase()`
Returns the phase of this complex number. This quantity is also known as the angle or argument.
Returns:
the phase of this complex number, a real number between -pi and pi
• #### plus

`public Complex plus(Complex that)`
Returns the sum of this complex number and the specified complex number.
Parameters:
`that` - the other complex number
Returns:
the complex number whose value is `(this + that)`
• #### minus

`public Complex minus(Complex that)`
Returns the result of subtracting the specified complex number from this complex number.
Parameters:
`that` - the other complex number
Returns:
the complex number whose value is `(this - that)`
• #### times

`public Complex times(Complex that)`
Returns the product of this complex number and the specified complex number.
Parameters:
`that` - the other complex number
Returns:
the complex number whose value is `(this * that)`
• #### scale

`public Complex scale(double alpha)`
Returns the product of this complex number and the specified scalar.
Parameters:
`alpha` - the scalar
Returns:
the complex number whose value is `(alpha * this)`
• #### times

```@Deprecated
public Complex times(double alpha)```
Deprecated. Replaced by `scale(double)`.
Returns the product of this complex number and the specified scalar.
Parameters:
`alpha` - the scalar
Returns:
the complex number whose value is `(alpha * this)`
• #### conjugate

`public Complex conjugate()`
Returns the complex conjugate of this complex number.
Returns:
the complex conjugate of this complex number
• #### reciprocal

`public Complex reciprocal()`
Returns the reciprocal of this complex number.
Returns:
the complex number whose value is `(1 / this)`
• #### re

`public double re()`
Returns the real part of this complex number.
Returns:
the real part of this complex number
• #### im

`public double im()`
Returns the imaginary part of this complex number.
Returns:
the imaginary part of this complex number
• #### divides

`public Complex divides(Complex that)`
Returns the result of dividing the specified complex number into this complex number.
Parameters:
`that` - the other complex number
Returns:
the complex number whose value is `(this / that)`
• #### exp

`public Complex exp()`
Returns the complex exponential of this complex number.
Returns:
the complex exponential of this complex number
• #### sin

`public Complex sin()`
Returns the complex sine of this complex number.
Returns:
the complex sine of this complex number
• #### cos

`public Complex cos()`
Returns the complex cosine of this complex number.
Returns:
the complex cosine of this complex number
• #### tan

`public Complex tan()`
Returns the complex tangent of this complex number.
Returns:
the complex tangent of this complex number
• #### main

`public static void main(String[] args)`
Unit tests the `Complex` data type.
Parameters:
`args` - the command-line arguments