Search in sources :

Example 21 with ArgumentTypeException

use of org.matheclipse.core.eval.exception.ArgumentTypeException in project symja_android_library by axkr.

the class CreamConvert method integerVariable.

private IntVariable integerVariable(Network net, IExpr expr) throws ArithmeticException {
    if (expr instanceof ISymbol) {
        IntVariable temp = map.get(expr);
        if (temp == null) {
            temp = new IntVariable(net);
            map.put((ISymbol) expr, temp);
        }
        return temp;
    }
    if (expr instanceof IInteger) {
        // throws ArithmeticException
        int value = ((IInteger) expr).toInt();
        return new IntVariable(net, value);
    }
    if (expr.isAST()) {
        IAST ast = (IAST) expr;
        if (ast.isPlus()) {
            IntVariable result = integerVariable(net, ast.arg1());
            for (int i = 2; i < ast.size(); i++) {
                result = result.add(integerVariable(net, ast.get(i)));
            }
            return result;
        } else if (ast.isTimes()) {
            IntVariable result = integerVariable(net, ast.arg1());
            for (int i = 2; i < ast.size(); i++) {
                result = result.multiply(integerVariable(net, ast.get(i)));
            }
            return result;
        } else if (ast.isPower()) {
            IExpr exponent = ast.exponent();
            if (exponent.isInteger()) {
                int value = ((IInteger) exponent).toInt();
                if (value > 0) {
                    IExpr base = ast.base();
                    IntVariable result = integerVariable(net, base);
                    for (int i = 1; i < value; i++) {
                        result = result.multiply(integerVariable(net, base));
                    }
                    return result;
                }
            }
        } else if (ast.isSameHeadSizeGE(S.Max, 3)) {
            IntVariable result = integerVariable(net, ast.arg1());
            for (int i = 2; i < ast.size(); i++) {
                result = result.max(integerVariable(net, ast.get(i)));
            }
            return result;
        } else if (ast.isSameHeadSizeGE(S.Min, 3)) {
            IntVariable result = integerVariable(net, ast.arg1());
            for (int i = 2; i < ast.size(); i++) {
                result = result.min(integerVariable(net, ast.get(i)));
            }
            return result;
        } else if (ast.isAbs()) {
            return integerVariable(net, ast.arg1()).abs();
        } else if (ast.isAST(S.Sign, 2)) {
            return integerVariable(net, ast.arg1()).sign();
        }
    }
    throw new ArgumentTypeException(expr.toString() + " is no int variable found for Solve(..., Integers)");
}
Also used : ISymbol(org.matheclipse.core.interfaces.ISymbol) IInteger(org.matheclipse.core.interfaces.IInteger) IAST(org.matheclipse.core.interfaces.IAST) IExpr(org.matheclipse.core.interfaces.IExpr) IntVariable(jp.ac.kobe_u.cs.cream.IntVariable) ArgumentTypeException(org.matheclipse.core.eval.exception.ArgumentTypeException)

Example 22 with ArgumentTypeException

use of org.matheclipse.core.eval.exception.ArgumentTypeException in project symja_android_library by axkr.

the class Expr2LP method expr2Constraint.

public LinearConstraint expr2Constraint() {
    double[] coefficients = new double[fVariables.size()];
    if (fExpr.isAST()) {
        IAST ast = (IAST) fExpr;
        if (ast.isEqual()) {
            IExpr expr = F.eval(F.Subtract(ast.arg1(), ast.arg2()));
            ISignedNumber num = expr2ObjectiveFunction(expr, coefficients);
            if (num == null) {
                return new LinearConstraint(coefficients, Relationship.EQ, 0);
            }
            return new LinearConstraint(coefficients, Relationship.EQ, -1 * num.doubleValue());
        }
        if (ast.isAST(S.GreaterEqual, 3)) {
            IExpr expr = F.eval(F.Subtract(ast.arg1(), ast.arg2()));
            ISignedNumber num = expr2ObjectiveFunction(expr, coefficients);
            if (num == null) {
                return new LinearConstraint(coefficients, Relationship.GEQ, 0);
            }
            return new LinearConstraint(coefficients, Relationship.GEQ, -1 * num.doubleValue());
        }
        if (ast.isAST(S.LessEqual, 3)) {
            IExpr expr = F.eval(F.Subtract(ast.arg1(), ast.arg2()));
            ISignedNumber num = expr2ObjectiveFunction(expr, coefficients);
            if (num == null) {
                return new LinearConstraint(coefficients, Relationship.LEQ, 0);
            }
            return new LinearConstraint(coefficients, Relationship.LEQ, -1 * num.doubleValue());
        }
    }
    throw new ArgumentTypeException("conversion from expression to linear programming expression failed for " + fExpr.toString());
}
Also used : ISignedNumber(org.matheclipse.core.interfaces.ISignedNumber) LinearConstraint(org.hipparchus.optim.linear.LinearConstraint) IAST(org.matheclipse.core.interfaces.IAST) IExpr(org.matheclipse.core.interfaces.IExpr) ArgumentTypeException(org.matheclipse.core.eval.exception.ArgumentTypeException)

Example 23 with ArgumentTypeException

use of org.matheclipse.core.eval.exception.ArgumentTypeException 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 24 with ArgumentTypeException

use of org.matheclipse.core.eval.exception.ArgumentTypeException in project symja_android_library by axkr.

the class EllipticIntegralsJS method carlsonRF.

private static Complex carlsonRF(Complex x, Complex y, Complex z, double tolerance) {
    // if ( isComplex(x) || isComplex(y) || isComplex(z) ) {
    // if (y.getImaginary()==0.0) {
    // y = new Complex(y.getReal());
    // }
    Complex xm = x;
    Complex ym = y;
    Complex zm = z;
    Complex A0 = x.add(y).add(z).divide(3.0);
    Complex Am = A0;
    double Q = Math.pow(3.0 * tolerance, -1.0 / 6.0) * Math.max(A0.subtract(x).norm(), Math.max(A0.subtract(y).norm(), A0.subtract(z).norm()));
    double g = 0.25;
    double pow4 = 1.0;
    while (true) {
        double absAm = Am.norm();
        if (Double.isNaN(absAm) || Double.isInfinite(absAm)) {
            throw new ArgumentTypeException("carlsonRF: Am is undefined");
        }
        Complex xs = xm.sqrt();
        Complex ys = ym.sqrt();
        Complex zs = zm.sqrt();
        Complex lm = xs.multiply(ys).add(xs.multiply(zs)).add(ys.multiply(zs));
        // Complex Am1 = Am.add(lm).multiply(g);
        xm = xm.add(lm).multiply(g);
        ym = ym.add(lm).multiply(g);
        zm = zm.add(lm).multiply(g);
        if (pow4 * Q < absAm) {
            break;
        }
        // Am=Am1;
        Am = Am.add(lm).multiply(g);
        pow4 *= g;
    }
    Complex t = new Complex(pow4).divide(Am);
    Complex X = A0.subtract(x).multiply(t);
    Complex Y = A0.subtract(y).multiply(t);
    Complex Z = X.add(Y).negate();
    Complex E2 = X.multiply(Y).subtract(Z.multiply(Z));
    Complex E3 = X.multiply(Y).multiply(Z);
    // Am.pow(-0.5)
    Complex AmPow = Am.pow(-0.5);
    return AmPow.multiply(E2.multiply(-924.0).add(E2.multiply(E2).multiply(385.0)).add(E3.multiply(660.0)).add(E2.multiply(E3).multiply(-630.0)).add(9240.0)).multiply(1.0 / 9240.0);
}
Also used : Complex(org.hipparchus.complex.Complex) ArgumentTypeException(org.matheclipse.core.eval.exception.ArgumentTypeException)

Example 25 with ArgumentTypeException

use of org.matheclipse.core.eval.exception.ArgumentTypeException 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)

Aggregations

ArgumentTypeException (org.matheclipse.core.eval.exception.ArgumentTypeException)44 IExpr (org.matheclipse.core.interfaces.IExpr)24 IAST (org.matheclipse.core.interfaces.IAST)16 Complex (org.hipparchus.complex.Complex)11 ISymbol (org.matheclipse.core.interfaces.ISymbol)10 EvalEngine (org.matheclipse.core.eval.EvalEngine)6 UnaryNumerical (org.matheclipse.core.generic.UnaryNumerical)6 UnivariateDifferentiableFunction (org.hipparchus.analysis.differentiation.UnivariateDifferentiableFunction)5 ISignedNumber (org.matheclipse.core.interfaces.ISignedNumber)5 ArrayList (java.util.ArrayList)4 IASTAppendable (org.matheclipse.core.interfaces.IASTAppendable)4 DSFactory (org.hipparchus.analysis.differentiation.DSFactory)3 FiniteDifferencesDifferentiator (org.hipparchus.analysis.differentiation.FiniteDifferencesDifferentiator)3 Config (org.matheclipse.core.basic.Config)3 F (org.matheclipse.core.expression.F)3 IInteger (org.matheclipse.core.interfaces.IInteger)3 INum (org.matheclipse.core.interfaces.INum)3 BisectionSolver (org.hipparchus.analysis.solvers.BisectionSolver)2 LinearConstraint (org.hipparchus.optim.linear.LinearConstraint)2 Gamma (org.hipparchus.special.Gamma)2