/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.apache.commons.math.complex; import java.io.Serializable; import java.util.ArrayList; import java.util.List; import org.apache.commons.math.FieldElement; import org.apache.commons.math.MathRuntimeException; import org.apache.commons.math.exception.util.LocalizedFormats; import org.apache.commons.math.util.MathUtils; import org.apache.commons.math.util.FastMath; /** * Representation of a Complex number - a number which has both a * real and imaginary part. *
* Implementations of arithmetic operations handle NaN
and
* infinite values according to the rules for {@link java.lang.Double}
* arithmetic, applying definitional formulas and returning NaN
or
* infinite values in real or imaginary parts as these arise in computation.
* See individual method javadocs for details.
* {@link #equals} identifies all values with NaN
in either real
* or imaginary part - e.g.,
* 1 + NaNi == NaN + i == NaN + NaNi.
*
* implements Serializable since 2.0
*
* @version $Revision: 990655 $ $Date: 2010-08-29 23:49:40 +0200 (dim. 29 août 2010) $
*/
public class Complex implements FieldElement
* Returns NaN
if either real or imaginary part is
* NaN
and Double.POSITIVE_INFINITY
if
* neither part is NaN
, but at least one part takes an infinite
* value.
* Uses the definitional formula *
* (a + bi) + (c + di) = (a+c) + (b+d)i **
* If either this or rhs
has a NaN value in either part,
* {@link #NaN} is returned; otherwise Inifinite and NaN values are
* returned in the parts of the result according to the rules for
* {@link java.lang.Double} arithmetic.
rhs
is null
*/
public Complex add(Complex rhs) {
return createComplex(real + rhs.getReal(),
imaginary + rhs.getImaginary());
}
/**
* Return the conjugate of this complex number. The conjugate of
* "A + Bi" is "A - Bi".
*
* {@link #NaN} is returned if either the real or imaginary
* part of this Complex number equals Double.NaN
.
* If the imaginary part is infinite, and the real part is not NaN,
* the returned value has infinite imaginary part of the opposite
* sign - e.g. the conjugate of 1 + POSITIVE_INFINITY i
* is 1 - NEGATIVE_INFINITY i
* Implements the definitional formula *
* a + bi ac + bd + (bc - ad)i
* ----------- = -------------------------
* c + di c2 + d2
*
* but uses
*
* prescaling of operands to limit the effects of overflows and
* underflows in the computation.
* * Infinite and NaN values are handled / returned according to the * following rules, applied in the order presented: *
rhs
has a NaN value in either part,
* {@link #NaN} is returned.rhs
equals {@link #ZERO}, {@link #NaN} is returned.
* rhs
are both infinite,
* {@link #NaN} is returned.rhs
is infinite (one or both parts infinite),
* {@link #ZERO} is returned.rhs
is finite, NaN values are
* returned in the parts of the result if the {@link java.lang.Double}
* rules applied to the definitional formula force NaN results.rhs
is null
*/
public Complex divide(Complex rhs) {
if (isNaN() || rhs.isNaN()) {
return NaN;
}
double c = rhs.getReal();
double d = rhs.getImaginary();
if (c == 0.0 && d == 0.0) {
return NaN;
}
if (rhs.isInfinite() && !isInfinite()) {
return ZERO;
}
if (FastMath.abs(c) < FastMath.abs(d)) {
double q = c / d;
double denominator = c * q + d;
return createComplex((real * q + imaginary) / denominator,
(imaginary * q - real) / denominator);
} else {
double q = d / c;
double denominator = d * q + c;
return createComplex((imaginary * q + real) / denominator,
(imaginary - real * q) / denominator);
}
}
/**
* Test for the equality of two Complex objects.
*
* If both the real and imaginary parts of two Complex numbers
* are exactly the same, and neither is Double.NaN
, the two
* Complex objects are considered to be equal.
* All NaN
values are considered to be equal - i.e, if either
* (or both) real and imaginary parts of the complex number are equal
* to Double.NaN
, the complex number is equal to
* Complex.NaN
.
* All NaN values have the same hash code.
* * @return a hash code value for this object */ @Override public int hashCode() { if (isNaN()) { return 7; } return 37 * (17 * MathUtils.hash(imaginary) + MathUtils.hash(real)); } /** * Access the imaginary part. * * @return the imaginary part */ public double getImaginary() { return imaginary; } /** * Access the real part. * * @return the real part */ public double getReal() { return real; } /** * Returns true if either or both parts of this complex number is NaN; * false otherwise * * @return true if either or both parts of this complex number is NaN; * false otherwise */ public boolean isNaN() { return isNaN; } /** * Returns true if either the real or imaginary part of this complex number * takes an infinite value (eitherDouble.POSITIVE_INFINITY
or
* Double.NEGATIVE_INFINITY
) and neither part
* is NaN
.
*
* @return true if one or both parts of this complex number are infinite
* and neither part is NaN
*/
public boolean isInfinite() {
return isInfinite;
}
/**
* Return the product of this complex number and the given complex number.
* * Implements preliminary checks for NaN and infinity followed by * the definitional formula: *
* (a + bi)(c + di) = (ac - bd) + (ad + bc)i
*
*
*
* Returns {@link #NaN} if either this or rhs
has one or more
* NaN parts.
*
rhs
has one or more
* NaN parts and if either this or rhs
has one or more
* infinite parts (same result is returned regardless of the sign of the
* components).
*
* * Returns finite values in components of the result per the * definitional formula in all remaining cases. *
* * @param rhs the other complex number * @return the complex number product * @throws NullPointerException ifrhs
is null
*/
public Complex multiply(Complex rhs) {
if (isNaN() || rhs.isNaN()) {
return NaN;
}
if (Double.isInfinite(real) || Double.isInfinite(imaginary) ||
Double.isInfinite(rhs.real)|| Double.isInfinite(rhs.imaginary)) {
// we don't use Complex.isInfinite() to avoid testing for NaN again
return INF;
}
return createComplex(real * rhs.real - imaginary * rhs.imaginary,
real * rhs.imaginary + imaginary * rhs.real);
}
/**
* Return the product of this complex number and the given scalar number.
* * Implements preliminary checks for NaN and infinity followed by * the definitional formula: *
* c(a + bi) = (ca) + (cb)i
*
*
*
* Returns {@link #NaN} if either this or rhs
has one or more
* NaN parts.
*
rhs
has one or more
* NaN parts and if either this or rhs
has one or more
* infinite parts (same result is returned regardless of the sign of the
* components).
*
* * Returns finite values in components of the result per the * definitional formula in all remaining cases. *
* * @param rhs the scalar number * @return the complex number product */ public Complex multiply(double rhs) { if (isNaN() || Double.isNaN(rhs)) { return NaN; } if (Double.isInfinite(real) || Double.isInfinite(imaginary) || Double.isInfinite(rhs)) { // we don't use Complex.isInfinite() to avoid testing for NaN again return INF; } return createComplex(real * rhs, imaginary * rhs); } /** * Return the additive inverse of this complex number. *
* Returns Complex.NaN
if either real or imaginary
* part of this Complex number equals Double.NaN
.
* Uses the definitional formula *
* (a + bi) - (c + di) = (a-c) + (b-d)i **
* If either this or rhs
has a NaN value in either part,
* {@link #NaN} is returned; otherwise inifinite and NaN values are
* returned in the parts of the result according to the rules for
* {@link java.lang.Double} arithmetic.
rhs
is null
*/
public Complex subtract(Complex rhs) {
if (isNaN() || rhs.isNaN()) {
return NaN;
}
return createComplex(real - rhs.getReal(),
imaginary - rhs.getImaginary());
}
/**
* Compute the
*
* inverse cosine of this complex number.
* * Implements the formula:
* acos(z) = -i (log(z + i (sqrt(1 - z2))))
*
* Returns {@link Complex#NaN} if either real or imaginary part of the
* input argument is NaN
or infinite.
* Implements the formula:
* asin(z) = -i (log(sqrt(1 - z2) + iz))
*
* Returns {@link Complex#NaN} if either real or imaginary part of the
* input argument is NaN
or infinite.
* Implements the formula:
* atan(z) = (i/2) log((i + z)/(i - z))
*
* Returns {@link Complex#NaN} if either real or imaginary part of the
* input argument is NaN
or infinite.
* Implements the formula:
* cos(a + bi) = cos(a)cosh(b) - sin(a)sinh(b)i
* where the (real) functions on the right-hand side are
* {@link java.lang.Math#sin}, {@link java.lang.Math#cos},
* {@link MathUtils#cosh} and {@link MathUtils#sinh}.
*
* Returns {@link Complex#NaN} if either real or imaginary part of the
* input argument is NaN
.
* Infinite values in real or imaginary parts of the input may result in * infinite or NaN values returned in parts of the result.
* Examples:
*
* cos(1 ± INFINITY i) = 1 ∓ INFINITY i
* cos(±INFINITY + i) = NaN + NaN i
* cos(±INFINITY ± INFINITY i) = NaN + NaN i
*
* @return the cosine of this complex number
* @since 1.2
*/
public Complex cos() {
if (isNaN()) {
return Complex.NaN;
}
return createComplex(FastMath.cos(real) * MathUtils.cosh(imaginary),
-FastMath.sin(real) * MathUtils.sinh(imaginary));
}
/**
* Compute the
*
* hyperbolic cosine of this complex number.
* * Implements the formula:
* cosh(a + bi) = cosh(a)cos(b) + sinh(a)sin(b)i
* where the (real) functions on the right-hand side are
* {@link java.lang.Math#sin}, {@link java.lang.Math#cos},
* {@link MathUtils#cosh} and {@link MathUtils#sinh}.
*
* Returns {@link Complex#NaN} if either real or imaginary part of the
* input argument is NaN
.
* Infinite values in real or imaginary parts of the input may result in * infinite or NaN values returned in parts of the result.
* Examples:
*
* cosh(1 ± INFINITY i) = NaN + NaN i
* cosh(±INFINITY + i) = INFINITY ± INFINITY i
* cosh(±INFINITY ± INFINITY i) = NaN + NaN i
*
* @return the hyperbolic cosine of this complex number.
* @since 1.2
*/
public Complex cosh() {
if (isNaN()) {
return Complex.NaN;
}
return createComplex(MathUtils.cosh(real) * FastMath.cos(imaginary),
MathUtils.sinh(real) * FastMath.sin(imaginary));
}
/**
* Compute the
*
* exponential function of this complex number.
* * Implements the formula:
* exp(a + bi) = exp(a)cos(b) + exp(a)sin(b)i
* where the (real) functions on the right-hand side are
* {@link java.lang.Math#exp}, {@link java.lang.Math#cos}, and
* {@link java.lang.Math#sin}.
*
* Returns {@link Complex#NaN} if either real or imaginary part of the
* input argument is NaN
.
* Infinite values in real or imaginary parts of the input may result in * infinite or NaN values returned in parts of the result.
* Examples:
*
* exp(1 ± INFINITY i) = NaN + NaN i
* exp(INFINITY + i) = INFINITY + INFINITY i
* exp(-INFINITY + i) = 0 + 0i
* exp(±INFINITY ± INFINITY i) = NaN + NaN i
*
* @return ethis
* @since 1.2
*/
public Complex exp() {
if (isNaN()) {
return Complex.NaN;
}
double expReal = FastMath.exp(real);
return createComplex(expReal * FastMath.cos(imaginary), expReal * FastMath.sin(imaginary));
}
/**
* Compute the
*
* natural logarithm of this complex number.
* * Implements the formula:
* log(a + bi) = ln(|a + bi|) + arg(a + bi)i
* where ln on the right hand side is {@link java.lang.Math#log},
* |a + bi|
is the modulus, {@link Complex#abs}, and
* arg(a + bi) = {@link java.lang.Math#atan2}(b, a)
*
* Returns {@link Complex#NaN} if either real or imaginary part of the
* input argument is NaN
.
* Infinite (or critical) values in real or imaginary parts of the input may * result in infinite or NaN values returned in parts of the result.
* Examples:
*
* log(1 ± INFINITY i) = INFINITY ± (π/2)i
* log(INFINITY + i) = INFINITY + 0i
* log(-INFINITY + i) = INFINITY + πi
* log(INFINITY ± INFINITY i) = INFINITY ± (π/4)i
* log(-INFINITY ± INFINITY i) = INFINITY ± (3π/4)i
* log(0 + 0i) = -INFINITY + 0i
*
*
* @return ln of this complex number.
* @since 1.2
*/
public Complex log() {
if (isNaN()) {
return Complex.NaN;
}
return createComplex(FastMath.log(abs()),
FastMath.atan2(imaginary, real));
}
/**
* Returns of value of this complex number raised to the power of x
.
* * Implements the formula:
* yx = exp(x·log(y))
* where exp
and log
are {@link #exp} and
* {@link #log}, respectively.
*
* Returns {@link Complex#NaN} if either real or imaginary part of the
* input argument is NaN
or infinite, or if y
* equals {@link Complex#ZERO}.
this
x
* @throws NullPointerException if x is null
* @since 1.2
*/
public Complex pow(Complex x) {
if (x == null) {
throw new NullPointerException();
}
return this.log().multiply(x).exp();
}
/**
* Compute the
*
* sine
* of this complex number.
* * Implements the formula:
* sin(a + bi) = sin(a)cosh(b) - cos(a)sinh(b)i
* where the (real) functions on the right-hand side are
* {@link java.lang.Math#sin}, {@link java.lang.Math#cos},
* {@link MathUtils#cosh} and {@link MathUtils#sinh}.
*
* Returns {@link Complex#NaN} if either real or imaginary part of the
* input argument is NaN
.
* Infinite values in real or imaginary parts of the input may result in * infinite or NaN values returned in parts of the result.
* Examples:
*
* sin(1 ± INFINITY i) = 1 ± INFINITY i
* sin(±INFINITY + i) = NaN + NaN i
* sin(±INFINITY ± INFINITY i) = NaN + NaN i
*
* @return the sine of this complex number.
* @since 1.2
*/
public Complex sin() {
if (isNaN()) {
return Complex.NaN;
}
return createComplex(FastMath.sin(real) * MathUtils.cosh(imaginary),
FastMath.cos(real) * MathUtils.sinh(imaginary));
}
/**
* Compute the
*
* hyperbolic sine of this complex number.
* * Implements the formula:
* sinh(a + bi) = sinh(a)cos(b)) + cosh(a)sin(b)i
* where the (real) functions on the right-hand side are
* {@link java.lang.Math#sin}, {@link java.lang.Math#cos},
* {@link MathUtils#cosh} and {@link MathUtils#sinh}.
*
* Returns {@link Complex#NaN} if either real or imaginary part of the
* input argument is NaN
.
* Infinite values in real or imaginary parts of the input may result in * infinite or NaN values returned in parts of the result.
* Examples:
*
* sinh(1 ± INFINITY i) = NaN + NaN i
* sinh(±INFINITY + i) = ± INFINITY + INFINITY i
* sinh(±INFINITY ± INFINITY i) = NaN + NaN i
*
* @return the hyperbolic sine of this complex number
* @since 1.2
*/
public Complex sinh() {
if (isNaN()) {
return Complex.NaN;
}
return createComplex(MathUtils.sinh(real) * FastMath.cos(imaginary),
MathUtils.cosh(real) * FastMath.sin(imaginary));
}
/**
* Compute the
*
* square root of this complex number.
*
* Implements the following algorithm to compute sqrt(a + bi)
:
*
t = sqrt((|a| + |a + bi|) / 2)
ifa ≥ 0
returnt + (b/2t)i
* else return|b|/2t + sign(b)t i
|a| = {@link Math#abs}(a)
|a + bi| = {@link Complex#abs}(a + bi)
sign(b) = {@link MathUtils#indicator}(b)
*
* Returns {@link Complex#NaN} if either real or imaginary part of the
* input argument is NaN
.
* Infinite values in real or imaginary parts of the input may result in * infinite or NaN values returned in parts of the result.
* Examples:
*
* sqrt(1 ± INFINITY i) = INFINITY + NaN i
* sqrt(INFINITY + i) = INFINITY + 0i
* sqrt(-INFINITY + i) = 0 + INFINITY i
* sqrt(INFINITY ± INFINITY i) = INFINITY + NaN i
* sqrt(-INFINITY ± INFINITY i) = NaN ± INFINITY i
*
*
* @return the square root of this complex number
* @since 1.2
*/
public Complex sqrt() {
if (isNaN()) {
return Complex.NaN;
}
if (real == 0.0 && imaginary == 0.0) {
return createComplex(0.0, 0.0);
}
double t = FastMath.sqrt((FastMath.abs(real) + abs()) / 2.0);
if (real >= 0.0) {
return createComplex(t, imaginary / (2.0 * t));
} else {
return createComplex(FastMath.abs(imaginary) / (2.0 * t),
MathUtils.indicator(imaginary) * t);
}
}
/**
* Compute the
*
* square root of 1 - this
2 for this complex
* number.
*
* Computes the result directly as
* sqrt(Complex.ONE.subtract(z.multiply(z)))
.
* Returns {@link Complex#NaN} if either real or imaginary part of the
* input argument is NaN
.
* Infinite values in real or imaginary parts of the input may result in * infinite or NaN values returned in parts of the result.
* * @return the square root of 1 -this
2
* @since 1.2
*/
public Complex sqrt1z() {
return createComplex(1.0, 0.0).subtract(this.multiply(this)).sqrt();
}
/**
* Compute the
*
* tangent of this complex number.
* * Implements the formula:
* tan(a + bi) = sin(2a)/(cos(2a)+cosh(2b)) + [sinh(2b)/(cos(2a)+cosh(2b))]i
* where the (real) functions on the right-hand side are
* {@link java.lang.Math#sin}, {@link java.lang.Math#cos},
* {@link MathUtils#cosh} and {@link MathUtils#sinh}.
*
* Returns {@link Complex#NaN} if either real or imaginary part of the
* input argument is NaN
.
* Infinite (or critical) values in real or imaginary parts of the input may * result in infinite or NaN values returned in parts of the result.
* Examples:
*
* tan(1 ± INFINITY i) = 0 + NaN i
* tan(±INFINITY + i) = NaN + NaN i
* tan(±INFINITY ± INFINITY i) = NaN + NaN i
* tan(±π/2 + 0 i) = ±INFINITY + NaN i
*
* @return the tangent of this complex number
* @since 1.2
*/
public Complex tan() {
if (isNaN()) {
return Complex.NaN;
}
double real2 = 2.0 * real;
double imaginary2 = 2.0 * imaginary;
double d = FastMath.cos(real2) + MathUtils.cosh(imaginary2);
return createComplex(FastMath.sin(real2) / d, MathUtils.sinh(imaginary2) / d);
}
/**
* Compute the
*
* hyperbolic tangent of this complex number.
* * Implements the formula:
* tan(a + bi) = sinh(2a)/(cosh(2a)+cos(2b)) + [sin(2b)/(cosh(2a)+cos(2b))]i
* where the (real) functions on the right-hand side are
* {@link java.lang.Math#sin}, {@link java.lang.Math#cos},
* {@link MathUtils#cosh} and {@link MathUtils#sinh}.
*
* Returns {@link Complex#NaN} if either real or imaginary part of the
* input argument is NaN
.
* Infinite values in real or imaginary parts of the input may result in * infinite or NaN values returned in parts of the result.
* Examples:
*
* tanh(1 ± INFINITY i) = NaN + NaN i
* tanh(±INFINITY + i) = NaN + 0 i
* tanh(±INFINITY ± INFINITY i) = NaN + NaN i
* tanh(0 + (π/2)i) = NaN + INFINITY i
*
* @return the hyperbolic tangent of this complex number
* @since 1.2
*/
public Complex tanh() {
if (isNaN()) {
return Complex.NaN;
}
double real2 = 2.0 * real;
double imaginary2 = 2.0 * imaginary;
double d = MathUtils.cosh(real2) + FastMath.cos(imaginary2);
return createComplex(MathUtils.sinh(real2) / d, FastMath.sin(imaginary2) / d);
}
/**
* Compute the argument of this complex number. *
*The argument is the angle phi between the positive real axis and the point * representing this number in the complex plane. The value returned is between -PI (not inclusive) * and PI (inclusive), with negative values returned for numbers with negative imaginary parts. *
*If either real or imaginary part (or both) is NaN, NaN is returned. Infinite parts are handled * as java.Math.atan2 handles them, essentially treating finite parts as zero in the presence of * an infinite coordinate and returning a multiple of pi/4 depending on the signs of the infinite * parts. See the javadoc for java.Math.atan2 for full details.
* * @return the argument of this complex number */ public double getArgument() { return FastMath.atan2(getImaginary(), getReal()); } /** *Computes the n-th roots of this complex number. *
*The nth roots are defined by the formula:
* zk = abs 1/n (cos(phi + 2πk/n) + i (sin(phi + 2πk/n))
* for k=0, 1, ..., n-1
, where abs
and phi
are
* respectively the {@link #abs() modulus} and {@link #getArgument() argument} of this complex number.
*
* If one or both parts of this complex number is NaN, a list with just one element, * {@link #NaN} is returned.
*if neither part is NaN, but at least one part is infinite, the result is a one-element * list containing {@link #INF}.
* * @param n degree of root * @return ListResolve the transient fields in a deserialized Complex Object.
*Subclasses will need to override {@link #createComplex} to deserialize properly
* @return A Complex instance with all fields resolved. * @since 2.0 */ protected final Object readResolve() { return createComplex(real, imaginary); } /** {@inheritDoc} */ public ComplexField getField() { return ComplexField.getInstance(); } }