Search in sources :

Example 91 with ISymbol

use of org.matheclipse.core.interfaces.ISymbol 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 92 with ISymbol

use of org.matheclipse.core.interfaces.ISymbol 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 93 with ISymbol

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

the class NFourierTransform method evaluate.

@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
    Validate.checkRange(ast, 4, 5);
    IExpr expr = ast.arg1();
    ISymbol t = Validate.checkSymbolType(ast, 2);
    // IExpr omega = ast.arg3();
    if (ast.size() > 4) {
        final Options options = new Options(ast.topHead(), ast, 4, engine);
        IExpr optionFourierParameters = options.getOption("FourierParameters");
        if (optionFourierParameters.isList()) {
        // analyze the parameters, if they are correct
        }
    }
    UnivariateFunction f = new UnaryNumerical(expr, t, engine);
    FastFourierTransformer fft = new FastFourierTransformer(DftNormalization.STANDARD);
    org.hipparchus.complex.Complex[] result = fft.transform(f, -1.0, 1.0, 8, TransformType.FORWARD);
    return Object2Expr.convertComplex(result);
}
Also used : Options(org.matheclipse.core.eval.util.Options) UnaryNumerical(org.matheclipse.core.generic.UnaryNumerical) ISymbol(org.matheclipse.core.interfaces.ISymbol) UnivariateFunction(org.hipparchus.analysis.UnivariateFunction) IExpr(org.matheclipse.core.interfaces.IExpr) FastFourierTransformer(org.hipparchus.transform.FastFourierTransformer)

Example 94 with ISymbol

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

the class InterpolatingPolynomial method evaluate.

@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
    Validate.checkSize(ast, 3);
    if (ast.arg1().isList() && ast.arg2().isSymbol()) {
        final IAST list = (IAST) ast.arg1();
        final ISymbol x = (ISymbol) ast.arg2();
        if (list.size() > 1) {
            int n = list.size() - 1;
            IExpr[] xv = new IExpr[n];
            IExpr[] yv = new IExpr[n];
            int[] dim = list.isMatrix();
            if (dim != null && dim[1] == 2) {
                if (dim[1] != 2) {
                    return F.NIL;
                }
                for (int i = 0; i < n; i++) {
                    IAST row = list.getAST(i + 1);
                    xv[i] = row.arg1();
                    yv[i] = row.arg2();
                }
            } else {
                for (int i = 0; i < n; i++) {
                    xv[i] = F.integer(i + 1);
                    yv[i] = list.get(i + 1);
                }
            }
            IExpr[] c = computeDividedDifference(xv, yv);
            IAST polynomial = F.Plus();
            IAST times, plus;
            IAST tempPlus = polynomial;
            // c[0]
            polynomial.append(c[0]);
            for (int i = 2; i < list.size(); i++) {
                times = F.Times();
                plus = F.Plus();
                times.append(plus);
                times.append(F.Subtract(x, xv[i - 2]));
                tempPlus.append(times);
                tempPlus = plus;
                tempPlus.append(c[i - 1]);
            }
            return polynomial;
        }
    }
    return F.NIL;
}
Also used : ISymbol(org.matheclipse.core.interfaces.ISymbol) IAST(org.matheclipse.core.interfaces.IAST) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 95 with ISymbol

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

the class InverseFunction method evaluate.

@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
    Validate.checkSize(ast, 2);
    ISymbol arg1 = Validate.checkSymbolType(ast, 1);
    return getUnaryInverseFunction(arg1);
}
Also used : ISymbol(org.matheclipse.core.interfaces.ISymbol)

Aggregations

ISymbol (org.matheclipse.core.interfaces.ISymbol)117 IExpr (org.matheclipse.core.interfaces.IExpr)79 IAST (org.matheclipse.core.interfaces.IAST)76 IInteger (org.matheclipse.core.interfaces.IInteger)18 WrongArgumentType (org.matheclipse.core.eval.exception.WrongArgumentType)12 INum (org.matheclipse.core.interfaces.INum)10 ISignedNumber (org.matheclipse.core.interfaces.ISignedNumber)10 IFraction (org.matheclipse.core.interfaces.IFraction)9 IBuiltInSymbol (org.matheclipse.core.interfaces.IBuiltInSymbol)8 IComplex (org.matheclipse.core.interfaces.IComplex)7 IComplexNum (org.matheclipse.core.interfaces.IComplexNum)7 ArrayList (java.util.ArrayList)6 IPatternObject (org.matheclipse.core.interfaces.IPatternObject)6 HashSet (java.util.HashSet)5 EvalEngine (org.matheclipse.core.eval.EvalEngine)5 Num (org.matheclipse.core.expression.Num)5 GenPolynomial (edu.jas.poly.GenPolynomial)4 Options (org.matheclipse.core.eval.util.Options)4 ExprPolynomialRing (org.matheclipse.core.polynomials.ExprPolynomialRing)4 ExpVector (edu.jas.poly.ExpVector)3