X-Git-Url: https://www.ginac.de/ginac.git//ginac.git?p=ginac.git;a=blobdiff_plain;f=ginac%2Ffactor.cpp;h=fb73897218db06ed31b40f32bf52aabca7e33acf;hp=204010be3750294640434dc28da698f9f452db69;hb=2b0ad5c381dc081cc4066b0c5f939f5169ad9ab3;hpb=edc92b7a463993da62357fb4afad053e8c6d0771 diff --git a/ginac/factor.cpp b/ginac/factor.cpp index 204010be..fb738972 100644 --- a/ginac/factor.cpp +++ b/ginac/factor.cpp @@ -365,10 +365,12 @@ static void umodpoly_from_ex(umodpoly& ump, const ex& e, const ex& x, const cl_m canonicalize(ump); } +#ifdef DEBUGFACTOR static void umodpoly_from_ex(umodpoly& ump, const ex& e, const ex& x, const cl_I& modulus) { umodpoly_from_ex(ump, e, x, find_modint_ring(modulus)); } +#endif static ex upoly_to_ex(const upoly& a, const ex& x) { @@ -423,7 +425,7 @@ static umodpoly umodpoly_to_umodpoly(const umodpoly& a, const cl_modint_ring& R, cl_modint_ring oldR = a[0].ring(); size_t sa = a.size(); e.resize(sa+m, R->zero()); - for ( int i=0; icanonhom(oldR->retract(a[i])); } canonicalize(e); @@ -606,7 +608,7 @@ static bool squarefree(const umodpoly& a) umodpoly b; deriv(a, b); if ( b.empty() ) { - return true; + return false; } umodpoly c; gcd(a, b, c); @@ -966,7 +968,6 @@ static void distinct_degree_factor(const umodpoly& a_, vector& degrees, upv w[1] = R->one(); umodpoly x = w; - bool nontrivial = false; while ( i <= nhalf ) { expt_pos(w, q); umodpoly buf; @@ -997,7 +998,6 @@ static void distinct_degree_factor(const umodpoly& a_, vector& degrees, upv static void same_degree_factor(const umodpoly& a, upvec& upv) { cl_modint_ring R = a[0].ring(); - int deg = degree(a); vector degrees; upvec ddfactors; @@ -1013,36 +1013,14 @@ static void same_degree_factor(const umodpoly& a, upvec& upv) } } +#define USE_SAME_DEGREE_FACTOR + static void factor_modular(const umodpoly& p, upvec& upv) { - upvec factors; - vector mult; - modsqrfree(p, factors, mult); - -#define USE_SAME_DEGREE_FACTOR #ifdef USE_SAME_DEGREE_FACTOR - for ( size_t i=0; i0; --j ) { - upv.insert(upv.end(), upvbuf.begin(), upvbuf.end()); - } - } + same_degree_factor(p, upv); #else - for ( size_t i=0; i0; --j ) { - upv.push_back(factors[i]); - } - } - } + berlekamp(p, upv); #endif } @@ -1104,19 +1082,42 @@ static upoly replace_lc(const upoly& poly, const cl_I& lc) return r; } +static inline cl_I calc_bound(const ex& a, const ex& x, int maxdeg) +{ + cl_I maxcoeff = 0; + cl_R coeff = 0; + for ( int i=a.degree(x); i>=a.ldegree(x); --i ) { + cl_I aa = abs(the(ex_to(a.coeff(x, i)).to_cl_N())); + if ( aa > maxcoeff ) maxcoeff = aa; + coeff = coeff + square(aa); + } + cl_I coeffnorm = ceiling1(the(cln::sqrt(coeff))); + cl_I B = coeffnorm * expt_pos(cl_I(2), cl_I(maxdeg)); + return ( B > maxcoeff ) ? B : maxcoeff; +} + +static inline cl_I calc_bound(const upoly& a, int maxdeg) +{ + cl_I maxcoeff = 0; + cl_R coeff = 0; + for ( int i=degree(a); i>=0; --i ) { + cl_I aa = abs(a[i]); + if ( aa > maxcoeff ) maxcoeff = aa; + coeff = coeff + square(aa); + } + cl_I coeffnorm = ceiling1(the(cln::sqrt(coeff))); + cl_I B = coeffnorm * expt_pos(cl_I(2), cl_I(maxdeg)); + return ( B > maxcoeff ) ? B : maxcoeff; +} + static void hensel_univar(const upoly& a_, unsigned int p, const umodpoly& u1_, const umodpoly& w1_, upoly& u, upoly& w) { upoly a = a_; const cl_modint_ring& R = u1_[0].ring(); // calc bound B - cl_R maxcoeff = 0; - for ( int i=degree(a); i>=0; --i ) { - maxcoeff = maxcoeff + square(abs(a[i])); - } - cl_I normmc = ceiling1(the(cln::sqrt(maxcoeff))); - cl_I maxdegree = (degree(u1_) > degree(w1_)) ? degree(u1_) : degree(w1_); - cl_I B = normmc * expt_pos(cl_I(2), maxdegree); + int maxdeg = (degree(u1_) > degree(w1_)) ? degree(u1_) : degree(w1_); + cl_I maxmodulus = 2*calc_bound(a, maxdeg); // step 1 cl_I alpha = lcoeff(a); @@ -1143,16 +1144,9 @@ static void hensel_univar(const upoly& a_, unsigned int p, const umodpoly& u1_, w = replace_lc(umodpoly_to_upoly(w1), alpha); upoly e = a - u * w; cl_I modulus = p; - const cl_I maxmodulus = 2*B*abs(alpha); // step 4 while ( !e.empty() && modulus < maxmodulus ) { - // ad-hoc divisablity check - for ( size_t k=0; kone()); + len = 1; + last = 0; split(); } int operator[](size_t i) const { return k[i]; } size_t size() const { return n; } - size_t size_first() const { return n-sum; } - size_t size_second() const { return sum; } + size_t size_left() const { return n-len; } + size_t size_right() const { return len; } #ifdef DEBUGFACTOR void get() const { DCOUTVAR(k); } #endif bool next() { - for ( size_t i=n-1; i>=1; --i ) { - if ( k[i] ) { - --k[i]; - --sum; - if ( sum > 0 ) { - split(); - return true; + if ( last == n-1 ) { + int rem = len - 1; + int p = last - 1; + while ( rem ) { + if ( k[p] ) { + --rem; + --p; + continue; } - else { - return false; + last = p - 1; + while ( k[last] == 0 ) { --last; } + if ( last == 0 && n == 2*len ) return false; + k[last++] = 0; + for ( size_t i=0; i<=len-rem; ++i ) { + k[last] = 1; + ++last; } + fill(k.begin()+last, k.end(), 0); + --last; + split(); + return true; } - ++k[i]; - ++sum; + last = len; + ++len; + if ( len > n/2 ) return false; + fill(k.begin(), k.begin()+len, 1); + fill(k.begin()+len+1, k.end(), 0); } - return false; + else { + k[last++] = 0; + k[last] = 1; + } + split(); + return true; } - void split() + umodpoly& left() { return lr[0]; } + umodpoly& right() { return lr[1]; } +private: + void split_cached() { - left = one; - right = one; - for ( size_t i=0; i= d ) { + lr[group] = lr[group] * cache[pos][d-1]; + } + else { + if ( cache[pos].size() == 0 ) { + cache[pos].push_back(factors[pos] * factors[pos+1]); + } + size_t j = pos + cache[pos].size() + 1; + d -= cache[pos].size(); + while ( d ) { + umodpoly buf = cache[pos].back() * factors[j]; + cache[pos].push_back(buf); + --d; + ++j; + } + lr[group] = lr[group] * cache[pos].back(); + } } else { - left = left * factors[i]; + lr[group] = lr[group] * factors[pos]; + } + } while ( i < n ); + } + void split() + { + lr[0] = one; + lr[1] = one; + if ( n > 6 ) { + split_cached(); + } + else { + for ( size_t i=0; i > cache; upvec factors; umodpoly one; - size_t n, sum; + size_t n; + size_t len; + size_t last; vector k; }; @@ -1289,7 +1339,7 @@ struct ModFactors upvec factors; }; -static ex factor_univariate(const ex& poly, const ex& x) +static ex factor_univariate(const ex& poly, const ex& x, unsigned int& prime) { ex unit, cont, prim_ex; poly.unitcontprim(x, unit, cont, prim_ex); @@ -1297,18 +1347,19 @@ static ex factor_univariate(const ex& poly, const ex& x) upoly_from_ex(prim, prim_ex, x); // determine proper prime and minimize number of modular factors - unsigned int p = 3, lastp = 3; + prime = 3; + unsigned int lastp = prime; cl_modint_ring R; unsigned int trials = 0; unsigned int minfactors = 0; - cl_I lc = lcoeff(prim); + cl_I lc = lcoeff(prim) * the(ex_to(cont).to_cl_N()); upvec factors; while ( trials < 2 ) { umodpoly modpoly; while ( true ) { - p = next_prime(p); - if ( !zerop(rem(lc, p)) ) { - R = find_modint_ring(p); + prime = next_prime(prime); + if ( !zerop(rem(lc, prime)) ) { + R = find_modint_ring(prime); umodpoly_from_upoly(modpoly, prim, R); if ( squarefree(modpoly) ) break; } @@ -1324,16 +1375,16 @@ static ex factor_univariate(const ex& poly, const ex& x) if ( minfactors == 0 || trialfactors.size() < minfactors ) { factors = trialfactors; - minfactors = factors.size(); - lastp = p; + minfactors = trialfactors.size(); + lastp = prime; trials = 1; } else { ++trials; } } - p = lastp; - R = find_modint_ring(p); + prime = lastp; + R = find_modint_ring(prime); // lift all factor combinations stack tocheck; @@ -1347,10 +1398,10 @@ static ex factor_univariate(const ex& poly, const ex& x) const size_t n = tocheck.top().factors.size(); factor_partition part(tocheck.top().factors); while ( true ) { - hensel_univar(tocheck.top().poly, p, part.left, part.right, f1, f2); + hensel_univar(tocheck.top().poly, prime, part.left(), part.right(), f1, f2); if ( !f1.empty() ) { - if ( part.size_first() == 1 ) { - if ( part.size_second() == 1 ) { + if ( part.size_left() == 1 ) { + if ( part.size_right() == 1 ) { result *= upoly_to_ex(f1, x) * upoly_to_ex(f2, x); tocheck.pop(); break; @@ -1365,8 +1416,8 @@ static ex factor_univariate(const ex& poly, const ex& x) } break; } - else if ( part.size_second() == 1 ) { - if ( part.size_first() == 1 ) { + else if ( part.size_right() == 1 ) { + if ( part.size_left() == 1 ) { result *= upoly_to_ex(f1, x) * upoly_to_ex(f2, x); tocheck.pop(); break; @@ -1382,7 +1433,7 @@ static ex factor_univariate(const ex& poly, const ex& x) break; } else { - upvec newfactors1(part.size_first()), newfactors2(part.size_second()); + upvec newfactors1(part.size_left()), newfactors2(part.size_right()); upvec::iterator i1 = newfactors1.begin(), i2 = newfactors2.begin(); for ( size_t i=0; i multivar_diophant(const vector& a_, const ex& x, const ex& c, const vector& I, unsigned int d, unsigned int p, unsigned int k); +static vector multivar_diophant(const vector& a_, const ex& x, const ex& c, const vector& I, unsigned int d, unsigned int p, unsigned int k); -upvec multiterm_eea_lift(const upvec& a, const ex& x, unsigned int p, unsigned int k) +static upvec multiterm_eea_lift(const upvec& a, const ex& x, unsigned int p, unsigned int k) { const size_t r = a.size(); cl_modint_ring R = find_modint_ring(expt_pos(cl_I(p),k)); @@ -1454,7 +1511,7 @@ upvec multiterm_eea_lift(const upvec& a, const ex& x, unsigned int p, unsigned i /** * Assert: a not empty. */ -void change_modulus(const cl_modint_ring& R, umodpoly& a) +static void change_modulus(const cl_modint_ring& R, umodpoly& a) { if ( a.empty() ) return; cl_modint_ring oldR = a[0].ring(); @@ -1465,7 +1522,7 @@ void change_modulus(const cl_modint_ring& R, umodpoly& a) canonicalize(a); } -void eea_lift(const umodpoly& a, const umodpoly& b, const ex& x, unsigned int p, unsigned int k, umodpoly& s_, umodpoly& t_) +static void eea_lift(const umodpoly& a, const umodpoly& b, const ex& x, unsigned int p, unsigned int k, umodpoly& s_, umodpoly& t_) { cl_modint_ring R = find_modint_ring(p); umodpoly amod = a; @@ -1508,7 +1565,7 @@ void eea_lift(const umodpoly& a, const umodpoly& b, const ex& x, unsigned int p, s_ = s; t_ = t; } -upvec univar_diophant(const upvec& a, const ex& x, unsigned int m, unsigned int p, unsigned int k) +static upvec univar_diophant(const upvec& a, const ex& x, unsigned int m, unsigned int p, unsigned int k) { cl_modint_ring R = find_modint_ring(expt_pos(cl_I(p),k)); @@ -1568,7 +1625,8 @@ static ex make_modular(const ex& e, const cl_modint_ring& R) return map(e.expand()); } -vector multivar_diophant(const vector& a_, const ex& x, const ex& c, const vector& I, unsigned int d, unsigned int p, unsigned int k) +static vector multivar_diophant(const vector& a_, const ex& x, const ex& c, const vector& I, + unsigned int d, unsigned int p, unsigned int k) { vector a = a_; @@ -1606,22 +1664,20 @@ vector multivar_diophant(const vector& a_, const ex& x, const ex& c, con ex e = make_modular(buf, R); ex monomial = 1; - for ( size_t m=1; m<=d; ++m ) { - while ( !e.is_zero() && e.has(xnu) ) { - monomial *= (xnu - alphanu); - monomial = expand(monomial); - ex cm = e.diff(ex_to(xnu), m).subs(xnu==alphanu) / factorial(m); - cm = make_modular(cm, R); - if ( !cm.is_zero() ) { - vector delta_s = multivar_diophant(anew, x, cm, Inew, d, p, k); - ex buf = e; - for ( size_t j=0; j(xnu), m).subs(xnu==alphanu) / factorial(m); + cm = make_modular(cm, R); + if ( !cm.is_zero() ) { + vector delta_s = multivar_diophant(anew, x, cm, Inew, d, p, k); + ex buf = e; + for ( size_t j=0; j& v) } #endif // def DEBUGFACTOR -ex hensel_multivar(const ex& a, const ex& x, const vector& I, unsigned int p, const cl_I& l, const upvec& u, const vector& lcU) +static ex hensel_multivar(const ex& a, const ex& x, const vector& I, unsigned int p, const cl_I& l, const upvec& u, const vector& lcU) { const size_t nu = I.size() + 1; const cl_modint_ring R = find_modint_ring(expt_pos(cl_I(p),l)); @@ -1788,13 +1844,11 @@ static ex put_factors_into_lst(const ex& e) if ( is_a(e) ) { result.append(1); result.append(e.op(0)); - result.append(e.op(1)); return result; } if ( is_a(e) || is_a(e) ) { result.append(1); result.append(e); - result.append(1); return result; } if ( is_a(e) ) { @@ -1806,11 +1860,9 @@ static ex put_factors_into_lst(const ex& e) } if ( is_a(op) ) { result.append(op.op(0)); - result.append(op.op(1)); } if ( is_a(op) || is_a(op) ) { result.append(op); - result.append(1); } } result.prepend(nfac); @@ -1829,15 +1881,18 @@ ostream& operator<<(ostream& o, const vector& v) } #endif // def DEBUGFACTOR -static bool checkdivisors(const lst& f, vector& d) +/** Checks whether in a set of numbers each has a unique prime factor. + * + * @param[in] f list of numbers to check + * @return true: if number set is bad, false: otherwise + */ +static bool checkdivisors(const lst& f) { - const int k = f.nops()-2; + const int k = f.nops(); numeric q, r; - d[0] = ex_to(f.op(0) * f.op(f.nops()-1)); - if ( d[0] == 1 && k == 1 && abs(f.op(1)) != 1 ) { - return false; - } - for ( int i=1; i<=k; ++i ) { + vector d(k); + d[0] = ex_to(abs(f.op(0))); + for ( int i=1; i(abs(f.op(i))); for ( int j=i-1; j>=0; --j ) { r = d[j]; @@ -1854,13 +1909,30 @@ static bool checkdivisors(const lst& f, vector& d) return false; } -static bool generate_set(const ex& u, const ex& vn, const exset& syms, const ex& f, const numeric& modulus, vector& a, vector& d) +/** Generates a set of evaluation points for a multivariate polynomial. + * The set fulfills the following conditions: + * 1. lcoeff(evaluated_polynomial) does not vanish + * 2. factors of lcoeff(evaluated_polynomial) have each a unique prime factor + * 3. evaluated_polynomial is square free + * See [W1] for more details. + * + * @param[in] u multivariate polynomial to be factored + * @param[in] vn leading coefficient of u in x (x==first symbol in syms) + * @param[in] syms set of symbols that appear in u + * @param[in] f lst containing the factors of the leading coefficient vn + * @param[in,out] modulus integer modulus for random number generation (i.e. |a_i| < modulus) + * @param[out] u0 returns the evaluated (univariate) polynomial + * @param[out] a returns the valid evaluation points. must have initial size equal + * number of symbols-1 before calling generate_set + */ +static void generate_set(const ex& u, const ex& vn, const exset& syms, const lst& f, + numeric& modulus, ex& u0, vector& a) { - // computation of d is actually not necessary const ex& x = *syms.begin(); - bool trying = true; - do { - ex u0 = u; + while ( true ) { + ++modulus; + /* generate a set of integers ... */ + u0 = u; ex vna = vn; ex vnatry; exset::const_iterator s = syms.begin(); @@ -1869,71 +1941,64 @@ static bool generate_set(const ex& u, const ex& vn, const exset& syms, const ex& do { a[i] = mod(numeric(rand()), 2*modulus) - modulus; vnatry = vna.subs(*s == a[i]); + /* ... for which the leading coefficient doesn't vanish ... */ } while ( vnatry == 0 ); vna = vnatry; u0 = u0.subs(*s == a[i]); ++s; } - if ( gcd(u0,u0.diff(ex_to(x))) != 1 ) { + /* ... for which u0 is square free ... */ + ex g = gcd(u0, u0.diff(ex_to(x))); + if ( !is_a(g) ) { continue; } - if ( is_a(vn) ) { - trying = false; - } - else { - lst fnum; - lst::const_iterator i = ex_to(f).begin(); - fnum.append(*i++); - bool problem = false; - while ( i!=ex_to(f).end() ) { - ex fs = *i; - if ( !is_a(fs) ) { + if ( !is_a(vn) ) { + /* ... and for which the evaluated factors have each an unique prime factor */ + lst fnum = f; + fnum.let_op(0) = fnum.op(0) * u0.content(x); + for ( size_t i=1; i(fnum.op(i)) ) { s = syms.begin(); ++s; - for ( size_t j=0; j=p.ldegree(x); --i ) { - cont = gcd(cont, p.coeff(x,ex_to(i).to_int())); + for ( int i=p.degree(x)-1; i>=p.ldegree(x); --i ) { + cont = gcd(cont, p.coeff(x,i)); if ( cont == 1 ) break; } ex pp = expand(normal(p / cont)); if ( !is_a(cont) ) { - return factor(cont) * factor(pp); + return factor_sqrfree(cont) * factor_sqrfree(pp); } /* factor leading coefficient */ - pp = pp.collect(x); - ex vn = pp.lcoeff(x); - pp = pp.expand(); + ex vn = pp.collect(x).lcoeff(x); ex vnlst; if ( is_a(vn) ) { vnlst = lst(vn); @@ -1943,200 +2008,131 @@ static ex factor_multivariate(const ex& poly, const exset& syms) vnlst = put_factors_into_lst(vnfactors); } - const numeric maxtrials = 3; - numeric modulus = (vnlst.nops()-1 > 3) ? vnlst.nops()-1 : 3; - numeric minimalr = -1; + const unsigned int maxtrials = 3; + numeric modulus = (vnlst.nops() > 3) ? vnlst.nops() : 3; vector a(syms.size()-1, 0); - vector d((vnlst.nops()-1)/2+1, 0); + /* try now to factorize until we are successful */ while ( true ) { - numeric trialcount = 0; + + unsigned int trialcount = 0; + unsigned int prime; + int factor_count = 0; + int min_factor_count = -1; ex u, delta; - unsigned int prime = 3; - size_t factor_count = 0; - ex ufac; - ex ufaclst; + ex ufac, ufaclst; + + /* try several evaluation points to reduce the number of modular factors */ while ( trialcount < maxtrials ) { - bool problem = generate_set(pp, vn, syms, vnlst, modulus, a, d); - if ( problem ) { - ++modulus; - continue; - } - u = pp; - s = syms.begin(); - ++s; - for ( size_t i=0; i(u.lcoeff(x)), prime) != 0 ) { - umodpoly modpoly; - umodpoly_from_ex(modpoly, u, x, R); - if ( squarefree(modpoly) ) break; - } - prime = next_prime(prime); - R = find_modint_ring(prime); - } - ufac = factor(u); + /* generate a set of valid evaluation points */ + generate_set(pp, vn, syms, ex_to(vnlst), modulus, u, a); + + ufac = factor_univariate(u, x, prime); ufaclst = put_factors_into_lst(ufac); - factor_count = (ufaclst.nops()-1)/2; - - // veto factorization for which gcd(u_i, u_j) != 1 for all i,j - upvec tryu; - for ( size_t i=0; i<(ufaclst.nops()-1)/2; ++i ) { - umodpoly newu; - umodpoly_from_ex(newu, ufaclst.op(i*2+1), x, R); - tryu.push_back(newu); - } - bool veto = false; - for ( size_t i=0; i factor_count ) { - minimalr = factor_count; + else if ( min_factor_count > factor_count ) { + /* new minimum, reset trial counter */ + min_factor_count = factor_count; trialcount = 0; } - if ( minimalr <= 1 ) { - return poly; - } } - vector ftilde((vnlst.nops()-1)/2+1); - ftilde[0] = ex_to(vnlst.op(0)); - for ( size_t i=1; i C(factor_count); + if ( is_a(vn) ) { + for ( size_t i=1; i(ft); } + else { + vector ftilde(vnlst.nops()-1); + for ( size_t i=0; i(ft); + } - vector used_flag((vnlst.nops()-1)/2+1, false); - vector D(factor_count, 1); - for ( size_t i=0; i<=factor_count; ++i ) { - numeric prefac; - if ( i == 0 ) { - prefac = ex_to(ufaclst.op(0)); - ftilde[0] = ftilde[0] / prefac; - vnlst.let_op(0) = vnlst.op(0) / prefac; - continue; + vector used_flag(ftilde.size(), false); + vector D(factor_count, 1); + if ( delta == 1 ) { + for ( int i=0; i(ufaclst.op(i+1).lcoeff(x)); + for ( int j=ftilde.size()-1; j>=0; --j ) { + int count = 0; + while ( irem(prefac, ftilde[j]) == 0 ) { + prefac = iquo(prefac, ftilde[j]); + ++count; + } + if ( count ) { + used_flag[j] = true; + D[i] = D[i] * pow(vnlst.op(j+1), count); + } + } + C[i] = D[i] * prefac; + } } else { - prefac = ex_to(ufaclst.op(2*(i-1)+1).lcoeff(x)); - } - for ( size_t j=(vnlst.nops()-1)/2+1; j>0; --j ) { - if ( abs(ftilde[j-1]) == 1 ) { - used_flag[j-1] = true; - continue; - } - numeric g = gcd(prefac, ftilde[j-1]); - if ( g != 1 ) { - prefac = prefac / g; - numeric count = abs(iquo(g, ftilde[j-1])); - used_flag[j-1] = true; - if ( i > 0 ) { - if ( j == 1 ) { - D[i-1] = D[i-1] * pow(vnlst.op(0), count); + for ( int i=0; i(ufaclst.op(i+1).lcoeff(x)); + for ( int j=ftilde.size()-1; j>=0; --j ) { + int count = 0; + while ( irem(prefac, ftilde[j]) == 0 ) { + prefac = iquo(prefac, ftilde[j]); + ++count; } - else { - D[i-1] = D[i-1] * pow(vnlst.op(2*(j-2)+1), count); + while ( irem(ex_to(delta)*prefac, ftilde[j]) == 0 ) { + numeric g = gcd(prefac, ex_to(ftilde[j])); + prefac = iquo(prefac, g); + delta = delta / (ftilde[j]/g); + ufaclst.let_op(i+1) = ufaclst.op(i+1) * (ftilde[j]/g); + ++count; + } + if ( count ) { + used_flag[j] = true; + D[i] = D[i] * pow(vnlst.op(j+1), count); } } - else { - ftilde[j-1] = ftilde[j-1] / prefac; - break; - } - ++j; + C[i] = D[i] * prefac; } } - } - - bool some_factor_unused = false; - for ( size_t i=0; i C(factor_count); - if ( delta == 1 ) { - for ( size_t i=0; i epv; s = syms.begin(); @@ -2147,37 +2143,29 @@ static ex factor_multivariate(const ex& poly, const exset& syms) epv.push_back(ep); } - // calc bound B - ex maxcoeff; - for ( int i=u.degree(x); i>=u.ldegree(x); --i ) { - maxcoeff += pow(abs(u.coeff(x, i)),2); - } - cl_I normmc = ceiling1(the(cln::sqrt(ex_to(maxcoeff).to_cl_N()))); - unsigned int maxdegree = 0; - for ( size_t i=0; i (int)maxdegree ) { - maxdegree = ufaclst[2*i+1].degree(x); + // calc bound p^l + int maxdeg = 0; + for ( int i=1; i<=factor_count; ++i ) { + if ( ufaclst.op(i).degree(x) > maxdeg ) { + maxdeg = ufaclst[i].degree(x); } } - cl_I B = normmc * expt_pos(cl_I(2), maxdegree); + cl_I B = 2*calc_bound(u, x, maxdeg); cl_I l = 1; cl_I pl = prime; while ( pl < B ) { l = l + 1; pl = pl * prime; } - - upvec 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 ) { - umodpoly newu; - umodpoly_from_ex(newu, ufaclst.op(i*2+1), x, R); - uvec.push_back(newu); + upvec modfactors(ufaclst.nops()-1); + for ( size_t i=1; i(sfpoly) ) {