Search in sources :

Example 51 with Complex

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

the class HypergeometricJS method hypergeometric2F0.

public static Complex hypergeometric2F0(Complex a, Complex b, Complex x, double tolerance) {
    int terms = 50;
    Complex s = Complex.ONE;
    Complex p = Complex.ONE;
    Complex pLast = p;
    // first few terms can be larger than unity
    boolean converging = false;
    int i = 1;
    while (Math.abs(p.getReal()) > tolerance || Math.abs(p.getImaginary()) > tolerance) {
        p = p.multiply(x).multiply(a).multiply(b).divide(i);
        if (p.norm() > pLast.norm() && converging) {
            // prevent runaway sum
            break;
        }
        if (p.norm() < pLast.norm()) {
            converging = true;
        }
        if (i > terms) {
            throw new ArgumentTypeException("not converging after " + terms + " terms");
        }
        s = s.add(p);
        a = a.add(1.0);
        b = b.add(1.0);
        i++;
        pLast = p;
    }
    return s;
}
Also used : Complex(org.hipparchus.complex.Complex) ArgumentTypeException(org.matheclipse.core.eval.exception.ArgumentTypeException)

Example 52 with Complex

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

the class HypergeometricJS method hypergeometric0F1.

// public static Complex hypergeometric0F1(Complex a, Complex x) {
// return hypergeometric0F1(a, x, Config.SPECIAL_FUNCTIONS_TOLERANCE);
// }
public static Complex hypergeometric0F1(Complex a, Complex x) {
    final double useAsymptotic = 100;
    if (a.isMathematicalInteger() && a.getReal() <= 0) {
        throw new ArgumentTypeException("hypergeometric function pole");
    }
    // asymptotic form as per Johansson
    if (x.norm() > useAsymptotic) {
        // transform variables for convenience
        Complex b = a.multiply(2).subtract(1);
        a = a.subtract(0.5);
        x = x.sqrt().multiply(4.0);
        // copied from hypergeometric1F1
        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), 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 x.divide(-2.0).exp().multiply(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.reciprocal()).divide(i);
        s = s.add(p);
        a = a.add(1);
        if (i++ > iterationLimit && iterationLimit > 0) {
            IterationLimitExceeded.throwIt(i, S.Hypergeometric0F1);
        }
    }
    return s;
}
Also used : ArgumentTypeException(org.matheclipse.core.eval.exception.ArgumentTypeException) Complex(org.hipparchus.complex.Complex)

Example 53 with Complex

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

the class NumericArrayExpr method arrayComplexFloatRecursive.

private static boolean arrayComplexFloatRecursive(IAST nestedListsOfValues, int level, float[] floatArr, 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();
            floatArr[index[0]++] = (float) value.getReal();
            floatArr[index[0]++] = (float) value.getImaginary();
        } else {
            if (!arg.isList() || !arrayComplexFloatRecursive((IAST) arg, level, floatArr, index)) {
                return false;
            }
        }
    }
    return true;
}
Also used : IExpr(org.matheclipse.core.interfaces.IExpr) Complex(org.hipparchus.complex.Complex)

Example 54 with Complex

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

the class ComplexNum method quotientRemainder.

/**
 * Return the quotient and remainder as an array <code>[quotient, remainder]</code> of the
 * division of <code>Complex</code> numbers <code>c1, c2</code>.
 *
 * <p>
 * See
 *
 * <ul>
 * <li><a href="https://en.wikipedia.org/wiki/Gaussian_integer">Wikipedia - Gaussian integer</a>
 * <li><a href=
 * "http://fermatslasttheorem.blogspot.com/2005/06/division-algorithm-for-gaussian.html">Division
 * Algorithm for Gaussian Integers </a>
 * </ul>
 *
 * @param cn1
 * @param cn2
 * @return the quotient and remainder as an array <code>[quotient, remainder]</code>
 */
public static ComplexNum[] quotientRemainder(ComplexNum cn1, ComplexNum cn2) {
    Complex c1 = cn1.fComplex;
    Complex c2 = cn2.fComplex;
    Complex[] arr = quotientRemainder(c1, c2);
    return new ComplexNum[] { valueOf(arr[0]), valueOf(arr[1]) };
}
Also used : IComplexNum(org.matheclipse.core.interfaces.IComplexNum) IComplex(org.matheclipse.core.interfaces.IComplex) Complex(org.hipparchus.complex.Complex)

Example 55 with Complex

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

the class ComplexFormFactory method convertInternal.

private void convertInternal(final StringBuilder buf, final IExpr o, final int precedence, boolean isASTHead) {
    if (o instanceof IAST) {
        final IAST list = (IAST) o;
        if (list.head().isSymbol()) {
            ISymbol head = (ISymbol) list.head();
            final Operator operator = getOperator(head);
            if (operator != null) {
                if (operator instanceof PostfixOperator) {
                    if (list.isAST1()) {
                        convertPostfixOperator(buf, list, (PostfixOperator) operator, precedence);
                        return;
                    }
                } else {
                    if (convertOperator(operator, list, buf, isASTHead ? Integer.MAX_VALUE : precedence, head)) {
                        return;
                    }
                }
            }
        }
        convertAST(buf, list);
        return;
    }
    if (o instanceof ISignedNumber) {
        double d = o.evalDouble();
        if (fPackagePrefix) {
            buf.append("org.hipparchus.complex.");
        }
        buf.append("Complex.valueOf(" + d + ")");
        return;
    }
    if (o instanceof INumber) {
        Complex c = o.evalComplex();
        if (c != null) {
            if (fPackagePrefix) {
                buf.append("org.hipparchus.complex.");
            }
            buf.append("Complex.valueOf(" + c.getReal() + ", " + c.getImaginary() + ")");
        } else {
            buf.append("Complex.valueOf(" + o.toString() + ")");
        }
        return;
    }
    if (o instanceof ISymbol) {
        convertSymbol(buf, (ISymbol) o);
        return;
    }
    if (o instanceof IPatternObject) {
        convertPattern(buf, (IPatternObject) o);
        return;
    }
    convertString(buf, o.toString());
}
Also used : PostfixOperator(org.matheclipse.parser.client.operator.PostfixOperator) Operator(org.matheclipse.parser.client.operator.Operator) InfixOperator(org.matheclipse.parser.client.operator.InfixOperator) PrefixOperator(org.matheclipse.parser.client.operator.PrefixOperator) ISymbol(org.matheclipse.core.interfaces.ISymbol) PostfixOperator(org.matheclipse.parser.client.operator.PostfixOperator) ISignedNumber(org.matheclipse.core.interfaces.ISignedNumber) INumber(org.matheclipse.core.interfaces.INumber) IPatternObject(org.matheclipse.core.interfaces.IPatternObject) IAST(org.matheclipse.core.interfaces.IAST) 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