📄 complex.java
字号:
* Compute the * <a href="http://mathworld.wolfram.com/NaturalLogarithm.html" TARGET="_top"> * natural logarithm</a> of this complex number. * <p> * Implements the formula: <pre> * <code> log(a + bi) = ln(|a + bi|) + arg(a + bi)i</code></pre> * where ln on the right hand side is {@link java.lang.Math#log}, * <code>|a + bi|</code> is the modulus, {@link Complex#abs}, and * <code>arg(a + bi) = {@link java.lang.Math#atan2}(b, a)</code></p> * <p> * Returns {@link Complex#NaN} if either real or imaginary part of the * input argument is <code>NaN</code>.</p> * <p> * 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.<pre> * Examples: * <code> * 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 * </code></pre></p> * * @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 <code>x</code>. * <p> * Implements the formula: <pre> * <code> y<sup>x</sup> = exp(x·log(y))</code></pre> * where <code>exp</code> and <code>log</code> are {@link #exp} and * {@link #log}, respectively.</p> * <p> * Returns {@link Complex#NaN} if either real or imaginary part of the * input argument is <code>NaN</code> or infinite, or if <code>y</code> * equals {@link Complex#ZERO}.</p> * * @param x the exponent. * @return <code>this</code><sup><code>x</code></sup> * @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 * <a href="http://mathworld.wolfram.com/Sine.html" TARGET="_top"> * sine</a> * of this complex number. * <p> * Implements the formula: <pre> * <code> sin(a + bi) = sin(a)cosh(b) - cos(a)sinh(b)i</code></pre> * 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}.</p> * <p> * Returns {@link Complex#NaN} if either real or imaginary part of the * input argument is <code>NaN</code>.</p> * <p> * Infinite values in real or imaginary parts of the input may result in * infinite or NaN values returned in parts of the result.<pre> * Examples: * <code> * sin(1 ± INFINITY i) = 1 ± INFINITY i * sin(±INFINITY + i) = NaN + NaN i * sin(±INFINITY ± INFINITY i) = NaN + NaN i</code></pre></p> * * @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 * <a href="http://mathworld.wolfram.com/HyperbolicSine.html" TARGET="_top"> * hyperbolic sine</a> of this complex number. * <p> * Implements the formula: <pre> * <code> sinh(a + bi) = sinh(a)cos(b)) + cosh(a)sin(b)i</code></pre> * 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}.</p> * <p> * Returns {@link Complex#NaN} if either real or imaginary part of the * input argument is <code>NaN</code>.</p> * <p> * Infinite values in real or imaginary parts of the input may result in * infinite or NaN values returned in parts of the result.<pre> * Examples: * <code> * sinh(1 ± INFINITY i) = NaN + NaN i * sinh(±INFINITY + i) = ± INFINITY + INFINITY i * sinh(±INFINITY ± INFINITY i) = NaN + NaN i</code></pre></p> * * @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 * <a href="http://mathworld.wolfram.com/SquareRoot.html" TARGET="_top"> * square root</a> of this complex number. * <p> * Implements the following algorithm to compute <code>sqrt(a + bi)</code>: * <ol><li>Let <code>t = sqrt((|a| + |a + bi|) / 2)</code></li> * <li><pre>if <code> a ≥ 0</code> return <code>t + (b/2t)i</code> * else return <code>|b|/2t + sign(b)t i </code></pre></li> * </ol> * where <ul> * <li><code>|a| = {@link Math#abs}(a)</code></li> * <li><code>|a + bi| = {@link Complex#abs}(a + bi) </code></li> * <li><code>sign(b) = {@link MathUtils#indicator}(b) </code> * </ul></p> * <p> * Returns {@link Complex#NaN} if either real or imaginary part of the * input argument is <code>NaN</code>.</p> * <p> * Infinite values in real or imaginary parts of the input may result in * infinite or NaN values returned in parts of the result.<pre> * Examples: * <code> * 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 * </code></pre></p> * * @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 * <a href="http://mathworld.wolfram.com/SquareRoot.html" TARGET="_top"> * square root</a> of 1 - <code>this</code><sup>2</sup> for this complex * number. * <p> * Computes the result directly as * <code>sqrt(Complex.ONE.subtract(z.multiply(z)))</code>.</p> * <p> * Returns {@link Complex#NaN} if either real or imaginary part of the * input argument is <code>NaN</code>.</p> * <p> * Infinite values in real or imaginary parts of the input may result in * infinite or NaN values returned in parts of the result.</p> * * @return the square root of 1 - <code>this</code><sup>2</sup> * @since 1.2 */ public Complex sqrt1z() { return createComplex(1.0, 0.0).subtract(this.multiply(this)).sqrt(); } /** * Compute the * <a href="http://mathworld.wolfram.com/Tangent.html" TARGET="_top"> * tangent</a> of this complex number. * <p> * Implements the formula: <pre> * <code>tan(a + bi) = sin(2a)/(cos(2a)+cosh(2b)) + [sinh(2b)/(cos(2a)+cosh(2b))]i</code></pre> * 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}.</p> * <p> * Returns {@link Complex#NaN} if either real or imaginary part of the * input argument is <code>NaN</code>.</p> * <p> * 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.<pre> * Examples: * <code> * 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</code></pre></p> * * @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 * <a href="http://mathworld.wolfram.com/HyperbolicTangent.html" TARGET="_top"> * hyperbolic tangent</a> of this complex number. * <p> * Implements the formula: <pre> * <code>tan(a + bi) = sinh(2a)/(cosh(2a)+cos(2b)) + [sin(2b)/(cosh(2a)+cos(2b))]i</code></pre> * 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}.</p> * <p> * Returns {@link Complex#NaN} if either real or imaginary part of the * input argument is <code>NaN</code>.</p> * <p> * Infinite values in real or imaginary parts of the input may result in * infinite or NaN values returned in parts of the result.<pre> * Examples: * <code> * 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</code></pre></p> * * @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); } /** * 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); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -