]> www.ginac.de Git - ginac.git/blobdiff - ginac/factor.cpp
Added code for distinct degree factorization.
[ginac.git] / ginac / factor.cpp
index 3afaec54727d32b19e70772827963ce7cb62fa0b..222e05fd55760cfd155c36ca0056060af5e1940e 100644 (file)
@@ -1,7 +1,12 @@
 /** @file factor.cpp
  *
- *  Polynomial factorization routines.
- *  Only univariate at the moment and completely non-optimized!
+ *  Polynomial factorization code (implementation).
+ *
+ *  Algorithms used can be found in
+ *    [W1]  An Improved Multivariate Polynomial Factoring Algorithm,
+ *          P.S.Wang, Mathematics of Computation, Vol. 32, No. 144 (1978) 1215--1231.
+ *    [GCL] Algorithms for Computer Algebra,
+ *          K.O.Geddes, S.R.Czapor, G.Labahn, Springer Verlag, 1992.
  */
 
 /*
@@ -44,6 +49,7 @@ using namespace GiNaC;
 #endif
 
 #include <algorithm>
+#include <cmath>
 #include <list>
 #include <vector>
 using namespace std;
@@ -67,15 +73,18 @@ namespace GiNaC {
 #define DCOUT2(str,var)
 #endif
 
+// forward declaration
+ex factor(const ex& poly, unsigned options);
+
+// anonymous namespace to hide all utility functions
 namespace {
 
-typedef vector<cl_MI> Vec;
-typedef vector<Vec> VecVec;
+typedef vector<cl_MI> mvec;
 
 #ifdef DEBUGFACTOR
-ostream& operator<<(ostream& o, const Vec& v)
+ostream& operator<<(ostream& o, const mvec& v)
 {
-       Vec::const_iterator i = v.begin(), end = v.end();
+       mvec::const_iterator i = v.begin(), end = v.end();
        while ( i != end ) {
                o << *i++ << " ";
        }
@@ -84,9 +93,9 @@ ostream& operator<<(ostream& o, const Vec& v)
 #endif // def DEBUGFACTOR
 
 #ifdef DEBUGFACTOR
-ostream& operator<<(ostream& o, const VecVec& v)
+ostream& operator<<(ostream& o, const vector<mvec>& v)
 {
-       VecVec::const_iterator i = v.begin(), end = v.end();
+       vector<mvec>::const_iterator i = v.begin(), end = v.end();
        while ( i != end ) {
                o << *i++ << endl;
        }
@@ -94,630 +103,260 @@ ostream& operator<<(ostream& o, const VecVec& v)
 }
 #endif // def DEBUGFACTOR
 
-struct Term
-{
-       cl_MI c;          // coefficient
-       unsigned int exp; // exponent >=0
-};
+////////////////////////////////////////////////////////////////////////////////
+// modular univariate polynomial code
+
+typedef cl_UP_MI umod;
+typedef vector<umod> umodvec;
+
+#define COPY(to,from) from.ring()->create(degree(from)); \
+       for ( int II=0; II<=degree(from); ++II ) to.set_coeff(II, coeff(from, II)); \
+       to.finalize()
 
 #ifdef DEBUGFACTOR
-ostream& operator<<(ostream& o, const Term& t)
+ostream& operator<<(ostream& o, const umodvec& v)
 {
-       if ( t.exp ) {
-               o << "(" << t.c << ")x^" << t.exp;
-       }
-       else {
-               o << "(" << t.c << ")";
+       umodvec::const_iterator i = v.begin(), end = v.end();
+       while ( i != end ) {
+               o << *i++ << " , " << endl;
        }
        return o;
 }
 #endif // def DEBUGFACTOR
 
-struct UniPoly
+static umod umod_from_ex(const ex& e, const ex& x, const cl_univpoly_modint_ring& UPR)
 {
-       cl_modint_ring R;
-       list<Term> terms;  // highest exponent first
-
-       UniPoly(const cl_modint_ring& ring) : R(ring) { }
-       UniPoly(const cl_modint_ring& ring, const ex& poly, const ex& x) : R(ring)
-       { 
-               // 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 ) {
-                               t.c = R->canonhom(coeff);
-                               if ( !zerop(t.c) ) {
-                                       t.exp = i;
-                                       terms.push_back(t);
-                               }
-                       }
-               }
-       }
-       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;
-               for ( unsigned int i=0; i<v.size(); ++i ) {
-                       if ( !zerop(v[i]) ) {
-                               t.c = v[i];
-                               t.exp = i;
-                               terms.push_front(t);
-                       }
-               }
-       }
-       unsigned int degree() const
-       {
-               if ( terms.size() ) {
-                       return terms.front().exp;
-               }
-               else {
-                       return 0;
-               }
-       }
-       bool zero() const { return (terms.size() == 0); }
-       const cl_MI operator[](unsigned int deg) const
-       {
-               list<Term>::const_iterator i = terms.begin(), end = terms.end();
-               for ( ; i != end; ++i ) {
-                       if ( i->exp == deg ) {
-                               return i->c;
-                       }
-                       if ( i->exp < deg ) {
-                               break;
-                       }
-               }
-               return R->zero();
-       }
-       void set(unsigned int deg, const cl_MI& c)
-       {
-               list<Term>::iterator i = terms.begin(), end = terms.end();
-               while ( i != end ) {
-                       if ( i->exp == deg ) {
-                               if ( !zerop(c) ) {
-                                       i->c = c;
-                               }
-                               else {
-                                       terms.erase(i);
-                               }
-                               return;
-                       }
-                       if ( i->exp < deg ) {
-                               break;
-                       }
-                       ++i;
-               }
-               if ( !zerop(c) ) {
-                       Term t;
-                       t.c = c;
-                       t.exp = deg;
-                       terms.insert(i, t);
-               }
-       }
-       ex to_ex(const ex& x, bool symmetric = true) const
-       {
-               ex r;
-               list<Term>::const_iterator i = terms.begin(), end = terms.end();
-               if ( symmetric ) {
-                       numeric mod(R->modulus);
-                       numeric halfmod = (mod-1)/2;
-                       for ( ; i != end; ++i ) {
-                               numeric n(R->retract(i->c));
-                               if ( n > halfmod ) {
-                                       r += pow(x, i->exp) * (n-mod);
-                               }
-                               else {
-                                       r += pow(x, i->exp) * n;
-                               }
-                       }
-               }
-               else {
-                       for ( ; i != end; ++i ) {
-                               r += pow(x, i->exp) * numeric(R->retract(i->c));
-                       }
-               }
-               return r;
-       }
-       void unit_normal()
-       {
-               if ( terms.size() ) {
-                       if ( terms.front().c != R->one() ) {
-                               list<Term>::iterator i = terms.begin(), end = terms.end();
-                               cl_MI cont = i->c;
-                               i->c = R->one();
-                               while ( ++i != end ) {
-                                       i->c = div(i->c, cont);
-                                       if ( zerop(i->c) ) {
-                                               terms.erase(i);
-                                       }
-                               }
-                       }
-               }
-       }
-       cl_MI unit() const
-       {
-               return terms.front().c;
-       }
-       void divide(const cl_MI& x)
-       {
-               list<Term>::iterator i = terms.begin(), end = terms.end();
-               for ( ; i != end; ++i ) {
-                       i->c = div(i->c, x);
-                       if ( zerop(i->c) ) {
-                               terms.erase(i);
-                       }
-               }
-       }
-       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();
-               while ( i != end ) {
-                       if ( i->exp > 0 ) {
-                               // assert: i->exp is multiple of prime
-                               i->exp /= prime;
-                       }
-                       ++i;
-               }
-       }
-       void deriv(UniPoly& d) const
-       {
-               list<Term>::const_iterator i = terms.begin(), end = terms.end();
-               while ( i != end ) {
-                       if ( i->exp ) {
-                               cl_MI newc = i->c * i->exp;
-                               if ( !zerop(newc) ) {
-                                       Term t;
-                                       t.c = newc;
-                                       t.exp = i->exp-1;
-                                       d.terms.push_back(t);
-                               }
-                       }
-                       ++i;
-               }
-       }
-       bool operator<(const UniPoly& o) const
-       {
-               if ( terms.size() != o.terms.size() ) {
-                       return terms.size() < o.terms.size();
-               }
-               list<Term>::const_iterator i1 = terms.begin(), end = terms.end();
-               list<Term>::const_iterator i2 = o.terms.begin();
-               while ( i1 != end ) {
-                       if ( i1->exp != i2->exp ) {
-                               return i1->exp < i2->exp;
-                       }
-                       if ( i1->c != i2->c ) {
-                               return R->retract(i1->c) < R->retract(i2->c);
-                       }
-                       ++i1; ++i2;
-               }
-               return true;
-       }
-       bool operator==(const UniPoly& o) const
-       {
-               if ( terms.size() != o.terms.size() ) {
-                       return false;
-               }
-               list<Term>::const_iterator i1 = terms.begin(), end = terms.end();
-               list<Term>::const_iterator i2 = o.terms.begin();
-               while ( i1 != end ) {
-                       if ( i1->exp != i2->exp ) {
-                               return false;
-                       }
-                       if ( i1->c != i2->c ) {
-                               return false;
-                       }
-                       ++i1; ++i2;
-               }
-               return true;
-       }
-       bool operator!=(const UniPoly& o) const
-       {
-               bool res = !(*this == o);
-               return res;
-       }
-};
-
-static UniPoly operator*(const UniPoly& a, const UniPoly& b)
-{
-       unsigned int n = a.degree()+b.degree();
-       UniPoly c(a.R);
-       Term t;
-       for ( unsigned int i=0 ; i<=n; ++i ) {
-               t.c = a.R->zero();
-               for ( unsigned int j=0 ; j<=i; ++j ) {
-                       t.c = t.c + a[j] * b[i-j];
-               }
-               if ( !zerop(t.c) ) {
-                       t.exp = i;
-                       c.terms.push_front(t);
-               }
-       }
-       return c;
+       // assert: e is in Z[x]
+       int deg = e.degree(x);
+       umod p = UPR->create(deg);
+       int ldeg = e.ldegree(x);
+       for ( ; deg>=ldeg; --deg ) {
+               cl_I coeff = the<cl_I>(ex_to<numeric>(e.coeff(x, deg)).to_cl_N());
+               p.set_coeff(deg, UPR->basering()->canonhom(coeff));
+       }
+       for ( ; deg>=0; --deg ) {
+               p.set_coeff(deg, UPR->basering()->zero());
+       }
+       p.finalize();
+       return p;
 }
 
-static UniPoly operator-(const UniPoly& a, const UniPoly& b)
+static umod umod_from_ex(const ex& e, const ex& x, const cl_modint_ring& R)
 {
-       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);
-                       c.terms.back().c = -c.terms.back().c;
-                       ++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);
-               ++ia;
-       }
-       while ( ib != bend ) {
-               c.terms.push_back(*ib);
-               c.terms.back().c = -c.terms.back().c;
-               ++ib;
-       }
-       return c;
+       return umod_from_ex(e, x, find_univpoly_ring(R));
 }
 
-static UniPoly operator*(const UniPoly& a, const cl_MI& fac)
+static umod umod_from_ex(const ex& e, const ex& x, const cl_I& modulus)
 {
-       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;
+       return umod_from_ex(e, x, find_modint_ring(modulus));
 }
 
-static UniPoly operator+(const UniPoly& a, const UniPoly& b)
+static umod umod_from_modvec(const mvec& mv)
 {
-       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);
-               ++ia;
+       size_t n = mv.size(); // assert: n>0
+       while ( n && zerop(mv[n-1]) ) --n;
+       cl_univpoly_modint_ring UPR = find_univpoly_ring(mv.front().ring());
+       if ( n == 0 ) {
+               umod p = UPR->create(-1);
+               p.finalize();
+               return p;
        }
-       while ( ib != bend ) {
-               c.terms.push_back(*ib);
-               ++ib;
+       umod p = UPR->create(n-1);
+       for ( size_t i=0; i<n; ++i ) {
+               p.set_coeff(i, mv[i]);
        }
-       return c;
+       p.finalize();
+       return p;
 }
 
-// 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)
+static umod divide(const umod& a, const cl_I& x)
 {
-       list<Term>::const_iterator i = t.terms.begin(), end = t.terms.end();
-       if ( i == end ) {
-               o << "0";
-               return o;
-       }
-       for ( ; i != end; ) {
-               o << *i++;
-               if ( i != end ) {
-                       o << " + ";
-               }
-       }
-       return o;
+       DCOUT(divide);
+       DCOUTVAR(a);
+       cl_univpoly_modint_ring UPR = a.ring();
+       cl_modint_ring R = UPR->basering();
+       int deg = degree(a);
+       umod newa = UPR->create(deg);
+       for ( int i=0; i<=deg; ++i ) {
+               cl_I c = R->retract(coeff(a, i));
+               newa.set_coeff(i, cl_MI(R, the<cl_I>(c / x)));
+       }
+       newa.finalize();
+       DCOUT(END divide);
+       return newa;
 }
-#endif // def DEBUGFACTOR
 
-#ifdef DEBUGFACTOR
-ostream& operator<<(ostream& o, const list<UniPoly>& t)
+static ex umod_to_ex(const umod& a, const ex& x)
 {
-       list<UniPoly>::const_iterator i = t.begin(), end = t.end();
-       o << "{" << endl;
-       for ( ; i != end; ) {
-               o << *i++ << endl;
-       }
-       o << "}" << endl;
-       return o;
+       ex e;
+       cl_modint_ring R = a.ring()->basering();
+       cl_I mod = R->modulus;
+       cl_I halfmod = (mod-1) >> 1;
+       for ( int i=degree(a); i>=0; --i ) {
+               cl_I n = R->retract(coeff(a, i));
+               if ( n > halfmod ) {
+                       e += numeric(n-mod) * pow(x, i);
+               } else {
+                       e += numeric(n) * pow(x, i);
+               }
+       }
+       return e;
 }
-#endif // def DEBUGFACTOR
-
-typedef vector<UniPoly> UniPolyVec;
 
-#ifdef DEBUGFACTOR
-ostream& operator<<(ostream& o, const UniPolyVec& v)
+static void unit_normal(umod& a)
 {
-       UniPolyVec::const_iterator i = v.begin(), end = v.end();
-       while ( i != end ) {
-               o << *i++ << " , " << endl;
+       int deg = degree(a);
+       if ( deg >= 0 ) {
+               cl_MI lc = coeff(a, deg);
+               cl_MI one = a.ring()->basering()->one();
+               if ( lc != one ) {
+                       umod newa = a.ring()->create(deg);
+                       newa.set_coeff(deg, one);
+                       for ( --deg; deg>=0; --deg ) {
+                               cl_MI nc = div(coeff(a, deg), lc);
+                               newa.set_coeff(deg, nc);
+                       }
+                       newa.finalize();
+                       a = newa;
+               }
        }
-       return o;
 }
-#endif // def DEBUGFACTOR
 
-struct UniFactor
+static umod rem(const umod& a, const umod& b)
 {
-       UniPoly p;
-       unsigned int exp;
-
-       UniFactor(const cl_modint_ring& ring) : p(ring) { }
-       UniFactor(const UniPoly& p_, unsigned int exp_) : p(p_), exp(exp_) { }
-       bool operator<(const UniFactor& o) const
-       {
-               return p < o.p;
+       int k, n;
+       n = degree(b);
+       k = degree(a) - n;
+       if ( k < 0 ) {
+               umod c = COPY(c, a);
+               return c;
        }
-};
-
-struct UniFactorVec
-{
-       vector<UniFactor> factors;
 
-       void unique()
-       {
-               sort(factors.begin(), factors.end());
-               if ( factors.size() > 1 ) {
-                       vector<UniFactor>::iterator i = factors.begin();
-                       vector<UniFactor>::const_iterator cmp = factors.begin()+1;
-                       vector<UniFactor>::iterator end = factors.end();
-                       while ( cmp != end ) {
-                               if ( i->p != cmp->p ) {
-                                       ++i;
-                                       ++cmp;
-                               }
-                               else {
-                                       i->exp += cmp->exp;
-                                       ++cmp;
-                               }
-                       }
-                       if ( i != end-1 ) {
-                               factors.erase(i+1, end);
+       umod c = COPY(c, a);
+       do {
+               cl_MI qk = div(coeff(c, n+k), coeff(b, n));
+               if ( !zerop(qk) ) {
+                       unsigned int j;
+                       for ( int i=0; i<n; ++i ) {
+                               j = n + k - 1 - i;
+                               c.set_coeff(j, coeff(c, j) - qk * coeff(b, j-k));
                        }
                }
-       }
-};
+       } while ( k-- );
 
-#ifdef DEBUGFACTOR
-ostream& operator<<(ostream& o, const UniFactorVec& ufv)
-{
-       for ( size_t i=0; i<ufv.factors.size(); ++i ) {
-               if ( i != ufv.factors.size()-1 ) {
-                       o << "*";
-               }
-               else {
-                       o << " ";
-               }
-               o << "[ " << ufv.factors[i].p << " ]^" << ufv.factors[i].exp << endl;
+       cl_MI zero = a.ring()->basering()->zero();
+       for ( int i=degree(a); i>=n; --i ) {
+               c.set_coeff(i, zero);
        }
-       return o;
+
+       c.finalize();
+       return c;
 }
-#endif // def DEBUGFACTOR
 
-static void rem(const UniPoly& a_, const UniPoly& b, UniPoly& c)
+static umod div(const umod& a, const umod& b)
 {
-       if ( a_.degree() < b.degree() ) {
-               c = a_;
-               return;
-       }
-
-       unsigned int k, n;
-       n = b.degree();
-       k = a_.degree() - n;
-
-       if ( n == 0 ) {
-               c.terms.clear();
-               return;
+       int k, n;
+       n = degree(b);
+       k = degree(a) - n;
+       if ( k < 0 ) {
+               umod q = a.ring()->create(-1);
+               q.finalize();
+               return q;
        }
 
-       c = a_;
-       Term termbuf;
-
-       while ( true ) {
-               cl_MI qk = div(c[n+k], b[n]);
+       umod c = COPY(c, a);
+       umod q = a.ring()->create(k);
+       do {
+               cl_MI qk = div(coeff(c, n+k), coeff(b, n));
                if ( !zerop(qk) ) {
+                       q.set_coeff(k, qk);
                        unsigned int j;
-                       for ( unsigned int i=0; i<n; ++i ) {
+                       for ( int i=0; i<n; ++i ) {
                                j = n + k - 1 - i;
-                               c.set(j, c[j] - qk*b[j-k]);
+                               c.set_coeff(j, coeff(c, j) - qk * coeff(b, j-k));
                        }
                }
-               if ( k == 0 ) break;
-               --k;
-       }
-       list<Term>::iterator i = c.terms.begin(), end = c.terms.end();
-       while ( i != end ) {
-               if ( i->exp <= n-1 ) {
-                       break;
-               }
-               ++i;
-       }
-       c.terms.erase(c.terms.begin(), i);
+       } while ( k-- );
+
+       q.finalize();
+       return q;
 }
 
-static void div(const UniPoly& a_, const UniPoly& b, UniPoly& q)
+static umod remdiv(const umod& a, const umod& b, umod& q)
 {
-       if ( a_.degree() < b.degree() ) {
-               q.terms.clear();
-               return;
-       }
-
-       unsigned int k, n;
-       n = b.degree();
-       k = a_.degree() - n;
-
-       UniPoly c = a_;
-       Term termbuf;
-
-       while ( true ) {
-               cl_MI qk = div(c[n+k], b[n]);
+       int k, n;
+       n = degree(b);
+       k = degree(a) - n;
+       if ( k < 0 ) {
+               q = a.ring()->create(-1);
+               q.finalize();
+               umod c = COPY(c, a);
+               return c;
+       }
+
+       umod c = COPY(c, a);
+       q = a.ring()->create(k);
+       do {
+               cl_MI qk = div(coeff(c, n+k), coeff(b, n));
                if ( !zerop(qk) ) {
-                       Term t;
-                       t.c = qk;
-                       t.exp = k;
-                       q.terms.push_back(t);
+                       q.set_coeff(k, qk);
                        unsigned int j;
-                       for ( unsigned int i=0; i<n; ++i ) {
+                       for ( int i=0; i<n; ++i ) {
                                j = n + k - 1 - i;
-                               c.set(j, c[j] - qk*b[j-k]);
+                               c.set_coeff(j, coeff(c, j) - qk * coeff(b, j-k));
                        }
                }
-               if ( k == 0 ) break;
-               --k;
-       }
-}
-
-static void gcd(const UniPoly& a, const UniPoly& b, UniPoly& c)
-{
-       c = a;
-       c.unit_normal();
-       UniPoly d = b;
-       d.unit_normal();
+       } while ( k-- );
 
-       if ( c.degree() < d.degree() ) {
-               gcd(b, a, c);
-               return;
+       cl_MI zero = a.ring()->basering()->zero();
+       for ( int i=degree(a); i>=n; --i ) {
+               c.set_coeff(i, zero);
        }
 
-       while ( !d.zero() ) {
-               UniPoly r(a.R);
-               rem(c, d, r);
-               c = d;
-               d = r;
-       }
-       c.unit_normal();
+       q.finalize();
+       c.finalize();
+       return c;
 }
 
-static bool is_one(const UniPoly& w)
+static umod gcd(const umod& a, const umod& b)
 {
-       if ( w.terms.size() == 1 && w[0] == w.R->one() ) {
-               return true;
-       }
-       return false;
+       if ( degree(a) < degree(b) ) return gcd(b, a);
+
+       umod c = COPY(c, a);
+       unit_normal(c);
+       umod d = COPY(d, b);
+       unit_normal(d);
+       while ( !zerop(d) ) {
+               umod r = rem(c, d);
+               c = COPY(c, d);
+               d = COPY(d, r);
+       }
+       unit_normal(c);
+       return c;
 }
 
-static void sqrfree_main(const UniPoly& a, UniFactorVec& fvec)
+static bool squarefree(const umod& a)
 {
-       unsigned int i = 1;
-       UniPoly b(a.R);
-       a.deriv(b);
-       if ( !b.zero() ) {
-               UniPoly c(a.R), w(a.R);
-               gcd(a, b, c);
-               div(a, c, w);
-               while ( !is_one(w) ) {
-                       UniPoly y(a.R), z(a.R);
-                       gcd(w, c, y);
-                       div(w, y, z);
-                       if ( !is_one(z) ) {
-                               UniFactor uf(z, i);
-                               fvec.factors.push_back(uf);
-                       }
-                       ++i;
-                       w = y;
-                       UniPoly cbuf(a.R);
-                       div(c, y, cbuf);
-                       c = cbuf;
-               }
-               if ( !is_one(c) ) {
-                       unsigned int prime = cl_I_to_uint(c.R->modulus);
-                       c.reduce_exponents(prime);
-                       unsigned int pos = fvec.factors.size();
-                       sqrfree_main(c, fvec);
-                       for ( unsigned int p=pos; p<fvec.factors.size(); ++p ) {
-                               fvec.factors[p].exp *= prime;
-                       }
-                       return;
-               }
-       }
-       else {
-               unsigned int prime = cl_I_to_uint(a.R->modulus);
-               UniPoly amod = a;
-               amod.reduce_exponents(prime);
-               unsigned int pos = fvec.factors.size();
-               sqrfree_main(amod, fvec);
-               for ( unsigned int p=pos; p<fvec.factors.size(); ++p ) {
-                       fvec.factors[p].exp *= prime;
-               }
-               return;
+       umod b = deriv(a);
+       if ( zerop(b) ) {
+               return false;
        }
+       umod one = a.ring()->one();
+       umod c = gcd(a, b);
+       return c == one;
 }
 
-static void squarefree(const UniPoly& a, UniFactorVec& fvec)
-{
-       sqrfree_main(a, fvec);
-       fvec.unique();
-}
+// END modular univariate polynomial code
+////////////////////////////////////////////////////////////////////////////////
 
-class Matrix
+////////////////////////////////////////////////////////////////////////////////
+// modular matrix
+
+class modular_matrix
 {
-       friend ostream& operator<<(ostream& o, const Matrix& m);
+       friend ostream& operator<<(ostream& o, const modular_matrix& m);
 public:
-       Matrix(size_t r_, size_t c_, const cl_MI& init) : r(r_), c(c_)
+       modular_matrix(size_t r_, size_t c_, const cl_MI& init) : r(r_), c(c_)
        {
                m.resize(c*r, init);
        }
@@ -727,7 +366,7 @@ public:
        cl_MI operator()(size_t row, size_t col) const { return m[row*c + col]; }
        void mul_col(size_t col, const cl_MI x)
        {
-               Vec::iterator i = m.begin() + col;
+               mvec::iterator i = m.begin() + col;
                for ( size_t rc=0; rc<r; ++rc ) {
                        *i = *i * x;
                        i += c;
@@ -735,8 +374,8 @@ public:
        }
        void sub_col(size_t col1, size_t col2, const cl_MI fac)
        {
-               Vec::iterator i1 = m.begin() + col1;
-               Vec::iterator i2 = m.begin() + col2;
+               mvec::iterator i1 = m.begin() + col1;
+               mvec::iterator i2 = m.begin() + col2;
                for ( size_t rc=0; rc<r; ++rc ) {
                        *i1 = *i1 - *i2 * fac;
                        i1 += c;
@@ -746,8 +385,8 @@ public:
        void switch_col(size_t col1, size_t col2)
        {
                cl_MI buf;
-               Vec::iterator i1 = m.begin() + col1;
-               Vec::iterator i2 = m.begin() + col2;
+               mvec::iterator i1 = m.begin() + col1;
+               mvec::iterator i2 = m.begin() + col2;
                for ( size_t rc=0; rc<r; ++rc ) {
                        buf = *i1; *i1 = *i2; *i2 = buf;
                        i1 += c;
@@ -785,7 +424,7 @@ public:
        }
        bool is_col_zero(size_t col) const
        {
-               Vec::const_iterator i = m.begin() + col;
+               mvec::const_iterator i = m.begin() + col;
                for ( size_t rr=0; rr<r; ++rr ) {
                        if ( !zerop(*i) ) {
                                return false;
@@ -796,7 +435,7 @@ public:
        }
        bool is_row_zero(size_t row) const
        {
-               Vec::const_iterator i = m.begin() + row*c;
+               mvec::const_iterator i = m.begin() + row*c;
                for ( size_t cc=0; cc<c; ++cc ) {
                        if ( !zerop(*i) ) {
                                return false;
@@ -807,25 +446,25 @@ public:
        }
        void set_row(size_t row, const vector<cl_MI>& newrow)
        {
-               Vec::iterator i1 = m.begin() + row*c;
-               Vec::const_iterator i2 = newrow.begin(), end = newrow.end();
+               mvec::iterator i1 = m.begin() + row*c;
+               mvec::const_iterator i2 = newrow.begin(), end = newrow.end();
                for ( ; i2 != end; ++i1, ++i2 ) {
                        *i1 = *i2;
                }
        }
-       Vec::const_iterator row_begin(size_t row) const { return m.begin()+row*c; }
-       Vec::const_iterator row_end(size_t row) const { return m.begin()+row*c+r; }
+       mvec::const_iterator row_begin(size_t row) const { return m.begin()+row*c; }
+       mvec::const_iterator row_end(size_t row) const { return m.begin()+row*c+r; }
 private:
        size_t r, c;
-       Vec m;
+       mvec m;
 };
 
 #ifdef DEBUGFACTOR
-Matrix operator*(const Matrix& m1, const Matrix& m2)
+modular_matrix operator*(const modular_matrix& m1, const modular_matrix& m2)
 {
        const unsigned int r = m1.rowsize();
        const unsigned int c = m2.colsize();
-       Matrix o(r,c,m1(0,0));
+       modular_matrix o(r,c,m1(0,0));
 
        for ( size_t i=0; i<r; ++i ) {
                for ( size_t j=0; j<c; ++j ) {
@@ -840,7 +479,7 @@ Matrix operator*(const Matrix& m1, const Matrix& m2)
        return o;
 }
 
-ostream& operator<<(ostream& o, const Matrix& m)
+ostream& operator<<(ostream& o, const modular_matrix& m)
 {
        vector<cl_MI>::const_iterator i = m.m.begin(), end = m.m.end();
        size_t wrap = 1;
@@ -855,10 +494,13 @@ ostream& operator<<(ostream& o, const Matrix& m)
 }
 #endif // def DEBUGFACTOR
 
-static void q_matrix(const UniPoly& a, Matrix& Q)
+// END modular matrix
+////////////////////////////////////////////////////////////////////////////////
+
+static void q_matrix(const umod& a, modular_matrix& Q)
 {
-       unsigned int n = a.degree();
-       unsigned int q = cl_I_to_uint(a.R->modulus);
+       int n = degree(a);
+       unsigned int q = cl_I_to_uint(a.ring()->basering()->modulus);
 // fast and buggy
 //     vector<cl_MI> r(n, a.R->zero());
 //     r[0] = a.R->one();
@@ -875,20 +517,21 @@ static void q_matrix(const UniPoly& a, Matrix& Q)
 //             }
 //     }
 // slow and (hopefully) correct
-       for ( size_t i=0; i<n; ++i ) {
-               UniPoly qk(a.R);
-               qk.set(i*q, a.R->one());
-               UniPoly r(a.R);
-               rem(qk, a, r);
-               Vec rvec;
-               for ( size_t j=0; j<n; ++j ) {
-                       rvec.push_back(r[j]);
+       cl_MI one = a.ring()->basering()->one();
+       for ( int i=0; i<n; ++i ) {
+               umod qk = a.ring()->create(i*q);
+               qk.set_coeff(i*q, one);
+               qk.finalize();
+               umod r = rem(qk, a);
+               mvec rvec;
+               for ( int j=0; j<n; ++j ) {
+                       rvec.push_back(coeff(r, j));
                }
                Q.set_row(i, rvec);
        }
 }
 
-static void nullspace(Matrix& M, vector<Vec>& basis)
+static void nullspace(modular_matrix& M, vector<mvec>& basis)
 {
        const size_t n = M.rowsize();
        const cl_MI one = M(0,0).ring()->one();
@@ -926,63 +569,63 @@ static void nullspace(Matrix& M, vector<Vec>& basis)
        }
        for ( size_t i=0; i<n; ++i ) {
                if ( !M.is_row_zero(i) ) {
-                       Vec nu(M.row_begin(i), M.row_end(i));
+                       mvec nu(M.row_begin(i), M.row_end(i));
                        basis.push_back(nu);
                }
        }
 }
 
-static void berlekamp(const UniPoly& a, UniPolyVec& upv)
+static void berlekamp(const umod& a, umodvec& upv)
 {
-       Matrix Q(a.degree(), a.degree(), a.R->zero());
+       cl_modint_ring R = a.ring()->basering();
+       const umod one = a.ring()->one();
+
+       modular_matrix Q(degree(a), degree(a), R->zero());
        q_matrix(a, Q);
-       VecVec nu;
+       vector<mvec> nu;
        nullspace(Q, nu);
        const unsigned int k = nu.size();
        if ( k == 1 ) {
                return;
        }
 
-       list<UniPoly> factors;
+       list<umod> factors;
        factors.push_back(a);
        unsigned int size = 1;
        unsigned int r = 1;
-       unsigned int q = cl_I_to_uint(a.R->modulus);
+       unsigned int q = cl_I_to_uint(R->modulus);
 
-       list<UniPoly>::iterator u = factors.begin();
+       list<umod>::iterator u = factors.begin();
 
        while ( true ) {
                for ( unsigned int s=0; s<q; ++s ) {
-                       UniPoly g(a.R);
-                       UniPoly nur(a.R, nu[r]);
-                       nur.set(0, nur[0] - cl_MI(a.R, s));
-                       gcd(nur, *u, g);
-                       if ( !is_one(g) && g != *u ) {
-                               UniPoly uo(a.R);
-                               div(*u, g, uo);
-                               if ( is_one(uo) ) {
+                       umod nur = umod_from_modvec(nu[r]);
+                       cl_MI buf = coeff(nur, 0) - cl_MI(R, s);
+                       nur.set_coeff(0, buf);
+                       nur.finalize();
+                       umod g = gcd(nur, *u);
+                       if ( g != one && g != *u ) {
+                               umod uo = div(*u, g);
+                               if ( uo == one ) {
                                        throw logic_error("berlekamp: unexpected divisor.");
                                }
                                else {
-                                       *u = uo;
+                                       *u = COPY((*u), uo);
                                }
                                factors.push_back(g);
                                size = 0;
-                               list<UniPoly>::const_iterator i = factors.begin(), end = factors.end();
+                               list<umod>::const_iterator i = factors.begin(), end = factors.end();
                                while ( i != end ) {
-                                       if ( i->degree() ) ++size; 
+                                       if ( degree(*i) ) ++size; 
                                        ++i;
                                }
                                if ( size == k ) {
-                                       list<UniPoly>::const_iterator i = factors.begin(), end = factors.end();
+                                       list<umod>::const_iterator i = factors.begin(), end = factors.end();
                                        while ( i != end ) {
                                                upv.push_back(*i++);
                                        }
                                        return;
                                }
-//                             if ( u->degree() < nur.degree() ) {
-//                                     break;
-//                             }
                        }
                }
                if ( ++r == k ) {
@@ -992,43 +635,245 @@ static void berlekamp(const UniPoly& a, UniPolyVec& upv)
        }
 }
 
-static void factor_modular(const UniPoly& p, UniPolyVec& upv)
+static umod rem_xq(int q, const umod& b)
+{
+       cl_univpoly_modint_ring UPR = b.ring();
+       cl_modint_ring R = UPR->basering();
+
+       int n = degree(b);
+       if ( n > q ) {
+               umod c = UPR->create(q);
+               c.set_coeff(q, R->one());
+               c.finalize();
+               return c;
+       }
+
+       mvec c(n+1, R->zero());
+       int k = q-n;
+       c[n] = R->one();
+       DCOUTVAR(k);
+
+       int ofs = 0;
+       do {
+               cl_MI qk = div(c[n-ofs], coeff(b, n));
+               if ( !zerop(qk) ) {
+                       for ( int i=1; i<=n; ++i ) {
+                               c[n-i+ofs] = c[n-i] - qk * coeff(b, n-i);
+                       }
+                       ofs = ofs ? 0 : 1;
+                       DCOUTVAR(ofs);
+                       DCOUTVAR(c);
+               }
+       } while ( k-- );
+
+       if ( ofs ) {
+               c.pop_back();
+       }
+       else {
+               c.erase(c.begin());
+       }
+       umod res = umod_from_modvec(c);
+       return res;
+}
+
+static void distinct_degree_factor(const umod& a_, umodvec& result)
+{
+       umod a = COPY(a, a_);
+
+       DCOUT(distinct_degree_factor);
+       DCOUTVAR(a);
+
+       cl_univpoly_modint_ring UPR = a.ring();
+       cl_modint_ring R = UPR->basering();
+       int q = cl_I_to_int(R->modulus);
+       int n = degree(a);
+       size_t nhalf = n/2;
+
+
+       size_t i = 1;
+       umod w = UPR->create(1);
+       w.set_coeff(1, R->one());
+       w.finalize();
+       umod x = COPY(x, w);
+
+       umodvec ai;
+
+       while ( i <= nhalf ) {
+               w = expt_pos(w, q);
+               w = rem(w, a);
+
+               ai.push_back(gcd(a, w-x));
+
+               if ( ai.back() != UPR->one() ) {
+                       a = div(a, ai.back());
+                       w = rem(w, a);
+               }
+
+               ++i;
+       }
+
+       result = ai;
+       DCOUTVAR(result);
+       DCOUT(END distinct_degree_factor);
+}
+
+static void same_degree_factor(const umod& a, umodvec& result)
 {
+       DCOUT(same_degree_factor);
+
+       cl_univpoly_modint_ring UPR = a.ring();
+       cl_modint_ring R = UPR->basering();
+       int deg = degree(a);
+
+       umodvec buf;
+       distinct_degree_factor(a, buf);
+       int degsum = 0;
+
+       for ( size_t i=0; i<buf.size(); ++i ) {
+               if ( buf[i] != UPR->one() ) {
+                       degsum += degree(buf[i]);
+                       umodvec upv;
+                       berlekamp(buf[i], upv);
+                       for ( size_t j=0; j<upv.size(); ++j ) {
+                               result.push_back(upv[j]);
+                       }
+               }
+       }
+
+       if ( degsum < deg ) {
+               result.push_back(a);
+       }
+
+       DCOUTVAR(result);
+       DCOUT(END same_degree_factor);
+}
+
+static void distinct_degree_factor_BSGS(const umod& a, umodvec& result)
+{
+       DCOUT(distinct_degree_factor_BSGS);
+       DCOUTVAR(a);
+
+       cl_univpoly_modint_ring UPR = a.ring();
+       cl_modint_ring R = UPR->basering();
+       int q = cl_I_to_int(R->modulus);
+       int n = degree(a);
+
+       cl_N pm = 0.3;
+       int l = cl_I_to_int(ceiling1(the<cl_F>(expt(n, pm))));
+       DCOUTVAR(l);
+       umodvec h(l+1, UPR->create(-1));
+       umod qk = UPR->create(1);
+       qk.set_coeff(1, R->one());
+       qk.finalize();
+       h[0] = qk;
+       DCOUTVAR(h[0]);
+       for ( int i=1; i<=l; ++i ) {
+               qk = expt_pos(h[i-1], q);
+               h[i] = rem(qk, a);
+               DCOUTVAR(i);
+               DCOUTVAR(h[i]);
+       }
+
+       int m = std::ceil(((double)n)/2/l);
+       DCOUTVAR(m);
+       umodvec H(m, UPR->create(-1));
+       int ql = std::pow(q, l);
+       H[0] = COPY(H[0], h[l]);
+       DCOUTVAR(H[0]);
+       for ( int i=1; i<m; ++i ) {
+               qk = expt_pos(H[i-1], ql);
+               H[i] = rem(qk, a);
+               DCOUTVAR(i);
+               DCOUTVAR(H[i]);
+       }
+
+       umodvec I(m, UPR->create(-1));
+       for ( int i=0; i<m; ++i ) {
+               I[i] = UPR->one();
+               for ( int j=0; j<l; ++j ) {
+                       I[i] = I[i] * (H[i] - h[j]);
+               }
+               DCOUTVAR(i);
+               DCOUTVAR(I[i]);
+               I[i] = rem(I[i], a);
+               DCOUTVAR(I[i]);
+       }
+
+       umodvec F(m, UPR->one());
+       umod f = COPY(f, a);
+       for ( int i=0; i<m; ++i ) {
+               DCOUTVAR(i);
+               umod g = gcd(f, I[i]); 
+               if ( g == UPR->one() ) continue;
+               F[i] = g;
+               f = div(f, g);
+               DCOUTVAR(F[i]);
+       }
+
+       result.resize(n, UPR->one());
+       if ( f != UPR->one() ) {
+               result[n] = f;
+       }
+       for ( int i=0; i<m; ++i ) {
+               DCOUTVAR(i);
+               umod f = COPY(f, F[i]);
+               for ( int j=l-1; j>=0; --j ) {
+                       umod g = gcd(f, H[i]-h[j]);
+                       result[l*(i+1)-j-1] = g;
+                       f = div(f, g);
+               }
+       }
+
+       DCOUTVAR(result);
+       DCOUT(END distinct_degree_factor_BSGS);
+}
+
+static void cantor_zassenhaus(const umod& a, umodvec& result)
+{
+}
+
+static void factor_modular(const umod& p, umodvec& upv)
+{
+       //same_degree_factor(p, upv);
        berlekamp(p, upv);
        return;
 }
 
-static void exteuclid(const UniPoly& a, const UniPoly& b, UniPoly& g, UniPoly& s, UniPoly& t)
+static void exteuclid(const umod& a, const umod& b, umod& g, umod& s, umod& t)
 {
-       if ( a.degree() < b.degree() ) {
+       if ( degree(a) < degree(b) ) {
                exteuclid(b, a, g, t, s);
                return;
        }
-       UniPoly c1(a.R), c2(a.R), d1(a.R), d2(a.R), q(a.R), r(a.R), r1(a.R), r2(a.R);
-       UniPoly c = a; c.unit_normal();
-       UniPoly d = b; d.unit_normal();
-       c1.set(0, a.R->one());
-       d2.set(0, a.R->one());
-       while ( !d.zero() ) {
-               q.terms.clear();
-               div(c, d, q);
-               r = c - q * d;
-               r1 = c1 - q * d1;
-               r2 = c2 - q * d2;
-               c = d;
-               c1 = d1;
-               c2 = d2;
-               d = r;
-               d1 = r1;
-               d2 = r2;
-       }
-       g = c; g.unit_normal();
-       s = c1;
-       s.divide(a.unit());
-       s.divide(c.unit());
-       t = c2;
-       t.divide(b.unit());
-       t.divide(c.unit());
+       umod c = COPY(c, a); unit_normal(c);
+       umod d = COPY(d, b); unit_normal(d);
+       umod c1 = a.ring()->one();
+       umod c2 = a.ring()->create(-1);
+       umod d1 = a.ring()->create(-1);
+       umod d2 = a.ring()->one();
+       while ( !zerop(d) ) {
+               umod q = div(c, d);
+               umod r = c - q * d;
+               umod r1 = c1 - q * d1;
+               umod r2 = c2 - q * d2;
+               c = COPY(c, d);
+               c1 = COPY(c1, d1);
+               c2 = COPY(c2, d2);
+               d = COPY(d, r);
+               d1 = COPY(d1, r1);
+               d2 = COPY(d2, r2);
+       }
+       g = COPY(g, c); unit_normal(g);
+       s = COPY(s, c1);
+       for ( int i=0; i<=degree(s); ++i ) {
+               s.set_coeff(i, coeff(s, i) * recip(coeff(a, degree(a)) * coeff(c, degree(c))));
+       }
+       s.finalize();
+       t = COPY(t, c2);
+       for ( int i=0; i<=degree(t); ++i ) {
+               t.set_coeff(i, coeff(t, i) * recip(coeff(b, degree(b)) * coeff(c, degree(c))));
+       }
+       t.finalize();
 }
 
 static ex replace_lc(const ex& poly, const ex& x, const ex& lc)
@@ -1037,10 +882,11 @@ static ex replace_lc(const ex& poly, const ex& x, const ex& lc)
        return r;
 }
 
-static ex hensel_univar(const ex& a_, const ex& x, unsigned int p, const UniPoly& u1_, const UniPoly& w1_, const ex& gamma_ = 0)
+static ex hensel_univar(const ex& a_, const ex& x, unsigned int p, const umod& u1_, const umod& w1_, const ex& gamma_ = 0)
 {
        ex a = a_;
-       const cl_modint_ring& R = u1_.R;
+       const cl_univpoly_modint_ring& UPR = u1_.ring();
+       const cl_modint_ring& R = UPR->basering();
 
        // calc bound B
        ex maxcoeff;
@@ -1048,8 +894,8 @@ static ex hensel_univar(const ex& a_, const ex& x, unsigned int p, const UniPoly
                maxcoeff += pow(abs(a.coeff(x, i)),2);
        }
        cl_I normmc = ceiling1(the<cl_R>(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 maxdegree = (degree(u1_) > degree(w1_)) ? degree(u1_) : degree(w1_);
+       cl_I B = normmc * expt_pos(cl_I(2), maxdegree);
 
        // step 1
        ex alpha = a.lcoeff(x);
@@ -1057,43 +903,45 @@ 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();
-       UniPoly nw1 = w1_;
-       nw1.unit_normal();
+       umod nu1 = COPY(nu1, u1_);
+       unit_normal(nu1);
+       umod nw1 = COPY(nw1, w1_);
+       unit_normal(nw1);
        ex phi;
-       phi = expand(gamma * nu1.to_ex(x));
-       UniPoly u1(R, phi, x);
-       phi = expand(alpha * nw1.to_ex(x));
-       UniPoly w1(R, phi, x);
+       phi = gamma * umod_to_ex(nu1, x);
+       umod u1 = umod_from_ex(phi, x, R);
+       phi = alpha * umod_to_ex(nw1, x);
+       umod w1 = umod_from_ex(phi, x, R);
 
        // step 2
-       UniPoly s(R), t(R), g(R);
+       umod g = UPR->create(-1);
+       umod s = UPR->create(-1);
+       umod t = UPR->create(-1);
        exteuclid(u1, w1, g, s, t);
 
        // step 3
-       ex u = replace_lc(u1.to_ex(x), x, gamma);
-       ex w = replace_lc(w1.to_ex(x), x, alpha);
+       ex u = replace_lc(umod_to_ex(u1, x), x, gamma);
+       ex w = replace_lc(umod_to_ex(w1, 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);
-               phi = expand(t.to_ex(x)*c);
-               UniPoly tautilde(R, phi, x);
-               UniPoly q(R), r(R);
-               div(sigmatilde, w1, q);
-               rem(sigmatilde, w1, r);
-               UniPoly sigma = r;
-               phi = expand(tautilde.to_ex(x) + q.to_ex(x) * u1.to_ex(x));
-               UniPoly tau(R, phi, x);
-               u = expand(u + tau.to_ex(x) * modulus);
-               w = expand(w + sigma.to_ex(x) * modulus);
+               phi = expand(umod_to_ex(s, x) * c);
+               umod sigmatilde = umod_from_ex(phi, x, R);
+               phi = expand(umod_to_ex(t, x) * c);
+               umod tautilde = umod_from_ex(phi, x, R);
+               umod q = UPR->create(-1);
+               umod r = remdiv(sigmatilde, w1, q);
+               umod sigma = COPY(sigma, r);
+               phi = expand(umod_to_ex(tautilde, x) + umod_to_ex(q, x) * umod_to_ex(u1, x));
+               umod tau = umod_from_ex(phi, x, R);
+               u = expand(u + umod_to_ex(tau, x) * modulus);
+               w = expand(w + umod_to_ex(sigma, x) * modulus);
                e = expand(a - u * w);
                modulus = modulus * p;
        }
@@ -1180,10 +1028,10 @@ private:
        vector<int> k;
 };
 
-static void split(const UniPolyVec& factors, const Partition& part, UniPoly& a, UniPoly& b)
+static void split(const umodvec& factors, const Partition& part, umod& a, umod& b)
 {
-       a.set(0, a.R->one());
-       b.set(0, a.R->one());
+       a = factors.front().ring()->one();
+       b = factors.front().ring()->one();
        for ( size_t i=0; i<part.size(); ++i ) {
                if ( part[i] ) {
                        b = b * factors[i];
@@ -1197,36 +1045,56 @@ static void split(const UniPolyVec& factors, const Partition& part, UniPoly& a,
 struct ModFactors
 {
        ex poly;
-       UniPolyVec factors;
+       umodvec factors;
 };
 
 static ex factor_univariate(const ex& poly, const ex& x)
 {
+       DCOUT(factor_univariate);
+       DCOUTVAR(poly);
+
        ex unit, cont, prim;
        poly.unitcontprim(x, unit, cont, prim);
 
-       // determine proper prime
-       unsigned int p = 3;
-       cl_modint_ring R = find_modint_ring(p);
-       while ( true ) {
-               if ( irem(ex_to<numeric>(prim.lcoeff(x)), p) != 0 ) {
-                       UniPoly modpoly(R, prim, x);
-                       UniFactorVec sqrfree_ufv;
-                       squarefree(modpoly, sqrfree_ufv);
-                       if ( sqrfree_ufv.factors.size() == 1 && sqrfree_ufv.factors.front().exp == 1 ) break;
-               }
-               p = next_prime(p);
-               R = find_modint_ring(p);
-       }
-
-       // do modular factorization
-       UniPoly modpoly(R, prim, x);
-       UniPolyVec factors;
-       factor_modular(modpoly, factors);
-       if ( factors.size() <= 1 ) {
-               // irreducible for sure
-               return poly;
+       // determine proper prime and minimize number of modular factors
+       unsigned int p = 3, lastp = 3;
+       cl_modint_ring R;
+       unsigned int trials = 0;
+       unsigned int minfactors = 0;
+       numeric lcoeff = ex_to<numeric>(prim.lcoeff(x));
+       umodvec factors;
+       while ( trials < 2 ) {
+               while ( true ) {
+                       p = next_prime(p);
+                       if ( irem(lcoeff, p) != 0 ) {
+                               R = find_modint_ring(p);
+                               umod modpoly = umod_from_ex(prim, x, R);
+                               if ( squarefree(modpoly) ) break;
+                       }
+               }
+
+               // do modular factorization
+               umod modpoly = umod_from_ex(prim, x, R);
+               umodvec trialfactors;
+               factor_modular(modpoly, trialfactors);
+               if ( trialfactors.size() <= 1 ) {
+                       // irreducible for sure
+                       return poly;
+               }
+
+               if ( minfactors == 0 || trialfactors.size() < minfactors ) {
+                       factors = trialfactors;
+                       minfactors = factors.size();
+                       lastp = p;
+                       trials = 1;
+               }
+               else {
+                       ++trials;
+               }
        }
+       p = lastp;
+       R = find_modint_ring(p);
+       cl_univpoly_modint_ring UPR = find_univpoly_ring(R);
 
        // lift all factor combinations
        stack<ModFactors> tocheck;
@@ -1239,7 +1107,8 @@ static ex factor_univariate(const ex& poly, const ex& x)
                const size_t n = tocheck.top().factors.size();
                Partition part(n);
                while ( true ) {
-                       UniPoly a(R), b(R);
+                       umod a = UPR->create(-1);
+                       umod b = UPR->create(-1);
                        split(tocheck.top().factors, part, a, b);
 
                        ex answer = hensel_univar(tocheck.top().poly, x, p, a, b);
@@ -1277,8 +1146,8 @@ static ex factor_univariate(const ex& poly, const ex& x)
                                        break;
                                }
                                else {
-                                       UniPolyVec newfactors1(part.size_first(), R), newfactors2(part.size_second(), R);
-                                       UniPolyVec::iterator i1 = newfactors1.begin(), i2 = newfactors2.begin();
+                                       umodvec newfactors1(part.size_first(), UPR->create(-1)), newfactors2(part.size_second(), UPR->create(-1));
+                                       umodvec::iterator i1 = newfactors1.begin(), i2 = newfactors2.begin();
                                        for ( size_t i=0; i<n; ++i ) {
                                                if ( part[i] ) {
                                                        *i2++ = tocheck.top().factors[i];
@@ -1306,31 +1175,22 @@ static ex factor_univariate(const ex& poly, const ex& x)
                }
        }
 
+       DCOUT(END factor_univariate);
        return unit * cont * result;
 }
 
-struct FindSymbolsMap : public map_function {
-       exset syms;
-       ex operator()(const ex& e)
-       {
-               if ( is_a<symbol>(e) ) {
-                       syms.insert(e);
-                       return e;
-               }
-               return e.map(*this);
-       }
-};
-
 struct EvalPoint
 {
        ex x;
        int evalpoint;
 };
 
+// MARK
+
 // 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)
+umodvec multiterm_eea_lift(const umodvec& a, const ex& x, unsigned int p, unsigned int k)
 {
        DCOUT(multiterm_eea_lift);
        DCOUTVAR(a);
@@ -1340,27 +1200,26 @@ UniPolyVec multiterm_eea_lift(const UniPolyVec& a, const ex& x, unsigned int p,
        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);
+       cl_univpoly_modint_ring UPR = find_univpoly_ring(R);
+       umodvec q(r-1, UPR->create(-1));
        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;
+       umod beta = UPR->one();
+       umodvec 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);
+               mdarg[0] = umod_to_ex(q[j-1], x);
+               mdarg[1] = umod_to_ex(a[j-1], 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;
+               vector<ex> exsigma = multivar_diophant(mdarg, x, umod_to_ex(beta, x), empty, 0, p, k);
+               umod sigma1 = umod_from_ex(exsigma[0], x, R);
+               umod sigma2 = umod_from_ex(exsigma[1], x, R);
+               beta = COPY(beta, sigma1);
                s.push_back(sigma2);
        }
        s.push_back(beta);
@@ -1370,69 +1229,72 @@ UniPolyVec multiterm_eea_lift(const UniPolyVec& a, const ex& x, unsigned int p,
        return s;
 }
 
-void eea_lift(const UniPoly& a, const UniPoly& b, const ex& x, unsigned int p, unsigned int k, UniPoly& s_, UniPoly& t_)
+void change_modulus(umod& out, const umod& in)
+{
+       // ASSERT: out and in have same degree
+       if ( out.ring() == in.ring() ) {
+               out = COPY(out, in);
+       }
+       else {
+               for ( int i=0; i<=degree(in); ++i ) {
+                       out.set_coeff(i, out.ring()->basering()->canonhom(in.ring()->basering()->retract(coeff(in, i))));
+               }
+               out.finalize();
+       }
+}
+
+void eea_lift(const umod& a, const umod& b, const ex& x, unsigned int p, unsigned int k, umod& s_, umod& 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);
+       cl_univpoly_modint_ring UPR = find_univpoly_ring(R);
+       umod amod = UPR->create(degree(a));
+       change_modulus(amod, a);
+       umod bmod = UPR->create(degree(b));
+       change_modulus(bmod, b);
+
+       umod g = UPR->create(-1);
+       umod smod = UPR->create(-1);
+       umod tmod = UPR->create(-1);
        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_univpoly_modint_ring UPRpk = find_univpoly_ring(Rpk);
+       umod s = UPRpk->create(degree(smod));
+       change_modulus(s, smod);
+       umod t = UPRpk->create(degree(tmod));
+       change_modulus(t, tmod);
 
        cl_I modulus(p);
-
-       UniPoly one(Rpk);
-       one.set(0, Rpk->one());
+       umod one = UPRpk->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);
+               umod e = one - a * s - b * t;
+               e = divide(e, modulus);
+               umod c = UPR->create(degree(e));
+               change_modulus(c, e);
+               umod sigmabar = smod * c;
+               umod taubar = tmod * c;
+               umod q = UPR->create(-1);
+               umod sigma = remdiv(sigmabar, bmod, q);
+               umod tau = taubar + q * amod;
+               umod sadd = UPRpk->create(degree(sigma));
+               change_modulus(sadd, sigma);
                cl_MI modmodulus(Rpk, modulus);
                s = s + sadd * modmodulus;
-               UniPoly tadd(Rpk, tau);
+               umod tadd = UPRpk->create(degree(tau));
+               change_modulus(tadd, 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)
+umodvec univar_diophant(const umodvec& a, const ex& x, unsigned int m, unsigned int p, unsigned int k)
 {
        DCOUT(univar_diophant);
        DCOUTVAR(a);
@@ -1442,33 +1304,32 @@ UniPolyVec univar_diophant(const UniPolyVec& a, const ex& x, unsigned int m, uns
        DCOUTVAR(k);
 
        cl_modint_ring R = find_modint_ring(expt_pos(cl_I(p),k));
+       cl_univpoly_modint_ring UPR = find_univpoly_ring(R);
 
        const size_t r = a.size();
-       UniPolyVec result;
+       umodvec result;
        if ( r > 2 ) {
-               UniPolyVec s = multiterm_eea_lift(a, x, p, k);
+               umodvec 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);
+                       ex phi = expand(pow(x,m) * umod_to_ex(s[j], x));
+                       umod bmod = umod_from_ex(phi, x, R);
+                       umod buf = rem(bmod, a[j]);
                        result.push_back(buf);
                }
        }
        else {
-               UniPoly s(R), t(R);
+               umod s = UPR->create(-1);
+               umod t = UPR->create(-1);
                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];
+               ex phi = expand(pow(x,m) * umod_to_ex(s, x));
+               umod bmod = umod_from_ex(phi, x, R);
+               umod q = UPR->create(-1);
+               umod buf = remdiv(bmod, a[0], q);
                result.push_back(buf);
+               phi = expand(pow(x,m) * umod_to_ex(t, x));
+               umod t1mod = umod_from_ex(phi, x, R);
+               umod buf2 = t1mod + q * a[1];
+               result.push_back(buf2);
        }
 
        DCOUTVAR(result);
@@ -1503,7 +1364,7 @@ struct make_modular_map : public map_function {
 static ex make_modular(const ex& e, const cl_modint_ring& R)
 {
        make_modular_map map(R);
-       return map(e);
+       return map(e.expand());
 }
 
 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)
@@ -1553,44 +1414,54 @@ vector<ex> multivar_diophant(const vector<ex>& a_, const ex& x, const ex& c, con
 
                vector<ex> anew = a;
                for ( size_t i=0; i<r; ++i ) {
-                       a[i] = a[i].subs(xnu == alphanu);
+                       anew[i] = anew[i].subs(xnu == alphanu);
                }
                ex cnew = c.subs(xnu == alphanu);
                vector<EvalPoint> Inew = I;
                Inew.pop_back();
-               vector<ex> sigma = multivar_diophant(anew, x, cnew, Inew, d, p, k);
+               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);
+               ex e = make_modular(buf, R);
 
+               DCOUTVAR(e);
+               DCOUTVAR(d);
                ex monomial = 1;
                for ( size_t m=1; m<=d; ++m ) {
-                       while ( !e.is_zero() ) {
+                       DCOUTVAR(m);
+                       while ( !e.is_zero() && e.has(xnu) ) {
                                monomial *= (xnu - alphanu);
                                monomial = expand(monomial);
+                               DCOUTVAR(monomial);
+                               DCOUTVAR(xnu);
+                               DCOUTVAR(alphanu);
                                ex cm = e.diff(ex_to<symbol>(xnu), m).subs(xnu==alphanu) / factorial(m);
+                               cm = make_modular(cm, R);
+                               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;
-                                       e = make_modular(e, R);
+                                       e = make_modular(buf, R);
+                                       DCOUTVAR(e);
                                }
                        }
                }
        }
        else {
-               UniPolyVec amod;
+               DCOUT(uniterm left);
+               umodvec amod;
                for ( size_t i=0; i<a.size(); ++i ) {
-                       UniPoly up(R, a[i], x);
+                       umod up = umod_from_ex(a[i], x, R);
                        amod.push_back(up);
                }
 
@@ -1612,7 +1483,7 @@ vector<ex> multivar_diophant(const vector<ex>& a_, const ex& x, const ex& c, con
                        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);
+                       umodvec delta_s = univar_diophant(amod, x, m, p, k);
                        cl_MI modcm;
                        cl_I poscm = cm;
                        while ( poscm < 0 ) {
@@ -1622,7 +1493,7 @@ vector<ex> multivar_diophant(const vector<ex>& a_, const ex& x, const ex& c, con
                        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);
+                               sigma[j] = sigma[j] + umod_to_ex(delta_s[j], x);
                        }
                        DCOUTVAR(delta_s);
 #ifdef DEBUGFACTOR
@@ -1671,7 +1542,7 @@ ostream& operator<<(ostream& o, const vector<EvalPoint>& v)
 #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)
+ex hensel_multivar(const ex& a, const ex& x, const vector<EvalPoint>& I, unsigned int p, const cl_I& l, const umodvec& u, const vector<ex>& lcU)
 {
        DCOUT(hensel_multivar);
        DCOUTVAR(a);
@@ -1713,7 +1584,7 @@ ex hensel_multivar(const ex& a, const ex& x, const vector<EvalPoint>& I, unsigne
        DCOUTVAR(n);
        vector<ex> U(n);
        for ( size_t i=0; i<n; ++i ) {
-               U[i] = u[i].to_ex(x);
+               U[i] = umod_to_ex(u[i], x);
        }
 #ifdef DEBUGFACTOR
        cout << "U ";
@@ -1725,18 +1596,19 @@ ex hensel_multivar(const ex& a, const ex& x, const vector<EvalPoint>& I, unsigne
                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];
@@ -1744,6 +1616,12 @@ ex hensel_multivar(const ex& a, const ex& x, const vector<EvalPoint>& I, unsigne
                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);
@@ -1761,8 +1639,6 @@ ex hensel_multivar(const ex& a, const ex& x, const vector<EvalPoint>& I, unsigne
                                ex c = dif.subs(xj==alphaj) / factorial(k);
                                DCOUTVAR(c);
                                if ( !c.is_zero() ) {
-                                       vector<EvalPoint> newI = I;
-                                       newI.pop_back();
                                        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);
@@ -1770,18 +1646,18 @@ ex hensel_multivar(const ex& a, const ex& x, const vector<EvalPoint>& I, unsigne
                                                deltaU[i] *= monomial;
                                                U[i] += deltaU[i];
                                                U[i] = make_modular(U[i], R);
+                                               DCOUTVAR(U[i]);
                                        }
                                        ex Uprod = 1;
                                        for ( size_t i=0; i<n; ++i ) {
                                                Uprod *= U[i];
                                        }
-                                       e = expand(A[j-1] - Uprod);
+                                       DCOUTVAR(Uprod.expand());
+                                       DCOUTVAR(A[j-1]);
+                                       e = A[j-1] - Uprod;
                                        e = make_modular(e, R);
                                        DCOUTVAR(e);
                                }
-                               else {
-                                       break;
-                               }
                        }
                }
        }
@@ -1829,7 +1705,7 @@ static ex put_factors_into_lst(const ex& e)
                DCOUTVAR(result);
                return result;
        }
-       if ( is_a<symbol>(e) ) {
+       if ( is_a<symbol>(e) || is_a<add>(e) ) {
                result.append(1);
                result.append(e);
                result.append(1);
@@ -1861,30 +1737,65 @@ static ex put_factors_into_lst(const ex& e)
        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 ) {
-               q = ex_to<numeric>(abs(f.op(i-1)));
+               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 void generate_set(const ex& u, const ex& vn, const exset& syms, const ex& f, const numeric& modulus, vector<numeric>& a, vector<numeric>& d)
+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 {
@@ -1894,51 +1805,59 @@ static void generate_set(const ex& u, const ex& vn, const exset& syms, const ex&
                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) ) {
-                       d = a;
                        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;
-                               s = syms.begin();
-                               ++s;
-                               for ( size_t j=0; j<a.size(); ++j ) {
-                                       fs = fs.subs(*s == a[j]);
+                               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
-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 ex factor_multivariate(const ex& poly, const exset& syms)
 {
        DCOUT(factor_multivariate);
@@ -1960,36 +1879,52 @@ static ex factor_multivariate(const ex& poly, const exset& syms)
        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 = p.lcoeff(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);
-       vector<numeric> d(syms.size()-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;
-               UniPolyVec uvec;
+               unsigned int prime = 3;
+               size_t factor_count = 0;
+               ex ufac;
+               ex ufaclst;
                while ( trialcount < maxtrials ) {
-                       uvec.clear();
-                       generate_set(pp, vn, syms, vnlst, modulus, a, d);
+                       bool problem = generate_set(pp, vn, syms, vnlst, modulus, a, d);
+                       DCOUTVAR(problem);
+                       if ( problem ) {
+                               ++modulus;
+                               continue;
+                       }
                        DCOUTVAR(a);
                        DCOUTVAR(d);
                        u = pp;
@@ -2000,134 +1935,182 @@ static ex factor_multivariate(const ex& poly, const exset& syms)
                                ++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);
-                                       if ( sqrfree_ufv.factors.size() == 1 && sqrfree_ufv.factors.front().exp == 1 ) break;
+                                       umod modpoly = umod_from_ex(u, x, R);
+                                       if ( squarefree(modpoly) ) break;
                                }
                                prime = next_prime(prime);
+                               DCOUTVAR(prime);
                                R = find_modint_ring(prime);
                        }
 
-                       UniPoly umod(R, u, x);
-                       DCOUTVAR(u);
-                       factor_modular(umod, uvec);
-                       DCOUTVAR(uvec);
+#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 ( uvec.size() == 1 ) {
+                       if ( factor_count <= 1 ) {
                                DCOUTVAR(poly);
                                DCOUT(END factor_multivariate);
                                return poly;
                        }
 
                        if ( minimalr < 0 ) {
-                               minimalr = uvec.size();
+                               minimalr = factor_count;
                        }
-                       else if ( minimalr == uvec.size() ) {
+                       else if ( minimalr == factor_count ) {
                                ++trialcount;
                                ++modulus;
                        }
-                       else if ( minimalr > uvec.size() ) {
-                               minimalr = uvec.size();
+                       else if ( minimalr > factor_count ) {
+                               minimalr = factor_count;
                                trialcount = 0;
                        }
                        DCOUTVAR(trialcount);
                        DCOUTVAR(minimalr);
-                       if ( minimalr == 0 ) {
+                       if ( minimalr <= 1 ) {
                                DCOUTVAR(poly);
                                DCOUT(END factor_multivariate);
                                return poly;
                        }
                }
 
-               vector<ex> C;
-               if ( vnlst.nops() == 1 ) {
-                       C.resize(uvec.size(), 1);
+               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);
                }
-               else {
+               DCOUTVAR(ftilde);
 
-                       vector<numeric> ftilde((vnlst.nops()-1)/2);
-                       for ( size_t i=0; i<ftilde.size(); ++i ) {
-                               ex ft = vnlst.op(i*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);
+               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;
                        }
-                       DCOUTVAR(ftilde);
-
-                       vector<ex> D;
-                       vector<bool> fflag(ftilde.size(), false);
-                       for ( size_t i=0; i<uvec.size(); ++i ) {
-                               ex ui = uvec[i].to_ex(x);
-                               ex Di = 1;
-                               numeric coeff = ex_to<numeric>(ui.lcoeff(x));
-                               for ( size_t j=0; j<ftilde.size(); ++j ) {
-                                       if ( numeric(coeff / ftilde[j]).is_integer() ) {
-                                               coeff = coeff / ftilde[j];
-                                               Di *= ftilde[j];
-                                               fflag[j] = true;
-                                               --j;
+                       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;
                                }
-                               D.push_back(Di.expand());
                        }
-                       for ( size_t i=0; i<fflag.size(); ++i ) {
-                               if ( !fflag[i] ) {
-                                       --minimalr;
-                                       continue;
-                               }
+               }
+               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;
                        }
-                       DCOUTVAR(D);
+               }
+               if ( some_factor_unused ) {
+                       DCOUT(some factor unused!);
+                       continue;
+               }
 
-                       C.resize(D.size());
-                       if ( delta == 1 ) {
-                               for ( size_t i=0; i<D.size(); ++i ) {
-                                       ex Dtilde = D[i];
-                                       s = syms.begin();
+               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;
-                                       for ( size_t j=0; j<a.size(); ++j ) {
-                                               Dtilde = Dtilde.subs(*s == a[j]);
-                                               ++s;
-                                       }
-                                       ex Ci = D[i] * (uvec[i].to_ex(x).lcoeff(x) / Dtilde);
-                                       C.push_back(Ci);
                                }
+                               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();
+               }
+               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;
-                                       for ( size_t j=0; j<a.size(); ++j ) {
-                                               Dtilde = Dtilde.subs(*s == a[j]);
-                                               ++s;
-                                       }
-                                       ex ui = uvec[i].to_ex(x);
-                                       ex Ci;
-                                       while ( true ) {
-                                               ex d = gcd(ui.lcoeff(x), Dtilde);
-                                               Ci = D[i] * ( ui.lcoeff(x) / d );
-                                               ui = ui * ( Dtilde[i] / d );
-                                               delta = delta / ( Dtilde[i] / d );
-                                               if ( delta == 1 ) break;
-                                               ui = delta * ui;
-                                               Ci = delta * Ci;
-                                               pp = pp * pow(delta, D.size()-1);
-                                       }
+                               }
+                               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;
@@ -2138,6 +2121,7 @@ static ex factor_multivariate(const ex& poly, const exset& syms)
                        ep.evalpoint = a[i].to_int();
                        epv.push_back(ep);
                }
+               DCOUTVAR(epv);
 
                // calc bound B
                ex maxcoeff;
@@ -2146,16 +2130,30 @@ static ex factor_multivariate(const ex& poly, const exset& syms)
                }
                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<uvec.size(); ++i ) {
-                       if ( uvec[i].degree() > maxdegree ) {
-                               maxdegree = uvec[i].degree();
+               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);
                        }
                }
-               unsigned int B = cl_I_to_uint(normmc * expt_pos(cl_I(2), maxdegree));
+               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;
+               }
+
+               umodvec 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 ) {
+                       umod newu = umod_from_ex(ufaclst.op(i*2+1), x, R);
+                       uvec.push_back(newu);
+               }
+               DCOUTVAR(uvec);
 
-               ex res = hensel_multivar(poly, x, epv, prime, B, uvec, C);
+               ex res = hensel_multivar(ufaclst.op(0)*pp, x, epv, prime, l, uvec, C);
                if ( res != lst() ) {
-                       ex result = cont;
+                       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);
@@ -2167,12 +2165,27 @@ static ex factor_multivariate(const ex& poly, const exset& syms)
        }
 }
 
+struct find_symbols_map : public map_function {
+       exset syms;
+       ex operator()(const ex& e)
+       {
+               if ( is_a<symbol>(e) ) {
+                       syms.insert(e);
+                       return e;
+               }
+               return e.map(*this);
+       }
+};
+
 static ex factor_sqrfree(const ex& poly)
 {
+       DCOUT(factor_sqrfree);
+
        // determine all symbols in poly
-       FindSymbolsMap findsymbols;
+       find_symbols_map findsymbols;
        findsymbols(poly);
        if ( findsymbols.syms.size() == 0 ) {
+               DCOUT(END factor_sqrfree);
                return poly;
        }
 
@@ -2182,25 +2195,76 @@ static ex factor_sqrfree(const ex& poly)
                if ( poly.ldegree(x) > 0 ) {
                        int ld = poly.ldegree(x);
                        ex res = factor_univariate(expand(poly/pow(x, ld)), x);
+                       DCOUT(END factor_sqrfree);
                        return res * pow(x,ld);
                }
                else {
                        ex res = factor_univariate(poly, x);
+                       DCOUT(END factor_sqrfree);
                        return res;
                }
        }
 
        // multivariate case
        ex res = factor_multivariate(poly, findsymbols.syms);
+       DCOUT(END factor_sqrfree);
        return res;
 }
 
+struct apply_factor_map : public map_function {
+       unsigned options;
+       apply_factor_map(unsigned options_) : options(options_) { }
+       ex operator()(const ex& e)
+       {
+               if ( e.info(info_flags::polynomial) ) {
+#ifdef DEBUGFACTOR
+                       return ::factor(e, options);
+#else
+                       return factor(e, options);
+#endif
+               }
+               if ( is_a<add>(e) ) {
+                       ex s1, s2;
+                       for ( size_t i=0; i<e.nops(); ++i ) {
+                               if ( e.op(i).info(info_flags::polynomial) ) {
+                                       s1 += e.op(i);
+                               }
+                               else {
+                                       s2 += e.op(i);
+                               }
+                       }
+                       s1 = s1.eval();
+                       s2 = s2.eval();
+#ifdef DEBUGFACTOR
+                       return ::factor(s1, options) + s2.map(*this);
+#else
+                       return factor(s1, options) + s2.map(*this);
+#endif
+               }
+               return e.map(*this);
+       }
+};
+
 } // anonymous namespace
 
-ex factor(const ex& poly)
+#ifdef DEBUGFACTOR
+ex factor(const ex& poly, unsigned options = 0)
+#else
+ex factor(const ex& poly, unsigned options)
+#endif
 {
+       // check arguments
+       if ( !poly.info(info_flags::polynomial) ) {
+               if ( options & factor_options::all ) {
+                       options &= ~factor_options::all;
+                       apply_factor_map factor_map(options);
+                       return factor_map(poly);
+               }
+               return poly;
+       }
+
        // determine all symbols in poly
-       FindSymbolsMap findsymbols;
+       find_symbols_map findsymbols;
        findsymbols(poly);
        if ( findsymbols.syms.size() == 0 ) {
                return poly;
@@ -2226,6 +2290,7 @@ ex factor(const ex& poly)
                return pow(f, sfpoly.op(1));
        }
        if ( is_a<mul>(sfpoly) ) {
+               // case: multiple factors
                ex res = 1;
                for ( size_t i=0; i<sfpoly.nops(); ++i ) {
                        const ex& t = sfpoly.op(i);