use of org.matheclipse.core.interfaces.IAST in project symja_android_library by axkr.
the class OutputFormFactory method convertPlusOperatorReversed.
private void convertPlusOperatorReversed(final Appendable buf, final IAST plusAST, final InfixOperator oper, final int precedence) throws IOException {
int operPrecedence = oper.getPrecedence();
if (operPrecedence < precedence) {
append(buf, "(");
}
String operatorStr = oper.getOperatorString();
IExpr plusArg;
int size = plusAST.size() - 1;
// print Plus[] in reverse order (i.e. numbers at last)
for (int i = size; i > 0; i--) {
plusArg = plusAST.get(i);
if (plusArg.isTimes()) {
final String multCh = ASTNodeFactory.MMA_STYLE_FACTORY.get("Times").getOperatorString();
boolean showOperator = true;
final IAST timesAST = (IAST) plusArg;
IExpr arg1 = timesAST.arg1();
if (arg1.isNumber() && (((INumber) arg1).complexSign() < 0)) {
if (((INumber) arg1).isOne()) {
showOperator = false;
} else {
if (arg1.isMinusOne()) {
append(buf, "-");
showOperator = false;
} else {
convertNumber(buf, (INumber) arg1, operPrecedence, NO_PLUS_CALL);
}
}
} else {
if (i < size) {
append(buf, operatorStr);
}
convert(buf, arg1, ASTNodeFactory.TIMES_PRECEDENCE, false);
}
IExpr timesArg;
for (int j = 2; j < timesAST.size(); j++) {
timesArg = timesAST.get(j);
if (showOperator) {
append(buf, multCh);
} else {
showOperator = true;
}
convert(buf, timesArg, ASTNodeFactory.TIMES_PRECEDENCE, false);
}
} else {
if (plusArg.isNumber() && (((INumber) plusArg).complexSign() < 0)) {
// special case negative number:
convert(buf, plusArg);
} else {
if (i < size) {
append(buf, operatorStr);
}
convert(buf, plusArg, ASTNodeFactory.PLUS_PRECEDENCE, false);
}
}
}
if (operPrecedence < precedence) {
append(buf, ")");
}
}
use of org.matheclipse.core.interfaces.IAST in project symja_android_library by axkr.
the class Plus method convert.
/*
* (non-Javadoc)
*
* @see org.matheclipse.core.form.mathml.IConverter#convert(java.lang.StringBuffer, org.matheclipse.parser.interfaces.IAST, int)
*/
@Override
public boolean convert(final StringBuffer buf, final IAST f, final int precedence) {
IExpr expr;
fFactory.tagStart(buf, fFirstTag);
precedenceOpen(buf, precedence);
final Times timesConverter = new org.matheclipse.core.form.mathml.reflection.Times();
timesConverter.setFactory(fFactory);
int size = f.size() - 1;
for (int i = size; i > 0; i--) {
expr = f.get(i);
if ((i < size) && (expr instanceof IAST) && ((IAST) expr).head().equals(F.Times)) {
timesConverter.convertTimesFraction(buf, (IAST) expr, fPrecedence, Times.PLUS_CALL);
} else {
if (i < size) {
if ((expr instanceof ISignedNumber) && (((ISignedNumber) expr).isNegative())) {
fFactory.tag(buf, "mo", "-");
expr = ((ISignedNumber) expr).negate();
} else {
fFactory.tag(buf, "mo", "+");
}
}
fFactory.convert(buf, expr, fPrecedence);
}
}
precedenceClose(buf, precedence);
fFactory.tagEnd(buf, fFirstTag);
return true;
}
use of org.matheclipse.core.interfaces.IAST in project symja_android_library by axkr.
the class PatternMatchingTestCase method check.
public void check(EvalEngine engine, boolean configMode, IAST ast, String strResult) {
try {
StringWriter buf = new StringWriter();
Config.SERVER_MODE = configMode;
if (Config.SERVER_MODE) {
IAST inExpr = ast;
TimeConstrainedEvaluator utility = new TimeConstrainedEvaluator(engine, false, Config.FOREVER);
utility.constrainedEval(buf, inExpr);
} else {
if (ast != null) {
OutputFormFactory off = OutputFormFactory.get();
off.setIgnoreNewLine(true);
OutputFormFactory.get().convert(buf, ast);
}
}
assertEquals(buf.toString(), strResult);
} catch (Exception e) {
e.printStackTrace();
assertEquals(e, "");
}
}
use of org.matheclipse.core.interfaces.IAST in project symja_android_library by axkr.
the class CompareToTestCase method testIssue122c.
public void testIssue122c() {
ISymbol b = F.$s("b");
ISymbol c = F.$s("c");
ISymbol x1 = F.$s("x1");
ISymbol x3 = F.$s("x3");
ISymbol x4 = F.$s("x4");
ISymbol x5 = F.$s("x5");
IPattern x1_c = F.$p(x1, c);
IPattern x3_b = F.$p(x3, b);
IPattern x3_c = F.$p(x3, c);
IPattern x5_c = F.$p(x5, c);
IAST ast1 = F.Times(x3, x5, x5_c);
IAST ast2 = F.Times(F.CN1, x1_c, x3_b, x3_c);
int res = ast1.compareTo(ast2);
assertEquals(2, res);
res = ast2.compareTo(ast1);
assertEquals(-2, res);
check("-Infinity+b+a", "-Infinity+a+b");
}
use of org.matheclipse.core.interfaces.IAST in project symja_android_library by axkr.
the class CompareToTestCase method testIssue122a.
public void testIssue122a() {
ISymbol b = F.$s("b");
ISymbol c = F.$s("c");
ISymbol x1 = F.$s("x1");
ISymbol x3 = F.$s("x3");
ISymbol x4 = F.$s("x4");
ISymbol x5 = F.$s("x5");
IPattern x1_c = F.$p(x1, c);
IPattern x3_b = F.$p(x3, b);
IPattern x3_c = F.$p(x3, c);
IPattern x4_c = F.$p(x4, c);
IAST ast1 = F.Times(F.CN1, x1_c, x3_b, x3_c);
IAST ast2 = F.Times(F.CN1, x3, x5, x1_c, x4_c);
int res = ast1.compareTo(ast2);
assertEquals(-1, res);
res = ast2.compareTo(ast1);
assertEquals(1, res);
check("-Infinity+b+a", "-Infinity+a+b");
}
Aggregations