Search in sources :

Example 36 with Complex

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

the class Convert method list2ComplexMatrix.

/**
 * Returns a <code>FieldMatrix<Complex></code> if possible.
 *
 * @param expr
 * @return <code>null</code> if the conversion isn't possible.
 * @throws ClassCastException
 * @throws IndexOutOfBoundsException
 */
public static FieldMatrix<Complex> list2ComplexMatrix(IExpr expr) throws ClassCastException, IndexOutOfBoundsException {
    if (expr == null) {
        return null;
    }
    int[] dim = expr.isMatrix();
    if (dim == null || dim[0] == 0 || dim[1] == 0) {
        return null;
    }
    if (expr.isSparseArray()) {
        // TODO optimize for sparse arrays
        // ISparseArray array = (ISparseArray) expr;
        expr = ((ISparseArray) expr).normal(false);
    }
    if (expr.isList()) {
        try {
            IAST list = (IAST) expr;
            IAST currInRow = (IAST) list.arg1();
            if (currInRow.isAST0()) {
                // special case 0-Matrix
                Complex[][] array = new Complex[0][0];
                return new Array2DRowFieldMatrix<Complex>(array, false);
            }
            final int rowSize = expr.argSize();
            final int colSize = currInRow.argSize();
            final Complex[][] elements = new Complex[rowSize][colSize];
            for (int i = 1; i < rowSize + 1; i++) {
                currInRow = (IAST) list.get(i);
                if (currInRow.isVector() < 0 || colSize != currInRow.argSize()) {
                    return null;
                }
                for (int j = 1; j < colSize + 1; j++) {
                    elements[i - 1][j - 1] = currInRow.get(j).evalComplex();
                }
            }
            return new Array2DRowFieldMatrix<Complex>(elements, false);
        } catch (ValidateException vex) {
        // pass
        }
    }
    return null;
}
Also used : ValidateException(org.matheclipse.core.eval.exception.ValidateException) Array2DRowFieldMatrix(org.hipparchus.linear.Array2DRowFieldMatrix) IAST(org.matheclipse.core.interfaces.IAST) Complex(org.hipparchus.complex.Complex)

Example 37 with Complex

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

the class HypergeometricJS method hypergeometric1F1.

// public static Complex hypergeometric1F1(Complex a, Complex b, Complex x) {
// return hypergeometric1F1(a, b, x, Config.SPECIAL_FUNCTIONS_TOLERANCE);
// }
public static Complex hypergeometric1F1(Complex a, Complex b, Complex x) {
    final double useAsymptotic = 30;
    if (b.isMathematicalInteger() && b.getReal() <= 0) {
        throw new ArgumentTypeException("hypergeometric function pole");
    }
    // Kummer transformation
    if (x.getReal() < 0) {
        return x.exp().multiply(hypergeometric1F1(b.subtract(a), b, x.negate()));
    }
    // asymptotic form as per Johansson arxiv.org/abs/1606.06977
    if (x.norm() > useAsymptotic) {
        Complex t1 = Arithmetic.lanczosApproxGamma(b).multiply(x.negate().pow(a.negate())).multiply(Arithmetic.lanczosApproxGamma(b.subtract(a)).reciprocal());
        t1 = t1.multiply(hypergeometric2F0(a, a.add(b.negate()).add(1.0), new Complex(-1.0).divide(x)));
        Complex t2 = Arithmetic.lanczosApproxGamma(b).multiply(x.pow(a.subtract(b))).multiply(x.exp()).multiply(Arithmetic.lanczosApproxGamma(a).reciprocal());
        t2 = t2.multiply(hypergeometric2F0(b.subtract(a), Complex.ONE.subtract(a), Complex.ONE.divide(x)));
        return t1.add(t2);
    }
    Complex s = Complex.ONE;
    Complex p = Complex.ONE;
    long i = 1;
    long iterationLimit = EvalEngine.get().getIterationLimit();
    while (Math.abs(p.getReal()) > Config.SPECIAL_FUNCTIONS_TOLERANCE || Math.abs(p.getImaginary()) > Config.SPECIAL_FUNCTIONS_TOLERANCE) {
        p = p.multiply(x).multiply(a).multiply(b.reciprocal()).divide(i);
        s = s.add(p);
        a = a.add(1.0);
        b = b.add(1.0);
        if (i++ > iterationLimit && iterationLimit > 0) {
            IterationLimitExceeded.throwIt(i, S.Hypergeometric1F1);
        }
    }
    return s;
}
Also used : ArgumentTypeException(org.matheclipse.core.eval.exception.ArgumentTypeException) Complex(org.hipparchus.complex.Complex)

Example 38 with Complex

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

the class HypergeometricJS method hypergeometric1F2.

public static Complex hypergeometric1F2(Complex a, Complex b, Complex c, Complex x) {
    final int useAsymptotic = 200;
    if (x.norm() > useAsymptotic) {
        Complex p = a.add(b.negate()).add(c.negate()).add(0.5).divide(2.0);
        ArrayList<Complex> ck = new ArrayList<Complex>();
        // 
        ck.add(Complex.ONE);
        ck.add(((a.multiply(3.0).add(b).add(c).add(-2.0)).multiply(a.subtract(b.add(c))).multiply(0.5)).add(b.multiply(c).multiply(2)).add(// 
        -3.0 / 8.0));
        ck.add((a.multiply(3.0).add(b).add(c).add(-2.0)).multiply(a.subtract(b.add(c))).multiply(0.25).add(b.multiply(c).add(-3.0 / 16.0)).pow(2).multiply(// 
        2));
        // 
        ck.add(new Complex(-1.0).multiply(a.multiply(2.0).subtract(3.0)).multiply(b).multiply(c));
        ck.add(a.pow(2.0).multiply(-8.0).add(a.multiply(11.0)).add(b).add(c).add(-2.0).multiply(a.subtract(b.add(c))).multiply(// 
        0.25));
        ck.add(new Complex(-3.0 / 16.0));
        IntFunction<Complex> w = k -> ck.get(k).multiply(x.negate().pow(-k / 2.0)).divide(Math.pow(2.0, k));
        Complex u1 = Complex.I.multiply(p.multiply(Math.PI).add(x.negate().sqrt().multiply(2.0))).exp();
        Complex u2 = new Complex(0.0, -1.0).multiply(p.multiply(Math.PI).add(x.negate().sqrt().multiply(2.0))).exp();
        Complex wLast = w.apply(2);
        Complex w2Negate = wLast.negate();
        Complex s = // 
        u1.multiply(new Complex(0.0, -1.0).multiply(w.apply(1)).add(w2Negate).add(1.0)).add(u2.multiply(Complex.I.multiply(w.apply(1)).add(w2Negate).add(1.0)));
        int k = 3;
        while (wLast.norm() > w.apply(k).norm()) {
            // 
            ck.add(a.multiply(-6.0).add(b.multiply(2)).add(c.multiply(2.0)).add(-4.0).multiply(k).add(a.pow(a).multiply(3.0)).add(b.subtract(c).pow(2.0).negate()).add(a.multiply(b.add(c).add(-2)).multiply(2.0).negate()).add(0.25).add(3.0 * k * k).multiply(1.0 / (2.0 * k)).multiply(// 
            ck.get(k - 1)).subtract(a.negate().add(b).add(c.negate()).add(-0.5).add(k).multiply(a.negate().add(b.negate()).add(c).add(-0.5).add(k)).multiply(a.negate().add(b).add(c).add(-2.5).add(k)).multiply(// 
            ck.get(k - 2))));
            wLast = w.apply(k);
            s = s.add(// 
            u1.multiply(new Complex(0.0, -1.0).pow(k)).multiply(wLast).add(u2.multiply(Complex.I.pow(k)).multiply(wLast)));
            k++;
        }
        Complex t1 = Arithmetic.lanczosApproxGamma(a).reciprocal().multiply(x.negate().pow(p)).multiply(s).divide(2.0 * Math.sqrt(Math.PI));
        Complex t2 = Arithmetic.lanczosApproxGamma(b.subtract(a)).reciprocal().multiply(Arithmetic.lanczosApproxGamma(c.subtract(a)).reciprocal()).multiply(x.negate().pow(a.negate())).multiply(hypergeometricSeries(new Complex[] { a, a.add(b.negate()).add(1), a.add(c.negate().add(1.0)) }, new Complex[] {}, // , true ) );
        x.reciprocal()));
        // hypergeometricSeries( [ a, add(a,neg(b),1), add(a,neg(c),1) ], [], inv(x), true ) );
        return Arithmetic.lanczosApproxGamma(b).multiply(Arithmetic.lanczosApproxGamma(c)).multiply(t1.add(t2));
    }
    return hypergeometricSeries(new Complex[] { a }, new Complex[] { b, c }, x);
}
Also used : Gamma(org.hipparchus.special.Gamma) EvalEngine(org.matheclipse.core.eval.EvalEngine) ResultException(org.matheclipse.core.eval.exception.ResultException) F(org.matheclipse.core.expression.F) Complex(org.hipparchus.complex.Complex) Config(org.matheclipse.core.basic.Config) Math.abs(java.lang.Math.abs) Function(java.util.function.Function) ArrayList(java.util.ArrayList) S(org.matheclipse.core.expression.S) Arithmetic(org.matheclipse.core.builtin.Arithmetic) RecursionLimitExceeded(org.matheclipse.core.eval.exception.RecursionLimitExceeded) IterationLimitExceeded(org.matheclipse.core.eval.exception.IterationLimitExceeded) IntFunction(java.util.function.IntFunction) ArgumentTypeException(org.matheclipse.core.eval.exception.ArgumentTypeException) ArrayList(java.util.ArrayList) Complex(org.hipparchus.complex.Complex)

Example 39 with Complex

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

the class Convert method list2ComplexVector.

public static FieldVector<Complex> list2ComplexVector(IExpr expr) throws ClassCastException {
    if (expr == null) {
        return null;
    }
    int dim = expr.isVector();
    if (dim <= 0) {
        return null;
    }
    if (expr.isSparseArray()) {
        // ISparseArray array = (ISparseArray) expr;
        // return array.toFieldVector(false);
        expr = ((ISparseArray) expr).normal(false);
    }
    if (expr.isList()) {
        try {
            final int rowSize = expr.argSize();
            IAST list = (IAST) expr;
            final Complex[] elements = new Complex[rowSize];
            for (int i = 0; i < rowSize; i++) {
                elements[i] = list.get(i + 1).evalComplex();
            }
            return new ArrayFieldVector<Complex>(elements, false);
        } catch (ValidateException vex) {
        // pass
        }
    }
    return null;
}
Also used : ArrayFieldVector(org.hipparchus.linear.ArrayFieldVector) ValidateException(org.matheclipse.core.eval.exception.ValidateException) IAST(org.matheclipse.core.interfaces.IAST) Complex(org.hipparchus.complex.Complex)

Example 40 with Complex

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

the class BesselJS method airyBiPrime.

public static Complex airyBiPrime(Complex x) {
    if (F.isZero(x)) {
        return new Complex(Math.pow(3.0, 1.6) / GammaJS.gamma(1.0 / 3.0));
    }
    if (x.getReal() < 0) {
        Complex xMinus = x.negate();
        Complex z = xMinus.pow(1.5).multiply(2.0 / 3.0);
        return xMinus.multiply(1.0 / Math.sqrt(3.0)).multiply(besselJ(2.0 / 3.0, z).add(besselJ(-2.0 / 3.0, z)));
    }
    Complex z = x.pow(1.5).multiply(2.0 / 3.0);
    return x.multiply(1.0 / Math.sqrt(3.0)).multiply(besselI(2.0 / 3.0, z).add(besselI(-2.0 / 3.0, z)));
}
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