Search in sources :

Example 6 with ExprPolynomialRing

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

the class ExprEvaluatorTest method testStringEval003.

public void testStringEval003() {
    try {
        ExprEvaluator util = new ExprEvaluator();
        IExpr expr = util.eval("x^2+y+a*x+b*y+c");
        assertEquals("c+a*x+x^2+y+b*y", expr.toString());
        final IAST variables = F.List(F.symbol("x"), F.symbol("y"));
        ExprPolynomialRing ring = new ExprPolynomialRing(ExprRingFactory.CONST, variables, variables.argSize(), ExprTermOrderByName.Lexicographic, false);
        ExprPolynomial poly = ring.create(expr);
        assertEquals("x^2 + a x + ( 1+b ) y + c ", poly.toString());
        // x degree
        assertEquals(2, poly.degree(0));
        // y degree
        assertEquals(1, poly.degree(1));
        // show internal structure:
        assertEquals("{{2,0}->1,{1,0}->a,{0,1}->1+b,{0,0}->c}", poly.coefficientRules().toString());
        System.out.println(poly.coefficientRules());
        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.longexponent.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.longexponent.ExprMonomial) ExprPolynomial(org.matheclipse.core.polynomials.longexponent.ExprPolynomial) MathException(org.matheclipse.parser.client.math.MathException)

Example 7 with ExprPolynomialRing

use of org.matheclipse.core.polynomials.longexponent.ExprPolynomialRing 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 numerator an expression which should be converted to JAS polynomial (using
 *        substitutions)
 * @param denominator a expression which could be converted to JAS polynomial (using
 *        substitutions)
 * @return <code>null</code> if the expressions couldn't be converted to JAS polynomials or gcd
 *         equals 1
 * @throws JASConversionException
 */
public static IExpr[] cancelGCD(final IExpr numerator, final IExpr denominator) throws JASConversionException {
    try {
        if (denominator.isInteger() && numerator.isPlus()) {
            IExpr[] result = Cancel.cancelPlusIntegerGCD((IAST) numerator, (IInteger) denominator);
            if (result != null) {
                return result;
            }
        }
        VariablesSet eVar = new VariablesSet(numerator);
        eVar.addVarList(denominator);
        if (eVar.size() == 0) {
            return null;
        }
        IAST vars = eVar.getVarList();
        PolynomialHomogenization substitutions = new PolynomialHomogenization(vars, EvalEngine.get());
        IExpr[] subst = substitutions.replaceForward(numerator, denominator);
        IExpr numeratorPolynomial = subst[0];
        IExpr denominatorPolynomial = subst[1];
        if (substitutions.size() > 0) {
            eVar.clear();
            eVar.addAll(substitutions.substitutedVariablesSet());
            vars = eVar.getVarList();
        }
        try {
            ExprPolynomialRing ring = new ExprPolynomialRing(vars);
            ExprPolynomial pol1 = ring.create(numeratorPolynomial);
            ExprPolynomial pol2 = ring.create(denominatorPolynomial);
            List<IExpr> varList = eVar.getVarList().copyTo();
            JASIExpr jas = new JASIExpr(varList, 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()) {
                return null;
            // 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)));
            }
            result[0] = substitutions.replaceBackward(result[0]);
            result[1] = substitutions.replaceBackward(result[1]);
            result[2] = substitutions.replaceBackward(result[2]);
            return result;
        } catch (RuntimeException rex) {
        }
        List<IExpr> varList = eVar.getVarList().copyTo();
        ComplexRing<BigRational> cfac = new ComplexRing<BigRational>(BigRational.ZERO);
        JASConvert<Complex<BigRational>> jas = new JASConvert<Complex<BigRational>>(varList, cfac);
        GenPolynomial<Complex<BigRational>> p1 = jas.expr2JAS(numeratorPolynomial, false);
        GenPolynomial<Complex<BigRational>> p2 = jas.expr2JAS(denominatorPolynomial, false);
        GreatestCommonDivisor<Complex<BigRational>> engine;
        engine = GCDFactory.getImplementation(cfac);
        GenPolynomial<Complex<BigRational>> gcd;
        // if (numeratorPolynomial.isSymbol()||denominatorPolynomial.isSymbol() ) {
        // gcd = jas.expr2IExprJAS(F.C1);
        // }else {
        gcd = engine.gcd(p1, p2);
        // }
        IExpr[] result = new IExpr[3];
        if (gcd.isONE()) {
            return null;
        // result[0] = jas.complexPoly2Expr(gcd);
        // result[1] = jas.complexPoly2Expr(p1);
        // result[2] = jas.complexPoly2Expr(p2);
        } else {
            result[0] = F.C1;
            result[1] = F.eval(jas.complexPoly2Expr(p1.divide(gcd)));
            result[2] = F.eval(jas.complexPoly2Expr(p2.divide(gcd)));
        }
        result[0] = substitutions.replaceBackward(result[0]);
        result[1] = substitutions.replaceBackward(result[1]);
        result[2] = substitutions.replaceBackward(result[2]);
        return result;
    } catch (RuntimeException e) {
        LOGGER.debug("Algebra.cancelGCD() failed", e);
    }
    return null;
}
Also used : PolynomialHomogenization(org.matheclipse.core.polynomials.PolynomialHomogenization) BigRational(edu.jas.arith.BigRational) VariablesSet(org.matheclipse.core.convert.VariablesSet) IComplex(org.matheclipse.core.interfaces.IComplex) FactorComplex(edu.jas.ufd.FactorComplex) Complex(edu.jas.poly.Complex) ExprPolynomialRing(org.matheclipse.core.polynomials.longexponent.ExprPolynomialRing) JASIExpr(org.matheclipse.core.convert.JASIExpr) ComplexRing(edu.jas.poly.ComplexRing) JASConvert(org.matheclipse.core.convert.JASConvert) JASIExpr(org.matheclipse.core.convert.JASIExpr) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST) ExprPolynomial(org.matheclipse.core.polynomials.longexponent.ExprPolynomial)

Example 8 with ExprPolynomialRing

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

the class DSolve method solveSingleODE.

private IExpr solveSingleODE(IExpr equation, IExpr xVar, IAST listOfVariables, IExpr C_1, EvalEngine engine) {
    ExprPolynomialRing ring = new ExprPolynomialRing(ExprRingFactory.CONST, listOfVariables);
    if (equation.isAST()) {
        IASTAppendable eq = ((IAST) equation).copyAppendable();
        if (!eq.isPlus()) {
            // create artificial Plus(...) expression
            eq = F.Plus(eq);
        }
        int j = 1;
        IAST[] deriveExpr = null;
        while (j < eq.size()) {
            IAST[] temp = eq.get(j).isDerivativeAST1();
            if (temp != null) {
                if (deriveExpr != null) {
                    // expression
                    return F.NIL;
                }
                deriveExpr = temp;
                // eliminate deriveExpr from Plus(...) expression
                eq.remove(j);
                continue;
            }
            j++;
        }
        if (deriveExpr != null) {
            int order = derivativeOrder(deriveExpr);
            if (order < 0) {
                return F.NIL;
            }
            try {
                ExprPolynomial poly = ring.create(eq.oneIdentity0(), false, true, false);
                if (order == 1 && poly.degree() <= 1) {
                    IAST coeffs = poly.coefficientList();
                    // degree 0
                    IExpr q = coeffs.arg1();
                    IExpr p = F.C0;
                    if (poly.degree() == 1) {
                        // degree 1
                        p = coeffs.arg2();
                    }
                    return linearODE(p, q, xVar, C_1, engine);
                }
            } catch (RuntimeException rex) {
                LOGGER.debug("DSolve.solveSingleODE() failed", rex);
            }
        }
    }
    return F.NIL;
}
Also used : ExprPolynomialRing(org.matheclipse.core.polynomials.longexponent.ExprPolynomialRing) IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) IAST(org.matheclipse.core.interfaces.IAST) IExpr(org.matheclipse.core.interfaces.IExpr) ExprPolynomial(org.matheclipse.core.polynomials.longexponent.ExprPolynomial)

Example 9 with ExprPolynomialRing

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

the class PolynomialFunctions method coefficientList.

public static IAST coefficientList(IExpr expr, IAST listOfVariables) {
    try {
        ExprPolynomialRing ring = new ExprPolynomialRing(listOfVariables);
        ExprPolynomial poly = ring.create(expr, true, false, true);
        if (poly.isZero()) {
            return F.CEmptyList;
        }
        return poly.coefficientList();
    } catch (LimitException le) {
        throw le;
    } catch (RuntimeException ex) {
        // org.matheclipse.core.polynomials.longexponent.ExprPolynomialRing.create()
        LOGGER.debug("PolynomialFunctions.coefficientList() failed", ex);
    }
    if (listOfVariables.argSize() > 0) {
        return F.Nest(S.List, expr, listOfVariables.argSize());
    }
    return F.NIL;
}
Also used : ExprPolynomialRing(org.matheclipse.core.polynomials.longexponent.ExprPolynomialRing) LimitException(org.matheclipse.core.eval.exception.LimitException) ExprPolynomial(org.matheclipse.core.polynomials.longexponent.ExprPolynomial)

Example 10 with ExprPolynomialRing

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

the class MinMaxFunctions method maximizeExprPolynomial.

private static IAST maximizeExprPolynomial(final IExpr expr, IAST varList) {
    IAST result = F.NIL;
    try {
        // try to generate a common expression polynomial
        ExprPolynomialRing ring = new ExprPolynomialRing(ExprRingFactory.CONST, varList);
        ExprPolynomial ePoly = ring.create(expr, false, false, false);
        ePoly = ePoly.multiplyByMinimumNegativeExponents();
        result = maximizeCubicPolynomial(ePoly, varList.arg1());
        // result = QuarticSolver.sortASTArguments(result);
        return result;
    } catch (ArithmeticException | JASConversionException e2) {
        LOGGER.debug("MinMaxFunctions.maximizeExprPolynomial() failed", e2);
    }
    return result;
}
Also used : ExprPolynomialRing(org.matheclipse.core.polynomials.longexponent.ExprPolynomialRing) IAST(org.matheclipse.core.interfaces.IAST) JASConversionException(org.matheclipse.core.eval.exception.JASConversionException) ExprPolynomial(org.matheclipse.core.polynomials.longexponent.ExprPolynomial)

Aggregations

ExprPolynomialRing (org.matheclipse.core.polynomials.longexponent.ExprPolynomialRing)12 ExprPolynomial (org.matheclipse.core.polynomials.longexponent.ExprPolynomial)11 IAST (org.matheclipse.core.interfaces.IAST)7 IExpr (org.matheclipse.core.interfaces.IExpr)6 JASConversionException (org.matheclipse.core.eval.exception.JASConversionException)4 ExprEvaluator (org.matheclipse.core.eval.ExprEvaluator)2 IASTMutable (org.matheclipse.core.interfaces.IASTMutable)2 ExprMonomial (org.matheclipse.core.polynomials.longexponent.ExprMonomial)2 SyntaxError (org.matheclipse.parser.client.SyntaxError)2 MathException (org.matheclipse.parser.client.math.MathException)2 BigRational (edu.jas.arith.BigRational)1 Complex (edu.jas.poly.Complex)1 ComplexRing (edu.jas.poly.ComplexRing)1 FactorComplex (edu.jas.ufd.FactorComplex)1 JASConvert (org.matheclipse.core.convert.JASConvert)1 JASIExpr (org.matheclipse.core.convert.JASIExpr)1 VariablesSet (org.matheclipse.core.convert.VariablesSet)1 LimitException (org.matheclipse.core.eval.exception.LimitException)1 IASTAppendable (org.matheclipse.core.interfaces.IASTAppendable)1 IComplex (org.matheclipse.core.interfaces.IComplex)1