use of edu.jas.poly.TermOrder in project symja_android_library by axkr.
the class Algebra method factorComplex.
public static IAST factorComplex(GenPolynomial<BigRational> polyRat, JASConvert<BigRational> jas, List<IExpr> varList, ISymbol head, boolean noGCDLCM) {
TermOrder termOrder = TermOrderByName.Lexicographic;
// Object[] objects = jas.factorTerms(polyRat);
String[] vars = new String[varList.size()];
for (int i = 0; i < varList.size(); i++) {
vars[i] = varList.get(i).toString();
}
Object[] objects = JASConvert.rationalFromRationalCoefficientsFactor(new GenPolynomialRing<BigRational>(BigRational.ZERO, varList.size(), termOrder, vars), polyRat);
java.math.BigInteger gcd = (java.math.BigInteger) objects[0];
java.math.BigInteger lcm = (java.math.BigInteger) objects[1];
GenPolynomial<BigRational> poly = (GenPolynomial<BigRational>) objects[2];
ComplexRing<BigRational> cfac = new ComplexRing<BigRational>(BigRational.ZERO);
GenPolynomialRing<Complex<BigRational>> cpfac = new GenPolynomialRing<Complex<BigRational>>(cfac, 1, termOrder);
GenPolynomial<Complex<BigRational>> a = PolyUtil.complexFromAny(cpfac, poly);
FactorComplex<BigRational> factorAbstract = new FactorComplex<BigRational>(cfac);
SortedMap<GenPolynomial<Complex<BigRational>>, Long> map = factorAbstract.factors(a);
IAST result = F.ast(head);
if (!noGCDLCM) {
if (!gcd.equals(java.math.BigInteger.ONE) || !lcm.equals(java.math.BigInteger.ONE)) {
result.append(F.fraction(gcd, lcm));
}
}
GenPolynomial<Complex<BigRational>> temp;
for (SortedMap.Entry<GenPolynomial<Complex<BigRational>>, Long> entry : map.entrySet()) {
if (entry.getKey().isONE() && entry.getValue().equals(1L)) {
continue;
}
temp = entry.getKey();
result.append(F.Power(jas.complexPoly2Expr(entry.getKey()), F.integer(entry.getValue())));
}
return result;
}
use of edu.jas.poly.TermOrder in project symja_android_library by axkr.
the class Options method getMonomialOrder.
public TermOrder getMonomialOrder(final IAST ast, final TermOrder defaultTermOrder) {
TermOrder termOrder = defaultTermOrder;
IExpr option = getOption("MonomialOrder");
if (option.isSymbol()) {
String orderStr = option.toString();
termOrder = getMonomialOrder(orderStr, termOrder);
}
return termOrder;
}
use of edu.jas.poly.TermOrder 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 edu.jas.poly.TermOrder 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 edu.jas.poly.TermOrder 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