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, ALMA Services. All Rights Reserved.
Permission to use, copy, modify, and distribute this software and its documentation is hereby granted provided that this copyright notice appears in all copies and that all modifications are clearly marked.
THE AUTHORS MAKE NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. THE AUTHORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
Last change: ALM 29 Aug 97 2:13 am
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;
}
}
public final static String VERSION
public final static String DATE
public final static String AUTHOR
public final static String REMARK
protected final static double TWO_PI
public final static Complex i
The other square root of -1 is - i.
public Complex()
public Complex(double re)
public Complex(Complex z)
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.
public static void main(String args[])
public static Complex real(double real)
public static Complex cart(double re,
double im)
public static Complex polar(double r,
double theta)
public static Complex pow(Complex base,
double exponent)
public static Complex pow(double base,
Complex exponent)
public static Complex pow(Complex base,
Complex exponent)
public boolean isInfinite()
public boolean isNaN()
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!
public double re()
re(x + i*y) = x
public double im()
im(x + i*y) = y
public double norm()
norm(x + i*y) = x*x + y*y
Always non-negative.
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.
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
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).
public Complex conj()
conj(x + i*y) = x - i*y
The conjugate appears "flipped" across the real axis.
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).
public Complex add(Complex z)
(a + i*b) + (c + i*d) = ((a+c) + i*(b+d))
public Complex sub(Complex z)
(a + i*b) - (c + i*d) = ((a-c) + i*(b-d))
public Complex mul(Complex z)
(a + i*b) * (c + i*d) = ( (a*c) - (b*d) + i*((a*d) + (b*c)) )
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
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.
public Complex pow(Complex exponent)
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
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.
public Complex sin()
sin(z) = ( exp(i*z) - exp(-i*z) ) / (2*i)
public Complex cos()
cos(z) = ( exp(i*z) + exp(-i*z) ) / 2
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
public Complex cosec()
cosec(z) = 1 / sin(z)
Domain Restrictions: cosec(z) is undefined whenever z = k * PI
where k is any integer
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
public Complex cot()
cot(z) = 1 / tan(z)
Domain Restrictions: cot(z) is undefined whenever z = k * PI
where k is any integer
public Complex sinh()
sinh(z) = ( exp(z) - exp(-z) ) / 2
public Complex cosh()
cosh(z) = ( exp(z) + exp(-z) ) / 2
public Complex tanh()
tanh(z) = sinh(z) / cosh(z)
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.
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.
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
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.
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.
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
public String toString()
This enables the Complex to be easily printed. For example, if z was 2 - 5i, then
System.out.println("z = " + z);
would print
z = (2 - 5i)