use of org.matheclipse.core.interfaces.IAST in project symja_android_library by axkr.
the class F method Plus.
public static IAST Plus(final IExpr a0, final IExpr a1) {
if (a0 != null && a1 != null) {
if (a0.isPlus() || a1.isPlus()) {
int size = 0;
if (a0.isPlus()) {
size += ((IAST) a0).size();
} else {
size++;
}
if (a1.isPlus()) {
size += ((IAST) a1).size();
} else {
size++;
}
IAST result = PlusAlloc(size);
if (a0.isPlus()) {
result.appendArgs((IAST) a0);
} else {
result.append(a0);
}
if (a1.isPlus()) {
result.appendArgs((IAST) a1);
} else {
result.append(a1);
}
EvalAttributes.sort(result);
return result;
}
if (a0.compareTo(a1) > 0) {
// swap arguments
return binary(Plus, a1, a0);
}
}
return binary(Plus, a0, a1);
}
use of org.matheclipse.core.interfaces.IAST in project symja_android_library by axkr.
the class F method Subtract.
public static IAST Subtract(final IExpr a0, final IExpr a1) {
if (a0.isPlus()) {
if (a1.isZero()) {
return (IAST) a0;
}
IAST clone = F.PlusAlloc(((IAST) a0).size() + 1);
clone.appendArgs((IAST) a0);
clone.append(binary(Times, CN1, a1));
return clone;
}
return binary(Plus, a0, binary(Times, CN1, a1));
}
use of org.matheclipse.core.interfaces.IAST in project symja_android_library by axkr.
the class Roots method rootsOfQuadraticPolynomial.
/**
* Solve a polynomial with degree <= 2.
*
* @param polynomial
* the polynomial
* @return <code>F.NIL</code> if no evaluation was possible.
*/
private static IAST rootsOfQuadraticPolynomial(ExprPolynomial polynomial) {
long varDegree = polynomial.degree(0);
IAST result = List();
if (polynomial.isConstant()) {
return result;
}
IExpr a;
IExpr b;
IExpr c;
IExpr d;
IExpr e;
if (varDegree <= 2) {
IEvalStepListener listener = EvalEngine.get().getStepListener();
if (listener != null) {
IAST temp = listener.rootsOfQuadraticPolynomial(polynomial);
if (temp.isPresent()) {
return temp;
}
}
// solve quadratic equation:
a = C0;
b = C0;
c = C0;
d = C0;
e = C0;
for (ExprMonomial monomial : polynomial) {
IExpr coeff = monomial.coefficient();
long lExp = monomial.exponent().getVal(0);
if (lExp == 4) {
a = coeff;
} else if (lExp == 3) {
b = coeff;
} else if (lExp == 2) {
c = coeff;
} else if (lExp == 1) {
d = coeff;
} else if (lExp == 0) {
e = coeff;
} else {
throw new ArithmeticException("Roots::Unexpected exponent value: " + lExp);
}
}
result = QuarticSolver.quarticSolve(a, b, c, d, e);
if (result.isPresent()) {
result = QuarticSolver.createSet(result);
return result;
}
}
return F.NIL;
}
use of org.matheclipse.core.interfaces.IAST in project symja_android_library by axkr.
the class Series method evaluate.
@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
if (!ToggleFeature.SERIES) {
return F.NIL;
}
if (ast.isAST2() && (ast.arg2().isVector() == 3)) {
IExpr function = ast.arg1();
IAST list = (IAST) ast.arg2();
IExpr x = list.arg1();
IExpr x0 = list.arg2();
try {
final int lowerLimit = ((ISignedNumber) x0).toInt();
if (lowerLimit != 0) {
// TODO support other cases than 0
return F.NIL;
}
x0 = F.integer(lowerLimit);
} catch (ClassCastException cce) {
} catch (ArithmeticException ae) {
}
final int n = Validate.checkIntType(list, 3, Integer.MIN_VALUE);
if (n < 0) {
return F.NIL;
}
if (function.isNumber()) {
return function;
}
IExpr step = F.C1;
return createSeriesData(function, x, x0, n, step);
}
return F.NIL;
}
use of org.matheclipse.core.interfaces.IAST in project symja_android_library by axkr.
the class Sign method evaluate.
@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
Validate.checkSize(ast, 2);
IExpr arg1 = ast.arg1();
if (arg1.isNumber()) {
return numberSign((INumber) arg1);
}
if (arg1.isIndeterminate()) {
return F.Indeterminate;
}
if (arg1.isDirectedInfinity()) {
IAST directedInfininty = (IAST) arg1;
if (directedInfininty.isComplexInfinity()) {
return F.Indeterminate;
}
if (directedInfininty.isAST1()) {
return F.Sign(directedInfininty.arg1());
}
}
if (arg1.isTimes()) {
IAST[] result = ((IAST) arg1).filter(new SignTimesFunction());
if (result[0].size() > 1) {
if (result[1].size() > 1) {
result[0].append(F.Sign(result[1]));
}
return result[0];
}
}
if (arg1.isPower() && arg1.getAt(2).isSignedNumber()) {
return F.Power(F.Sign(arg1.getAt(1)), arg1.getAt(2));
}
if (AbstractAssumptions.assumeNegative(arg1)) {
return F.CN1;
}
if (AbstractAssumptions.assumePositive(arg1)) {
return F.C1;
}
IExpr negExpr = AbstractFunctionEvaluator.getNormalizedNegativeExpression(arg1);
if (negExpr.isPresent()) {
return F.Times(F.CN1, F.Sign(negExpr));
}
INumber number = arg1.evalNumber();
if (number != null) {
return numberSign(number);
}
return F.NIL;
}
Aggregations