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;
}
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;
}
use of org.matheclipse.core.polynomials.ExprMonomial in project symja_android_library by axkr.
the class Roots method rootsOfQuarticPolynomial.
/**
* Solve a polynomial with degree <= 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;
}
Aggregations