Search in sources :

Example 1 with OrdinaryDifferentialEquation

use of org.hipparchus.ode.OrdinaryDifferentialEquation 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)

Aggregations

AbstractIntegrator (org.hipparchus.ode.AbstractIntegrator)1 OrdinaryDifferentialEquation (org.hipparchus.ode.OrdinaryDifferentialEquation)1 DormandPrince853Integrator (org.hipparchus.ode.nonstiff.DormandPrince853Integrator)1 IAST (org.matheclipse.core.interfaces.IAST)1 IExpr (org.matheclipse.core.interfaces.IExpr)1 ISignedNumber (org.matheclipse.core.interfaces.ISignedNumber)1 ISymbol (org.matheclipse.core.interfaces.ISymbol)1