Search in sources :

Example 36 with ISignedNumber

use of org.matheclipse.core.interfaces.ISignedNumber in project symja_android_library by axkr.

the class ExprEvaluator method evaluateDoube.

/**
	 * Parse the given <code>expression String</code> and evaluate it to a double value
	 * 
	 * @param expression
	 * @return
	 * @throws SyntaxError
	 */
public double evaluateDoube(final String inputExpression) {
    if (inputExpression != null) {
        EvalEngine.set(engine);
        engine.reset();
        fExpr = engine.parse(inputExpression);
        if (fExpr != null) {
            IExpr temp = evaluate(F.N(fExpr));
            if (temp.isSignedNumber()) {
                return ((ISignedNumber) temp).doubleValue();
            }
        }
    }
    return Double.NaN;
}
Also used : ISignedNumber(org.matheclipse.core.interfaces.ISignedNumber) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 37 with ISignedNumber

use of org.matheclipse.core.interfaces.ISignedNumber in project symja_android_library by axkr.

the class NIntegrate method evaluate.

@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
    Validate.checkRange(ast, 3);
    ISymbol method = LegendreGauss;
    int maxPoints = DEFAULT_MAX_POINTS;
    int maxIterations = DEFAULT_MAX_ITERATIONS;
    // automatic scale value
    int precisionGoal = 16;
    if (ast.size() >= 4) {
        final Options options = new Options(ast.topHead(), ast, 3, engine);
        IExpr option = options.getOption("Method");
        if (option.isSymbol()) {
            method = (ISymbol) option;
        }
        option = options.getOption("MaxPoints");
        if (option.isSignedNumber()) {
            try {
                maxPoints = ((ISignedNumber) option).toInt();
            } catch (ArithmeticException ae) {
                engine.printMessage("Error in option MaxPoints. Using default value: " + maxPoints);
            }
        }
        option = options.getOption("MaxIterations");
        if (option.isSignedNumber()) {
            try {
                maxIterations = ((ISignedNumber) option).toInt();
            } catch (ArithmeticException ae) {
                engine.printMessage("Error in option MaxIterations. Using default value: " + maxIterations);
            }
        }
        option = options.getOption("PrecisionGoal");
        if (option.isSignedNumber()) {
            try {
                precisionGoal = ((ISignedNumber) option).toInt();
            } catch (ArithmeticException ae) {
                engine.printMessage("Error in option PrecisionGoal. Using default value: " + precisionGoal);
            }
        }
    }
    if (ast.arg2().isList()) {
        IAST list = (IAST) ast.arg2();
        IExpr function = ast.arg1();
        if (list.isAST3() && list.arg1().isSymbol()) {
            ISignedNumber min = list.arg2().evalSignedNumber();
            ISignedNumber max = list.arg3().evalSignedNumber();
            if (min != null && max != null) {
                if (function.isAST(F.Equal, 3)) {
                    function = F.Plus(((IAST) function).arg1(), F.Negate(((IAST) function).arg2()));
                }
                try {
                    double result = integrate(method.getSymbolName(), list, min.doubleValue(), max.doubleValue(), function, maxPoints, maxIterations);
                    result = Precision.round(result, precisionGoal);
                    return Num.valueOf(result);
                } catch (Exception e) {
                    throw new WrappedException(e);
                }
            }
        }
    }
    return F.NIL;
}
Also used : Options(org.matheclipse.core.eval.util.Options) WrappedException(org.matheclipse.core.eval.exception.WrappedException) ISymbol(org.matheclipse.core.interfaces.ISymbol) ISignedNumber(org.matheclipse.core.interfaces.ISignedNumber) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST) WrappedException(org.matheclipse.core.eval.exception.WrappedException) MathIllegalStateException(org.hipparchus.exception.MathIllegalStateException)

Example 38 with ISignedNumber

use of org.matheclipse.core.interfaces.ISignedNumber in project symja_android_library by axkr.

the class NDSolve method evaluate.

@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
    if (!ToggleFeature.DSOLVE) {
        return F.NIL;
    }
    Validate.checkSize(ast, 4);
    if (ast.arg3().isAST(F.List, 4)) {
        try {
            IAST uFunctionSymbols = Validate.checkSymbolOrSymbolList(ast, 2);
            int dimension = uFunctionSymbols.size() - 1;
            IAST xVarList = (IAST) ast.arg3();
            ISymbol xVar = (ISymbol) xVarList.arg1();
            IExpr xMinExpr = xVarList.arg2();
            IExpr xMaxExpr = xVarList.arg3();
            if (xMinExpr.isSignedNumber() && xMaxExpr.isSignedNumber()) {
                double xMin = ((ISignedNumber) xMinExpr).doubleValue();
                double xMax = ((ISignedNumber) xMaxExpr).doubleValue();
                double xStep = 0.1;
                IAST listOfEquations = Validate.checkEquations(ast, 1).clone();
                IExpr[][] boundaryCondition = new IExpr[2][dimension];
                int i = 1;
                while (i < listOfEquations.size()) {
                    IExpr equation = listOfEquations.get(i);
                    if (equation.isFree(xVar)) {
                        if (determineSingleBoundary(equation, uFunctionSymbols, xVar, boundaryCondition, engine)) {
                            listOfEquations.remove(i);
                            continue;
                        }
                    }
                    i++;
                }
                IExpr[] dotEquations = new IExpr[dimension];
                i = 1;
                while (i < listOfEquations.size()) {
                    IExpr equation = listOfEquations.get(i);
                    if (!equation.isFree(xVar)) {
                        if (determineSingleDotEquation(equation, uFunctionSymbols, xVar, dotEquations, engine)) {
                            listOfEquations.remove(i);
                            continue;
                        }
                    }
                    i++;
                }
                if (uFunctionSymbols.isList()) {
                    AbstractIntegrator dp853 = new DormandPrince853Integrator(1.0e-8, 100.0, 1.0e-10, 1.0e-10);
                    double[] xyz = new double[dimension];
                    for (int j = 0; j < dimension; j++) {
                        xyz[j] = ((INum) engine.evalN(boundaryCondition[1][j])).doubleValue();
                    }
                    OrdinaryDifferentialEquation ode = new FirstODE(engine, dotEquations, uFunctionSymbols, xVar);
                    IAST resultList = F.List();
                    IAST result = F.Interpolation(resultList);
                    IAST list;
                    for (double tDouble = xMin; tDouble < xMax; tDouble += xStep) {
                        list = F.List(F.num(tDouble));
                        dp853.integrate(ode, tDouble, xyz, tDouble + xStep, xyz);
                        for (int j = 0; j < xyz.length; j++) {
                            list.append(F.num(xyz[j]));
                        }
                        resultList.append(list);
                    }
                    resultList.setEvalFlags(IAST.IS_MATRIX);
                    return result;
                }
            }
        } catch (RuntimeException rex) {
            if (Config.SHOW_STACKTRACE) {
                rex.printStackTrace();
            }
        }
    }
    return F.NIL;
}
Also used : ISymbol(org.matheclipse.core.interfaces.ISymbol) ISignedNumber(org.matheclipse.core.interfaces.ISignedNumber) AbstractIntegrator(org.hipparchus.ode.AbstractIntegrator) OrdinaryDifferentialEquation(org.hipparchus.ode.OrdinaryDifferentialEquation) IAST(org.matheclipse.core.interfaces.IAST) IExpr(org.matheclipse.core.interfaces.IExpr) DormandPrince853Integrator(org.hipparchus.ode.nonstiff.DormandPrince853Integrator)

Example 39 with ISignedNumber

use of org.matheclipse.core.interfaces.ISignedNumber in project symja_android_library by axkr.

the class Min method evaluate.

@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
    Validate.checkRange(ast, 1);
    if (ast.isAST0()) {
        return F.CInfinity;
    }
    if (ast.arg1().isInterval1()) {
        IAST list = (IAST) ast.arg1().getAt(1);
        try {
            return (ISignedNumber) list.arg1();
        } catch (ClassCastException cca) {
        // do nothing
        }
    }
    IAST resultList = EvalAttributes.flatten(F.List, ast);
    if (resultList.isPresent()) {
        return minimum(resultList, true);
    }
    return minimum(ast, false);
}
Also used : ISignedNumber(org.matheclipse.core.interfaces.ISignedNumber) IAST(org.matheclipse.core.interfaces.IAST)

Example 40 with ISignedNumber

use of org.matheclipse.core.interfaces.ISignedNumber in project symja_android_library by axkr.

the class CoreCallbackFunction method evaluate.

@Override
public double evaluate(DoubleEvaluator doubleEngine, FunctionNode functionNode, double[] args) {
    ASTNode node = functionNode.getNode(0);
    if (node instanceof SymbolNode) {
        AST2Expr ast2Expr = new AST2Expr();
        IExpr head = ast2Expr.convert(node);
        IAST fun = F.ast(head);
        for (int i = 0; i < args.length; i++) {
            fun.append(F.num(args[i]));
        }
        final IExpr result = F.evaln(fun);
        if (result.isSignedNumber()) {
            return ((ISignedNumber) result).doubleValue();
        }
    } else if (node instanceof FunctionNode) {
        AST2Expr ast2Expr = new AST2Expr();
        IExpr head = ast2Expr.convert(node);
        IAST fun = F.ast(head);
        for (int i = 0; i < args.length; i++) {
            fun.append(F.num(args[i]));
        }
        final IExpr result = F.evaln(fun);
        if (result.isSignedNumber()) {
            return ((ISignedNumber) result).doubleValue();
        }
    }
    throw new MathException("CoreCallbackFunction#evaluate() not possible for: " + functionNode.toString());
}
Also used : SymbolNode(org.matheclipse.parser.client.ast.SymbolNode) ISignedNumber(org.matheclipse.core.interfaces.ISignedNumber) MathException(org.matheclipse.parser.client.math.MathException) ASTNode(org.matheclipse.parser.client.ast.ASTNode) FunctionNode(org.matheclipse.parser.client.ast.FunctionNode) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST) AST2Expr(org.matheclipse.core.convert.AST2Expr)

Aggregations

ISignedNumber (org.matheclipse.core.interfaces.ISignedNumber)49 IExpr (org.matheclipse.core.interfaces.IExpr)31 IAST (org.matheclipse.core.interfaces.IAST)22 ISymbol (org.matheclipse.core.interfaces.ISymbol)10 WrongArgumentType (org.matheclipse.core.eval.exception.WrongArgumentType)7 LinearConstraint (org.hipparchus.optim.linear.LinearConstraint)3 Num (org.matheclipse.core.expression.Num)3 IInteger (org.matheclipse.core.interfaces.IInteger)3 IntegerDistribution (org.hipparchus.distribution.IntegerDistribution)2 RealDistribution (org.hipparchus.distribution.RealDistribution)2 MathIllegalStateException (org.hipparchus.exception.MathIllegalStateException)2 LinearObjectiveFunction (org.hipparchus.optim.linear.LinearObjectiveFunction)2 WrappedException (org.matheclipse.core.eval.exception.WrappedException)2 Options (org.matheclipse.core.eval.util.Options)2 ComplexNum (org.matheclipse.core.expression.ComplexNum)2 IComplex (org.matheclipse.core.interfaces.IComplex)2 IComplexNum (org.matheclipse.core.interfaces.IComplexNum)2 INum (org.matheclipse.core.interfaces.INum)2 IRational (org.matheclipse.core.interfaces.IRational)2 ArrayList (java.util.ArrayList)1