use of org.matheclipse.core.interfaces.IASTMutable in project symja_android_library by axkr.
the class AbstractFunctionEvaluator method getPowerNegativeExpression.
/**
* Check if the expression is canonical negative.
*
* @param expression
* @param checkTimesPlus check <code>Times(...)</code> and <code>Plus(...)</code> expressions
* @return the negated negative expression or <code>F.NIL</code> if a negative expression couldn't
* be extracted.
*/
public static IExpr getPowerNegativeExpression(final IExpr expression, boolean checkTimesPlus) {
IASTMutable result = F.NIL;
if (expression.isNumber()) {
if (((INumber) expression).complexSign() < 0) {
return ((INumber) expression).negate();
}
return F.NIL;
}
if (expression.isAST()) {
if (checkTimesPlus && expression.isTimes()) {
IAST timesAST = ((IAST) expression);
IExpr arg1 = timesAST.arg1();
if (arg1.isNumber()) {
if (((INumber) arg1).complexSign() < 0) {
IExpr negNum = ((INumber) arg1).negate();
if (negNum.isOne()) {
return timesAST.rest().oneIdentity1();
}
return timesAST.setAtCopy(1, negNum);
}
} else if (arg1.isNegativeInfinity()) {
return timesAST.setAtCopy(1, F.CInfinity);
} else if (arg1.isNegative()) {
IExpr negNum = arg1.negate();
return timesAST.setAtCopy(1, negNum);
}
} else if (checkTimesPlus && expression.isPlus()) {
IAST plusAST = ((IAST) expression);
IExpr arg1 = plusAST.arg1();
if (arg1.isNumber()) {
if (((INumber) arg1).complexSign() < 0) {
result = plusAST.copy();
result.set(1, arg1.negate());
for (int i = 2; i < plusAST.size(); i++) {
result.set(i, plusAST.get(i).negate());
}
return result;
}
} else if (arg1.isNegativeInfinity()) {
result = plusAST.copy();
result.set(1, F.CInfinity);
for (int i = 2; i < plusAST.size(); i++) {
result.set(i, plusAST.get(i).negate());
}
return result;
} else if (arg1.isTimes()) {
IExpr arg1Negated = getPowerNegativeExpression(arg1, checkTimesPlus);
if (arg1Negated.isPresent()) {
int positiveElementsCounter = 0;
result = plusAST.copy();
result.set(1, arg1Negated);
for (int i = 2; i < plusAST.size(); i++) {
IExpr temp = plusAST.get(i);
if (!temp.isTimes() && !temp.isPower()) {
return F.NIL;
}
arg1Negated = getPowerNegativeExpression(temp, checkTimesPlus);
if (arg1Negated.isPresent()) {
result.set(i, arg1Negated);
} else {
positiveElementsCounter++;
if (positiveElementsCounter * 2 > plusAST.argSize()) {
// than number of negative elements
return F.NIL;
}
result.set(i, temp.negate());
}
}
return result;
}
}
// } else if (expression.isNegativeInfinity()) {
// return F.CInfinity;
} else if (expression.isDirectedInfinity() && expression.isAST1()) {
IExpr arg1 = expression.first();
if (arg1.isMinusOne()) {
return F.CInfinity;
}
if (arg1.isNegativeImaginaryUnit()) {
return F.DirectedInfinity(F.CI);
}
}
}
// }
return F.NIL;
}
use of org.matheclipse.core.interfaces.IASTMutable in project symja_android_library by axkr.
the class AbstractFunctionEvaluator method peelOff.
/**
* Split plusAST into two parts, a "rest" and a multiple of Pi/2. This assumes plusAST to be an
* Plus() expression. The multiple of Pi/2 returned in the second position is always a IRational
* number.
*
* @param plusAST
* @param engine
* @return <code>F.NIL</code> if no multiple is found.
*/
public static IAST peelOff(final IAST plusAST, final EvalEngine engine) {
IRational k = null;
for (int i = 1; i < plusAST.size(); i++) {
IExpr temp = plusAST.get(i);
if (temp.equals(S.Pi)) {
k = F.C1;
break;
}
if (temp.isTimes2()) {
if (temp.first().isRational() && temp.second().equals(S.Pi)) {
k = (IRational) temp.first();
break;
}
}
}
if (k != null) {
IASTMutable result = F.binaryAST2(S.List, plusAST, F.C0);
IExpr m1 = F.Times(k.mod(F.C1D2), S.Pi);
IExpr m2 = S.Subtract.of(engine, F.Times(k, S.Pi), m1);
result.set(1, S.Subtract.of(plusAST, m2));
result.set(2, m2);
return result;
}
return F.NIL;
}
use of org.matheclipse.core.interfaces.IASTMutable in project symja_android_library by axkr.
the class EvalAttributes method simpleEval.
public static IExpr simpleEval(IASTMutable result) {
IASTMutable temp = result;
if (temp.isFlatAST()) {
IASTMutable t = EvalAttributes.flatten(temp);
if (t.isPresent()) {
temp = t;
}
}
if (temp.isOrderlessAST()) {
EvalAttributes.sort(temp);
if (temp.isPlus()) {
if (temp.first().isZero()) {
IAST rest = temp.rest();
rest.isFreeOfPatterns();
return rest.oneIdentity0();
}
} else if (temp.isTimes()) {
if (temp.first().isOne()) {
IAST rest = temp.rest();
rest.isFreeOfPatterns();
return rest.oneIdentity1();
}
// else if (temp.first().isZero()) {
// return temp.first();
// }
}
}
if (temp.isOneIdentityAST1()) {
return temp.first();
}
temp.isFreeOfPatterns();
return temp;
}
use of org.matheclipse.core.interfaces.IASTMutable in project symja_android_library by axkr.
the class EvalAttributes method copySortLess.
/**
* Copy the <code>ast</code> and return the sorted copy using function <code>Less(a, b)</code>.
*
* @param ast
* @return return the sorted copy
*/
public static final IAST copySortLess(final IAST ast) {
final IASTMutable sortedList = ast.copy();
sortLess(sortedList);
return sortedList;
}
use of org.matheclipse.core.interfaces.IASTMutable in project symja_android_library by axkr.
the class CreateTensor method createTensorMutable.
private IASTMutable createTensorMutable(IExpr head, int[] dims) {
IASTMutable list = F.ast(head, dims[0]);
int[] index = new int[dims.length];
for (int i = 0; i < index.length; i++) {
index[i] = 1;
}
createTensorRecursive(list, dims, 0, index);
return list;
}
Aggregations