Class Complex
- java.lang.Object
-
- com.singularsys.jep.standard.Complex
-
- All Implemented Interfaces:
java.io.Serializable
- Direct Known Subclasses:
Complex.NonPropagatingImmutableComplex
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., 2.3.0 beta 2 does not implement Number anymore, as caused too many problems., 4.0.0 new power(int) methods, 4.0.0 ImmutableComplex and NonPropogatingImmutableComplex sub classes
- Author:
- Nathan Funk
- See Also:
example application using complex calculation
,speed test applications using complex number
, Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
Complex.NonPropagatingImmutableComplex
An immutable version of the Complex class.
-
Constructor Summary
Constructors Constructor Description Complex()
Default constructor, gives zero.Complex(double x)
Constructor from a single double value.Complex(double x, double y)
Initialize the real and imaginary components to the values given by the parameters.Complex(Complex z)
Copy constructorComplex(java.lang.Number x)
Construct from a Number.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method 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()
Returns the arccos of this complex number.Complex
acosh()
Returns the inverse hyperbolic cosine of this complex number.Complex
add(Complex z)
Adds the complex number with another complex value.double
arg()
Returns the argument of this complex number (Math.atan2(re,im))Complex
asin()
Returns the arcsin of this complex number.Complex
asinh()
Returns the inverse hyperbolic sine of this complex number.Complex
atan()
Returns the arc tangent of this complex number.Complex
atanh()
Returns the inverse hyperbolic tangent of this complex number.Complex
conj()
Returns the complex conjugate.Complex
cos()
Returns the cosine of this complex number.Complex
cosh()
Returns the hyperbolic cosine of this complex number.Complex
div(Complex z)
Returns the result of dividing this complex number by the parameter.double
doubleValue()
Returns real part.boolean
eq(Complex z)
An equals method compatible with double ==boolean
equals(Complex w, double tolerance)
Compares this object with the Complex number given as parameterboolean
equals(java.lang.Object o)
Compares this object against the specified object.Complex
fastPower(int n)
Calculate integer powers by repeated multiplications.float
floatValue()
Returns real part.int
hashCode()
Always override hashCode when you override equals.double
im()
Returns the imaginary component of this objectint
intValue()
Returns real part.boolean
isInfinite()
Returnstrue
if either the real or imaginary component of thisComplex
is an infinite value.boolean
isNaN()
Returnstrue
if either the real or imaginary component of thisComplex
is a Not-a-Number (NaN
) value.Complex
log()
Returns the logarithm of this complex number.long
longValue()
Returns real part.Complex
mul(double b)
Multiply the complex number with a double value.Complex
mul(Complex z)
Multiply the complex number with another complex 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(double exponent)
Returns this complex number raised to a double argument.Complex
power(int n)
Raise this complex to an integer power.Complex
power(Complex exponent)
Returns the value of this complex number raised to the power of a complex exponent If the this is zero return this.Complex
powerD(double exponent)
Returns the value of this complex number raised to the power of a real component (in double precision).Complex
powerI(int n)
Raise a complex number to an integer power.double
re()
Returns the real component of this objectComplex
reciprocal()
Returns the reciprocal of a complex number 1/z.void
set(double x, double y)
Sets the real and imaginary values of the object.void
set(Complex z)
Copies the values from the parameter object to this objectvoid
setIm(double y)
Sets the imaginary component of the objectvoid
setRe(double x)
Sets the real component of the objectComplex
sin()
Returns the sine of this complex number.Complex
sinh()
Returns the hyperbolic sine of this complex number.Complex
sqrt()
Calculates the square root of this object.Complex
sub(Complex z)
Subtracts a complex number from thisComplex
tan()
Returns the tangent of this complex number.Complex
tanh()
Returns the hyperbolic tangent of this complex number.java.lang.String
toString()
Returns the value of this complex number as a string in the format:java.lang.String
toString(boolean withI, boolean brackets)
Format complex number with the a the x+y i notation with optional brackets.java.lang.String
toString(java.text.NumberFormat format)
Print complex number in standard format with a number format applied to individual components.java.lang.String
toString(java.text.NumberFormat format, boolean brackets)
Prints using specified number format in format or "2" or "3 i" or "(2+3 i)" if brackets is true or "2+3 i" if brackets is false.
-
-
-
Constructor Detail
-
Complex
public Complex()
Default constructor, gives zero.
-
Complex
public Complex(double x)
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:
x
- real part
-
Complex
public Complex(java.lang.Number x)
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:
x
- number representing real value
-
Complex
public Complex(Complex z)
Copy constructor- Parameters:
z
- complex number to copy from
-
Complex
public Complex(double x, double y)
Initialize the real and imaginary components to the values given by the parameters.- Parameters:
x
- real componenty
- imaginary component
-
-
Method Detail
-
re
public double re()
Returns the real component of this object- Returns:
- the real part
-
im
public double im()
Returns the imaginary component of this object- Returns:
- the imaginary part
-
set
public void set(Complex z)
Copies the values from the parameter object to this object- Parameters:
z
- complex number to copy from
-
set
public void set(double x, double y)
Sets the real and imaginary values of the object.- Parameters:
x
- real party
- imaginary part
-
setRe
public void setRe(double x)
Sets the real component of the object- Parameters:
x
- real part
-
setIm
public void setIm(double y)
Sets the imaginary component of the object- Parameters:
y
- imaginary part
-
equals
public boolean equals(Complex w, double tolerance)
Compares this object with the Complex number given as parameterb
. Thetolerance
parameter is the radius within which theb
number must lie for the two complex numbers to be considered equal.- Parameters:
w
- complex number to compare withtolerance
- distance to consider points as equals- Returns:
true
if the complex number are considered equal,false
otherwise.
-
eq
public boolean eq(Complex z)
An equals method compatible with double ==- Parameters:
z
- complex number to test- Returns:
- true if real parts are equals and complex parts are equals
- Since:
- 3.5
-
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.equals() applies to each field:- If d1 and d2 both represent Double.NaN, then the equals method returns true, even though Double.NaN==Double.NaN has the value false.
- If d1 represents +0.0 while d2 represents -0.0, or vice versa, the equal test has the value false, even though +0.0==-0.0 has the value true.
- Overrides:
equals
in classjava.lang.Object
- Parameters:
o
- object to test against- Returns:
- true is this equals o
- 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 classjava.lang.Object
- Returns:
- a hashcode
-
toString
public java.lang.String toString()
Returns the value of this complex number as a string in the format:(real, imaginary)
. Uses thestandard.Complex.ToStringFormat
message property- Overrides:
toString
in classjava.lang.Object
- Returns:
- string representation
-
toString
public java.lang.String toString(boolean withI, boolean brackets)
Format complex number with the a the x+y i notation with optional brackets. The following message properties are usedstandard.Complex.ToStringNoBracketsRealWithI={0}
used when imaginary part is zero and real part positive or zerostandard.Complex.ToStringNoBracketsNegRealWithI=-{0}
used when imaginary part is zero and real part negative (abs(re) passed to format)standard.Complex.ToStringNoBracketsImaginaryWithI={1} i
used when imaginary part is zerostandard.Complex.ToStringNoBracketsImaginaryWithNegI=-{1} i
when real part is zero and imaginary part is negative (abs(im) passed to format)standard.Complex.ToStringNoBracketsWithI={0}+{1} i
with positive imaginary componentstandard.Complex.ToStringNoBracketsWithNegI={0}-{1} i
with negative imaginary componentstandard.Complex.ToStringBracketsRealWithI={0}
used when real part is positive and the imaginary part is zerostandard.Complex.ToStringBracketsNegRealWithI=(-{0})
used when imaginary part is zero and real part negative (abs(re) passed to format)standard.Complex.ToStringBracketsImaginaryWithI={1} i
used when imaginary part is zerostandard.Complex.ToStringBracketsImaginaryWithNegI=(-{1} i)
used when imaginary part is negativestandard.Complex.ToStringBracketsWithI=({0}+{1} i)
with positive imaginary componentstandard.Complex.ToStringBracketsWithNegI=({0}-{1} i)
with negative imaginary component (abs(im) passed to format)
- Parameters:
withI
- whether to use the x+yi format, if false is the same as the standard toString methodbrackets
- whether to enclose expressions in brackets if needed to prevent ambiguity, behaviour may change depending on message properties- Returns:
- string rep
-
toString
public java.lang.String toString(java.text.NumberFormat format)
Print complex number in standard format with a number format applied to individual components. This methods relies on propertiesstandard.Complex.ToStringWithNumberFormat=({0}, {1})
- Parameters:
format
- NumberFormat to apply to individual components- Returns:
- string representation
-
toString
public java.lang.String toString(java.text.NumberFormat format, boolean brackets)
Prints using specified number format in format or "2" or "3 i" or "(2+3 i)" if brackets is true or "2+3 i" if brackets is false. This methods relies on propertiesstandard.Complex.ToStringNoBracketsRealWithI={0}
used when imaginary part is zero and real part positive or zerostandard.Complex.ToStringNoBracketsNegRealWithI=-{0}
used when imaginary part is zero and real part negative (abs(re) passed to format)standard.Complex.ToStringNoBracketsImaginaryWithI={1} i
used when imaginary part is zerostandard.Complex.ToStringNoBracketsImaginaryWithNegI=-{1} i
when real part is zero and imaginary part is negative (abs(im) passed to format)standard.Complex.ToStringNoBracketsWithI={0}+{1} i
with positive imaginary componentstandard.Complex.ToStringNoBracketsWithNegI={0}-{1} i
with negative imaginary componentstandard.Complex.ToStringBracketsRealWithI={0}
used when real part is positive and the imaginary part is zerostandard.Complex.ToStringBracketsNegRealWithI=(-{0})
used when imaginary part is zero and real part negative (abs(re) passed to format)standard.Complex.ToStringBracketsImaginaryWithI={1} i
used when imaginary part is zerostandard.Complex.ToStringBracketsImaginaryWithNegI=(-{1} i)
used when imaginary part is negativestandard.Complex.ToStringBracketsWithI=({0}+{1} i)
with positive imaginary componentstandard.Complex.ToStringBracketsWithNegI=({0}-{1} i)
with negative imaginary component (abs(im) passed to format)
- Parameters:
format
- the NumberFormat to use, no sign or negative format should be usedbrackets
- whether to enclose expressions in brackets if needed to prevent ambiguity, behaviour may change depending on message properties- Returns:
- string rep
-
isInfinite
public boolean isInfinite()
Returnstrue
if either the real or imaginary component of thisComplex
is an infinite value.- Returns:
true
if either component of theComplex
object is infinite;false
, otherwise.
-
isNaN
public boolean isNaN()
Returnstrue
if either the real or imaginary component of thisComplex
is a Not-a-Number (NaN
) value.- Returns:
true
if either component of theComplex
object isNaN
;false
, otherwise.
-
abs
public double abs()
Returns the absolute value of the complex number.- Returns:
- the absolute value
-
abs2
public double abs2()
Returns the square of the absolute value (re*re+im*im).- Returns:
- square of absolute value
-
arg
public double arg()
Returns the argument of this complex number (Math.atan2(re,im))- Returns:
- the argument
-
neg
public Complex neg()
Returns the negative value of this complex number.- Returns:
- -(x+i y)
-
mul
public Complex mul(double b)
Multiply the complex number with a double value.- Parameters:
b
- real number to multiply by- Returns:
- The result of the multiplication
-
add
public Complex add(Complex z)
Adds the complex number with another complex value.- Parameters:
z
- complex number to add- Returns:
- The result of the addition
- Since:
- 2.3.0.1
-
sub
public Complex sub(Complex z)
Subtracts a complex number from this- Parameters:
z
- complex number to subtract- Returns:
- (this) - z
- Since:
- 2.3.0.1
-
mul
public Complex mul(Complex z)
Multiply the complex number with another complex value.- Parameters:
z
- complex number to multiply- Returns:
- The result of the multiplication
-
div
public Complex div(Complex z)
Returns the result of dividing this complex number by the parameter. Algorithm adapted from Numerical Recipes in C - The Art of Scientific Computing ISBN 0-521-43108-5- Parameters:
z
- complex number to divide by- Returns:
- (this)/z
-
reciprocal
public Complex reciprocal()
Returns the reciprocal of a complex number 1/z. This was broken in version < 4.0.- Returns:
- 1/(this)
-
power
public Complex power(int n)
Raise this complex to an integer power. For small power use thefastPower(int)
method which does repeated multiplication and is generally faster. For larger power usepowerI(int)
which can be more accurate.- Parameters:
n
- the power- Returns:
- (this)^n
-
powerI
public Complex powerI(int n)
Raise a complex number to an integer power. Uses the polar form[r;th]^n -> [r^n;n th]
and the efficient power method fromPower.power(double, int)
Slower than- Parameters:
n
-- Returns:
-
fastPower
public Complex fastPower(int n)
Calculate integer powers by repeated multiplications. Soz^5 = z*(z*z)*(z*z)
. This routine is generally faster but can accumulate errors for large exponents.- Parameters:
n
-- Returns:
-
powerD
public Complex powerD(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 [email protected] .
Testing with roots of unity show errors are typically within 128 * ulp, so |actual-result|/|actual| < 1e-13.
- Parameters:
exponent
- exponent- Returns:
- (this)^exponent
-
power
public Complex power(double exponent)
Returns this complex number raised to a double argument. If the exponent is an integer returnspower(int)
otherwise callspowerD(double)
.- Parameters:
exponent
-- Returns:
- this^exponent
-
power
public Complex power(Complex exponent)
Returns the value of this complex number raised to the power of a complex exponent If the this is zero return this. If the exponent has zero imaginary part use- Parameters:
exponent
- exponent- Returns:
- (this)^exponent
-
conj
public Complex conj()
Returns the complex conjugate.- Returns:
- (x-i y)
-
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.- Returns:
- ln(z) = ln(r) + i th
-
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)- Returns:
- sqrt(z)
-
sin
public Complex sin()
Returns the sine of this complex number.- Returns:
- sin(this)
-
cos
public Complex cos()
Returns the cosine of this complex number.- Returns:
- cos(this)
-
tan
public Complex tan()
Returns the tangent of this complex number.- Returns:
- tan(this)
-
asin
public Complex asin()
Returns the arcsin of this complex number.- Returns:
- asin(this)
-
acos
public Complex acos()
Returns the arccos of this complex number.- Returns:
- acos(this)
-
atan
public Complex atan()
Returns the arc tangent of this complex number.- Returns:
- atan(this)
-
sinh
public Complex sinh()
Returns the hyperbolic sine of this complex number.- Returns:
- sinh(this)
-
cosh
public Complex cosh()
Returns the hyperbolic cosine of this complex number.- Returns:
- cosh(this)
-
tanh
public Complex tanh()
Returns the hyperbolic tangent of this complex number.- Returns:
- tanh(this)
-
asinh
public Complex asinh()
Returns the inverse hyperbolic sine of this complex number.- Returns:
- asinh(this)
-
acosh
public Complex acosh()
Returns the inverse hyperbolic cosine of this complex number.- Returns:
- acosh(this)
-
atanh
public Complex atanh()
Returns the inverse hyperbolic tangent of this complex number.- Returns:
- atanh(this)
-
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 radiustheta
- The angle- Returns:
- The complex result.
- Since:
- 2.3.0.1
-
doubleValue
public double doubleValue()
Returns real part.- Returns:
- real part
- Since:
- 2.3.0.1
-
floatValue
public float floatValue()
Returns real part.- Returns:
- real part converted to a float
- Since:
- 2.3.0.1
-
intValue
public int intValue()
Returns real part.- Returns:
- real part converted to an int
- Since:
- 2.3.0.1
-
longValue
public long longValue()
Returns real part.- Returns:
- real part converted to a long
- Since:
- 2.3.0.1
-
-