/** * 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); }
/** * Returns a {@code Complex} whose value is {@code (-this)}. * Returns {@code NaN} if either real or imaginary * part of this Complex number is {@code Double.NaN}. * * @return {@code -this}. */ public Complex negate() { if (isNaN) { return NaN; } return createComplex(-real, -imaginary); }
/** * Returns the conjugate of this complex number. * The conjugate of {@code a + bi} is {@code a - bi}. * <p> * {@link #NaN} is returned if either the real or imaginary * part of this Complex number equals {@code Double.NaN}. * </p><p> * If the imaginary part is infinite, and the real part is not * {@code NaN}, the returned value has infinite imaginary part * of the opposite sign, e.g. the conjugate of * {@code 1 + POSITIVE_INFINITY i} is {@code 1 - NEGATIVE_INFINITY i}. * </p> * @return the conjugate of this Complex object. */ public Complex conjugate() { if (isNaN) { return NaN; } return createComplex(real, -imaginary); }
/** * Returns a {@code Complex} whose value is {@code (this + addend)}, * with {@code addend} interpreted as a real number. * * @param addend Value to be added to this {@code Complex}. * @return {@code this + addend}. * @see #add(Complex) */ public Complex add(double addend) { if (isNaN || Double.isNaN(addend)) { return NaN; } return createComplex(real + addend, imaginary); }
/** * Returns a {@code Complex} whose value is * {@code (this - subtrahend)}. * * @param subtrahend value to be subtracted from this {@code Complex}. * @return {@code this - subtrahend}. * @see #subtract(Complex) */ public Complex subtract(double subtrahend) { if (isNaN || Double.isNaN(subtrahend)) { return NaN; } return createComplex(real - subtrahend, imaginary); }
/** * Returns a {@code Complex} whose value is {@code this * factor}, with {@code factor} * interpreted as a integer number. * * @param factor value to be multiplied by this {@code Complex}. * @return {@code this * factor}. * @see #multiply(Complex) */ public Complex multiply(final int factor) { if (isNaN) { return NaN; } if (Double.isInfinite(real) || Double.isInfinite(imaginary)) { return INF; } return createComplex(real * factor, imaginary * factor); }
/** * Returns a {@code Complex} whose value is {@code this * factor}, with {@code factor} * interpreted as a real number. * * @param factor value to be multiplied by this {@code Complex}. * @return {@code this * factor}. * @see #multiply(Complex) */ public Complex multiply(double factor) { if (isNaN || Double.isNaN(factor)) { return NaN; } if (Double.isInfinite(real) || Double.isInfinite(imaginary) || Double.isInfinite(factor)) { // we don't use isInfinite() to avoid testing for NaN again return INF; } return createComplex(real * factor, imaginary * factor); }
/** * Returns a {@code Complex} whose value is {@code (this / divisor)}, * with {@code divisor} interpreted as a real number. * * @param divisor Value by which this {@code Complex} is to be divided. * @return {@code this / divisor}. * @see #divide(Complex) */ public Complex divide(double divisor) { if (isNaN || Double.isNaN(divisor)) { return NaN; } if (divisor == 0d) { return NaN; } if (Double.isInfinite(divisor)) { return !isInfinite() ? ZERO : NaN; } return createComplex(real / divisor, imaginary / divisor); }
/** {@inheritDoc} */ public Complex reciprocal() { if (isNaN) { return NaN; } if (real == 0.0 && imaginary == 0.0) { return INF; } if (isInfinite) { return ZERO; } if (FastMath.abs(real) < FastMath.abs(imaginary)) { double q = real / imaginary; double scale = 1. / (real * q + imaginary); return createComplex(scale * q, -scale); } else { double q = imaginary / real; double scale = 1. / (imaginary * q + real); return createComplex(scale, -scale * q); } }
return createComplex(real * factor.real - imaginary * factor.imaginary, real * factor.imaginary + imaginary * factor.real);
/** * Compute the * <a href="http://mathworld.wolfram.com/SquareRoot.html" TARGET="_top"> * square root</a> of <code>1 - this<sup>2</sup></code> for this complex * number. * Computes the result directly as * {@code sqrt(ONE.subtract(z.multiply(z)))}. * <p> * Returns {@link Complex#NaN} if either real or imaginary part of the * input argument is {@code NaN}. * </p> * 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 <code>1 - this<sup>2</sup></code>. * @since 1.2 */ public Complex sqrt1z() { return createComplex(1.0, 0.0).subtract(this.multiply(this)).sqrt(); }
/** * Returns a {@code Complex} whose value is * {@code (this + addend)}. * Uses the definitional formula * <p> * {@code (a + bi) + (c + di) = (a+c) + (b+d)i} * </p> * If either {@code this} or {@code addend} has a {@code NaN} value in * either part, {@link #NaN} is returned; otherwise {@code Infinite} * and {@code NaN} values are returned in the parts of the result * according to the rules for {@link java.lang.Double} arithmetic. * * @param addend Value to be added to this {@code Complex}. * @return {@code this + addend}. * @throws NullArgumentException if {@code addend} is {@code null}. */ public Complex add(Complex addend) throws NullArgumentException { MathUtils.checkNotNull(addend); if (isNaN || addend.isNaN) { return NaN; } return createComplex(real + addend.getReal(), imaginary + addend.getImaginary()); }
/** * Returns a {@code Complex} whose value is * {@code (this - subtrahend)}. * Uses the definitional formula * <p> * {@code (a + bi) - (c + di) = (a-c) + (b-d)i} * </p> * If either {@code this} or {@code subtrahend} has a {@code NaN]} value in either part, * {@link #NaN} is returned; otherwise infinite and {@code NaN} values are * returned in the parts of the result according to the rules for * {@link java.lang.Double} arithmetic. * * @param subtrahend value to be subtracted from this {@code Complex}. * @return {@code this - subtrahend}. * @throws NullArgumentException if {@code subtrahend} is {@code null}. */ public Complex subtract(Complex subtrahend) throws NullArgumentException { MathUtils.checkNotNull(subtrahend); if (isNaN || subtrahend.isNaN) { return NaN; } return createComplex(real - subtrahend.getReal(), imaginary - subtrahend.getImaginary()); }
return createComplex(expReal * FastMath.cos(imaginary), expReal * FastMath.sin(imaginary));
return createComplex(FastMath.log(abs()), FastMath.atan2(imaginary, real));
return createComplex(FastMath.sinh(real) * FastMath.cos(imaginary), FastMath.cosh(real) * FastMath.sin(imaginary));
return createComplex(FastMath.cosh(real) * FastMath.cos(imaginary), FastMath.sinh(real) * FastMath.sin(imaginary));
return createComplex(FastMath.cos(real) * FastMath.cosh(imaginary), -FastMath.sin(real) * FastMath.sinh(imaginary));
return createComplex(FastMath.sin(real) * FastMath.cosh(imaginary), FastMath.cos(real) * FastMath.sinh(imaginary));
/** * Compute the * <a href="http://mathworld.wolfram.com/InverseTangent.html" TARGET="_top"> * inverse tangent</a> of this complex number. * Implements the formula: * <p> * {@code atan(z) = (i/2) log((i + z)/(i - z))} * </p><p> * Returns {@link Complex#NaN} if either real or imaginary part of the * input argument is {@code NaN} or infinite.</p> * * @return the inverse tangent of this complex number * @since 1.2 */ public Complex atan() { if (isNaN) { return NaN; } return this.add(I).divide(I.subtract(this)).log() .multiply(I.divide(createComplex(2.0, 0.0))); }