Search in sources :

Example 26 with ISymbol

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

the class Plot method evaluate.

public IExpr evaluate(final IAST ast, EvalEngine engine) {
    if ((ast.size() >= 3) && (ast.size() <= 4) && ast.arg2().isList()) {
        try {
            final IAST rangeList = (IAST) ast.arg2();
            if (rangeList.isAST3()) {
                final ISymbol x = (ISymbol) rangeList.arg1();
                final IExpr xMin = engine.evalN(rangeList.arg2());
                final IExpr xMax = engine.evalN(rangeList.arg3());
                if ((!(xMin instanceof INum)) || (!(xMax instanceof INum))) {
                    return F.NIL;
                }
                final double xMinD = ((INum) xMin).getRealPart();
                final double xMaxd = ((INum) xMax).getRealPart();
                if (xMaxd <= xMinD) {
                    return F.NIL;
                }
                double yMinD = 0.0f;
                double yMaxD = 0.0f;
                if ((ast.isAST3()) && ast.get(3).isList()) {
                    final IAST lsty = (IAST) ast.arg3();
                    if (lsty.isAST2()) {
                        final IExpr y0 = engine.evalN(lsty.arg1());
                        final IExpr y1 = engine.evalN(lsty.arg2());
                        if ((y0 instanceof INum) && (y1 instanceof INum)) {
                            yMinD = ((INum) y0).getRealPart();
                            yMaxD = ((INum) y1).getRealPart();
                        }
                    }
                }
                final IAST graphics = Graphics();
                IAST line = Line();
                IExpr temp;
                if (ast.get(1).isList()) {
                    final IAST list = (IAST) ast.get(1);
                    final IAST primitives = List();
                    for (int i = 1; i < list.size(); i++) {
                        temp = plotLine(xMinD, xMaxd, yMinD, yMaxD, list.get(2), x, engine);
                        if (temp.isPresent()) {
                            line.append(temp);
                            primitives.append(line);
                        }
                        if (i < list.size() - 1) {
                            line = Line();
                        }
                    }
                    graphics.append(primitives);
                } else {
                    temp = plotLine(xMinD, xMaxd, yMinD, yMaxD, ast.get(1), x, engine);
                    if (temp.isPresent()) {
                        line.append(temp);
                        graphics.append(line);
                    }
                }
                final IExpr[] options = { Rule(F.PlotRange, F.Automatic), Rule(F.AxesStyle, F.Automatic), Rule(F.AxesOrigin, List(F.C0, F.C0)), Rule(F.Axes, F.True), Rule(F.Background, F.White) };
                graphics.append(F.ast(options, F.List));
                return Show(graphics);
            }
        } catch (RuntimeException rex) {
            if (Config.SHOW_STACKTRACE) {
                rex.printStackTrace();
            }
        }
    }
    return F.Null;
}
Also used : ISymbol(org.matheclipse.core.interfaces.ISymbol) IAST(org.matheclipse.core.interfaces.IAST) IExpr(org.matheclipse.core.interfaces.IExpr) INum(org.matheclipse.core.interfaces.INum)

Example 27 with ISymbol

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

the class NRoots method roots.

public static IAST roots(final IExpr arg1, IAST variables, EvalEngine engine) {
    if (variables.size() != 2) {
        // factor only possible for univariate polynomials
        engine.printMessage("NRoots: factorization only possible for univariate polynomials");
        return F.NIL;
    }
    IExpr expr = evalExpandAll(arg1);
    ISymbol sym = (ISymbol) variables.arg1();
    double[] coefficients = Expr2Object.toPolynomial(expr, sym);
    if (coefficients != null) {
        LaguerreSolver solver = new LaguerreSolver(Config.DEFAULT_ROOTS_CHOP_DELTA);
        Complex[] roots = solver.solveAllComplex(coefficients, 0);
        return Object2Expr.convertComplex(roots);
    }
    IExpr denom = F.C1;
    if (expr.isAST()) {
        expr = Algebra.together((IAST) expr);
        // split expr into numerator and denominator
        denom = engine.evaluate(F.Denominator(expr));
        if (!denom.isOne()) {
            // search roots for the numerator expression
            expr = engine.evaluate(F.Numerator(expr));
        }
    }
    return rootsOfVariable(expr, denom);
}
Also used : ISymbol(org.matheclipse.core.interfaces.ISymbol) LaguerreSolver(org.hipparchus.analysis.solvers.LaguerreSolver) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST) Complex(org.hipparchus.complex.Complex)

Example 28 with ISymbol

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

the class NSolve method rootsOfUnivariatePolynomial.

/**
	 * Evaluate the roots of a univariate polynomial with the Roots[] function.
	 * 
	 * @param exprAnalyzer
	 * @param fListOfVariables
	 * @return
	 */
private static IAST rootsOfUnivariatePolynomial(ExprAnalyzer exprAnalyzer, EvalEngine engine) {
    IExpr expr = exprAnalyzer.getNumerator();
    IExpr denom = exprAnalyzer.getDenominator();
    // try to solve the expr for a symbol in the symbol set
    for (ISymbol sym : exprAnalyzer.getSymbolSet()) {
        IExpr temp = Roots.rootsOfVariable(expr, denom, F.List(sym), true, engine);
        if (temp.isPresent()) {
            IAST resultList = F.List();
            if (temp.isASTSizeGE(F.List, 2)) {
                IAST rootsList = (IAST) temp;
                for (IExpr root : rootsList) {
                    IAST rule = F.Rule(sym, root);
                    resultList.append(rule);
                }
                return resultList;
            }
            return F.NIL;
        }
    }
    return F.NIL;
}
Also used : ISymbol(org.matheclipse.core.interfaces.ISymbol) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST)

Example 29 with ISymbol

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

the class Resultant method evaluate.

@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
    Validate.checkSize(ast, 4);
    // TODO allow multinomials
    IExpr arg3 = Validate.checkSymbolType(ast, 3);
    ISymbol x = (ISymbol) arg3;
    IExpr a = F.evalExpandAll(ast.arg1());
    IExpr b = F.evalExpandAll(ast.arg2());
    ExprPolynomialRing ring = new ExprPolynomialRing(F.List(x));
    try {
        // check if a is a polynomial otherwise check ArithmeticException,
        // ClassCastException
        ring.create(a);
    } catch (RuntimeException ex) {
        throw new WrongArgumentType(ast, a, 1, "Polynomial expected!");
    }
    try {
        // check if b is a polynomial otherwise check ArithmeticException,
        // ClassCastException
        ring.create(b);
        return F.Together(resultant(a, b, x, engine));
    } catch (RuntimeException ex) {
        throw new WrongArgumentType(ast, b, 2, "Polynomial expected!");
    }
}
Also used : ExprPolynomialRing(org.matheclipse.core.polynomials.ExprPolynomialRing) ISymbol(org.matheclipse.core.interfaces.ISymbol) WrongArgumentType(org.matheclipse.core.eval.exception.WrongArgumentType) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 30 with ISymbol

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

the class Plot3D method evaluate.

public IExpr evaluate(final IAST ast, EvalEngine engine) {
    // ISymbol optionsArray[] = new ISymbol[] { f.BoxRatios, f.PlotRange };
    if ((ast.size() >= 4) && ast.get(2).isList() && ast.get(3).isList()) {
        try {
            final IAST graphics = SurfaceGraphics();
            IExpr temp;
            // x-Range
            final IAST lst1 = (IAST) ast.arg2();
            // y-Range
            final IAST lst2 = (IAST) ast.arg3();
            if (lst1.isAST3() && lst2.isAST3()) {
                // final Options hOptions = new Options(F.Plot3D, ast, 4);
                // IAST allOptions = List();
                // for (int i = 0; i < optionsArray.length; i++) {
                // allOptions.add(optionsArray[i]);
                // }
                // allOptions = hOptions.replaceAll(allOptions);
                final ISymbol x = (ISymbol) lst1.arg1();
                final IExpr xMin = engine.evalN(lst1.arg2());
                final IExpr xMax = engine.evalN(lst1.arg3());
                final ISymbol y = (ISymbol) lst2.arg1();
                final IExpr yMin = engine.evalN(lst2.arg2());
                final IExpr yMax = engine.evalN(lst2.arg3());
                if ((!(xMin instanceof INum)) || (!(xMax instanceof INum)) || (!(yMin instanceof INum)) || (!(yMax instanceof INum))) {
                    return F.NIL;
                }
                final double xMinD = ((INum) xMin).getRealPart();
                final double xMaxD = ((INum) xMax).getRealPart();
                final double yMinD = ((INum) yMin).getRealPart();
                final double yMaxD = ((INum) yMax).getRealPart();
                if (xMaxD <= xMinD) {
                    return F.NIL;
                }
                if (yMaxD <= yMinD) {
                    return F.NIL;
                }
                // double y0d = -10.0f;
                // double y1d = 10.0f;
                // double params[] = {ad, bd, cd, dd, -10.0, 10.0};
                temp = plotArray(xMinD, xMaxD, yMinD, yMaxD, ast.get(1), (ISymbol) lst1.get(1), (ISymbol) lst2.get(1), engine);
                graphics.append(temp);
                final IAST options = List();
                // for (int i = 0; i < optionsArray.length; i++) {
                // options.add(Rule(optionsArray[i], allOptions.get(i)));
                // }
                options.append(Rule(F.PlotRange, F.Automatic));
                options.append(Rule(F.MeshRange, List(List(xMin, xMax), List(yMin, yMax))));
                graphics.append(options);
                return Show(graphics);
            }
        } catch (RuntimeException rex) {
            if (Config.SHOW_STACKTRACE) {
                rex.printStackTrace();
            }
        }
    }
    return F.Null;
}
Also used : ISymbol(org.matheclipse.core.interfaces.ISymbol) IAST(org.matheclipse.core.interfaces.IAST) IExpr(org.matheclipse.core.interfaces.IExpr) INum(org.matheclipse.core.interfaces.INum)

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