use of org.matheclipse.core.interfaces.IExpr in project symja_android_library by axkr.
the class AbstractArg12 method binaryOperator.
public IExpr binaryOperator(final IExpr o0, final IExpr o1) {
IExpr result = F.NIL;
if (o0 instanceof ApcomplexNum) {
if (o1.isNumber()) {
result = e2ApcomplexArg((ApcomplexNum) o0, ((INumber) o1).apcomplexNumValue(((ApcomplexNum) o0).precision()));
}
if (result.isPresent()) {
return result;
}
return e2ObjArg(o0, o1);
} else if (o1 instanceof ApcomplexNum) {
if (o0.isNumber()) {
result = e2ApcomplexArg(((INumber) o0).apcomplexNumValue(((ApcomplexNum) o1).precision()), (ApcomplexNum) o1);
}
if (result.isPresent()) {
return result;
}
return e2ObjArg(o0, o1);
} else if (o0 instanceof ComplexNum) {
if (o1.isNumber()) {
result = e2DblComArg((ComplexNum) o0, ((INumber) o1).complexNumValue());
}
if (result.isPresent()) {
return result;
}
return e2ObjArg(o0, o1);
} else if (o1 instanceof ComplexNum) {
if (o0.isNumber()) {
result = e2DblComArg(((INumber) o0).complexNumValue(), (ComplexNum) o1);
}
if (result.isPresent()) {
return result;
}
return e2ObjArg(o0, o1);
}
if (o0 instanceof ApfloatNum) {
if (o1.isSignedNumber()) {
result = e2ApfloatArg((ApfloatNum) o0, ((ISignedNumber) o1).apfloatNumValue(((ApfloatNum) o0).precision()));
}
if (result.isPresent()) {
return result;
}
return e2ObjArg(o0, o1);
} else if (o1 instanceof ApfloatNum) {
if (o0.isSignedNumber()) {
result = e2ApfloatArg(((ISignedNumber) o0).apfloatNumValue(((ApfloatNum) o1).precision()), (ApfloatNum) o1);
}
if (result.isPresent()) {
return result;
}
return e2ObjArg(o0, o1);
} else if (o0 instanceof Num) {
if (o1.isSignedNumber()) {
result = e2DblArg((Num) o0, ((ISignedNumber) o1).numValue());
}
if (result.isPresent()) {
return result;
}
return e2ObjArg(o0, o1);
} else if (o1 instanceof Num) {
if (o0.isSignedNumber()) {
result = e2DblArg(((ISignedNumber) o0).numValue(), (Num) o1);
}
if (result.isPresent()) {
return result;
}
return e2ObjArg(o0, o1);
}
result = e2ObjArg(o0, o1);
if (result.isPresent()) {
return result;
}
if (o0 instanceof IInteger) {
if (o1 instanceof IInteger) {
return e2IntArg((IInteger) o0, (IInteger) o1);
}
if (o1 instanceof IFraction) {
return e2FraArg(F.fraction((IInteger) o0, F.C1), (IFraction) o1);
}
if (o1 instanceof IComplex) {
return e2ComArg(F.complex((IInteger) o0, F.C0), (IComplex) o1);
}
return F.NIL;
}
if (o0 instanceof IFraction) {
if (o1 instanceof IInteger) {
return e2FraArg((IFraction) o0, F.fraction((IInteger) o1, F.C1));
}
if (o1 instanceof IFraction) {
return e2FraArg((IFraction) o0, (IFraction) o1);
}
if (o1 instanceof IComplex) {
return e2ComArg(F.complex((IFraction) o0), (IComplex) o1);
}
return F.NIL;
}
if (o0 instanceof IComplex) {
if (o1 instanceof IInteger) {
return eComIntArg((IComplex) o0, (IInteger) o1);
}
if (o1 instanceof IFraction) {
return eComFraArg((IComplex) o0, (IFraction) o1);
}
if (o1 instanceof IComplex) {
return e2ComArg((IComplex) o0, (IComplex) o1);
}
}
if (o0 instanceof ISymbol) {
if (o1 instanceof ISymbol) {
return e2SymArg((ISymbol) o0, (ISymbol) o1);
}
}
if (o0 instanceof IAST) {
if (o1 instanceof IInteger) {
return eFunIntArg((IAST) o0, (IInteger) o1);
}
if (o1 instanceof IAST) {
return e2FunArg((IAST) o0, (IAST) o1);
}
}
return F.NIL;
}
use of org.matheclipse.core.interfaces.IExpr in project symja_android_library by axkr.
the class EvalAttributes method flatten.
/**
* Flatten the list (i.e. the ASTs head element has the same head) element
* has the same head) example: suppose the head f should be flattened out:
* <br>
* <code>f[a,b,f[x,y,f[u,v]],z] ==> f[a,b,x,y,u,v,z]</code>
*
* @param head
* the head of the expression, which should be flattened.
* @param sublist
* the <code>sublist</code> which should be added to the
* <code>result</code> list.
* @param result
* the <code>result</code> list, where all sublist elements with
* the same <code>head</code> should be appended.
* @param recursionCounter
* @param level
* the recursion level up to which the list should be flattened
* @return <code>true</code> if a sublist was flattened out into the
* <code>result</code> list.
*/
public static boolean flatten(final ISymbol head, final IAST sublist, final IAST result, final int recursionCounter, final int level) {
boolean isEvaled = false;
IExpr expr;
final int astSize = sublist.size();
for (int i = 1; i < astSize; i++) {
expr = sublist.get(i);
if (expr.isAST(head) && recursionCounter < level) {
isEvaled = true;
flatten(head, (IAST) expr, result, recursionCounter + 1, level);
} else {
result.append(expr);
}
}
return isEvaled;
}
use of org.matheclipse.core.interfaces.IExpr in project symja_android_library by axkr.
the class EvalAttributes method sort3Args.
/**
* Sort an AST with 3 arguments.
*
* @param ast
* an ast with 3 arguments
* @return
*/
private static boolean sort3Args(final IAST ast) {
IExpr temp;
// http://stackoverflow.com/questions/4793251/sorting-int-array-with-only-3-elements
boolean evaled = false;
if (ast.arg1().compareTo(ast.arg2()) > 0) {
// swap arguments
temp = ast.arg2();
ast.set(2, ast.arg1());
ast.set(1, temp);
evaled = true;
}
if (ast.arg2().compareTo(ast.arg3()) > 0) {
// swap arguments
temp = ast.arg3();
ast.set(3, ast.arg2());
ast.set(2, temp);
evaled = true;
if (ast.arg1().compareTo(ast.arg2()) > 0) {
// swap arguments
temp = ast.arg2();
ast.set(2, ast.arg1());
ast.set(1, temp);
}
}
ast.addEvalFlags(IAST.IS_SORTED);
if (evaled) {
if (Config.SHOW_STACKTRACE) {
checkCachedHashcode(ast);
}
}
return evaled;
}
use of org.matheclipse.core.interfaces.IExpr in project symja_android_library by axkr.
the class TeXUtilities method toTeX.
/**
* Converts the inputExpression string into a TeX expression and writes the
* result to the given <code>Writer</code>
*
* @param inputExpression
* @param out
*/
public synchronized void toTeX(final String inputExpression, final Writer out) {
IExpr parsedExpression = null;
if (inputExpression != null) {
try {
parsedExpression = fParser.parse(inputExpression);
// parsedExpression = AST2Expr.CONST.convert(node);
} catch (final Throwable e) {
// parsedExpression == null ==> fError occured
}
}
toTeX(parsedExpression, out);
}
use of org.matheclipse.core.interfaces.IExpr in project symja_android_library by axkr.
the class TimesOp method addMerge.
/**
* Add or merge the <code>key, value</code> pair into the given
* <code>timesMap</code>.
*
* @param key
* the key expression
* @param value
* the value expression
*/
public boolean addMerge(final IExpr key, final IExpr value) {
IExpr temp = timesMap.get(key);
if (temp == null) {
timesMap.put(key, value);
return false;
}
// merge both values
if (temp.isNumber() && value.isNumber()) {
temp = temp.plus(value);
if (temp.isOne()) {
timesMap.remove(key);
return true;
}
} else if (temp.head().equals(F.Plus)) {
((IAST) temp).append(value);
} else {
temp = F.Plus(temp, value);
}
timesMap.put(key, temp);
return true;
}
Aggregations