3 * Polynomial factorization (implementation).
5 * The interface function factor() at the end of this file is defined in the
6 * GiNaC namespace. All other utility functions and classes are defined in an
7 * additional anonymous namespace.
9 * Factorization starts by doing a square free factorization and making the
10 * coefficients integer. Then, depending on the number of free variables it
11 * proceeds either in dedicated univariate or multivariate factorization code.
13 * Univariate factorization does a modular factorization via Berlekamp's
14 * algorithm and distinct degree factorization. Hensel lifting is used at the
17 * Multivariate factorization uses the univariate factorization (applying a
18 * evaluation homomorphism first) and Hensel lifting raises the answer to the
19 * multivariate domain. The Hensel lifting code is completely distinct from the
20 * code used by the univariate factorization.
22 * Algorithms used can be found in
23 * [Wan] An Improved Multivariate Polynomial Factoring Algorithm,
25 * Mathematics of Computation, Vol. 32, No. 144 (1978) 1215--1231.
26 * [GCL] Algorithms for Computer Algebra,
27 * K.O.Geddes, S.R.Czapor, G.Labahn,
28 * Springer Verlag, 1992.
29 * [Mig] Some Useful Bounds,
31 * In "Computer Algebra, Symbolic and Algebraic Computation" (B.Buchberger et al., eds.),
32 * pp. 259-263, Springer-Verlag, New York, 1982.
36 * GiNaC Copyright (C) 1999-2023 Johannes Gutenberg University Mainz, Germany
38 * This program is free software; you can redistribute it and/or modify
39 * it under the terms of the GNU General Public License as published by
40 * the Free Software Foundation; either version 2 of the License, or
41 * (at your option) any later version.
43 * This program is distributed in the hope that it will be useful,
44 * but WITHOUT ANY WARRANTY; without even the implied warranty of
45 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
46 * GNU General Public License for more details.
48 * You should have received a copy of the GNU General Public License
49 * along with this program; if not, write to the Free Software
50 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
59 #include "operators.h"
62 #include "relational.h"
68 #include <type_traits>
84 // anonymous namespace to hide all utility functions
88 #define DCOUT(str) cout << #str << endl
89 #define DCOUTVAR(var) cout << #var << ": " << var << endl
90 #define DCOUT2(str,var) cout << #str << ": " << var << endl
91 ostream& operator<<(ostream& o, const vector<int>& v)
93 auto i = v.begin(), end = v.end();
100 static ostream& operator<<(ostream& o, const vector<cl_I>& v)
102 auto i = v.begin(), end = v.end();
104 o << *i << "[" << i-v.begin() << "]" << " ";
109 static ostream& operator<<(ostream& o, const vector<cl_MI>& v)
111 auto i = v.begin(), end = v.end();
113 o << *i << "[" << i-v.begin() << "]" << " ";
118 ostream& operator<<(ostream& o, const vector<numeric>& v)
120 for ( size_t i=0; i<v.size(); ++i ) {
125 ostream& operator<<(ostream& o, const vector<vector<cl_MI>>& v)
127 auto i = v.begin(), end = v.end();
129 o << i-v.begin() << ": " << *i << endl;
136 #define DCOUTVAR(var)
137 #define DCOUT2(str,var)
138 #endif // def DEBUGFACTOR
140 ////////////////////////////////////////////////////////////////////////////////
141 // modular univariate polynomial code
143 typedef std::vector<cln::cl_MI> umodpoly;
144 typedef std::vector<cln::cl_I> upoly;
145 typedef vector<umodpoly> upvec;
150 // CHANGED size_t -> int !!!
151 template<typename T> static int degree(const T& p)
156 template<typename T> static typename T::value_type lcoeff(const T& p)
158 return p[p.size() - 1];
161 /** Make the polynomial unit normal (having unit normal leading coefficient).
163 * @param[in, out] a polynomial to make unit normal
164 * @return true if polynomial a was already unit normal, false otherwise
166 static bool normalize_in_field(umodpoly& a)
170 if ( lcoeff(a) == a[0].ring()->one() ) {
174 const cln::cl_MI lc_1 = recip(lcoeff(a));
175 for (std::size_t k = a.size(); k-- != 0; )
180 /** Remove leading zero coefficients from polynomial.
182 * @param[in, out] p polynomial from which the zero leading coefficients will be removed
183 * @param[in] hint assume all coefficients of order ≥ hint are zero
185 template<typename T> static void
186 canonicalize(T& p, const typename T::size_type hint = std::numeric_limits<typename T::size_type>::max())
188 std::size_t i = min(p.size(), hint);
190 while ( i-- && zerop(p[i]) ) { }
192 p.erase(p.begin() + i + 1, p.end());
195 // END COPY FROM UPOLY.H
197 template<typename T> struct uvar_poly_p
199 static const bool value = false;
202 template<> struct uvar_poly_p<upoly>
204 static const bool value = true;
207 template<> struct uvar_poly_p<umodpoly>
209 static const bool value = true;
213 // Don't define this for anything but univariate polynomials.
214 static typename enable_if<uvar_poly_p<T>::value, T>::type
215 operator+(const T& a, const T& b)
222 for ( ; i<sb; ++i ) {
225 for ( ; i<sa; ++i ) {
234 for ( ; i<sa; ++i ) {
237 for ( ; i<sb; ++i ) {
246 // Don't define this for anything but univariate polynomials. Otherwise
247 // overload resolution might fail (this actually happens when compiling
248 // GiNaC with g++ 3.4).
249 static typename enable_if<uvar_poly_p<T>::value, T>::type
250 operator-(const T& a, const T& b)
257 for ( ; i<sb; ++i ) {
260 for ( ; i<sa; ++i ) {
269 for ( ; i<sa; ++i ) {
272 for ( ; i<sb; ++i ) {
280 static upoly operator*(const upoly& a, const upoly& b)
283 if ( a.empty() || b.empty() ) return c;
285 int n = degree(a) + degree(b);
287 for ( int i=0 ; i<=n; ++i ) {
288 for ( int j=0 ; j<=i; ++j ) {
289 if ( j > degree(a) || (i-j) > degree(b) ) continue;
290 c[i] = c[i] + a[j] * b[i-j];
297 static umodpoly operator*(const umodpoly& a, const umodpoly& b)
300 if ( a.empty() || b.empty() ) return c;
302 int n = degree(a) + degree(b);
303 c.resize(n+1, a[0].ring()->zero());
304 for ( int i=0 ; i<=n; ++i ) {
305 for ( int j=0 ; j<=i; ++j ) {
306 if ( j > degree(a) || (i-j) > degree(b) ) continue;
307 c[i] = c[i] + a[j] * b[i-j];
314 static upoly operator*(const upoly& a, const cl_I& x)
321 for ( size_t i=0; i<a.size(); ++i ) {
327 static upoly operator/(const upoly& a, const cl_I& x)
334 for ( size_t i=0; i<a.size(); ++i ) {
335 r[i] = exquo(a[i],x);
340 static umodpoly operator*(const umodpoly& a, const cl_MI& x)
342 umodpoly r(a.size());
343 for ( size_t i=0; i<a.size(); ++i ) {
350 static void upoly_from_ex(upoly& up, const ex& e, const ex& x)
352 // assert: e is in Z[x]
353 int deg = e.degree(x);
355 int ldeg = e.ldegree(x);
356 for ( ; deg>=ldeg; --deg ) {
357 up[deg] = the<cl_I>(ex_to<numeric>(e.coeff(x, deg)).to_cl_N());
359 for ( ; deg>=0; --deg ) {
365 static void umodpoly_from_upoly(umodpoly& ump, const upoly& e, const cl_modint_ring& R)
369 for ( ; deg>=0; --deg ) {
370 ump[deg] = R->canonhom(e[deg]);
375 static void umodpoly_from_ex(umodpoly& ump, const ex& e, const ex& x, const cl_modint_ring& R)
377 // assert: e is in Z[x]
378 int deg = e.degree(x);
380 int ldeg = e.ldegree(x);
381 for ( ; deg>=ldeg; --deg ) {
382 cl_I coeff = the<cl_I>(ex_to<numeric>(e.coeff(x, deg)).to_cl_N());
383 ump[deg] = R->canonhom(coeff);
385 for ( ; deg>=0; --deg ) {
386 ump[deg] = R->zero();
392 static void umodpoly_from_ex(umodpoly& ump, const ex& e, const ex& x, const cl_I& modulus)
394 umodpoly_from_ex(ump, e, x, find_modint_ring(modulus));
398 static ex upoly_to_ex(const upoly& a, const ex& x)
400 if ( a.empty() ) return 0;
402 for ( int i=degree(a); i>=0; --i ) {
403 e += numeric(a[i]) * pow(x, i);
408 static ex umodpoly_to_ex(const umodpoly& a, const ex& x)
410 if ( a.empty() ) return 0;
411 cl_modint_ring R = a[0].ring();
412 cl_I mod = R->modulus;
413 cl_I halfmod = (mod-1) >> 1;
415 for ( int i=degree(a); i>=0; --i ) {
416 cl_I n = R->retract(a[i]);
418 e += numeric(n-mod) * pow(x, i);
420 e += numeric(n) * pow(x, i);
426 static upoly umodpoly_to_upoly(const umodpoly& a)
429 if ( a.empty() ) return e;
430 cl_modint_ring R = a[0].ring();
431 cl_I mod = R->modulus;
432 cl_I halfmod = (mod-1) >> 1;
433 for ( int i=degree(a); i>=0; --i ) {
434 cl_I n = R->retract(a[i]);
444 static umodpoly umodpoly_to_umodpoly(const umodpoly& a, const cl_modint_ring& R, unsigned int m)
447 if ( a.empty() ) return e;
448 cl_modint_ring oldR = a[0].ring();
449 size_t sa = a.size();
450 e.resize(sa+m, R->zero());
451 for ( size_t i=0; i<sa; ++i ) {
452 e[i+m] = R->canonhom(oldR->retract(a[i]));
458 /** Divides all coefficients of the polynomial a by the positive integer x.
459 * All coefficients are supposed to be divisible by x. If they are not, the
460 * division will raise an exception.
462 * @param[in,out] a polynomial of which the coefficients will be reduced by x
463 * @param[in] x positive integer that divides the coefficients
465 static void reduce_coeff(umodpoly& a, const cl_I& x)
467 if ( a.empty() ) return;
469 cl_modint_ring R = a[0].ring();
471 // cln cannot perform this division in the modular field
472 cl_I c = R->retract(i);
473 i = cl_MI(R, exquopos(c, x));
477 /** Calculates remainder of a/b.
478 * Assertion: a and b not empty.
480 * @param[in] a polynomial dividend
481 * @param[in] b polynomial divisor
482 * @param[out] r polynomial remainder
484 static void rem(const umodpoly& a, const umodpoly& b, umodpoly& r)
493 cl_MI qk = div(r[n+k], b[n]);
495 for ( int i=0; i<n; ++i ) {
496 unsigned int j = n + k - 1 - i;
497 r[j] = r[j] - qk * b[j-k];
502 fill(r.begin()+n, r.end(), a[0].ring()->zero());
506 /** Calculates quotient of a/b.
507 * Assertion: a and b not empty.
509 * @param[in] a polynomial dividend
510 * @param[in] b polynomial divisor
511 * @param[out] q polynomial quotient
513 static void div(const umodpoly& a, const umodpoly& b, umodpoly& q)
522 q.resize(k+1, a[0].ring()->zero());
524 cl_MI qk = div(r[n+k], b[n]);
527 for ( int i=0; i<n; ++i ) {
528 unsigned int j = n + k - 1 - i;
529 r[j] = r[j] - qk * b[j-k];
537 /** Calculates quotient and remainder of a/b.
538 * Assertion: a and b not empty.
540 * @param[in] a polynomial dividend
541 * @param[in] b polynomial divisor
542 * @param[out] r polynomial remainder
543 * @param[out] q polynomial quotient
545 static void remdiv(const umodpoly& a, const umodpoly& b, umodpoly& r, umodpoly& q)
554 q.resize(k+1, a[0].ring()->zero());
556 cl_MI qk = div(r[n+k], b[n]);
559 for ( int i=0; i<n; ++i ) {
560 unsigned int j = n + k - 1 - i;
561 r[j] = r[j] - qk * b[j-k];
566 fill(r.begin()+n, r.end(), a[0].ring()->zero());
571 /** Calculates the GCD of polynomial a and b.
573 * @param[in] a polynomial
574 * @param[in] b polynomial
577 static void gcd(const umodpoly& a, const umodpoly& b, umodpoly& c)
579 if ( degree(a) < degree(b) ) return gcd(b, a, c);
582 normalize_in_field(c);
584 normalize_in_field(d);
586 while ( !d.empty() ) {
591 normalize_in_field(c);
594 /** Calculates the derivative of the polynomial a.
596 * @param[in] a polynomial of which to take the derivative
597 * @param[out] d result/derivative
599 static void deriv(const umodpoly& a, umodpoly& d)
602 if ( a.size() <= 1 ) return;
604 d.insert(d.begin(), a.begin()+1, a.end());
606 for ( int i=1; i<max; ++i ) {
612 static bool unequal_one(const umodpoly& a)
614 return ( a.size() != 1 || a[0] != a[0].ring()->one() );
617 static bool equal_one(const umodpoly& a)
619 return ( a.size() == 1 && a[0] == a[0].ring()->one() );
622 /** Returns true if polynomial a is square free.
624 * @param[in] a polynomial to check
625 * @return true if polynomial is square free, false otherwise
627 static bool squarefree(const umodpoly& a)
639 /** Computes w^q mod a.
640 * Uses theorem 2.1 from A.K.Lenstra's PhD thesis; see exercise 8.13 in [GCL].
642 * @param[in] w polynomial
643 * @param[in] a modulus polynomial
644 * @param[in] q common modulus of w and a
645 * @param[out] r result
647 static void expt_pos_Q(const umodpoly& w, const umodpoly& a, unsigned int q, umodpoly& r)
649 if ( w.empty() ) return;
650 cl_MI zero = w[0].ring()->zero();
652 umodpoly buf(deg*q+1, zero);
653 for ( size_t i=0; i<=deg; ++i ) {
659 // END modular univariate polynomial code
660 ////////////////////////////////////////////////////////////////////////////////
662 ////////////////////////////////////////////////////////////////////////////////
665 typedef vector<cl_MI> mvec;
670 friend ostream& operator<<(ostream& o, const modular_matrix& m);
673 modular_matrix(size_t r_, size_t c_, const cl_MI& init) : r(r_), c(c_)
677 size_t rowsize() const { return r; }
678 size_t colsize() const { return c; }
679 cl_MI& operator()(size_t row, size_t col) { return m[row*c + col]; }
680 cl_MI operator()(size_t row, size_t col) const { return m[row*c + col]; }
681 void mul_col(size_t col, const cl_MI x)
683 for ( size_t rc=0; rc<r; ++rc ) {
684 std::size_t i = c*rc + col;
688 void sub_col(size_t col1, size_t col2, const cl_MI fac)
690 for ( size_t rc=0; rc<r; ++rc ) {
691 std::size_t i1 = col1 + c*rc;
692 std::size_t i2 = col2 + c*rc;
693 m[i1] = m[i1] - m[i2]*fac;
696 void switch_col(size_t col1, size_t col2)
698 for ( size_t rc=0; rc<r; ++rc ) {
699 std::size_t i1 = col1 + rc*c;
700 std::size_t i2 = col2 + rc*c;
701 std::swap(m[i1], m[i2]);
704 void mul_row(size_t row, const cl_MI x)
706 for ( size_t cc=0; cc<c; ++cc ) {
707 std::size_t i = row*c + cc;
711 void sub_row(size_t row1, size_t row2, const cl_MI fac)
713 for ( size_t cc=0; cc<c; ++cc ) {
714 std::size_t i1 = row1*c + cc;
715 std::size_t i2 = row2*c + cc;
716 m[i1] = m[i1] - m[i2]*fac;
719 void switch_row(size_t row1, size_t row2)
721 for ( size_t cc=0; cc<c; ++cc ) {
722 std::size_t i1 = row1*c + cc;
723 std::size_t i2 = row2*c + cc;
724 std::swap(m[i1], m[i2]);
727 bool is_col_zero(size_t col) const
729 for ( size_t rr=0; rr<r; ++rr ) {
730 std::size_t i = col + rr*c;
731 if ( !zerop(m[i]) ) {
737 bool is_row_zero(size_t row) const
739 for ( size_t cc=0; cc<c; ++cc ) {
740 std::size_t i = row*c + cc;
741 if ( !zerop(m[i]) ) {
747 void set_row(size_t row, const vector<cl_MI>& newrow)
749 for (std::size_t i2 = 0; i2 < newrow.size(); ++i2) {
750 std::size_t i1 = row*c + i2;
754 mvec::const_iterator row_begin(size_t row) const { return m.begin()+row*c; }
755 mvec::const_iterator row_end(size_t row) const { return m.begin()+row*c+r; }
762 modular_matrix operator*(const modular_matrix& m1, const modular_matrix& m2)
764 const unsigned int r = m1.rowsize();
765 const unsigned int c = m2.colsize();
766 modular_matrix o(r,c,m1(0,0));
768 for ( size_t i=0; i<r; ++i ) {
769 for ( size_t j=0; j<c; ++j ) {
771 buf = m1(i,0) * m2(0,j);
772 for ( size_t k=1; k<c; ++k ) {
773 buf = buf + m1(i,k)*m2(k,j);
781 ostream& operator<<(ostream& o, const modular_matrix& m)
783 cl_modint_ring R = m(0,0).ring();
785 for ( size_t i=0; i<m.rowsize(); ++i ) {
787 for ( size_t j=0; j<m.colsize()-1; ++j ) {
788 o << R->retract(m(i,j)) << ",";
790 o << R->retract(m(i,m.colsize()-1)) << "}";
791 if ( i != m.rowsize()-1 ) {
798 #endif // def DEBUGFACTOR
800 // END modular matrix
801 ////////////////////////////////////////////////////////////////////////////////
803 /** Calculates the Q matrix for a polynomial. Used by Berlekamp's algorithm.
805 * The implementation follows algorithm 8.5 of [GCL].
807 * @param[in] a_ modular polynomial
808 * @param[out] Q Q matrix
810 static void q_matrix(const umodpoly& a_, modular_matrix& Q)
813 normalize_in_field(a);
816 unsigned int q = cl_I_to_uint(a[0].ring()->modulus);
817 umodpoly r(n, a[0].ring()->zero());
818 r[0] = a[0].ring()->one();
820 unsigned int max = (n-1) * q;
821 for ( size_t m=1; m<=max; ++m ) {
822 cl_MI rn_1 = r.back();
823 for ( size_t i=n-1; i>0; --i ) {
824 r[i] = r[i-1] - (rn_1 * a[i]);
827 if ( (m % q) == 0 ) {
833 /** Determine the nullspace of a matrix M-1.
835 * @param[in,out] M matrix, will be modified
836 * @param[out] basis calculated nullspace of M-1
838 static void nullspace(modular_matrix& M, vector<mvec>& basis)
840 const size_t n = M.rowsize();
841 const cl_MI one = M(0,0).ring()->one();
842 for ( size_t i=0; i<n; ++i ) {
843 M(i,i) = M(i,i) - one;
845 for ( size_t r=0; r<n; ++r ) {
847 for ( ; cc<n; ++cc ) {
848 if ( !zerop(M(r,cc)) ) {
850 if ( !zerop(M(cc,cc)) ) {
862 M.mul_col(r, recip(M(r,r)));
863 for ( cc=0; cc<n; ++cc ) {
865 M.sub_col(cc, r, M(r,cc));
871 for ( size_t i=0; i<n; ++i ) {
872 M(i,i) = M(i,i) - one;
874 for ( size_t i=0; i<n; ++i ) {
875 if ( !M.is_row_zero(i) ) {
876 mvec nu(M.row_begin(i), M.row_end(i));
882 /** Berlekamp's modular factorization.
884 * The implementation follows algorithm 8.4 of [GCL].
886 * @param[in] a modular polynomial
887 * @param[out] upv vector containing modular factors. if upv was not empty the
888 * new elements are added at the end
890 static void berlekamp(const umodpoly& a, upvec& upv)
892 cl_modint_ring R = a[0].ring();
893 umodpoly one(1, R->one());
895 // find nullspace of Q matrix
896 modular_matrix Q(degree(a), degree(a), R->zero());
901 const unsigned int k = nu.size();
907 list<umodpoly> factors = {a};
908 unsigned int size = 1;
910 unsigned int q = cl_I_to_uint(R->modulus);
912 list<umodpoly>::iterator u = factors.begin();
914 // calculate all gcd's
916 for ( unsigned int s=0; s<q; ++s ) {
917 umodpoly nur = nu[r];
918 nur[0] = nur[0] - cl_MI(R, s);
922 if ( unequal_one(g) && g != *u ) {
925 if ( equal_one(uo) ) {
926 throw logic_error("berlekamp: unexpected divisor.");
930 factors.push_back(g);
932 for (auto & i : factors) {
937 for (auto & i : factors) {
951 // modular square free factorization is not used at the moment so we deactivate
955 /** Calculates a^(1/prime).
957 * @param[in] a polynomial
958 * @param[in] prime prime number -> exponent 1/prime
959 * @param[out] ap resulting polynomial
961 static void expt_1_over_p(const umodpoly& a, unsigned int prime, umodpoly& ap)
963 size_t newdeg = degree(a)/prime;
966 for ( size_t i=1; i<=newdeg; ++i ) {
971 /** Modular square free factorization.
973 * @param[in] a polynomial
974 * @param[out] factors modular factors
975 * @param[out] mult corresponding multiplicities (exponents)
977 static void modsqrfree(const umodpoly& a, upvec& factors, vector<int>& mult)
979 const unsigned int prime = cl_I_to_uint(a[0].ring()->modulus);
988 while ( unequal_one(w) ) {
993 factors.push_back(z);
1001 if ( unequal_one(c) ) {
1003 expt_1_over_p(c, prime, cp);
1004 size_t previ = mult.size();
1005 modsqrfree(cp, factors, mult);
1006 for ( size_t i=previ; i<mult.size(); ++i ) {
1012 expt_1_over_p(a, prime, ap);
1013 size_t previ = mult.size();
1014 modsqrfree(ap, factors, mult);
1015 for ( size_t i=previ; i<mult.size(); ++i ) {
1021 #endif // deactivation of square free factorization
1023 /** Distinct degree factorization (DDF).
1025 * The implementation follows algorithm 8.8 of [GCL].
1027 * @param[in] a_ modular polynomial
1028 * @param[out] degrees vector containing the degrees of the factors of the
1029 * corresponding polynomials in ddfactors.
1030 * @param[out] ddfactors vector containing polynomials which factors have the
1031 * degree given in degrees.
1033 static void distinct_degree_factor(const umodpoly& a_, vector<int>& degrees, upvec& ddfactors)
1037 cl_modint_ring R = a[0].ring();
1038 int q = cl_I_to_int(R->modulus);
1039 int nhalf = degree(a)/2;
1042 umodpoly w = {R->zero(), R->one()};
1045 while ( i <= nhalf ) {
1047 expt_pos_Q(w, a, q, buf);
1050 if ( unequal_one(buf) ) {
1051 degrees.push_back(i);
1052 ddfactors.push_back(buf);
1056 nhalf = degree(a)/2;
1062 if ( unequal_one(a) ) {
1063 degrees.push_back(degree(a));
1064 ddfactors.push_back(a);
1068 /** Modular same degree factorization.
1069 * Same degree factorization is a kind of misnomer. It performs distinct degree
1070 * factorization, but instead of using the Cantor-Zassenhaus algorithm it
1071 * (sub-optimally) uses Berlekamp's algorithm for the factors of the same
1074 * @param[in] a modular polynomial
1075 * @param[out] upv vector containing modular factors. if upv was not empty the
1076 * new elements are added at the end
1078 static void same_degree_factor(const umodpoly& a, upvec& upv)
1080 cl_modint_ring R = a[0].ring();
1082 vector<int> degrees;
1084 distinct_degree_factor(a, degrees, ddfactors);
1086 for ( size_t i=0; i<degrees.size(); ++i ) {
1087 if ( degrees[i] == degree(ddfactors[i]) ) {
1088 upv.push_back(ddfactors[i]);
1090 berlekamp(ddfactors[i], upv);
1095 // Yes, we can (choose).
1096 #define USE_SAME_DEGREE_FACTOR
1098 /** Modular univariate factorization.
1100 * In principle, we have two algorithms at our disposal: Berlekamp's algorithm
1101 * and same degree factorization (SDF). SDF seems to be slightly faster in
1102 * almost all cases so it is activated as default.
1104 * @param[in] p modular polynomial
1105 * @param[out] upv vector containing modular factors. if upv was not empty the
1106 * new elements are added at the end
1108 static void factor_modular(const umodpoly& p, upvec& upv)
1110 #ifdef USE_SAME_DEGREE_FACTOR
1111 same_degree_factor(p, upv);
1117 /** Calculates modular polynomials s and t such that a*s+b*t==1.
1118 * Assertion: a and b are relatively prime and not zero.
1120 * @param[in] a polynomial
1121 * @param[in] b polynomial
1122 * @param[out] s polynomial
1123 * @param[out] t polynomial
1125 static void exteuclid(const umodpoly& a, const umodpoly& b, umodpoly& s, umodpoly& t)
1127 if ( degree(a) < degree(b) ) {
1128 exteuclid(b, a, t, s);
1132 umodpoly one(1, a[0].ring()->one());
1133 umodpoly c = a; normalize_in_field(c);
1134 umodpoly d = b; normalize_in_field(d);
1142 umodpoly r = c - q * d;
1143 umodpoly r1 = s - q * d1;
1144 umodpoly r2 = t - q * d2;
1148 if ( r.empty() ) break;
1153 cl_MI fac = recip(lcoeff(a) * lcoeff(c));
1154 for (auto & i : s) {
1158 fac = recip(lcoeff(b) * lcoeff(c));
1159 for (auto & i : t) {
1165 /** Replaces the leading coefficient in a polynomial by a given number.
1167 * @param[in] poly polynomial to change
1168 * @param[in] lc new leading coefficient
1169 * @return changed polynomial
1171 static upoly replace_lc(const upoly& poly, const cl_I& lc)
1173 if ( poly.empty() ) return poly;
1179 /** Calculates bound for the product of absolute values (modulus) of the roots.
1180 * Uses Landau's inequality, see [Mig].
1182 static inline cl_I calc_bound(const ex& a, const ex& x)
1185 for ( int i=a.degree(x); i>=a.ldegree(x); --i ) {
1186 cl_I aa = abs(the<cl_I>(ex_to<numeric>(a.coeff(x, i)).to_cl_N()));
1187 radicand = radicand + square(aa);
1189 return ceiling1(the<cl_R>(cln::sqrt(radicand)));
1192 /** Calculates bound for the product of absolute values (modulus) of the roots.
1193 * Uses Landau's inequality, see [Mig].
1195 static inline cl_I calc_bound(const upoly& a)
1198 for ( int i=degree(a); i>=0; --i ) {
1199 cl_I aa = abs(a[i]);
1200 radicand = radicand + square(aa);
1202 return ceiling1(the<cl_R>(cln::sqrt(radicand)));
1205 /** Hensel lifting as used by factor_univariate().
1207 * The implementation follows algorithm 6.1 of [GCL].
1209 * @param[in] a_ primitive univariate polynomials
1210 * @param[in] p prime number that does not divide lcoeff(a)
1211 * @param[in] u1_ modular factor of a (mod p)
1212 * @param[in] w1_ modular factor of a (mod p), relatively prime to u1_,
1213 * fulfilling u1_*w1_ == a mod p
1214 * @param[out] u lifted factor
1215 * @param[out] w lifted factor, u*w = a
1217 static void hensel_univar(const upoly& a_, unsigned int p, const umodpoly& u1_, const umodpoly& w1_, upoly& u, upoly& w)
1220 const cl_modint_ring& R = u1_[0].ring();
1223 int maxdeg = (degree(u1_) > degree(w1_)) ? degree(u1_) : degree(w1_);
1224 cl_I maxmodulus = ash(calc_bound(a), maxdeg+1); // = 2 * calc_bound(a) * 2^maxdeg
1227 cl_I alpha = lcoeff(a);
1230 normalize_in_field(nu1);
1232 normalize_in_field(nw1);
1234 phi = umodpoly_to_upoly(nu1) * alpha;
1236 umodpoly_from_upoly(u1, phi, R);
1237 phi = umodpoly_to_upoly(nw1) * alpha;
1239 umodpoly_from_upoly(w1, phi, R);
1244 exteuclid(u1, w1, s, t);
1247 u = replace_lc(umodpoly_to_upoly(u1), alpha);
1248 w = replace_lc(umodpoly_to_upoly(w1), alpha);
1249 upoly e = a - u * w;
1253 while ( !e.empty() && modulus < maxmodulus ) {
1254 upoly c = e / modulus;
1255 phi = umodpoly_to_upoly(s) * c;
1256 umodpoly sigmatilde;
1257 umodpoly_from_upoly(sigmatilde, phi, R);
1258 phi = umodpoly_to_upoly(t) * c;
1260 umodpoly_from_upoly(tautilde, phi, R);
1262 remdiv(sigmatilde, w1, r, q);
1264 phi = umodpoly_to_upoly(tautilde) + umodpoly_to_upoly(q) * umodpoly_to_upoly(u1);
1266 umodpoly_from_upoly(tau, phi, R);
1267 u = u + umodpoly_to_upoly(tau) * modulus;
1268 w = w + umodpoly_to_upoly(sigma) * modulus;
1270 modulus = modulus * p;
1276 for ( size_t i=1; i<u.size(); ++i ) {
1278 if ( g == 1 ) break;
1292 /** Returns a new small prime number.
1294 * @param[in] n an integer
1295 * @return smallest prime greater than n
1297 static unsigned int next_prime(unsigned int n)
1299 static vector<unsigned int> primes = {2, 3, 5, 7};
1300 unsigned int candidate = primes.back();
1301 while (primes.back() <= n) {
1303 bool is_prime = true;
1304 for (size_t i=1; primes[i]*primes[i]<=candidate; ++i) {
1305 if (candidate % primes[i] == 0) {
1311 primes.push_back(candidate);
1313 for (auto & it : primes) {
1318 throw logic_error("next_prime: should not reach this point!");
1321 /** Manages the splitting of a vector of modular factors into two partitions.
1323 class factor_partition
1326 /** Takes the vector of modular factors and initializes the first partition */
1327 factor_partition(const upvec& factors_) : factors(factors_)
1333 one.resize(1, factors.front()[0].ring()->one());
1338 int operator[](size_t i) const { return k[i]; }
1339 size_t size() const { return n; }
1340 size_t size_left() const { return n-len; }
1341 size_t size_right() const { return len; }
1342 /** Initializes the next partition.
1343 Returns true, if there is one, false otherwise. */
1346 if ( last == n-1 ) {
1356 while ( k[last] == 0 ) { --last; }
1357 if ( last == 0 && n == 2*len ) return false;
1359 for ( size_t i=0; i<=len-rem; ++i ) {
1363 fill(k.begin()+last, k.end(), 0);
1370 if ( len > n/2 ) return false;
1371 fill(k.begin(), k.begin()+len, 1);
1372 fill(k.begin()+len+1, k.end(), 0);
1380 /** Get first partition */
1381 umodpoly& left() { return lr[0]; }
1382 /** Get second partition */
1383 umodpoly& right() { return lr[1]; }
1392 while ( i < n && k[i] == group ) { ++d; ++i; }
1394 if ( cache[pos].size() >= d ) {
1395 lr[group] = lr[group] * cache[pos][d-1];
1397 if ( cache[pos].size() == 0 ) {
1398 cache[pos].push_back(factors[pos] * factors[pos+1]);
1400 size_t j = pos + cache[pos].size() + 1;
1401 d -= cache[pos].size();
1403 umodpoly buf = cache[pos].back() * factors[j];
1404 cache[pos].push_back(buf);
1408 lr[group] = lr[group] * cache[pos].back();
1411 lr[group] = lr[group] * factors[pos];
1422 for ( size_t i=0; i<n; ++i ) {
1423 lr[k[i]] = lr[k[i]] * factors[i];
1429 vector<vector<umodpoly>> cache;
1438 /** Contains a pair of univariate polynomial and its modular factors.
1439 * Used by factor_univariate().
1447 /** Univariate polynomial factorization.
1449 * Modular factorization is tried for several primes to minimize the number of
1450 * modular factors. Then, Hensel lifting is performed.
1452 * @param[in] poly expanded square free univariate polynomial
1453 * @param[in] x symbol
1454 * @param[in,out] prime prime number to start trying modular factorization with,
1455 * output value is the prime number actually used
1457 static ex factor_univariate(const ex& poly, const ex& x, unsigned int& prime)
1459 ex unit, cont, prim_ex;
1460 poly.unitcontprim(x, unit, cont, prim_ex);
1462 upoly_from_ex(prim, prim_ex, x);
1463 if (prim_ex.is_equal(1)) {
1467 // determine proper prime and minimize number of modular factors
1469 unsigned int lastp = prime;
1471 unsigned int trials = 0;
1472 unsigned int minfactors = 0;
1474 const numeric& cont_n = ex_to<numeric>(cont);
1476 if (cont_n.is_integer()) {
1477 i_cont = the<cl_I>(cont_n.to_cl_N());
1479 // poly \in Q[x] => poly = q ipoly, ipoly \in Z[x], q \in Q
1480 // factor(poly) \equiv q factor(ipoly)
1483 cl_I lc = lcoeff(prim)*i_cont;
1485 while ( trials < 2 ) {
1488 prime = next_prime(prime);
1489 if ( !zerop(rem(lc, prime)) ) {
1490 R = find_modint_ring(prime);
1491 umodpoly_from_upoly(modpoly, prim, R);
1492 if ( squarefree(modpoly) ) break;
1496 // do modular factorization
1498 factor_modular(modpoly, trialfactors);
1499 if ( trialfactors.size() <= 1 ) {
1500 // irreducible for sure
1504 if ( minfactors == 0 || trialfactors.size() < minfactors ) {
1505 factors = trialfactors;
1506 minfactors = trialfactors.size();
1514 R = find_modint_ring(prime);
1516 // lift all factor combinations
1517 stack<ModFactors> tocheck;
1520 mf.factors = factors;
1524 while ( tocheck.size() ) {
1525 const size_t n = tocheck.top().factors.size();
1526 factor_partition part(tocheck.top().factors);
1528 // call Hensel lifting
1529 hensel_univar(tocheck.top().poly, prime, part.left(), part.right(), f1, f2);
1530 if ( !f1.empty() ) {
1531 // successful, update the stack and the result
1532 if ( part.size_left() == 1 ) {
1533 if ( part.size_right() == 1 ) {
1534 result *= upoly_to_ex(f1, x) * upoly_to_ex(f2, x);
1538 result *= upoly_to_ex(f1, x);
1539 tocheck.top().poly = f2;
1540 for ( size_t i=0; i<n; ++i ) {
1541 if ( part[i] == 0 ) {
1542 tocheck.top().factors.erase(tocheck.top().factors.begin()+i);
1548 else if ( part.size_right() == 1 ) {
1549 if ( part.size_left() == 1 ) {
1550 result *= upoly_to_ex(f1, x) * upoly_to_ex(f2, x);
1554 result *= upoly_to_ex(f2, x);
1555 tocheck.top().poly = f1;
1556 for ( size_t i=0; i<n; ++i ) {
1557 if ( part[i] == 1 ) {
1558 tocheck.top().factors.erase(tocheck.top().factors.begin()+i);
1564 upvec newfactors1(part.size_left()), newfactors2(part.size_right());
1565 auto i1 = newfactors1.begin(), i2 = newfactors2.begin();
1566 for ( size_t i=0; i<n; ++i ) {
1568 *i2++ = tocheck.top().factors[i];
1570 *i1++ = tocheck.top().factors[i];
1573 tocheck.top().factors = newfactors1;
1574 tocheck.top().poly = f1;
1576 mf.factors = newfactors2;
1583 if ( !part.next() ) {
1584 // if no more combinations left, return polynomial as
1586 result *= upoly_to_ex(tocheck.top().poly, x);
1594 return unit * cont * result;
1597 /** Second interface to factor_univariate() to be used if the information about
1598 * the prime is not needed.
1600 static inline ex factor_univariate(const ex& poly, const ex& x)
1603 return factor_univariate(poly, x, prime);
1606 /** Represents an evaluation point (<symbol>==<integer>).
1615 ostream& operator<<(ostream& o, const vector<EvalPoint>& v)
1617 for ( size_t i=0; i<v.size(); ++i ) {
1618 o << "(" << v[i].x << "==" << v[i].evalpoint << ") ";
1622 #endif // def DEBUGFACTOR
1624 // forward declaration
1625 static 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);
1627 /** Utility function for multivariate Hensel lifting.
1629 * Solves the equation
1630 * s_1*b_1 + ... + s_r*b_r == 1 mod p^k
1631 * with deg(s_i) < deg(a_i)
1632 * and with given b_1 = a_1 * ... * a_{i-1} * a_{i+1} * ... * a_r
1634 * The implementation follows algorithm 6.3 of [GCL].
1636 * @param[in] a vector of modular univariate polynomials
1637 * @param[in] x symbol
1638 * @param[in] p prime number
1639 * @param[in] k p^k is modulus
1640 * @return vector of polynomials (s_i)
1642 static upvec multiterm_eea_lift(const upvec& a, const ex& x, unsigned int p, unsigned int k)
1644 const size_t r = a.size();
1645 cl_modint_ring R = find_modint_ring(expt_pos(cl_I(p),k));
1648 for ( size_t j=r-2; j>=1; --j ) {
1649 q[j-1] = a[j] * q[j];
1651 umodpoly beta(1, R->one());
1653 for ( size_t j=1; j<r; ++j ) {
1654 vector<ex> mdarg(2);
1655 mdarg[0] = umodpoly_to_ex(q[j-1], x);
1656 mdarg[1] = umodpoly_to_ex(a[j-1], x);
1657 vector<EvalPoint> empty;
1658 vector<ex> exsigma = multivar_diophant(mdarg, x, umodpoly_to_ex(beta, x), empty, 0, p, k);
1660 umodpoly_from_ex(sigma1, exsigma[0], x, R);
1662 umodpoly_from_ex(sigma2, exsigma[1], x, R);
1664 s.push_back(sigma2);
1670 /** Changes the modulus of a modular polynomial. Used by eea_lift().
1672 * @param[in] R new modular ring
1673 * @param[in,out] a polynomial to change (in situ)
1675 static void change_modulus(const cl_modint_ring& R, umodpoly& a)
1677 if ( a.empty() ) return;
1678 cl_modint_ring oldR = a[0].ring();
1679 for (auto & i : a) {
1680 i = R->canonhom(oldR->retract(i));
1685 /** Utility function for multivariate Hensel lifting.
1687 * Solves s*a + t*b == 1 mod p^k given a,b.
1689 * The implementation follows algorithm 6.3 of [GCL].
1691 * @param[in] a polynomial
1692 * @param[in] b polynomial
1693 * @param[in] x symbol
1694 * @param[in] p prime number
1695 * @param[in] k p^k is modulus
1696 * @param[out] s_ output polynomial
1697 * @param[out] t_ output polynomial
1699 static void eea_lift(const umodpoly& a, const umodpoly& b, const ex& x, unsigned int p, unsigned int k, umodpoly& s_, umodpoly& t_)
1701 cl_modint_ring R = find_modint_ring(p);
1703 change_modulus(R, amod);
1705 change_modulus(R, bmod);
1709 exteuclid(amod, bmod, smod, tmod);
1711 cl_modint_ring Rpk = find_modint_ring(expt_pos(cl_I(p),k));
1713 change_modulus(Rpk, s);
1715 change_modulus(Rpk, t);
1718 umodpoly one(1, Rpk->one());
1719 for ( size_t j=1; j<k; ++j ) {
1720 umodpoly e = one - a * s - b * t;
1721 reduce_coeff(e, modulus);
1723 change_modulus(R, c);
1724 umodpoly sigmabar = smod * c;
1725 umodpoly taubar = tmod * c;
1727 remdiv(sigmabar, bmod, sigma, q);
1728 umodpoly tau = taubar + q * amod;
1729 umodpoly sadd = sigma;
1730 change_modulus(Rpk, sadd);
1731 cl_MI modmodulus(Rpk, modulus);
1732 s = s + sadd * modmodulus;
1733 umodpoly tadd = tau;
1734 change_modulus(Rpk, tadd);
1735 t = t + tadd * modmodulus;
1736 modulus = modulus * p;
1742 /** Utility function for multivariate Hensel lifting.
1744 * Solves the equation
1745 * s_1*b_1 + ... + s_r*b_r == x^m mod p^k
1746 * with given b_1 = a_1 * ... * a_{i-1} * a_{i+1} * ... * a_r
1748 * The implementation follows algorithm 6.3 of [GCL].
1750 * @param a vector with univariate polynomials mod p^k
1752 * @param m exponent of x^m in the equation to solve
1753 * @param p prime number
1754 * @param k p^k is modulus
1755 * @return vector of polynomials (s_i)
1757 static upvec univar_diophant(const upvec& a, const ex& x, unsigned int m, unsigned int p, unsigned int k)
1759 cl_modint_ring R = find_modint_ring(expt_pos(cl_I(p),k));
1761 const size_t r = a.size();
1764 upvec s = multiterm_eea_lift(a, x, p, k);
1765 for ( size_t j=0; j<r; ++j ) {
1766 umodpoly bmod = umodpoly_to_umodpoly(s[j], R, m);
1768 rem(bmod, a[j], buf);
1769 result.push_back(buf);
1773 eea_lift(a[1], a[0], x, p, k, s, t);
1774 umodpoly bmod = umodpoly_to_umodpoly(s, R, m);
1776 remdiv(bmod, a[0], buf, q);
1777 result.push_back(buf);
1778 umodpoly t1mod = umodpoly_to_umodpoly(t, R, m);
1779 buf = t1mod + q * a[1];
1780 result.push_back(buf);
1786 /** Map used by function make_modular().
1787 * Finds every coefficient in a polynomial and replaces it by is value in the
1788 * given modular ring R (symmetric representation).
1790 struct make_modular_map : public map_function {
1792 make_modular_map(const cl_modint_ring& R_) : R(R_) { }
1793 ex operator()(const ex& e) override
1795 if ( is_a<add>(e) || is_a<mul>(e) ) {
1796 return e.map(*this);
1798 else if ( is_a<numeric>(e) ) {
1799 numeric mod(R->modulus);
1800 numeric halfmod = (mod-1)/2;
1801 cl_MI emod = R->canonhom(the<cl_I>(ex_to<numeric>(e).to_cl_N()));
1802 numeric n(R->retract(emod));
1803 if ( n > halfmod ) {
1813 /** Helps mimicking modular multivariate polynomial arithmetic.
1815 * @param e expression of which to make the coefficients equal to their value
1816 * in the modular ring R (symmetric representation)
1817 * @param R modular ring
1818 * @return resulting expression
1820 static ex make_modular(const ex& e, const cl_modint_ring& R)
1822 make_modular_map map(R);
1823 return map(e.expand());
1826 /** Utility function for multivariate Hensel lifting.
1828 * Returns the polynomials s_i that fulfill
1829 * s_1*b_1 + ... + s_r*b_r == c mod <I^(d+1),p^k>
1830 * with given b_1 = a_1 * ... * a_{i-1} * a_{i+1} * ... * a_r
1832 * The implementation follows algorithm 6.2 of [GCL].
1834 * @param a_ vector of multivariate factors mod p^k
1835 * @param x symbol (equiv. x_1 in [GCL])
1836 * @param c polynomial mod p^k
1837 * @param I vector of evaluation points
1838 * @param d maximum total degree of result
1839 * @param p prime number
1840 * @param k p^k is modulus
1841 * @return vector of polynomials (s_i)
1843 static vector<ex> multivar_diophant(const vector<ex>& a_, const ex& x, const ex& c, const vector<EvalPoint>& I,
1844 unsigned int d, unsigned int p, unsigned int k)
1848 const cl_I modulus = expt_pos(cl_I(p),k);
1849 const cl_modint_ring R = find_modint_ring(modulus);
1850 const size_t r = a.size();
1851 const size_t nu = I.size() + 1;
1855 ex xnu = I.back().x;
1856 int alphanu = I.back().evalpoint;
1859 for ( size_t i=0; i<r; ++i ) {
1863 for ( size_t i=0; i<r; ++i ) {
1864 b[i] = normal(A / a[i]);
1867 vector<ex> anew = a;
1868 for ( size_t i=0; i<r; ++i ) {
1869 anew[i] = anew[i].subs(xnu == alphanu);
1871 ex cnew = c.subs(xnu == alphanu);
1872 vector<EvalPoint> Inew = I;
1874 sigma = multivar_diophant(anew, x, cnew, Inew, d, p, k);
1877 for ( size_t i=0; i<r; ++i ) {
1878 buf -= sigma[i] * b[i];
1880 ex e = make_modular(buf, R);
1883 for ( size_t m=1; !e.is_zero() && e.has(xnu) && m<=d; ++m ) {
1884 monomial *= (xnu - alphanu);
1885 monomial = expand(monomial);
1886 ex cm = e.diff(ex_to<symbol>(xnu), m).subs(xnu==alphanu) / factorial(m);
1887 cm = make_modular(cm, R);
1888 if ( !cm.is_zero() ) {
1889 vector<ex> delta_s = multivar_diophant(anew, x, cm, Inew, d, p, k);
1891 for ( size_t j=0; j<delta_s.size(); ++j ) {
1892 delta_s[j] *= monomial;
1893 sigma[j] += delta_s[j];
1894 buf -= delta_s[j] * b[j];
1896 e = make_modular(buf, R);
1901 for ( size_t i=0; i<a.size(); ++i ) {
1903 umodpoly_from_ex(up, a[i], x, R);
1907 sigma.insert(sigma.begin(), r, 0);
1910 if ( is_a<add>(c) ) {
1917 for ( size_t i=0; i<nterms; ++i ) {
1918 int m = z.degree(x);
1919 cl_I cm = the<cl_I>(ex_to<numeric>(z.lcoeff(x)).to_cl_N());
1920 upvec delta_s = univar_diophant(amod, x, m, p, k);
1922 cl_I poscm = plusp(cm) ? cm : mod(cm, modulus);
1923 modcm = cl_MI(R, poscm);
1924 for ( size_t j=0; j<delta_s.size(); ++j ) {
1925 delta_s[j] = delta_s[j] * modcm;
1926 sigma[j] = sigma[j] + umodpoly_to_ex(delta_s[j], x);
1928 if ( nterms > 1 && i+1 != nterms ) {
1934 for ( size_t i=0; i<sigma.size(); ++i ) {
1935 sigma[i] = make_modular(sigma[i], R);
1941 /** Multivariate Hensel lifting.
1942 * The implementation follows algorithm 6.4 of [GCL].
1943 * Since we don't have a data type for modular multivariate polynomials, the
1944 * respective operations are done in a GiNaC::ex and the function
1945 * make_modular() is then called to make the coefficient modular p^l.
1947 * @param a multivariate polynomial primitive in x
1948 * @param x symbol (equiv. x_1 in [GCL])
1949 * @param I vector of evaluation points (x_2==a_2,x_3==a_3,...)
1950 * @param p prime number (should not divide lcoeff(a mod I))
1951 * @param l p^l is the modulus of the lifted univariate field
1952 * @param u vector of modular (mod p^l) factors of a mod I
1953 * @param lcU correct leading coefficient of the univariate factors of a mod I
1954 * @return list GiNaC::lst with lifted factors (multivariate factors of a),
1955 * empty if Hensel lifting did not succeed
1957 static ex hensel_multivar(const ex& a, const ex& x, const vector<EvalPoint>& I,
1958 unsigned int p, const cl_I& l, const upvec& u, const vector<ex>& lcU)
1960 const size_t nu = I.size() + 1;
1961 const cl_modint_ring R = find_modint_ring(expt_pos(cl_I(p),l));
1966 for ( size_t j=nu; j>=2; --j ) {
1968 int alpha = I[j-2].evalpoint;
1969 A[j-2] = A[j-1].subs(x==alpha);
1970 A[j-2] = make_modular(A[j-2], R);
1973 int maxdeg = a.degree(I.front().x);
1974 for ( size_t i=1; i<I.size(); ++i ) {
1975 int maxdeg2 = a.degree(I[i].x);
1976 if ( maxdeg2 > maxdeg ) maxdeg = maxdeg2;
1979 const size_t n = u.size();
1981 for ( size_t i=0; i<n; ++i ) {
1982 U[i] = umodpoly_to_ex(u[i], x);
1985 for ( size_t j=2; j<=nu; ++j ) {
1988 for ( size_t m=0; m<n; ++m) {
1989 if ( lcU[m] != 1 ) {
1991 for ( size_t i=j-1; i<nu-1; ++i ) {
1992 coef = coef.subs(I[i].x == I[i].evalpoint);
1994 coef = make_modular(coef, R);
1995 int deg = U[m].degree(x);
1996 U[m] = U[m] - U[m].lcoeff(x) * pow(x,deg) + coef * pow(x,deg);
2000 for ( size_t i=0; i<n; ++i ) {
2003 ex e = expand(A[j-1] - Uprod);
2005 vector<EvalPoint> newI;
2006 for ( size_t i=1; i<=j-2; ++i ) {
2007 newI.push_back(I[i-1]);
2011 int alphaj = I[j-2].evalpoint;
2012 size_t deg = A[j-1].degree(xj);
2013 for ( size_t k=1; k<=deg; ++k ) {
2014 if ( !e.is_zero() ) {
2015 monomial *= (xj - alphaj);
2016 monomial = expand(monomial);
2017 ex dif = e.diff(ex_to<symbol>(xj), k);
2018 ex c = dif.subs(xj==alphaj) / factorial(k);
2019 if ( !c.is_zero() ) {
2020 vector<ex> deltaU = multivar_diophant(U1, x, c, newI, maxdeg, p, cl_I_to_uint(l));
2021 for ( size_t i=0; i<n; ++i ) {
2022 deltaU[i] *= monomial;
2024 U[i] = make_modular(U[i], R);
2027 for ( size_t i=0; i<n; ++i ) {
2031 e = make_modular(e, R);
2038 for ( size_t i=0; i<U.size(); ++i ) {
2041 if ( expand(a-acand).is_zero() ) {
2042 return lst(U.begin(), U.end());
2048 /** Takes a factorized expression and puts the factors in a vector. The exponents
2049 * of the factors are discarded, e.g. 7*x^2*(y+1)^4 --> {7,x,y+1}. The first
2050 * element of the result is always the numeric coefficient.
2052 static exvector put_factors_into_vec(const ex& e)
2055 if ( is_a<numeric>(e) ) {
2056 result.push_back(e);
2059 if ( is_a<power>(e) ) {
2060 result.push_back(1);
2061 result.push_back(e.op(0));
2064 if ( is_a<symbol>(e) || is_a<add>(e) ) {
2065 ex icont(e.integer_content());
2066 result.push_back(icont);
2067 result.push_back(e/icont);
2070 if ( is_a<mul>(e) ) {
2072 result.push_back(nfac);
2073 for ( size_t i=0; i<e.nops(); ++i ) {
2075 if ( is_a<numeric>(op) ) {
2078 if ( is_a<power>(op) ) {
2079 result.push_back(op.op(0));
2081 if ( is_a<symbol>(op) || is_a<add>(op) ) {
2082 result.push_back(op);
2088 throw runtime_error("put_factors_into_vec: bad term.");
2091 /** Checks a set of numbers for whether each number has a unique prime factor.
2093 * @param[in] f numbers to check
2094 * @return true: if number set is bad, false: if set is okay (has unique
2097 static bool checkdivisors(const exvector& f)
2099 const int k = f.size();
2101 vector<numeric> d(k);
2102 d[0] = ex_to<numeric>(abs(f[0]));
2103 for ( int i=1; i<k; ++i ) {
2104 q = ex_to<numeric>(abs(f[i]));
2105 for ( int j=i-1; j>=0; --j ) {
2120 /** Generates a set of evaluation points for a multivariate polynomial.
2121 * The set fulfills the following conditions:
2122 * 1. lcoeff(evaluated_polynomial) does not vanish
2123 * 2. factors of lcoeff(evaluated_polynomial) have each a unique prime factor
2124 * 3. evaluated_polynomial is square free
2125 * See [Wan] for more details.
2127 * @param[in] u multivariate polynomial to be factored
2128 * @param[in] vn leading coefficient of u in x (x==first symbol in syms)
2129 * @param[in] x first symbol that appears in u
2130 * @param[in] syms_wox remaining symbols that appear in u
2131 * @param[in] f vector containing the factors of the leading coefficient vn
2132 * @param[in,out] modulus integer modulus for random number generation (i.e. |a_i| < modulus)
2133 * @param[out] u0 returns the evaluated (univariate) polynomial
2134 * @param[out] a returns the valid evaluation points. must have initial size equal
2135 * number of symbols-1 before calling generate_set
2137 static void generate_set(const ex& u, const ex& vn, const ex& x, const exset& syms_wox, const exvector& f,
2138 numeric& modulus, ex& u0, vector<numeric>& a)
2142 // generate a set of integers ...
2146 auto s = syms_wox.begin();
2147 for ( size_t i=0; i<a.size(); ++i ) {
2149 a[i] = mod(numeric(rand()), 2*modulus) - modulus;
2150 vnatry = vna.subs(*s == a[i]);
2151 // ... for which the leading coefficient doesn't vanish ...
2152 } while ( vnatry == 0 );
2154 u0 = u0.subs(*s == a[i]);
2157 // ... for which u0 is square free ...
2158 ex g = gcd(u0, u0.diff(ex_to<symbol>(x)));
2159 if ( !is_a<numeric>(g) ) {
2162 if ( !is_a<numeric>(vn) ) {
2163 // ... and for which the evaluated factors have each an unique prime factor
2165 fnum[0] = fnum[0] * u0.content(x);
2166 for ( size_t i=1; i<fnum.size(); ++i ) {
2167 if ( !is_a<numeric>(fnum[i]) ) {
2168 s = syms_wox.begin();
2169 for ( size_t j=0; j<a.size(); ++j, ++s ) {
2170 fnum[i] = fnum[i].subs(*s == a[j]);
2174 if ( checkdivisors(fnum) ) {
2178 // ok, we have a valid set now
2183 // forward declaration
2184 static ex factor_sqrfree(const ex& poly);
2186 /** Used by factor_multivariate().
2188 struct factorization_ctx {
2189 const ex poly, x; // polynomial, first symbol x...
2190 const exset syms_wox; // ...remaining symbols w/o x
2191 ex unit, cont, pp; // unit * cont * pp == poly
2192 ex vn; exvector vnlst; // leading coeff, factors of leading coeff
2193 numeric modulus; // incremented each time we try
2194 /** returns factors or empty if it did not succeed */
2195 ex try_next_evaluation_homomorphism()
2197 constexpr unsigned maxtrials = 3;
2198 vector<numeric> a(syms_wox.size(), 0);
2200 unsigned int trialcount = 0;
2202 int factor_count = 0;
2203 int min_factor_count = -1;
2208 // try several evaluation points to reduce the number of factors
2209 while ( trialcount < maxtrials ) {
2211 // generate a set of valid evaluation points
2212 generate_set(pp, vn, x, syms_wox, vnlst, modulus, u, a);
2214 ufac = factor_univariate(u, x, prime);
2215 ufaclst = put_factors_into_vec(ufac);
2216 factor_count = ufaclst.size()-1;
2219 if ( factor_count <= 1 ) {
2223 if ( min_factor_count < 0 ) {
2225 min_factor_count = factor_count;
2227 else if ( min_factor_count == factor_count ) {
2231 else if ( min_factor_count > factor_count ) {
2232 // new minimum, reset trial counter
2233 min_factor_count = factor_count;
2238 // determine true leading coefficients for the Hensel lifting
2239 vector<ex> C(factor_count);
2240 if ( is_a<numeric>(vn) ) {
2242 for ( size_t i=1; i<ufaclst.size(); ++i ) {
2243 C[i-1] = ufaclst[i].lcoeff(x);
2247 // we use the property of the ftilde having a unique prime factor.
2248 // details can be found in [Wan].
2250 vector<numeric> ftilde(vnlst.size()-1);
2251 for ( size_t i=0; i<ftilde.size(); ++i ) {
2253 auto s = syms_wox.begin();
2254 for ( size_t j=0; j<a.size(); ++j ) {
2255 ft = ft.subs(*s == a[j]);
2258 ftilde[i] = ex_to<numeric>(ft);
2260 // calculate D and C
2261 vector<bool> used_flag(ftilde.size(), false);
2262 vector<ex> D(factor_count, 1);
2264 for ( int i=0; i<factor_count; ++i ) {
2265 numeric prefac = ex_to<numeric>(ufaclst[i+1].lcoeff(x));
2266 for ( int j=ftilde.size()-1; j>=0; --j ) {
2269 while ( irem(prefac, ftilde[j], q) == 0 ) {
2274 used_flag[j] = true;
2275 D[i] = D[i] * pow(vnlst[j+1], count);
2278 C[i] = D[i] * prefac;
2281 for ( int i=0; i<factor_count; ++i ) {
2282 numeric prefac = ex_to<numeric>(ufaclst[i+1].lcoeff(x));
2283 for ( int j=ftilde.size()-1; j>=0; --j ) {
2286 while ( irem(prefac, ftilde[j], q) == 0 ) {
2290 while ( irem(ex_to<numeric>(delta)*prefac, ftilde[j]) == 0 ) {
2291 numeric g = gcd(prefac, ex_to<numeric>(ftilde[j]));
2292 prefac = iquo(prefac, g);
2293 delta = delta / (ftilde[j]/g);
2294 ufaclst[i+1] = ufaclst[i+1] * (ftilde[j]/g);
2298 used_flag[j] = true;
2299 D[i] = D[i] * pow(vnlst[j+1], count);
2302 C[i] = D[i] * prefac;
2305 // check if something went wrong
2306 bool some_factor_unused = false;
2307 for ( size_t i=0; i<used_flag.size(); ++i ) {
2308 if ( !used_flag[i] ) {
2309 some_factor_unused = true;
2313 if ( some_factor_unused ) {
2314 return lst{}; // next try
2318 // multiply the remaining content of the univariate polynomial into the
2321 C[0] = C[0] * delta;
2322 ufaclst[1] = ufaclst[1] * delta;
2325 // set up evaluation points
2326 vector<EvalPoint> epv;
2327 auto s = syms_wox.begin();
2328 for ( size_t i=0; i<a.size(); ++i ) {
2329 epv.emplace_back(EvalPoint{*s++, a[i].to_int()});
2334 for ( int i=1; i<=factor_count; ++i ) {
2335 if ( ufaclst[i].degree(x) > maxdeg ) {
2336 maxdeg = ufaclst[i].degree(x);
2339 cl_I B = ash(calc_bound(u, x), maxdeg+1); // = 2 * calc_bound(u,x) * 2^maxdeg
2347 // set up modular factors (mod p^l)
2348 cl_modint_ring R = find_modint_ring(pl);
2349 upvec modfactors(ufaclst.size()-1);
2350 for ( size_t i=1; i<ufaclst.size(); ++i ) {
2351 umodpoly_from_ex(modfactors[i-1], ufaclst[i], x, R);
2354 // try Hensel lifting
2355 return hensel_multivar(pp, x, epv, prime, l, modfactors, C);
2359 /** Multivariate factorization.
2361 * The implementation is based on the algorithm described in [Wan].
2362 * An evaluation homomorphism (a set of integers) is determined that fulfills
2363 * certain criteria. The evaluated polynomial is univariate and is factorized
2364 * by factor_univariate(). The main work then is to find the correct leading
2365 * coefficients of the univariate factors. They have to correspond to the
2366 * factors of the (multivariate) leading coefficient of the input polynomial
2367 * (as defined for a specific variable x). After that the Hensel lifting can be
2368 * performed. This is done in round-robin for each x in syms until success.
2370 * @param[in] poly expanded, square free polynomial
2371 * @param[in] syms contains the symbols in the polynomial
2372 * @return factorized polynomial
2374 static ex factor_multivariate(const ex& poly, const exset& syms)
2376 // set up one factorization context for each symbol
2377 vector<factorization_ctx> ctx_in_x;
2378 for (auto x : syms) {
2379 exset syms_wox; // remaining syms w/o x
2380 copy_if(syms.begin(), syms.end(),
2381 inserter(syms_wox, syms_wox.end()), [x](const ex& y){ return y != x; });
2383 factorization_ctx ctx = {.poly = poly, .x = x,
2384 .syms_wox = syms_wox};
2386 // make polynomial primitive
2387 poly.unitcontprim(x, ctx.unit, ctx.cont, ctx.pp);
2388 if ( !is_a<numeric>(ctx.cont) ) {
2389 // content is a polynomial in one or more of remaining syms, let's start over
2390 return ctx.unit * factor_sqrfree(ctx.cont) * factor_sqrfree(ctx.pp);
2393 // find factors of leading coefficient
2394 ctx.vn = ctx.pp.collect(x).lcoeff(x);
2395 ctx.vnlst = put_factors_into_vec(factor(ctx.vn));
2397 ctx.modulus = (ctx.vnlst.size() > 3) ? ctx.vnlst.size() : numeric(3);
2399 ctx_in_x.push_back(ctx);
2402 // try an evaluation homomorphism for each context in round-robin
2403 auto ctx = ctx_in_x.begin();
2406 ex res = ctx->try_next_evaluation_homomorphism();
2408 if ( res != lst{} ) {
2409 // found the factors
2410 ex result = ctx->cont * ctx->unit;
2411 for ( size_t i=0; i<res.nops(); ++i ) {
2413 res.op(i).unitcontprim(ctx->x, unit, cont, pp);
2414 result *= unit * cont * pp;
2419 // switch context for next symbol
2420 if (++ctx == ctx_in_x.end()) {
2421 ctx = ctx_in_x.begin();
2426 /** Finds all symbols in an expression. Used by factor_sqrfree() and factor().
2428 struct find_symbols_map : public map_function {
2430 ex operator()(const ex& e) override
2432 if ( is_a<symbol>(e) ) {
2436 return e.map(*this);
2440 /** Factorizes a polynomial that is square free. It calls either the univariate
2441 * or the multivariate factorization functions.
2443 static ex factor_sqrfree(const ex& poly)
2445 // determine all symbols in poly
2446 find_symbols_map findsymbols;
2448 if ( findsymbols.syms.size() == 0 ) {
2452 if ( findsymbols.syms.size() == 1 ) {
2454 const ex& x = *(findsymbols.syms.begin());
2455 int ld = poly.ldegree(x);
2457 // pull out direct factors
2458 ex res = factor_univariate(expand(poly/pow(x, ld)), x);
2459 return res * pow(x,ld);
2461 ex res = factor_univariate(poly, x);
2466 // multivariate case
2467 ex res = factor_multivariate(poly, findsymbols.syms);
2471 /** Map used by factor() when factor_options::all is given to access all
2472 * subexpressions and to call factor() on them.
2474 struct apply_factor_map : public map_function {
2476 apply_factor_map(unsigned options_) : options(options_) { }
2477 ex operator()(const ex& e) override
2479 if ( e.info(info_flags::polynomial) ) {
2480 return factor(e, options);
2482 if ( is_a<add>(e) ) {
2484 for ( size_t i=0; i<e.nops(); ++i ) {
2485 if ( e.op(i).info(info_flags::polynomial) ) {
2491 return factor(s1, options) + s2.map(*this);
2493 return e.map(*this);
2497 /** Iterate through explicit factors of e, call yield(f, k) for
2498 * each factor of the form f^k.
2500 * Note that this function doesn't factor e itself, it only
2501 * iterates through the factors already explicitly present.
2503 template <typename F> void
2504 factor_iter(const ex &e, F yield)
2507 for (const auto &f : e) {
2508 if (is_a<power>(f)) {
2509 yield(f.op(0), f.op(1));
2515 if (is_a<power>(e)) {
2516 yield(e.op(0), e.op(1));
2523 /** This function factorizes a polynomial. It checks the arguments,
2524 * tries a square free factorization, and then calls factor_sqrfree
2525 * to do the hard work.
2527 * This function expands its argument, so for polynomials with
2528 * explicit factors it's better to call it on each one separately
2529 * (or use factor() which does just that).
2531 static ex factor1(const ex& poly, unsigned options)
2534 if ( !poly.info(info_flags::polynomial) ) {
2535 if ( options & factor_options::all ) {
2536 options &= ~factor_options::all;
2537 apply_factor_map factor_map(options);
2538 return factor_map(poly);
2543 // determine all symbols in poly
2544 find_symbols_map findsymbols;
2546 if ( findsymbols.syms.size() == 0 ) {
2550 for (auto & i : findsymbols.syms ) {
2554 // make poly square free
2555 ex sfpoly = sqrfree(poly.expand(), syms);
2557 // factorize the square free components
2560 [&](const ex &f, const ex &k) {
2561 if ( is_a<add>(f) ) {
2562 res *= pow(factor_sqrfree(f), k);
2564 // simple case: (monomial)^exponent
2571 } // anonymous namespace
2573 /** Interface function to the outside world. It uses factor1()
2574 * on each of the explicitly present factors of poly.
2576 ex factor(const ex& poly, unsigned options)
2580 [&](const ex &f1, const ex &k1) {
2581 factor_iter(factor1(f1, options),
2582 [&](const ex &f2, const ex &k2) {
2583 result *= pow(f2, k1*k2);
2589 } // namespace GiNaC