Search in sources :

Example 6 with ExprPolynomial

use of org.matheclipse.core.polynomials.longexponent.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 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 7 with ExprPolynomial

use of org.matheclipse.core.polynomials.longexponent.ExprPolynomial 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 8 with ExprPolynomial

use of org.matheclipse.core.polynomials.longexponent.ExprPolynomial 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 9 with ExprPolynomial

use of org.matheclipse.core.polynomials.longexponent.ExprPolynomial 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)

Example 10 with ExprPolynomial

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

the class RootsFunctions method coefficients.

/**
 * Get the coefficient list of a univariate polynomial.
 *
 * @param polynomial
 * @param variable
 * @return <code>null</code> if the list couldn't be evaluated.
 */
public static double[] coefficients(IExpr polynomial, final ISymbol variable) throws JASConversionException {
    try {
        ExprPolynomialRing ring = new ExprPolynomialRing(F.list(variable));
        ExprPolynomial poly = ring.create(polynomial);
        IAST list = poly.coefficientList();
        int degree = list.size() - 2;
        double[] result = new double[degree + 1];
        for (int i = 1; i < list.size(); i++) {
            ISignedNumber temp = list.get(i).evalReal();
            if (temp != null) {
                result[i - 1] = temp.doubleValue();
            } else {
                return null;
            }
        }
        return result;
    } catch (RuntimeException ex) {
        // Polynomial expected!
        return null;
    }
}
Also used : ExprPolynomialRing(org.matheclipse.core.polynomials.longexponent.ExprPolynomialRing) ISignedNumber(org.matheclipse.core.interfaces.ISignedNumber) IAST(org.matheclipse.core.interfaces.IAST) ExprPolynomial(org.matheclipse.core.polynomials.longexponent.ExprPolynomial)

Aggregations

ExprPolynomial (org.matheclipse.core.polynomials.longexponent.ExprPolynomial)11 ExprPolynomialRing (org.matheclipse.core.polynomials.longexponent.ExprPolynomialRing)11 IAST (org.matheclipse.core.interfaces.IAST)7 IExpr (org.matheclipse.core.interfaces.IExpr)5 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