Search in sources :

Example 81 with IASTMutable

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

the class D method dPiecewise.

private static IExpr dPiecewise(int[] dim, final IAST piecewiseFunction, final IAST ast, EvalEngine engine) {
    IAST list = (IAST) piecewiseFunction.arg1();
    if (list.size() > 1) {
        IASTAppendable pwResult = F.ListAlloc(list.size());
        for (int i = 1; i < list.size(); i++) {
            IASTMutable piecewiseD = ast.copy();
            piecewiseD.set(1, list.get(i).first());
            pwResult.append(F.list(piecewiseD, list.get(i).second()));
        }
        if (piecewiseFunction.size() > 2) {
            IASTMutable piecewiseD = ast.copy();
            piecewiseD.set(1, piecewiseFunction.arg2());
            pwResult.append(F.list(engine.evaluate(piecewiseD), S.True));
        }
        IASTMutable piecewise = piecewiseFunction.copy();
        piecewise.set(1, pwResult);
        if (piecewise.size() > 2) {
            piecewise.set(2, S.Indeterminate);
        }
        return piecewise;
    }
    return F.NIL;
}
Also used : IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) IAST(org.matheclipse.core.interfaces.IAST) IASTMutable(org.matheclipse.core.interfaces.IASTMutable)

Example 82 with IASTMutable

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

the class RootsFunctions method roots.

public static IAST roots(final IExpr arg1, IAST variables, EvalEngine engine) {
    if (variables.size() != 2) {
        // factor only possible for univariate polynomials
        LOGGER.log(engine.getLogLevel(), "NRoots: factorization only possible for univariate polynomials");
        return F.NIL;
    }
    IExpr expr = evalExpandAll(arg1, engine);
    IExpr variable = variables.arg1();
    double[] coefficients = Expr2Object.toPolynomial(expr, variable);
    if (coefficients != null) {
        try {
            IASTMutable list;
            if (coefficients.length <= 4) {
                IASTAppendable p = F.PlusAlloc(coefficients.length);
                for (int i = 0; i < coefficients.length; i++) {
                    p.append(F.Times(F.num(coefficients[i]), F.Power(variable, i)));
                }
                expr = engine.evaluate(p);
                list = QuarticSolver.solve(p, variables.arg1());
                for (int i = 1; i < list.size(); i++) {
                    expr = engine.evaluate(list.get(i));
                    if (expr.isInexactNumber()) {
                        list.set(i, F.chopNumber((INumber) expr, Config.DEFAULT_ROOTS_CHOP_DELTA));
                    }
                }
            } else {
                LaguerreSolver solver = new LaguerreSolver(Config.DEFAULT_ROOTS_CHOP_DELTA);
                // see https://github.com/Hipparchus-Math/hipparchus/issues/177 for initial value
                org.hipparchus.complex.Complex[] roots = solver.solveAllComplex(coefficients, 1.0);
                list = Object2Expr.convertComplex(true, roots);
            }
            EvalAttributes.sort(list);
            return list;
        } catch (org.hipparchus.exception.MathRuntimeException mrex) {
            LOGGER.debug("RootsFunctions.roots() failed", mrex);
            return F.NIL;
        }
    }
    IExpr denom = F.C1;
    if (expr.isAST()) {
        expr = Algebra.together((IAST) expr, engine);
        // 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 : LaguerreSolver(org.hipparchus.analysis.solvers.LaguerreSolver) IASTMutable(org.matheclipse.core.interfaces.IASTMutable) Complex(edu.jas.poly.Complex) IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) INumber(org.matheclipse.core.interfaces.INumber) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST)

Example 83 with IASTMutable

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

the class RootsFunctions method rootsOfVariable.

/**
 * @param expr
 * @param denominator
 * @param variables
 * @param numericSolutions
 * @param engine
 * @return <code>F.NIL</code> if no evaluation was possible.
 */
public static IAST rootsOfVariable(final IExpr expr, final IExpr denominator, final IAST variables, boolean numericSolutions, EvalEngine engine) {
    IASTMutable result = F.NIL;
    // ASTRange r = new ASTRange(variables, 1);
    // List<IExpr> varList = r;
    List<IExpr> varList = variables.copyTo();
    try {
        IExpr temp;
        IAST list = rootsOfQuadraticExprPolynomial(expr, variables);
        if (list.isPresent()) {
            return list;
        }
        JASConvert<BigRational> jas = new JASConvert<BigRational>(varList, BigRational.ZERO);
        GenPolynomial<BigRational> polyRat = jas.expr2JAS(expr, numericSolutions);
        // if (polyRat.degree(0) <= 2) {
        result = rootsOfExprPolynomial(expr, variables, false);
        if (result.isPresent()) {
            return result;
        }
        // }
        IASTAppendable newResult = F.ListAlloc(8);
        IAST factorRational = Algebra.factorRational(polyRat, jas, S.List);
        for (int i = 1; i < factorRational.size(); i++) {
            temp = F.evalExpand(factorRational.get(i));
            IAST quarticResultList = QuarticSolver.solve(temp, variables.arg1());
            if (quarticResultList.isPresent()) {
                for (int j = 1; j < quarticResultList.size(); j++) {
                    if (numericSolutions) {
                        newResult.append(F.chopExpr(engine.evalN(quarticResultList.get(j)), Config.DEFAULT_ROOTS_CHOP_DELTA));
                    } else {
                        newResult.append(quarticResultList.get(j));
                    }
                }
            } else {
                polyRat = jas.expr2JAS(temp, numericSolutions);
                IAST factorComplex = Algebra.factorRational(polyRat, jas, S.List);
                for (int k = 1; k < factorComplex.size(); k++) {
                    temp = F.evalExpand(factorComplex.get(k));
                    quarticResultList = QuarticSolver.solve(temp, variables.arg1());
                    if (quarticResultList.isPresent()) {
                        for (int j = 1; j < quarticResultList.size(); j++) {
                            if (numericSolutions) {
                                newResult.append(F.chopExpr(engine.evalN(quarticResultList.get(j)), Config.DEFAULT_ROOTS_CHOP_DELTA));
                            } else {
                                newResult.append(quarticResultList.get(j));
                            }
                        }
                    } else {
                        double[] coefficients = coefficients(temp, (ISymbol) variables.arg1());
                        if (coefficients == null) {
                            return F.NIL;
                        }
                        IAST resultList = findRoots(coefficients);
                        // true);
                        if (resultList.size() > 0) {
                            newResult.appendArgs(resultList);
                        }
                    }
                }
            }
        }
        newResult = QuarticSolver.createSet(newResult);
        return newResult;
    } catch (RuntimeException rex) {
        // JAS or "findRoots" may throw RuntimeExceptions
        result = rootsOfExprPolynomial(expr, variables, true);
    }
    if (result.isPresent()) {
        if (!denominator.isNumber()) {
            // eliminate roots from the result list, which occur in the
            // denominator
            int i = 1;
            IASTAppendable appendable = F.NIL;
            while (i < result.size()) {
                IExpr temp = denominator.replaceAll(F.Rule(variables.arg1(), result.get(i)));
                if (temp.isPresent() && engine.evaluate(temp).isZero()) {
                    if (!appendable.isPresent()) {
                        appendable = result.removeAtClone(i);
                        continue;
                    }
                    appendable.remove(i);
                    continue;
                }
                i++;
            }
        }
        return result;
    }
    return F.NIL;
}
Also used : IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) BigRational(edu.jas.arith.BigRational) JASConvert(org.matheclipse.core.convert.JASConvert) IASTMutable(org.matheclipse.core.interfaces.IASTMutable) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST)

Example 84 with IASTMutable

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

the class RootsFunctions method rootsOfQuadraticExprPolynomial.

/**
 * Solve a polynomial with degree &lt;= 2.
 *
 * @param expr
 * @param varList
 * @return <code>F.NIL</code> if no evaluation was possible.
 */
private static IAST rootsOfQuadraticExprPolynomial(final IExpr expr, IAST varList) {
    IASTMutable result = F.NIL;
    try {
        // try to generate a common expression polynomial
        ExprPolynomialRing ring = new ExprPolynomialRing(ExprRingFactory.CONST, varList);
        ExprPolynomial ePoly = ring.create(expr, false, false, false);
        ePoly = ePoly.multiplyByMinimumNegativeExponents();
        result = rootsOfQuadraticPolynomial(ePoly);
        if (result.isPresent() && expr.isNumericMode()) {
            for (int i = 1; i < result.size(); i++) {
                result.set(i, F.chopExpr(result.get(i), Config.DEFAULT_ROOTS_CHOP_DELTA));
            }
        }
        result = QuarticSolver.sortASTArguments(result);
        return result;
    } catch (JASConversionException e2) {
        LOGGER.debug("RootsFunctions.rootsOfQuadraticExprPolynomial() failed", e2);
    }
    return result;
}
Also used : ExprPolynomialRing(org.matheclipse.core.polynomials.longexponent.ExprPolynomialRing) IASTMutable(org.matheclipse.core.interfaces.IASTMutable) JASConversionException(org.matheclipse.core.eval.exception.JASConversionException) ExprPolynomial(org.matheclipse.core.polynomials.longexponent.ExprPolynomial)

Example 85 with IASTMutable

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

the class Solve method solveInequations.

protected static IASTMutable solveInequations(IASTMutable subSolutionList, IAST inequationsList, int maximumNumberOfResults, EvalEngine engine) {
    if (inequationsList.isEmpty()) {
        return QuarticSolver.sortASTArguments(subSolutionList);
    }
    if (subSolutionList.isListOfLists()) {
        IASTAppendable result = F.ListAlloc(subSolutionList.size());
        for (int i = 1; i < subSolutionList.size(); i++) {
            IASTMutable list = (IASTMutable) subSolutionList.get(i);
            IExpr temp = F.subst(inequationsList, list);
            boolean[] isNumeric = new boolean[] { false };
            temp = engine.evalQuiet(temp);
            if (temp.isAST()) {
                IASTMutable[] lists = SolveUtils.filterSolveLists((IASTMutable) temp, list, isNumeric);
                if (lists[2].isPresent()) {
                    if (!lists[2].isEmptyList()) {
                        result.append(lists[2]);
                    }
                }
            }
        }
        return result;
    }
    return F.NIL;
}
Also used : IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) IASTMutable(org.matheclipse.core.interfaces.IASTMutable) IExpr(org.matheclipse.core.interfaces.IExpr)

Aggregations

IASTMutable (org.matheclipse.core.interfaces.IASTMutable)92 IExpr (org.matheclipse.core.interfaces.IExpr)60 IAST (org.matheclipse.core.interfaces.IAST)34 IASTAppendable (org.matheclipse.core.interfaces.IASTAppendable)26 ISymbol (org.matheclipse.core.interfaces.ISymbol)12 IInteger (org.matheclipse.core.interfaces.IInteger)5 Map (java.util.Map)4 IComplex (org.matheclipse.core.interfaces.IComplex)4 IRational (org.matheclipse.core.interfaces.IRational)4 ArrayList (java.util.ArrayList)3 TreeMap (java.util.TreeMap)3 EvalEngine (org.matheclipse.core.eval.EvalEngine)3 JASConversionException (org.matheclipse.core.eval.exception.JASConversionException)3 ValidateException (org.matheclipse.core.eval.exception.ValidateException)3 INumber (org.matheclipse.core.interfaces.INumber)3 IPatternObject (org.matheclipse.core.interfaces.IPatternObject)3 ISparseArray (org.matheclipse.core.interfaces.ISparseArray)3 ASTNode (org.matheclipse.parser.client.ast.ASTNode)3 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)2 AST2Expr (org.matheclipse.core.convert.AST2Expr)2