|
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object jpicedt.util.math.Complex
public class Complex
A Java class for performing complex number arithmetic to double
precision.
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.cMul(z).cAdd(c); if (z.abs() > 2) return false; }
return true; }
}
Field Summary | |
---|---|
static String |
AUTHOR
|
static String |
DATE
|
static Complex |
I
A constant representing i, the famous square root of -1. |
static Complex |
ONE
|
static String |
REMARK
|
static double |
TWO_PI
Twice PI radians is the same thing as 360 degrees. |
static String |
VERSION
|
static Complex |
ZERO
|
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. |
double |
abs2()
Returns the square of the "length" of a Complex number. |
boolean |
absoluteIsCloseTo(Complex z,
double tolerance)
Renvoie cSub(z).normInf() <= tolerance . |
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)
|
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. |
Complex |
cAdd(Complex z)
To perform z1 + z2, you write z1.cAdd(z2) . |
Complex |
cAdd(double z)
CAdd z to this without modifying this, and returns the result. |
static Complex |
cart(double re,
double im)
Returns a Complex from real and imaginary parts. |
Complex |
cConj()
Returns the Complex "conjugate" of this. |
Complex |
cCos()
Returns the cosine of a Complex number. |
Complex |
cCot()
Returns the cotangent of a Complex number. |
Complex |
cDiv(Complex z)
To perform z1 / z2, you write z1.cDiv(z2) . |
Complex |
cIMul()
multiply this by i, without modifying this. |
Complex |
cLog()
Returns the principal natural logarithm of a Complex
number. |
Object |
clone()
Overrides the Cloneable interface. |
Complex |
cMIMul()
multiply this by -i, without modifying this. |
Complex |
cMul(Complex z)
To perform z1 * z2, you write z1.cMul(z2) . |
Complex |
cMul(double z)
|
Complex |
cNeg()
Returns the "negative" of a Complex number. |
Complex |
conj()
conjugate this, and return it for convenience |
Complex |
cosec()
Returns the cosecant of a Complex number. |
Complex |
cosh()
Returns the hyperbolic cosine of a Complex number. |
Complex |
cPow(Complex exponent)
Renvoie la valeur Complex du this élevée
raised to the power of a à la puissance d'un exposant
Complex sans que this ne soit modifié |
Complex |
cScale(double scalar)
Returns the Complex scaled by a real number. |
Complex |
cSin()
Returns the sine of a Complex number. |
Complex |
cSqrt()
Returns a Complex representing one of the two square roots. |
Complex |
cSub(Complex z)
|
Complex |
cSub(double z)
Subtracts z from this without modifying this, and returns the result. |
Complex |
cTan()
Returns the tangent of a Complex number. |
Complex |
div(Complex z)
|
boolean |
equals(Complex z,
double tolerance)
Renvoie absoluteIsCloseTo(z,Math.abs(tolerance)) . |
Complex |
exp()
Returns the number e "raised to" a Complex power. |
double |
im()
Extracts the imaginary part of a Complex as a double . |
Complex |
iMul()
multiply this by i, which modifies this. |
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. |
static void |
main(String[] args)
Useful for checking up on the exact version. |
Complex |
miMul()
multiply this by -i, which modifies this. |
Complex |
mul(Complex z)
|
Complex |
mul(double z)
|
Complex |
neg()
negate this, which modifies this. |
double |
norm1()
|
double |
normInf()
|
static Complex |
polar(double r,
double theta)
Returns a Complex from a size and direction. |
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 . |
boolean |
relativeIsCloseTo(Complex z,
double tolerance)
Renvoie cSub(z).normInf() <= tolerance *
Math.max(normInf(), z.normInf()) . |
Complex |
sec()
Returns the secant of a Complex number. |
Complex |
sinh()
Returns the hyperbolic sine of a Complex number. |
Complex |
sub(Complex z)
|
Complex |
sub(double z)
|
Complex |
tanh()
Returns the hyperbolic tangent of a Complex number. |
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 String VERSION
public static final String DATE
public static final String AUTHOR
public static final String REMARK
public static final double TWO_PI
public static final Complex I
The other square root of -1 is - i.
public static final Complex ZERO
public static final Complex ONE
Constructor Detail |
---|
public Complex()
Complex
representing the number zero.
public Complex(double re)
Complex
representing a real number.
re
- The real number
public Complex(Complex z)
Complex
from an existing
Complex
.
z
- A Complex
number
public Complex(double re, double im)
Complex
from real and imaginary parts.
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 re, double im)
,
polar(double, double)
Method Detail |
---|
public static void main(String[] args)
public static Complex cart(double re, double im)
Complex
from real and imaginary parts.
re
- Real partim
- Imaginary part
Complex
from Cartesian coordinates
re()
,
im()
,
polar(double, double)
,
toString()
public static Complex polar(double r, double theta)
Complex
from a size and direction.
r
- Sizetheta
- Direction (in radians)
Complex
from Polar coordinates
abs()
,
arg()
,
cart(double re, double im)
public static Complex pow(Complex base, double exponent)
Complex
base raised to the power of the exponent.
base
- The base "to raise"exponent
- The exponent "by which to raise"
pow(double, Complex)
public static Complex pow(double base, Complex exponent)
Complex
exponent.
base
- The base "to raise"exponent
- The exponent "by which to raise"
pow(Complex, Complex)
,
exp()
public static Complex pow(Complex base, Complex exponent)
Complex
base raised to the power of the Complex
exponent.
base
- The base "to raise"exponent
- The exponent "by which to raise"
pow(Complex, double)
,
cPow(Complex)
public boolean isInfinite()
true
if either the real or imaginary component of this
Complex
is an infinite value.
true
if either component of the Complex
object is infinite; false
, otherwise.
public boolean isNaN()
true
if either the real or imaginary component of this
Complex
is a Not-a-Number (NaN) value.
true
if either component of the Complex
object is NaN; false
, otherwise.
public boolean equals(Complex z, double tolerance)
absoluteIsCloseTo(z,Math.abs(tolerance))
. Veuillez
préférer la méthode absoluteIsCloseTo(jpicedt.util.math.Complex, double)
si vous savez que
tolerance > 0
.
z
- Le Complex
auquel on compare this
tolerance
- LA tolérance pour la comparaison
true
ou false
absoluteIsCloseTo(jpicedt.util.math.Complex, double)
public boolean absoluteIsCloseTo(Complex z, double tolerance)
cSub(z).normInf() <= tolerance
.
z
- une valeur Complex
à laquelle this
est comparé.tolerance
- une valeur double
, donnant la limite
jusqu'à laquelle this
et z
sont considérés
proches.
boolean
, vraie la distance de
this
à z
selon la norme infinie normInf()
n'excède pas la tolérance tolerance
.relativeIsCloseTo(jpicedt.util.math.Complex, double)
public boolean relativeIsCloseTo(Complex z, double tolerance)
cSub(z).normInf() <= tolerance *
Math.max(normInf(), z.normInf())
.
z
- une valeur Complex
à laquelle this
est comparé.tolerance
- une valeur double
, donnant la limite
jusqu'à laquelle this
et z
sont considérés
proches.
boolean
, vraie la distance de
this
à z
selon la norme infinie normInf()
n'excède pas tolerance
partie-pour-un de la plus
grande des normes normInf()
de this
et
z
.absoluteIsCloseTo(jpicedt.util.math.Complex, double)
public 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 Object
Object
that is a copy of this Complex
object.
Cloneable
,
Object.clone()
public double re()
Complex
as a double
.
re(x + i*y) = x
im()
,
cart(double re, double im)
public double im()
Complex
as a double
.
im(x + i*y) = y
re()
,
cart(double re, double im)
public double abs2()
Complex
number.
(x + i*y).abs2() = x*x + y*y
Always non-negative.
abs()
public double norm1()
public double normInf()
public double abs()
Complex
number.
abs(z) = sqrt(abs2(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)
,
abs2()
public double arg()
Complex
number, in
radians, measured counter-clockwise from the real axis. (Think of the
reals as the x-axis, and the imaginaries as the y-axis.)
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 cNeg()
Complex
number.
cNeg(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
cScale(double)
public Complex neg()
public Complex cConj()
Complex
"conjugate" of this.
cConj(x + i*y) = x - i*y
The conjugate appears "flipped" across the real axis.
Complex
conjugate
public Complex conj()
public Complex cScale(double scalar)
Complex
scaled by a real number.
cScale((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
scaled by a real number
cMul(Complex)
,
cDiv(Complex)
,
cNeg()
,
cNeg()
public Complex cAdd(Complex z)
z1.cAdd(z2)
.
(a + i*b) + (c + i*d) = ((a+c) + i*(b+d))
public Complex add(Complex z)
public Complex cSub(Complex z)
public Complex sub(Complex z)
public Complex sub(double z)
public Complex cSub(double z)
public Complex cAdd(double z)
public Complex cMul(Complex z)
z1.cMul(z2)
.
(a + i*b) * (c + i*d) = ( (a*c) - (b*d) + i*((a*d) + (b*c)) )
cScale(double)
public Complex cMul(double z)
public Complex mul(Complex z)
public Complex mul(double z)
public Complex cDiv(Complex z)
z1.cDiv(z2)
.
(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
cScale(double)
public Complex div(Complex z)
public Complex cSqrt()
Complex
representing one of the two square roots.
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 cNeg()
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 cPow(Complex exponent)
Complex
du this
élevée
raised to the power of a à la puissance d'un exposant
Complex
sans que this
ne soit modifié
exponent
- L'exposant "auquel on élève"
Complex
"raised to the power of" the exponentpow(Complex, Complex)
public Complex exp()
Complex
power.
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
cLog()
,
cPow(Complex exponent)
public Complex cLog()
Complex
number.
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
natural logarithm
exp()
public Complex cSin()
Complex
number.
sin(z) = ( exp(i*z) - exp(-i*z) ) / (2*i)
Complex
sine
asin()
,
sinh()
,
cosec()
,
cCos()
,
cTan()
public Complex cCos()
Complex
number.
cos(z) = ( exp(i*z) + exp(-i*z) ) / 2
Complex
cosine
acos()
,
cosh()
,
sec()
,
cSin()
,
cTan()
public Complex iMul()
public Complex cIMul()
public Complex miMul()
public Complex cMIMul()
public Complex cTan()
Complex
number.
tan(z) = sin(z) / cos(z)
Domain Restrictions: tan(z) is undefined whenever z = (k + 1/2) * PI
where k is any integer
Complex
tangent
atan()
,
tanh()
,
cCot()
,
cSin()
,
cCos()
public Complex cosec()
Complex
number.
cosec(z) = 1 / sin(z)
Domain Restrictions: cosec(z) is undefined whenever z = k * PI
where k is any integer
Complex
cosecant
cSin()
,
sec()
,
cCot()
public Complex sec()
Complex
number.
sec(z) = 1 / cos(z)
Domain Restrictions: sec(z) is undefined whenever z = (k + 1/2) * PI
where k is any integer
Complex
secant
cCos()
,
cosec()
,
cCot()
public Complex cCot()
Complex
number.
cot(z) = 1 / tan(z)
Domain Restrictions: cot(z) is undefined whenever z = k *
PI
where k is any integer
Complex
cotangent
cTan()
,
cosec()
,
sec()
public Complex sinh()
Complex
number.
sinh(z) = ( exp(z) - exp(-z) ) / 2
Complex
hyperbolic sine
cSin()
,
asinh()
public Complex cosh()
Complex
number.
cosh(z) = ( exp(z) + exp(-z) ) / 2
Complex
hyperbolic cosine
cCos()
,
acosh()
public Complex tanh()
Complex
number.
tanh(z) = sinh(z) / cosh(z)
Complex
hyperbolic tangent
cTan()
,
atanh()
public Complex asin()
Complex
number.
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
arc sine
cSin()
,
sinh()
public Complex acos()
Complex
number.
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
arc cosine
cCos()
,
cosh()
public Complex atan()
Complex
number.
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
arc tangent
cTan()
,
tanh()
public Complex asinh()
Complex
number.
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
inverse hyperbolic sine
sinh()
public Complex acosh()
Complex
number.
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
inverse hyperbolic cosine
cosh()
public Complex atanh()
Complex
number.
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
inverse hyperbolic tangent
tanh()
public String toString()
Complex
into a 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 Object
String
containing the cartesian coordinate representation
cart(double re, double im)
|
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |