Search in sources :

Example 91 with IAST

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

Example 92 with IAST

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

Example 93 with IAST

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

Example 94 with IAST

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

Example 95 with IAST

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