Search in sources :

Example 66 with IAST

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, ")");
    }
}
Also used : INumber(org.matheclipse.core.interfaces.INumber) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST)

Example 67 with IAST

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;
}
Also used : ISignedNumber(org.matheclipse.core.interfaces.ISignedNumber) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST)

Example 68 with IAST

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, "");
    }
}
Also used : TimeConstrainedEvaluator(org.matheclipse.core.eval.TimeConstrainedEvaluator) StringWriter(java.io.StringWriter) IAST(org.matheclipse.core.interfaces.IAST) OutputFormFactory(org.matheclipse.core.form.output.OutputFormFactory)

Example 69 with IAST

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");
}
Also used : IPattern(org.matheclipse.core.interfaces.IPattern) ISymbol(org.matheclipse.core.interfaces.ISymbol) IAST(org.matheclipse.core.interfaces.IAST)

Example 70 with IAST

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");
}
Also used : IPattern(org.matheclipse.core.interfaces.IPattern) ISymbol(org.matheclipse.core.interfaces.ISymbol) IAST(org.matheclipse.core.interfaces.IAST)

Aggregations

IAST (org.matheclipse.core.interfaces.IAST)413 IExpr (org.matheclipse.core.interfaces.IExpr)248 ISymbol (org.matheclipse.core.interfaces.ISymbol)76 IInteger (org.matheclipse.core.interfaces.IInteger)34 WrongArgumentType (org.matheclipse.core.eval.exception.WrongArgumentType)30 ISignedNumber (org.matheclipse.core.interfaces.ISignedNumber)22 ExpVector (edu.jas.poly.ExpVector)15 ArrayList (java.util.ArrayList)14 BigRational (edu.jas.arith.BigRational)13 JASIExpr (org.matheclipse.core.convert.JASIExpr)13 VariablesSet (org.matheclipse.core.convert.VariablesSet)13 INum (org.matheclipse.core.interfaces.INum)13 ExprPolynomial (org.matheclipse.core.polynomials.ExprPolynomial)12 GenPolynomial (edu.jas.poly.GenPolynomial)11 JASConversionException (org.matheclipse.core.eval.exception.JASConversionException)11 IFraction (org.matheclipse.core.interfaces.IFraction)11 INumber (org.matheclipse.core.interfaces.INumber)11 IComplex (org.matheclipse.core.interfaces.IComplex)10 ModLong (edu.jas.arith.ModLong)9 ExprPolynomialRing (org.matheclipse.core.polynomials.ExprPolynomialRing)9