Package edu.princeton.cs.algs4
Class Complex
- Object
-
- edu.princeton.cs.algs4.Complex
-
public class Complex extends Object
TheComplexclass 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.This computes correct results if all arithmetic performed is without floating-point rounding error or arithmetic overflow. In practice, there will be floating-point rounding error.
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 Description Complex(double real, double imag)Initializes a complex number from the specified real and imaginary parts.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description doubleabs()Returns the absolute value of this complex number.Complexconjugate()Returns the complex conjugate of this complex number.Complexcos()Returns the complex cosine of this complex number.Complexdivides(Complex that)Returns the result of dividing the specified complex number into this complex number.Complexexp()Returns the complex exponential of this complex number.doubleim()Returns the imaginary part of this complex number.static voidmain(String[] args)Unit tests theComplexdata type.Complexminus(Complex that)Returns the result of subtracting the specified complex number from this complex number.doublephase()Returns the phase of this complex number.Complexplus(Complex that)Returns the sum of this complex number and the specified complex number.doublere()Returns the real part of this complex number.Complexreciprocal()Returns the reciprocal of this complex number.Complexscale(double alpha)Returns the product of this complex number and the specified scalar.Complexsin()Returns the complex sine of this complex number.Complextan()Returns the complex tangent of this complex number.Complextimes(Complex that)Returns the product of this complex number and the specified complex number.StringtoString()Returns a string representation of this complex number.
-
-
-
Method Detail
-
toString
public String toString()
Returns a string representation of this complex number.
-
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)
-
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 theComplexdata type.- Parameters:
args- the command-line arguments
-
-