public class Complex extends Object implements Serializable
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.
Constructor and Description |
---|
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(Number re_in)
Construct from a Number.
|
Modifier and Type | Method and Description |
---|---|
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
|
boolean |
equals(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(Number r,
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
|
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() |
String |
toString()
Returns the value of this complex number as a string in the format:
|
String |
toString(NumberFormat format) |
String |
toString(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
|
public Complex()
public Complex(double re_in)
public Complex(Number re_in)
public Complex(Complex z)
public Complex(double re_in, double im_in)
public double re()
public double im()
public void set(Complex z)
public void set(double re_in, double im_in)
public void setRe(double re_in)
public void setIm(double im_in)
public boolean equals(Complex b, double tolerance)
b. The
toleranceparameter is the radius within which the
bnumber must lie for the two complex numbers to be considered equal.
trueif the complex number are considered equal,
falseotherwise.
public boolean equals(Object o)
public int hashCode()
public String toString()
(real, imaginary).
public String toString(NumberFormat format)
public String toString(NumberFormat format, boolean flag)
public boolean isInfinite()
public boolean isNaN()
public double abs()
Adapted from Numerical Recipes in C -
The Art of Scientific Computing
ISBN 0-521-43108-5
public double abs2()
public double arg()
public Complex neg()
public Complex mul(double b)
public Complex add(Complex b)
public Complex sub(Complex b)
public Complex mul(Complex b)
public Complex div(Complex b)
public Complex power(double exponent)
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 occuring 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 [email protected] .
public Complex power(Complex exponent)
public Complex conj()
public Complex log()
public Complex sqrt()
public Complex sin()
public Complex cos()
public Complex tan()
public Complex asin()
public Complex acos()
public Complex atan()
public Complex sinh()
public Complex cosh()
public Complex tanh()
public Complex asinh()
public Complex acosh()
public Complex atanh()
public static Complex polarValueOf(Number r, Number theta)
r
- The radiustheta
- The anglepublic double doubleValue()
public float floatValue()
public int intValue()
public long longValue()
Jas4pp 1.5 © Java Analysis Studio for Particle Physics