|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Objectjmathlib.toolbox.jmathlib.matrix._private.Jampack.Z
public class Z
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 (Z c, int maxIter) {
Z z= new Z(0, 0);
for (int i= 0; i < maxIter; i++) {
z= z.Times(z).Plus(c);
if (z.abs() > 2) return false;
}
return true;
}
}
| Field Summary | |
|---|---|
static java.lang.String |
AUTHOR
|
static java.lang.String |
DATE
|
static Z |
i
A constant representing i, the famous square root of -1. |
static Z |
I
Imaginary unit. |
double |
im
|
static Z |
One
The number 1 stored as a complex number |
static Z |
ONE
Z 1. |
double |
re
|
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
|
static Z |
Zero
The number 0 stored as a complex number |
static Z |
ZERO
Z 0. |
| Constructor Summary | |
|---|---|
Z()
Constructs a Complex representing the number zero. |
|
Z(double re)
Constructs a Complex representing a real number. |
|
Z(double re,
double im)
Constructs a Complex from real and imaginary parts. |
|
Z(Z z)
Constructs a separate new Complex from an existing Complex. |
|
| Method Summary | |
|---|---|
double |
abs()
Returns the magnitude of a Complex number. |
static double |
abs(Z z)
|
Z |
acos()
Returns the principal arc cosine of a Complex number. |
Z |
acosh()
Returns the principal inverse hyperbolic cosine of a Complex number. |
double |
arg()
Returns the principal angle of a Complex number, in radians, measured counter-clockwise from the real axis. |
Z |
asin()
Returns the principal arc sine of a Complex number. |
Z |
asinh()
Returns the principal inverse hyperbolic sine of a Complex number. |
Z |
atan()
Returns the principal arc tangent of a Complex number. |
Z |
atanh()
Returns the principal inverse hyperbolic tangent of a Complex number. |
static Z |
cart(double re,
double im)
Returns a Complex from real and imaginary parts. |
java.lang.Object |
clone()
Overrides the Cloneable interface. |
Z |
conj()
Returns the Complex "conjugate". |
Z |
Conj(Z a)
|
Z |
cos()
Returns the cosine of a Complex number. |
Z |
cosec()
Returns the cosecant of a Complex number. |
Z |
cosh()
Returns the hyperbolic cosine of a Complex number. |
Z |
cot()
Returns the cotangent of a Complex number. |
Z |
Div(Z z)
To perform z1 / z2, you write z1.Div(z2) . |
static Z |
Div(Z z,
double x)
|
static Z |
Div(Z z,
double x,
double y)
|
Z |
Div(Z a,
Z b)
|
Z |
Eq(Z a)
|
boolean |
equals(java.lang.Object arg)
test if the object is equal to this one within a tolerance of 1E-10 |
boolean |
equals(Z z,
double tolerance)
Decides if two Complex numbers are "sufficiently" alike to be considered equal. |
Z |
Exch(Z a)
Interchanges the real and imaginary parts of two Z's. |
Z |
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. |
Z |
log()
Returns the principal natural logarithm of a Complex number. |
Z |
Minus(Z z)
To perform z1 - z2, you write z1.Minus(z2) . |
Z |
Minus(Z z1,
Z z2)
|
Z |
neg()
Returns the "negative" of a Complex number. |
double |
norm()
Returns the square of the "length" of a Complex number. |
Z |
Plus(Z z)
To perform z1 + z2, you write z1.Plus(z2) . |
Z |
Plus(Z z1,
Z z2)
|
static Z |
polar(double r,
double theta)
Returns a Complex from a size and direction. |
static Z |
pow(double base,
Z exponent)
Returns the base raised to the power of the Complex exponent. |
Z |
pow(Z exponent)
Returns this Complex raised to the power of a Complex exponent. |
static Z |
pow(Z base,
double exponent)
Returns the Complex base raised to the power of the exponent. |
static Z |
pow(Z base,
Z exponent)
Returns the Complex base raised to the power of the Complex exponent. |
double |
re()
Extracts the real part of a Complex as a double. |
static Z |
real(double real)
Returns a Complex representing a real number. |
Z |
scale(double scalar)
Returns the Complex scaled by a real number. |
Z |
sec()
Returns the secant of a Complex number. |
Z |
sin()
Returns the sine of a Complex number. |
Z |
sinh()
Returns the hyperbolic sine of a Complex number. |
Z |
Sqrt()
Returns a Complex representing one of the two square roots. |
static void |
Sqrt(Z z)
|
Z |
tan()
Returns the tangent of a Complex number. |
Z |
tanh()
Returns the hyperbolic tangent of a Complex number. |
Z |
Times(Z z)
To perform z1 * z2, you write z1.Times(z2) . |
Z |
Times(Z a,
Z b)
|
java.lang.String |
toString()
Converts a Complex into a String of the form
(a + bi). |
| Methods inherited from class java.lang.Object |
|---|
finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| Field Detail |
|---|
public static final java.lang.String VERSION
public static final java.lang.String DATE
public static final java.lang.String AUTHOR
public static final java.lang.String REMARK
public static final Z ONE
public static final Z ZERO
public static final Z I
protected static final double TWO_PI
public static final Z i
The other square root of -1 is - i.
public static final Z One
public static final Z Zero
public double re
public double im
| Constructor Detail |
|---|
public Z()
public Z(double re)
re - The real number
Complex#real(double)public Z(Z z)
z - A Complex number
public Z(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
Complex#cart(double, double),
Complex#polar(double, double)| Method Detail |
|---|
public static Z real(double real)
real - The real number
Complex#re(),
Complex#cart(double, double)
public static Z cart(double re,
double im)
re - Real partim - Imaginary part
Complex#re(),
Complex#im(),
Complex#polar(double, double),
Complex#toString()
public static Z polar(double r,
double theta)
r - Sizetheta - Direction (in radians)
Complex#abs(),
Complex#arg(),
Complex#cart(double, double)
public static Z pow(Z base,
double exponent)
base - The base "to raise"exponent - The exponent "by which to raise"
Complex#pow(double, Complex)
public static Z pow(double base,
Z exponent)
base - The base "to raise"exponent - The exponent "by which to raise"
Complex#pow(Complex, Complex),
Complex#exp()
public static Z pow(Z base,
Z exponent)
base - The base "to raise"exponent - The exponent "by which to raise"
Complex#pow(Complex, double),
Complex#pow(Complex)public boolean isInfinite()
public boolean isNaN()
public boolean equals(Z 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 boolean equals(java.lang.Object arg)
equals in class java.lang.Objectarg - = the object to test againstpublic 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:
Z z1 = new Z(0, 1);
Z z2 = (Complex) z1.clone();
clone in class java.lang.ObjectCloneable,
Object.clone()public double re()
re(x + i*y) = x
Complex#im(),
Complex#cart(double, double),
Complex#real(double)public double im()
im(x + i*y) = y
Complex#re(),
Complex#cart(double, double)public double norm()
norm(x + i*y) = x*x + y*y
Always non-negative.
Complex#abs()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.
Complex#arg(),
Complex#polar(double, double),
Complex#norm()public static double abs(Z z)
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
Complex#abs(),
Complex#polar(double, double)public Z 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).
Complex#scale(double)public Z conj()
conj(x + i*y) = x - i*y
The conjugate appears "flipped" across the real axis.
public Z Conj(Z a)
public Z 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
Complex#Times(Complex),
Complex#Div(Complex),
Complex#neg()public Z Plus(Z z)
(a + i*b) + (c + i*d) = ((a+c) + i*(b+d))
public Z Plus(Z z1,
Z z2)
public Z Minus(Z z)
(a + i*b) - (c + i*d) = ((a-c) + i*(b-d))
public Z Minus(Z z1,
Z z2)
public Z Times(Z z)
(a + i*b) * (c + i*d) = ( (a*c) - (b*d) + i*((a*d) + (b*c)) )
Complex#scale(double)
public Z Times(Z a,
Z b)
public Z Div(Z 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
Complex#scale(double)
public Z Div(Z a,
Z b)
public static Z Div(Z z,
double x)
public static Z Div(Z z,
double x,
double y)
public Z 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.
Complex#pow(Complex, double)public static void Sqrt(Z z)
public Z pow(Z exponent)
exponent - The exponent "by which to raise"
Complex#pow(Complex, Complex)public Z 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
Complex#log(),
Complex#pow(double, Complex)public Z 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.
Complex#exp()public Z sin()
sin(z) = ( exp(i*z) - exp(-i*z) ) / (2*i)
Complex#asin(),
Complex#sinh(),
Complex#cosec(),
Complex#cos(),
Complex#tan()public Z cos()
cos(z) = ( exp(i*z) + exp(-i*z) ) / 2
Complex#acos(),
Complex#cosh(),
Complex#sec(),
Complex#sin(),
Complex#tan()public Z tan()
tan(z) = sin(z) / cos(z)
Domain Restrictions: tan(z) is undefined whenever z = (k + 1/2) * PI
where k is any integer
Complex#atan(),
Complex#tanh(),
Complex#cot(),
Complex#sin(),
Complex#cos()public Z cosec()
cosec(z) = 1 / sin(z)
Domain Restrictions: cosec(z) is undefined whenever z = k * PI
where k is any integer
Complex#sin(),
Complex#sec(),
Complex#cot()public Z sec()
sec(z) = 1 / cos(z)
Domain Restrictions: sec(z) is undefined whenever z = (k + 1/2) * PI
where k is any integer
Complex#cos(),
Complex#cosec(),
Complex#cot()public Z cot()
cot(z) = 1 / tan(z)
Domain Restrictions: cot(z) is undefined whenever z = k * PI
where k is any integer
Complex#tan(),
Complex#cosec(),
Complex#sec()public Z sinh()
sinh(z) = ( exp(z) - exp(-z) ) / 2
Complex#sin(),
Complex#asinh()public Z cosh()
cosh(z) = ( exp(z) + exp(-z) ) / 2
Complex#cos(),
Complex#acosh()public Z tanh()
tanh(z) = sinh(z) / cosh(z)
Complex#tan(),
Complex#atanh()public Z 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.
Complex#sin(),
Complex#sinh()public Z 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.
Complex#cos(),
Complex#cosh()public Z 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
Complex#tan(),
Complex#tanh()public Z 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.
Complex#sinh()public Z 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.
Complex#cosh()public Z 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
Complex#tanh()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.ObjectString containing the cartesian coordinate representation
Complex#cart(double, double)public Z Exch(Z a)
a - a Z
public Z Eq(Z a)
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||