Search in sources :

Example 6 with ExprMonomial

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

the class MinMaxFunctions method minimizeCubicPolynomial.

private static IAST minimizeCubicPolynomial(ExprPolynomial polynomial, IExpr x) {
    long varDegree = polynomial.degree(0);
    IExpr a;
    IExpr b;
    IExpr c;
    IExpr d;
    IExpr e;
    if (varDegree <= 3) {
        // solve cubic or quadratic minimize:
        a = F.C0;
        b = F.C0;
        c = F.C0;
        d = F.C0;
        e = F.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("Minimize::Unexpected exponent value: " + lExp);
            }
        }
        if (a.isPossibleZero(false)) {
            if (b.isPossibleZero(false)) {
                // quadratic
                if (c.isPossibleZero(false)) {
                    if (d.isPossibleZero(false)) {
                        return F.list(e, F.CEmptyList);
                    } else {
                        // linear
                        return F.list(F.Piecewise(F.list(F.list(e, F.Equal(d, F.C0))), F.CNInfinity), F.list(F.Rule(x, F.Piecewise(F.list(F.list(F.C0, F.Equal(d, F.C0))), S.Indeterminate))));
                    }
                } else {
                    return F.List(F.Piecewise(F.list(F.list(e, F.And(F.Equal(d, 0), F.GreaterEqual(c, 0))), F.list(F.Times(F.C1D4, F.Power(c, -1), F.Plus(F.Times(-1, F.Power(d, 2)), F.Times(4, c, e))), F.Or(F.And(F.Greater(d, 0), F.Greater(c, 0)), F.And(F.Less(d, 0), F.Greater(c, 0))))), F.CNInfinity), F.list(F.Rule(x, F.Piecewise(F.list(F.list(F.Times(F.CN1D2, F.Power(c, -1), d), F.Or(F.And(F.Greater(d, 0), F.Greater(c, 0)), F.And(F.Less(d, 0), F.Greater(c, 0)))), F.list(F.C0, F.And(F.Equal(d, 0), F.GreaterEqual(c, 0)))), S.Indeterminate))));
                }
            } else {
                // cubic
                return F.list(F.Piecewise(F.list(F.list(e, F.Or(F.And(F.Equal(d, F.C0), F.Equal(c, F.C0), F.Equal(b, F.C0)), F.And(F.Equal(d, F.C0), F.Greater(c, F.C0), F.Equal(b, F.C0)))), F.list(F.Times(F.C1D4, F.Power(c, F.CN1), F.Plus(F.Negate(F.Sqr(d)), F.Times(F.C4, c, e))), F.Or(F.And(F.Greater(d, F.C0), F.Greater(c, F.C0), F.Equal(b, F.C0)), F.And(F.Less(d, F.C0), F.Greater(c, F.C0), F.Equal(b, F.C0))))), F.Noo), F.list(F.Rule(x, F.Piecewise(F.list(F.list(F.Times(F.CN1D2, F.Power(c, F.CN1), d), F.Or(F.And(F.Greater(d, F.C0), F.Greater(c, F.C0), F.Equal(b, F.C0)), F.And(F.Less(d, F.C0), F.Greater(c, F.C0), F.Equal(b, F.C0)))), F.list(F.C0, F.Or(F.And(F.Equal(d, F.C0), F.Equal(c, F.C0), F.Equal(b, F.C0)), F.And(F.Equal(d, F.C0), F.Greater(c, F.C0), F.Equal(b, F.C0))))), F.Indeterminate))));
            }
        }
    }
    return F.NIL;
}
Also used : IExpr(org.matheclipse.core.interfaces.IExpr) ExprMonomial(org.matheclipse.core.polynomials.longexponent.ExprMonomial)

Example 7 with ExprMonomial

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

the class MinMaxFunctions method maximizeCubicPolynomial.

private static IAST maximizeCubicPolynomial(ExprPolynomial polynomial, IExpr x) {
    long varDegree = polynomial.degree(0);
    IExpr a;
    IExpr b;
    IExpr c;
    IExpr d;
    IExpr e;
    if (varDegree <= 3) {
        // solve cubic or quadratic maximize:
        a = F.C0;
        b = F.C0;
        c = F.C0;
        d = F.C0;
        e = F.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("Maximize::Unexpected exponent value: " + lExp);
            }
        }
        if (a.isPossibleZero(false)) {
            if (b.isPossibleZero(false)) {
                // quadratic
                if (c.isPossibleZero(false)) {
                    if (d.isPossibleZero(false)) {
                        return F.list(e, F.CEmptyList);
                    } else {
                        // linear
                        return F.list(F.Piecewise(F.list(F.list(e, F.Equal(d, F.C0))), F.CInfinity), F.list(F.Rule(x, F.Piecewise(F.list(F.list(F.C0, F.Equal(d, F.C0))), S.Indeterminate))));
                    }
                } else {
                    return F.List(F.Piecewise(F.list(F.list(e, F.And(F.Equal(d, 0), F.LessEqual(c, 0))), F.list(F.Times(F.C1D4, F.Power(c, -1), F.Plus(F.Times(-1, F.Power(d, 2)), F.Times(4, c, e))), F.Or(F.And(F.Greater(d, 0), F.Less(c, 0)), F.And(F.Less(d, 0), F.Less(c, 0))))), F.CInfinity), F.list(F.Rule(x, F.Piecewise(F.list(F.list(F.Times(F.CN1D2, F.Power(c, -1), d), F.Or(F.And(F.Greater(d, 0), F.Less(c, 0)), F.And(F.Less(d, 0), F.Less(c, 0)))), F.list(F.C0, F.And(F.Equal(d, 0), F.LessEqual(c, 0)))), S.Indeterminate))));
                }
            } else {
                // cubic
                return F.list(F.Piecewise(F.list(F.list(e, F.Or(F.And(F.Equal(d, F.C0), F.Equal(c, F.C0), F.Equal(b, F.C0)), F.And(F.Equal(d, F.C0), F.Less(c, F.C0), F.Equal(b, F.C0)))), F.list(F.Times(F.C1D4, F.Power(c, F.CN1), F.Plus(F.Negate(F.Sqr(d)), F.Times(F.C4, c, e))), F.Or(F.And(F.Greater(d, F.C0), F.Less(c, F.C0), F.Equal(b, F.C0)), F.And(F.Less(d, F.C0), F.Less(c, F.C0), F.Equal(b, F.C0))))), F.oo), F.list(F.Rule(x, F.Piecewise(F.list(F.list(F.Times(F.CN1D2, F.Power(c, F.CN1), d), F.Or(F.And(F.Greater(d, F.C0), F.Less(c, F.C0), F.Equal(b, F.C0)), F.And(F.Less(d, F.C0), F.Less(c, F.C0), F.Equal(b, F.C0)))), F.list(F.C0, F.Or(F.And(F.Equal(d, F.C0), F.Equal(c, F.C0), F.Equal(b, F.C0)), F.And(F.Equal(d, F.C0), F.Less(c, F.C0), F.Equal(b, F.C0))))), F.Indeterminate))));
            }
        }
    }
    return F.NIL;
}
Also used : IExpr(org.matheclipse.core.interfaces.IExpr) ExprMonomial(org.matheclipse.core.polynomials.longexponent.ExprMonomial)

Example 8 with ExprMonomial

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

the class RootsFunctions 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 IASTAppendable rootsOfQuarticPolynomial(ExprPolynomial polynomial) {
    long varDegree = polynomial.degree(0);
    if (polynomial.isConstant()) {
        return F.ListAlloc(0);
    }
    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;
            }
        }
        IASTAppendable result = QuarticSolver.quarticSolve(a, b, c, d, e);
        if (result.isPresent()) {
            return (IASTAppendable) QuarticSolver.sortASTArguments(result);
        }
    }
    return F.NIL;
}
Also used : IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) IExpr(org.matheclipse.core.interfaces.IExpr) ExprMonomial(org.matheclipse.core.polynomials.longexponent.ExprMonomial)

Aggregations

IExpr (org.matheclipse.core.interfaces.IExpr)8 ExprMonomial (org.matheclipse.core.polynomials.longexponent.ExprMonomial)8 IAST (org.matheclipse.core.interfaces.IAST)4 IASTAppendable (org.matheclipse.core.interfaces.IASTAppendable)4 ExprEvaluator (org.matheclipse.core.eval.ExprEvaluator)2 ExprPolynomial (org.matheclipse.core.polynomials.longexponent.ExprPolynomial)2 ExprPolynomialRing (org.matheclipse.core.polynomials.longexponent.ExprPolynomialRing)2 SyntaxError (org.matheclipse.parser.client.SyntaxError)2 MathException (org.matheclipse.parser.client.math.MathException)2 EvalEngine (org.matheclipse.core.eval.EvalEngine)1 IASTMutable (org.matheclipse.core.interfaces.IASTMutable)1 IEvalStepListener (org.matheclipse.core.interfaces.IEvalStepListener)1 INumber (org.matheclipse.core.interfaces.INumber)1 IRational (org.matheclipse.core.interfaces.IRational)1