Package org.djutils.complex
Class Complex
java.lang.Object
org.djutils.complex.Complex
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-2024 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
Modifier and TypeFieldDescriptionstatic final Complex
The imaginary unit value (i).final double
Imaginary component.static final Complex
The negative imaginary unit value (i).static final Complex
The (real) minus one value in the complex number space.static final Complex
The (real) one value in the complex number space.final double
Real component.static final Complex
The zero value in the complex number space. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionConstruct the complex conjugate of this Complex.divideBy
(double rightOperand) Divide this Complex by a scalar.Divide this Complex by another Complex.boolean
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
Determine if this Complex has a real component of zero.boolean
isReal()
Determine if this Complex has an imaginary component of zero.minus
(double rightOperand) Subtract a scalar from this Complex.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.plus
(double rightOperand) Add a scalar to this Complex.Add this Complex and another Complex.Compute the reciprocal of this Complex.rotate
(double angle) Rotate this Complex by an angle.times
(double rightOperand) Multiply this Complex with a scalar.Multiply this Complex with another Complex.toString()
-
Field Details
-
re
public final double reReal component. -
im
public final double imImaginary component. -
ZERO
The zero value in the complex number space. -
ONE
The (real) one value in the complex number space. -
MINUS_ONE
The (real) minus one value in the complex number space. -
I
The imaginary unit value (i). -
MINUS_I
The negative imaginary unit value (i).
-
-
Constructor Details
-
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 Details
-
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
Construct the complex conjugate of this Complex.- Returns:
- Complex; the complex conjugate of this
-
rotate
Rotate this Complex by an angle.- Parameters:
angle
- double; the angle (in Radians)- Returns:
- complex; the result of the rotation
-
plus
Add this Complex and another Complex.- Parameters:
rightOperand
- Complex; the other Complex- Returns:
- Complex; the sum of this Complex and the other Complex
-
plus
Add a scalar to this Complex.- Parameters:
rightOperand
- double; the scalar- Returns:
- Complex; the sum of this Complex and the scalar
-
minus
Subtract another Complex from this Complex.- Parameters:
rightOperand
- Complex; the other Complex- Returns:
- Complex; the difference of this Complex and the other Complex
-
minus
Subtract a scalar from this Complex.- Parameters:
rightOperand
- double; the scalar- Returns:
- Complex; the difference of this Complex and the scalar
-
times
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
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
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
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
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
-
toString
-
hashCode
public int hashCode() -
equals
-