|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object Complex
A Java class for performing complex number arithmetic to double precision.
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. |
private double |
im
|
private 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
|
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. |
private static double |
abs(double x,
double y)
|
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. |
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) . |
private static void |
div(Complex z,
double x,
double y)
|
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. |
private static void |
inv(Complex z)
Returns the Complex multiplicative inverse. |
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. |
private static void |
sqrt(Complex z)
|
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 |
clone, equals, 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
protected static final double TWO_PI
public static final Complex i
The other square root of -1 is - i.
private double re
private double im
Constructor Detail |
public Complex()
public Complex(double re)
re
- The real number
real(double)
public Complex(Complex z)
z
- A Complex number
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 static void main(java.lang.String[] args)
public static Complex real(double real)
real
- The real number
re()
,
cart(double, double)
public static Complex cart(double re, double im)
re
- Real partim
- Imaginary part
re()
,
im()
,
polar(double, double)
,
toString()
public static Complex polar(double r, double theta)
r
- Sizetheta
- Direction (in radians)
abs()
,
arg()
,
cart(double, double)
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 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 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!
z
- The Complex to compare withtolerance
- The tolerance for equality
public double re()
re(x + i*y) = x
im()
,
cart(double, double)
,
real(double)
public double im()
im(x + i*y) = y
re()
,
cart(double, double)
public double norm()
norm(x + i*y) = x*x + y*y
Always non-negative.
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.
arg()
,
polar(double, double)
,
norm()
private static double abs(double x, double y)
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 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 Complex conj()
conj(x + i*y) = x - i*y
The conjugate appears "flipped" across the real axis.
private static void inv(Complex z)
inv(z) = 1 / z
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 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)) )
scale(double)
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)
private static void div(Complex z, double x, double y)
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)
private static void sqrt(Complex z)
public Complex pow(Complex exponent)
exponent
- The exponent "by which to raise"
pow(Complex, Complex)
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 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 Complex sin()
sin(z) = ( exp(i*z) - exp(-i*z) ) / (2*i)
asin()
,
sinh()
,
cosec()
,
cos()
,
tan()
public Complex cos()
cos(z) = ( exp(i*z) + exp(-i*z) ) / 2
acos()
,
cosh()
,
sec()
,
sin()
,
tan()
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 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 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 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 sinh()
sinh(z) = ( exp(z) - exp(-z) ) / 2
sin()
,
asinh()
public Complex cosh()
cosh(z) = ( exp(z) + exp(-z) ) / 2
cos()
,
acosh()
public Complex tanh()
tanh(z) = sinh(z) / cosh(z)
tan()
,
atanh()
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 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 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 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 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 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 java.lang.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)
cart(double, double)
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |