use of org.matheclipse.core.eval.exception.JASConversionException in project symja_android_library by axkr.
the class Roots method rootsOfQuadraticExprPolynomial.
/**
* Solve a polynomial with degree <= 2.
*
* @param expr
* @param varList
* @return <code>F.NIL</code> if no evaluation was possible.
*/
private static IAST rootsOfQuadraticExprPolynomial(final IExpr expr, IAST varList) {
IAST result = F.NIL;
try {
// try to generate a common expression polynomial
ExprPolynomialRing ring = new ExprPolynomialRing(ExprRingFactory.CONST, varList);
ExprPolynomial ePoly = ring.create(expr, false, false);
ePoly = ePoly.multiplyByMinimumNegativeExponents();
result = rootsOfQuadraticPolynomial(ePoly);
if (result.isPresent() && expr.isNumericMode()) {
for (int i = 1; i < result.size(); i++) {
result.set(i, F.chopExpr(result.get(i), Config.DEFAULT_ROOTS_CHOP_DELTA));
}
}
} catch (JASConversionException e2) {
if (Config.SHOW_STACKTRACE) {
e2.printStackTrace();
}
}
return result;
}
use of org.matheclipse.core.eval.exception.JASConversionException in project symja_android_library by axkr.
the class Roots method rootsOfExprPolynomial.
private static IAST rootsOfExprPolynomial(final IExpr expr, IAST varList, boolean rootsOfQuartic) {
IAST result = F.NIL;
try {
// try to generate a common expression polynomial
ExprPolynomialRing ring = new ExprPolynomialRing(ExprRingFactory.CONST, varList);
ExprPolynomial ePoly = ring.create(expr, false, false);
ePoly = ePoly.multiplyByMinimumNegativeExponents();
if (ePoly.degree(0) >= 3) {
result = unitPolynomial(ePoly.degree(0), ePoly);
if (result.isPresent()) {
result = QuarticSolver.createSet(result);
return result;
}
}
if (!rootsOfQuartic && ePoly.degree(0) > 2) {
return F.NIL;
}
result = rootsOfQuarticPolynomial(ePoly);
if (result.isPresent()) {
if (expr.isNumericMode()) {
for (int i = 1; i < result.size(); i++) {
result.set(i, F.chopExpr(result.get(i), Config.DEFAULT_ROOTS_CHOP_DELTA));
}
}
return result;
}
} catch (JASConversionException e2) {
if (Config.SHOW_STACKTRACE) {
e2.printStackTrace();
}
}
return F.NIL;
}
use of org.matheclipse.core.eval.exception.JASConversionException in project symja_android_library by axkr.
the class MonomialList method evaluate.
@Override
public IExpr evaluate(final IAST ast, final EvalEngine engine) {
Validate.checkRange(ast, 2, 5);
IExpr expr = F.evalExpandAll(ast.arg1());
VariablesSet eVar;
IAST symbolList = F.List();
List<IExpr> varList;
if (ast.isAST1()) {
// extract all variables from the polynomial expression
eVar = new VariablesSet(ast.arg1());
eVar.appendToList(symbolList.args());
varList = eVar.getArrayList();
} else {
symbolList = Validate.checkSymbolOrSymbolList(ast, 2);
varList = new ArrayList<IExpr>(symbolList.size() - 1);
for (int i = 1; i < symbolList.size(); i++) {
varList.add(symbolList.get(i));
}
}
TermOrder termOrder = TermOrderByName.Lexicographic;
try {
if (ast.size() > 3) {
if (ast.arg3() instanceof IStringX) {
// NegativeLexicographic
String orderStr = ast.arg3().toString();
termOrder = Options.getMonomialOrder(orderStr, termOrder);
}
final Options options = new Options(ast.topHead(), ast, 2, engine);
IExpr option = options.getOption("Modulus");
if (option.isSignedNumber()) {
return monomialListModulus(expr, varList, termOrder, option);
}
}
if (USE_JAS_POLYNOMIAL) {
return monomialList(expr, varList, termOrder);
} else {
ExprPolynomialRing ring = new ExprPolynomialRing(symbolList, new ExprTermOrder(termOrder.getEvord()));
ExprPolynomial poly = ring.create(expr);
return poly.monomialList();
}
} catch (JASConversionException jce) {
// toInt() conversion failed
if (Config.DEBUG) {
jce.printStackTrace();
}
}
return F.NIL;
}
use of org.matheclipse.core.eval.exception.JASConversionException in project symja_android_library by axkr.
the class CoefficientRules method evaluate.
@Override
public IExpr evaluate(final IAST ast, final EvalEngine engine) {
Validate.checkRange(ast, 2, 5);
IExpr expr = F.evalExpandAll(ast.arg1());
VariablesSet eVar;
IAST symbolList = F.List();
List<IExpr> varList;
if (ast.isAST1()) {
// extract all variables from the polynomial expression
eVar = new VariablesSet(ast.arg1());
eVar.appendToList(symbolList.args());
varList = eVar.getArrayList();
} else {
symbolList = Validate.checkSymbolOrSymbolList(ast, 2);
varList = new ArrayList<IExpr>(symbolList.size() - 1);
for (int i = 1; i < symbolList.size(); i++) {
varList.add((ISymbol) symbolList.get(i));
}
}
TermOrder termOrder = TermOrderByName.Lexicographic;
try {
if (ast.size() > 3) {
if (ast.arg3() instanceof IStringX) {
String orderStr = ast.arg3().toString();
termOrder = Options.getMonomialOrder(orderStr, termOrder);
}
final Options options = new Options(ast.topHead(), ast, 2, engine);
IExpr option = options.getOption("Modulus");
if (option.isSignedNumber()) {
return coefficientRulesModulus(expr, varList, termOrder, option);
}
}
if (MonomialList.USE_JAS_POLYNOMIAL) {
return coefficientRules(expr, varList, termOrder);
} else {
ExprPolynomialRing ring = new ExprPolynomialRing(symbolList, new ExprTermOrder(termOrder.getEvord()));
ExprPolynomial poly = ring.create(expr);
return poly.coefficientRules();
}
} catch (JASConversionException jce) {
// toInt() conversion failed
if (Config.DEBUG) {
jce.printStackTrace();
}
}
return F.NIL;
}
use of org.matheclipse.core.eval.exception.JASConversionException in project symja_android_library by axkr.
the class GroebnerBasis method solveGroebnerBasis.
/**
* Used in <code>Solve()</code> function to reduce the polynomial list of
* equations.
*
* @param listOfPolynomials
* a list of polynomials
* @param listOfVariables
* a list of variable symbols
* @return <code>F.NIL</code> if
* <code>stopUnevaluatedOnPolynomialConversionError==true</code> and
* one of the polynomials in <code>listOfPolynomials</code> are not
* convertible to JAS polynomials
*/
public static IAST solveGroebnerBasis(IAST listOfPolynomials, IAST listOfVariables) {
List<ISymbol> varList = new ArrayList<ISymbol>(listOfVariables.size() - 1);
for (int i = 1; i < listOfVariables.size(); i++) {
if (!listOfVariables.get(i).isSymbol()) {
return F.NIL;
}
varList.add((ISymbol) listOfVariables.get(i));
}
IAST rest = F.List();
List<GenPolynomial<BigRational>> polyList = new ArrayList<GenPolynomial<BigRational>>(listOfPolynomials.size() - 1);
TermOrder termOrder = TermOrderByName.IGRLEX;
JASConvert<BigRational> jas = new JASConvert<BigRational>(varList, BigRational.ZERO, termOrder);
for (int i = 1; i < listOfPolynomials.size(); i++) {
IExpr expr = F.evalExpandAll(listOfPolynomials.get(i));
try {
GenPolynomial<BigRational> poly = jas.expr2JAS(expr, false);
polyList.add(poly);
} catch (JASConversionException e) {
rest.append(expr);
}
}
if (polyList.size() == 0) {
return F.NIL;
}
GroebnerBaseAbstract<BigRational> engine = GBAlgorithmBuilder.<BigRational>polynomialRing(jas.getPolynomialRingFactory()).fractionFree().syzygyPairlist().build();
List<GenPolynomial<BigRational>> opl = engine.GB(polyList);
IAST resultList = F.List();
// polynomial to result list
for (GenPolynomial<BigRational> p : opl) {
resultList.append(jas.integerPoly2Expr((GenPolynomial<BigInteger>) jas.factorTerms(p)[2]));
}
for (int i = 1; i < rest.size(); i++) {
resultList.append(rest.get(i));
}
return resultList;
}
Aggregations