gov.sandia.cognition.math
Class ComplexNumber

java.lang.Object
  extended by gov.sandia.cognition.util.AbstractCloneableSerializable
      extended by gov.sandia.cognition.math.AbstractRing<RingType>
          extended by gov.sandia.cognition.math.AbstractEuclideanRing<FieldType>
              extended by gov.sandia.cognition.math.AbstractField<ComplexNumber>
                  extended by gov.sandia.cognition.math.ComplexNumber
All Implemented Interfaces:
EuclideanRing<ComplexNumber>, Field<ComplexNumber>, Ring<ComplexNumber>, CloneableSerializable, Serializable, Cloneable

@CodeReviews(reviews={@CodeReview(reviewer="Kevin R. Dixon",date="2008-02-08",changesNeeded=false,comments={"Fixed one or two typos in comments.","Should ComplexNumber be an interface, with implementations being rectangular and polar?","Certainly doesn\'t need to be addressed now, but something to think about.","Otherwise, looks fine."}),@CodeReview(reviewer="Jonathan McClain",date="2006-05-15",changesNeeded=false,comments="Looks good, but advise a second person go over this because there is a lot of math here.")})
@PublicationReference(author="Wikipedia",
                      title="Complex number",
                      type=WebPage,
                      year=2008,
                      url="http://en.wikipedia.org/wiki/Complex_number")
public class ComplexNumber
extends AbstractField<ComplexNumber>
implements Serializable

Represents a complex number in a rectangular manner, explicitly storing the real and imaginary portions: real + j*imaginary

Since:
1.0
Author:
Kevin R. Dixon
See Also:
Serialized Form

Constructor Summary
ComplexNumber()
          Creates a new instance of ComplexNumber with zero magnitude.
ComplexNumber(ComplexNumber other)
          Copy constructor
ComplexNumber(double realPart, double imaginaryPart)
          Creates a new instance of ComplexNumber using the specified complex parts
 
Method Summary
 ComplexNumber clone()
          Returns a deep copy of this
 ComplexNumber computeExponent()
          Computes the natural-base exponent of the complex number, such that this = log(exp(this)) = exp(log(this))
 ComplexNumber computeNaturalLogarithm()
          Computes the natural-base logarithm of the complex number, such that this = log(exp(this)) = exp(log(this))
 ComplexNumber conjugate()
          Switches the sign of the imaginary part of this complex number.
 void conjugateEquals()
          Switches the sign of the imaginary part of this complex number.
 ComplexNumber divide(ComplexNumber other)
          Arithmetic division of this by other using polar coordinates: magnitude = this.magnitude / other.magnitude phase = this.phase - other.phase answer.realPart = magnitude * cos( phase ) answer.imaginaryPart = magnitude * sin( phase )
 ComplexNumber dividedBy(ComplexNumber other)
          Deprecated. Use divide.
 void dividedByEquals(ComplexNumber other)
          Deprecated. Use divideEquals.
 void divideEquals(ComplexNumber other)
          Inline arithmetic division of this by other: this.magnitude /= other.magnitude this.phase -= other.phase
 void dotTimesEquals(ComplexNumber other)
          Inline element-wise multiplication of this and other
 boolean equals(ComplexNumber other, double effectiveZero)
          Determines if two RingType objects are effectively equal
 boolean equals(Object other)
          Determines if two RingType objects are equal
 double getImaginaryPart()
          Returns the imaginary portion of the complex number, no computation needed
 double getMagnitude()
          Computes the magnitude of the complex number, sometimes called the length of the number.
 double getPhase()
          Computes the phase in radians of the complex number, sometimes called the angle.
 double getRealPart()
          Returns the real portion of the complex number, no computation needed
 int hashCode()
           
 void inverseEquals()
          Changes this value to be its inverse.
 boolean isZero(double effectiveZero)
          Determines if this ring is equal to zero using the element-wise effective zero value.
 void minusEquals(ComplexNumber other)
          Inline arithmetic subtraction of other from this
 void plusEquals(ComplexNumber number)
          Inline addition between this and the complex number
 void scaledPlusEquals(double scaleFactor, ComplexNumber other)
          Inline arithmetic addition of this and other after element-wise scaling of other by scaleFactor.
 void scaleEquals(double scaleFactor)
          Inline element-wise scaling of this by scaleFactor
 void setImaginaryPart(double imaginaryPart)
          Sets the imaginary portion of the complex number
 void setRealPart(double realPart)
          Sets the real part of the complex number
 ComplexNumber times(ComplexNumber other)
          Arithmetic multiplication of this and other using polar coordinates: magnitude = this.magnitude * other.magnitude phase = this.phase + other.phase answer.realPart = magnitude * cos( phase ) answer.imaginaryPart = magnitude * sin( phase )
 void timesEquals(ComplexNumber other)
          Inline arithmetic multiplication of this and other: this.magnitude *= other.magnitude this.phase += other.phase
 String toString()
           
 
Methods inherited from class gov.sandia.cognition.math.AbstractField
inverse
 
Methods inherited from class gov.sandia.cognition.math.AbstractRing
dotTimes, isZero, minus, negative, negativeEquals, plus, scale, scaledMinus, scaledMinusEquals, scaledPlus, zero
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface gov.sandia.cognition.math.Ring
dotTimes, isZero, minus, negative, negativeEquals, plus, scale, scaledMinus, scaledMinusEquals, scaledPlus, zero
 

Constructor Detail

ComplexNumber

public ComplexNumber()
Creates a new instance of ComplexNumber with zero magnitude.


ComplexNumber

public ComplexNumber(double realPart,
                     double imaginaryPart)
Creates a new instance of ComplexNumber using the specified complex parts

Parameters:
realPart - real part of the complex number
imaginaryPart - imaginary part of the complex number

ComplexNumber

public ComplexNumber(ComplexNumber other)
Copy constructor

Parameters:
other - ComplexNumber to copy
Method Detail

clone

public ComplexNumber clone()
Returns a deep copy of this

Specified by:
clone in interface Ring<ComplexNumber>
Specified by:
clone in interface CloneableSerializable
Overrides:
clone in class AbstractRing<ComplexNumber>
Returns:
deep copy of this, with the same complex parts

plusEquals

public void plusEquals(ComplexNumber number)
Inline addition between this and the complex number

Specified by:
plusEquals in interface Ring<ComplexNumber>
Parameters:
number - number to add this to

getMagnitude

public double getMagnitude()
Computes the magnitude of the complex number, sometimes called the length of the number. So that if the number is stored as "x + j*y", then the magnitude is sqrt( x*x + y*y )

Returns:
magnitude of the complex number

getPhase

public double getPhase()
Computes the phase in radians of the complex number, sometimes called the angle. If the number is stored as "x + j*y", then the phase is atan2( y, x )

Returns:
phase, in radians, of the complex number

computeExponent

public ComplexNumber computeExponent()
Computes the natural-base exponent of the complex number, such that this = log(exp(this)) = exp(log(this))

Returns:
exp( x + j*y )

computeNaturalLogarithm

public ComplexNumber computeNaturalLogarithm()
Computes the natural-base logarithm of the complex number, such that this = log(exp(this)) = exp(log(this))

Returns:
log( x + j*y )

dotTimesEquals

public void dotTimesEquals(ComplexNumber other)
Description copied from interface: Ring
Inline element-wise multiplication of this and other

Specified by:
dotTimesEquals in interface Ring<ComplexNumber>
Parameters:
other - elements of other will be multiplied to the corresponding elements of this

minusEquals

public void minusEquals(ComplexNumber other)
Description copied from interface: Ring
Inline arithmetic subtraction of other from this

Specified by:
minusEquals in interface Ring<ComplexNumber>
Parameters:
other - object to subtract from this

scaleEquals

public void scaleEquals(double scaleFactor)
Description copied from interface: Ring
Inline element-wise scaling of this by scaleFactor

Specified by:
scaleEquals in interface Ring<ComplexNumber>
Parameters:
scaleFactor - amount to scale the elements of this

scaledPlusEquals

public void scaledPlusEquals(double scaleFactor,
                             ComplexNumber other)
Description copied from interface: Ring
Inline arithmetic addition of this and other after element-wise scaling of other by scaleFactor. If this is x, other is y, and scaleFactor is a, then this method is equivalent to x += a * y. It is typically a more efficient way of doing this.plusEquals(other.scale(scaleFactor)) since it can avoid intermediate object creation.

Specified by:
scaledPlusEquals in interface Ring<ComplexNumber>
Parameters:
scaleFactor - The scale factor to multiply by the elements of other before adding to the elements of this.
other - Object to scale and then add to this.

times

public ComplexNumber times(ComplexNumber other)
Arithmetic multiplication of this and other using polar coordinates: magnitude = this.magnitude * other.magnitude phase = this.phase + other.phase answer.realPart = magnitude * cos( phase ) answer.imaginaryPart = magnitude * sin( phase )

Specified by:
times in interface EuclideanRing<ComplexNumber>
Overrides:
times in class AbstractEuclideanRing<ComplexNumber>
Parameters:
other - complex number by which to multiply this
Returns:
answer

timesEquals

public void timesEquals(ComplexNumber other)
Inline arithmetic multiplication of this and other: this.magnitude *= other.magnitude this.phase += other.phase

Specified by:
timesEquals in interface EuclideanRing<ComplexNumber>
Parameters:
other - complex number by which to multiple this

divide

public ComplexNumber divide(ComplexNumber other)
Arithmetic division of this by other using polar coordinates: magnitude = this.magnitude / other.magnitude phase = this.phase - other.phase answer.realPart = magnitude * cos( phase ) answer.imaginaryPart = magnitude * sin( phase )

Specified by:
divide in interface EuclideanRing<ComplexNumber>
Overrides:
divide in class AbstractEuclideanRing<ComplexNumber>
Parameters:
other - complex number by which to divide this
Returns:
answer

divideEquals

public void divideEquals(ComplexNumber other)
Inline arithmetic division of this by other: this.magnitude /= other.magnitude this.phase -= other.phase

Specified by:
divideEquals in interface EuclideanRing<ComplexNumber>
Parameters:
other - complex number by which to divide this

dividedBy

@Deprecated
public ComplexNumber dividedBy(ComplexNumber other)
Deprecated. Use divide.

Arithmetic division.

Parameters:
other - The other complex number.
Returns:
A new complex number.

dividedByEquals

@Deprecated
public void dividedByEquals(ComplexNumber other)
Deprecated. Use divideEquals.

Inline arithmetic division.

Parameters:
other - The other complex number.

inverseEquals

public void inverseEquals()
Description copied from interface: Field
Changes this value to be its inverse.

Specified by:
inverseEquals in interface Field<ComplexNumber>

hashCode

public int hashCode()
Overrides:
hashCode in class Object

equals

public boolean equals(Object other)
Description copied from interface: Ring
Determines if two RingType objects are equal

Specified by:
equals in interface Ring<ComplexNumber>
Overrides:
equals in class Object
Parameters:
other - RingType to compare against this
Returns:
True if the two objects are equal, false otherwise

equals

public boolean equals(ComplexNumber other,
                      double effectiveZero)
Description copied from interface: Ring
Determines if two RingType objects are effectively equal

Specified by:
equals in interface Ring<ComplexNumber>
Parameters:
other - RingType to compare against this
effectiveZero - tolerance threshold for element-wise equality
Returns:
True if the two objects are equal, false otherwise

toString

public String toString()
Overrides:
toString in class Object

getRealPart

public double getRealPart()
Returns the real portion of the complex number, no computation needed

Returns:
real part of the complex number

setRealPart

public void setRealPart(double realPart)
Sets the real part of the complex number

Parameters:
realPart - real portion of the complex number

getImaginaryPart

public double getImaginaryPart()
Returns the imaginary portion of the complex number, no computation needed

Returns:
imaginary part of the complex number

setImaginaryPart

public void setImaginaryPart(double imaginaryPart)
Sets the imaginary portion of the complex number

Parameters:
imaginaryPart - imaginary portion of the complex number

conjugateEquals

public void conjugateEquals()
Switches the sign of the imaginary part of this complex number. That is, if y = a + jb, then its conjugate equals y_conjugate = a - jb.


conjugate

public ComplexNumber conjugate()
Switches the sign of the imaginary part of this complex number. That is, if y = a + jb, then its conjugate equals y_conjugate = a - jb.

Returns:
The conjugate of this complex number.

isZero

public boolean isZero(double effectiveZero)
Description copied from interface: Ring
Determines if this ring is equal to zero using the element-wise effective zero value.

Specified by:
isZero in interface Ring<ComplexNumber>
Parameters:
effectiveZero - Tolerance threshold for element-wise equality
Returns:
True if all of the elements of this ring are effectively zero.