use of org.matheclipse.core.convert.JASIExpr in project symja_android_library by axkr.
the class MonomialList method monomialList.
/**
* Get the monomial list of a univariate polynomial.
*
* @param polynomial
* @param variable
* @param termOrder
* the JAS term ordering
* @return the list of monomials of the univariate polynomial.
*/
private static IAST monomialList(IExpr polynomial, final List<IExpr> variablesList, final TermOrder termOrder) throws JASConversionException {
JASIExpr jas = new JASIExpr(variablesList, ExprRingFactory.CONST, termOrder, false);
GenPolynomial<IExpr> polyExpr = jas.expr2IExprJAS(polynomial);
IAST list = F.List();
for (Map.Entry<ExpVector, IExpr> monomial : polyExpr.getMap().entrySet()) {
IExpr coeff = monomial.getValue();
ExpVector exp = monomial.getKey();
IAST monomTimes = F.Times();
jas.monomialToExpr(coeff, exp, monomTimes);
list.append(monomTimes);
}
return list;
}
use of org.matheclipse.core.convert.JASIExpr in project symja_android_library by axkr.
the class CoefficientRules method coefficientRules.
/**
* Get exponent vectors and coefficients of monomials of a polynomial
* expression.
*
* @param polynomial
* @param variable
* @param termOrder
* the JAS term ordering
* @return the list of monomials of the univariate polynomial.
*/
public static IAST coefficientRules(IExpr polynomial, final List<IExpr> variablesList, final TermOrder termOrder) throws JASConversionException {
JASIExpr jas = new JASIExpr(variablesList, ExprRingFactory.CONST, termOrder, false);
GenPolynomial<IExpr> polyExpr = jas.expr2IExprJAS(polynomial);
IAST resultList = F.List();
for (Monomial<IExpr> monomial : polyExpr) {
IAST ruleList = F.List();
IExpr coeff = monomial.coefficient();
ExpVector exp = monomial.exponent();
int len = exp.length();
for (int i = 0; i < len; i++) {
ruleList.append(F.integer(exp.getVal(len - i - 1)));
}
resultList.append(F.Rule(ruleList, coeff));
}
return resultList;
}
use of org.matheclipse.core.convert.JASIExpr 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;
}
Aggregations