Search in sources :

Example 11 with ExprPolynomialRing

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

Example 12 with ExprPolynomialRing

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

the class RootsFunctions 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) {
    IASTMutable 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 = 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));
            }
        }
        result = QuarticSolver.sortASTArguments(result);
        return result;
    } catch (JASConversionException e2) {
        LOGGER.debug("RootsFunctions.rootsOfQuadraticExprPolynomial() failed", e2);
    }
    return result;
}
Also used : ExprPolynomialRing(org.matheclipse.core.polynomials.longexponent.ExprPolynomialRing) IASTMutable(org.matheclipse.core.interfaces.IASTMutable) 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