org.opensourcephysics.numerics
Class Complex

java.lang.Object
  extended by org.opensourcephysics.numerics.Complex

public class Complex
extends java.lang.Object


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 z)
          Returns the sum of two complex numbers
 Complex add(double d)
          Returns the sum of a complex number and a double
 double arg()
          Returns the argument of this complex number (Math.atan2(re,im))
 Complex asin()
           
 Complex asinh()
           
 Complex atan()
           
 Complex atanh()
           
 Complex cartesian()
          Convert polar to Cartesian
 Complex conjugate()
          Conjugats the complex number
 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.
 Complex div(double d)
          Devide the complex number by a double value.
 boolean equals(Complex b, double tolerance)
          Compares this object with the Complex number given as parameter
 Complex exp()
          Returns e to the power of the complex number
 double im()
          Returns the imaginary component of this object
 Complex invert()
           
 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.
 double mag()
          Returns the magnitude of the complex number
 Complex mul(Complex b)
          Multiplies the complex number with another complex value.
 Complex mul(double b)
          Multiplies the complex number with a double value.
 Complex neg()
          Returns the negative value of this complex number.
static Complex parseComplex(java.lang.String s)
          Convert text representation to a Complex.
 Complex polar()
          Convert Cartesian to polar
 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
 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 subtract(Complex z)
          Returns the subtraction of complex z from a complex number
 Complex subtract(double d)
          Subtracts the double d from the complex number Returns the subtraction of complex z from a complex number
 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:
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, 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.

Parameters:
re_in -

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.

Parameters:
re_in -

Complex

public Complex(Complex z)
Copy constructor

Parameters:
z -

Complex

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

Parameters:
re_in -
im_in -
Method Detail

polar

public Complex polar()
Convert Cartesian to polar


cartesian

public Complex cartesian()
Convert polar to Cartesian


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.

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

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).


mag

public double mag()
Returns the magnitude of the complex number


arg

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


parseComplex

public static Complex parseComplex(java.lang.String s)
Convert text representation to a Complex. input format (real_double,imaginary_double)


add

public Complex add(Complex z)
Returns the sum of two complex numbers


add

public Complex add(double d)
Returns the sum of a complex number and a double


subtract

public Complex subtract(Complex z)
Returns the subtraction of complex z from a complex number


subtract

public Complex subtract(double d)
Subtracts the double d from the complex number Returns the subtraction of complex z from a complex number


neg

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


mul

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

Returns:
The result of the multiplication

mul

public Complex mul(Complex b)
Multiplies 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.


div

public Complex div(double d)
Devide the complex number by a double value.

Returns:
The result of the division

invert

public Complex invert()

conjugate

public Complex conjugate()
Conjugats the complex number


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 esthetics 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


exp

public Complex exp()
Returns e to the power of the complex number


log

public Complex log()
Returns the logarithm of this complex number.


sqrt

public Complex sqrt()
Calculates the square root of this object.


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()

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.