Search in sources :

Example 11 with LimitException

use of org.matheclipse.core.eval.exception.LimitException in project symja_android_library by axkr.

the class PolynomialFunctions method coefficientList.

public static IAST coefficientList(IExpr expr, IAST listOfVariables) {
    try {
        ExprPolynomialRing ring = new ExprPolynomialRing(listOfVariables);
        ExprPolynomial poly = ring.create(expr, true, false, true);
        if (poly.isZero()) {
            return F.CEmptyList;
        }
        return poly.coefficientList();
    } catch (LimitException le) {
        throw le;
    } catch (RuntimeException ex) {
        // org.matheclipse.core.polynomials.longexponent.ExprPolynomialRing.create()
        LOGGER.debug("PolynomialFunctions.coefficientList() failed", ex);
    }
    if (listOfVariables.argSize() > 0) {
        return F.Nest(S.List, expr, listOfVariables.argSize());
    }
    return F.NIL;
}
Also used : ExprPolynomialRing(org.matheclipse.core.polynomials.longexponent.ExprPolynomialRing) LimitException(org.matheclipse.core.eval.exception.LimitException) ExprPolynomial(org.matheclipse.core.polynomials.longexponent.ExprPolynomial)

Example 12 with LimitException

use of org.matheclipse.core.eval.exception.LimitException in project symja_android_library by axkr.

the class Solve method of.

/**
 * @param ast the <code>Solve(...)</code> ast
 * @param numeric if true, try to find a numerically solution
 * @param engine
 * @return
 */
public static IExpr of(final IAST ast, boolean numeric, EvalEngine engine) {
    boolean[] isNumeric = new boolean[] { numeric };
    try {
        if (ast.arg1().isEmptyList()) {
            return F.list(F.CEmptyList);
        }
        IAST userDefinedVariables = Validate.checkIsVariableOrVariableList(ast, 2, ast.topHead(), engine);
        if (userDefinedVariables.isPresent()) {
            IAST equationVariables = VariablesSet.getVariables(ast.arg1());
            if (userDefinedVariables.isEmpty()) {
                userDefinedVariables = equationVariables;
            }
            ISymbol domain = S.Complexes;
            if (ast.isAST3()) {
                if (!ast.arg3().isSymbol()) {
                    LOGGER.log(engine.getLogLevel(), "{}: domain definition expected at position 3 instead of {}", ast.topHead(), ast.arg3());
                    return F.NIL;
                }
                domain = (ISymbol) ast.arg3();
                if (domain.equals(S.Booleans)) {
                    return BooleanFunctions.solveInstances(ast.arg1(), userDefinedVariables, Integer.MAX_VALUE);
                }
                if (domain.equals(S.Integers)) {
                    return solveIntegers(ast, equationVariables, userDefinedVariables, Integer.MAX_VALUE, engine);
                }
                if (!domain.equals(S.Reals) && !domain.equals(S.Complexes)) {
                    Level level = engine.getLogLevel();
                    LOGGER.log(level, "{}: domain definition expected at position 3 instead of {}", ast.topHead(), domain);
                    return F.NIL;
                }
            }
            IAssumptions oldAssumptions = engine.getAssumptions();
            try {
                IAssumptions assum = setVariablesReals(userDefinedVariables, domain);
                if (assum != null) {
                    engine.setAssumptions(assum);
                }
                IAST termsList = Validate.checkEquationsAndInequations(ast, 1);
                IASTMutable[] lists = SolveUtils.filterSolveLists(termsList, F.NIL, isNumeric);
                boolean numericFlag = isNumeric[0] || numeric;
                if (lists[2].isPresent()) {
                    IExpr result = solveNumeric(lists[2], numericFlag, engine);
                    if (!result.isPresent()) {
                        return IOFunctions.printMessage(ast.topHead(), "nsmet", F.list(ast.topHead()), engine);
                    }
                    return checkDomain(result, domain);
                }
                IASTMutable termsEqualZeroList = lists[0];
                IExpr result = solveRecursive(termsEqualZeroList, lists[1], numericFlag, userDefinedVariables, engine);
                if (!result.isPresent()) {
                    return IOFunctions.printMessage(ast.topHead(), "nsmet", F.list(ast.topHead()), engine);
                }
                return checkDomain(result, domain);
            } finally {
                engine.setAssumptions(oldAssumptions);
            }
        }
    } catch (ValidateException ve) {
        return IOFunctions.printMessage(S.Solve, ve, engine);
    } catch (LimitException e) {
        LOGGER.log(engine.getLogLevel(), S.Solve, e);
    } catch (RuntimeException rex) {
        LOGGER.debug("Solve.of() failed() failed", rex);
    }
    return F.NIL;
}
Also used : ValidateException(org.matheclipse.core.eval.exception.ValidateException) ISymbol(org.matheclipse.core.interfaces.ISymbol) IAssumptions(org.matheclipse.core.eval.util.IAssumptions) Level(org.apache.logging.log4j.Level) IAST(org.matheclipse.core.interfaces.IAST) IASTMutable(org.matheclipse.core.interfaces.IASTMutable) IExpr(org.matheclipse.core.interfaces.IExpr) LimitException(org.matheclipse.core.eval.exception.LimitException)

Example 13 with LimitException

use of org.matheclipse.core.eval.exception.LimitException in project symja_android_library by axkr.

the class Solve method solveTimesEquationsRecursively.

/**
 * Analyze the <code>termsEqualZeroList</code> if it contains a <code>Times[..., ,...]</code>
 * expression. If true, set the factors equal to <code>0</code> and solve the equations
 * recursively.
 *
 * @param termsEqualZeroList the list of expressions, which should equal <code>0</code>
 * @param numericFlag
 * @param variables the variables for which the equations should be solved
 * @param engine the evaluation engine
 * @return
 */
private static IASTMutable solveTimesEquationsRecursively(IASTMutable termsEqualZeroList, IAST inequationsList, boolean numericFlag, IAST variables, boolean multipleValues, EvalEngine engine) {
    try {
        IASTMutable resultList = solveEquations(termsEqualZeroList, inequationsList, variables, 0, engine);
        if (resultList.isPresent() && !resultList.isEmpty()) {
            return resultList;
        }
        Set<IExpr> subSolutionSet = new TreeSet<IExpr>();
        for (int i = 1; i < termsEqualZeroList.size(); i++) {
            IExpr termEQZero = termsEqualZeroList.get(i);
            if (termEQZero.isTimes()) {
                solveTimesAST((IAST) termEQZero, termsEqualZeroList, inequationsList, numericFlag, variables, multipleValues, engine, subSolutionSet, i);
            } else {
                if (termEQZero.isAST()) {
                    // try factoring
                    termEQZero = S.Factor.of(engine, termEQZero);
                    if (termEQZero.isTimes()) {
                        solveTimesAST((IAST) termEQZero, termsEqualZeroList, inequationsList, numericFlag, variables, multipleValues, engine, subSolutionSet, i);
                    }
                }
            }
        }
        if (subSolutionSet.size() > 0) {
            return F.ListAlloc(subSolutionSet);
        }
        return resultList;
    } catch (LimitException le) {
        LOGGER.debug("Solve.solveTimesEquationsRecursively() failed", le);
        throw le;
    } catch (RuntimeException rex) {
        LOGGER.debug("Solve.solveTimesEquationsRecursively() failed", rex);
        if (Config.SHOW_STACKTRACE) {
            rex.printStackTrace();
        }
    }
    return F.NIL;
}
Also used : TreeSet(java.util.TreeSet) IASTMutable(org.matheclipse.core.interfaces.IASTMutable) IExpr(org.matheclipse.core.interfaces.IExpr) LimitException(org.matheclipse.core.eval.exception.LimitException)

Aggregations

LimitException (org.matheclipse.core.eval.exception.LimitException)13 IExpr (org.matheclipse.core.interfaces.IExpr)10 IAST (org.matheclipse.core.interfaces.IAST)7 ISymbol (org.matheclipse.core.interfaces.ISymbol)5 IInteger (org.matheclipse.core.interfaces.IInteger)4 EvalEngine (org.matheclipse.core.eval.EvalEngine)3 Num (org.matheclipse.core.expression.Num)3 INum (org.matheclipse.core.interfaces.INum)3 ISignedNumber (org.matheclipse.core.interfaces.ISignedNumber)3 MathRuntimeException (org.hipparchus.exception.MathRuntimeException)2 RealMatrix (org.hipparchus.linear.RealMatrix)2 ArgumentTypeException (org.matheclipse.core.eval.exception.ArgumentTypeException)2 ValidateException (org.matheclipse.core.eval.exception.ValidateException)2 ApcomplexNum (org.matheclipse.core.expression.ApcomplexNum)2 ApfloatNum (org.matheclipse.core.expression.ApfloatNum)2 IASTAppendable (org.matheclipse.core.interfaces.IASTAppendable)2 IASTMutable (org.matheclipse.core.interfaces.IASTMutable)2 IRational (org.matheclipse.core.interfaces.IRational)2 Entry (java.util.Map.Entry)1 TreeSet (java.util.TreeSet)1