/** * Be cautious with this method, no new internal couble complex is created * * @param value * a double complex numeric value * @return */ protected static ComplexNum newInstance(final Complex value) { ComplexNum d = new ComplexNum(0.0, 0.0); d.fComplex = value; return d; }
/** * Compute the * <a href="http://mathworld.wolfram.com/InverseTangent.html" TARGET="_top"> * inverse tangent</a> for the given complex argument. * <p> * Implements the formula: <pre> * <code> atan(z) = (i/2) log((i + z)/(i - z)) </code></pre> * <p> * Returns {@link ComplexNum#NaN} if either real or imaginary part of the * input argument is <code>NaN</code> or infinite. * * @param z the value whose inverse tangent is to be returned * @return the inverse tangent of <code>z</code> * @throws NullPointerException if <code>z</code> is null */ public static ComplexNum atan(final ComplexNum z) { if (z.isNaN()) { return ComplexNum.NaN; } return ComplexNum.I.multiply( log(ComplexNum.I.add(z).divide(ComplexNum.I.subtract(z)))) .divide(ComplexNum.valueOf(2.0, 0.0)); }
/** * Compares this expression with the specified expression for order. Returns a * negative integer, zero, or a positive integer as this expression is * canonical less than, equal to, or greater than the specified expression. */ public int compareTo(final IExpr obj) { if (obj instanceof ComplexNum) { return compareTo(((ComplexNum) obj).fComplex); // return fComplex.compareTo(((DoubleComplexImpl) obj).fComplex); } return (hierarchy() - (obj).hierarchy()); }
/** {@inheritDoc} */ @Override public INumber fractionalPart() { return F.complexNum(getRealPart() % 1, getImaginaryPart() % 1); }
if (z.isNaN()) { return ComplexNum.NaN; return ComplexNum.valueOf(Math.log(z.dabs()), Math.atan2(z.getImaginary(), z.getReal()));
if (z.isNaN()) { return ComplexNum.NaN; return ComplexNum.valueOf(Math.log(z.dabs()), Math.atan2(z.imDoubleValue(), z.reDoubleValue()));
/** * Compute the * <a href="http://mathworld.wolfram.com/InverseSine.html" TARGET="_top"> * inverse sine</a> for the given complex argument. * <p> * Implements the formula: <pre> * <code> asin(z) = -i (log(sqrt(1 - z<sup>2</sup>) + iz)) </code></pre> * <p> * Returns {@link ComplexNum#NaN} if either real or imaginary part of the * input argument is <code>NaN</code> or infinite. * * @param z the value whose inverse sine is to be returned. * @return the inverse sine of <code>z</code>. * @throws NullPointerException if <code>z</code> is null */ public static ComplexNum asin(final ComplexNum z) { if (z.isNaN()) { return ComplexNum.NaN; } return ComplexNum.I.negate().multiply(log(sqrt1z(z).add( ComplexNum.I.multiply(z)))); }
/** * @param that * @return */ @Override public IExpr plus(final IExpr that) { if (that instanceof ComplexNum) { return newInstance(fComplex.add(((ComplexNum) that).fComplex)); } if (that instanceof ApcomplexNum) { ApcomplexNum acn = (ApcomplexNum) that; return ApcomplexNum.valueOf(getRealPart(), getImaginaryPart(), acn.fApcomplex.precision()).add(acn); } if (that instanceof ApfloatNum) { ApfloatNum afn = (ApfloatNum) that; return ApcomplexNum.valueOf(getRealPart(), getImaginaryPart(), afn.fApfloat.precision()) .add(ApcomplexNum.valueOf(afn.fApfloat, Apcomplex.ZERO)); } if (that instanceof Num) { return add(ComplexNum.valueOf(((Num) that).getRealPart())); } return IComplexNum.super.plus(that); }
/** * @param that * @return */ @Override public IExpr times(final IExpr that) { if (that instanceof ComplexNum) { return newInstance(fComplex.multiply(((ComplexNum) that).fComplex)); } if (that instanceof ApcomplexNum) { ApcomplexNum acn = (ApcomplexNum) that; return ApcomplexNum.valueOf(getRealPart(), getImaginaryPart(), acn.fApcomplex.precision()).multiply(acn); } if (that instanceof ApfloatNum) { ApfloatNum afn = (ApfloatNum) that; return ApcomplexNum.valueOf(getRealPart(), getImaginaryPart(), afn.fApfloat.precision()) .multiply(ApcomplexNum.valueOf(afn.fApfloat, Apcomplex.ZERO)); } if (that instanceof Num) { return multiply(ComplexNum.valueOf(((Num) that).getRealPart())); } return IComplexNum.super.times(that); }
/** * Return the absolute value of this complex number. * <p> * Returns <code>NaN</code> if either real or imaginary part is * <code>NaN</code> and <code>Double.POSITIVE_INFINITY</code> if neither part * is <code>NaN</code>, but at least one part takes an infinite value. * * @return the absolute value */ public double dabs() { if (isNaN()) { return Double.NaN; } if (isInfinite()) { return Double.POSITIVE_INFINITY; } if (Math.abs(getReal()) < Math.abs(getImaginary())) { if (getImaginary() == 0.0) { return Math.abs(getReal()); } final double q = getReal() / getImaginary(); return (Math.abs(getImaginary()) * Math.sqrt(1 + q * q)); } else { if (getReal() == 0.0) { return Math.abs(getImaginary()); } final double q = getImaginary() / getReal(); return (Math.abs(getReal()) * Math.sqrt(1 + q * q)); } }
/** * Compute the * <a href="http://mathworld.wolfram.com/SquareRoot.html" TARGET="_top"> * square root</a> of 1 - <code>z</code><sup>2</sup> for the given complex * argument. * <p> * Computes the result directly as * <code>sqrt(DoubleComplexImpl.ONE.subtract(z.multiply(z)))</code>. * <p> * Returns {@link ComplexNum#NaN} if either real or imaginary part of the * input argument is <code>NaN</code>. * <p> * Infinite values in real or imaginary parts of the input may result in * infinite or NaN values returned in parts of the result. * * @param z the value * @return the square root of 1 - <code>z</code><sup>2</sup> * @throws NullPointerException if <code>z</code> is null */ public static ComplexNum sqrt1z(final ComplexNum z) { return sqrt(ComplexNum.ONE.subtract(z.multiply(z))); }
/** {@inheritDoc} */ @Override public double dabs() { return dabs(fComplex); }
@Override public IExpr plus(final IExpr that) { if (that instanceof ApfloatNum) { return add(ApfloatNum.valueOf(fDouble, ((ApfloatNum) that).fApfloat.precision())); } if (that instanceof Num) { return valueOf(fDouble + ((Num) that).fDouble); } if (that instanceof ApcomplexNum) { return ApcomplexNum.valueOf(fDouble, ((ApcomplexNum) that).fApcomplex.precision()).add((ApcomplexNum) that); } if (that instanceof ComplexNum) { return ComplexNum.valueOf(fDouble).add((ComplexNum) that); } return INum.super.plus(that); }
@Override public IExpr times(final IExpr that) { if (that instanceof ApfloatNum) { return multiply(ApfloatNum.valueOf(fDouble, ((ApfloatNum) that).fApfloat.precision())); } if (that instanceof Num) { return valueOf(fDouble * ((Num) that).fDouble); } if (that instanceof ApcomplexNum) { return ApcomplexNum.valueOf(fDouble, ((ApcomplexNum) that).fApcomplex.precision()) .multiply((ApcomplexNum) that); } if (that instanceof ComplexNum) { return ComplexNum.valueOf(fDouble).multiply((ComplexNum) that); } return INum.super.times(that); }
/** {@inheritDoc} */ @Override public IExpr dec() { return add(MINUS_ONE); }
/** * Returns of value of <code>y</code> 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> * Returns {@link ComplexNum#NaN} if either real or imaginary part of the * input argument is <code>NaN</code> or infinite, or if <code>y</code> * equals {@link ComplexNum#ZERO}. * * @param y the base. * @param x the exponent. * @return <code>y</code><sup><code>x</code></sup> * @throws NullPointerException if either x or y is null */ public static ComplexNum pow(final ComplexNum y, final ComplexNum x) { return exp(x.multiply(log(y))); }
/** * Compares this expression with the specified expression for order. Returns a negative integer, zero, or a positive * integer as this expression is canonical less than, equal to, or greater than the specified expression. */ @Override public int compareTo(final IExpr expr) { if (expr instanceof ComplexNum) { return compareTo(((ComplexNum) expr).fComplex); } return IComplexNum.super.compareTo(expr); }
/** {@inheritDoc} */ @Override public ISignedNumber im() { return F.num(getImaginaryPart()); }
/** {@inheritDoc} */ @Override public ISignedNumber re() { return F.num(getRealPart()); }
if (z.isNaN()) { return ComplexNum.NaN; final double b = z.getImaginary(); final double expA = Math.exp(z.getReal()); return ComplexNum.valueOf(expA * Math.cos(b), expA * Math.sin(b));