/** * Calculate the result array <code> * [ poly1.divide(gcd(poly1, poly2)), poly2.divide(gcd(poly1, poly2)) ]</code> if the given * expressions <code>poly1</code> and <code>poly2</code> are univariate polynomials with equal * variable name. * * @param poly1 univariate polynomial * @param poly2 univariate polynomial * @return <code>null</code> if the expressions couldn't be converted to JAS polynomials */ public static IExpr[] cancelGCD(IExpr poly1, IExpr poly2) throws JASConversionException { try { ExprVariables eVar = new ExprVariables(poly1); eVar.addVarList(poly2); if (!eVar.isSize(1)) { // gcd only possible for univariate polynomials return null; } ASTRange r = new ASTRange(eVar.getVarList(), 1); JASConvert<BigRational> jas = new JASConvert<BigRational>(r.toList(), BigRational.ZERO); GenPolynomial<BigRational> p1 = jas.expr2JAS(poly1); GenPolynomial<BigRational> p2 = jas.expr2JAS(poly2); GenPolynomial<BigRational> gcd = p1.gcd(p2); IExpr[] result = new IExpr[2]; if (gcd.isONE()) { result[0] = jas.rationalPoly2Expr(p1); result[1] = jas.rationalPoly2Expr(p2); } else { result[0] = jas.rationalPoly2Expr(p1.divide(gcd)); result[1] = jas.rationalPoly2Expr(p2.divide(gcd)); } return result; } catch (Exception e) { if (Config.SHOW_STACKTRACE) { e.printStackTrace(); } } return null; }
public static boolean polynomialQ(final IExpr polnomialExpr, final IAST variables) { try { IExpr expr = F.evalExpandAll(polnomialExpr); ASTRange r = new ASTRange(variables, 1); JASConvert<IExpr> jas = new JASConvert<IExpr>(r.toList(), new ExprRingFactory()); return jas.expr2IExprJAS(expr) != null; } catch (JASConversionException e) { // exception will be thrown if the expression is not a JAS polynomial } return false; }
@Override public IExpr evaluate(final IAST lst) { if (lst.size() >= 3) { try { if (lst.get(1).isVector() < 0) { return null; } if (lst.get(2).isVector() < 0) { return null; } if (lst.size() == 3) { IAST vars = (IAST) lst.get(2); if (vars.size() <= 1) { return null; } List<ISymbol> varList = new ArrayList<ISymbol>(vars.size() - 1); String[] pvars = new String[vars.size() - 1]; for (int i = 1; i < vars.size(); i++) { if (!vars.get(i).isSymbol()) { return null; } varList.add((ISymbol) vars.get(i)); pvars[i - 1] = ((ISymbol) vars.get(i)).toString(); } GroebnerBasePartial<BigRational> gbp = new GroebnerBasePartial<BigRational>(); IAST polys = (IAST) lst.get(1); List<GenPolynomial<BigRational>> polyList = new ArrayList<GenPolynomial<BigRational>>(polys.size() - 1); JASConvert<BigRational> jas = new JASConvert<BigRational>(varList, BigRational.ZERO); for (int i = 1; i < polys.size(); i++) { IExpr expr = F.evalExpandAll(polys.get(i)); GenPolynomial<BigRational> poly = jas.expr2JAS(expr); polyList.add(poly); } OptimizedPolynomialList<BigRational> opl = gbp.partialGB(polyList, pvars); // System.out.println(opl); IAST resultList = F.List(); for (GenPolynomial<BigRational> p : opl.list) { // System.out.println(p); resultList.add(jas.poly2Expr(p, null)); } return resultList; } } catch (JASConversionException e) { if (Config.SHOW_STACKTRACE) { e.printStackTrace(); } } } return null; }