|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object ca.uwaterloo.alumni.dwharder.Numbers.Complex
public final class Complex
A class implementing complex numbers using pairs of doubles.
This package allows the user to construct, manipulate, and model with complex numbers. A complex number is of the form a + ib where a, b are real numbers. The symbol i commutes with real numbers and obeys the multiplication rule i2 = -1.
In the description of each non-static method, this complex number is represented by z = a + ib. The coefficients a and ib are the real and imaginary parts of z, respectively. The parts a and ib will also be referred to as the 0th and 1st terms of the complex number z, respectively.
Any other complex number is represented by w = c + id. The variables x and n are used to represent real numbers and integers, respectively.
Instances of this class are immutable.
This class returns correct answers on branch cuts. Additionally, this class tries to give reasonable treatment to ∞ and NaN. For example, ONE.multiply(Double.POSITIVE_INFINITY) returns ∞ + i0. Most implementations would return ∞ + iNaN because they would simply multiply both the real part and the imaginary part by the multiplier. This result is probably not the desired result.
This class is based on the methods found in the Java classes Double, Math, and java.math.BigDecimal.
There is very little interaction between the classes in this package. If there is a significant request for such behaviour, it will be added (seemlessly) in the future.
Double
,
Math
,
Serialized FormField Summary | |
---|---|
static Complex |
I
The constant i. |
static Complex |
NaN
The constant NaN + iNaN. |
static Complex |
ONE
The constant 1. |
static Complex |
ZERO
The constant 0. |
Constructor Summary | |
---|---|
Complex(double b)
Generates the imaginary complex number 0 + ib. |
|
Complex(double[] v)
Generates a complex number based on the entries of the array v. |
|
Complex(double a,
double b)
Generates the complex number a + ib. |
Method Summary | |
---|---|
double |
abs()
Returns the absolute value of this complex number z. |
double |
abs2()
Returns the square of the absolute value of this complex number z. |
Complex |
acos()
Returns the principal inverse cosine of this complex number z. |
Complex |
acosh()
Returns the principal inverse hyperbolic cosine of this complex number z. |
Complex |
acot()
Returns the principal inverse cotangent of this complex number z. |
Complex |
acoth()
Returns the principal inverse hyperbolic cotangent of this complex number z. |
Complex |
acsc()
Returns the principal inverse cosecant of this complex number z. |
Complex |
acsch()
Returns the principal inverse hyperbolic cosecant of this complex number z. |
Complex |
add(Complex w)
Returns the sum of this complex number z and the complex number w = c + id. |
Complex |
add(double x)
Returns the sum of this complex number z and the real number x. |
Complex |
add(int n,
double x)
Returns this complex number z with x added onto its nth term. |
Complex |
addI(double x)
Returns the sum of this complex number z and the imaginary number ix. |
double |
argument()
Returns the argument of this complex number z. |
Complex |
asec()
Returns the principal inverse secant of this complex number z. |
Complex |
asech()
Returns the principal inverse hyperbolic secant of this complex number z. |
Complex |
asin()
Returns the principal inverse sine of this complex number z. |
Complex |
asinh()
Returns the principal inverse hyperbolic sine of this complex number z. |
Complex |
atan()
Returns the principal inverse tangent of this complex number z. |
Complex |
atanh()
Returns the principal inverse hyperbolic tangent of this complex number z. |
Complex |
ceil()
Returns the ceiling of each of the parts of this complex number z. |
double |
coefficient(int n)
Returns the coefficient of the nth term of this complex number z. |
Complex |
conjugate()
Returns the complex conjugate of this complex number z. |
Complex |
cos()
Returns the cosine of this complex number z. |
Complex |
cosh()
Returns the hyperbolic cosine of this complex number z. |
Complex |
cot()
Returns the cotangent of this complex number z. |
Complex |
coth()
Returns the hyperbolic cotangent of this complex number z. |
Complex |
csc()
Returns the cosecant of this complex number z. |
Complex |
csch()
Returns the hyperbolic cosecant of this complex number z. |
double |
csgn()
Returns the sign of the real part of this complex number z when |z| != 0, and 0.0 otherwise. |
Complex |
divide(Complex w)
Returns the quotient of this complex number z over the complex number w = c + id. |
boolean |
equals(java.lang.Object w)
This complex number z equals the complex number w = c + id if a == c and b == d. |
boolean |
equals(java.lang.Object z,
double eps)
Returns true if this complex number z equals the complex number w = c + id. |
Complex |
exp()
Returns Euler's number (e) raised to the power of this complex number z. |
Complex |
floor()
Returns the floor of each of the parts of this complex number z. |
int |
hashCode()
Returns a hash code for the this complex number z based on the real and imaginary parts. |
Complex |
horner(Complex[] v)
Evaluate the polynomial with complex coefficients, given by the array of complex numbers v, at this complex number z. |
Complex |
horner(double[] v)
Evaluate the polynomial with real coefficients, given by the array of doubles v, at this complex number z. |
Complex |
imag()
Returns the imaginary part of this complex number z. |
double |
imagI()
Returns the coefficient of i of this complex number z. |
static java.lang.String |
imaginaryUnit(java.lang.String s)
Set the character to be used for the imaginary unit. |
boolean |
isImaginary()
Returns true if the real part of this complex number z is zero, otherwise false. |
boolean |
isInfinite()
Returns true if either part of this complex number z is infinite, otherwise false. |
boolean |
isNaN()
Returns true if either coefficient of this complex number z is not-a-number (NaN), otherwise false. |
boolean |
isReal()
Returns true if the coefficient of the imaginary part of this complex number z is zero, otherwise false. |
boolean |
isZero()
Returns true if both coefficients of this complex number z are zero, otherwise false. |
Complex |
log()
Returns the natural logarithm of this complex number z. |
Complex |
log10()
Returns the logarithm base 10 of this complex number z. |
Complex |
multiply(Complex w)
Returns the product of this complex number z and the complex number w = c + id. |
Complex |
multiply(double x)
Returns the product of this complex number z and the real number x. |
Complex |
multiplyI(double x)
Returns the product of this complex number z and the imaginary number ix. |
Complex |
negate()
Returns the negative of this complex number z. |
Complex |
pow(Complex w)
|
Complex |
pow(double x)
Returns this complex number z raised to the real number x. |
Complex |
pow(int n)
Returns this complex number z raised to the integer n. |
Complex |
project(boolean[] v)
Project this complex number z onto the subspace defined by the boolean vector v. |
Complex |
project(int n)
Project this complex number z onto the nth term. |
static Complex |
random()
A factory method for constructing a complex number with a random real and imaginary parts. |
Complex |
random(boolean[] v)
A factory method to create a random complex number defined by the boolean vector v. |
static Complex |
random(int n)
A factory method for constructing a complex number with a random nth coefficient. |
static Complex |
randomImaginary()
A factory method for constructing a complex number with a random imaginary part. |
static Complex |
randomReal()
A factory method for constructing a complex number with a random real part. |
double |
real()
Returns the real part of this complex number z. |
Complex |
rint()
Returns each part of the complex number z rounded to the nearest double equal to an integer. |
Complex |
sec()
Returns the secant of this complex number z. |
Complex |
sech()
Returns the hyperbolic secant of this complex number z. |
Complex |
signum()
Returns a normalized form of this complex number z when it is non-zero and z otherwise. |
Complex |
sin()
Returns the sine of this complex number z. |
Complex |
sinh()
Returns the hyperbolic sine of this complex number z. |
Complex |
sqr()
Returns the square of this complex number z. |
Complex |
sqrt()
Returns the square root of this complex number z. |
Complex |
subtract(Complex w)
Returns the difference between this complex number z and the complex number w = c + id. |
Complex |
tan()
Returns the tangent of this complex number z. |
Complex |
tanh()
Returns the hyperbolic tangent of this complex number z. |
double[] |
toArray()
Convert this complex number z to an array of doubles. |
double[] |
toArray(byte[] v)
Convert this complex number z to an array of doubles. |
java.lang.String |
toMathMLContent()
Returns this complex number z as a string using content MathML. |
java.lang.String |
toMathMLPresentation()
Returns this complex number z as a string using presentation MathML. |
java.lang.String |
toString()
Returns a string representing this complex number z. |
Methods inherited from class java.lang.Object |
---|
getClass, notify, notifyAll, wait, wait, wait |
Field Detail |
---|
public static final Complex ZERO
public static final Complex ONE
public static final Complex I
public static final Complex NaN
Constructor Detail |
---|
public Complex(double a, double b)
Generates the complex number a + ib.
a
- the real partb
- the coefficient of ipublic Complex(double b)
Generates the imaginary complex number 0 + ib.
b
- the coefficient of ipublic Complex(double[] v)
Generates a complex number based on the entries of the array v.
An array v of length 2 defines the complex number v0 + iv1, and
an array v of length 1 defines the complex number 0 + iv1.
v
- array of length 1 or 2Method Detail |
---|
public static final Complex random()
A factory method for constructing a complex number with a random real and imaginary parts.
Math.random()
public static final Complex random(int n)
A factory method for constructing a complex number with a random nth coefficient.
Math.random()
public final Complex random(boolean[] v)
A factory method to create a random complex number defined by the boolean vector v.
In the result, the coefficient of the nth term is a random double if vn == true.
v
- an array indicating a subspace
Math.random()
public static final Complex randomReal()
A factory method for constructing a complex number with a random real part.
Math.random()
public static final Complex randomImaginary()
A factory method for constructing a complex number with a random imaginary part.
Math.random()
public final boolean equals(java.lang.Object w)
This complex number z equals the complex number w = c + id if a == c and b == d.
Note: NaN == NaN returns false but 0.0 == -0.0 returns true.
equals
in class java.lang.Object
public final boolean equals(java.lang.Object z, double eps)
Returns true if this complex number z equals the complex number w = c + id.
Two nonzero complex numbers o and p are equal up to ε if |z - w|/min( |z|, |w| ) < ε.
If z = 0 or w = 0 then we check that |z| < ε or |w| < ε, respectively.
eps
- the allowable relative difference: greater than or equal to 0
public final int hashCode()
Returns a hash code for the this complex number z based on the real and imaginary parts.
hashCode
in class java.lang.Object
public final java.lang.String toString()
Returns a string representing this complex number z.
If N1 = abs(a) and N2 = abs(b), then the return value is "[-]N1 (+|-) iN2".
The user can force the use of 'j' instead of 'i' as the imaginary unit by Complex.imaginaryUnit('j').
toString
in class java.lang.Object
public static final java.lang.String imaginaryUnit(java.lang.String s)
Set the character to be used for the imaginary unit.
For example, "j"
.
public final java.lang.String toMathMLContent()
Returns this complex number z as a string using content MathML.
public final java.lang.String toMathMLPresentation()
Returns this complex number z as a string using presentation MathML.
public final double[] toArray()
Convert this complex number z to an array of doubles.
public final double[] toArray(byte[] v)
Convert this complex number z to an array of doubles.
The coefficient placed into the nth entry of the array is determined by vn.
v
- an array of 0 or 1 indicating which coefficient is to be placed into the nth entry of the array
public final boolean isZero()
public final boolean isReal()
public final boolean isImaginary()
public final boolean isNaN()
Double.isNaN(double)
public final boolean isInfinite()
Double.isInfinite(double)
public final double abs()
Returns the absolute value of this complex number z.
abs(z) = |z| = sqrt(a2 + b2)
Special cases:
public final double abs2()
Returns the square of the absolute value of this complex number z.
abs2(z) = |z|2 = a2 + b2
Special cases:
public final double real()
Returns the real part of this complex number z.
real(z) = a.
public final Complex imag()
Returns the imaginary part of this complex number z.
imag(z) = 0 + ib.
To be consistent with all other classes in this package, this routine returns a complex number. To get the double value, use imagI
public final double imagI()
Returns the coefficient of i of this complex number z.
imagI(z) = b
public final double coefficient(int n)
Returns the coefficient of the nth term of this complex number z.
n
- the number (0 or 1) of the coefficient to return
public final Complex project(int n)
Project this complex number z onto the nth term.
n
- the number of term (0 or 1) to be projected onto
public final Complex project(boolean[] v)
Project this complex number z onto the subspace defined by the boolean vector v.
In the result, the coefficient of the nth term is set to 0.0 if vn == false.
v
- an array indicating the terms to project onto
public final Complex conjugate()
Returns the complex conjugate of this complex number z.
conjugate(z) = a - ib
public final double argument()
Returns the argument of this complex number z.
This is a value on the interval (π, &pi] and z = |z|eiargument(z).
argument(z) = atan2( b, a )
public final Complex signum()
Returns a normalized form of this complex number z when it is non-zero and z otherwise.
public final double csgn()
Returns the sign of the real part of this complex number z when |z| != 0, and 0.0 otherwise.
Special cases:
public final Complex add(Complex w)
Returns the sum of this complex number z and the complex number w = c + id.
z + w = (a + c) + i(b + d)
public final Complex add(int n, double x)
Returns this complex number z with x added onto its nth term.
n
- the number (0, 1, 2, ..., 15) of the onto which to add x
public final Complex add(double x)
Returns the sum of this complex number z and the real number x.
z + x = (a + x) + ib
public final Complex addI(double x)
Returns the sum of this complex number z and the imaginary number ix.
z + ix = a + i(b + x)
public final Complex subtract(Complex w)
Returns the difference between this complex number z and the complex number w = c + id.
z - w = (a - c) + i(b - d)
public final Complex negate()
Returns the negative of this complex number z.
-z = -a - ib
public final Complex multiply(Complex w)
Returns the product of this complex number z and the complex number w = c + id.
z w = (ac - bd) + i(ad + bc)
Multiplication is distributive with the following multiplication matrix for 1 and the imaginary unit:
× | 1 | i |
1 | 1 | i |
i | i | -1 |
There are numerous special cases when any component is NaN or infinite.
public final Complex multiply(double x)
Returns the product of this complex number z and the real number x.
z x = ax + ibx
Special cases:
public final Complex multiplyI(double x)
Returns the product of this complex number z and the imaginary number ix.
z ix = -bx + iax
Special cases:
public final Complex divide(Complex w)
Returns the quotient of this complex number z over the complex number w = c + id.
z/w = (ac + bd)/|w|2 + i(-ad + bc)|w|2.
There are numerous special cases when any component is NaN or infinite.
public final Complex pow(Complex w)
public final Complex pow(double x)
Returns this complex number z raised to the real number x.
public final Complex pow(int n)
Returns this complex number z raised to the integer n.
public final Complex sqr()
Returns the square of this complex number z.
public final Complex horner(double[] v)
Evaluate the polynomial with real coefficients, given by the array of doubles v, at this complex number z.
If n = v.length - 1 then this method evaluates v0 + v1z + ... + vnzn using Horner's rule.
public final Complex horner(Complex[] v)
Evaluate the polynomial with complex coefficients, given by the array of complex numbers v, at this complex number z.
If n = v.length - 1 then this method evaluates v0 + v1z + ... + vnzn using Horner's rule.
This method is not defined for other classes due to non-commutativity.
public final Complex sqrt()
Returns the square root of this complex number z.
The branch cut is on (-∞, 0).
public final Complex exp()
Returns Euler's number (e) raised to the power of this complex number z.
ez = ea(cos(b) + isin(b))
public final Complex log()
Returns the natural logarithm of this complex number z.
public final Complex log10()
Returns the logarithm base 10 of this complex number z.
public final Complex sin()
Returns the sine of this complex number z.
public final Complex cos()
Returns the cosine of this complex number z.
public final Complex tan()
Returns the tangent of this complex number z.
public final Complex sec()
Returns the secant of this complex number z.
public final Complex csc()
Returns the cosecant of this complex number z.
public final Complex cot()
Returns the cotangent of this complex number z.
public final Complex sinh()
Returns the hyperbolic sine of this complex number z.
public final Complex cosh()
Returns the hyperbolic cosine of this complex number z.
public final Complex tanh()
Returns the hyperbolic tangent of this complex number z.
public final Complex sech()
Returns the hyperbolic secant of this complex number z.
public final Complex csch()
Returns the hyperbolic cosecant of this complex number z.
public final Complex coth()
Returns the hyperbolic cotangent of this complex number z.
public final Complex asin()
Returns the principal inverse sine of this complex number z.
The branch cuts are (-∞, -1) and (1, ∞).
public final Complex acos()
Returns the principal inverse cosine of this complex number z.
The branch cuts are (-∞, -1) and (1, ∞).
public final Complex atan()
Returns the principal inverse tangent of this complex number z.
The branch cuts are (-i∞, -i] and [i, i∞).
public final Complex asec()
Returns the principal inverse secant of this complex number z.
The branch cut is (-1, 1).
public final Complex acsc()
Returns the principal inverse cosecant of this complex number z.
The branch cut is (-1, 1).
public final Complex acot()
Returns the principal inverse cotangent of this complex number z.
The branch cut is (-i, i).
public final Complex asinh()
Returns the principal inverse hyperbolic sine of this complex number z.
The branch cuts are (-i∞, -i) and (i, i∞).
public final Complex acosh()
Returns the principal inverse hyperbolic cosine of this complex number z.
The branch cut is (-∞, 1).
public final Complex atanh()
Returns the principal inverse hyperbolic tangent of this complex number z.
The branch cuts are (-∞, -1] and [1, ∞).
public final Complex asech()
Returns the principal inverse hyperbolic secant of this complex number z.
The branch cuts are (-∞, 0] and (1, ∞).
public final Complex acsch()
Returns the principal inverse hyperbolic cosecant of this complex number z.
The branch cut is (-i, i).
public final Complex acoth()
Returns the principal inverse hyperbolic cotangent of this complex number z.
The branch cut is [-1, 1].
public final Complex ceil()
Returns the ceiling of each of the parts of this complex number z.
Math.ceil(double)
public final Complex floor()
Returns the floor of each of the parts of this complex number z.
Math.floor(double)
public final Complex rint()
Returns each part of the complex number z rounded to the nearest double equal to an integer.
Math.rint(double)
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |