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 newComplexNumber
object with both real and imaginary parts 0 (z = 0 + 0i).ComplexNumber(double real, double imaginary)
Constructs a newComplexNumber
object.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
add(ComplexNumber z)
Adds anotherComplexNumber
to the current complex number.static ComplexNumber
add(ComplexNumber z1, ComplexNumber z2)
Adds twoComplexNumber
.ComplexNumber
conjugate()
The complex conjugate of the current complex number.static ComplexNumber
cos(ComplexNumber z)
Calculates the cosine of theComplexNumber
static ComplexNumber
cosec(ComplexNumber z)
Calculates the co-secant of theComplexNumber
static ComplexNumber
cot(ComplexNumber z)
Calculates the co-tangent of theComplexNumber
void
divide(ComplexNumber z)
Divides the currentComplexNumber
by anotherComplexNumber
.static ComplexNumber
divide(ComplexNumber z1, ComplexNumber z2)
Divides oneComplexNumber
by another.boolean
equals(Object z)
Checks if the passedComplexNumber
is equal to the current.static ComplexNumber
exp(ComplexNumber z)
Calculates the exponential of theComplexNumber
String
format(int format_id)
Formats the Complex number as x+yi or r.cis(theta)double
getArg()
The argument/phase of the current complex number.double
getIm()
The imaginary part ofComplexNumber
double
getRe()
The real part ofComplexNumber
ComplexNumber
inverse()
The inverse/reciprocal of the complex number.double
mod()
The modulus, magnitude or the absolute value of current complex number.void
multiply(ComplexNumber z)
Multiplies anotherComplexNumber
to the current complex number.static ComplexNumber
multiply(ComplexNumber z1, ComplexNumber z2)
Multiplies oneComplexNumber
to another.static ComplexNumber
parseComplex(String s)
Parses theString
as aComplexNumber
of type x+yi.static ComplexNumber
pow(ComplexNumber z, int power)
Calculates theComplexNumber
to the passed integer power.static ComplexNumber
sec(ComplexNumber z)
Calculates the secant of theComplexNumber
void
set(ComplexNumber z)
Sets the value of current complex number to the passed complex number.static ComplexNumber
sin(ComplexNumber z)
Calculates the sine of theComplexNumber
ComplexNumber
square()
The square of the current complex number.void
subtract(ComplexNumber z)
Subtracts anotherComplexNumber
from the current complex number.static ComplexNumber
subtract(ComplexNumber z1, ComplexNumber z2)
Subtracts oneComplexNumber
from another.static ComplexNumber
tan(ComplexNumber z)
Calculates the tangent of theComplexNumber
String
toString()
-
-
-
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 newComplexNumber
object with both real and imaginary parts 0 (z = 0 + 0i).
-
ComplexNumber
public ComplexNumber(double real, double imaginary)
Constructs a newComplexNumber
object.- 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 anotherComplexNumber
to the current complex number.- Parameters:
z
- the complex number to be added to the current complex number
-
subtract
public void subtract(ComplexNumber z)
Subtracts anotherComplexNumber
from the current complex number.- Parameters:
z
- the complex number to be subtracted from the current complex number
-
multiply
public void multiply(ComplexNumber z)
Multiplies anotherComplexNumber
to 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 currentComplexNumber
by 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 oneComplexNumber
from another.- Parameters:
z1
- the firstComplexNumber
.z2
- the secondComplexNumber
.- Returns:
- the resultant
ComplexNumber
(z1 - z2).
-
multiply
public static ComplexNumber multiply(ComplexNumber z1, ComplexNumber z2)
Multiplies oneComplexNumber
to another.- Parameters:
z1
- the firstComplexNumber
.z2
- the secondComplexNumber
.- Returns:
- the resultant
ComplexNumber
(z1 * z2).
-
divide
public static ComplexNumber divide(ComplexNumber z1, ComplexNumber z2)
Divides oneComplexNumber
by 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
ComplexNumber
object 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
ComplexNumber
which 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
ComplexNumber
which is e^(input z)
-
pow
public static ComplexNumber pow(ComplexNumber z, int power)
Calculates theComplexNumber
to the passed integer power.- Parameters:
z
- The input complex numberpower
- The power.- Returns:
- a
ComplexNumber
which is (z)^power
-
sin
public static ComplexNumber sin(ComplexNumber z)
Calculates the sine of theComplexNumber
- Parameters:
z
- the input complex number- Returns:
- a
ComplexNumber
which 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
ComplexNumber
which 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
ComplexNumber
which 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
ComplexNumber
which 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
ComplexNumber
which 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
ComplexNumber
which 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 theString
as aComplexNumber
of type x+yi.- Parameters:
s
- the input complex number as string- Returns:
- a
ComplexNumber
which is represented by the string.
-
equals
public final boolean equals(Object z)
Checks if the passedComplexNumber
is 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.XY
orComplexNumber.RCIS
.- Returns:
- a string representation of the complex number
- Throws:
IllegalArgumentException
- if the format_id does not match.
-
-