com.singularsys.jep.standard
Class Complex

java.lang.Object
  extended by com.singularsys.jep.standard.Complex
All Implemented Interfaces:
java.io.Serializable

public class Complex
extends java.lang.Object
implements java.io.Serializable

Represents a complex number with double precision real and imaginary components. Includes complex arithmetic functions.

The two main sources of reference used for creating this class were:
- "Numerical Recipes in C - The Art of Scientific Computing" (ISBN 0-521-43108-5) http://www.nr.com and
- The org.netlib.math.complex package (http://www.netlib.org) which was developed by Sandy Anderson and Priyantha Jayanetti (published under GPL).

Some of the arithmetic functions in this class are based on the mathematical equations given in the source of the netlib package. The functions were validated by comparing results with the netlib complex class.

It is important to note that the netlib complex package is more extensive and efficient (e.g. Garbage collector friendly) than this implementation. If high precision and efficiency if of necessity it is recommended to use the netlib package.

Version:
2.3.0 alpha now extends Number, has add and sub methods., 2.3.0 beta 1 now overrides equals and hashCode. /* @version 2.3.0 beta 2 does not implement Number anymore, as caused too many problems.
Author:
Nathan Funk
See Also:
Serialized Form

Constructor Summary
Complex()
          Default constructor.
Complex(Complex z)
          Copy constructor
Complex(double re_in)
          Constructor from a single double value.
Complex(double re_in, double im_in)
          Initialize the real and imaginary components to the values given by the parameters.
Complex(java.lang.Number re_in)
          Construct from a Number.
 
Method Summary
 double abs()
          Returns the absolute value of the complex number.
 double abs2()
          Returns the square of the absolute value (re*re+im*im).
 Complex acos()
           
 Complex acosh()
           
 Complex add(Complex b)
          Adds the complex number with another complex value.
 double arg()
          Returns the argument of this complex number (Math.atan2(re,im))
 Complex asin()
           
 Complex asinh()
           
 Complex atan()
           
 Complex atanh()
           
 Complex conj()
          Returns the complex conjugate.
 Complex cos()
          Returns the cosine of this complex number.
 Complex cosh()
           
 Complex div(Complex b)
          Returns the result of dividing this complex number by the parameter.
 double doubleValue()
          Returns real part.
 boolean equals(Complex b, double tolerance)
          Compares this object with the Complex number given as parameter b.
 boolean equals(java.lang.Object o)
          Compares this object against the specified object.
 float floatValue()
          Returns real part.
 int hashCode()
          Always override hashCode when you override equals.
 double im()
          Returns the imaginary component of this object
 int intValue()
          Returns real part.
 boolean isInfinite()
          Returns true if either the real or imaginary component of this Complex is an infinite value.
 boolean isNaN()
          Returns true if either the real or imaginary component of this Complex is a Not-a-Number (NaN) value.
 Complex log()
          Returns the logarithm of this complex number.
 long longValue()
          Returns real part.
 Complex mul(Complex b)
          Multiply the complex number with another complex value.
 Complex mul(double b)
          Multiply the complex number with a double value.
 Complex neg()
          Returns the negative value of this complex number.
static Complex polarValueOf(java.lang.Number r, java.lang.Number theta)
          Converts an [r,theta] pair to a complex number r * e^(i theta).
 Complex power(Complex exponent)
          Returns the value of this complex number raised to the power of a complex exponent
 Complex power(double exponent)
          Returns the value of this complex number raised to the power of a real component (in double precision).
 double re()
          Returns the real component of this object
 Complex reciprocal()
          Returns the reciprocal of a complex number 1/z.
 void set(Complex z)
          Copies the values from the parameter object to this object
 void set(double re_in, double im_in)
          Sets the real and imaginary values of the object.
 void setIm(double im_in)
          Sets the imaginary component of the object
 void setRe(double re_in)
          Sets the real component of the object
 Complex sin()
          Returns the sine of this complex number.
 Complex sinh()
           
 Complex sqrt()
          Calculates the square root of this object.
 Complex sub(Complex b)
          Adds the complex number with another complex value.
 Complex tan()
          Returns the tangent of this complex number.
 Complex tanh()
           
 java.lang.String toString()
          Returns the value of this complex number as a string in the format: (real, imaginary).
 java.lang.String toString(java.text.NumberFormat format)
           
 java.lang.String toString(java.text.NumberFormat format, boolean flag)
          Prints using specified number format in format or "2" or "3 i" or "(2+3 i)" if flag is true or "2+3 i" if flag is false
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Complex

public Complex()
Default constructor.


Complex

public Complex(double re_in)
Constructor from a single double value. The complex number is initialized with the real component equal to the parameter, and the imaginary component equal to zero.


Complex

public Complex(java.lang.Number re_in)
Construct from a Number. This constructor uses the doubleValue() method of the parameter to initialize the real component of the complex number. The imaginary component is initialized to zero.


Complex

public Complex(Complex z)
Copy constructor


Complex

public Complex(double re_in,
               double im_in)
Initialize the real and imaginary components to the values given by the parameters.

Method Detail

re

public double re()
Returns the real component of this object


im

public double im()
Returns the imaginary component of this object


set

public void set(Complex z)
Copies the values from the parameter object to this object


set

public void set(double re_in,
                double im_in)
Sets the real and imaginary values of the object.


setRe

public void setRe(double re_in)
Sets the real component of the object


setIm

public void setIm(double im_in)
Sets the imaginary component of the object


equals

public boolean equals(Complex b,
                      double tolerance)
Compares this object with the Complex number given as parameter
b
. The
tolerance
parameter is the radius within which the
b
number must lie for the two complex numbers to be considered equal.

Returns:
true
if the complex number are considered equal,
false
otherwise.

equals

public boolean equals(java.lang.Object o)
Compares this object against the specified object. The result is true if and only if the argument is not null and is a Complex object that represents the same complex number. Equality follows the same pattern as Double applies to each field: This definition allows hash tables to operate properly.

Overrides:
equals in class java.lang.Object
Since:
2.3.0.2

hashCode

public int hashCode()
Always override hashCode when you override equals. Effective Java, Joshua Bloch, Sun Press

Overrides:
hashCode in class java.lang.Object

toString

public java.lang.String toString()
Returns the value of this complex number as a string in the format:
(real, imaginary)
.

Overrides:
toString in class java.lang.Object

toString

public java.lang.String toString(java.text.NumberFormat format)

toString

public java.lang.String toString(java.text.NumberFormat format,
                                 boolean flag)
Prints using specified number format in format or "2" or "3 i" or "(2+3 i)" if flag is true or "2+3 i" if flag is false


isInfinite

public boolean isInfinite()
Returns true if either the real or imaginary component of this Complex is an infinite value.

Returns:
true if either component of the Complex object is infinite; false, otherwise.


isNaN

public boolean isNaN()
Returns true if either the real or imaginary component of this Complex is a Not-a-Number (NaN) value.

Returns:
true if either component of the Complex object is NaN; false, otherwise.


abs

public double abs()
Returns the absolute value of the complex number.


abs2

public double abs2()
Returns the square of the absolute value (re*re+im*im).


arg

public double arg()
Returns the argument of this complex number (Math.atan2(re,im))


neg

public Complex neg()
Returns the negative value of this complex number.


mul

public Complex mul(double b)
Multiply the complex number with a double value.

Returns:
The result of the multiplication

add

public Complex add(Complex b)
Adds the complex number with another complex value.

Returns:
The result of the addition
Since:
2.3.0.1

sub

public Complex sub(Complex b)
Adds the complex number with another complex value.

Returns:
The result of the addition
Since:
2.3.0.1

mul

public Complex mul(Complex b)
Multiply the complex number with another complex value.

Returns:
The result of the multiplication

div

public Complex div(Complex b)
Returns the result of dividing this complex number by the parameter.


reciprocal

public Complex reciprocal()
Returns the reciprocal of a complex number 1/z.


power

public Complex power(double exponent)
Returns the value of this complex number raised to the power of a real component (in double precision).

This method considers special cases where a simpler algorithm would return "ugly" results.
For example when the expression (-1e40)^0.5 is evaluated without considering the special case, the argument of the base is the double number closest to pi. When sin and cos are used for the final evaluation of the result, the slight difference of the argument from pi causes a non-zero value for the real component of the result. Because the value of the base is so high, the error is magnified. Although the error is normal for floating point calculations, the consideration of commonly occurring special cases improves the accuracy and aesthetics of the results.

If you know a more elegant way to solve this problem, please let me know at nathanfunk@hotmail.com .


power

public Complex power(Complex exponent)
Returns the value of this complex number raised to the power of a complex exponent


conj

public Complex conj()
Returns the complex conjugate.


log

public Complex log()
Returns the logarithm of this complex number. The real part is Math.log(this.abs()) and the complex part is this.arg(), in the range -pi .. pi.


sqrt

public Complex sqrt()
Calculates the square root of this object. Adapted from Numerical Recipes in C - The Art of Scientific Computing (ISBN 0-521-43108-5)


sin

public Complex sin()
Returns the sine of this complex number.


cos

public Complex cos()
Returns the cosine of this complex number.


tan

public Complex tan()
Returns the tangent of this complex number.


asin

public Complex asin()

acos

public Complex acos()

atan

public Complex atan()

sinh

public Complex sinh()

cosh

public Complex cosh()

tanh

public Complex tanh()

asinh

public Complex asinh()

acosh

public Complex acosh()

atanh

public Complex atanh()

polarValueOf

public static Complex polarValueOf(java.lang.Number r,
                                   java.lang.Number theta)
Converts an [r,theta] pair to a complex number r * e^(i theta).

Parameters:
r - The radius
theta - The angle
Returns:
The complex result.
Since:
2.3.0.1

doubleValue

public double doubleValue()
Returns real part.

Since:
2.3.0.1

floatValue

public float floatValue()
Returns real part.

Since:
2.3.0.1

intValue

public int intValue()
Returns real part.

Since:
2.3.0.1

longValue

public long longValue()
Returns real part.

Since:
2.3.0.1


Copyright © 2010 Singular Systems http://www.singularsys.com/jep