Search in sources :

Example 31 with Complex

use of org.hipparchus.complex.Complex in project symja_android_library by axkr.

the class EllipticFunctionsJS method weierstrassP.

public static Complex weierstrassP(Complex x, Complex g2, Complex g3) {
    // if ( !isComplex(x) ) x = complex(x);
    Complex[] sol = weierstrassRoots(g2, g3);
    Complex e1 = sol[0];
    Complex e2 = sol[1];
    Complex e3 = sol[2];
    // Whittaker & Watson, Section 22.351
    Complex m = e2.subtract(e3).divide(e1.subtract(e3));
    Complex pow = jacobiSN(x.multiply(e1.subtract(e3).sqrt()), m).reciprocal();
    pow = pow.multiply(pow);
    return e3.add(e1.subtract(e3).multiply(pow));
}
Also used : Complex(org.hipparchus.complex.Complex)

Example 32 with Complex

use of org.hipparchus.complex.Complex in project symja_android_library by axkr.

the class JavaComplexFormFactory method convertAST.

@Override
public void convertAST(final StringBuilder buf, final IAST function) {
    if (function.isNumericFunction(true)) {
        try {
            Complex value = EvalEngine.get().evalComplex(function);
            if (value != null) {
                buf.append("Complex.valueOf(");
                buf.append(value.getReal());
                buf.append(",");
                buf.append(value.getImaginary());
                buf.append(")");
                return;
            }
        } catch (RuntimeException rex) {
        // 
        }
    }
    IExpr head = function.head();
    if (head.isSymbol() && function.size() > 1) {
        String str = functionHead((ISymbol) head);
        if (str != null) {
            if (function.isPower()) {
                IExpr base = function.base();
                IExpr exponent = function.exponent();
                if (exponent.isNumEqualRational(F.C1D2)) {
                    buf.append("(");
                    convertInternal(buf, base);
                    buf.append(").sqrt()");
                    return;
                } else if (exponent.isNumEqualRational(F.CN1D2)) {
                    buf.append("(");
                    convertInternal(buf, base);
                    buf.append(").reciprocal().sqrt()");
                    return;
                } else if (exponent.isMinusOne()) {
                    buf.append("(");
                    convertInternal(buf, base);
                    buf.append(").reciprocal()");
                    return;
                }
            }
            // Complex atan2(Complex x) {
            buf.append("(");
            convertInternal(buf, function.first());
            buf.append(")" + str);
            if (function.isAST(S.ArcTan, 3)) {
                buf.append("2");
            }
            convertArgs(buf, head, function, 2);
            return;
        }
    }
    if (function.headID() > 0) {
        if (function.isAST(S.Defer, 2) || function.isAST(S.Evaluate, 2) || function.isAST(S.Hold, 2) || function.isUnevaluated()) {
            convertInternal(buf, function.first());
            return;
        }
        if (function.isPower()) {
            IExpr base = function.base();
            IExpr exponent = function.exponent();
            if (exponent.isMinusOne()) {
                buf.append("1.0/(");
                convertInternal(buf, base);
                buf.append(")");
                return;
            }
            if (exponent.isNumEqualRational(F.C1D2)) {
                buf.append("Math.sqrt(");
                convertInternal(buf, base);
                buf.append(")");
                return;
            }
            if (exponent.isNumEqualRational(F.C1D3)) {
                buf.append("Math.cbrt(");
                convertInternal(buf, base);
                buf.append(")");
                return;
            }
            buf.append("Math.pow");
            convertArgs(buf, head, function, 1);
            return;
        }
        buf.append("F.");
        buf.append(head.toString());
        buf.append(".ofN(");
        convertArgs(buf, head, function, 1);
        buf.append(")");
        return;
    }
    if (function.isInfinity()) {
        buf.append("Double.POSITIVE_INFINITY");
        return;
    }
    if (function.isNegativeInfinity()) {
        buf.append("Double.NEGATIVE_INFINITY");
        return;
    }
    convertInternal(buf, head);
    convertArgs(buf, head, function, 1);
}
Also used : IExpr(org.matheclipse.core.interfaces.IExpr) Complex(org.hipparchus.complex.Complex)

Example 33 with Complex

use of org.hipparchus.complex.Complex in project symja_android_library by axkr.

the class NumericArrayExpr method arrayComplexDoubleRecursive.

private static boolean arrayComplexDoubleRecursive(IAST nestedListsOfValues, int level, double[] doubleArr, int[] index) throws RangeException, TypeException {
    level--;
    for (int i = 1; i < nestedListsOfValues.size(); i++) {
        IExpr arg = nestedListsOfValues.get(i);
        if (level == 0) {
            if (arg.isList()) {
                return false;
            }
            Complex value = arg.evalComplex();
            doubleArr[index[0]++] = value.getReal();
            doubleArr[index[0]++] = value.getImaginary();
        } else {
            if (!arg.isList() || !arrayDoubleRecursive((IAST) arg, level, doubleArr, index)) {
                return false;
            }
        }
    }
    return true;
}
Also used : IExpr(org.matheclipse.core.interfaces.IExpr) Complex(org.hipparchus.complex.Complex)

Example 34 with Complex

use of org.hipparchus.complex.Complex in project symja_android_library by axkr.

the class ExprEvaluatorTest method testEvalComplex.

public void testEvalComplex() {
    EvalEngine engine = new EvalEngine(true);
    ExprEvaluator eval = new ExprEvaluator(engine, true, (short) 20);
    String str = "1/(Pi + I)";
    Complex c = eval.evalComplex(str);
    assertEquals("(0.2890254822222363, -0.09199966835037525)", c.toString());
}
Also used : ExprEvaluator(org.matheclipse.core.eval.ExprEvaluator) EvalEngine(org.matheclipse.core.eval.EvalEngine) Complex(org.hipparchus.complex.Complex)

Example 35 with Complex

use of org.hipparchus.complex.Complex in project symja_android_library by axkr.

the class InverseFourier method fourier.

/**
 * Uses decimation-in-time or Cooley-Tukey FFT
 *
 * @param vector of length of power of 2
 * @param b is +1 for forward, and -1 for inverse transform
 * @return discrete Fourier transform of given vector
 */
private static IAST fourier(IAST vector, int b) {
    final int n = vector.argSize();
    Complex[] array = Convert.list2Complex(vector);
    if (array == null) {
        return F.NIL;
    }
    // FastFourierTransformer fft = new FastFourierTransformer(DftNormalization.UNITARY);
    // org.hipparchus.complex.Complex[] result = fft.transform(array, TransformType.INVERSE);
    // return Object2Expr.convertComplex(true, result);
    int j = 0;
    for (int i = 0; i < n; ++i) {
        if (j > i) {
            Complex val = array[i];
            array[i] = array[j];
            array[j] = val;
        }
        int m = n >> 1;
        while (m > 0 && j >= m) {
            j -= m;
            m >>= 1;
        }
        j += m;
    }
    int mmax = 1;
    while (n > mmax) {
        int istep = mmax << 1;
        final double thalf = b * Math.PI / istep;
        final double wtemp = Math.sin(thalf);
        Complex wp = new Complex(-2 * wtemp * wtemp, Math.sin(thalf + thalf));
        Complex w = Complex.ONE;
        for (int m = 0; m < mmax; ++m) {
            for (int i = m; i < n; i += istep) {
                j = i + mmax;
                Complex temp = array[j].multiply(w);
                array[j] = array[i].subtract(temp);
                array[i] = array[i].add(temp);
            }
            w = w.add(w.multiply(wp));
        }
        mmax = istep;
    }
    return F.Divide(Convert.toVector(array), F.Sqrt(n));
}
Also used : Complex(org.hipparchus.complex.Complex)

Aggregations

Complex (org.hipparchus.complex.Complex)74 ArgumentTypeException (org.matheclipse.core.eval.exception.ArgumentTypeException)12 EvalEngine (org.matheclipse.core.eval.EvalEngine)8 IExpr (org.matheclipse.core.interfaces.IExpr)6 Config (org.matheclipse.core.basic.Config)4 F (org.matheclipse.core.expression.F)4 IAST (org.matheclipse.core.interfaces.IAST)4 Gamma (org.hipparchus.special.Gamma)3 Arithmetic (org.matheclipse.core.builtin.Arithmetic)3 IterationLimitExceeded (org.matheclipse.core.eval.exception.IterationLimitExceeded)3 ValidateException (org.matheclipse.core.eval.exception.ValidateException)3 S (org.matheclipse.core.expression.S)3 IComplex (org.matheclipse.core.interfaces.IComplex)3 ISymbol (org.matheclipse.core.interfaces.ISymbol)3 Math.abs (java.lang.Math.abs)2 ArrayList (java.util.ArrayList)2 Function (java.util.function.Function)2 IntFunction (java.util.function.IntFunction)2 DSFactory (org.hipparchus.analysis.differentiation.DSFactory)2 FiniteDifferencesDifferentiator (org.hipparchus.analysis.differentiation.FiniteDifferencesDifferentiator)2