Search in sources :

Example 1 with IExpr

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;
}
Also used : IFraction(org.matheclipse.core.interfaces.IFraction) IComplex(org.matheclipse.core.interfaces.IComplex) ISignedNumber(org.matheclipse.core.interfaces.ISignedNumber) ISymbol(org.matheclipse.core.interfaces.ISymbol) INumber(org.matheclipse.core.interfaces.INumber) IInteger(org.matheclipse.core.interfaces.IInteger) ComplexNum(org.matheclipse.core.expression.ComplexNum) IComplexNum(org.matheclipse.core.interfaces.IComplexNum) ApfloatNum(org.matheclipse.core.expression.ApfloatNum) INum(org.matheclipse.core.interfaces.INum) ApcomplexNum(org.matheclipse.core.expression.ApcomplexNum) ComplexNum(org.matheclipse.core.expression.ComplexNum) Num(org.matheclipse.core.expression.Num) IComplexNum(org.matheclipse.core.interfaces.IComplexNum) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST) ApfloatNum(org.matheclipse.core.expression.ApfloatNum) ApcomplexNum(org.matheclipse.core.expression.ApcomplexNum)

Example 2 with IExpr

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

Example 3 with IExpr

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

Example 4 with IExpr

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

Example 5 with IExpr

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

Aggregations

IExpr (org.matheclipse.core.interfaces.IExpr)1328 IAST (org.matheclipse.core.interfaces.IAST)477 IASTAppendable (org.matheclipse.core.interfaces.IASTAppendable)225 ISymbol (org.matheclipse.core.interfaces.ISymbol)154 Map (java.util.Map)104 TreeMap (java.util.TreeMap)90 SortedMap (java.util.SortedMap)87 EvalEngine (org.matheclipse.core.eval.EvalEngine)79 IASTMutable (org.matheclipse.core.interfaces.IASTMutable)69 IInteger (org.matheclipse.core.interfaces.IInteger)68 ISignedNumber (org.matheclipse.core.interfaces.ISignedNumber)60 JASIExpr (org.matheclipse.core.convert.JASIExpr)59 SyntaxError (org.matheclipse.parser.client.SyntaxError)50 MathException (org.matheclipse.parser.client.math.MathException)47 PrettyPrint (edu.jas.kern.PrettyPrint)46 IOException (java.io.IOException)43 ArrayList (java.util.ArrayList)40 INumber (org.matheclipse.core.interfaces.INumber)34 ArgumentTypeException (org.matheclipse.core.eval.exception.ArgumentTypeException)33 ExprEvaluator (org.matheclipse.core.eval.ExprEvaluator)30