com.monkeycoder.monkeyfractal.base
Class ComplexNumber

java.lang.Object
  extended by com.monkeycoder.monkeyfractal.base.ComplexNumber

public class ComplexNumber
extends java.lang.Object

This class represents a Complex Number (a + bi) and provides basic math functions for using them.

Originally part of the MonkeyFractal project.

Author:
David Stephens

Field Summary
 double i
          The imaginary number part of this complex number.
 double r
          The real number part of this complex number.
 
Constructor Summary
ComplexNumber()
          Creates a new ComplexNumber initialized to (0 + 0i).
ComplexNumber(double inR, double inI)
          Creates a new ComplexNumber initialized to (inR + inIi).
 
Method Summary
 double actualDistFromOrigin()
          Returns the actual distance from the origin of the Complex Plane.
static double actualDistFromOrigin(ComplexNumber inComplex)
          Returns the actual distance from the origin of the Complex Plane.
 void add(ComplexNumber inComplex)
          Calculates the sum of this ComplexNumber and the supplied ComplexNumber.
static ComplexNumber add(ComplexNumber inComplex1, ComplexNumber inComplex2)
          Calculates the sum of two ComplexNumbers.
 double distFromOrigin()
          Returns the distance from the origin of the Complex Plane.
static double distFromOrigin(ComplexNumber inComplex)
          Returns the distance from the origin of the Complex Plane.
 void divide(ComplexNumber inComplex)
          Divides this ComplexNumber by the supplied ComplexNumber.
static ComplexNumber divide(ComplexNumber inComplex1, ComplexNumber inComplex2)
          Calculates the quotient of two ComplexNumbers.
 double getImaginary()
          Returns the Imaginary part of this ComplexNumber.
 double getReal()
          Returns the Real part of this ComplexNumber.
 boolean isZero()
          Determines whether or not both the real and imaginary parts of this complex number are zero.
 void multiply(ComplexNumber inComplex)
          Multiplies this ComplexNumber by the supplied ComplexNumber.
static ComplexNumber multiply(ComplexNumber inComplex1, ComplexNumber inComplex2)
          Calculates the product of two ComplexNumbers.
 void set(ComplexNumber inComplex)
          Sets the Real and Imaginary parts of this ComplexNumber to the real and imaginary parts of the passed in ComplexNumber.
 void set(double inR, double inI)
          Sets the Real and Imaginary parts of this ComplexNumber to (inR + inIi).
 void setImaginary(double inI)
          Sets the Imaginary part of this ComplexNumber to the Imaginary part passed in.
 void setReal(double inR)
          Sets the Real part of this ComplexNumber to the Real part passed in.
 void square()
          Squares this ComplexNumber.
static ComplexNumber square(ComplexNumber inComplex1)
          Calculates the square of the ComplexNumber passed in.
 void subtract(ComplexNumber inComplex)
          Subtracts the supplied ComplexNumber from this ComplexNumber.
static ComplexNumber subtract(ComplexNumber inComplex1, ComplexNumber inComplex2)
          Calculates the difference of two ComplexNumbers.
 java.lang.String toString()
          Returns this ComplexNumber as a String.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

r

public double r
The real number part of this complex number.


i

public double i
The imaginary number part of this complex number.

Constructor Detail

ComplexNumber

public ComplexNumber()
Creates a new ComplexNumber initialized to (0 + 0i).


ComplexNumber

public ComplexNumber(double inR,
                     double inI)
Creates a new ComplexNumber initialized to (inR + inIi).

Parameters:
inR - The real number part this to which this ComplexNumber will be initialized.
inI - The imaginary number part this to which this ComplexNumber will be initialized.
Method Detail

set

public void set(double inR,
                double inI)
Sets the Real and Imaginary parts of this ComplexNumber to (inR + inIi).

Parameters:
inR - The real number part this to which this ComplexNumber will be set.
inI - The imaginary number part this to which this ComplexNumber will be set.

set

public void set(ComplexNumber inComplex)
Sets the Real and Imaginary parts of this ComplexNumber to the real and imaginary parts of the passed in ComplexNumber.

Parameters:
inComplex - The ComplexNumber that will be used for values to set this ComplexNumber.

setReal

public void setReal(double inR)
Sets the Real part of this ComplexNumber to the Real part passed in. The Imaginary part is not changed.

Parameters:
inR - The real number part this to which this ComplexNumber will be set.

setImaginary

public void setImaginary(double inI)
Sets the Imaginary part of this ComplexNumber to the Imaginary part passed in. The Real part is not changed.

Parameters:
inI - The imaginary number part this to which this ComplexNumber will be set.

getReal

public double getReal()
Returns the Real part of this ComplexNumber.

Returns:
The real number part this ComplexNumber.

getImaginary

public double getImaginary()
Returns the Imaginary part of this ComplexNumber.

Returns:
The imaginary number part this ComplexNumber.

distFromOrigin

public double distFromOrigin()
Returns the distance from the origin of the Complex Plane. This method is often useful for fractal calculations. This method performs a quick and dirty form of the math required to determine the distance from the origin. The answers it provides aren't accurate at all, but are derived quickly.

Returns:
The distance from the origin of the Complex Plane.

actualDistFromOrigin

public double actualDistFromOrigin()
Returns the actual distance from the origin of the Complex Plane. This method is often useful for fractal calculations. This method performs the full form of the math required to accurately produce the distance, thus making it's answers accurate but slow.

Returns:
The distance from the origin of the Complex Plane.

distFromOrigin

public static double distFromOrigin(ComplexNumber inComplex)
Returns the distance from the origin of the Complex Plane. This method is often useful for fractal calculations. This method performs a quick and dirty form of the math required to determine the distance from the origin. The answers it provides aren't accurate at all, but are derived quickly. This version returns the distance from the origin of passed in ComplexNumber.

Parameters:
inComplex - The ComplexNumber for which the distance from the origin will be calculated.
Returns:
The distance from the origin of the Complex Plane.

actualDistFromOrigin

public static double actualDistFromOrigin(ComplexNumber inComplex)
Returns the actual distance from the origin of the Complex Plane. This method is often useful for fractal calculations. This method performs the full form of the math required to accurately produce the distance, thus making it's answers accurate but slow. This version returns the distance from the origin of passed in ComplexNumber.

Parameters:
inComplex - The ComplexNumber for which the distance from the origin will be calculated.
Returns:
The distance from the origin of the Complex Plane.

isZero

public boolean isZero()
Determines whether or not both the real and imaginary parts of this complex number are zero.

Returns:
Whether or not both the real and imaginary parts of this complex number are zero.

toString

public java.lang.String toString()
Returns this ComplexNumber as a String.

Overrides:
toString in class java.lang.Object
Returns:
This ComplexNumber as a String.

add

public void add(ComplexNumber inComplex)
Calculates the sum of this ComplexNumber and the supplied ComplexNumber. The result of this calculation is the new value of this ComplexNumber.
This method should be used instead of the static version, as it is way faster.

Parameters:
inComplex - The ComplexNumber to be added to this one.

subtract

public void subtract(ComplexNumber inComplex)
Subtracts the supplied ComplexNumber from this ComplexNumber. The result of this calculation is the new value of this ComplexNumber.
This method should be used instead of the static version, as it is way faster.

Parameters:
inComplex - The ComplexNumber to be subtracted from this one.

multiply

public void multiply(ComplexNumber inComplex)
Multiplies this ComplexNumber by the supplied ComplexNumber. The result of this calculation is the new value of this ComplexNumber.
This method should be used instead of the static version, as it is way faster.

Parameters:
inComplex - The ComplexNumber to be multiplied by this one.

divide

public void divide(ComplexNumber inComplex)
Divides this ComplexNumber by the supplied ComplexNumber. The result of this calculation is the new value of this ComplexNumber.
This method should be used instead of the static version, as it is way faster.

Parameters:
inComplex - The ComplexNumber that will divide this one.

square

public void square()
Squares this ComplexNumber. The result of this calculation is the new value of this ComplexNumber.
This method should be used instead of the static version, as it is way faster.


add

public static ComplexNumber add(ComplexNumber inComplex1,
                                ComplexNumber inComplex2)
Calculates the sum of two ComplexNumbers. This function returns the result as a new ComplexNumber.

Parameters:
inComplex1 - The first ComplexNumber to be added.
inComplex2 - The second ComplexNumber to be added.
Returns:
The ComplexNumber sum of the two ComplexNumbers passed in.

subtract

public static ComplexNumber subtract(ComplexNumber inComplex1,
                                     ComplexNumber inComplex2)
Calculates the difference of two ComplexNumbers. This function returns the result as a new ComplexNumber.

Parameters:
inComplex1 - The ComplexNumber from which the second ComplexNumber will be subtracted.
inComplex2 - The ComplexNumber that will be subtracted from the first ComplexNumber.
Returns:
The ComplexNumber difference of the two ComplexNumbers passed in.

multiply

public static ComplexNumber multiply(ComplexNumber inComplex1,
                                     ComplexNumber inComplex2)
Calculates the product of two ComplexNumbers. This function returns the result as a new ComplexNumber.

Parameters:
inComplex1 - The first ComplexNumber to be multiplied.
inComplex2 - The second ComplexNumber to be multiplied.
Returns:
The ComplexNumber product of the two ComplexNumbers passed in.

divide

public static ComplexNumber divide(ComplexNumber inComplex1,
                                   ComplexNumber inComplex2)
Calculates the quotient of two ComplexNumbers. This function returns the result as a new ComplexNumber.

Parameters:
inComplex1 - The ComplexNumber that will be divided by the second ComplexNumber.
inComplex2 - The ComplexNumber that will be divide the first ComplexNumber.
Returns:
The ComplexNumber quotient of the two ComplexNumbers passed in.

square

public static ComplexNumber square(ComplexNumber inComplex1)
Calculates the square of the ComplexNumber passed in. This function returns the result as a new ComplexNumber.

Parameters:
inComplex1 - The ComplexNumber to square.
Returns:
The ComplexNumber that is the squared value of the parameter.