use of edu.jas.poly.GenPolynomial in project symja_android_library by axkr.
the class GroebnerBasis method computeGroebnerBasis.
/**
*
* @param listOfPolynomials
* a list of polynomials
* @param listOfVariables
* a list of variable symbols
* @param termOrder
* the term order
* @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
*/
private static IAST computeGroebnerBasis(IAST listOfPolynomials, IAST listOfVariables, TermOrder termOrder) {
List<ISymbol> varList = new ArrayList<ISymbol>(listOfVariables.size() - 1);
String[] pvars = new String[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));
pvars[i - 1] = ((ISymbol) listOfVariables.get(i)).toString();
}
List<GenPolynomial<BigRational>> polyList = new ArrayList<GenPolynomial<BigRational>>(listOfPolynomials.size() - 1);
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) {
return F.NIL;
}
}
if (polyList.size() == 0) {
return F.NIL;
}
GroebnerBasePartial<BigRational> gbp = new GroebnerBasePartial<BigRational>();
OptimizedPolynomialList<BigRational> opl = gbp.partialGB(polyList, pvars);
List<GenPolynomial<BigRational>> list = OrderedPolynomialList.sort(opl.list);
IAST resultList = F.List();
for (GenPolynomial<BigRational> p : list) {
// convert rational to integer coefficients and add
// polynomial to result list
resultList.append(jas.integerPoly2Expr((GenPolynomial<BigInteger>) jas.factorTerms(p)[2]));
}
return resultList;
}
use of edu.jas.poly.GenPolynomial in project symja_android_library by axkr.
the class JASConvert method integral2Expr.
/**
* Convert a jas <code>Integral</code> into a matheclipse expression
*
* @param integral
* the JAS Integral
* @return
*/
public IAST integral2Expr(Integral<BigRational> integral) {
IAST sum = F.Plus();
GenPolynomial<BigRational> pol = integral.pol;
List<GenPolynomial<BigRational>> rational = integral.rational;
List<LogIntegral<BigRational>> logarithm = integral.logarithm;
if (!pol.isZERO()) {
sum.append(rationalPoly2Expr(pol));
}
if (rational.size() != 0) {
int i = 0;
while (i < rational.size()) {
sum.append(F.Times(rationalPoly2Expr(rational.get(i++)), F.Power(rationalPoly2Expr(rational.get(i++)), F.CN1)));
}
}
if (logarithm.size() != 0) {
for (LogIntegral<BigRational> pf : logarithm) {
sum.append(logIntegral2Expr(pf));
}
}
return sum;
}
use of edu.jas.poly.GenPolynomial in project symja_android_library by axkr.
the class JASConvert method logIntegral2Expr.
/**
* Convert a jas <code>LogIntegral</code> into a matheclipse expression
*
* @param logIntegral
* the JAS LogIntegral
* @return
*/
public IAST logIntegral2Expr(LogIntegral<BigRational> logIntegral) {
IAST plus = F.Plus();
List<BigRational> cfactors = logIntegral.cfactors;
List<GenPolynomial<BigRational>> cdenom = logIntegral.cdenom;
List<AlgebraicNumber<BigRational>> afactors = logIntegral.afactors;
List<GenPolynomial<AlgebraicNumber<BigRational>>> adenom = logIntegral.adenom;
if (cfactors.size() > 0) {
for (int i = 0; i < cfactors.size(); i++) {
BigRational cp = cfactors.get(i);
GenPolynomial<BigRational> p = cdenom.get(i);
plus.append(F.Times(F.fraction(cp.numerator(), cp.denominator()), F.Log(rationalPoly2Expr(p))));
}
}
// TODO implement this conversion for AlgebraicNumbers...
if (afactors.size() > 0) {
for (int i = 0; i < afactors.size(); i++) {
AlgebraicNumber<BigRational> ap = afactors.get(i);
AlgebraicNumberRing<BigRational> ar = ap.factory();
GenPolynomial<AlgebraicNumber<BigRational>> p = adenom.get(i);
if (p.degree(0) < ar.modul.degree(0) && ar.modul.degree(0) > 2) {
}
GenPolynomial<BigRational> v = ap.getVal();
IAST times = F.Times();
if (p.degree(0) < ar.modul.degree(0) && ar.modul.degree(0) > 2) {
IAST rootOf = F.ast(F.RootOf);
rootOf.append(rationalPoly2Expr(ar.modul));
times.append(rootOf);
throw new UnsupportedOperationException("JASConvert#logIntegral2Expr()");
}
times.append(rationalPoly2Expr(v));
times.append(F.Log(polyAlgebraicNumber2Expr(p)));
plus.append(times);
}
}
return plus;
}
use of edu.jas.poly.GenPolynomial in project symja_android_library by axkr.
the class JASIExpr method expr2IExprPoly.
private GenPolynomial<IExpr> expr2IExprPoly(final IExpr exprPoly) throws ArithmeticException, ClassCastException {
if (exprPoly instanceof IAST) {
final IAST ast = (IAST) exprPoly;
GenPolynomial<IExpr> result = fPolyFactory.getZERO();
GenPolynomial<IExpr> p = fPolyFactory.getZERO();
if (ast.isPlus()) {
IExpr expr = ast.arg1();
result = expr2IExprPoly(expr);
for (int i = 2; i < ast.size(); i++) {
expr = ast.get(i);
p = expr2IExprPoly(expr);
result = result.sum(p);
}
return result;
} else if (ast.isTimes()) {
IExpr expr = ast.arg1();
result = expr2IExprPoly(expr);
for (int i = 2; i < ast.size(); i++) {
expr = ast.get(i);
p = expr2IExprPoly(expr);
result = result.multiply(p);
}
return result;
} else if (ast.isPower()) {
final IExpr expr = ast.arg1();
if (expr instanceof ISymbol) {
ExpVector leer = fPolyFactory.evzero;
int ix = leer.indexVar(expr.toString(), fPolyFactory.getVars());
if (ix >= 0) {
int exponent = -1;
try {
exponent = Validate.checkPowerExponent(ast);
} catch (WrongArgumentType e) {
//
}
if (exponent < 0) {
throw new ArithmeticException("JASConvert:expr2Poly - invalid exponent: " + ast.arg2().toString());
}
ExpVector e = ExpVector.create(fVariables.size(), ix, exponent);
return fPolyFactory.getONE().multiply(e);
}
}
} else if (fNumericFunction) {
if (ast.isNumericFunction()) {
return new GenPolynomial<IExpr>(fPolyFactory, ast);
}
}
} else if (exprPoly instanceof ISymbol) {
ExpVector leer = fPolyFactory.evzero;
int ix = leer.indexVar(exprPoly.toString(), fPolyFactory.getVars());
if (ix >= 0) {
ExpVector e = ExpVector.create(fVariables.size(), ix, 1L);
return fPolyFactory.getONE().multiply(e);
}
if (fNumericFunction) {
if (exprPoly.isNumericFunction()) {
return new GenPolynomial<IExpr>(fPolyFactory, exprPoly);
}
throw new ClassCastException(exprPoly.toString());
} else {
return new GenPolynomial<IExpr>(fPolyFactory, exprPoly);
}
} else if (exprPoly instanceof IInteger) {
return new GenPolynomial<IExpr>(fPolyFactory, exprPoly);
} else if (exprPoly instanceof IFraction) {
return new GenPolynomial<IExpr>(fPolyFactory, exprPoly);
}
if (exprPoly.isFree(t -> fVariables.contains(t), true)) {
return new GenPolynomial<IExpr>(fPolyFactory, exprPoly);
} else {
for (int i = 0; i < fVariables.size(); i++) {
if (fVariables.get(i).equals(exprPoly)) {
ExpVector e = ExpVector.create(fVariables.size(), i, 1L);
return fPolyFactory.getONE().multiply(e);
}
}
}
throw new ClassCastException(exprPoly.toString());
}
use of edu.jas.poly.GenPolynomial in project symja_android_library by axkr.
the class JASModInteger method expr2IExprPoly.
private GenPolynomial<ModLong> expr2IExprPoly(final IExpr exprPoly) throws ArithmeticException, ClassCastException {
if (exprPoly instanceof IAST) {
final IAST ast = (IAST) exprPoly;
GenPolynomial<ModLong> result = fPolyFactory.getZERO();
GenPolynomial<ModLong> p = fPolyFactory.getZERO();
if (ast.isPlus()) {
IExpr expr = ast.arg1();
result = expr2IExprPoly(expr);
for (int i = 2; i < ast.size(); i++) {
expr = ast.get(i);
p = expr2IExprPoly(expr);
result = result.sum(p);
}
return result;
} else if (ast.isTimes()) {
IExpr expr = ast.arg1();
result = expr2IExprPoly(expr);
for (int i = 2; i < ast.size(); i++) {
expr = ast.get(i);
p = expr2IExprPoly(expr);
result = result.multiply(p);
}
return result;
} else if (ast.isPower()) {
final IExpr expr = ast.arg1();
for (int i = 0; i < fVariables.size(); i++) {
if (fVariables.get(i).equals(expr)) {
int exponent = -1;
try {
exponent = Validate.checkPowerExponent(ast);
} catch (WrongArgumentType e) {
//
}
if (exponent < 0) {
throw new ArithmeticException("JASConvert:expr2Poly - invalid exponent: " + ast.arg2().toString());
}
ExpVector e = ExpVector.create(fVariables.size(), i, exponent);
return fPolyFactory.getONE().multiply(e);
}
}
}
} else if (exprPoly instanceof ISymbol) {
for (int i = 0; i < fVariables.size(); i++) {
if (fVariables.get(i).equals(exprPoly)) {
ExpVector e = ExpVector.create(fVariables.size(), i, 1L);
return fPolyFactory.getONE().multiply(e);
}
}
return new GenPolynomial(fPolyFactory, exprPoly);
} else if (exprPoly instanceof IInteger) {
return fPolyFactory.fromInteger((java.math.BigInteger) ((IInteger) exprPoly).asType(java.math.BigInteger.class));
} else if (exprPoly instanceof IFraction) {
return fraction2Poly((IFraction) exprPoly);
}
if (exprPoly.isFree(t -> fVariables.contains(t), true)) {
return new GenPolynomial(fPolyFactory, exprPoly);
} else {
for (int i = 0; i < fVariables.size(); i++) {
if (fVariables.get(i).equals(exprPoly)) {
ExpVector e = ExpVector.create(fVariables.size(), i, 1L);
return fPolyFactory.getONE().multiply(e);
}
}
}
throw new ClassCastException(exprPoly.toString());
}
Aggregations