use of org.matheclipse.core.polynomials.longexponent.ExprPolynomialRing in project symja_android_library by axkr.
the class ExprEvaluatorTest method testStringEval003.
public void testStringEval003() {
try {
ExprEvaluator util = new ExprEvaluator();
IExpr expr = util.eval("x^2+y+a*x+b*y+c");
assertEquals("c+a*x+x^2+y+b*y", expr.toString());
final IAST variables = F.List(F.symbol("x"), F.symbol("y"));
ExprPolynomialRing ring = new ExprPolynomialRing(ExprRingFactory.CONST, variables, variables.argSize(), ExprTermOrderByName.Lexicographic, false);
ExprPolynomial poly = ring.create(expr);
assertEquals("x^2 + a x + ( 1+b ) y + c ", poly.toString());
// x degree
assertEquals(2, poly.degree(0));
// y degree
assertEquals(1, poly.degree(1));
// show internal structure:
assertEquals("{{2,0}->1,{1,0}->a,{0,1}->1+b,{0,0}->c}", poly.coefficientRules().toString());
System.out.println(poly.coefficientRules());
for (ExprMonomial monomial : poly) {
System.out.println(monomial.toString());
}
} catch (SyntaxError e) {
// catch Symja parser errors here
System.out.println(e.getMessage());
} catch (MathException me) {
// catch Symja math errors here
System.out.println(me.getMessage());
} catch (Exception e) {
e.printStackTrace();
} catch (final StackOverflowError soe) {
System.out.println(soe.getMessage());
} catch (final OutOfMemoryError oome) {
System.out.println(oome.getMessage());
}
}
use of org.matheclipse.core.polynomials.longexponent.ExprPolynomialRing in project symja_android_library by axkr.
the class Algebra method cancelGCD.
/**
* Calculate the 3 elements result array
*
* <pre>
* [
* commonFactor,
* numeratorPolynomial.divide(gcd(numeratorPolynomial, denominatorPolynomial)),
* denominatorPolynomial.divide(gcd(numeratorPolynomial, denominatorPolynomial))
* ]
* </pre>
*
* for the given expressions <code>numeratorPolynomial</code> and <code>denominatorPolynomial
* </code>.
*
* @param numerator an expression which should be converted to JAS polynomial (using
* substitutions)
* @param denominator a expression which could be converted to JAS polynomial (using
* substitutions)
* @return <code>null</code> if the expressions couldn't be converted to JAS polynomials or gcd
* equals 1
* @throws JASConversionException
*/
public static IExpr[] cancelGCD(final IExpr numerator, final IExpr denominator) throws JASConversionException {
try {
if (denominator.isInteger() && numerator.isPlus()) {
IExpr[] result = Cancel.cancelPlusIntegerGCD((IAST) numerator, (IInteger) denominator);
if (result != null) {
return result;
}
}
VariablesSet eVar = new VariablesSet(numerator);
eVar.addVarList(denominator);
if (eVar.size() == 0) {
return null;
}
IAST vars = eVar.getVarList();
PolynomialHomogenization substitutions = new PolynomialHomogenization(vars, EvalEngine.get());
IExpr[] subst = substitutions.replaceForward(numerator, denominator);
IExpr numeratorPolynomial = subst[0];
IExpr denominatorPolynomial = subst[1];
if (substitutions.size() > 0) {
eVar.clear();
eVar.addAll(substitutions.substitutedVariablesSet());
vars = eVar.getVarList();
}
try {
ExprPolynomialRing ring = new ExprPolynomialRing(vars);
ExprPolynomial pol1 = ring.create(numeratorPolynomial);
ExprPolynomial pol2 = ring.create(denominatorPolynomial);
List<IExpr> varList = eVar.getVarList().copyTo();
JASIExpr jas = new JASIExpr(varList, true);
GenPolynomial<IExpr> p1 = jas.expr2IExprJAS(pol1);
GenPolynomial<IExpr> p2 = jas.expr2IExprJAS(pol2);
GreatestCommonDivisor<IExpr> engine;
engine = GCDFactory.getImplementation(ExprRingFactory.CONST);
GenPolynomial<IExpr> gcd = engine.gcd(p1, p2);
IExpr[] result = new IExpr[3];
if (gcd.isONE()) {
return null;
// result[0] = jas.exprPoly2Expr(gcd);
// result[1] = jas.exprPoly2Expr(p1);
// result[2] = jas.exprPoly2Expr(p2);
} else {
result[0] = F.C1;
result[1] = F.eval(jas.exprPoly2Expr(p1.divide(gcd)));
result[2] = F.eval(jas.exprPoly2Expr(p2.divide(gcd)));
}
result[0] = substitutions.replaceBackward(result[0]);
result[1] = substitutions.replaceBackward(result[1]);
result[2] = substitutions.replaceBackward(result[2]);
return result;
} catch (RuntimeException rex) {
}
List<IExpr> varList = eVar.getVarList().copyTo();
ComplexRing<BigRational> cfac = new ComplexRing<BigRational>(BigRational.ZERO);
JASConvert<Complex<BigRational>> jas = new JASConvert<Complex<BigRational>>(varList, cfac);
GenPolynomial<Complex<BigRational>> p1 = jas.expr2JAS(numeratorPolynomial, false);
GenPolynomial<Complex<BigRational>> p2 = jas.expr2JAS(denominatorPolynomial, false);
GreatestCommonDivisor<Complex<BigRational>> engine;
engine = GCDFactory.getImplementation(cfac);
GenPolynomial<Complex<BigRational>> gcd;
// if (numeratorPolynomial.isSymbol()||denominatorPolynomial.isSymbol() ) {
// gcd = jas.expr2IExprJAS(F.C1);
// }else {
gcd = engine.gcd(p1, p2);
// }
IExpr[] result = new IExpr[3];
if (gcd.isONE()) {
return null;
// result[0] = jas.complexPoly2Expr(gcd);
// result[1] = jas.complexPoly2Expr(p1);
// result[2] = jas.complexPoly2Expr(p2);
} else {
result[0] = F.C1;
result[1] = F.eval(jas.complexPoly2Expr(p1.divide(gcd)));
result[2] = F.eval(jas.complexPoly2Expr(p2.divide(gcd)));
}
result[0] = substitutions.replaceBackward(result[0]);
result[1] = substitutions.replaceBackward(result[1]);
result[2] = substitutions.replaceBackward(result[2]);
return result;
} catch (RuntimeException e) {
LOGGER.debug("Algebra.cancelGCD() failed", e);
}
return null;
}
use of org.matheclipse.core.polynomials.longexponent.ExprPolynomialRing in project symja_android_library by axkr.
the class DSolve method solveSingleODE.
private IExpr solveSingleODE(IExpr equation, IExpr xVar, IAST listOfVariables, IExpr C_1, EvalEngine engine) {
ExprPolynomialRing ring = new ExprPolynomialRing(ExprRingFactory.CONST, listOfVariables);
if (equation.isAST()) {
IASTAppendable eq = ((IAST) equation).copyAppendable();
if (!eq.isPlus()) {
// create artificial Plus(...) expression
eq = F.Plus(eq);
}
int j = 1;
IAST[] deriveExpr = null;
while (j < eq.size()) {
IAST[] temp = eq.get(j).isDerivativeAST1();
if (temp != null) {
if (deriveExpr != null) {
// expression
return F.NIL;
}
deriveExpr = temp;
// eliminate deriveExpr from Plus(...) expression
eq.remove(j);
continue;
}
j++;
}
if (deriveExpr != null) {
int order = derivativeOrder(deriveExpr);
if (order < 0) {
return F.NIL;
}
try {
ExprPolynomial poly = ring.create(eq.oneIdentity0(), false, true, false);
if (order == 1 && poly.degree() <= 1) {
IAST coeffs = poly.coefficientList();
// degree 0
IExpr q = coeffs.arg1();
IExpr p = F.C0;
if (poly.degree() == 1) {
// degree 1
p = coeffs.arg2();
}
return linearODE(p, q, xVar, C_1, engine);
}
} catch (RuntimeException rex) {
LOGGER.debug("DSolve.solveSingleODE() failed", rex);
}
}
}
return F.NIL;
}
use of org.matheclipse.core.polynomials.longexponent.ExprPolynomialRing in project symja_android_library by axkr.
the class PolynomialFunctions method coefficientList.
public static IAST coefficientList(IExpr expr, IAST listOfVariables) {
try {
ExprPolynomialRing ring = new ExprPolynomialRing(listOfVariables);
ExprPolynomial poly = ring.create(expr, true, false, true);
if (poly.isZero()) {
return F.CEmptyList;
}
return poly.coefficientList();
} catch (LimitException le) {
throw le;
} catch (RuntimeException ex) {
// org.matheclipse.core.polynomials.longexponent.ExprPolynomialRing.create()
LOGGER.debug("PolynomialFunctions.coefficientList() failed", ex);
}
if (listOfVariables.argSize() > 0) {
return F.Nest(S.List, expr, listOfVariables.argSize());
}
return F.NIL;
}
use of org.matheclipse.core.polynomials.longexponent.ExprPolynomialRing in project symja_android_library by axkr.
the class MinMaxFunctions method maximizeExprPolynomial.
private static IAST maximizeExprPolynomial(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, false);
ePoly = ePoly.multiplyByMinimumNegativeExponents();
result = maximizeCubicPolynomial(ePoly, varList.arg1());
// result = QuarticSolver.sortASTArguments(result);
return result;
} catch (ArithmeticException | JASConversionException e2) {
LOGGER.debug("MinMaxFunctions.maximizeExprPolynomial() failed", e2);
}
return result;
}
Aggregations