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());
}
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;
}
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);
}
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;
}
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;
}
Aggregations