Search in sources :

Example 1 with IEvalStepListener

use of org.matheclipse.core.interfaces.IEvalStepListener 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 IEvalStepListener

use of org.matheclipse.core.interfaces.IEvalStepListener in project symja_android_library by axkr.

the class FieldReducedRowEchelonForm method rowReduce.

/**
	 * Create the &quot;reduced row echelon form&quot; of a matrix.
	 * 
	 * See: <a href="http://en.wikipedia.org/wiki/Row_echelon_form">Wikipedia -
	 * Row echelon form</a>.
	 * 
	 * @return
	 */
private FieldMatrix<IExpr> rowReduce() {
    int maxRows = numRows;
    RowColIndex pivot = new RowColIndex(0, 0);
    int submatrix = 0;
    for (int x = 0; x < numCols; x++) {
        pivot = new RowColIndex(pivot.row, x);
        // The pivot position is at the top.
        for (int i = x; i < numCols; i++) {
            if (isColumnZeroFromRow(pivot) == false) {
                break;
            } else {
                pivot.col = i;
            }
        }
        // Step 2
        // Select a nonzero entry in the pivot column with the highest
        // absolute value as a pivot.
        pivot = findPivot(pivot);
        if (isZero(getCoordinate(pivot))) {
            pivot.row++;
            if (pivot.row >= maxRows) {
                break;
            }
            continue;
        }
        // move this row to the top of the submatrix
        if (pivot.row != submatrix) {
            swapRow(new RowColIndex(submatrix, pivot.col), pivot);
        }
        // Force pivot to be 1
        if (!isOne(getCoordinate(pivot))) {
            IExpr scalar = getCoordinate(pivot).inverse();
            scaleRow(pivot, scalar);
        }
        // belowPivot = belowPivot + (Pivot * -belowPivot)
        for (int i = pivot.row; i < numRows; i++) {
            if (i == pivot.row) {
                continue;
            }
            RowColIndex belowPivot = new RowColIndex(i, pivot.col);
            IExpr complement = (getCoordinate(belowPivot).negate().divide(getCoordinate(pivot)));
            multiplyAdd(belowPivot, pivot, complement);
        }
        // above the pivot
        for (int i = pivot.row; i >= 0; i--) {
            if (i == pivot.row) {
                if (!isOne(getCoordinate(pivot))) {
                    scaleRow(pivot, getCoordinate(pivot).inverse());
                }
                continue;
            }
            if (i == pivot.row) {
                continue;
            }
            RowColIndex abovePivot = new RowColIndex(i, pivot.col);
            IExpr complement = (getCoordinate(abovePivot).negate().divide(getCoordinate(pivot)));
            multiplyAdd(abovePivot, pivot, complement);
        }
        // are no more nonzero entries.
        if ((pivot.row + 1) >= maxRows) {
            // {
            break;
        }
        submatrix++;
        pivot.row++;
    }
    EvalEngine engine = EvalEngine.get();
    IEvalStepListener listener = engine.getStepListener();
    if (listener != null) {
        listener.add(Convert.matrix2List(originalMatrix), Convert.matrix2List(rowReducedMatrix), engine.getRecursionCounter(), -1, "ReducedRowEchelonForm");
    }
    return rowReducedMatrix;
}
Also used : EvalEngine(org.matheclipse.core.eval.EvalEngine) IEvalStepListener(org.matheclipse.core.interfaces.IEvalStepListener) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 3 with IEvalStepListener

use of org.matheclipse.core.interfaces.IEvalStepListener in project symja_android_library by axkr.

the class RootsFunctions method rootsOfQuadraticPolynomial.

/**
 * Solve a polynomial with degree &lt;= 2.
 *
 * @param polynomial the polynomial
 * @return <code>F.NIL</code> if no evaluation was possible.
 */
private static IASTAppendable rootsOfQuadraticPolynomial(ExprPolynomial polynomial) {
    long varDegree = polynomial.degree(0);
    if (polynomial.isConstant()) {
        return F.ListAlloc(1);
    }
    IExpr a;
    IExpr b;
    IExpr c;
    IExpr d;
    IExpr e;
    if (varDegree <= 2) {
        IEvalStepListener listener = EvalEngine.get().getStepListener();
        if (listener != null) {
            IASTAppendable 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);
            }
        }
        IASTAppendable result = QuarticSolver.quarticSolve(a, b, c, d, e);
        if (result.isPresent()) {
            result = (IASTAppendable) QuarticSolver.sortASTArguments(result);
            return result;
        }
    }
    return F.NIL;
}
Also used : IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) IEvalStepListener(org.matheclipse.core.interfaces.IEvalStepListener) IExpr(org.matheclipse.core.interfaces.IExpr) ExprMonomial(org.matheclipse.core.polynomials.longexponent.ExprMonomial)

Example 4 with IEvalStepListener

use of org.matheclipse.core.interfaces.IEvalStepListener in project symja_android_library by axkr.

the class PatternMatcherAndEvaluator method checkRHSCondition.

/**
 * Check if the condition for the right-hand-sides <code>Module[], With[] or Condition[]</code>
 * expressions evaluates to <code>true</code>.
 *
 * @return <code>true</code> if the right-hand-sides condition is fulfilled or not all patterns
 *         are assigned.
 */
@Override
public boolean checkRHSCondition(EvalEngine engine) {
    IPatternMap patternMap = createPatternMap();
    if (patternMap.getRHSEvaluated()) {
        return true;
    }
    if (!(fRightHandSide.isCondition() || fRightHandSide.isModuleOrWithCondition())) {
        return true;
    } else {
        if (!patternMap.isAllPatternsAssigned()) {
            return true;
        } else {
            boolean matched = false;
            IExpr rhs = patternMap.substituteSymbols(fRightHandSide, F.CEmptySequence);
            engine.pushOptionsStack();
            IEvalStepListener stepListener = engine.getStepListener();
            try {
                engine.setOptionsPattern(fLhsPatternExpr.topHead(), patternMap);
                if (Config.TRACE_REWRITE_RULE && stepListener != null) {
                    IExpr lhs = getLHSExprToMatch();
                    if (lhs.isPresent()) {
                        stepListener.setUp(lhs, 0);
                        fReturnResult = engine.addEvaluatedTraceStep(lhs, rhs, lhs.topHead(), F.$str("RewriteRule"));
                    } else {
                        fReturnResult = engine.evaluate(rhs);
                    }
                } else {
                    fReturnResult = engine.evaluate(rhs);
                }
                matched = true;
            } catch (final ConditionException e) {
                matched = false;
            } catch (final ReturnException e) {
                fReturnResult = e.getValue();
                matched = true;
            } finally {
                engine.popOptionsStack();
            }
            if (Config.TRACE_REWRITE_RULE && stepListener != null) {
                stepListener.tearDown(0, matched);
            }
            patternMap.setRHSEvaluated(matched);
            return matched;
        }
    }
}
Also used : ConditionException(org.matheclipse.core.eval.exception.ConditionException) IEvalStepListener(org.matheclipse.core.interfaces.IEvalStepListener) IExpr(org.matheclipse.core.interfaces.IExpr) ReturnException(org.matheclipse.core.eval.exception.ReturnException)

Aggregations

IEvalStepListener (org.matheclipse.core.interfaces.IEvalStepListener)4 IExpr (org.matheclipse.core.interfaces.IExpr)4 EvalEngine (org.matheclipse.core.eval.EvalEngine)1 ConditionException (org.matheclipse.core.eval.exception.ConditionException)1 ReturnException (org.matheclipse.core.eval.exception.ReturnException)1 IAST (org.matheclipse.core.interfaces.IAST)1 IASTAppendable (org.matheclipse.core.interfaces.IASTAppendable)1 ExprMonomial (org.matheclipse.core.polynomials.ExprMonomial)1 ExprMonomial (org.matheclipse.core.polynomials.longexponent.ExprMonomial)1