@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 java.io.Serializable
Constructor and Description |
---|
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
|
Modifier and Type | Method and Description |
---|---|
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(java.lang.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
|
java.lang.String |
toString() |
inverse
dotTimes, isZero, minus, negative, negativeEquals, plus, scale, scaledMinus, scaledMinusEquals, scaledPlus, zero
finalize, getClass, notify, notifyAll, wait, wait, wait
dotTimes, isZero, minus, negative, negativeEquals, plus, scale, scaledMinus, scaledMinusEquals, scaledPlus, zero
public ComplexNumber()
public ComplexNumber(double realPart, double imaginaryPart)
realPart
- real part of the complex numberimaginaryPart
- imaginary part of the complex numberpublic ComplexNumber(ComplexNumber other)
other
- ComplexNumber to copypublic ComplexNumber clone()
clone
in interface Ring<ComplexNumber>
clone
in interface CloneableSerializable
clone
in class AbstractRing<ComplexNumber>
public void plusEquals(ComplexNumber number)
plusEquals
in interface Ring<ComplexNumber>
number
- number to add this topublic double getMagnitude()
public double getPhase()
public ComplexNumber computeExponent()
public ComplexNumber computeNaturalLogarithm()
public void dotTimesEquals(ComplexNumber other)
Ring
this
and
other
dotTimesEquals
in interface Ring<ComplexNumber>
other
- elements of other will be multiplied to the corresponding
elements of this
public void minusEquals(ComplexNumber other)
Ring
other
from
this
minusEquals
in interface Ring<ComplexNumber>
other
- object to subtract from this
public void scaleEquals(double scaleFactor)
Ring
this
by
scaleFactor
scaleEquals
in interface Ring<ComplexNumber>
scaleFactor
- amount to scale the elements of this
public void scaledPlusEquals(double scaleFactor, ComplexNumber other)
Ring
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.scaledPlusEquals
in interface Ring<ComplexNumber>
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.public ComplexNumber times(ComplexNumber other)
times
in interface EuclideanRing<ComplexNumber>
times
in class AbstractEuclideanRing<ComplexNumber>
other
- complex number by which to multiply thispublic void timesEquals(ComplexNumber other)
timesEquals
in interface EuclideanRing<ComplexNumber>
other
- complex number by which to multiple thispublic ComplexNumber divide(ComplexNumber other)
divide
in interface EuclideanRing<ComplexNumber>
divide
in class AbstractEuclideanRing<ComplexNumber>
other
- complex number by which to divide thispublic void divideEquals(ComplexNumber other)
divideEquals
in interface EuclideanRing<ComplexNumber>
other
- complex number by which to divide this@Deprecated public ComplexNumber dividedBy(ComplexNumber other)
other
- The other complex number.@Deprecated public void dividedByEquals(ComplexNumber other)
other
- The other complex number.public void inverseEquals()
Field
inverseEquals
in interface Field<ComplexNumber>
public int hashCode()
hashCode
in class java.lang.Object
public boolean equals(java.lang.Object other)
Ring
equals
in interface Ring<ComplexNumber>
equals
in class java.lang.Object
other
- RingType to compare against this
public boolean equals(ComplexNumber other, double effectiveZero)
Ring
equals
in interface Ring<ComplexNumber>
other
- RingType to compare against this
effectiveZero
- tolerance threshold for element-wise equalitypublic java.lang.String toString()
toString
in class java.lang.Object
public double getRealPart()
public void setRealPart(double realPart)
realPart
- real portion of the complex numberpublic double getImaginaryPart()
public void setImaginaryPart(double imaginaryPart)
imaginaryPart
- imaginary portion of the complex numberpublic void conjugateEquals()
public ComplexNumber conjugate()
public boolean isZero(double effectiveZero)
Ring
isZero
in interface Ring<ComplexNumber>
effectiveZero
- Tolerance threshold for element-wise equality