Class ComplexNumber
- java.lang.Object
-
- org.noise_planet.noisemodelling.pathfinder.utils.ComplexNumber
-
public class ComplexNumber extends Object
ComplexNumber is a class which implements complex numbers in Java. It includes basic operations that can be performed on complex numbers such as, addition, subtraction, multiplication, conjugate, modulus and squaring. The data type for Complex Numbers. The features of this library include: Arithmetic Operations (addition, subtraction, multiplication, division) Complex Specific Operations - Conjugate, Inverse, Absolute/Magnitude, Argument/Phase Trigonometric Operations - sin, cos, tan, cot, sec, cosec Mathematical Functions - exp Complex Parsing of type x+yi- Version:
- 1.2
- Author:
- Abdul Fatir
-
-
Constructor Summary
Constructors Constructor Description ComplexNumber()Constructs a newComplexNumberobject with both real and imaginary parts 0 (z = 0 + 0i).ComplexNumber(double real, double imaginary)Constructs a newComplexNumberobject.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidadd(ComplexNumber z)Adds anotherComplexNumberto the current complex number.static ComplexNumberadd(ComplexNumber z1, ComplexNumber z2)Adds twoComplexNumber.ComplexNumberconjugate()The complex conjugate of the current complex number.static ComplexNumbercos(ComplexNumber z)Calculates the cosine of theComplexNumberstatic ComplexNumbercosec(ComplexNumber z)Calculates the co-secant of theComplexNumberstatic ComplexNumbercot(ComplexNumber z)Calculates the co-tangent of theComplexNumbervoiddivide(ComplexNumber z)Divides the currentComplexNumberby anotherComplexNumber.static ComplexNumberdivide(ComplexNumber z1, ComplexNumber z2)Divides oneComplexNumberby another.booleanequals(Object z)Checks if the passedComplexNumberis equal to the current.static ComplexNumberexp(ComplexNumber z)Calculates the exponential of theComplexNumberStringformat(int format_id)Formats the Complex number as x+yi or r.cis(theta)doublegetArg()The argument/phase of the current complex number.doublegetIm()The imaginary part ofComplexNumberdoublegetRe()The real part ofComplexNumberComplexNumberinverse()The inverse/reciprocal of the complex number.doublemod()The modulus, magnitude or the absolute value of current complex number.voidmultiply(ComplexNumber z)Multiplies anotherComplexNumberto the current complex number.static ComplexNumbermultiply(ComplexNumber z1, ComplexNumber z2)Multiplies oneComplexNumberto another.static ComplexNumberparseComplex(String s)Parses theStringas aComplexNumberof type x+yi.static ComplexNumberpow(ComplexNumber z, int power)Calculates theComplexNumberto the passed integer power.static ComplexNumbersec(ComplexNumber z)Calculates the secant of theComplexNumbervoidset(ComplexNumber z)Sets the value of current complex number to the passed complex number.static ComplexNumbersin(ComplexNumber z)Calculates the sine of theComplexNumberComplexNumbersquare()The square of the current complex number.voidsubtract(ComplexNumber z)Subtracts anotherComplexNumberfrom the current complex number.static ComplexNumbersubtract(ComplexNumber z1, ComplexNumber z2)Subtracts oneComplexNumberfrom another.static ComplexNumbertan(ComplexNumber z)Calculates the tangent of theComplexNumberStringtoString()
-
-
-
Field Detail
-
XY
public static final int XY
Used informat(int)to format the complex number as x+yi- See Also:
- Constant Field Values
-
RCIS
public static final int RCIS
Used informat(int)to format the complex number as R.cis(theta), where theta is arg(z)- See Also:
- Constant Field Values
-
-
Constructor Detail
-
ComplexNumber
public ComplexNumber()
Constructs a newComplexNumberobject with both real and imaginary parts 0 (z = 0 + 0i).
-
ComplexNumber
public ComplexNumber(double real, double imaginary)Constructs a newComplexNumberobject.- Parameters:
real- the real part, Re(z), of the complex numberimaginary- the imaginary part, Im(z), of the complex number
-
-
Method Detail
-
add
public void add(ComplexNumber z)
Adds anotherComplexNumberto the current complex number.- Parameters:
z- the complex number to be added to the current complex number
-
subtract
public void subtract(ComplexNumber z)
Subtracts anotherComplexNumberfrom the current complex number.- Parameters:
z- the complex number to be subtracted from the current complex number
-
multiply
public void multiply(ComplexNumber z)
Multiplies anotherComplexNumberto the current complex number.- Parameters:
z- the complex number to be multiplied to the current complex number
-
divide
public void divide(ComplexNumber z)
Divides the currentComplexNumberby anotherComplexNumber.- Parameters:
z- the divisor
-
set
public void set(ComplexNumber z)
Sets the value of current complex number to the passed complex number.- Parameters:
z- the complex number
-
add
public static ComplexNumber add(ComplexNumber z1, ComplexNumber z2)
Adds twoComplexNumber.- Parameters:
z1- the firstComplexNumber.z2- the secondComplexNumber.- Returns:
- the resultant
ComplexNumber(z1 + z2).
-
subtract
public static ComplexNumber subtract(ComplexNumber z1, ComplexNumber z2)
Subtracts oneComplexNumberfrom another.- Parameters:
z1- the firstComplexNumber.z2- the secondComplexNumber.- Returns:
- the resultant
ComplexNumber(z1 - z2).
-
multiply
public static ComplexNumber multiply(ComplexNumber z1, ComplexNumber z2)
Multiplies oneComplexNumberto another.- Parameters:
z1- the firstComplexNumber.z2- the secondComplexNumber.- Returns:
- the resultant
ComplexNumber(z1 * z2).
-
divide
public static ComplexNumber divide(ComplexNumber z1, ComplexNumber z2)
Divides oneComplexNumberby another.- Parameters:
z1- the firstComplexNumber.z2- the secondComplexNumber.- Returns:
- the resultant
ComplexNumber(z1 / z2).
-
conjugate
public ComplexNumber conjugate()
The complex conjugate of the current complex number.- Returns:
- a
ComplexNumberobject which is the conjugate of the current complex number
-
mod
public double mod()
The modulus, magnitude or the absolute value of current complex number.- Returns:
- the magnitude or modulus of current complex number
-
square
public ComplexNumber square()
The square of the current complex number.- Returns:
- a
ComplexNumberwhich is the square of the current complex number.
-
toString
public String toString()
-
exp
public static ComplexNumber exp(ComplexNumber z)
Calculates the exponential of theComplexNumber- Parameters:
z- The input complex number- Returns:
- a
ComplexNumberwhich is e^(input z)
-
pow
public static ComplexNumber pow(ComplexNumber z, int power)
Calculates theComplexNumberto the passed integer power.- Parameters:
z- The input complex numberpower- The power.- Returns:
- a
ComplexNumberwhich is (z)^power
-
sin
public static ComplexNumber sin(ComplexNumber z)
Calculates the sine of theComplexNumber- Parameters:
z- the input complex number- Returns:
- a
ComplexNumberwhich is the sine of z.
-
cos
public static ComplexNumber cos(ComplexNumber z)
Calculates the cosine of theComplexNumber- Parameters:
z- the input complex number- Returns:
- a
ComplexNumberwhich is the cosine of z.
-
tan
public static ComplexNumber tan(ComplexNumber z)
Calculates the tangent of theComplexNumber- Parameters:
z- the input complex number- Returns:
- a
ComplexNumberwhich is the tangent of z.
-
cot
public static ComplexNumber cot(ComplexNumber z)
Calculates the co-tangent of theComplexNumber- Parameters:
z- the input complex number- Returns:
- a
ComplexNumberwhich is the co-tangent of z.
-
sec
public static ComplexNumber sec(ComplexNumber z)
Calculates the secant of theComplexNumber- Parameters:
z- the input complex number- Returns:
- a
ComplexNumberwhich is the secant of z.
-
cosec
public static ComplexNumber cosec(ComplexNumber z)
Calculates the co-secant of theComplexNumber- Parameters:
z- the input complex number- Returns:
- a
ComplexNumberwhich is the co-secant of z.
-
getRe
public double getRe()
The real part ofComplexNumber- Returns:
- the real part of the complex number
-
getIm
public double getIm()
The imaginary part ofComplexNumber- Returns:
- the imaginary part of the complex number
-
getArg
public double getArg()
The argument/phase of the current complex number.- Returns:
- arg(z) - the argument of current complex number
-
parseComplex
public static ComplexNumber parseComplex(String s)
Parses theStringas aComplexNumberof type x+yi.- Parameters:
s- the input complex number as string- Returns:
- a
ComplexNumberwhich is represented by the string.
-
equals
public final boolean equals(Object z)
Checks if the passedComplexNumberis equal to the current.
-
inverse
public ComplexNumber inverse()
The inverse/reciprocal of the complex number.- Returns:
- the reciprocal of current complex number.
-
format
public String format(int format_id) throws IllegalArgumentException
Formats the Complex number as x+yi or r.cis(theta)- Parameters:
format_id- the format IDComplexNumber.XYorComplexNumber.RCIS.- Returns:
- a string representation of the complex number
- Throws:
IllegalArgumentException- if the format_id does not match.
-
-