Search in sources :

Example 1 with ExprMonomial

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

the class Roots method rootsOfQuadraticPolynomial.

/**
	 * Solve a polynomial with degree <= 2.
	 * 
	 * @param polynomial
	 *            the polynomial
	 * @return <code>F.NIL</code> if no evaluation was possible.
	 */
private static IAST rootsOfQuadraticPolynomial(ExprPolynomial polynomial) {
    long varDegree = polynomial.degree(0);
    IAST result = List();
    if (polynomial.isConstant()) {
        return result;
    }
    IExpr a;
    IExpr b;
    IExpr c;
    IExpr d;
    IExpr e;
    if (varDegree <= 2) {
        IEvalStepListener listener = EvalEngine.get().getStepListener();
        if (listener != null) {
            IAST temp = listener.rootsOfQuadraticPolynomial(polynomial);
            if (temp.isPresent()) {
                return temp;
            }
        }
        // solve quadratic equation:
        a = C0;
        b = C0;
        c = C0;
        d = C0;
        e = C0;
        for (ExprMonomial monomial : polynomial) {
            IExpr coeff = monomial.coefficient();
            long lExp = monomial.exponent().getVal(0);
            if (lExp == 4) {
                a = coeff;
            } else if (lExp == 3) {
                b = coeff;
            } else if (lExp == 2) {
                c = coeff;
            } else if (lExp == 1) {
                d = coeff;
            } else if (lExp == 0) {
                e = coeff;
            } else {
                throw new ArithmeticException("Roots::Unexpected exponent value: " + lExp);
            }
        }
        result = QuarticSolver.quarticSolve(a, b, c, d, e);
        if (result.isPresent()) {
            result = QuarticSolver.createSet(result);
            return result;
        }
    }
    return F.NIL;
}
Also used : IEvalStepListener(org.matheclipse.core.interfaces.IEvalStepListener) IAST(org.matheclipse.core.interfaces.IAST) IExpr(org.matheclipse.core.interfaces.IExpr) ExprMonomial(org.matheclipse.core.polynomials.ExprMonomial)

Example 2 with ExprMonomial

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

the class Roots method unitPolynomial.

/**
	 * Solve polynomials of the form <code>a * x^varDegree + b == 0</code>
	 * 
	 * @param varDegree
	 * @param polynomial
	 * @return
	 */
private static IAST unitPolynomial(long varDegree, ExprPolynomial polynomial) {
    IExpr a;
    IExpr b;
    a = C0;
    b = C0;
    for (ExprMonomial monomial : polynomial) {
        IExpr coeff = monomial.coefficient();
        long lExp = monomial.exponent().getVal(0);
        if (lExp == varDegree) {
            a = coeff;
        } else if (lExp == 0) {
            b = coeff;
        } else {
            return F.NIL;
        }
    }
    IAST result = F.List();
    // a * x^varDegree + b
    if (!a.isOne()) {
        a = F.Power(a, F.fraction(-1, varDegree));
    }
    if (!b.isOne()) {
        b = F.Power(b, F.fraction(1, varDegree));
    }
    if ((varDegree & 0x0001) == 0x0001) {
        // odd
        for (int i = 1; i <= varDegree; i++) {
            result.append(F.Times(F.Power(F.CN1, i - 1), F.Power(F.CN1, F.fraction(i, varDegree)), b, a));
        }
    } else {
        // even
        long size = varDegree / 2;
        int k = 1;
        for (int i = 1; i <= size; i++) {
            result.append(F.Times(F.CN1, F.Power(F.CN1, F.fraction(k, varDegree)), b, a));
            result.append(F.Times(F.Power(F.CN1, F.fraction(k, varDegree)), b, a));
            k += 2;
        }
    }
    return result;
}
Also used : IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST) ExprMonomial(org.matheclipse.core.polynomials.ExprMonomial)

Example 3 with ExprMonomial

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

the class Roots method rootsOfQuarticPolynomial.

/**
	 * Solve a polynomial with degree &lt;= 4.
	 * 
	 * @param polynomial
	 *            the polynomial
	 * @return <code>F.NIL</code> if no evaluation was possible.
	 */
private static IAST rootsOfQuarticPolynomial(ExprPolynomial polynomial) {
    long varDegree = polynomial.degree(0);
    if (polynomial.isConstant()) {
        return List();
    }
    IExpr a;
    IExpr b;
    IExpr c;
    IExpr d;
    IExpr e;
    if (varDegree <= 4) {
        // solve quartic equation:
        a = C0;
        b = C0;
        c = C0;
        d = C0;
        e = C0;
        for (ExprMonomial monomial : polynomial) {
            IExpr coeff = monomial.coefficient();
            long lExp = monomial.exponent().getVal(0);
            if (lExp == 4) {
                a = coeff;
            } else if (lExp == 3) {
                b = coeff;
            } else if (lExp == 2) {
                c = coeff;
            } else if (lExp == 1) {
                d = coeff;
            } else if (lExp == 0) {
                e = coeff;
            } else {
                return F.NIL;
            }
        }
        IAST result = QuarticSolver.quarticSolve(a, b, c, d, e);
        if (result.isPresent()) {
            result = QuarticSolver.createSet(result);
            return result;
        }
    }
    return F.NIL;
}
Also used : IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST) ExprMonomial(org.matheclipse.core.polynomials.ExprMonomial)

Aggregations

IAST (org.matheclipse.core.interfaces.IAST)3 IExpr (org.matheclipse.core.interfaces.IExpr)3 ExprMonomial (org.matheclipse.core.polynomials.ExprMonomial)3 IEvalStepListener (org.matheclipse.core.interfaces.IEvalStepListener)1