/** * Builds a new two dimensional array of {@code double} filled with the real * and imaginary parts of the specified {@link Complex} numbers. In the * returned array {@code dataRI}, the data is laid out as follows * <ul> * <li>{@code dataRI[0][i] = dataC[i].getReal()},</li> * <li>{@code dataRI[1][i] = dataC[i].getImaginary()}.</li> * </ul> * * @param dataC the array of {@link Complex} data to be transformed * @return a two dimensional array filled with the real and imaginary parts * of the specified complex input */ public static double[][] createRealImaginaryArray(final Complex[] dataC) { final double[][] dataRI = new double[2][dataC.length]; final double[] dataR = dataRI[0]; final double[] dataI = dataRI[1]; for (int i = 0; i < dataC.length; i++) { final Complex c = dataC[i]; dataR[i] = c.getReal(); dataI[i] = c.getImaginary(); } return dataRI; }
/** * Multiply every component in the given complex array by the * given real number. The change is made in place. * * @param f the complex array to be scaled * @param d the real scaling coefficient * @return a reference to the scaled array */ public static Complex[] scaleArray(Complex[] f, double d) { for (int i = 0; i < f.length; i++) { f[i] = new Complex(d * f[i].getReal(), d * f[i].getImaginary()); } return f; }
/** * 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. * <p> * If either real or imaginary part (or both) is NaN, NaN is returned. * Infinite parts are handled as {@code 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 {@code Math.atan2} for full details. * * @return the argument of {@code this}. */ public double getArgument() { return FastMath.atan2(getImaginary(), getReal()); }
/** * Return the tanh of a complex number * * @param num the tanh of a complex number * @return the tanh of a complex number */ public static IComplexNumber hardTanh(IComplexNumber num) { Complex c = new Complex(num.realComponent().doubleValue(), num.imaginaryComponent().doubleValue()).tanh(); if (c.getReal() < -1.0) c = new Complex(-1.0, c.getImaginary()); return Nd4j.createDouble(c.getReal(), c.getImaginary()); }
/** * Return the log value of the given complex number * * @param num the number to getScalar the absolute value for * @return the absolute value of this complex number */ public static IComplexNumber neg(IComplexNumber num) { Complex c = new Complex(num.realComponent().doubleValue(), num.imaginaryComponent().doubleValue()).negate(); return Nd4j.createDouble(c.getReal(), c.getImaginary()); }
/** * Return the sin value of the given complex number * * @param num the number to getScalar the absolute value for * @return the absolute value of this complex number */ public static IComplexNumber asin(IComplexNumber num) { Complex c = new Complex(num.realComponent().doubleValue(), num.imaginaryComponent().doubleValue()).asin(); return Nd4j.createDouble(c.getReal(), c.getImaginary()); }
/** * Return the cos of a complex number * * @param num the tanh of a complex number * @return the tanh of a complex number */ public static IComplexNumber cos(IComplexNumber num) { Complex c = new Complex(num.realComponent().doubleValue(), num.imaginaryComponent().doubleValue()).cos(); return Nd4j.createDouble(c.getReal(), c.getImaginary()); }
/** * Return the tanh of a complex number * * @param num the tanh of a complex number * @return the tanh of a complex number */ public static IComplexNumber tanh(IComplexNumber num) { Complex c = new Complex(num.realComponent().doubleValue(), num.imaginaryComponent().doubleValue()).tanh(); return Nd4j.createDouble(c.getReal(), c.getImaginary()); }
/** * Return the sin value of the given complex number * * @param num the number to getScalar the absolute value for * @return the absolute value of this complex number */ public static IComplexNumber atan(IComplexNumber num) { Complex c = new Complex(num.realComponent().doubleValue(), num.imaginaryComponent().doubleValue()).atan(); return Nd4j.createDouble(c.getReal(), c.getImaginary()); }
/** * Return the sin value of the given complex number * * @param num the number to getScalar the absolute value for * @return the absolute value of this complex number */ public static IComplexNumber acos(IComplexNumber num) { Complex c = new Complex(num.realComponent().doubleValue(), num.imaginaryComponent().doubleValue()).acos(); return Nd4j.createDouble(c.getReal(), c.getImaginary()); }
/** * Return the absolute value of the given complex number * * @param num the number to getScalar the absolute value for * @return the absolute value of this complex number */ public static IComplexNumber sqrt(IComplexNumber num) { Complex c = new Complex(num.realComponent().doubleValue(), num.imaginaryComponent().doubleValue()).sqrt(); return Nd4j.createDouble(c.getReal(), c.getImaginary()); }
/** * Return the sin value of the given complex number * * @param num the number to getScalar the absolute value for * @return the absolute value of this complex number */ public static IComplexNumber sin(IComplexNumber num) { Complex c = new Complex(num.realComponent().doubleValue(), num.imaginaryComponent().doubleValue()).sin(); return Nd4j.createDouble(c.getReal(), c.getImaginary()); }
/** * Return the log value of the given complex number * * @param num the number to getScalar the absolute value for * @return the absolute value of this complex number */ public static IComplexNumber log(IComplexNumber num) { Complex c = new Complex(num.realComponent().doubleValue(), num.imaginaryComponent().doubleValue()).log(); return Nd4j.createDouble(c.getReal(), c.getImaginary()); }
/** * Return the ceiling value of the given complex number * * @param num the number to getScalar the absolute value for * @return the absolute value of this complex number */ public static IComplexNumber ceil(IComplexNumber num) { Complex c = new Complex(FastMath.ceil(num.realComponent().doubleValue()), FastMath.ceil(num.imaginaryComponent().doubleValue())); return Nd4j.createDouble(c.getReal(), c.getImaginary()); }
/** * Return the floor value of the given complex number * * @param num the number to getScalar the absolute value for * @return the absolute value of this complex number */ public static IComplexNumber floor(IComplexNumber num) { Complex c = new Complex(FastMath.floor(num.realComponent().doubleValue()), FastMath.floor(num.imaginaryComponent().doubleValue())); return Nd4j.createDouble(c.getReal(), c.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()); }
/** * 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()); }
/** * Raise a complex number to a power * * @param num the number to raise * @param power the power to raise to * @return the number raised to a power */ public static IComplexNumber pow(IComplexNumber num, IComplexNumber power) { Complex c = new Complex(num.realComponent().doubleValue(), num.imaginaryComponent().doubleValue()).pow( new Complex(power.realComponent().doubleValue(), power.imaginaryComponent().doubleValue())); if (c.isNaN()) c = new Complex(Nd4j.EPS_THRESHOLD, 0.0); return Nd4j.createDouble(c.getReal(), c.getImaginary()); }
/** * Raise a complex number to a power * * @param num the number to raise * @param power the power to raise to * @return the number raised to a power */ public static IComplexNumber pow(IComplexNumber num, double power) { Complex c = new Complex(num.realComponent().doubleValue(), num.imaginaryComponent().doubleValue()).pow(power); if (c.isNaN()) c = new Complex(Nd4j.EPS_THRESHOLD, 0.0); return Nd4j.createDouble(c.getReal(), c.getImaginary()); }
/** * Check whether the given complex root is actually a real zero * in the given interval, within the solver tolerance level. * * @param min Lower bound for the interval. * @param max Upper bound for the interval. * @param z Complex root. * @return {@code true} if z is a real zero. */ public boolean isRoot(double min, double max, Complex z) { if (isSequence(min, z.getReal(), max)) { double tolerance = FastMath.max(getRelativeAccuracy() * z.abs(), getAbsoluteAccuracy()); return (FastMath.abs(z.getImaginary()) <= tolerance) || (z.abs() <= getFunctionValueAccuracy()); } return false; }