org.apache.commons.math.complex.Complex Maven / Gradle / Ivy
Show all versions of commons-math Show documentation
/*
* 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.util.MathUtils;
/**
* 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: 791237 $ $Date: 2009-07-05 08:53:13 -0400 (Sun, 05 Jul 2009) $
*/
public class Complex implements FieldElement, Serializable {
/** Serializable version identifier */
private static final long serialVersionUID = -6195664516687396620L;
/** The square root of -1. A number representing "0.0 + 1.0i" */
public static final Complex I = new Complex(0.0, 1.0);
/** A complex number representing "NaN + NaNi" */
public static final Complex NaN = new Complex(Double.NaN, Double.NaN);
/** A complex number representing "+INF + INFi" */
public static final Complex INF = new Complex(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY);
/** A complex number representing "1.0 + 0.0i" */
public static final Complex ONE = new Complex(1.0, 0.0);
/** A complex number representing "0.0 + 0.0i" */
public static final Complex ZERO = new Complex(0.0, 0.0);
/**
* The imaginary part
*/
private final double imaginary;
/**
* The real part
*/
private final double real;
/**
* Record whether this complex number is equal to NaN
*/
private final transient boolean isNaN;
/**
* Record whether this complex number is infinite
*/
private final transient boolean isInfinite;
/**
* Create a complex number given the real and imaginary parts.
*
* @param real the real part
* @param imaginary the imaginary part
*/
public Complex(double real, double imaginary) {
super();
this.real = real;
this.imaginary = imaginary;
isNaN = Double.isNaN(real) || Double.isNaN(imaginary);
isInfinite = !isNaN &&
(Double.isInfinite(real) || Double.isInfinite(imaginary));
}
/**
* Return the absolute value of this complex number.
*
* 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.
*
* @return the absolute value
*/
public double abs() {
if (isNaN()) {
return Double.NaN;
}
if (isInfinite()) {
return Double.POSITIVE_INFINITY;
}
if (Math.abs(real) < Math.abs(imaginary)) {
if (imaginary == 0.0) {
return Math.abs(real);
}
double q = real / imaginary;
return (Math.abs(imaginary) * Math.sqrt(1 + q*q));
} else {
if (real == 0.0) {
return Math.abs(imaginary);
}
double q = imaginary / real;
return (Math.abs(real) * Math.sqrt(1 + q*q));
}
}
/**
* Return the sum of this complex number and the given complex number.
*
* 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.
*
* @param rhs the other complex number
* @return the complex number sum
* @throws NullPointerException if 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
*
* @return the conjugate of this Complex object
*/
public Complex conjugate() {
if (isNaN()) {
return NaN;
}
return createComplex(real, -imaginary);
}
/**
* Return the quotient of this complex number and the given complex number.
*
* 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:
*
* - If either this or
rhs
has a NaN value in either part,
* {@link #NaN} is returned.
* - If
rhs
equals {@link #ZERO}, {@link #NaN} is returned.
*
* - If this and
rhs
are both infinite,
* {@link #NaN} is returned.
* - If this is finite (i.e., has no infinite or NaN parts) and
*
rhs
is infinite (one or both parts infinite),
* {@link #ZERO} is returned.
* - If this is infinite and
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.
*
*
* @param rhs the other complex number
* @return the complex number quotient
* @throws NullPointerException if 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 (Math.abs(c) < Math.abs(d)) {
if (d == 0.0) {
return createComplex(real/c, imaginary/c);
}
double q = c / d;
double denominator = c * q + d;
return createComplex((real * q + imaginary) / denominator,
(imaginary * q - real) / denominator);
} else {
if (c == 0.0) {
return createComplex(imaginary/d, -real/c);
}
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
.
*
* @param other Object to test for equality to this
* @return true if two Complex objects are equal, false if
* object is null, not an instance of Complex, or
* not equal to this Complex instance
*
*/
@Override
public boolean equals(Object other) {
boolean ret;
if (this == other) {
ret = true;
} else if (other == null) {
ret = false;
} else {
try {
Complex rhs = (Complex)other;
if (rhs.isNaN()) {
ret = this.isNaN();
} else {
ret = (real == rhs.real) && (imaginary == rhs.imaginary);
}
} catch (ClassCastException ex) {
// ignore exception
ret = false;
}
}
return ret;
}
/**
* Get a hashCode for the complex number.
*
* 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 (either Double.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.
*
* Returns {@link #INF} if neither this nor 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 if rhs
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.
*
* Returns {@link #INF} if neither this nor 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
.
*
* @return the negation of this complex number
*/
public Complex negate() {
if (isNaN()) {
return NaN;
}
return createComplex(-real, -imaginary);
}
/**
* Return the difference between this complex number and the given complex
* number.
*
* 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.
*
* @param rhs the other complex number
* @return the complex number difference
* @throws NullPointerException if 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.
*
* @return the inverse cosine of this complex number
* @since 1.2
*/
public Complex acos() {
if (isNaN()) {
return Complex.NaN;
}
return this.add(this.sqrt1z().multiply(Complex.I)).log()
.multiply(Complex.I.negate());
}
/**
* Compute the
*
* inverse sine of this complex number.
*
* 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.
*
* @return the inverse sine of this complex number.
* @since 1.2
*/
public Complex asin() {
if (isNaN()) {
return Complex.NaN;
}
return sqrt1z().add(this.multiply(Complex.I)).log()
.multiply(Complex.I.negate());
}
/**
* Compute the
*
* inverse tangent of this complex number.
*
* 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.
*
* @return the inverse tangent of this complex number
* @since 1.2
*/
public Complex atan() {
if (isNaN()) {
return Complex.NaN;
}
return this.add(Complex.I).divide(Complex.I.subtract(this)).log()
.multiply(Complex.I.divide(createComplex(2.0, 0.0)));
}
/**
* Compute the
*
* cosine
* of this complex number.
*
* 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(Math.cos(real) * MathUtils.cosh(imaginary),
-Math.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) * Math.cos(imaginary),
MathUtils.sinh(real) * Math.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 = Math.exp(real);
return createComplex(expReal * Math.cos(imaginary), expReal * Math.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(Math.log(abs()),
Math.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}.
*
* @param x the exponent.
* @return 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(Math.sin(real) * MathUtils.cosh(imaginary),
Math.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) * Math.cos(imaginary),
MathUtils.cosh(real) * Math.sin(imaginary));
}
/**
* Compute the
*
* square root of this complex number.
*
* Implements the following algorithm to compute sqrt(a + bi)
:
*
- Let
t = sqrt((|a| + |a + bi|) / 2)
* if a ≥ 0
return t + (b/2t)i
* else return |b|/2t + sign(b)t i
*
* where
* |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 = Math.sqrt((Math.abs(real) + abs()) / 2.0);
if (real >= 0.0) {
return createComplex(t, imaginary / (2.0 * t));
} else {
return createComplex(Math.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 = Math.cos(real2) + MathUtils.cosh(imaginary2);
return createComplex(Math.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) + Math.cos(imaginary2);
return createComplex(MathUtils.sinh(real2) / d, Math.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 Math.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 List all nth roots of this complex number
* @throws IllegalArgumentException if parameter n is less than or equal to 0
* @since 2.0
*/
public List nthRoot(int n) throws IllegalArgumentException {
if (n <= 0) {
throw MathRuntimeException.createIllegalArgumentException(
"cannot compute nth root for null or negative n: {0}",
n);
}
List result = new ArrayList();
if (isNaN()) {
result.add(Complex.NaN);
return result;
}
if (isInfinite()) {
result.add(Complex.INF);
return result;
}
// nth root of abs -- faster / more accurate to use a solver here?
final double nthRootOfAbs = Math.pow(abs(), 1.0 / n);
// Compute nth roots of complex number with k = 0, 1, ... n-1
final double nthPhi = getArgument()/n;
final double slice = 2 * Math.PI / n;
double innerPart = nthPhi;
for (int k = 0; k < n ; k++) {
// inner part
final double realPart = nthRootOfAbs * Math.cos(innerPart);
final double imaginaryPart = nthRootOfAbs * Math.sin(innerPart);
result.add(createComplex(realPart, imaginaryPart));
innerPart += slice;
}
return result;
}
/**
* Create a complex number given the real and imaginary parts.
*
* @param real the real part
* @param imaginary the imaginary part
* @return a new complex number instance
* @since 1.2
*/
protected Complex createComplex(double real, double imaginary) {
return new Complex(real, imaginary);
}
/**
* Resolve 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();
}
}