Search in sources :

Example 1 with IAssumptions

use of org.matheclipse.core.eval.util.IAssumptions in project symja_android_library by axkr.

the class Refine method evaluate.

@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
    Validate.checkSize(ast, 3);
    final IExpr arg2 = engine.evaluate(ast.arg2());
    IAssumptions assumptions = determineAssumptions(ast.topHead(), arg2, engine);
    if (assumptions != null) {
        return refineAssumptions(ast.arg1(), assumptions, engine);
    }
    return F.NIL;
}
Also used : IAssumptions(org.matheclipse.core.eval.util.IAssumptions) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 2 with IAssumptions

use of org.matheclipse.core.eval.util.IAssumptions in project symja_android_library by axkr.

the class F method symbol.

public static ISymbol symbol(final String symbolName, final String contextStr, IAST assumptionAST, EvalEngine engine) {
    if (contextStr.length() == 0) {
        return symbol(symbolName, assumptionAST, engine);
    }
    ISymbol symbol;
    ContextPath contextPath = engine.getContextPath();
    Context context = contextPath.getContext(contextStr);
    // if (context == null) {
    // contextPath.add(new Context(contextStr));
    // }
    symbol = ContextPath.getSymbol(symbolName, context, engine.isRelaxedSyntax());
    if (assumptionAST != null) {
        IExpr temp = Lambda.replaceSlots(assumptionAST, List(symbol)).orElse(assumptionAST);
        if (temp.isAST()) {
            IAssumptions assumptions = engine.getAssumptions();
            if (assumptions == null) {
                assumptions = org.matheclipse.core.eval.util.Assumptions.getInstance(temp);
                engine.setAssumptions(assumptions);
            } else {
                assumptions.addAssumption(temp);
            }
        }
    }
    return symbol;
}
Also used : ApfloatContext(org.apfloat.ApfloatContext) ISymbol(org.matheclipse.core.interfaces.ISymbol) IAssumptions(org.matheclipse.core.eval.util.IAssumptions) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 3 with IAssumptions

use of org.matheclipse.core.eval.util.IAssumptions in project symja_android_library by axkr.

the class Integrate method evaluate.

@Override
public IExpr evaluate(IAST holdallAST, EvalEngine engine) {
    if (Config.JAS_NO_THREADS) {
        // Android changed: call static initializer in evaluate() method.
        new IntegrateInitializer().run();
    } else {
    // see #setUp() method
    }
    try {
        // wait for initializer run is completed, no matter how many threads call evaluate() method
        await();
    } catch (InterruptedException ignored) {
    }
    IAssumptions oldAssumptions = engine.getAssumptions();
    boolean numericMode = engine.isNumericMode();
    try {
        OptionArgs options = null;
        if (holdallAST.size() > 3) {
            options = new OptionArgs(S.Integrate, holdallAST, holdallAST.size() - 1, engine);
            if (!options.isInvalidPosition()) {
                holdallAST = holdallAST.most();
            }
        }
        IExpr assumptionExpr = OptionArgs.determineAssumptions(holdallAST, -1, options);
        if (assumptionExpr.isPresent() && assumptionExpr.isAST()) {
            IAssumptions assumptions = org.matheclipse.core.eval.util.Assumptions.getInstance(assumptionExpr);
            if (assumptions != null) {
                engine.setAssumptions(assumptions);
            }
        }
        boolean evaled = false;
        IExpr result;
        engine.setNumericMode(false);
        if (holdallAST.size() < 3 || holdallAST.isEvalFlagOn(IAST.BUILT_IN_EVALED)) {
            return F.NIL;
        }
        final IExpr arg1Holdall = holdallAST.arg1();
        final IExpr a1 = NumberTheory.rationalize(arg1Holdall, false).orElse(arg1Holdall);
        IExpr arg1 = engine.evaluateNIL(a1);
        if (arg1.isPresent()) {
            evaled = true;
        } else {
            arg1 = a1;
        }
        if (arg1.isIndeterminate()) {
            return S.Indeterminate;
        }
        if (holdallAST.size() > 3) {
            // Integrate[Integrate[fxy, y], x] ...
            return holdallAST.foldRight((x, y) -> engine.evaluateNIL(F.Integrate(x, y)), arg1, 2);
        }
        IExpr arg2 = engine.evaluateNIL(holdallAST.arg2());
        if (arg2.isPresent()) {
            evaled = true;
        } else {
            arg2 = holdallAST.arg2();
        }
        if (arg2.isList()) {
            IAST xList = (IAST) arg2;
            if (xList.isVector() == 3) {
                // Integrate[f[x], {x,a,b}]
                IAST copy = holdallAST.setAtCopy(2, xList.arg1());
                IExpr temp = engine.evaluate(copy);
                if (temp.isFreeAST(S.Integrate)) {
                    return definiteIntegral(temp, xList, engine);
                }
            }
            return F.NIL;
        }
        if (arg1.isList() && arg2.isSymbol()) {
            return mapIntegrate((IAST) arg1, arg2);
        }
        final IASTAppendable ast = holdallAST.setAtClone(1, arg1);
        ast.set(2, arg2);
        final IExpr x = ast.arg2();
        if (!x.isVariable()) {
            // `1` is not a valid variable.
            return IOFunctions.printMessage(ast.topHead(), "ivar", F.list(x), engine);
        }
        if (arg1.isNumber()) {
            // Integrate[x_?NumberQ,y_Symbol] -> x*y
            return Times(arg1, x);
        }
        if (arg1 instanceof ASTSeriesData) {
            ASTSeriesData series = ((ASTSeriesData) arg1);
            if (series.getX().equals(x)) {
                final IExpr temp = ((ASTSeriesData) arg1).integrate(x);
                if (temp != null) {
                    return temp;
                }
            }
            return F.NIL;
        }
        if (arg1.isFree(x, true)) {
            // Integrate[x_,y_Symbol] -> x*y /; FreeQ[x,y]
            return Times(arg1, x);
        }
        if (arg1.equals(x)) {
            // Integrate[x_,x_Symbol] -> x^2 / 2
            return Times(F.C1D2, Power(arg1, F.C2));
        }
        boolean showSteps = false;
        if (showSteps) {
            LOGGER.info(arg1);
            if (DEBUG_EXPR.contains(arg1)) {
            // System.exit(-1);
            }
            DEBUG_EXPR.add(arg1);
        }
        if (arg1.isAST()) {
            final IAST fx = (IAST) arg1;
            if (fx.topHead().equals(x)) {
                // issue #91
                return F.NIL;
            }
            int[] dim = fx.isPiecewise();
            if (dim != null) {
                return integratePiecewise(dim, fx, ast);
            }
            result = integrateAbs(fx, x);
            if (result.isPresent()) {
                if (result == S.Undefined) {
                    return F.NIL;
                }
                return result;
            }
            result = integrateByRubiRules(fx, x, ast, engine);
            if (result.isPresent()) {
                IExpr temp = result.replaceAll(f -> {
                    if (f.isAST(UtilityFunctionCtors.Unintegrable, 3)) {
                        IAST integrate = F.Integrate(f.first(), f.second());
                        integrate.addEvalFlags(IAST.BUILT_IN_EVALED);
                        return integrate;
                    } else if (f.isAST(F.$rubi("CannotIntegrate"), 3)) {
                        IAST integrate = F.Integrate(f.first(), f.second());
                        integrate.addEvalFlags(IAST.BUILT_IN_EVALED);
                        return integrate;
                    }
                    return F.NIL;
                });
                return temp.orElse(result);
            }
            if (fx.isTimes()) {
                IAST[] temp = fx.filter(arg -> arg.isFree(x));
                IExpr free = temp[0].oneIdentity1();
                if (!free.isOne()) {
                    IExpr rest = temp[1].oneIdentity1();
                    // Integrate[free_ * rest_,x_Symbol] -> free*Integrate[rest, x] /; FreeQ[free,x]
                    return Times(free, Integrate(rest, x));
                }
            }
            if (fx.isPower()) {
                // base ^ exponent
                IExpr base = fx.base();
                IExpr exponent = fx.exponent();
                if (base.equals(x) && exponent.isFree(x)) {
                    if (exponent.isMinusOne()) {
                        // Integrate[ 1 / x_ , x_ ] -> Log[x]
                        return Log(x);
                    }
                    // Integrate[ x_ ^n_ , x_ ] -> x^(n+1)/(n+1) /; FreeQ[n, x]
                    IExpr temp = Plus(F.C1, exponent);
                    return Divide(Power(x, temp), temp);
                }
                if (exponent.equals(x) && base.isFree(x)) {
                    if (base.isE()) {
                        // E^x
                        return fx;
                    }
                    // a^x / Log(a)
                    return F.Divide(fx, F.Log(base));
                }
            }
            result = callRestIntegrate(fx, x, engine);
            if (result.isPresent()) {
                return result;
            }
        }
        return evaled ? ast : F.NIL;
    } finally {
        engine.setAssumptions(oldAssumptions);
        engine.setNumericMode(numericMode);
    }
}
Also used : IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) IAssumptions(org.matheclipse.core.eval.util.IAssumptions) ASTSeriesData(org.matheclipse.core.expression.ASTSeriesData) OptionArgs(org.matheclipse.core.eval.util.OptionArgs) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST)

Example 4 with IAssumptions

use of org.matheclipse.core.eval.util.IAssumptions in project symja_android_library by axkr.

the class Solve method setVariablesReals.

/**
 * If <code>domain</code> is {@link S#Reals} create the {@link F#Element(IExpr, Reals)} assumption
 * for each variable.
 *
 * @param userDefinedVariables
 * @param domain
 * @return <code>null</code> if no assumption was created
 */
private static IAssumptions setVariablesReals(IAST userDefinedVariables, ISymbol domain) {
    IAssumptions assum;
    if (domain.equals(S.Reals)) {
        IASTAppendable list = F.ListAlloc(userDefinedVariables.size());
        for (int i = 1; i < userDefinedVariables.size(); i++) {
            list.append(F.Element(userDefinedVariables.get(i), domain));
        }
        assum = Assumptions.getInstance(list);
        return assum;
    }
    return null;
}
Also used : IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) IAssumptions(org.matheclipse.core.eval.util.IAssumptions)

Example 5 with IAssumptions

use of org.matheclipse.core.eval.util.IAssumptions 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)

Aggregations

IAssumptions (org.matheclipse.core.eval.util.IAssumptions)7 IExpr (org.matheclipse.core.interfaces.IExpr)6 IAST (org.matheclipse.core.interfaces.IAST)3 IASTAppendable (org.matheclipse.core.interfaces.IASTAppendable)3 OptionArgs (org.matheclipse.core.eval.util.OptionArgs)2 ISymbol (org.matheclipse.core.interfaces.ISymbol)2 Level (org.apache.logging.log4j.Level)1 ApfloatContext (org.apfloat.ApfloatContext)1 VariablesSet (org.matheclipse.core.convert.VariablesSet)1 LimitException (org.matheclipse.core.eval.exception.LimitException)1 ValidateException (org.matheclipse.core.eval.exception.ValidateException)1 ASTSeriesData (org.matheclipse.core.expression.ASTSeriesData)1 IASTMutable (org.matheclipse.core.interfaces.IASTMutable)1