Fixed various bugs in multivariate factorization.
[ginac.git] / ginac / factor.cpp
index 5489b4ceb79eb87a99ba36ef9e17e5dac96b3005..84b96eb8b07ec31612323c9e91425f5bc0d451fb 100644 (file)
  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  */
 
+//#define DEBUGFACTOR
+
+#ifdef DEBUGFACTOR
+#include <ostream>
+#include <ginac/ginac.h>
+using namespace GiNaC;
+#else
 #include "factor.h"
 
 #include "ex.h"
@@ -34,6 +41,7 @@
 #include "mul.h"
 #include "normal.h"
 #include "add.h"
+#endif
 
 #include <algorithm>
 #include <list>
@@ -43,13 +51,21 @@ using namespace std;
 #include <cln/cln.h>
 using namespace cln;
 
-//#define DEBUGFACTOR
-
 #ifdef DEBUGFACTOR
-#include <ostream>
-#endif // def DEBUGFACTOR
-
+namespace Factor {
+#else
 namespace GiNaC {
+#endif
+
+#ifdef DEBUGFACTOR
+#define DCOUT(str) cout << #str << endl
+#define DCOUTVAR(var) cout << #var << ": " << var << endl
+#define DCOUT2(str,var) cout << #str << ": " << var << endl
+#else
+#define DCOUT(str)
+#define DCOUTVAR(var)
+#define DCOUT2(str,var)
+#endif
 
 namespace {
 
@@ -108,8 +124,8 @@ struct UniPoly
                // assert: poly is in Z[x]
                Term t;
                for ( int i=poly.degree(x); i>=poly.ldegree(x); --i ) {
-                       int coeff = ex_to<numeric>(poly.coeff(x,i)).to_int();
-                       if ( coeff ) {
+                       cl_I coeff = the<cl_I>(ex_to<numeric>(poly.coeff(x,i)).to_cl_N());
+                       if ( !zerop(coeff) ) {
                                t.c = R->canonhom(coeff);
                                if ( !zerop(t.c) ) {
                                        t.exp = i;
@@ -118,6 +134,22 @@ struct UniPoly
                        }
                }
        }
+       UniPoly(const cl_modint_ring& ring, const UniPoly& poly) : R(ring)
+       { 
+               if ( R->modulus == poly.R->modulus ) {
+                       terms = poly.terms;
+               }
+               else {
+                       list<Term>::const_iterator i=poly.terms.begin(), end=poly.terms.end();
+                       for ( ; i!=end; ++i ) {
+                               terms.push_back(*i);
+                               terms.back().c = R->canonhom(poly.R->retract(i->c));
+                               if ( zerop(terms.back().c) ) {
+                                       terms.pop_back();
+                               }
+                       }
+               }
+       }
        UniPoly(const cl_modint_ring& ring, const Vec& v) : R(ring)
        {
                Term t;
@@ -231,6 +263,13 @@ struct UniPoly
                        }
                }
        }
+       void divide(const cl_I& x)
+       {
+               list<Term>::iterator i = terms.begin(), end = terms.end();
+               for ( ; i != end; ++i ) {
+                       i->c = cl_MI(R, the<cl_I>(R->retract(i->c) / x));
+               }
+       }
        void reduce_exponents(unsigned int prime)
        {
                list<Term>::iterator i = terms.begin(), end = terms.end();
@@ -356,18 +395,68 @@ static UniPoly operator-(const UniPoly& a, const UniPoly& b)
        return c;
 }
 
-static UniPoly operator-(const UniPoly& a)
+static UniPoly operator*(const UniPoly& a, const cl_MI& fac)
+{
+       unsigned int n = a.degree();
+       UniPoly c(a.R);
+       Term t;
+       for ( unsigned int i=0 ; i<=n; ++i ) {
+               t.c = a[i] * fac;
+               if ( !zerop(t.c) ) {
+                       t.exp = i;
+                       c.terms.push_front(t);
+               }
+       }
+       return c;
+}
+
+static UniPoly operator+(const UniPoly& a, const UniPoly& b)
 {
        list<Term>::const_iterator ia = a.terms.begin(), aend = a.terms.end();
+       list<Term>::const_iterator ib = b.terms.begin(), bend = b.terms.end();
        UniPoly c(a.R);
+       while ( ia != aend && ib != bend ) {
+               if ( ia->exp > ib->exp ) {
+                       c.terms.push_back(*ia);
+                       ++ia;
+               }
+               else if ( ia->exp < ib->exp ) {
+                       c.terms.push_back(*ib);
+                       ++ib;
+               }
+               else {
+                       Term t;
+                       t.exp = ia->exp;
+                       t.c = ia->c + ib->c;
+                       if ( !zerop(t.c) ) {
+                               c.terms.push_back(t);
+                       }
+                       ++ia; ++ib;
+               }
+       }
        while ( ia != aend ) {
                c.terms.push_back(*ia);
-               c.terms.back().c = -c.terms.back().c;
                ++ia;
        }
+       while ( ib != bend ) {
+               c.terms.push_back(*ib);
+               ++ib;
+       }
        return c;
 }
 
+// static UniPoly operator-(const UniPoly& a)
+// {
+//     list<Term>::const_iterator ia = a.terms.begin(), aend = a.terms.end();
+//     UniPoly c(a.R);
+//     while ( ia != aend ) {
+//             c.terms.push_back(*ia);
+//             c.terms.back().c = -c.terms.back().c;
+//             ++ia;
+//     }
+//     return c;
+// }
+
 #ifdef DEBUGFACTOR
 ostream& operator<<(ostream& o, const UniPoly& t)
 {
@@ -401,6 +490,17 @@ ostream& operator<<(ostream& o, const list<UniPoly>& t)
 
 typedef vector<UniPoly> UniPolyVec;
 
+#ifdef DEBUGFACTOR
+ostream& operator<<(ostream& o, const UniPolyVec& v)
+{
+       UniPolyVec::const_iterator i = v.begin(), end = v.end();
+       while ( i != end ) {
+               o << *i++ << " , " << endl;
+       }
+       return o;
+}
+#endif // def DEBUGFACTOR
+
 struct UniFactor
 {
        UniPoly p;
@@ -654,6 +754,46 @@ public:
                        i2 += c;
                }
        }
+       void mul_row(size_t row, const cl_MI x)
+       {
+               vector<cl_MI>::iterator i = m.begin() + row*c;
+               for ( size_t cc=0; cc<c; ++cc ) {
+                       *i = *i * x;
+                       ++i;
+               }
+       }
+       void sub_row(size_t row1, size_t row2, const cl_MI fac)
+       {
+               vector<cl_MI>::iterator i1 = m.begin() + row1*c;
+               vector<cl_MI>::iterator i2 = m.begin() + row2*c;
+               for ( size_t cc=0; cc<c; ++cc ) {
+                       *i1 = *i1 - *i2 * fac;
+                       ++i1;
+                       ++i2;
+               }
+       }
+       void switch_row(size_t row1, size_t row2)
+       {
+               cl_MI buf;
+               vector<cl_MI>::iterator i1 = m.begin() + row1*c;
+               vector<cl_MI>::iterator i2 = m.begin() + row2*c;
+               for ( size_t cc=0; cc<c; ++cc ) {
+                       buf = *i1; *i1 = *i2; *i2 = buf;
+                       ++i1;
+                       ++i2;
+               }
+       }
+       bool is_col_zero(size_t col) const
+       {
+               Vec::const_iterator i = m.begin() + col;
+               for ( size_t rr=0; rr<r; ++rr ) {
+                       if ( !zerop(*i) ) {
+                               return false;
+                       }
+                       i += c;
+               }
+               return true;
+       }
        bool is_row_zero(size_t row) const
        {
                Vec::const_iterator i = m.begin() + row*c;
@@ -681,6 +821,25 @@ private:
 };
 
 #ifdef DEBUGFACTOR
+Matrix operator*(const Matrix& m1, const Matrix& m2)
+{
+       const unsigned int r = m1.rowsize();
+       const unsigned int c = m2.colsize();
+       Matrix o(r,c,m1(0,0));
+
+       for ( size_t i=0; i<r; ++i ) {
+               for ( size_t j=0; j<c; ++j ) {
+                       cl_MI buf;
+                       buf = m1(i,0) * m2(0,j);
+                       for ( size_t k=1; k<c; ++k ) {
+                               buf = buf + m1(i,k)*m2(k,j);
+                       }
+                       o(i,j) = buf;
+               }
+       }
+       return o;
+}
+
 ostream& operator<<(ostream& o, const Matrix& m)
 {
        vector<cl_MI>::const_iterator i = m.m.begin(), end = m.m.end();
@@ -888,9 +1047,9 @@ static ex hensel_univar(const ex& a_, const ex& x, unsigned int p, const UniPoly
        for ( int i=a.degree(x); i>=a.ldegree(x); --i ) {
                maxcoeff += pow(abs(a.coeff(x, i)),2);
        }
-       cl_I normmc = ceiling1(the<cl_F>(cln::sqrt(ex_to<numeric>(maxcoeff).to_cl_N())));
-       unsigned int maxdegree = (u1_.degree() > w1_.degree()) ? u1_.degree() : w1_.degree();
-       unsigned int B = cl_I_to_uint(normmc * expt_pos(cl_I(2), maxdegree));
+       cl_I normmc = ceiling1(the<cl_R>(cln::sqrt(ex_to<numeric>(maxcoeff).to_cl_N())));
+       cl_I maxdegree = (u1_.degree() > w1_.degree()) ? u1_.degree() : w1_.degree();
+       cl_I B = normmc * expt_pos(cl_I(2), maxdegree);
 
        // step 1
        ex alpha = a.lcoeff(x);
@@ -898,7 +1057,7 @@ static ex hensel_univar(const ex& a_, const ex& x, unsigned int p, const UniPoly
        if ( gamma == 0 ) {
                gamma = alpha;
        }
-       unsigned int gamma_ui = ex_to<numeric>(abs(gamma)).to_int();
+       numeric gamma_ui = ex_to<numeric>(abs(gamma));
        a = a * gamma;
        UniPoly nu1 = u1_;
        nu1.unit_normal();
@@ -918,10 +1077,11 @@ static ex hensel_univar(const ex& a_, const ex& x, unsigned int p, const UniPoly
        ex u = replace_lc(u1.to_ex(x), x, gamma);
        ex w = replace_lc(w1.to_ex(x), x, alpha);
        ex e = expand(a - u * w);
-       unsigned int modulus = p;
+       numeric modulus = p;
+       const numeric maxmodulus = 2*numeric(B)*gamma_ui;
 
        // step 4
-       while ( !e.is_zero() && modulus < 2*B*gamma_ui ) {
+       while ( !e.is_zero() && modulus < maxmodulus ) {
                ex c = e / modulus;
                phi = expand(s.to_ex(x)*c);
                UniPoly sigmatilde(R, phi, x);
@@ -994,6 +1154,15 @@ public:
        size_t size() const { return n; }
        size_t size_first() const { return n-sum; }
        size_t size_second() const { return sum; }
+#ifdef DEBUGFACTOR
+       void get() const
+       {
+               for ( size_t i=0; i<k.size(); ++i ) {
+                       cout << k[i] << " ";
+               }
+               cout << endl;
+       }
+#endif
        bool next()
        {
                for ( size_t i=n-1; i>=1; --i ) {
@@ -1153,6 +1322,1001 @@ struct FindSymbolsMap : public map_function {
        }
 };
 
+struct EvalPoint
+{
+       ex x;
+       int evalpoint;
+};
+
+// forward declaration
+vector<ex> multivar_diophant(const vector<ex>& a_, const ex& x, const ex& c, const vector<EvalPoint>& I, unsigned int d, unsigned int p, unsigned int k);
+
+UniPolyVec multiterm_eea_lift(const UniPolyVec& a, const ex& x, unsigned int p, unsigned int k)
+{
+       DCOUT(multiterm_eea_lift);
+       DCOUTVAR(a);
+       DCOUTVAR(p);
+       DCOUTVAR(k);
+
+       const size_t r = a.size();
+       DCOUTVAR(r);
+       cl_modint_ring R = find_modint_ring(expt_pos(cl_I(p),k));
+       UniPoly fill(R);
+       UniPolyVec q(r-1, fill);
+       q[r-2] = a[r-1];
+       for ( size_t j=r-2; j>=1; --j ) {
+               q[j-1] = a[j] * q[j];
+       }
+       DCOUTVAR(q);
+       UniPoly beta(R);
+       beta.set(0, R->one());
+       UniPolyVec s;
+       for ( size_t j=1; j<r; ++j ) {
+               DCOUTVAR(j);
+               DCOUTVAR(beta);
+               vector<ex> mdarg(2);
+               mdarg[0] = q[j-1].to_ex(x);
+               mdarg[1] = a[j-1].to_ex(x);
+               vector<EvalPoint> empty;
+               vector<ex> exsigma = multivar_diophant(mdarg, x, beta.to_ex(x), empty, 0, p, k);
+               UniPoly sigma1(R, exsigma[0], x);
+               UniPoly sigma2(R, exsigma[1], x);
+               beta = sigma1;
+               s.push_back(sigma2);
+       }
+       s.push_back(beta);
+
+       DCOUTVAR(s);
+       DCOUT(END multiterm_eea_lift);
+       return s;
+}
+
+void eea_lift(const UniPoly& a, const UniPoly& b, const ex& x, unsigned int p, unsigned int k, UniPoly& s_, UniPoly& t_)
+{
+       DCOUT(eea_lift);
+       DCOUTVAR(a);
+       DCOUTVAR(b);
+       DCOUTVAR(x);
+       DCOUTVAR(p);
+       DCOUTVAR(k);
+
+       cl_modint_ring R = find_modint_ring(p);
+       UniPoly amod(R, a);
+       UniPoly bmod(R, b);
+       DCOUTVAR(amod);
+       DCOUTVAR(bmod);
+
+       UniPoly smod(R), tmod(R), g(R);
+       exteuclid(amod, bmod, g, smod, tmod);
+       
+       DCOUTVAR(smod);
+       DCOUTVAR(tmod);
+       DCOUTVAR(g);
+
+       cl_modint_ring Rpk = find_modint_ring(expt_pos(cl_I(p),k));
+       UniPoly s(Rpk, smod);
+       UniPoly t(Rpk, tmod);
+       DCOUTVAR(s);
+       DCOUTVAR(t);
+
+       cl_I modulus(p);
+
+       UniPoly one(Rpk);
+       one.set(0, Rpk->one());
+       for ( size_t j=1; j<k; ++j ) {
+               UniPoly e = one - a * s - b * t;
+               e.divide(modulus);
+               UniPoly c(R, e);
+               UniPoly sigmabar(R);
+               sigmabar = smod * c;
+               UniPoly taubar(R);
+               taubar = tmod * c;
+               UniPoly q(R);
+               div(sigmabar, bmod, q);
+               UniPoly sigma(R);
+               rem(sigmabar, bmod, sigma);
+               UniPoly tau(R);
+               tau = taubar + q * amod;
+               UniPoly sadd(Rpk, sigma);
+               cl_MI modmodulus(Rpk, modulus);
+               s = s + sadd * modmodulus;
+               UniPoly tadd(Rpk, tau);
+               t = t + tadd * modmodulus;
+               modulus = modulus * p;
+       }
+
+       s_ = s; t_ = t;
+
+       DCOUTVAR(s);
+       DCOUTVAR(t);
+       DCOUT2(check, a*s + b*t);
+       DCOUT(END eea_lift);
+}
+
+UniPolyVec univar_diophant(const UniPolyVec& a, const ex& x, unsigned int m, unsigned int p, unsigned int k)
+{
+       DCOUT(univar_diophant);
+       DCOUTVAR(a);
+       DCOUTVAR(x);
+       DCOUTVAR(m);
+       DCOUTVAR(p);
+       DCOUTVAR(k);
+
+       cl_modint_ring R = find_modint_ring(expt_pos(cl_I(p),k));
+
+       const size_t r = a.size();
+       UniPolyVec result;
+       if ( r > 2 ) {
+               UniPolyVec s = multiterm_eea_lift(a, x, p, k);
+               for ( size_t j=0; j<r; ++j ) {
+                       ex phi = expand(pow(x,m)*s[j].to_ex(x));
+                       UniPoly bmod(R, phi, x);
+                       UniPoly buf(R);
+                       rem(bmod, a[j], buf);
+                       result.push_back(buf);
+               }
+       }
+       else {
+               UniPoly s(R), t(R);
+               eea_lift(a[1], a[0], x, p, k, s, t);
+               ex phi = expand(pow(x,m)*s.to_ex(x));
+               UniPoly bmod(R, phi, x);
+               UniPoly buf(R);
+               rem(bmod, a[0], buf);
+               result.push_back(buf);
+               UniPoly q(R);
+               div(bmod, a[0], q);
+               phi = expand(pow(x,m)*t.to_ex(x));
+               UniPoly t1mod(R, phi, x);
+               buf = t1mod + q * a[1];
+               result.push_back(buf);
+       }
+
+       DCOUTVAR(result);
+       DCOUT(END univar_diophant);
+       return result;
+}
+
+struct make_modular_map : public map_function {
+       cl_modint_ring R;
+       make_modular_map(const cl_modint_ring& R_) : R(R_) { }
+       ex operator()(const ex& e)
+       {
+               if ( is_a<add>(e) || is_a<mul>(e) ) {
+                       return e.map(*this);
+               }
+               else if ( is_a<numeric>(e) ) {
+                       numeric mod(R->modulus);
+                       numeric halfmod = (mod-1)/2;
+                       cl_MI emod = R->canonhom(the<cl_I>(ex_to<numeric>(e).to_cl_N()));
+                       numeric n(R->retract(emod));
+                       if ( n > halfmod ) {
+                               return n-mod;
+                       }
+                       else {
+                               return n;
+                       }
+               }
+               return e;
+       }
+};
+
+static ex make_modular(const ex& e, const cl_modint_ring& R)
+{
+       make_modular_map map(R);
+       return map(e);
+}
+
+vector<ex> multivar_diophant(const vector<ex>& a_, const ex& x, const ex& c, const vector<EvalPoint>& I, unsigned int d, unsigned int p, unsigned int k)
+{
+       vector<ex> a = a_;
+
+       DCOUT(multivar_diophant);
+#ifdef DEBUGFACTOR
+       cout << "a ";
+       for ( size_t i=0; i<a.size(); ++i ) {
+               cout << a[i] << " ";
+       }
+       cout << endl;
+#endif
+       DCOUTVAR(x);
+       DCOUTVAR(c);
+#ifdef DEBUGFACTOR
+       cout << "I ";
+       for ( size_t i=0; i<I.size(); ++i ) {
+               cout << I[i].x << "=" << I[i].evalpoint << " ";
+       }
+       cout << endl;
+#endif
+       DCOUTVAR(d);
+       DCOUTVAR(p);
+       DCOUTVAR(k);
+
+       const cl_modint_ring R = find_modint_ring(expt_pos(cl_I(p),k));
+       const size_t r = a.size();
+       const size_t nu = I.size() + 1;
+       DCOUTVAR(r);
+       DCOUTVAR(nu);
+
+       vector<ex> sigma;
+       if ( nu > 1 ) {
+               ex xnu = I.back().x;
+               int alphanu = I.back().evalpoint;
+
+               ex A = 1;
+               for ( size_t i=0; i<r; ++i ) {
+                       A *= a[i];
+               }
+               vector<ex> b(r);
+               for ( size_t i=0; i<r; ++i ) {
+                       b[i] = normal(A / a[i]);
+               }
+
+               vector<ex> anew = a;
+               for ( size_t i=0; i<r; ++i ) {
+                       anew[i] = anew[i].subs(xnu == alphanu);
+               }
+               ex cnew = c.subs(xnu == alphanu);
+               vector<EvalPoint> Inew = I;
+               Inew.pop_back();
+               sigma = multivar_diophant(anew, x, cnew, Inew, d, p, k);
+               DCOUTVAR(sigma);
+
+               ex buf = c;
+               for ( size_t i=0; i<r; ++i ) {
+                       buf -= sigma[i] * b[i];
+               }
+               ex e = buf;
+               e = make_modular(e, R);
+
+               e = e.expand();
+               DCOUTVAR(e);
+               DCOUTVAR(d);
+               ex monomial = 1;
+               for ( size_t m=1; m<=d; ++m ) {
+                       DCOUTVAR(m);
+                       while ( !e.is_zero() && e.has(xnu) ) {
+                               monomial *= (xnu - alphanu);
+                               monomial = expand(monomial);
+                               DCOUTVAR(xnu);
+                               DCOUTVAR(alphanu);
+                               ex cm = e.diff(ex_to<symbol>(xnu), m).subs(xnu==alphanu) / factorial(m);
+                               DCOUTVAR(cm);
+                               if ( !cm.is_zero() ) {
+                                       vector<ex> delta_s = multivar_diophant(anew, x, cm, Inew, d, p, k);
+                                       DCOUTVAR(delta_s);
+                                       ex buf = e;
+                                       for ( size_t j=0; j<delta_s.size(); ++j ) {
+                                               delta_s[j] *= monomial;
+                                               sigma[j] += delta_s[j];
+                                               buf -= delta_s[j] * b[j];
+                                       }
+                                       e = buf.expand();
+                                       e = make_modular(e, R);
+                               }
+                       }
+               }
+       }
+       else {
+               DCOUT(uniterm left);
+               UniPolyVec amod;
+               for ( size_t i=0; i<a.size(); ++i ) {
+                       UniPoly up(R, a[i], x);
+                       amod.push_back(up);
+               }
+
+               sigma.insert(sigma.begin(), r, 0);
+               size_t nterms;
+               ex z;
+               if ( is_a<add>(c) ) {
+                       nterms = c.nops();
+                       z = c.op(0);
+               }
+               else {
+                       nterms = 1;
+                       z = c;
+               }
+               DCOUTVAR(nterms);
+               for ( size_t i=0; i<nterms; ++i ) {
+                       DCOUTVAR(z);
+                       int m = z.degree(x);
+                       DCOUTVAR(m);
+                       cl_I cm = the<cl_I>(ex_to<numeric>(z.lcoeff(x)).to_cl_N());
+                       DCOUTVAR(cm);
+                       UniPolyVec delta_s = univar_diophant(amod, x, m, p, k);
+                       cl_MI modcm;
+                       cl_I poscm = cm;
+                       while ( poscm < 0 ) {
+                               poscm = poscm + expt_pos(cl_I(p),k);
+                       }
+                       modcm = cl_MI(R, poscm);
+                       DCOUTVAR(modcm);
+                       for ( size_t j=0; j<delta_s.size(); ++j ) {
+                               delta_s[j] = delta_s[j] * modcm;
+                               sigma[j] = sigma[j] + delta_s[j].to_ex(x);
+                       }
+                       DCOUTVAR(delta_s);
+#ifdef DEBUGFACTOR
+                       cout << "STEP " << i << " sigma ";
+                       for ( size_t p=0; p<sigma.size(); ++p ) {
+                               cout << sigma[p] << " ";
+                       }
+                       cout << endl;
+#endif
+                       if ( nterms > 1 ) {
+                               z = c.op(i+1);
+                       }
+               }
+       }
+#ifdef DEBUGFACTOR
+       cout << "sigma ";
+       for ( size_t i=0; i<sigma.size(); ++i ) {
+               cout << sigma[i] << " ";
+       }
+       cout << endl;
+#endif
+
+       for ( size_t i=0; i<sigma.size(); ++i ) {
+               sigma[i] = make_modular(sigma[i], R);
+       }
+
+#ifdef DEBUGFACTOR
+       cout << "sigma ";
+       for ( size_t i=0; i<sigma.size(); ++i ) {
+               cout << sigma[i] << " ";
+       }
+       cout << endl;
+#endif
+       DCOUT(END multivar_diophant);
+       return sigma;
+}
+
+#ifdef DEBUGFACTOR
+ostream& operator<<(ostream& o, const vector<EvalPoint>& v)
+{
+       for ( size_t i=0; i<v.size(); ++i ) {
+               o << "(" << v[i].x << "==" << v[i].evalpoint << ") ";
+       }
+       return o;
+}
+#endif // def DEBUGFACTOR
+
+
+ex hensel_multivar(const ex& a, const ex& x, const vector<EvalPoint>& I, unsigned int p, const cl_I& l, const UniPolyVec& u, const vector<ex>& lcU)
+{
+       DCOUT(hensel_multivar);
+       DCOUTVAR(a);
+       DCOUTVAR(x);
+       DCOUTVAR(I);
+       DCOUTVAR(p);
+       DCOUTVAR(l);
+       DCOUTVAR(u);
+       DCOUTVAR(lcU);
+       const size_t nu = I.size() + 1;
+       const cl_modint_ring R = find_modint_ring(expt_pos(cl_I(p),l));
+
+       DCOUTVAR(nu);
+       
+       vector<ex> A(nu);
+       A[nu-1] = a;
+
+       for ( size_t j=nu; j>=2; --j ) {
+               ex x = I[j-2].x;
+               int alpha = I[j-2].evalpoint;
+               A[j-2] = A[j-1].subs(x==alpha);
+               A[j-2] = make_modular(A[j-2], R);
+       }
+
+#ifdef DEBUGFACTOR
+       cout << "A ";
+       for ( size_t i=0; i<A.size(); ++i) cout << A[i] << " ";
+       cout << endl;
+#endif
+
+       int maxdeg = a.degree(I.front().x);
+       for ( size_t i=1; i<I.size(); ++i ) {
+               int maxdeg2 = a.degree(I[i].x);
+               if ( maxdeg2 > maxdeg ) maxdeg = maxdeg2;
+       }
+       DCOUTVAR(maxdeg);
+
+       const size_t n = u.size();
+       DCOUTVAR(n);
+       vector<ex> U(n);
+       for ( size_t i=0; i<n; ++i ) {
+               U[i] = u[i].to_ex(x);
+       }
+#ifdef DEBUGFACTOR
+       cout << "U ";
+       for ( size_t i=0; i<U.size(); ++i) cout << U[i] << " ";
+       cout << endl;
+#endif
+
+       for ( size_t j=2; j<=nu; ++j ) {
+               DCOUTVAR(j);
+               vector<ex> U1 = U;
+               ex monomial = 1;
+               DCOUTVAR(U);
+               for ( size_t m=0; m<n; ++m) {
+                       if ( lcU[m] != 1 ) {
+                               ex coef = lcU[m];
+                               for ( size_t i=j-1; i<nu-1; ++i ) {
+                                       coef = coef.subs(I[i].x == I[i].evalpoint);
+                               }
+                               coef = expand(coef);
+                               coef = make_modular(coef, R);
+                               int deg = U[m].degree(x);
+                               U[m] = U[m] - U[m].lcoeff(x) * pow(x,deg) + coef * pow(x,deg);
+                       }
+               }
+               DCOUTVAR(U);
+               ex Uprod = 1;
+               for ( size_t i=0; i<n; ++i ) {
+                       Uprod *= U[i];
+               }
+               ex e = expand(A[j-1] - Uprod);
+               DCOUTVAR(e);
+
+               vector<EvalPoint> newI;
+               for ( size_t i=1; i<=j-2; ++i ) {
+                       newI.push_back(I[i-1]);
+               }
+               DCOUTVAR(newI);
+
+               ex xj = I[j-2].x;
+               int alphaj = I[j-2].evalpoint;
+               size_t deg = A[j-1].degree(xj);
+               DCOUTVAR(deg);
+               for ( size_t k=1; k<=deg; ++k ) {
+                       DCOUTVAR(k);
+                       if ( !e.is_zero() ) {
+                               DCOUTVAR(xj);
+                               DCOUTVAR(alphaj);
+                               monomial *= (xj - alphaj);
+                               monomial = expand(monomial);
+                               DCOUTVAR(monomial);
+                               ex dif = e.diff(ex_to<symbol>(xj), k);
+                               DCOUTVAR(dif);
+                               ex c = dif.subs(xj==alphaj) / factorial(k);
+                               DCOUTVAR(c);
+                               if ( !c.is_zero() ) {
+                                       vector<ex> deltaU = multivar_diophant(U1, x, c, newI, maxdeg, p, cl_I_to_uint(l));
+                                       for ( size_t i=0; i<n; ++i ) {
+                                               DCOUTVAR(i);
+                                               DCOUTVAR(deltaU[i]);
+                                               deltaU[i] *= monomial;
+                                               U[i] += deltaU[i];
+                                               U[i] = make_modular(U[i], R);
+                                               U[i] = U[i].expand();
+                                               DCOUTVAR(U[i]);
+                                       }
+                                       ex Uprod = 1;
+                                       for ( size_t i=0; i<n; ++i ) {
+                                               Uprod *= U[i];
+                                       }
+                                       DCOUTVAR(Uprod.expand());
+                                       DCOUTVAR(A[j-1]);
+                                       e = expand(A[j-1] - Uprod);
+                                       e = make_modular(e, R);
+                                       DCOUTVAR(e);
+                               }
+                               else {
+                                       break;
+                               }
+                       }
+               }
+       }
+
+       ex acand = 1;
+       for ( size_t i=0; i<U.size(); ++i ) {
+               acand *= U[i];
+       }
+       DCOUTVAR(acand);
+       if ( expand(a-acand).is_zero() ) {
+               lst res;
+               for ( size_t i=0; i<U.size(); ++i ) {
+                       res.append(U[i]);
+               }
+               DCOUTVAR(res);
+               DCOUT(END hensel_multivar);
+               return res;
+       }
+       else {
+               lst res;
+               DCOUTVAR(res);
+               DCOUT(END hensel_multivar);
+               return lst();
+       }
+}
+
+static ex put_factors_into_lst(const ex& e)
+{
+       DCOUT(put_factors_into_lst);
+       DCOUTVAR(e);
+
+       lst result;
+
+       if ( is_a<numeric>(e) ) {
+               result.append(e);
+               DCOUT(END put_factors_into_lst);
+               DCOUTVAR(result);
+               return result;
+       }
+       if ( is_a<power>(e) ) {
+               result.append(1);
+               result.append(e.op(0));
+               result.append(e.op(1));
+               DCOUT(END put_factors_into_lst);
+               DCOUTVAR(result);
+               return result;
+       }
+       if ( is_a<symbol>(e) || is_a<add>(e) ) {
+               result.append(1);
+               result.append(e);
+               result.append(1);
+               DCOUT(END put_factors_into_lst);
+               DCOUTVAR(result);
+               return result;
+       }
+       if ( is_a<mul>(e) ) {
+               ex nfac = 1;
+               for ( size_t i=0; i<e.nops(); ++i ) {
+                       ex op = e.op(i);
+                       if ( is_a<numeric>(op) ) {
+                               nfac = op;
+                       }
+                       if ( is_a<power>(op) ) {
+                               result.append(op.op(0));
+                               result.append(op.op(1));
+                       }
+                       if ( is_a<symbol>(op) || is_a<add>(op) ) {
+                               result.append(op);
+                               result.append(1);
+                       }
+               }
+               result.prepend(nfac);
+               DCOUT(END put_factors_into_lst);
+               DCOUTVAR(result);
+               return result;
+       }
+       throw runtime_error("put_factors_into_lst: bad term.");
+}
+
+#ifdef DEBUGFACTOR
+ostream& operator<<(ostream& o, const vector<numeric>& v)
+{
+       for ( size_t i=0; i<v.size(); ++i ) {
+               o << v[i] << " ";
+       }
+       return o;
+}
+#endif // def DEBUGFACTOR
+
+static bool checkdivisors(const lst& f, vector<numeric>& d)
+{
+       DCOUT(checkdivisors);
+       const int k = f.nops()-2;
+       DCOUTVAR(k);
+       DCOUTVAR(d.size());
+       numeric q, r;
+       d[0] = ex_to<numeric>(f.op(0) * f.op(f.nops()-1));
+       if ( d[0] == 1 && k == 1 && abs(f.op(1)) != 1 ) {
+               DCOUT(false);
+               DCOUT(END checkdivisors);
+               return false;
+       }
+       DCOUTVAR(d[0]);
+       for ( int i=1; i<=k; ++i ) {
+               DCOUTVAR(i);
+               DCOUTVAR(abs(f.op(i)));
+               q = ex_to<numeric>(abs(f.op(i)));
+               DCOUTVAR(q);
+               for ( int j=i-1; j>=0; --j ) {
+                       r = d[j];
+                       DCOUTVAR(r);
+                       do {
+                               r = gcd(r, q);
+                               DCOUTVAR(r);
+                               q = q/r;
+                               DCOUTVAR(q);
+                       } while ( r != 1 );
+                       if ( q == 1 ) {
+                               DCOUT(true);
+                               DCOUT(END checkdivisors);
+                               return true;
+                       }
+               }
+               d[i] = q;
+       }
+       DCOUT(false);
+       DCOUT(END checkdivisors);
+       return false;
+}
+
+static bool generate_set(const ex& u, const ex& vn, const exset& syms, const ex& f, const numeric& modulus, vector<numeric>& a, vector<numeric>& d)
+{
+       // computation of d is actually not necessary
+       DCOUT(generate_set);
+       DCOUTVAR(u);
+       DCOUTVAR(vn);
+       DCOUTVAR(f);
+       DCOUTVAR(modulus);
+       const ex& x = *syms.begin();
+       bool trying = true;
+       do {
+               ex u0 = u;
+               ex vna = vn;
+               ex vnatry;
+               exset::const_iterator s = syms.begin();
+               ++s;
+               for ( size_t i=0; i<a.size(); ++i ) {
+                       DCOUTVAR(*s);
+                       do {
+                               a[i] = mod(numeric(rand()), 2*modulus) - modulus;
+                               vnatry = vna.subs(*s == a[i]);
+                       } while ( vnatry == 0 );
+                       vna = vnatry;
+                       u0 = u0.subs(*s == a[i]);
+                       ++s;
+               }
+               DCOUTVAR(a);
+               DCOUTVAR(u0);
+               if ( gcd(u0,u0.diff(ex_to<symbol>(x))) != 1 ) {
+                       continue;
+               }
+               if ( is_a<numeric>(vn) ) {
+                       trying = false;
+               }
+               else {
+                       DCOUT(do substitution);
+                       lst fnum;
+                       lst::const_iterator i = ex_to<lst>(f).begin();
+                       fnum.append(*i++);
+                       bool problem = false;
+                       while ( i!=ex_to<lst>(f).end() ) {
+                               ex fs = *i;
+                               if ( !is_a<numeric>(fs) ) {
+                                       s = syms.begin();
+                                       ++s;
+                                       for ( size_t j=0; j<a.size(); ++j ) {
+                                               fs = fs.subs(*s == a[j]);
+                                               ++s;
+                                       }
+                                       if ( abs(fs) == 1 ) {
+                                               problem = true;
+                                               break;
+                                       }
+                               }
+                               fnum.append(fs);
+                               ++i; ++i;
+                       }
+                       if ( problem ) {
+                               return true;
+                       }
+                       ex con = u0.content(x);
+                       fnum.append(con);
+                       DCOUTVAR(fnum);
+                       trying = checkdivisors(fnum, d);
+               }
+       } while ( trying );
+       DCOUT(END generate_set);
+       return false;
+}
+
+#ifdef DEBUGFACTOR
+ex factor(const ex&);
+#endif
+
+static ex factor_multivariate(const ex& poly, const exset& syms)
+{
+       DCOUT(factor_multivariate);
+       DCOUTVAR(poly);
+
+       exset::const_iterator s;
+       const ex& x = *syms.begin();
+       DCOUTVAR(x);
+
+       /* make polynomial primitive */
+       ex p = poly.expand().collect(x);
+       DCOUTVAR(p);
+       ex cont = p.lcoeff(x);
+       for ( numeric i=p.degree(x)-1; i>=p.ldegree(x); --i ) {
+               cont = gcd(cont, p.coeff(x,ex_to<numeric>(i).to_int()));
+               if ( cont == 1 ) break;
+       }
+       DCOUTVAR(cont);
+       ex pp = expand(normal(p / cont));
+       DCOUTVAR(pp);
+       if ( !is_a<numeric>(cont) ) {
+#ifdef DEBUGFACTOR
+               return ::factor(cont) * ::factor(pp);
+#else
+               return factor(cont) * factor(pp);
+#endif
+       }
+
+       /* factor leading coefficient */
+       pp = pp.collect(x);
+       ex vn = pp.lcoeff(x);
+       pp = pp.expand();
+       ex vnlst;
+       if ( is_a<numeric>(vn) ) {
+               vnlst = lst(vn);
+       }
+       else {
+#ifdef DEBUGFACTOR
+               ex vnfactors = ::factor(vn);
+#else
+               ex vnfactors = factor(vn);
+#endif
+               vnlst = put_factors_into_lst(vnfactors);
+       }
+       DCOUTVAR(vnlst);
+
+       const numeric maxtrials = 3;
+       numeric modulus = (vnlst.nops()-1 > 3) ? vnlst.nops()-1 : 3;
+       DCOUTVAR(modulus);
+       numeric minimalr = -1;
+       vector<numeric> a(syms.size()-1, 0);
+       vector<numeric> d((vnlst.nops()-1)/2+1, 0);
+
+       while ( true ) {
+               numeric trialcount = 0;
+               ex u, delta;
+               unsigned int prime;
+               size_t factor_count;
+               ex ufac;
+               ex ufaclst;
+               while ( trialcount < maxtrials ) {
+                       bool problem = generate_set(pp, vn, syms, vnlst, modulus, a, d);
+                       DCOUTVAR(problem);
+                       if ( problem ) {
+                               ++modulus;
+                               continue;
+                       }
+                       DCOUTVAR(a);
+                       DCOUTVAR(d);
+                       u = pp;
+                       s = syms.begin();
+                       ++s;
+                       for ( size_t i=0; i<a.size(); ++i ) {
+                               u = u.subs(*s == a[i]);
+                               ++s;
+                       }
+                       delta = u.content(x);
+                       DCOUTVAR(u);
+
+                       // determine proper prime
+                       prime = 3;
+                       DCOUTVAR(prime);
+                       cl_modint_ring R = find_modint_ring(prime);
+                       DCOUTVAR(u.lcoeff(x));
+                       while ( true ) {
+                               if ( irem(ex_to<numeric>(u.lcoeff(x)), prime) != 0 ) {
+                                       UniPoly modpoly(R, u, x);
+                                       UniFactorVec sqrfree_ufv;
+                                       squarefree(modpoly, sqrfree_ufv);
+                                       DCOUTVAR(sqrfree_ufv);
+                                       if ( sqrfree_ufv.factors.size() == 1 && sqrfree_ufv.factors.front().exp == 1 ) break;
+                               }
+                               prime = next_prime(prime);
+                               DCOUTVAR(prime);
+                               R = find_modint_ring(prime);
+                       }
+
+#ifdef DEBUGFACTOR
+                       ufac = ::factor(u);
+#else
+                       ufac = factor(u);
+#endif
+                       DCOUTVAR(ufac);
+                       ufaclst = put_factors_into_lst(ufac);
+                       DCOUTVAR(ufaclst);
+                       factor_count = (ufaclst.nops()-1)/2;
+                       DCOUTVAR(factor_count);
+
+                       if ( factor_count <= 1 ) {
+                               DCOUTVAR(poly);
+                               DCOUT(END factor_multivariate);
+                               return poly;
+                       }
+
+                       if ( minimalr < 0 ) {
+                               minimalr = factor_count;
+                       }
+                       else if ( minimalr == factor_count ) {
+                               ++trialcount;
+                               ++modulus;
+                       }
+                       else if ( minimalr > factor_count ) {
+                               minimalr = factor_count;
+                               trialcount = 0;
+                       }
+                       DCOUTVAR(trialcount);
+                       DCOUTVAR(minimalr);
+                       if ( minimalr <= 1 ) {
+                               DCOUTVAR(poly);
+                               DCOUT(END factor_multivariate);
+                               return poly;
+                       }
+               }
+
+               vector<numeric> ftilde((vnlst.nops()-1)/2+1);
+               ftilde[0] = ex_to<numeric>(vnlst.op(0));
+               for ( size_t i=1; i<ftilde.size(); ++i ) {
+                       ex ft = vnlst.op((i-1)*2+1);
+                       s = syms.begin();
+                       ++s;
+                       for ( size_t j=0; j<a.size(); ++j ) {
+                               ft = ft.subs(*s == a[j]);
+                               ++s;
+                       }
+                       ftilde[i] = ex_to<numeric>(ft);
+               }
+               DCOUTVAR(ftilde);
+
+               vector<bool> used_flag((vnlst.nops()-1)/2+1, false);
+               vector<ex> D(factor_count, 1);
+               for ( size_t i=0; i<=factor_count; ++i ) {
+                       DCOUTVAR(i);
+                       numeric prefac;
+                       if ( i == 0 ) {
+                               prefac = ex_to<numeric>(ufaclst.op(0));
+                               ftilde[0] = ftilde[0] / prefac;
+                               vnlst.let_op(0) = vnlst.op(0) / prefac;
+                               continue;
+                       }
+                       else {
+                               prefac = ex_to<numeric>(ufaclst.op(2*(i-1)+1).lcoeff(x));
+                       }
+                       DCOUTVAR(prefac);
+                       for ( size_t j=(vnlst.nops()-1)/2+1; j>0; --j ) {
+                               DCOUTVAR(j);
+                               DCOUTVAR(prefac);
+                               DCOUTVAR(ftilde[j-1]);
+                               if ( abs(ftilde[j-1]) == 1 ) {
+                                       used_flag[j-1] = true;
+                                       continue;
+                               }
+                               numeric g = gcd(prefac, ftilde[j-1]);
+                               DCOUTVAR(g);
+                               if ( g != 1 ) {
+                                       DCOUT(has_common_prime);
+                                       prefac = prefac / g;
+                                       numeric count = abs(iquo(g, ftilde[j-1]));
+                                       DCOUTVAR(count);
+                                       used_flag[j-1] = true;
+                                       if ( i > 0 ) {
+                                               if ( j == 1 ) {
+                                                       D[i-1] = D[i-1] * pow(vnlst.op(0), count);
+                                               }
+                                               else {
+                                                       D[i-1] = D[i-1] * pow(vnlst.op(2*(j-2)+1), count);
+                                               }
+                                       }
+                                       else {
+                                               ftilde[j-1] = ftilde[j-1] / prefac;
+                                               DCOUT(BREAK);
+                                               DCOUTVAR(ftilde[j-1]);
+                                               break;
+                                       }
+                                       ++j;
+                               }
+                       }
+               }
+               DCOUTVAR(D);
+
+               bool some_factor_unused = false;
+               for ( size_t i=0; i<used_flag.size(); ++i ) {
+                       if ( !used_flag[i] ) {
+                               some_factor_unused = true;
+                               break;
+                       }
+               }
+               if ( some_factor_unused ) {
+                       DCOUT(some factor unused!);
+                       continue;
+               }
+
+               vector<ex> C(factor_count);
+               DCOUTVAR(C);
+               DCOUTVAR(delta);
+               if ( delta == 1 ) {
+                       for ( size_t i=0; i<D.size(); ++i ) {
+                               ex Dtilde = D[i];
+                               s = syms.begin();
+                               ++s;
+                               for ( size_t j=0; j<a.size(); ++j ) {
+                                       Dtilde = Dtilde.subs(*s == a[j]);
+                                       ++s;
+                               }
+                               DCOUTVAR(Dtilde);
+                               C[i] = D[i] * (ufaclst.op(2*i+1).lcoeff(x) / Dtilde);
+                       }
+               }
+               else {
+                       for ( size_t i=0; i<D.size(); ++i ) {
+                               ex Dtilde = D[i];
+                               s = syms.begin();
+                               ++s;
+                               for ( size_t j=0; j<a.size(); ++j ) {
+                                       Dtilde = Dtilde.subs(*s == a[j]);
+                                       ++s;
+                               }
+                               ex ui;
+                               if ( i == 0 ) {
+                                       ui = ufaclst.op(0);
+                               }
+                               else {
+                                       ui = ufaclst.op(2*(i-1)+1);
+                               }
+                               while ( true ) {
+                                       ex d = gcd(ui.lcoeff(x), Dtilde);
+                                       C[i] = D[i] * ( ui.lcoeff(x) / d );
+                                       ui = ui * ( Dtilde[i] / d );
+                                       delta = delta / ( Dtilde[i] / d );
+                                       if ( delta == 1 ) break;
+                                       ui = delta * ui;
+                                       C[i] = delta * C[i];
+                                       pp = pp * pow(delta, D.size()-1);
+                               }
+                       }
+               }
+               DCOUTVAR(C);
+
+               EvalPoint ep;
+               vector<EvalPoint> epv;
+               s = syms.begin();
+               ++s;
+               for ( size_t i=0; i<a.size(); ++i ) {
+                       ep.x = *s++;
+                       ep.evalpoint = a[i].to_int();
+                       epv.push_back(ep);
+               }
+               DCOUTVAR(epv);
+
+               // calc bound B
+               ex maxcoeff;
+               for ( int i=u.degree(x); i>=u.ldegree(x); --i ) {
+                       maxcoeff += pow(abs(u.coeff(x, i)),2);
+               }
+               cl_I normmc = ceiling1(the<cl_R>(cln::sqrt(ex_to<numeric>(maxcoeff).to_cl_N())));
+               unsigned int maxdegree = 0;
+               for ( size_t i=0; i<factor_count; ++i ) {
+                       if ( ufaclst[2*i+1].degree(x) > (int)maxdegree ) {
+                               maxdegree = ufaclst[2*i+1].degree(x);
+                       }
+               }
+               cl_I B = normmc * expt_pos(cl_I(2), maxdegree);
+               cl_I l = 1;
+               cl_I pl = prime;
+               while ( pl < B ) {
+                       l = l + 1;
+                       pl = pl * prime;
+               }
+
+               UniPolyVec uvec;
+               cl_modint_ring R = find_modint_ring(expt_pos(cl_I(prime),l));
+               for ( size_t i=0; i<(ufaclst.nops()-1)/2; ++i ) {
+                       UniPoly newu(R, ufaclst.op(i*2+1), x);
+                       uvec.push_back(newu);
+               }
+               DCOUTVAR(uvec);
+
+               ex res = hensel_multivar(ufaclst.op(0)*pp, x, epv, prime, l, uvec, C);
+               if ( res != lst() ) {
+                       ex result = cont * ufaclst.op(0);
+                       for ( size_t i=0; i<res.nops(); ++i ) {
+                               result *= res.op(i).content(x) * res.op(i).unit(x);
+                               result *= res.op(i).primpart(x);
+                       }
+                       DCOUTVAR(result);
+                       DCOUT(END factor_multivariate);
+                       return result;
+               }
+       }
+}
+
 static ex factor_sqrfree(const ex& poly)
 {
        // determine all symbols in poly
@@ -1163,6 +2327,7 @@ static ex factor_sqrfree(const ex& poly)
        }
 
        if ( findsymbols.syms.size() == 1 ) {
+               // univariate case
                const ex& x = *(findsymbols.syms.begin());
                if ( poly.ldegree(x) > 0 ) {
                        int ld = poly.ldegree(x);
@@ -1175,8 +2340,9 @@ static ex factor_sqrfree(const ex& poly)
                }
        }
 
-       // multivariate case not yet implemented!
-       throw runtime_error("multivariate case not yet implemented!");
+       // multivariate case
+       ex res = factor_multivariate(poly, findsymbols.syms);
+       return res;
 }
 
 } // anonymous namespace
@@ -1233,6 +2399,9 @@ ex factor(const ex& poly)
                }
                return res;
        }
+       if ( is_a<symbol>(sfpoly) ) {
+               return poly;
+       }
        // case: (polynomial)
        ex f = factor_sqrfree(sfpoly);
        return f;