Search in sources :

Example 1 with ExprPolynomial

use of org.matheclipse.core.polynomials.ExprPolynomial in project symja_android_library by axkr.

the class Limit method timesLimit.

private static IExpr timesLimit(final IAST timesAST, LimitData data) {
    IAST isFreeResult = timesAST.partitionTimes(Predicates.isFree(data.getSymbol()), F.C1, F.C1, F.List);
    if (!isFreeResult.get(1).isOne()) {
        return F.Times(isFreeResult.get(1), data.limit(isFreeResult.get(2)));
    }
    IExpr[] parts = Algebra.fractionalPartsTimesPower(timesAST, false, false, true, true);
    if (parts != null) {
        IExpr numerator = parts[0];
        IExpr denominator = parts[1];
        IExpr limit = data.getLimitValue();
        ISymbol symbol = data.getSymbol();
        if (limit.isInfinity() || limit.isNegativeInfinity()) {
            try {
                ExprPolynomialRing ring = new ExprPolynomialRing(symbol);
                ExprPolynomial denominatorPoly = ring.create(denominator);
                ExprPolynomial numeratorPoly = ring.create(numerator);
                return limitsInfinityOfRationalFunctions(numeratorPoly, denominatorPoly, symbol, limit, data);
            } catch (RuntimeException e) {
                if (Config.DEBUG) {
                    e.printStackTrace();
                }
            }
        }
        IExpr plusResult = Algebra.partialFractionDecompositionRational(new PartialFractionGenerator(), parts, symbol);
        if (plusResult.isPlus()) {
            // if (plusResult.size() > 2) {
            return data.mapLimit((IAST) plusResult);
        // }
        }
        if (denominator.isOne()) {
            if (limit.isInfinity() || limit.isNegativeInfinity()) {
                IExpr temp = substituteInfinity(timesAST, data);
                if (temp.isPresent()) {
                    return temp;
                }
            }
        }
        IExpr temp = numeratorDenominatorLimit(numerator, denominator, data);
        if (temp.isPresent()) {
            return temp;
        }
    }
    return data.mapLimit(timesAST);
}
Also used : ExprPolynomialRing(org.matheclipse.core.polynomials.ExprPolynomialRing) ISymbol(org.matheclipse.core.interfaces.ISymbol) IAST(org.matheclipse.core.interfaces.IAST) IExpr(org.matheclipse.core.interfaces.IExpr) ExprPolynomial(org.matheclipse.core.polynomials.ExprPolynomial) PartialFractionGenerator(org.matheclipse.core.polynomials.PartialFractionGenerator)

Example 2 with ExprPolynomial

use of org.matheclipse.core.polynomials.ExprPolynomial in project symja_android_library by axkr.

the class CoefficientList method univariateCoefficientList.

/**
	 * 
	 * @param polynomial
	 * @param variable
	 * @param resultList
	 *            the coefficient list of the given univariate polynomial in increasing order
	 * @param resultListDiff
	 *            the coefficient list of the derivative of the given univariate polynomial
	 * @return the degree of the univariate polynomial; if <code>degree >= Short.MAX_VALUE</code>, the result list will be empty.
	 */
public static long univariateCoefficientList(IExpr polynomial, ISymbol variable, List<IExpr> resultList, List<IExpr> resultListDiff) throws JASConversionException {
    try {
        ExprPolynomialRing ring = new ExprPolynomialRing(F.List(variable));
        ExprPolynomial poly = ring.create(polynomial);
        // PolynomialOld poly = new PolynomialOld(polynomial, (ISymbol) variable);
        // if (!poly.isPolynomial()) {
        // throw new WrongArgumentType(polynomial, "Polynomial expected!");
        // }
        IAST polyExpr = poly.coefficientList();
        int degree = polyExpr.size() - 2;
        if (degree >= Short.MAX_VALUE) {
            return degree;
        }
        for (int i = 0; i <= degree; i++) {
            IExpr temp = polyExpr.get(i + 1);
            resultList.add(temp);
        }
        IAST polyDiff = poly.derivative().coefficientList();
        int degreeDiff = polyDiff.size() - 2;
        for (int i = 0; i <= degreeDiff; i++) {
            IExpr temp = polyDiff.get(i + 1);
            resultListDiff.add(temp);
        }
        return degree;
    } catch (RuntimeException ex) {
        throw new WrongArgumentType(polynomial, "Polynomial expected!");
    }
}
Also used : ExprPolynomialRing(org.matheclipse.core.polynomials.ExprPolynomialRing) WrongArgumentType(org.matheclipse.core.eval.exception.WrongArgumentType) IAST(org.matheclipse.core.interfaces.IAST) IExpr(org.matheclipse.core.interfaces.IExpr) ExprPolynomial(org.matheclipse.core.polynomials.ExprPolynomial)

Example 3 with ExprPolynomial

use of org.matheclipse.core.polynomials.ExprPolynomial in project symja_android_library by axkr.

the class PolynomialExample method main.

public static void main(String[] args) {
    try {
        ExprEvaluator util = new ExprEvaluator();
        IExpr expr = util.evaluate("x^2+y+a*x+b*y+c");
        System.out.println(expr.toString());
        final IAST variables = F.List(F.x, F.y);
        ExprPolynomialRing ring = new ExprPolynomialRing(ExprRingFactory.CONST, variables, variables.size() - 1, ExprTermOrderByName.Lexicographic, false);
        ExprPolynomial poly = ring.create(expr);
        System.out.println(poly.toString());
        // x degree
        System.out.println(poly.degree(0));
        // y degree
        System.out.println(poly.degree(1));
        // show internal structure:
        System.out.println(poly.coefficientRules());
        System.out.println();
        for (ExprMonomial monomial : poly) {
            System.out.println(monomial.toString());
        }
    } catch (SyntaxError e) {
        // catch Symja parser errors here
        System.out.println(e.getMessage());
    } catch (MathException me) {
        // catch Symja math errors here
        System.out.println(me.getMessage());
    } catch (Exception e) {
        e.printStackTrace();
    } catch (final StackOverflowError soe) {
        System.out.println(soe.getMessage());
    } catch (final OutOfMemoryError oome) {
        System.out.println(oome.getMessage());
    }
}
Also used : ExprPolynomialRing(org.matheclipse.core.polynomials.ExprPolynomialRing) ExprEvaluator(org.matheclipse.core.eval.ExprEvaluator) SyntaxError(org.matheclipse.parser.client.SyntaxError) MathException(org.matheclipse.parser.client.math.MathException) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST) ExprMonomial(org.matheclipse.core.polynomials.ExprMonomial) ExprPolynomial(org.matheclipse.core.polynomials.ExprPolynomial) MathException(org.matheclipse.parser.client.math.MathException)

Example 4 with ExprPolynomial

use of org.matheclipse.core.polynomials.ExprPolynomial in project symja_android_library by axkr.

the class AbstractAST method isPolynomialOfMaxDegree.

public final boolean isPolynomialOfMaxDegree(IAST variables, long maxDegree) {
    try {
        if (isPlus() || isTimes() || isPower()) {
            IExpr expr = F.evalExpandAll(this);
            ExprPolynomialRing ring = new ExprPolynomialRing(variables);
            ExprPolynomial poly = ring.create(expr);
            return poly.degree() <= maxDegree;
        }
    } catch (RuntimeException ex) {
    //
    }
    return false;
}
Also used : ExprPolynomialRing(org.matheclipse.core.polynomials.ExprPolynomialRing) IExpr(org.matheclipse.core.interfaces.IExpr) ExprPolynomial(org.matheclipse.core.polynomials.ExprPolynomial)

Example 5 with ExprPolynomial

use of org.matheclipse.core.polynomials.ExprPolynomial in project symja_android_library by axkr.

the class Algebra method cancelGCD.

/**
	 * Calculate the 3 elements result array
	 * 
	 * <pre>
	 * [ 
	 *   commonFactor, 
	 *   numeratorPolynomial.divide(gcd(numeratorPolynomial, denominatorPolynomial)), 
	 *   denominatorPolynomial.divide(gcd(numeratorPolynomial, denominatorPolynomial)) 
	 * ]
	 * </pre>
	 * 
	 * for the given expressions <code>numeratorPolynomial</code> and <code>denominatorPolynomial</code>.
	 * 
	 * 
	 * @param numeratorPolynomial
	 *            a <code>BigRational</code> polynomial which could be converted to JAS polynomial
	 * @param denominatorPolynomial
	 *            a <code>BigRational</code> polynomial which could be converted to JAS polynomial
	 * @return <code>null</code> if the expressions couldn't be converted to JAS polynomials or gcd equals 1
	 * @throws JASConversionException
	 */
public static IExpr[] cancelGCD(IExpr numeratorPolynomial, IExpr denominatorPolynomial) throws JASConversionException {
    try {
        if (denominatorPolynomial.isInteger() && numeratorPolynomial.isPlus()) {
            IExpr[] result = Cancel.cancelPlusIntegerGCD((IAST) numeratorPolynomial, (IInteger) denominatorPolynomial);
            if (result != null) {
                return result;
            }
        }
        VariablesSet eVar = new VariablesSet(numeratorPolynomial);
        eVar.addVarList(denominatorPolynomial);
        if (eVar.size() == 0) {
            return null;
        }
        IAST vars = eVar.getVarList();
        ExprPolynomialRing ring = new ExprPolynomialRing(vars);
        ExprPolynomial pol1 = ring.create(numeratorPolynomial);
        ExprPolynomial pol2 = ring.create(denominatorPolynomial);
        ASTRange r = new ASTRange(eVar.getVarList(), 1);
        JASIExpr jas = new JASIExpr(r, true);
        GenPolynomial<IExpr> p1 = jas.expr2IExprJAS(pol1);
        GenPolynomial<IExpr> p2 = jas.expr2IExprJAS(pol2);
        GreatestCommonDivisor<IExpr> engine;
        engine = GCDFactory.getImplementation(ExprRingFactory.CONST);
        GenPolynomial<IExpr> gcd = engine.gcd(p1, p2);
        IExpr[] result = new IExpr[3];
        if (gcd.isONE()) {
            result[0] = jas.exprPoly2Expr(gcd);
            result[1] = jas.exprPoly2Expr(p1);
            result[2] = jas.exprPoly2Expr(p2);
        } else {
            result[0] = F.C1;
            result[1] = F.eval(jas.exprPoly2Expr(p1.divide(gcd)));
            result[2] = F.eval(jas.exprPoly2Expr(p2.divide(gcd)));
        }
        return result;
    } catch (RuntimeException e) {
        if (Config.DEBUG) {
            e.printStackTrace();
        }
    }
    return null;
}
Also used : ExprPolynomialRing(org.matheclipse.core.polynomials.ExprPolynomialRing) ASTRange(org.matheclipse.core.expression.ASTRange) JASIExpr(org.matheclipse.core.convert.JASIExpr) JASIExpr(org.matheclipse.core.convert.JASIExpr) IExpr(org.matheclipse.core.interfaces.IExpr) VariablesSet(org.matheclipse.core.convert.VariablesSet) IAST(org.matheclipse.core.interfaces.IAST) ExprPolynomial(org.matheclipse.core.polynomials.ExprPolynomial)

Aggregations

ExprPolynomial (org.matheclipse.core.polynomials.ExprPolynomial)15 ExprPolynomialRing (org.matheclipse.core.polynomials.ExprPolynomialRing)15 IAST (org.matheclipse.core.interfaces.IAST)12 IExpr (org.matheclipse.core.interfaces.IExpr)12 VariablesSet (org.matheclipse.core.convert.VariablesSet)4 JASConversionException (org.matheclipse.core.eval.exception.JASConversionException)4 WrongArgumentType (org.matheclipse.core.eval.exception.WrongArgumentType)4 JASIExpr (org.matheclipse.core.convert.JASIExpr)3 ISymbol (org.matheclipse.core.interfaces.ISymbol)3 TermOrder (edu.jas.poly.TermOrder)2 Options (org.matheclipse.core.eval.util.Options)2 IStringX (org.matheclipse.core.interfaces.IStringX)2 ExprTermOrder (org.matheclipse.core.polynomials.ExprTermOrder)2 ExprEvaluator (org.matheclipse.core.eval.ExprEvaluator)1 ASTRange (org.matheclipse.core.expression.ASTRange)1 IInteger (org.matheclipse.core.interfaces.IInteger)1 ISignedNumber (org.matheclipse.core.interfaces.ISignedNumber)1 ExpVectorLong (org.matheclipse.core.polynomials.ExpVectorLong)1 ExprMonomial (org.matheclipse.core.polynomials.ExprMonomial)1 PartialFractionGenerator (org.matheclipse.core.polynomials.PartialFractionGenerator)1