Search in sources :

Example 1 with JASIExpr

use of org.matheclipse.core.convert.JASIExpr in project symja_android_library by axkr.

the class MonomialList method monomialList.

/**
	 * Get the monomial list of a univariate polynomial.
	 * 
	 * @param polynomial
	 * @param variable
	 * @param termOrder
	 *            the JAS term ordering
	 * @return the list of monomials of the univariate polynomial.
	 */
private static IAST monomialList(IExpr polynomial, final List<IExpr> variablesList, final TermOrder termOrder) throws JASConversionException {
    JASIExpr jas = new JASIExpr(variablesList, ExprRingFactory.CONST, termOrder, false);
    GenPolynomial<IExpr> polyExpr = jas.expr2IExprJAS(polynomial);
    IAST list = F.List();
    for (Map.Entry<ExpVector, IExpr> monomial : polyExpr.getMap().entrySet()) {
        IExpr coeff = monomial.getValue();
        ExpVector exp = monomial.getKey();
        IAST monomTimes = F.Times();
        jas.monomialToExpr(coeff, exp, monomTimes);
        list.append(monomTimes);
    }
    return list;
}
Also used : JASIExpr(org.matheclipse.core.convert.JASIExpr) ExpVector(edu.jas.poly.ExpVector) JASIExpr(org.matheclipse.core.convert.JASIExpr) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST) Map(java.util.Map)

Example 2 with JASIExpr

use of org.matheclipse.core.convert.JASIExpr in project symja_android_library by axkr.

the class CoefficientRules method coefficientRules.

/**
	 * Get exponent vectors and coefficients of monomials of a polynomial
	 * expression.
	 * 
	 * @param polynomial
	 * @param variable
	 * @param termOrder
	 *            the JAS term ordering
	 * @return the list of monomials of the univariate polynomial.
	 */
public static IAST coefficientRules(IExpr polynomial, final List<IExpr> variablesList, final TermOrder termOrder) throws JASConversionException {
    JASIExpr jas = new JASIExpr(variablesList, ExprRingFactory.CONST, termOrder, false);
    GenPolynomial<IExpr> polyExpr = jas.expr2IExprJAS(polynomial);
    IAST resultList = F.List();
    for (Monomial<IExpr> monomial : polyExpr) {
        IAST ruleList = F.List();
        IExpr coeff = monomial.coefficient();
        ExpVector exp = monomial.exponent();
        int len = exp.length();
        for (int i = 0; i < len; i++) {
            ruleList.append(F.integer(exp.getVal(len - i - 1)));
        }
        resultList.append(F.Rule(ruleList, coeff));
    }
    return resultList;
}
Also used : JASIExpr(org.matheclipse.core.convert.JASIExpr) ExpVector(edu.jas.poly.ExpVector) JASIExpr(org.matheclipse.core.convert.JASIExpr) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST)

Example 3 with JASIExpr

use of org.matheclipse.core.convert.JASIExpr 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)

Aggregations

JASIExpr (org.matheclipse.core.convert.JASIExpr)3 IAST (org.matheclipse.core.interfaces.IAST)3 IExpr (org.matheclipse.core.interfaces.IExpr)3 ExpVector (edu.jas.poly.ExpVector)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 Map (java.util.Map)1 JASConvert (org.matheclipse.core.convert.JASConvert)1 VariablesSet (org.matheclipse.core.convert.VariablesSet)1 IComplex (org.matheclipse.core.interfaces.IComplex)1 PolynomialHomogenization (org.matheclipse.core.polynomials.PolynomialHomogenization)1 ExprPolynomial (org.matheclipse.core.polynomials.longexponent.ExprPolynomial)1 ExprPolynomialRing (org.matheclipse.core.polynomials.longexponent.ExprPolynomialRing)1