Search in sources :

Example 26 with IASTAppendable

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

the class Validate method checkEquationAndInequation.

private static void checkEquationAndInequation(IExpr eq, IASTAppendable termsEqualZeroList) {
    if (eq.isEqual()) {
        IAST equal = (IAST) eq;
        IExpr subtract = EvalEngine.get().evaluate(F.Subtract(equal.arg1(), equal.arg2()));
        if (subtract.isList()) {
            IAST list = (IAST) subtract;
            IASTAppendable result = F.ListAlloc(list.size());
            for (int i = 1; i < list.size(); i++) {
                IExpr arg = list.get(i);
                termsEqualZeroList.append(F.Equal(arg.isTimes() ? arg : F.evalExpandAll(arg), F.C0));
            }
            return;
        }
        termsEqualZeroList.append(F.Equal(subtract.isTimes() ? subtract : F.evalExpandAll(subtract), F.C0));
        return;
    }
    if (eq.isAST2()) {
        IAST equal = (IAST) eq;
        IExpr head = equal.head();
        if (head.equals(S.Equal) || head.equals(S.Unequal) || head.equals(S.Greater) || head.equals(S.GreaterEqual) || head.equals(S.Less) || head.equals(S.LessEqual)) {
            final IExpr[] arr = new IExpr[] { F.expandAll(equal.arg1(), true, true), F.expandAll(equal.arg2(), true, true) };
            termsEqualZeroList.append(F.ast(arr, head));
            return;
        }
    } else if (eq.isTrue()) {
        termsEqualZeroList.append(S.True);
        return;
    } else if (eq.isFalse()) {
        termsEqualZeroList.append(S.False);
        return;
    }
    // not an equation or inequation
    throw new ArgumentTypeException("binary equation or inequation expression expected instead of " + eq.toString());
}
Also used : IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) IAST(org.matheclipse.core.interfaces.IAST) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 27 with IASTAppendable

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

the class Validate method checkEquationsAndInequations.

/**
 * Check if the argument at the given <code>ast</code> position is an equation or inequation (i.e.
 * <code>Equal(a,b)</code>) or a list of equations or inequations or a boolean <code>And()</code>
 * expression of equations and return a list of expanded expressions, which should be equal to
 * <code>0</code>.
 *
 * @param ast
 * @param position the position of the equations argument in the <code>ast</code> expression.
 * @return
 */
public static IASTAppendable checkEquationsAndInequations(final IAST ast, int position) {
    IExpr expr = ast.get(position);
    IAST eqns = null;
    IASTAppendable termsEqualZeroList;
    if (expr.isList() || expr.isAnd()) {
        // a list of equations or inequations or a boolean AND expression of equations
        eqns = (IAST) expr;
        termsEqualZeroList = F.ListAlloc(eqns.size());
        for (int i = 1; i < eqns.size(); i++) {
            IExpr arg = eqns.get(i);
            if (arg.isAST2()) {
                IAST eq = (IAST) arg;
                checkEquationAndInequation(eq, termsEqualZeroList);
            } else {
                // not an equation or inequation
                throw new ArgumentTypeException("binary equation or inequation expression expected at position " + i);
            }
        }
    } else {
        termsEqualZeroList = F.ListAlloc();
        checkEquationAndInequation(expr, termsEqualZeroList);
    }
    return termsEqualZeroList;
}
Also used : IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST)

Example 28 with IASTAppendable

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

the class AbstractCoreFunctionOptionEvaluator method setOptions.

protected void setOptions(final ISymbol symbol, IBuiltInSymbol[] lhsOptionSymbol, IExpr[] rhsValue) {
    optionSymbols = lhsOptionSymbol;
    IASTAppendable list = F.ListAlloc(rhsValue.length);
    for (int i = 0; i < rhsValue.length; i++) {
        list.append(F.Rule(lhsOptionSymbol[i], rhsValue[i]));
    }
    super.setOptions(symbol, list);
}
Also used : IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable)

Example 29 with IASTAppendable

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

the class Lambda method testMap2.

private static IExpr testMap2(IAST list, Predicate<IExpr> predicate, Function<IExpr, IExpr> function1, Function<IExpr, IExpr> function2) {
    IASTAppendable result = F.NIL;
    int size = list.size();
    for (int i = 1; i < size; i++) {
        IExpr temp = list.get(i);
        if (predicate.test(temp)) {
            if (!result.isPresent()) {
                result = list.copyAppendable();
                for (int j = 0; j < i; j++) {
                    result.set(j, function2.apply(temp));
                }
            }
            result.set(i, function1.apply(temp));
            continue;
        }
        if (result != null) {
            result.set(i, function2.apply(temp));
        }
    }
    return result;
}
Also used : IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 30 with IASTAppendable

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

the class Algebra method fractionalPartsTimesPower.

/**
 * Return the numerator and denominator for the given <code>Times[...]</code> or <code>Power[a, b]
 * </code> AST, by separating positive and negative powers.
 *
 * @param timesPower a Times[] or Power[] expression (a*b*c....) or a^b
 * @param splitNumeratorOne split a fractional number into numerator and denominator, only if the
 *        numerator is 1, if <code>true</code>, ignore <code>splitFractionalNumbers</code>
 *        parameter.
 * @param splitFractionalNumbers split a fractional number into numerator and denominator
 * @param trig try to find a trigonometric numerator/denominator form (Example: <code>Csc[x]
 *     </code> gives <code>1 / Sin[x]</code>)
 * @param evalParts evaluate the determined numerator and denominator parts
 * @param negateNumerDenom negate numerator and denominator, if they are both negative
 * @param splitPowerPlusExponents split <code>Power()</code> expressions with <code>Plus()</code>
 *        exponents like <code>a^(-x+y)</code> into numerator <code>a^y</code> and denominator
 *        <code>
 *     a^x</code>
 * @return the numerator and denominator expression and an optional fractional number (maybe
 *         <code>null</code>), if splitNumeratorOne is <code>true</code>.
 */
public static IExpr[] fractionalPartsTimesPower(final IAST timesPower, boolean splitNumeratorOne, boolean splitFractionalNumbers, boolean trig, boolean evalParts, boolean negateNumerDenom, boolean splitPowerPlusExponents) {
    if (timesPower.isPower()) {
        IExpr[] parts = Apart.fractionalPartsPower(timesPower, trig, splitPowerPlusExponents);
        if (parts != null) {
            return parts;
        }
        return null;
    }
    IAST timesAST = timesPower;
    IExpr[] result = new IExpr[3];
    result[2] = null;
    IASTAppendable numerator = F.TimesAlloc(timesAST.size());
    IASTAppendable denominator = F.TimesAlloc(timesAST.size());
    // IExpr arg;
    IAST argAST;
    boolean evaled = false;
    boolean splitFractionEvaled = false;
    for (int i = 1; i < timesAST.size(); i++) {
        final IExpr arg = timesAST.get(i);
        if (arg.isAST()) {
            argAST = (IAST) arg;
            if (trig && argAST.isAST1()) {
                IExpr numerForm = Numerator.getTrigForm(argAST, trig);
                if (numerForm.isPresent()) {
                    IExpr denomForm = Denominator.getTrigForm(argAST, trig);
                    if (denomForm.isPresent()) {
                        if (!numerForm.isOne()) {
                            numerator.append(numerForm);
                        }
                        if (!denomForm.isOne()) {
                            denominator.append(denomForm);
                        }
                        evaled = true;
                        continue;
                    }
                }
            } else if (arg.isPower()) {
                IExpr[] parts = Apart.fractionalPartsPower((IAST) arg, trig, splitPowerPlusExponents);
                if (parts != null) {
                    if (!parts[0].isOne()) {
                        numerator.append(parts[0]);
                    }
                    if (!parts[1].isOne()) {
                        denominator.append(parts[1]);
                    }
                    evaled = true;
                    continue;
                }
            }
        } else if (i == 1 && arg.isFraction()) {
            if (splitNumeratorOne) {
                IFraction fr = (IFraction) arg;
                if (fr.numerator().isOne()) {
                    denominator.append(fr.denominator());
                    splitFractionEvaled = true;
                    continue;
                }
                if (fr.numerator().isMinusOne()) {
                    numerator.append(fr.numerator());
                    denominator.append(fr.denominator());
                    splitFractionEvaled = true;
                    continue;
                }
                result[2] = fr;
                continue;
            } else if (splitFractionalNumbers) {
                IFraction fr = (IFraction) arg;
                if (!fr.numerator().isOne()) {
                    numerator.append(fr.numerator());
                }
                denominator.append(fr.denominator());
                evaled = true;
                continue;
            }
        }
        numerator.append(arg);
    }
    if (evaled) {
        if (evalParts) {
            result[0] = F.eval(numerator);
            result[1] = F.eval(denominator);
        } else {
            result[0] = numerator.oneIdentity1();
            result[1] = denominator.oneIdentity1();
        }
        if (negateNumerDenom && result[0].isNegative() && result[1].isPlus() && ((IAST) result[1]).isAST2()) {
            // negate numerator and denominator:
            result[0] = result[0].negate();
            result[1] = result[1].negate();
        }
        return result;
    }
    if (splitFractionEvaled) {
        result[0] = numerator.oneIdentity1();
        if (!result[0].isTimes() && !result[0].isPlus()) {
            result[1] = denominator.oneIdentity1();
            return result;
        }
        if (result[0].isTimes() && ((IAST) result[0]).isAST2() && ((IAST) result[0]).arg1().isMinusOne()) {
            result[1] = denominator.oneIdentity1();
            return result;
        }
    }
    return null;
}
Also used : IFraction(org.matheclipse.core.interfaces.IFraction) IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) JASIExpr(org.matheclipse.core.convert.JASIExpr) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST)

Aggregations

IASTAppendable (org.matheclipse.core.interfaces.IASTAppendable)363 IExpr (org.matheclipse.core.interfaces.IExpr)219 IAST (org.matheclipse.core.interfaces.IAST)130 ISymbol (org.matheclipse.core.interfaces.ISymbol)36 IASTMutable (org.matheclipse.core.interfaces.IASTMutable)30 IInteger (org.matheclipse.core.interfaces.IInteger)29 Map (java.util.Map)28 EvalEngine (org.matheclipse.core.eval.EvalEngine)20 PrettyPrint (edu.jas.kern.PrettyPrint)13 SortedMap (java.util.SortedMap)13 ArrayList (java.util.ArrayList)12 F (org.matheclipse.core.expression.F)12 BigRational (edu.jas.arith.BigRational)10 LogManager (org.apache.logging.log4j.LogManager)10 Logger (org.apache.logging.log4j.Logger)10 ExpVector (edu.jas.poly.ExpVector)9 HashMap (java.util.HashMap)9 IBuiltInSymbol (org.matheclipse.core.interfaces.IBuiltInSymbol)8 IStringX (org.matheclipse.core.interfaces.IStringX)8 ASTNode (org.matheclipse.parser.client.ast.ASTNode)8