Class Complex
- All Implemented Interfaces:
Serializable
- Direct Known Subclasses:
Complex.NonPropagatingImmutableComplex
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 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. see com.singularsys.jepexamples.applets.Fractal example application, not included in GWTJep
- Version:
- 2.3.0 alpha now extends Number, has
add()
andsub()
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 NonPropagatingImmutableComplex subclasses - Author:
- Nathan Funk
- See Also:
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
An immutable version of the Complex class. -
Field Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptiondouble
abs()
Returns the absolute value of the complex number.double
abs2()
Returns the square of the absolute value (re*re+im*im).acos()
Returns the arccos of this complex number.acosh()
Returns the inverse hyperbolic cosine of this complex number.Adds the complex number with another complex value.double
arg()
Returns the argument of this complex number (Math.atan2(re,im))asin()
Returns the arcsin of this complex number.asinh()
Returns the inverse hyperbolic sine of this complex number.atan()
Returns the arc tangent of this complex number.atanh()
Returns the inverse hyperbolic tangent of this complex number.conj()
Returns the complex conjugate.cos()
Returns the cosine of this complex number.cosh()
Returns the hyperbolic cosine of this complex number.Returns the result of dividing this complex number by the parameter.double
Returns real part.boolean
An equals method compatible with double ==boolean
Compares this object with the Complex number given as parameterboolean
Compares this object against the specified object.fastPower
(int n) Calculate integer powers by repeated multiplications.float
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
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.log()
Returns the logarithm of this complex number.long
Returns real part.mul
(double b) Multiply the complex number with a double value.Multiply the complex number with another complex value.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).power
(double exponent) Returns this complex number raised to a double argument.power
(int n) Raise this complex to an integer power.Returns the value of this complex number raised to the power of a complex exponent If this is zero return this.powerD
(double exponent) Returns the value of this complex number raised to the power of a real component (in double precision).powerI
(int n) Raise a complex number to an integer power.double
re()
Returns the real component of this objectReturns the reciprocal of a complex number 1/z.void
set
(double x, double y) Sets the real and imaginary values of the object.void
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 objectsin()
Returns the sine of this complex number.sinh()
Returns the hyperbolic sine of this complex number.sqrt()
Calculates the square root of this object.Subtracts a complex number from thistan()
Returns the tangent of this complex number.tanh()
Returns the hyperbolic tangent of this complex number.toString()
Returns the value of this complex number as a string in the format:toString
(boolean withI, boolean brackets) Format complex number with the x+y i notation with optional brackets.toString
(NumberFormat format) Print complex number in standard format with a number format applied to individual components.toString
(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.
-
Field Details
-
ZERO
Constant 0+0 i -
ONE
Constant 1+0 i -
MINUS_ONE
Constant -1+0 i -
I
Constant 0+1 i -
MINUS_I
Constant 0-1 i
-
-
Constructor Details
-
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
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
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 Details
-
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
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
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
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
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.
-
hashCode
public int hashCode()Always override hashCode when you override equals. Effective Java, Joshua Bloch, Sun Press -
toString
Returns the value of this complex number as a string in the format:(real, imaginary)
. Uses thestandard.Complex.ToStringFormat
message property -
toString
Format complex number with 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
Print complex number in standard format with a number format applied to individual components. This method relies on propertiesstandard.Complex.ToStringWithNumberFormat=({0}, {1})
- Parameters:
format
- NumberFormat to apply to individual components- Returns:
- string representation
-
toString
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 method 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
Returns the negative value of this complex number.- Returns:
- -(x+i y)
-
mul
Multiply the complex number with a double value.- Parameters:
b
- real number to multiply by- Returns:
- The result of the multiplication
-
add
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
Subtracts a complex number from this- Parameters:
z
- complex number to subtract- Returns:
- (this) - z
- Since:
- 2.3.0.1
-
mul
Multiply the complex number with another complex value.- Parameters:
z
- complex number to multiply- Returns:
- The result of the multiplication
-
div
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
Returns the reciprocal of a complex number 1/z. This was broken in version < 4.0.- Returns:
- 1/(this)
-
power
Raise this complex to an integer power. For small power use thefastPower(int)
method which does not do repeated multiplication and is generally faster. For larger power usepowerI(int)
which can be more accurate.- Parameters:
n
- the power- Returns:
- (this)^n
-
powerI
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
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
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
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
Returns the value of this complex number raised to the power of a complex exponent If this is zero return this. If the exponent has zero imaginary part use- Parameters:
exponent
- exponent- Returns:
- (this)^exponent
-
conj
Returns the complex conjugate.- Returns:
- (x-i y)
-
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
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
Returns the sine of this complex number.- Returns:
- sin(this)
-
cos
Returns the cosine of this complex number.- Returns:
- cos(this)
-
tan
Returns the tangent of this complex number.- Returns:
- tan(this)
-
asin
Returns the arcsin of this complex number.- Returns:
- asin(this)
-
acos
Returns the arccos of this complex number.- Returns:
- acos(this)
-
atan
Returns the arc tangent of this complex number.- Returns:
- atan(this)
-
sinh
Returns the hyperbolic sine of this complex number.- Returns:
- sinh(this)
-
cosh
Returns the hyperbolic cosine of this complex number.- Returns:
- cosh(this)
-
tanh
Returns the hyperbolic tangent of this complex number.- Returns:
- tanh(this)
-
asinh
Returns the inverse hyperbolic sine of this complex number.- Returns:
- asinh(this)
-
acosh
Returns the inverse hyperbolic cosine of this complex number.- Returns:
- acosh(this)
-
atanh
Returns the inverse hyperbolic tangent of this complex number.- Returns:
- atanh(this)
-
polarValueOf
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
-