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;
}
use of org.matheclipse.core.interfaces.IEvalStepListener in project symja_android_library by axkr.
the class FieldReducedRowEchelonForm method rowReduce.
/**
* Create the "reduced row echelon form" 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;
}
use of org.matheclipse.core.interfaces.IEvalStepListener in project symja_android_library by axkr.
the class RootsFunctions method rootsOfQuadraticPolynomial.
/**
* Solve a polynomial with degree <= 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;
}
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;
}
}
}
Aggregations