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-2022 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 static bool normalize_in_field(umodpoly& a)
165 if ( lcoeff(a) == a[0].ring()->one() ) {
169 const cln::cl_MI lc_1 = recip(lcoeff(a));
170 for (std::size_t k = a.size(); k-- != 0; )
175 template<typename T> static void
176 canonicalize(T& p, const typename T::size_type hint = std::numeric_limits<typename T::size_type>::max())
181 std::size_t i = p.size() - 1;
182 // Be fast if the polynomial is already canonicalized
189 bool is_zero = false;
207 p.erase(p.begin() + i, p.end());
210 // END COPY FROM UPOLY.H
212 static void expt_pos(umodpoly& a, unsigned int q)
214 if ( a.empty() ) return;
215 cl_MI zero = a[0].ring()->zero();
217 a.resize(degree(a)*q+1, zero);
218 for ( int i=deg; i>0; --i ) {
224 template<typename T> struct uvar_poly_p
226 static const bool value = false;
229 template<> struct uvar_poly_p<upoly>
231 static const bool value = true;
234 template<> struct uvar_poly_p<umodpoly>
236 static const bool value = true;
240 // Don't define this for anything but univariate polynomials.
241 static typename enable_if<uvar_poly_p<T>::value, T>::type
242 operator+(const T& a, const T& b)
249 for ( ; i<sb; ++i ) {
252 for ( ; i<sa; ++i ) {
261 for ( ; i<sa; ++i ) {
264 for ( ; i<sb; ++i ) {
273 // Don't define this for anything but univariate polynomials. Otherwise
274 // overload resolution might fail (this actually happens when compiling
275 // GiNaC with g++ 3.4).
276 static typename enable_if<uvar_poly_p<T>::value, T>::type
277 operator-(const T& a, const T& b)
284 for ( ; i<sb; ++i ) {
287 for ( ; i<sa; ++i ) {
296 for ( ; i<sa; ++i ) {
299 for ( ; i<sb; ++i ) {
307 static upoly operator*(const upoly& a, const upoly& b)
310 if ( a.empty() || b.empty() ) return c;
312 int n = degree(a) + degree(b);
314 for ( int i=0 ; i<=n; ++i ) {
315 for ( int j=0 ; j<=i; ++j ) {
316 if ( j > degree(a) || (i-j) > degree(b) ) continue;
317 c[i] = c[i] + a[j] * b[i-j];
324 static umodpoly operator*(const umodpoly& a, const umodpoly& b)
327 if ( a.empty() || b.empty() ) return c;
329 int n = degree(a) + degree(b);
330 c.resize(n+1, a[0].ring()->zero());
331 for ( int i=0 ; i<=n; ++i ) {
332 for ( int j=0 ; j<=i; ++j ) {
333 if ( j > degree(a) || (i-j) > degree(b) ) continue;
334 c[i] = c[i] + a[j] * b[i-j];
341 static upoly operator*(const upoly& a, const cl_I& x)
348 for ( size_t i=0; i<a.size(); ++i ) {
354 static upoly operator/(const upoly& a, const cl_I& x)
361 for ( size_t i=0; i<a.size(); ++i ) {
362 r[i] = exquo(a[i],x);
367 static umodpoly operator*(const umodpoly& a, const cl_MI& x)
369 umodpoly r(a.size());
370 for ( size_t i=0; i<a.size(); ++i ) {
377 static void upoly_from_ex(upoly& up, const ex& e, const ex& x)
379 // assert: e is in Z[x]
380 int deg = e.degree(x);
382 int ldeg = e.ldegree(x);
383 for ( ; deg>=ldeg; --deg ) {
384 up[deg] = the<cl_I>(ex_to<numeric>(e.coeff(x, deg)).to_cl_N());
386 for ( ; deg>=0; --deg ) {
392 static void umodpoly_from_upoly(umodpoly& ump, const upoly& e, const cl_modint_ring& R)
396 for ( ; deg>=0; --deg ) {
397 ump[deg] = R->canonhom(e[deg]);
402 static void umodpoly_from_ex(umodpoly& ump, const ex& e, const ex& x, const cl_modint_ring& R)
404 // assert: e is in Z[x]
405 int deg = e.degree(x);
407 int ldeg = e.ldegree(x);
408 for ( ; deg>=ldeg; --deg ) {
409 cl_I coeff = the<cl_I>(ex_to<numeric>(e.coeff(x, deg)).to_cl_N());
410 ump[deg] = R->canonhom(coeff);
412 for ( ; deg>=0; --deg ) {
413 ump[deg] = R->zero();
419 static void umodpoly_from_ex(umodpoly& ump, const ex& e, const ex& x, const cl_I& modulus)
421 umodpoly_from_ex(ump, e, x, find_modint_ring(modulus));
425 static ex upoly_to_ex(const upoly& a, const ex& x)
427 if ( a.empty() ) return 0;
429 for ( int i=degree(a); i>=0; --i ) {
430 e += numeric(a[i]) * pow(x, i);
435 static ex umodpoly_to_ex(const umodpoly& a, const ex& x)
437 if ( a.empty() ) return 0;
438 cl_modint_ring R = a[0].ring();
439 cl_I mod = R->modulus;
440 cl_I halfmod = (mod-1) >> 1;
442 for ( int i=degree(a); i>=0; --i ) {
443 cl_I n = R->retract(a[i]);
445 e += numeric(n-mod) * pow(x, i);
447 e += numeric(n) * pow(x, i);
453 static upoly umodpoly_to_upoly(const umodpoly& a)
456 if ( a.empty() ) return e;
457 cl_modint_ring R = a[0].ring();
458 cl_I mod = R->modulus;
459 cl_I halfmod = (mod-1) >> 1;
460 for ( int i=degree(a); i>=0; --i ) {
461 cl_I n = R->retract(a[i]);
471 static umodpoly umodpoly_to_umodpoly(const umodpoly& a, const cl_modint_ring& R, unsigned int m)
474 if ( a.empty() ) return e;
475 cl_modint_ring oldR = a[0].ring();
476 size_t sa = a.size();
477 e.resize(sa+m, R->zero());
478 for ( size_t i=0; i<sa; ++i ) {
479 e[i+m] = R->canonhom(oldR->retract(a[i]));
485 /** Divides all coefficients of the polynomial a by the integer x.
486 * All coefficients are supposed to be divisible by x. If they are not, the
487 * the<cl_I> cast will raise an exception.
489 * @param[in,out] a polynomial of which the coefficients will be reduced by x
490 * @param[in] x integer that divides the coefficients
492 static void reduce_coeff(umodpoly& a, const cl_I& x)
494 if ( a.empty() ) return;
496 cl_modint_ring R = a[0].ring();
498 // cln cannot perform this division in the modular field
499 cl_I c = R->retract(i);
500 i = cl_MI(R, the<cl_I>(c / x));
504 /** Calculates remainder of a/b.
505 * Assertion: a and b not empty.
507 * @param[in] a polynomial dividend
508 * @param[in] b polynomial divisor
509 * @param[out] r polynomial remainder
511 static void rem(const umodpoly& a, const umodpoly& b, umodpoly& r)
520 cl_MI qk = div(r[n+k], b[n]);
522 for ( int i=0; i<n; ++i ) {
523 unsigned int j = n + k - 1 - i;
524 r[j] = r[j] - qk * b[j-k];
529 fill(r.begin()+n, r.end(), a[0].ring()->zero());
533 /** Calculates quotient of a/b.
534 * Assertion: a and b not empty.
536 * @param[in] a polynomial dividend
537 * @param[in] b polynomial divisor
538 * @param[out] q polynomial quotient
540 static void div(const umodpoly& a, const umodpoly& b, umodpoly& q)
549 q.resize(k+1, a[0].ring()->zero());
551 cl_MI qk = div(r[n+k], b[n]);
554 for ( int i=0; i<n; ++i ) {
555 unsigned int j = n + k - 1 - i;
556 r[j] = r[j] - qk * b[j-k];
564 /** Calculates quotient and remainder of a/b.
565 * Assertion: a and b not empty.
567 * @param[in] a polynomial dividend
568 * @param[in] b polynomial divisor
569 * @param[out] r polynomial remainder
570 * @param[out] q polynomial quotient
572 static void remdiv(const umodpoly& a, const umodpoly& b, umodpoly& r, umodpoly& q)
581 q.resize(k+1, a[0].ring()->zero());
583 cl_MI qk = div(r[n+k], b[n]);
586 for ( int i=0; i<n; ++i ) {
587 unsigned int j = n + k - 1 - i;
588 r[j] = r[j] - qk * b[j-k];
593 fill(r.begin()+n, r.end(), a[0].ring()->zero());
598 /** Calculates the GCD of polynomial a and b.
600 * @param[in] a polynomial
601 * @param[in] b polynomial
604 static void gcd(const umodpoly& a, const umodpoly& b, umodpoly& c)
606 if ( degree(a) < degree(b) ) return gcd(b, a, c);
609 normalize_in_field(c);
611 normalize_in_field(d);
613 while ( !d.empty() ) {
618 normalize_in_field(c);
621 /** Calculates the derivative of the polynomial a.
623 * @param[in] a polynomial of which to take the derivative
624 * @param[out] d result/derivative
626 static void deriv(const umodpoly& a, umodpoly& d)
629 if ( a.size() <= 1 ) return;
631 d.insert(d.begin(), a.begin()+1, a.end());
633 for ( int i=1; i<max; ++i ) {
639 static bool unequal_one(const umodpoly& a)
641 if ( a.empty() ) return true;
642 return ( a.size() != 1 || a[0] != a[0].ring()->one() );
645 static bool equal_one(const umodpoly& a)
647 return ( a.size() == 1 && a[0] == a[0].ring()->one() );
650 /** Returns true if polynomial a is square free.
652 * @param[in] a polynomial to check
653 * @return true if polynomial is square free, false otherwise
655 static bool squarefree(const umodpoly& a)
667 // END modular univariate polynomial code
668 ////////////////////////////////////////////////////////////////////////////////
670 ////////////////////////////////////////////////////////////////////////////////
673 typedef vector<cl_MI> mvec;
678 friend ostream& operator<<(ostream& o, const modular_matrix& m);
681 modular_matrix(size_t r_, size_t c_, const cl_MI& init) : r(r_), c(c_)
685 size_t rowsize() const { return r; }
686 size_t colsize() const { return c; }
687 cl_MI& operator()(size_t row, size_t col) { return m[row*c + col]; }
688 cl_MI operator()(size_t row, size_t col) const { return m[row*c + col]; }
689 void mul_col(size_t col, const cl_MI x)
691 for ( size_t rc=0; rc<r; ++rc ) {
692 std::size_t i = c*rc + col;
696 void sub_col(size_t col1, size_t col2, const cl_MI fac)
698 for ( size_t rc=0; rc<r; ++rc ) {
699 std::size_t i1 = col1 + c*rc;
700 std::size_t i2 = col2 + c*rc;
701 m[i1] = m[i1] - m[i2]*fac;
704 void switch_col(size_t col1, size_t col2)
706 for ( size_t rc=0; rc<r; ++rc ) {
707 std::size_t i1 = col1 + rc*c;
708 std::size_t i2 = col2 + rc*c;
709 std::swap(m[i1], m[i2]);
712 void mul_row(size_t row, const cl_MI x)
714 for ( size_t cc=0; cc<c; ++cc ) {
715 std::size_t i = row*c + cc;
719 void sub_row(size_t row1, size_t row2, const cl_MI fac)
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 m[i1] = m[i1] - m[i2]*fac;
727 void switch_row(size_t row1, size_t row2)
729 for ( size_t cc=0; cc<c; ++cc ) {
730 std::size_t i1 = row1*c + cc;
731 std::size_t i2 = row2*c + cc;
732 std::swap(m[i1], m[i2]);
735 bool is_col_zero(size_t col) const
737 for ( size_t rr=0; rr<r; ++rr ) {
738 std::size_t i = col + rr*c;
739 if ( !zerop(m[i]) ) {
745 bool is_row_zero(size_t row) const
747 for ( size_t cc=0; cc<c; ++cc ) {
748 std::size_t i = row*c + cc;
749 if ( !zerop(m[i]) ) {
755 void set_row(size_t row, const vector<cl_MI>& newrow)
757 for (std::size_t i2 = 0; i2 < newrow.size(); ++i2) {
758 std::size_t i1 = row*c + i2;
762 mvec::const_iterator row_begin(size_t row) const { return m.begin()+row*c; }
763 mvec::const_iterator row_end(size_t row) const { return m.begin()+row*c+r; }
770 modular_matrix operator*(const modular_matrix& m1, const modular_matrix& m2)
772 const unsigned int r = m1.rowsize();
773 const unsigned int c = m2.colsize();
774 modular_matrix o(r,c,m1(0,0));
776 for ( size_t i=0; i<r; ++i ) {
777 for ( size_t j=0; j<c; ++j ) {
779 buf = m1(i,0) * m2(0,j);
780 for ( size_t k=1; k<c; ++k ) {
781 buf = buf + m1(i,k)*m2(k,j);
789 ostream& operator<<(ostream& o, const modular_matrix& m)
791 cl_modint_ring R = m(0,0).ring();
793 for ( size_t i=0; i<m.rowsize(); ++i ) {
795 for ( size_t j=0; j<m.colsize()-1; ++j ) {
796 o << R->retract(m(i,j)) << ",";
798 o << R->retract(m(i,m.colsize()-1)) << "}";
799 if ( i != m.rowsize()-1 ) {
806 #endif // def DEBUGFACTOR
808 // END modular matrix
809 ////////////////////////////////////////////////////////////////////////////////
811 /** Calculates the Q matrix for a polynomial. Used by Berlekamp's algorithm.
813 * The implementation follows algorithm 8.5 of [GCL].
815 * @param[in] a_ modular polynomial
816 * @param[out] Q Q matrix
818 static void q_matrix(const umodpoly& a_, modular_matrix& Q)
821 normalize_in_field(a);
824 unsigned int q = cl_I_to_uint(a[0].ring()->modulus);
825 umodpoly r(n, a[0].ring()->zero());
826 r[0] = a[0].ring()->one();
828 unsigned int max = (n-1) * q;
829 for ( size_t m=1; m<=max; ++m ) {
830 cl_MI rn_1 = r.back();
831 for ( size_t i=n-1; i>0; --i ) {
832 r[i] = r[i-1] - (rn_1 * a[i]);
835 if ( (m % q) == 0 ) {
841 /** Determine the nullspace of a matrix M-1.
843 * @param[in,out] M matrix, will be modified
844 * @param[out] basis calculated nullspace of M-1
846 static void nullspace(modular_matrix& M, vector<mvec>& basis)
848 const size_t n = M.rowsize();
849 const cl_MI one = M(0,0).ring()->one();
850 for ( size_t i=0; i<n; ++i ) {
851 M(i,i) = M(i,i) - one;
853 for ( size_t r=0; r<n; ++r ) {
855 for ( ; cc<n; ++cc ) {
856 if ( !zerop(M(r,cc)) ) {
858 if ( !zerop(M(cc,cc)) ) {
870 M.mul_col(r, recip(M(r,r)));
871 for ( cc=0; cc<n; ++cc ) {
873 M.sub_col(cc, r, M(r,cc));
879 for ( size_t i=0; i<n; ++i ) {
880 M(i,i) = M(i,i) - one;
882 for ( size_t i=0; i<n; ++i ) {
883 if ( !M.is_row_zero(i) ) {
884 mvec nu(M.row_begin(i), M.row_end(i));
890 /** Berlekamp's modular factorization.
892 * The implementation follows algorithm 8.4 of [GCL].
894 * @param[in] a modular polynomial
895 * @param[out] upv vector containing modular factors. if upv was not empty the
896 * new elements are added at the end
898 static void berlekamp(const umodpoly& a, upvec& upv)
900 cl_modint_ring R = a[0].ring();
901 umodpoly one(1, R->one());
903 // find nullspace of Q matrix
904 modular_matrix Q(degree(a), degree(a), R->zero());
909 const unsigned int k = nu.size();
915 list<umodpoly> factors = {a};
916 unsigned int size = 1;
918 unsigned int q = cl_I_to_uint(R->modulus);
920 list<umodpoly>::iterator u = factors.begin();
922 // calculate all gcd's
924 for ( unsigned int s=0; s<q; ++s ) {
925 umodpoly nur = nu[r];
926 nur[0] = nur[0] - cl_MI(R, s);
930 if ( unequal_one(g) && g != *u ) {
933 if ( equal_one(uo) ) {
934 throw logic_error("berlekamp: unexpected divisor.");
938 factors.push_back(g);
940 for (auto & i : factors) {
945 for (auto & i : factors) {
959 // modular square free factorization is not used at the moment so we deactivate
963 /** Calculates a^(1/prime).
965 * @param[in] a polynomial
966 * @param[in] prime prime number -> exponent 1/prime
967 * @param[in] ap resulting polynomial
969 static void expt_1_over_p(const umodpoly& a, unsigned int prime, umodpoly& ap)
971 size_t newdeg = degree(a)/prime;
974 for ( size_t i=1; i<=newdeg; ++i ) {
979 /** Modular square free factorization.
981 * @param[in] a polynomial
982 * @param[out] factors modular factors
983 * @param[out] mult corresponding multiplicities (exponents)
985 static void modsqrfree(const umodpoly& a, upvec& factors, vector<int>& mult)
987 const unsigned int prime = cl_I_to_uint(a[0].ring()->modulus);
996 while ( unequal_one(w) ) {
1001 factors.push_back(z);
1009 if ( unequal_one(c) ) {
1011 expt_1_over_p(c, prime, cp);
1012 size_t previ = mult.size();
1013 modsqrfree(cp, factors, mult);
1014 for ( size_t i=previ; i<mult.size(); ++i ) {
1020 expt_1_over_p(a, prime, ap);
1021 size_t previ = mult.size();
1022 modsqrfree(ap, factors, mult);
1023 for ( size_t i=previ; i<mult.size(); ++i ) {
1029 #endif // deactivation of square free factorization
1031 /** Distinct degree factorization (DDF).
1033 * The implementation follows algorithm 8.8 of [GCL].
1035 * @param[in] a_ modular polynomial
1036 * @param[out] degrees vector containing the degrees of the factors of the
1037 * corresponding polynomials in ddfactors.
1038 * @param[out] ddfactors vector containing polynomials which factors have the
1039 * degree given in degrees.
1041 static void distinct_degree_factor(const umodpoly& a_, vector<int>& degrees, upvec& ddfactors)
1045 cl_modint_ring R = a[0].ring();
1046 int q = cl_I_to_int(R->modulus);
1047 int nhalf = degree(a)/2;
1055 while ( i <= nhalf ) {
1060 umodpoly wx = w - x;
1062 if ( unequal_one(buf) ) {
1063 degrees.push_back(i);
1064 ddfactors.push_back(buf);
1066 if ( unequal_one(buf) ) {
1070 nhalf = degree(a)/2;
1076 if ( unequal_one(a) ) {
1077 degrees.push_back(degree(a));
1078 ddfactors.push_back(a);
1082 /** Modular same degree factorization.
1083 * Same degree factorization is a kind of misnomer. It performs distinct degree
1084 * factorization, but instead of using the Cantor-Zassenhaus algorithm it
1085 * (sub-optimally) uses Berlekamp's algorithm for the factors of the same
1088 * @param[in] a modular polynomial
1089 * @param[out] upv vector containing modular factors. if upv was not empty the
1090 * new elements are added at the end
1092 static void same_degree_factor(const umodpoly& a, upvec& upv)
1094 cl_modint_ring R = a[0].ring();
1096 vector<int> degrees;
1098 distinct_degree_factor(a, degrees, ddfactors);
1100 for ( size_t i=0; i<degrees.size(); ++i ) {
1101 if ( degrees[i] == degree(ddfactors[i]) ) {
1102 upv.push_back(ddfactors[i]);
1104 berlekamp(ddfactors[i], upv);
1109 // Yes, we can (choose).
1110 #define USE_SAME_DEGREE_FACTOR
1112 /** Modular univariate factorization.
1114 * In principle, we have two algorithms at our disposal: Berlekamp's algorithm
1115 * and same degree factorization (SDF). SDF seems to be slightly faster in
1116 * almost all cases so it is activated as default.
1118 * @param[in] p modular polynomial
1119 * @param[out] upv vector containing modular factors. if upv was not empty the
1120 * new elements are added at the end
1122 static void factor_modular(const umodpoly& p, upvec& upv)
1124 #ifdef USE_SAME_DEGREE_FACTOR
1125 same_degree_factor(p, upv);
1131 /** Calculates modular polynomials s and t such that a*s+b*t==1.
1132 * Assertion: a and b are relatively prime and not zero.
1134 * @param[in] a polynomial
1135 * @param[in] b polynomial
1136 * @param[out] s polynomial
1137 * @param[out] t polynomial
1139 static void exteuclid(const umodpoly& a, const umodpoly& b, umodpoly& s, umodpoly& t)
1141 if ( degree(a) < degree(b) ) {
1142 exteuclid(b, a, t, s);
1146 umodpoly one(1, a[0].ring()->one());
1147 umodpoly c = a; normalize_in_field(c);
1148 umodpoly d = b; normalize_in_field(d);
1156 umodpoly r = c - q * d;
1157 umodpoly r1 = s - q * d1;
1158 umodpoly r2 = t - q * d2;
1162 if ( r.empty() ) break;
1167 cl_MI fac = recip(lcoeff(a) * lcoeff(c));
1168 for (auto & i : s) {
1172 fac = recip(lcoeff(b) * lcoeff(c));
1173 for (auto & i : t) {
1179 /** Replaces the leading coefficient in a polynomial by a given number.
1181 * @param[in] poly polynomial to change
1182 * @param[in] lc new leading coefficient
1183 * @return changed polynomial
1185 static upoly replace_lc(const upoly& poly, const cl_I& lc)
1187 if ( poly.empty() ) return poly;
1193 /** Calculates bound for the product of absolute values (modulus) of the roots.
1194 * Uses Landau's inequality, see [Mig].
1196 static inline cl_I calc_bound(const ex& a, const ex& x)
1199 for ( int i=a.degree(x); i>=a.ldegree(x); --i ) {
1200 cl_I aa = abs(the<cl_I>(ex_to<numeric>(a.coeff(x, i)).to_cl_N()));
1201 radicand = radicand + square(aa);
1203 return ceiling1(the<cl_R>(cln::sqrt(radicand)));
1206 /** Calculates bound for the product of absolute values (modulus) of the roots.
1207 * Uses Landau's inequality, see [Mig].
1209 static inline cl_I calc_bound(const upoly& a)
1212 for ( int i=degree(a); i>=0; --i ) {
1213 cl_I aa = abs(a[i]);
1214 radicand = radicand + square(aa);
1216 return ceiling1(the<cl_R>(cln::sqrt(radicand)));
1219 /** Hensel lifting as used by factor_univariate().
1221 * The implementation follows algorithm 6.1 of [GCL].
1223 * @param[in] a_ primitive univariate polynomials
1224 * @param[in] p prime number that does not divide lcoeff(a)
1225 * @param[in] u1_ modular factor of a (mod p)
1226 * @param[in] w1_ modular factor of a (mod p), relatively prime to u1_,
1227 * fulfilling u1_*w1_ == a mod p
1228 * @param[out] u lifted factor
1229 * @param[out] w lifted factor, u*w = a
1231 static void hensel_univar(const upoly& a_, unsigned int p, const umodpoly& u1_, const umodpoly& w1_, upoly& u, upoly& w)
1234 const cl_modint_ring& R = u1_[0].ring();
1237 int maxdeg = (degree(u1_) > degree(w1_)) ? degree(u1_) : degree(w1_);
1238 cl_I maxmodulus = calc_bound(a) * ash(cl_I(1), maxdeg+1); // 2 * calc_bound(a) * 2^maxdeg
1241 cl_I alpha = lcoeff(a);
1244 normalize_in_field(nu1);
1246 normalize_in_field(nw1);
1248 phi = umodpoly_to_upoly(nu1) * alpha;
1250 umodpoly_from_upoly(u1, phi, R);
1251 phi = umodpoly_to_upoly(nw1) * alpha;
1253 umodpoly_from_upoly(w1, phi, R);
1258 exteuclid(u1, w1, s, t);
1261 u = replace_lc(umodpoly_to_upoly(u1), alpha);
1262 w = replace_lc(umodpoly_to_upoly(w1), alpha);
1263 upoly e = a - u * w;
1267 while ( !e.empty() && modulus < maxmodulus ) {
1268 upoly c = e / modulus;
1269 phi = umodpoly_to_upoly(s) * c;
1270 umodpoly sigmatilde;
1271 umodpoly_from_upoly(sigmatilde, phi, R);
1272 phi = umodpoly_to_upoly(t) * c;
1274 umodpoly_from_upoly(tautilde, phi, R);
1276 remdiv(sigmatilde, w1, r, q);
1278 phi = umodpoly_to_upoly(tautilde) + umodpoly_to_upoly(q) * umodpoly_to_upoly(u1);
1280 umodpoly_from_upoly(tau, phi, R);
1281 u = u + umodpoly_to_upoly(tau) * modulus;
1282 w = w + umodpoly_to_upoly(sigma) * modulus;
1284 modulus = modulus * p;
1290 for ( size_t i=1; i<u.size(); ++i ) {
1292 if ( g == 1 ) break;
1306 /** Returns a new prime number.
1308 * @param[in] p prime number
1309 * @return next prime number after p
1311 static unsigned int next_prime(unsigned int p)
1313 static vector<unsigned int> primes;
1314 if (primes.empty()) {
1317 if ( p >= primes.back() ) {
1318 unsigned int candidate = primes.back() + 2;
1320 size_t n = primes.size()/2;
1321 for ( size_t i=0; i<n; ++i ) {
1322 if (candidate % primes[i])
1327 primes.push_back(candidate);
1333 for (auto & it : primes) {
1338 throw logic_error("next_prime: should not reach this point!");
1341 /** Manages the splitting of a vector of modular factors into two partitions.
1343 class factor_partition
1346 /** Takes the vector of modular factors and initializes the first partition */
1347 factor_partition(const upvec& factors_) : factors(factors_)
1353 one.resize(1, factors.front()[0].ring()->one());
1358 int operator[](size_t i) const { return k[i]; }
1359 size_t size() const { return n; }
1360 size_t size_left() const { return n-len; }
1361 size_t size_right() const { return len; }
1362 /** Initializes the next partition.
1363 Returns true, if there is one, false otherwise. */
1366 if ( last == n-1 ) {
1376 while ( k[last] == 0 ) { --last; }
1377 if ( last == 0 && n == 2*len ) return false;
1379 for ( size_t i=0; i<=len-rem; ++i ) {
1383 fill(k.begin()+last, k.end(), 0);
1390 if ( len > n/2 ) return false;
1391 fill(k.begin(), k.begin()+len, 1);
1392 fill(k.begin()+len+1, k.end(), 0);
1400 /** Get first partition */
1401 umodpoly& left() { return lr[0]; }
1402 /** Get second partition */
1403 umodpoly& right() { return lr[1]; }
1412 while ( i < n && k[i] == group ) { ++d; ++i; }
1414 if ( cache[pos].size() >= d ) {
1415 lr[group] = lr[group] * cache[pos][d-1];
1417 if ( cache[pos].size() == 0 ) {
1418 cache[pos].push_back(factors[pos] * factors[pos+1]);
1420 size_t j = pos + cache[pos].size() + 1;
1421 d -= cache[pos].size();
1423 umodpoly buf = cache[pos].back() * factors[j];
1424 cache[pos].push_back(buf);
1428 lr[group] = lr[group] * cache[pos].back();
1431 lr[group] = lr[group] * factors[pos];
1442 for ( size_t i=0; i<n; ++i ) {
1443 lr[k[i]] = lr[k[i]] * factors[i];
1449 vector<vector<umodpoly>> cache;
1458 /** Contains a pair of univariate polynomial and its modular factors.
1459 * Used by factor_univariate().
1467 /** Univariate polynomial factorization.
1469 * Modular factorization is tried for several primes to minimize the number of
1470 * modular factors. Then, Hensel lifting is performed.
1472 * @param[in] poly expanded square free univariate polynomial
1473 * @param[in] x symbol
1474 * @param[in,out] prime prime number to start trying modular factorization with,
1475 * output value is the prime number actually used
1477 static ex factor_univariate(const ex& poly, const ex& x, unsigned int& prime)
1479 ex unit, cont, prim_ex;
1480 poly.unitcontprim(x, unit, cont, prim_ex);
1482 upoly_from_ex(prim, prim_ex, x);
1483 if (prim_ex.is_equal(1)) {
1487 // determine proper prime and minimize number of modular factors
1489 unsigned int lastp = prime;
1491 unsigned int trials = 0;
1492 unsigned int minfactors = 0;
1494 const numeric& cont_n = ex_to<numeric>(cont);
1496 if (cont_n.is_integer()) {
1497 i_cont = the<cl_I>(cont_n.to_cl_N());
1499 // poly \in Q[x] => poly = q ipoly, ipoly \in Z[x], q \in Q
1500 // factor(poly) \equiv q factor(ipoly)
1503 cl_I lc = lcoeff(prim)*i_cont;
1505 while ( trials < 2 ) {
1508 prime = next_prime(prime);
1509 if ( !zerop(rem(lc, prime)) ) {
1510 R = find_modint_ring(prime);
1511 umodpoly_from_upoly(modpoly, prim, R);
1512 if ( squarefree(modpoly) ) break;
1516 // do modular factorization
1518 factor_modular(modpoly, trialfactors);
1519 if ( trialfactors.size() <= 1 ) {
1520 // irreducible for sure
1524 if ( minfactors == 0 || trialfactors.size() < minfactors ) {
1525 factors = trialfactors;
1526 minfactors = trialfactors.size();
1534 R = find_modint_ring(prime);
1536 // lift all factor combinations
1537 stack<ModFactors> tocheck;
1540 mf.factors = factors;
1544 while ( tocheck.size() ) {
1545 const size_t n = tocheck.top().factors.size();
1546 factor_partition part(tocheck.top().factors);
1548 // call Hensel lifting
1549 hensel_univar(tocheck.top().poly, prime, part.left(), part.right(), f1, f2);
1550 if ( !f1.empty() ) {
1551 // successful, update the stack and the result
1552 if ( part.size_left() == 1 ) {
1553 if ( part.size_right() == 1 ) {
1554 result *= upoly_to_ex(f1, x) * upoly_to_ex(f2, x);
1558 result *= upoly_to_ex(f1, x);
1559 tocheck.top().poly = f2;
1560 for ( size_t i=0; i<n; ++i ) {
1561 if ( part[i] == 0 ) {
1562 tocheck.top().factors.erase(tocheck.top().factors.begin()+i);
1568 else if ( part.size_right() == 1 ) {
1569 if ( part.size_left() == 1 ) {
1570 result *= upoly_to_ex(f1, x) * upoly_to_ex(f2, x);
1574 result *= upoly_to_ex(f2, x);
1575 tocheck.top().poly = f1;
1576 for ( size_t i=0; i<n; ++i ) {
1577 if ( part[i] == 1 ) {
1578 tocheck.top().factors.erase(tocheck.top().factors.begin()+i);
1584 upvec newfactors1(part.size_left()), newfactors2(part.size_right());
1585 auto i1 = newfactors1.begin(), i2 = newfactors2.begin();
1586 for ( size_t i=0; i<n; ++i ) {
1588 *i2++ = tocheck.top().factors[i];
1590 *i1++ = tocheck.top().factors[i];
1593 tocheck.top().factors = newfactors1;
1594 tocheck.top().poly = f1;
1596 mf.factors = newfactors2;
1603 if ( !part.next() ) {
1604 // if no more combinations left, return polynomial as
1606 result *= upoly_to_ex(tocheck.top().poly, x);
1614 return unit * cont * result;
1617 /** Second interface to factor_univariate() to be used if the information about
1618 * the prime is not needed.
1620 static inline ex factor_univariate(const ex& poly, const ex& x)
1623 return factor_univariate(poly, x, prime);
1626 /** Represents an evaluation point (<symbol>==<integer>).
1635 ostream& operator<<(ostream& o, const vector<EvalPoint>& v)
1637 for ( size_t i=0; i<v.size(); ++i ) {
1638 o << "(" << v[i].x << "==" << v[i].evalpoint << ") ";
1642 #endif // def DEBUGFACTOR
1644 // forward declaration
1645 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);
1647 /** Utility function for multivariate Hensel lifting.
1649 * Solves the equation
1650 * s_1*b_1 + ... + s_r*b_r == 1 mod p^k
1651 * with deg(s_i) < deg(a_i)
1652 * and with given b_1 = a_1 * ... * a_{i-1} * a_{i+1} * ... * a_r
1654 * The implementation follows algorithm 6.3 of [GCL].
1656 * @param[in] a vector of modular univariate polynomials
1657 * @param[in] x symbol
1658 * @param[in] p prime number
1659 * @param[in] k p^k is modulus
1660 * @return vector of polynomials (s_i)
1662 static upvec multiterm_eea_lift(const upvec& a, const ex& x, unsigned int p, unsigned int k)
1664 const size_t r = a.size();
1665 cl_modint_ring R = find_modint_ring(expt_pos(cl_I(p),k));
1668 for ( size_t j=r-2; j>=1; --j ) {
1669 q[j-1] = a[j] * q[j];
1671 umodpoly beta(1, R->one());
1673 for ( size_t j=1; j<r; ++j ) {
1674 vector<ex> mdarg(2);
1675 mdarg[0] = umodpoly_to_ex(q[j-1], x);
1676 mdarg[1] = umodpoly_to_ex(a[j-1], x);
1677 vector<EvalPoint> empty;
1678 vector<ex> exsigma = multivar_diophant(mdarg, x, umodpoly_to_ex(beta, x), empty, 0, p, k);
1680 umodpoly_from_ex(sigma1, exsigma[0], x, R);
1682 umodpoly_from_ex(sigma2, exsigma[1], x, R);
1684 s.push_back(sigma2);
1690 /** Changes the modulus of a modular polynomial. Used by eea_lift().
1692 * @param[in] R new modular ring
1693 * @param[in,out] a polynomial to change (in situ)
1695 static void change_modulus(const cl_modint_ring& R, umodpoly& a)
1697 if ( a.empty() ) return;
1698 cl_modint_ring oldR = a[0].ring();
1699 for (auto & i : a) {
1700 i = R->canonhom(oldR->retract(i));
1705 /** Utility function for multivariate Hensel lifting.
1707 * Solves s*a + t*b == 1 mod p^k given a,b.
1709 * The implementation follows algorithm 6.3 of [GCL].
1711 * @param[in] a polynomial
1712 * @param[in] b polynomial
1713 * @param[in] x symbol
1714 * @param[in] p prime number
1715 * @param[in] k p^k is modulus
1716 * @param[out] s_ output polynomial
1717 * @param[out] t_ output polynomial
1719 static void eea_lift(const umodpoly& a, const umodpoly& b, const ex& x, unsigned int p, unsigned int k, umodpoly& s_, umodpoly& t_)
1721 cl_modint_ring R = find_modint_ring(p);
1723 change_modulus(R, amod);
1725 change_modulus(R, bmod);
1729 exteuclid(amod, bmod, smod, tmod);
1731 cl_modint_ring Rpk = find_modint_ring(expt_pos(cl_I(p),k));
1733 change_modulus(Rpk, s);
1735 change_modulus(Rpk, t);
1738 umodpoly one(1, Rpk->one());
1739 for ( size_t j=1; j<k; ++j ) {
1740 umodpoly e = one - a * s - b * t;
1741 reduce_coeff(e, modulus);
1743 change_modulus(R, c);
1744 umodpoly sigmabar = smod * c;
1745 umodpoly taubar = tmod * c;
1747 remdiv(sigmabar, bmod, sigma, q);
1748 umodpoly tau = taubar + q * amod;
1749 umodpoly sadd = sigma;
1750 change_modulus(Rpk, sadd);
1751 cl_MI modmodulus(Rpk, modulus);
1752 s = s + sadd * modmodulus;
1753 umodpoly tadd = tau;
1754 change_modulus(Rpk, tadd);
1755 t = t + tadd * modmodulus;
1756 modulus = modulus * p;
1762 /** Utility function for multivariate Hensel lifting.
1764 * Solves the equation
1765 * s_1*b_1 + ... + s_r*b_r == x^m mod p^k
1766 * with given b_1 = a_1 * ... * a_{i-1} * a_{i+1} * ... * a_r
1768 * The implementation follows algorithm 6.3 of [GCL].
1770 * @param a vector with univariate polynomials mod p^k
1772 * @param m exponent of x^m in the equation to solve
1773 * @param p prime number
1774 * @param k p^k is modulus
1775 * @return vector of polynomials (s_i)
1777 static upvec univar_diophant(const upvec& a, const ex& x, unsigned int m, unsigned int p, unsigned int k)
1779 cl_modint_ring R = find_modint_ring(expt_pos(cl_I(p),k));
1781 const size_t r = a.size();
1784 upvec s = multiterm_eea_lift(a, x, p, k);
1785 for ( size_t j=0; j<r; ++j ) {
1786 umodpoly bmod = umodpoly_to_umodpoly(s[j], R, m);
1788 rem(bmod, a[j], buf);
1789 result.push_back(buf);
1793 eea_lift(a[1], a[0], x, p, k, s, t);
1794 umodpoly bmod = umodpoly_to_umodpoly(s, R, m);
1796 remdiv(bmod, a[0], buf, q);
1797 result.push_back(buf);
1798 umodpoly t1mod = umodpoly_to_umodpoly(t, R, m);
1799 buf = t1mod + q * a[1];
1800 result.push_back(buf);
1806 /** Map used by function make_modular().
1807 * Finds every coefficient in a polynomial and replaces it by is value in the
1808 * given modular ring R (symmetric representation).
1810 struct make_modular_map : public map_function {
1812 make_modular_map(const cl_modint_ring& R_) : R(R_) { }
1813 ex operator()(const ex& e) override
1815 if ( is_a<add>(e) || is_a<mul>(e) ) {
1816 return e.map(*this);
1818 else if ( is_a<numeric>(e) ) {
1819 numeric mod(R->modulus);
1820 numeric halfmod = (mod-1)/2;
1821 cl_MI emod = R->canonhom(the<cl_I>(ex_to<numeric>(e).to_cl_N()));
1822 numeric n(R->retract(emod));
1823 if ( n > halfmod ) {
1833 /** Helps mimicking modular multivariate polynomial arithmetic.
1835 * @param e expression of which to make the coefficients equal to their value
1836 * in the modular ring R (symmetric representation)
1837 * @param R modular ring
1838 * @return resulting expression
1840 static ex make_modular(const ex& e, const cl_modint_ring& R)
1842 make_modular_map map(R);
1843 return map(e.expand());
1846 /** Utility function for multivariate Hensel lifting.
1848 * Returns the polynomials s_i that fulfill
1849 * s_1*b_1 + ... + s_r*b_r == c mod <I^(d+1),p^k>
1850 * with given b_1 = a_1 * ... * a_{i-1} * a_{i+1} * ... * a_r
1852 * The implementation follows algorithm 6.2 of [GCL].
1854 * @param a_ vector of multivariate factors mod p^k
1855 * @param x symbol (equiv. x_1 in [GCL])
1856 * @param c polynomial mod p^k
1857 * @param I vector of evaluation points
1858 * @param d maximum total degree of result
1859 * @param p prime number
1860 * @param k p^k is modulus
1861 * @return vector of polynomials (s_i)
1863 static vector<ex> multivar_diophant(const vector<ex>& a_, const ex& x, const ex& c, const vector<EvalPoint>& I,
1864 unsigned int d, unsigned int p, unsigned int k)
1868 const cl_I modulus = expt_pos(cl_I(p),k);
1869 const cl_modint_ring R = find_modint_ring(modulus);
1870 const size_t r = a.size();
1871 const size_t nu = I.size() + 1;
1875 ex xnu = I.back().x;
1876 int alphanu = I.back().evalpoint;
1879 for ( size_t i=0; i<r; ++i ) {
1883 for ( size_t i=0; i<r; ++i ) {
1884 b[i] = normal(A / a[i]);
1887 vector<ex> anew = a;
1888 for ( size_t i=0; i<r; ++i ) {
1889 anew[i] = anew[i].subs(xnu == alphanu);
1891 ex cnew = c.subs(xnu == alphanu);
1892 vector<EvalPoint> Inew = I;
1894 sigma = multivar_diophant(anew, x, cnew, Inew, d, p, k);
1897 for ( size_t i=0; i<r; ++i ) {
1898 buf -= sigma[i] * b[i];
1900 ex e = make_modular(buf, R);
1903 for ( size_t m=1; !e.is_zero() && e.has(xnu) && m<=d; ++m ) {
1904 monomial *= (xnu - alphanu);
1905 monomial = expand(monomial);
1906 ex cm = e.diff(ex_to<symbol>(xnu), m).subs(xnu==alphanu) / factorial(m);
1907 cm = make_modular(cm, R);
1908 if ( !cm.is_zero() ) {
1909 vector<ex> delta_s = multivar_diophant(anew, x, cm, Inew, d, p, k);
1911 for ( size_t j=0; j<delta_s.size(); ++j ) {
1912 delta_s[j] *= monomial;
1913 sigma[j] += delta_s[j];
1914 buf -= delta_s[j] * b[j];
1916 e = make_modular(buf, R);
1921 for ( size_t i=0; i<a.size(); ++i ) {
1923 umodpoly_from_ex(up, a[i], x, R);
1927 sigma.insert(sigma.begin(), r, 0);
1930 if ( is_a<add>(c) ) {
1937 for ( size_t i=0; i<nterms; ++i ) {
1938 int m = z.degree(x);
1939 cl_I cm = the<cl_I>(ex_to<numeric>(z.lcoeff(x)).to_cl_N());
1940 upvec delta_s = univar_diophant(amod, x, m, p, k);
1942 cl_I poscm = plusp(cm) ? cm : mod(cm, modulus);
1943 modcm = cl_MI(R, poscm);
1944 for ( size_t j=0; j<delta_s.size(); ++j ) {
1945 delta_s[j] = delta_s[j] * modcm;
1946 sigma[j] = sigma[j] + umodpoly_to_ex(delta_s[j], x);
1948 if ( nterms > 1 && i+1 != nterms ) {
1954 for ( size_t i=0; i<sigma.size(); ++i ) {
1955 sigma[i] = make_modular(sigma[i], R);
1961 /** Multivariate Hensel lifting.
1962 * The implementation follows algorithm 6.4 of [GCL].
1963 * Since we don't have a data type for modular multivariate polynomials, the
1964 * respective operations are done in a GiNaC::ex and the function
1965 * make_modular() is then called to make the coefficient modular p^l.
1967 * @param a multivariate polynomial primitive in x
1968 * @param x symbol (equiv. x_1 in [GCL])
1969 * @param I vector of evaluation points (x_2==a_2,x_3==a_3,...)
1970 * @param p prime number (should not divide lcoeff(a mod I))
1971 * @param l p^l is the modulus of the lifted univariate field
1972 * @param u vector of modular (mod p^l) factors of a mod I
1973 * @param lcU correct leading coefficient of the univariate factors of a mod I
1974 * @return list GiNaC::lst with lifted factors (multivariate factors of a),
1975 * empty if Hensel lifting did not succeed
1977 static ex hensel_multivar(const ex& a, const ex& x, const vector<EvalPoint>& I,
1978 unsigned int p, const cl_I& l, const upvec& u, const vector<ex>& lcU)
1980 const size_t nu = I.size() + 1;
1981 const cl_modint_ring R = find_modint_ring(expt_pos(cl_I(p),l));
1986 for ( size_t j=nu; j>=2; --j ) {
1988 int alpha = I[j-2].evalpoint;
1989 A[j-2] = A[j-1].subs(x==alpha);
1990 A[j-2] = make_modular(A[j-2], R);
1993 int maxdeg = a.degree(I.front().x);
1994 for ( size_t i=1; i<I.size(); ++i ) {
1995 int maxdeg2 = a.degree(I[i].x);
1996 if ( maxdeg2 > maxdeg ) maxdeg = maxdeg2;
1999 const size_t n = u.size();
2001 for ( size_t i=0; i<n; ++i ) {
2002 U[i] = umodpoly_to_ex(u[i], x);
2005 for ( size_t j=2; j<=nu; ++j ) {
2008 for ( size_t m=0; m<n; ++m) {
2009 if ( lcU[m] != 1 ) {
2011 for ( size_t i=j-1; i<nu-1; ++i ) {
2012 coef = coef.subs(I[i].x == I[i].evalpoint);
2014 coef = make_modular(coef, R);
2015 int deg = U[m].degree(x);
2016 U[m] = U[m] - U[m].lcoeff(x) * pow(x,deg) + coef * pow(x,deg);
2020 for ( size_t i=0; i<n; ++i ) {
2023 ex e = expand(A[j-1] - Uprod);
2025 vector<EvalPoint> newI;
2026 for ( size_t i=1; i<=j-2; ++i ) {
2027 newI.push_back(I[i-1]);
2031 int alphaj = I[j-2].evalpoint;
2032 size_t deg = A[j-1].degree(xj);
2033 for ( size_t k=1; k<=deg; ++k ) {
2034 if ( !e.is_zero() ) {
2035 monomial *= (xj - alphaj);
2036 monomial = expand(monomial);
2037 ex dif = e.diff(ex_to<symbol>(xj), k);
2038 ex c = dif.subs(xj==alphaj) / factorial(k);
2039 if ( !c.is_zero() ) {
2040 vector<ex> deltaU = multivar_diophant(U1, x, c, newI, maxdeg, p, cl_I_to_uint(l));
2041 for ( size_t i=0; i<n; ++i ) {
2042 deltaU[i] *= monomial;
2044 U[i] = make_modular(U[i], R);
2047 for ( size_t i=0; i<n; ++i ) {
2051 e = make_modular(e, R);
2058 for ( size_t i=0; i<U.size(); ++i ) {
2061 if ( expand(a-acand).is_zero() ) {
2062 return lst(U.begin(), U.end());
2068 /** Takes a factorized expression and puts the factors in a vector. The exponents
2069 * of the factors are discarded, e.g. 7*x^2*(y+1)^4 --> {7,x,y+1}. The first
2070 * element of the result is always the numeric coefficient.
2072 static exvector put_factors_into_vec(const ex& e)
2075 if ( is_a<numeric>(e) ) {
2076 result.push_back(e);
2079 if ( is_a<power>(e) ) {
2080 result.push_back(1);
2081 result.push_back(e.op(0));
2084 if ( is_a<symbol>(e) || is_a<add>(e) ) {
2085 ex icont(e.integer_content());
2086 result.push_back(icont);
2087 result.push_back(e/icont);
2090 if ( is_a<mul>(e) ) {
2092 result.push_back(nfac);
2093 for ( size_t i=0; i<e.nops(); ++i ) {
2095 if ( is_a<numeric>(op) ) {
2098 if ( is_a<power>(op) ) {
2099 result.push_back(op.op(0));
2101 if ( is_a<symbol>(op) || is_a<add>(op) ) {
2102 result.push_back(op);
2108 throw runtime_error("put_factors_into_vec: bad term.");
2111 /** Checks a set of numbers for whether each number has a unique prime factor.
2113 * @param[in] f numbers to check
2114 * @return true: if number set is bad, false: if set is okay (has unique
2117 static bool checkdivisors(const exvector& f)
2119 const int k = f.size();
2121 vector<numeric> d(k);
2122 d[0] = ex_to<numeric>(abs(f[0]));
2123 for ( int i=1; i<k; ++i ) {
2124 q = ex_to<numeric>(abs(f[i]));
2125 for ( int j=i-1; j>=0; --j ) {
2140 /** Generates a set of evaluation points for a multivariate polynomial.
2141 * The set fulfills the following conditions:
2142 * 1. lcoeff(evaluated_polynomial) does not vanish
2143 * 2. factors of lcoeff(evaluated_polynomial) have each a unique prime factor
2144 * 3. evaluated_polynomial is square free
2145 * See [Wan] for more details.
2147 * @param[in] u multivariate polynomial to be factored
2148 * @param[in] vn leading coefficient of u in x (x==first symbol in syms)
2149 * @param[in] x first symbol that appears in u
2150 * @param[in] syms_wox remaining symbols that appear in u
2151 * @param[in] f vector containing the factors of the leading coefficient vn
2152 * @param[in,out] modulus integer modulus for random number generation (i.e. |a_i| < modulus)
2153 * @param[out] u0 returns the evaluated (univariate) polynomial
2154 * @param[out] a returns the valid evaluation points. must have initial size equal
2155 * number of symbols-1 before calling generate_set
2157 static void generate_set(const ex& u, const ex& vn, const ex& x, const exset& syms_wox, const exvector& f,
2158 numeric& modulus, ex& u0, vector<numeric>& a)
2162 // generate a set of integers ...
2166 auto s = syms_wox.begin();
2167 for ( size_t i=0; i<a.size(); ++i ) {
2169 a[i] = mod(numeric(rand()), 2*modulus) - modulus;
2170 vnatry = vna.subs(*s == a[i]);
2171 // ... for which the leading coefficient doesn't vanish ...
2172 } while ( vnatry == 0 );
2174 u0 = u0.subs(*s == a[i]);
2177 // ... for which u0 is square free ...
2178 ex g = gcd(u0, u0.diff(ex_to<symbol>(x)));
2179 if ( !is_a<numeric>(g) ) {
2182 if ( !is_a<numeric>(vn) ) {
2183 // ... and for which the evaluated factors have each an unique prime factor
2185 fnum[0] = fnum[0] * u0.content(x);
2186 for ( size_t i=1; i<fnum.size(); ++i ) {
2187 if ( !is_a<numeric>(fnum[i]) ) {
2188 s = syms_wox.begin();
2189 for ( size_t j=0; j<a.size(); ++j, ++s ) {
2190 fnum[i] = fnum[i].subs(*s == a[j]);
2194 if ( checkdivisors(fnum) ) {
2198 // ok, we have a valid set now
2203 // forward declaration
2204 static ex factor_sqrfree(const ex& poly);
2206 /** Used by factor_multivariate().
2208 struct factorization_ctx {
2209 const ex poly, x; // polynomial, first symbol x...
2210 const exset syms_wox; // ...remaining symbols w/o x
2211 ex unit, cont, pp; // unit * cont * pp == poly
2212 ex vn; exvector vnlst; // leading coeff, factors of leading coeff
2213 numeric modulus; // incremented each time we try
2214 /** returns factors or empty if it did not succeed */
2215 ex try_next_evaluation_homomorphism()
2217 constexpr unsigned maxtrials = 3;
2218 vector<numeric> a(syms_wox.size(), 0);
2220 unsigned int trialcount = 0;
2222 int factor_count = 0;
2223 int min_factor_count = -1;
2228 // try several evaluation points to reduce the number of factors
2229 while ( trialcount < maxtrials ) {
2231 // generate a set of valid evaluation points
2232 generate_set(pp, vn, x, syms_wox, vnlst, modulus, u, a);
2234 ufac = factor_univariate(u, x, prime);
2235 ufaclst = put_factors_into_vec(ufac);
2236 factor_count = ufaclst.size()-1;
2239 if ( factor_count <= 1 ) {
2243 if ( min_factor_count < 0 ) {
2245 min_factor_count = factor_count;
2247 else if ( min_factor_count == factor_count ) {
2251 else if ( min_factor_count > factor_count ) {
2252 // new minimum, reset trial counter
2253 min_factor_count = factor_count;
2258 // determine true leading coefficients for the Hensel lifting
2259 vector<ex> C(factor_count);
2260 if ( is_a<numeric>(vn) ) {
2262 for ( size_t i=1; i<ufaclst.size(); ++i ) {
2263 C[i-1] = ufaclst[i].lcoeff(x);
2267 // we use the property of the ftilde having a unique prime factor.
2268 // details can be found in [Wan].
2270 vector<numeric> ftilde(vnlst.size()-1);
2271 for ( size_t i=0; i<ftilde.size(); ++i ) {
2273 auto s = syms_wox.begin();
2274 for ( size_t j=0; j<a.size(); ++j ) {
2275 ft = ft.subs(*s == a[j]);
2278 ftilde[i] = ex_to<numeric>(ft);
2280 // calculate D and C
2281 vector<bool> used_flag(ftilde.size(), false);
2282 vector<ex> D(factor_count, 1);
2284 for ( int i=0; i<factor_count; ++i ) {
2285 numeric prefac = ex_to<numeric>(ufaclst[i+1].lcoeff(x));
2286 for ( int j=ftilde.size()-1; j>=0; --j ) {
2288 while ( irem(prefac, ftilde[j]) == 0 ) {
2289 prefac = iquo(prefac, ftilde[j]);
2293 used_flag[j] = true;
2294 D[i] = D[i] * pow(vnlst[j+1], count);
2297 C[i] = D[i] * prefac;
2300 for ( int i=0; i<factor_count; ++i ) {
2301 numeric prefac = ex_to<numeric>(ufaclst[i+1].lcoeff(x));
2302 for ( int j=ftilde.size()-1; j>=0; --j ) {
2304 while ( irem(prefac, ftilde[j]) == 0 ) {
2305 prefac = iquo(prefac, ftilde[j]);
2308 while ( irem(ex_to<numeric>(delta)*prefac, ftilde[j]) == 0 ) {
2309 numeric g = gcd(prefac, ex_to<numeric>(ftilde[j]));
2310 prefac = iquo(prefac, g);
2311 delta = delta / (ftilde[j]/g);
2312 ufaclst[i+1] = ufaclst[i+1] * (ftilde[j]/g);
2316 used_flag[j] = true;
2317 D[i] = D[i] * pow(vnlst[j+1], count);
2320 C[i] = D[i] * prefac;
2323 // check if something went wrong
2324 bool some_factor_unused = false;
2325 for ( size_t i=0; i<used_flag.size(); ++i ) {
2326 if ( !used_flag[i] ) {
2327 some_factor_unused = true;
2331 if ( some_factor_unused ) {
2332 return lst{}; // next try
2336 // multiply the remaining content of the univariate polynomial into the
2339 C[0] = C[0] * delta;
2340 ufaclst[1] = ufaclst[1] * delta;
2343 // set up evaluation points
2345 vector<EvalPoint> epv;
2346 auto s = syms_wox.begin();
2347 for ( size_t i=0; i<a.size(); ++i ) {
2349 ep.evalpoint = a[i].to_int();
2355 for ( int i=1; i<=factor_count; ++i ) {
2356 if ( ufaclst[i].degree(x) > maxdeg ) {
2357 maxdeg = ufaclst[i].degree(x);
2360 cl_I B = calc_bound(u, x) * ash(cl_I(1), maxdeg+1); // 2 * calc_bound(u,x) * 2^maxdeg
2368 // set up modular factors (mod p^l)
2369 cl_modint_ring R = find_modint_ring(pl);
2370 upvec modfactors(ufaclst.size()-1);
2371 for ( size_t i=1; i<ufaclst.size(); ++i ) {
2372 umodpoly_from_ex(modfactors[i-1], ufaclst[i], x, R);
2375 // try Hensel lifting
2376 return hensel_multivar(pp, x, epv, prime, l, modfactors, C);
2380 /** Multivariate factorization.
2382 * The implementation is based on the algorithm described in [Wan].
2383 * An evaluation homomorphism (a set of integers) is determined that fulfills
2384 * certain criteria. The evaluated polynomial is univariate and is factorized
2385 * by factor_univariate(). The main work then is to find the correct leading
2386 * coefficients of the univariate factors. They have to correspond to the
2387 * factors of the (multivariate) leading coefficient of the input polynomial
2388 * (as defined for a specific variable x). After that the Hensel lifting can be
2389 * performed. This is done in round-robin for each x in syms until success.
2391 * @param[in] poly expanded, square free polynomial
2392 * @param[in] syms contains the symbols in the polynomial
2393 * @return factorized polynomial
2395 static ex factor_multivariate(const ex& poly, const exset& syms)
2397 // set up one factorization context for each symbol
2398 vector<factorization_ctx> ctx_in_x;
2399 for (auto x : syms) {
2400 exset syms_wox; // remaining syms w/o x
2401 copy_if(syms.begin(), syms.end(),
2402 inserter(syms_wox, syms_wox.end()), [x](const ex& y){ return y != x; });
2404 factorization_ctx ctx = {.poly = poly, .x = x,
2405 .syms_wox = syms_wox};
2407 // make polynomial primitive
2408 poly.unitcontprim(x, ctx.unit, ctx.cont, ctx.pp);
2409 if ( !is_a<numeric>(ctx.cont) ) {
2410 // content is a polynomial in one or more of remaining syms, let's start over
2411 return ctx.unit * factor_sqrfree(ctx.cont) * factor_sqrfree(ctx.pp);
2414 // find factors of leading coefficient
2415 ctx.vn = ctx.pp.collect(x).lcoeff(x);
2416 ctx.vnlst = put_factors_into_vec(factor(ctx.vn));
2418 ctx.modulus = (ctx.vnlst.size() > 3) ? ctx.vnlst.size() : 3;
2420 ctx_in_x.push_back(ctx);
2423 // try an evaluation homomorphism for each context in round-robin
2424 auto ctx = ctx_in_x.begin();
2427 ex res = ctx->try_next_evaluation_homomorphism();
2429 if ( res != lst{} ) {
2430 // found the factors
2431 ex result = ctx->cont * ctx->unit;
2432 for ( size_t i=0; i<res.nops(); ++i ) {
2434 res.op(i).unitcontprim(ctx->x, unit, cont, pp);
2435 result *= unit * cont * pp;
2440 // switch context for next symbol
2441 if (++ctx == ctx_in_x.end()) {
2442 ctx = ctx_in_x.begin();
2447 /** Finds all symbols in an expression. Used by factor_sqrfree() and factor().
2449 struct find_symbols_map : public map_function {
2451 ex operator()(const ex& e) override
2453 if ( is_a<symbol>(e) ) {
2457 return e.map(*this);
2461 /** Factorizes a polynomial that is square free. It calls either the univariate
2462 * or the multivariate factorization functions.
2464 static ex factor_sqrfree(const ex& poly)
2466 // determine all symbols in poly
2467 find_symbols_map findsymbols;
2469 if ( findsymbols.syms.size() == 0 ) {
2473 if ( findsymbols.syms.size() == 1 ) {
2475 const ex& x = *(findsymbols.syms.begin());
2476 int ld = poly.ldegree(x);
2478 // pull out direct factors
2479 ex res = factor_univariate(expand(poly/pow(x, ld)), x);
2480 return res * pow(x,ld);
2482 ex res = factor_univariate(poly, x);
2487 // multivariate case
2488 ex res = factor_multivariate(poly, findsymbols.syms);
2492 /** Map used by factor() when factor_options::all is given to access all
2493 * subexpressions and to call factor() on them.
2495 struct apply_factor_map : public map_function {
2497 apply_factor_map(unsigned options_) : options(options_) { }
2498 ex operator()(const ex& e) override
2500 if ( e.info(info_flags::polynomial) ) {
2501 return factor(e, options);
2503 if ( is_a<add>(e) ) {
2505 for ( size_t i=0; i<e.nops(); ++i ) {
2506 if ( e.op(i).info(info_flags::polynomial) ) {
2512 return factor(s1, options) + s2.map(*this);
2514 return e.map(*this);
2518 /** Iterate through explicit factors of e, call yield(f, k) for
2519 * each factor of the form f^k.
2521 * Note that this function doesn't factor e itself, it only
2522 * iterates through the factors already explicitly present.
2524 template <typename F> void
2525 factor_iter(const ex &e, F yield)
2528 for (const auto &f : e) {
2529 if (is_a<power>(f)) {
2530 yield(f.op(0), f.op(1));
2536 if (is_a<power>(e)) {
2537 yield(e.op(0), e.op(1));
2544 /** This function factorizes a polynomial. It checks the arguments,
2545 * tries a square free factorization, and then calls factor_sqrfree
2546 * to do the hard work.
2548 * This function expands its argument, so for polynomials with
2549 * explicit factors it's better to call it on each one separately
2550 * (or use factor() which does just that).
2552 static ex factor1(const ex& poly, unsigned options)
2555 if ( !poly.info(info_flags::polynomial) ) {
2556 if ( options & factor_options::all ) {
2557 options &= ~factor_options::all;
2558 apply_factor_map factor_map(options);
2559 return factor_map(poly);
2564 // determine all symbols in poly
2565 find_symbols_map findsymbols;
2567 if ( findsymbols.syms.size() == 0 ) {
2571 for (auto & i : findsymbols.syms ) {
2575 // make poly square free
2576 ex sfpoly = sqrfree(poly.expand(), syms);
2578 // factorize the square free components
2581 [&](const ex &f, const ex &k) {
2582 if ( is_a<add>(f) ) {
2583 res *= pow(factor_sqrfree(f), k);
2585 // simple case: (monomial)^exponent
2592 } // anonymous namespace
2594 /** Interface function to the outside world. It uses factor1()
2595 * on each of the explicitly present factors of poly.
2597 ex factor(const ex& poly, unsigned options)
2601 [&](const ex &f1, const ex &k1) {
2602 factor_iter(factor1(f1, options),
2603 [&](const ex &f2, const ex &k2) {
2604 result *= pow(f2, k1*k2);
2610 } // namespace GiNaC