java.lang.Object | +--ORG.netlib.math.complex.Complex
A Java class for performing complex number arithmetic to double precision.
This applet has been adapted
from a Vector
Visualization applet by Vladimir Sorokin.
Copyright (c) 1997 - 2001, Alexander Anderson.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at
your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
USA.
The latest version of this Complex class is available from the Netlib Repository.
Here's an example of the style the class permits:
import ORG.netlib.math.complex.Complex;
public class Test {
public boolean isInMandelbrot (Complex c, int maxIter) { Complex z= new Complex(0, 0);
for (int i= 0; i < maxIter; i++) { z= z.mul(z).add(c); if (z.abs() > 2) return false; }
return true; }
}
Field Summary | |
static java.lang.String |
AUTHOR
|
static java.lang.String |
DATE
|
static Complex |
i
A constant representing i, the famous square root of -1. |
static java.lang.String |
REMARK
|
protected static double |
TWO_PI
Twice PI radians is the same thing as 360 degrees. |
static java.lang.String |
VERSION
|
Constructor Summary | |
Complex()
Constructs a Complex representing the number zero. |
|
Complex(Complex z)
Constructs a separate new Complex from an existing Complex. |
|
Complex(double re)
Constructs a Complex representing a real number. |
|
Complex(double re,
double im)
Constructs a Complex from real and imaginary parts. |
Method Summary | |
double |
abs()
Returns the magnitude of a Complex number. |
Complex |
acos()
Returns the principal arc cosine of a Complex number. |
Complex |
acosh()
Returns the principal inverse hyperbolic cosine of a Complex number. |
Complex |
add(Complex z)
To perform z1 + z2, you write z1.add(z2) . |
double |
arg()
Returns the principal angle of a Complex number, in radians, measured counter-clockwise from the real axis. |
Complex |
asin()
Returns the principal arc sine of a Complex number. |
Complex |
asinh()
Returns the principal inverse hyperbolic sine of a Complex number. |
Complex |
atan()
Returns the principal arc tangent of a Complex number. |
Complex |
atanh()
Returns the principal inverse hyperbolic tangent of a Complex number. |
static Complex |
cart(double re,
double im)
Returns a Complex from real and imaginary parts. |
java.lang.Object |
clone()
Overrides the Cloneable interface. |
Complex |
conj()
Returns the Complex "conjugate". |
Complex |
cos()
Returns the cosine of a Complex number. |
Complex |
cosec()
Returns the cosecant of a Complex number. |
Complex |
cosh()
Returns the hyperbolic cosine of a Complex number. |
Complex |
cot()
Returns the cotangent of a Complex number. |
Complex |
div(Complex z)
To perform z1 / z2, you write z1.div(z2) . |
boolean |
equals(Complex z,
double tolerance)
Decides if two Complex numbers are "sufficiently" alike to be considered equal. |
Complex |
exp()
Returns the number e "raised to" a Complex power. |
double |
im()
Extracts the imaginary part of a Complex as a double. |
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 principal natural logarithm of a Complex number. |
static void |
main(java.lang.String[] args)
Useful for checking up on the exact version. |
Complex |
mul(Complex z)
To perform z1 * z2, you write z1.mul(z2) . |
Complex |
neg()
Returns the "negative" of a Complex number. |
double |
norm()
Returns the square of the "length" of a Complex number. |
static Complex |
polar(double r,
double theta)
Returns a Complex from a size and direction. |
Complex |
pow(Complex exponent)
Returns this Complex raised to the power of a Complex exponent. |
static Complex |
pow(Complex base,
Complex exponent)
Returns the Complex base raised to the power of the Complex exponent. |
static Complex |
pow(Complex base,
double exponent)
Returns the Complex base raised to the power of the exponent. |
static Complex |
pow(double base,
Complex exponent)
Returns the base raised to the power of the Complex exponent. |
double |
re()
Extracts the real part of a Complex as a double. |
static Complex |
real(double real)
Returns a Complex representing a real number. |
Complex |
scale(double scalar)
Returns the Complex scaled by a real number. |
Complex |
sec()
Returns the secant of a Complex number. |
Complex |
sin()
Returns the sine of a Complex number. |
Complex |
sinh()
Returns the hyperbolic sine of a Complex number. |
Complex |
sqrt()
Returns a Complex representing one of the two square roots. |
Complex |
sub(Complex z)
To perform z1 - z2, you write z1.sub(z2) . |
Complex |
tan()
Returns the tangent of a Complex number. |
Complex |
tanh()
Returns the hyperbolic tangent of a Complex number. |
java.lang.String |
toString()
Converts a Complex into a String of the form
(a + bi). |
Methods inherited from class java.lang.Object |
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
public static final java.lang.String AUTHOR
public static final java.lang.String DATE
public static final Complex i
The other square root of -1 is - i.
public static final java.lang.String REMARK
protected static final double TWO_PI
public static final java.lang.String VERSION
Constructor Detail |
public Complex()
public Complex(Complex z)
z
- A Complex number
public Complex(double re)
re
- The real number
real(double)
public Complex(double re, double im)
Note: All methods in class
Complex which deliver a Complex are written such that
no intermediate Complex objects get generated. This means that
you can easily anticipate the likely effects on garbage collection caused
by your own coding.
re
- Real partim
- Imaginary part
cart(double, double)
,
polar(double, double)
Method Detail |
public double abs()
abs(z) = sqrt(norm(z))
In other words, it's Pythagorean distance from the origin (0 + 0i, or zero).
The magnitude is also referred to as the "modulus" or "length".
Always non-negative.
arg()
,
polar(double, double)
,
norm()
public Complex acos()
acos(z) = -i * log( z + i * sqrt(1 - z*z) )
There are infinitely many solutions, besides the principal solution. If A is the principal solution of acos(z), the others are of the form:
2*k*PI +/- A
where k is any integer.
cos()
,
cosh()
public Complex acosh()
acosh(z) = log(z + sqrt(z*z - 1))
There are infinitely many solutions, besides the principal solution. If A is the principal solution of acosh(z), the others are of the form:
2*k*PI*i +/- A
where k is any integer.
cosh()
public Complex add(Complex z)
(a + i*b) + (c + i*d) = ((a+c) + i*(b+d))
public double arg()
There are infinitely many solutions, besides the principal solution. If A is the principal solution of arg(z), the others are of the form:
A + 2*k*PI
where k is any integer.
arg() always returns a double between -PI and +PI.
Note: 2*PI radians is the same as 360 degrees.
Domain Restrictions: There are no restrictions: the
class defines arg(0) to be 0
abs()
,
polar(double, double)
public Complex asin()
asin(z) = -i * log(i*z + sqrt(1 - z*z))
There are infinitely many solutions, besides the principal solution. If A is the principal solution of asin(z), the others are of the form:
k*PI + (-1)k * A
where k is any integer.
sin()
,
sinh()
public Complex asinh()
asinh(z) = log(z + sqrt(z*z + 1))
There are infinitely many solutions, besides the principal solution. If A is the principal solution of asinh(z), the others are of the form:
k*PI*i + (-1)k * A
where k is any integer.
sinh()
public Complex atan()
atan(z) = -i/2 * log( (i-z)/(i+z) )
There are infinitely many solutions, besides the principal solution. If A is the principal solution of atan(z), the others are of the form:
A + k*PI
where k is any integer.
Domain Restrictions: atan(z) is undefined for z = + i or z = - i
tan()
,
tanh()
public Complex atanh()
atanh(z) = 1/2 * log( (1+z)/(1-z) )
There are infinitely many solutions, besides the principal solution. If A is the principal solution of atanh(z), the others are of the form:
A + k*PI*i
where k is any integer.
Domain Restrictions: atanh(z) is undefined for z = + 1 or z = - 1
tanh()
public static Complex cart(double re, double im)
re
- Real partim
- Imaginary part
re()
,
im()
,
polar(double, double)
,
toString()
public java.lang.Object clone()
Cloneable
interface.
Standard override; no change in semantics.
The following Java code example illustrates how to clone, or copy, a Complex number:
Complex z1 = new Complex(0, 1); Complex z2 = (Complex) z1.clone();
clone
in class java.lang.Object
Cloneable
,
Object.clone()
public Complex conj()
conj(x + i*y) = x - i*y
The conjugate appears "flipped" across the real axis.
public Complex cos()
cos(z) = ( exp(i*z) + exp(-i*z) ) / 2
acos()
,
cosh()
,
sec()
,
sin()
,
tan()
public Complex cosec()
cosec(z) = 1 / sin(z)
Domain Restrictions: cosec(z) is undefined whenever z = k * PI
where k is any integer
sin()
,
sec()
,
cot()
public Complex cosh()
cosh(z) = ( exp(z) + exp(-z) ) / 2
cos()
,
acosh()
public Complex cot()
cot(z) = 1 / tan(z)
Domain Restrictions: cot(z) is undefined whenever z = k * PI
where k is any integer
tan()
,
cosec()
,
sec()
public Complex div(Complex z)
(a + i*b) / (c + i*d) = ( (a*c) + (b*d) + i*((b*c) - (a*d)) ) / norm(c + i*d)
Take care not to divide by zero!
Note: Complex arithmetic in Java never causes
exceptions. You have to deliberately check for overflow, division by
zero, and so on, for yourself.
Domain Restrictions: z1/z2 is undefined if z2 = 0
scale(double)
public boolean equals(Complex z, double tolerance)
tolerance is the maximum magnitude of the difference between them before they are considered not equal.
Checking for equality between two real numbers on computer hardware is a tricky business. Try
System.out.println((1.0/3.0 * 3.0));
and you'll see the nature of the problem! It's just as tricky with Complex numbers.
Realize that because of these complications, it's possible to find that the magnitude of one Complex number a is less than another, b, and yet a.equals(b, myTolerance) returns true. Be aware!
z
- The Complex to compare withtolerance
- The tolerance for equality
public Complex exp()
exp(x + i*y) = exp(x) * ( cos(y) + i * sin(y) )
Note:
Also, the following is quietly amazing:
The value of e, a transcendental number, is
roughly 2.71828182846...
ePI*i = - 1
log()
,
pow(double, Complex)
public double im()
im(x + i*y) = y
re()
,
cart(double, double)
public boolean isInfinite()
public boolean isNaN()
public Complex log()
log(z) = log(abs(z)) + i * arg(z)
There are infinitely many solutions, besides the principal solution. If L is the principal solution of log(z), the others are of the form:
L + (2*k*PI)*i
where k is any integer.
exp()
public static void main(java.lang.String[] args)
public Complex mul(Complex z)
(a + i*b) * (c + i*d) = ( (a*c) - (b*d) + i*((a*d) + (b*c)) )
scale(double)
public Complex neg()
neg(a + i*b) = -a - i*b
The magnitude of the negative is the same, but the angle is flipped through PI (or 180 degrees).
scale(double)
public double norm()
norm(x + i*y) = x*x + y*y
Always non-negative.
abs()
public static Complex polar(double r, double theta)
r
- Sizetheta
- Direction (in radians)
abs()
,
arg()
,
cart(double, double)
public Complex pow(Complex exponent)
exponent
- The exponent "by which to raise"
pow(Complex, Complex)
public static Complex pow(Complex base, Complex exponent)
base
- The base "to raise"exponent
- The exponent "by which to raise"
pow(Complex, double)
,
pow(Complex)
public static Complex pow(Complex base, double exponent)
base
- The base "to raise"exponent
- The exponent "by which to raise"
pow(double, Complex)
public static Complex pow(double base, Complex exponent)
base
- The base "to raise"exponent
- The exponent "by which to raise"
pow(Complex, Complex)
,
exp()
public double re()
re(x + i*y) = x
im()
,
cart(double, double)
,
real(double)
public static Complex real(double real)
real
- The real number
re()
,
cart(double, double)
public Complex scale(double scalar)
scale((x + i*y), s) = (x*s + i*y*s)
Scaling by the real number 2.0, doubles the magnitude, but leaves the arg() unchanged. Scaling by -1.0 keeps the magnitude the same, but flips the arg() by PI (180 degrees).
scalar
- A real number scale factor
mul(Complex)
,
div(Complex)
,
neg()
public Complex sec()
sec(z) = 1 / cos(z)
Domain Restrictions: sec(z) is undefined whenever z = (k + 1/2) * PI
where k is any integer
cos()
,
cosec()
,
cot()
public Complex sin()
sin(z) = ( exp(i*z) - exp(-i*z) ) / (2*i)
asin()
,
sinh()
,
cosec()
,
cos()
,
tan()
public Complex sinh()
sinh(z) = ( exp(z) - exp(-z) ) / 2
sin()
,
asinh()
public Complex sqrt()
sqrt(z) = sqrt(abs(z)) * ( cos(arg(z)/2) + i * sin(arg(z)/2) )
For any complex number z, sqrt(z) will return the complex root whose arg is arg(z)/2.
Note: There are always two square roots for each
Complex number, except for 0 + 0i, or zero. The other
root is the neg() of the first one. Just as the two roots of
4 are 2 and -2, the two roots of -1 are i and - i.
pow(Complex, double)
public Complex sub(Complex z)
(a + i*b) - (c + i*d) = ((a-c) + i*(b-d))
public Complex tan()
tan(z) = sin(z) / cos(z)
Domain Restrictions: tan(z) is undefined whenever z = (k + 1/2) * PI
where k is any integer
atan()
,
tanh()
,
cot()
,
sin()
,
cos()
public Complex tanh()
tanh(z) = sinh(z) / cosh(z)
tan()
,
atanh()
public java.lang.String toString()
String
of the form
(a + bi).
This enables a Complex to be easily printed. For example, if z was 2 - 5i, then
System.out.println("z = " + z);would print something like
z = (2.0 - 5.0i)
toString
in class java.lang.Object
String
containing the cartesian coordinate representation
cart(double, double)