Search in sources :

Example 6 with ExprPolynomialRing

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

the class Resultant method evaluate.

@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
    Validate.checkSize(ast, 4);
    // TODO allow multinomials
    IExpr arg3 = Validate.checkSymbolType(ast, 3);
    ISymbol x = (ISymbol) arg3;
    IExpr a = F.evalExpandAll(ast.arg1());
    IExpr b = F.evalExpandAll(ast.arg2());
    ExprPolynomialRing ring = new ExprPolynomialRing(F.List(x));
    try {
        // check if a is a polynomial otherwise check ArithmeticException,
        // ClassCastException
        ring.create(a);
    } catch (RuntimeException ex) {
        throw new WrongArgumentType(ast, a, 1, "Polynomial expected!");
    }
    try {
        // check if b is a polynomial otherwise check ArithmeticException,
        // ClassCastException
        ring.create(b);
        return F.Together(resultant(a, b, x, engine));
    } catch (RuntimeException ex) {
        throw new WrongArgumentType(ast, b, 2, "Polynomial expected!");
    }
}
Also used : ExprPolynomialRing(org.matheclipse.core.polynomials.ExprPolynomialRing) ISymbol(org.matheclipse.core.interfaces.ISymbol) WrongArgumentType(org.matheclipse.core.eval.exception.WrongArgumentType) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 7 with ExprPolynomialRing

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

Example 8 with ExprPolynomialRing

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

the class Coefficient method evaluate.

@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
    Validate.checkRange(ast, 3, 4);
    IExpr arg2 = ast.arg2();
    // list of variable expressions extracted from the second argument
    IAST listOfVariables = null;
    // array of corresponding exponents for the list of variables
    long[] exponents = null;
    if (arg2.isTimes()) {
        // Times(x, y^a,...)
        IAST arg2AST = (IAST) arg2;
        VariablesSet eVar = new VariablesSet(arg2AST);
        listOfVariables = eVar.getVarList();
        exponents = new long[listOfVariables.size() - 1];
        for (int i = 0; i < exponents.length; i++) {
            exponents[i] = 0L;
        }
        for (int i = 1; i < arg2AST.size(); i++) {
            long value = 1L;
            IExpr a1 = arg2AST.get(i);
            if (arg2AST.get(i).isPower() && arg2AST.get(i).getAt(2).isInteger()) {
                a1 = arg2AST.get(i).getAt(1);
                IInteger ii = (IInteger) arg2AST.get(i).getAt(2);
                try {
                    value = ii.toLong();
                } catch (ArithmeticException ae) {
                    return F.NIL;
                }
            }
            if (!setExponent(listOfVariables, a1, exponents, value)) {
                return F.NIL;
            }
        }
    } else {
        listOfVariables = F.List();
        listOfVariables.append(arg2);
        exponents = new long[1];
        exponents[0] = 1;
    }
    try {
        long n = 1;
        if (ast.isAST3()) {
            if (ast.arg3().isNegativeInfinity()) {
                return F.C0;
            }
            n = Validate.checkLongType(ast.arg3());
            for (int i = 0; i < exponents.length; i++) {
                exponents[i] *= n;
            }
        }
        ExpVectorLong expArr = new ExpVectorLong(exponents);
        IExpr expr = F.evalExpandAll(ast.arg1());
        ExprPolynomialRing ring = new ExprPolynomialRing(ExprRingFactory.CONST, listOfVariables, listOfVariables.size() - 1);
        ExprPolynomial poly = ring.create(expr, true, true);
        return poly.coefficient(expArr);
    } catch (RuntimeException ae) {
        if (Config.DEBUG) {
            ae.printStackTrace();
        }
        return F.C0;
    }
}
Also used : ExpVectorLong(org.matheclipse.core.polynomials.ExpVectorLong) ExprPolynomialRing(org.matheclipse.core.polynomials.ExprPolynomialRing) IInteger(org.matheclipse.core.interfaces.IInteger) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST) VariablesSet(org.matheclipse.core.convert.VariablesSet) ExprPolynomial(org.matheclipse.core.polynomials.ExprPolynomial)

Example 9 with ExprPolynomialRing

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

the class Roots method rootsOfQuadraticExprPolynomial.

/**
	 * Solve a polynomial with degree &lt;= 2.
	 * 
	 * @param expr
	 * @param varList
	 * @return <code>F.NIL</code> if no evaluation was possible.
	 */
private static IAST rootsOfQuadraticExprPolynomial(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);
        ePoly = ePoly.multiplyByMinimumNegativeExponents();
        result = rootsOfQuadraticPolynomial(ePoly);
        if (result.isPresent() && expr.isNumericMode()) {
            for (int i = 1; i < result.size(); i++) {
                result.set(i, F.chopExpr(result.get(i), Config.DEFAULT_ROOTS_CHOP_DELTA));
            }
        }
    } catch (JASConversionException e2) {
        if (Config.SHOW_STACKTRACE) {
            e2.printStackTrace();
        }
    }
    return result;
}
Also used : ExprPolynomialRing(org.matheclipse.core.polynomials.ExprPolynomialRing) IAST(org.matheclipse.core.interfaces.IAST) JASConversionException(org.matheclipse.core.eval.exception.JASConversionException) ExprPolynomial(org.matheclipse.core.polynomials.ExprPolynomial)

Example 10 with ExprPolynomialRing

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

the class Roots method rootsOfExprPolynomial.

private static IAST rootsOfExprPolynomial(final IExpr expr, IAST varList, boolean rootsOfQuartic) {
    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);
        ePoly = ePoly.multiplyByMinimumNegativeExponents();
        if (ePoly.degree(0) >= 3) {
            result = unitPolynomial(ePoly.degree(0), ePoly);
            if (result.isPresent()) {
                result = QuarticSolver.createSet(result);
                return result;
            }
        }
        if (!rootsOfQuartic && ePoly.degree(0) > 2) {
            return F.NIL;
        }
        result = rootsOfQuarticPolynomial(ePoly);
        if (result.isPresent()) {
            if (expr.isNumericMode()) {
                for (int i = 1; i < result.size(); i++) {
                    result.set(i, F.chopExpr(result.get(i), Config.DEFAULT_ROOTS_CHOP_DELTA));
                }
            }
            return result;
        }
    } catch (JASConversionException e2) {
        if (Config.SHOW_STACKTRACE) {
            e2.printStackTrace();
        }
    }
    return F.NIL;
}
Also used : ExprPolynomialRing(org.matheclipse.core.polynomials.ExprPolynomialRing) IAST(org.matheclipse.core.interfaces.IAST) JASConversionException(org.matheclipse.core.eval.exception.JASConversionException) ExprPolynomial(org.matheclipse.core.polynomials.ExprPolynomial)

Aggregations

ExprPolynomialRing (org.matheclipse.core.polynomials.ExprPolynomialRing)17 ExprPolynomial (org.matheclipse.core.polynomials.ExprPolynomial)15 IExpr (org.matheclipse.core.interfaces.IExpr)14 IAST (org.matheclipse.core.interfaces.IAST)12 WrongArgumentType (org.matheclipse.core.eval.exception.WrongArgumentType)5 VariablesSet (org.matheclipse.core.convert.VariablesSet)4 JASConversionException (org.matheclipse.core.eval.exception.JASConversionException)4 ISymbol (org.matheclipse.core.interfaces.ISymbol)4 JASIExpr (org.matheclipse.core.convert.JASIExpr)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