]> www.ginac.de Git - ginac.git/blobdiff - ginac/polynomial/mgcd.cpp
[PATCH 1/2] extract_integer_content: check for rational numbers properly.
[ginac.git] / ginac / polynomial / mgcd.cpp
index b0c1b70a23219c0e414642c1d1ee23e91fe7bfdd..7500805e4129cdf008226b50702c3ed39465fbf8 100644 (file)
@@ -3,7 +3,7 @@
  *  Chinese remainder algorithm. */
 
 /*
- *  GiNaC Copyright (C) 1999-2009 Johannes Gutenberg University Mainz, Germany
+ *  GiNaC Copyright (C) 1999-2010 Johannes Gutenberg University Mainz, Germany
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
 #include "primes_factory.h"
 #include "divide_in_z_p.h"
 #include "poly_cra.h"
+#include <numeric> // std::accumulate
 
 #include <cln/integer.h>
+#include <cln/integer_ring.h>
+#include <cln/rational.h>
+#include <cln/rational_ring.h>
 
 namespace GiNaC {
 
@@ -36,14 +40,23 @@ static cln::cl_I extract_integer_content(ex& Apr, const ex& A)
 {
        static const cln::cl_I n1(1);
        const numeric icont_ = A.integer_content();
-       const cln::cl_I icont = cln::the<cln::cl_I>(icont_.to_cl_N());
-       if (icont != 1) {
+       if (cln::instanceof(icont_.to_cl_N(), cln::cl_I_ring)) {
+               const cln::cl_I icont = cln::the<cln::cl_I>(icont_.to_cl_N());
+               if (icont != 1) {
+                       Apr = (A/icont_).expand();
+                       return icont;
+               } else {
+                       Apr = A;
+                       return n1;
+               }
+       }
+       if (cln::instanceof(icont_.to_cl_N(), cln::cl_RA_ring)) {
                Apr = (A/icont_).expand();
-               return icont;
-       } else {
-               Apr = A;
+               // A is a polynomail over rationals, so GCD is defined
+               // up to arbitrary rational number.
                return n1;
        }
+       GINAC_ASSERT(NULL == "expected polynomial over integers or rationals");
 }
 
 ex chinrem_gcd(const ex& A_, const ex& B_, const exvector& vars)
@@ -58,12 +71,15 @@ ex chinrem_gcd(const ex& A_, const ex& B_, const exvector& vars)
        const cln::cl_I g_lc = cln::gcd(a_lc, b_lc);
 
        const ex& x(vars.back());
-       int n = std::min(A.degree(x), B.degree(x));
+       exp_vector_t n = std::min(degree_vector(A, vars), degree_vector(B, vars));
+       const int nTot = std::accumulate(n.begin(), n.end(), 0);
        const cln::cl_I A_max_coeff = to_cl_I(A.max_coefficient()); 
        const cln::cl_I B_max_coeff = to_cl_I(B.max_coefficient());
-       const cln::cl_I lcoeff_limit = (cln::cl_I(1) << n)*cln::abs(g_lc)*
+
+       const cln::cl_I lcoeff_limit = (cln::cl_I(1) << nTot)*cln::abs(g_lc)*
                std::min(A_max_coeff, B_max_coeff);
 
+
        cln::cl_I q = 0;
        ex H = 0;
 
@@ -83,8 +99,8 @@ ex chinrem_gcd(const ex& A_, const ex& B_, const exvector& vars)
                const cln::cl_I Cp_lc = integer_lcoeff(Cp, vars);
                const cln::cl_I nlc = smod(recip(Cp_lc, p)*g_lcp, p);
                Cp = (Cp*numeric(nlc)).expand().smod(pnum);
-               int cp_deg = Cp.degree(x);
-               if (cp_deg == 0)
+               exp_vector_t cp_deg = degree_vector(Cp, vars);
+               if (zerop(cp_deg))
                        return numeric(g_lc);
                if (zerop(q)) {
                        H = Cp;