Class ComplexNumber
java.lang.Object
org.noise_planet.noisemodelling.pathfinder.utils.ComplexNumber
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
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionConstructs a newComplexNumberobject with both real and imaginary parts 0 (z = 0 + 0i).ComplexNumber(double real, double imaginary) Constructs a newComplexNumberobject. -
Method Summary
Modifier and TypeMethodDescriptionvoidadd(ComplexNumber z) Adds anotherComplexNumberto the current complex number.static ComplexNumberadd(ComplexNumber z1, ComplexNumber z2) Adds twoComplexNumber.The complex conjugate of the current complex number.static ComplexNumbercos(ComplexNumber z) Calculates the cosine of theComplexNumberstatic ComplexNumberCalculates the co-secant of theComplexNumberstatic ComplexNumbercot(ComplexNumber z) Calculates the co-tangent of theComplexNumbervoidDivides the currentComplexNumberby anotherComplexNumber.static ComplexNumberdivide(ComplexNumber z1, ComplexNumber z2) Divides oneComplexNumberby another.final booleanChecks if the passedComplexNumberis equal to the current.static ComplexNumberexp(ComplexNumber z) Calculates the exponential of theComplexNumberformat(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 ofComplexNumberinverse()The inverse/reciprocal of the complex number.doublemod()The modulus, magnitude or the absolute value of current complex number.voidMultiplies anotherComplexNumberto the current complex number.static ComplexNumbermultiply(ComplexNumber z1, ComplexNumber z2) Multiplies oneComplexNumberto another.static ComplexNumberParses 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 theComplexNumbersquare()The square of the current complex number.voidSubtracts anotherComplexNumberfrom the current complex number.static ComplexNumbersubtract(ComplexNumber z1, ComplexNumber z2) Subtracts oneComplexNumberfrom another.static ComplexNumbertan(ComplexNumber z) Calculates the tangent of theComplexNumbertoString()
-
Field Details
-
XY
public static final int XYUsed informat(int)to format the complex number as x+yi- See Also:
-
RCIS
public static final int RCISUsed informat(int)to format the complex number as R.cis(theta), where theta is arg(z)- See Also:
-
-
Constructor Details
-
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 Details
-
add
Adds anotherComplexNumberto the current complex number.- Parameters:
z- the complex number to be added to the current complex number
-
subtract
Subtracts anotherComplexNumberfrom the current complex number.- Parameters:
z- the complex number to be subtracted from the current complex number
-
multiply
Multiplies anotherComplexNumberto the current complex number.- Parameters:
z- the complex number to be multiplied to the current complex number
-
divide
Divides the currentComplexNumberby anotherComplexNumber.- Parameters:
z- the divisor
-
set
Sets the value of current complex number to the passed complex number.- Parameters:
z- the complex number
-
add
Adds twoComplexNumber.- Parameters:
z1- the firstComplexNumber.z2- the secondComplexNumber.- Returns:
- the resultant
ComplexNumber(z1 + z2).
-
subtract
Subtracts oneComplexNumberfrom another.- Parameters:
z1- the firstComplexNumber.z2- the secondComplexNumber.- Returns:
- the resultant
ComplexNumber(z1 - z2).
-
multiply
Multiplies oneComplexNumberto another.- Parameters:
z1- the firstComplexNumber.z2- the secondComplexNumber.- Returns:
- the resultant
ComplexNumber(z1 * z2).
-
divide
Divides oneComplexNumberby another.- Parameters:
z1- the firstComplexNumber.z2- the secondComplexNumber.- Returns:
- the resultant
ComplexNumber(z1 / z2).
-
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
The square of the current complex number.- Returns:
- a
ComplexNumberwhich is the square of the current complex number.
-
toString
-
exp
Calculates the exponential of theComplexNumber- Parameters:
z- The input complex number- Returns:
- a
ComplexNumberwhich is e^(input z)
-
pow
Calculates theComplexNumberto the passed integer power.- Parameters:
z- The input complex numberpower- The power.- Returns:
- a
ComplexNumberwhich is (z)^power
-
sin
Calculates the sine of theComplexNumber- Parameters:
z- the input complex number- Returns:
- a
ComplexNumberwhich is the sine of z.
-
cos
Calculates the cosine of theComplexNumber- Parameters:
z- the input complex number- Returns:
- a
ComplexNumberwhich is the cosine of z.
-
tan
Calculates the tangent of theComplexNumber- Parameters:
z- the input complex number- Returns:
- a
ComplexNumberwhich is the tangent of z.
-
cot
Calculates the co-tangent of theComplexNumber- Parameters:
z- the input complex number- Returns:
- a
ComplexNumberwhich is the co-tangent of z.
-
sec
Calculates the secant of theComplexNumber- Parameters:
z- the input complex number- Returns:
- a
ComplexNumberwhich is the secant of z.
-
cosec
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
Parses theStringas aComplexNumberof type x+yi.- Parameters:
s- the input complex number as string- Returns:
- a
ComplexNumberwhich is represented by the string.
-
equals
Checks if the passedComplexNumberis equal to the current. -
inverse
The inverse/reciprocal of the complex number.- Returns:
- the reciprocal of current complex number.
-
format
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.
-