Package org.djutils.complex
Class Complex
- java.lang.Object
-
- org.djutils.complex.Complex
-
public class Complex extends Object
Complex.java. Immutable complex numbers. This implementation stores the real and imaginary component as a double value. These fields are directly accessible, or through a getter. There are also getters for the norm and phi, but this are a CPU intensive.Copyright (c) 2021-2021 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
BSD-style license. See DJUTILS License.- Author:
- Alexander Verbraeck, Peter Knoppers
-
-
Field Summary
Fields Modifier and Type Field Description static Complex
I
The imaginary unit value (i).double
im
Imaginary component.static Complex
MINUS_I
The negative imaginary unit value (i).static Complex
MINUS_ONE
The (real) minus one value in the complex number space.static Complex
ONE
The (real) one value in the complex number space.double
re
Real component.static Complex
ZERO
The zero value in the complex number space.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Complex
conjugate()
Construct the complex conjugate of this Complex.Complex
divideBy(double rightOperand)
Divide this Complex by a scalar.Complex
divideBy(Complex rightOperand)
Divide this Complex by another Complex.boolean
equals(Object obj)
double
getIm()
Retrieve the imaginary component of this complex number.double
getRe()
Retrieve the real component of this complex number.int
hashCode()
static double
hypot(double x, double y)
Better implementation of the hypotenuse function (faster and more accurate than the one in the java Math library).boolean
isImaginary()
Determine if this Complex has a real component of zero.boolean
isReal()
Determine if this Complex has an imaginary component of zero.Complex
minus(double rightOperand)
Subtract a scalar from this Complex.Complex
minus(Complex rightOperand)
Subtract another Complex from this Complex.double
norm()
Compute and return the norm, or radius, or absolute value of this complex number.double
phi()
Compute and return the phase or phi of this complex number.Complex
plus(double rightOperand)
Add a scalar to this Complex.Complex
plus(Complex rightOperand)
Add this Complex and another Complex.Complex
reciprocal()
Compute the reciprocal of this Complex.Complex
rotate(double angle)
Rotate this Complex by an angle.Complex
times(double rightOperand)
Multiply this Complex with a scalar.Complex
times(Complex rightOperand)
Multiply this Complex with another Complex.String
toString()
-
-
-
Field Detail
-
re
public final double re
Real component.
-
im
public final double im
Imaginary component.
-
ZERO
public static final Complex ZERO
The zero value in the complex number space.
-
ONE
public static final Complex ONE
The (real) one value in the complex number space.
-
MINUS_ONE
public static final Complex MINUS_ONE
The (real) minus one value in the complex number space.
-
I
public static final Complex I
The imaginary unit value (i).
-
MINUS_I
public static final Complex MINUS_I
The negative imaginary unit value (i).
-
-
Constructor Detail
-
Complex
public Complex(double re, double im)
Construct a new complex number.- Parameters:
re
- double; real component of the new complex numberim
- double; imaginary component of the new complex number
-
Complex
public Complex(double re)
Construct a new complex number with specified real component and zero imaginary component.- Parameters:
re
- double; the real component of the new complex number
-
-
Method Detail
-
getRe
public double getRe()
Retrieve the real component of this complex number.- Returns:
- double; the real component of this complex number
-
getIm
public double getIm()
Retrieve the imaginary component of this complex number.- Returns:
- double; the imaginary component of this complex number
-
norm
public double norm()
Compute and return the norm, or radius, or absolute value of this complex number.- Returns:
- double; the norm, or radius, or absolute value of this complex number. Due to the fact that in this implementation of complex numbers, all values are stored as a real an imaginary double value; the result may overflow to Double.POSITIVE_INFINITY, even though both the real and imaginary part of the complex number can be represented.
-
hypot
public static double hypot(double x, double y)
Better implementation of the hypotenuse function (faster and more accurate than the one in the java Math library).
Derived from An improved algorithm for hypot(a, b) by Carlos F. Borges.- Parameters:
x
- double; the x valuey
- double; the y value- Returns:
- double; hypot(x, y)
-
phi
public double phi()
Compute and return the phase or phi of this complex number.- Returns:
- double; the phase or phi of this complex number in Radians. Due to the fact that in this implementation of complex numbers, all values are stored as a real and imaginary value; the result of this method is always normalized to the interval (-π,π].
-
isReal
public boolean isReal()
Determine if this Complex has an imaginary component of zero.- Returns:
- boolean; true if the imaginary component of this Complex number is 0.0; false if the imaginary component of this Complex number is not 0.0.
-
isImaginary
public boolean isImaginary()
Determine if this Complex has a real component of zero.- Returns:
- boolean; true if the real component of this Complex number is 0.0; false if the real component of this Complex number is not 0.0
-
conjugate
public Complex conjugate()
Construct the complex conjugate of this Complex.- Returns:
- Complex; the complex conjugate of this
-
rotate
public Complex rotate(double angle)
Rotate this Complex by an angle.- Parameters:
angle
- double; the angle (in Radians)- Returns:
- complex; the result of the rotation
-
plus
public Complex plus(Complex rightOperand)
Add this Complex and another Complex.- Parameters:
rightOperand
- Complex; the other Complex- Returns:
- Complex; the sum of this Complex and the other Complex
-
plus
public Complex plus(double rightOperand)
Add a scalar to this Complex.- Parameters:
rightOperand
- double; the scalar- Returns:
- Complex; the sum of this Complex and the scalar
-
minus
public Complex minus(Complex rightOperand)
Subtract another Complex from this Complex.- Parameters:
rightOperand
- Complex; the other Complex- Returns:
- Complex; the difference of this Complex and the other Complex
-
minus
public Complex minus(double rightOperand)
Subtract a scalar from this Complex.- Parameters:
rightOperand
- double; the scalar- Returns:
- Complex; the difference of this Complex and the scalar
-
times
public Complex times(Complex rightOperand)
Multiply this Complex with another Complex.- Parameters:
rightOperand
- Complex; the right hand side operand- Returns:
- Complex; the product of this Complex and the other Complex
-
times
public Complex times(double rightOperand)
Multiply this Complex with a scalar.- Parameters:
rightOperand
- double; the right hand side operand- Returns:
- Complex; the product of this Complex and the scalar
-
reciprocal
public Complex reciprocal()
Compute the reciprocal of this Complex. If this is zero, the result will have the re field set to Double.POSITIVE_INFINITY and the imaginary part set to NEGATIVE_INFINITY.- Returns:
- Complex; the reciprocal of this Complex (1 / this)
-
divideBy
public Complex divideBy(Complex rightOperand)
Divide this Complex by another Complex. Division by ZERO yields a Complex with re set to Infinity if this.re != 0 and NaN if this.re == 0 and im set to Infinity if this.im != 0 and NaN if this.im == 0.- Parameters:
rightOperand
- Complex; the right hand side operand- Returns:
- Complex; the ratio of this Complex and the right hand side operand
-
divideBy
public Complex divideBy(double rightOperand)
Divide this Complex by a scalar. Division by 0.0 yields a Complex with re set to Infinity if this.re != 0 and NaN if this.re == 0 and im set to Infinity if this.im != 0 and NaN if this.im == 0.- Parameters:
rightOperand
- double; the scalar right hand side operand- Returns:
- Complex; the ratio of this Complex and the right hand side operand
-
-