[24b338] | 1 | /*****************************************************************************\ |
---|
[806c18] | 2 | * Computer Algebra System SINGULAR |
---|
[24b338] | 3 | \*****************************************************************************/ |
---|
| 4 | /** @file facFqSquarefree.cc |
---|
[806c18] | 5 | * |
---|
[24b338] | 6 | * This file provides functions for squarefrees factorizing over |
---|
[806c18] | 7 | * \f$ F_{p} \f$ , \f$ F_{p}(\alpha ) \f$ or GF, as decribed in "Factoring |
---|
| 8 | * multivariate polynomials over a finite field" by L. Bernardin |
---|
[24b338] | 9 | * |
---|
| 10 | * @author Martin Lee |
---|
| 11 | * |
---|
| 12 | * @internal @version \$Id$ |
---|
| 13 | * |
---|
| 14 | **/ |
---|
| 15 | /*****************************************************************************/ |
---|
| 16 | |
---|
[963057] | 17 | #include <config.h> |
---|
| 18 | |
---|
[24b338] | 19 | #include "canonicalform.h" |
---|
| 20 | |
---|
| 21 | #include "cf_gcd_smallp.h" |
---|
| 22 | #include "cf_iter.h" |
---|
| 23 | #include "cf_map.h" |
---|
| 24 | #include "cf_util.h" |
---|
[fecc08] | 25 | #include "cf_factory.h" |
---|
[24b338] | 26 | #include "facFqSquarefree.h" |
---|
| 27 | |
---|
[806c18] | 28 | static inline |
---|
| 29 | CanonicalForm |
---|
| 30 | pthRoot (const CanonicalForm & F, const int & q) |
---|
[24b338] | 31 | { |
---|
| 32 | CanonicalForm A= F; |
---|
| 33 | int p= getCharacteristic (); |
---|
[806c18] | 34 | if (A.inCoeffDomain()) |
---|
[24b338] | 35 | { |
---|
| 36 | A= power (A, q/p); |
---|
| 37 | return A; |
---|
| 38 | } |
---|
[806c18] | 39 | else |
---|
[24b338] | 40 | { |
---|
| 41 | CanonicalForm buf= 0; |
---|
[806c18] | 42 | for (CFIterator i= A; i.hasTerms(); i++) |
---|
[24b338] | 43 | buf= buf + power(A.mvar(), i.exp()/p)*pthRoot (i.coeff(), q); |
---|
| 44 | return buf; |
---|
| 45 | } |
---|
| 46 | } |
---|
| 47 | |
---|
| 48 | CanonicalForm |
---|
| 49 | maxpthRoot (const CanonicalForm & F, const int & q, int& l) |
---|
| 50 | { |
---|
| 51 | CanonicalForm result= F; |
---|
| 52 | bool derivZero= true; |
---|
| 53 | l= 0; |
---|
| 54 | while (derivZero) |
---|
| 55 | { |
---|
| 56 | for (int i= 1; i <= result.level(); i++) |
---|
| 57 | { |
---|
| 58 | if (!deriv (result, Variable (i)).isZero()) |
---|
| 59 | { |
---|
| 60 | derivZero= false; |
---|
| 61 | break; |
---|
| 62 | } |
---|
| 63 | } |
---|
| 64 | if (!derivZero) |
---|
| 65 | break; |
---|
| 66 | result= pthRoot (result, q); |
---|
| 67 | l++; |
---|
| 68 | } |
---|
| 69 | return result; |
---|
[806c18] | 70 | } |
---|
[24b338] | 71 | |
---|
[806c18] | 72 | static inline |
---|
| 73 | CFFList |
---|
| 74 | sqrfPosDer (const CanonicalForm & F, const Variable & x, const int & k, |
---|
| 75 | const Variable & alpha, CanonicalForm & c) |
---|
| 76 | { |
---|
[24b338] | 77 | Variable buf= alpha; |
---|
| 78 | CanonicalForm b= deriv (F, x); |
---|
[04cdf06] | 79 | c= gcd (F, b); |
---|
[24b338] | 80 | CanonicalForm w= F/c; |
---|
| 81 | CanonicalForm v= b/c; |
---|
| 82 | CanonicalForm u= v - deriv (w, x); |
---|
| 83 | int j= 1; |
---|
| 84 | int p= getCharacteristic(); |
---|
| 85 | CanonicalForm g; |
---|
| 86 | CFFList result; |
---|
[806c18] | 87 | while (j < p - 1 && degree(u) >= 0) |
---|
[24b338] | 88 | { |
---|
[04cdf06] | 89 | g= gcd (w, u); |
---|
[806c18] | 90 | if (degree(g) > 0) |
---|
[24b338] | 91 | result.append (CFFactor (g, j)); |
---|
| 92 | w= w/g; |
---|
| 93 | c= c/w; |
---|
| 94 | v= u/g; |
---|
| 95 | u= v - deriv (w, x); |
---|
| 96 | j++; |
---|
| 97 | } |
---|
| 98 | if (degree(w) > 0) |
---|
| 99 | result.append (CFFactor (w, j)); |
---|
| 100 | return result; |
---|
| 101 | } |
---|
| 102 | |
---|
[806c18] | 103 | CFFList |
---|
| 104 | squarefreeFactorization (const CanonicalForm & F, const Variable & alpha) |
---|
[24b338] | 105 | { |
---|
| 106 | int p= getCharacteristic(); |
---|
| 107 | CanonicalForm A= F; |
---|
| 108 | CFMap M; |
---|
| 109 | A= compress (A, M); |
---|
| 110 | Variable x= A.mvar(); |
---|
| 111 | int l= x.level(); |
---|
| 112 | int k; |
---|
| 113 | bool GF= false; |
---|
[806c18] | 114 | if (CFFactory::gettype() == GaloisFieldDomain) |
---|
[24b338] | 115 | { |
---|
| 116 | k= getGFDegree(); |
---|
| 117 | GF= true; |
---|
| 118 | } |
---|
[1372ae] | 119 | if (alpha.level() != 1) |
---|
[24b338] | 120 | k= degree (getMipo (alpha)); |
---|
[806c18] | 121 | else |
---|
[24b338] | 122 | k= 1; |
---|
| 123 | Variable buf, buf2; |
---|
| 124 | CanonicalForm tmp; |
---|
| 125 | |
---|
| 126 | CFFList tmp1, tmp2; |
---|
| 127 | bool found; |
---|
[806c18] | 128 | for (int i= l; i > 0; i--) |
---|
[24b338] | 129 | { |
---|
| 130 | buf= Variable (i); |
---|
[806c18] | 131 | if (degree (deriv (A, buf)) >= 0) |
---|
[24b338] | 132 | { |
---|
| 133 | if (GF) |
---|
| 134 | tmp1= sqrfPosDer (A, buf, k, alpha, tmp); |
---|
[1372ae] | 135 | else if (GF == false && alpha.level() != 1) |
---|
[24b338] | 136 | tmp1= sqrfPosDer (A, buf, k, alpha, tmp); |
---|
[806c18] | 137 | else |
---|
[24b338] | 138 | tmp1= sqrfPosDer (A, buf, 1, alpha, tmp); |
---|
[806c18] | 139 | A= tmp; |
---|
| 140 | for (CFFListIterator j= tmp1; j.hasItem(); j++) |
---|
[24b338] | 141 | { |
---|
| 142 | found= false; |
---|
| 143 | CFFListIterator k= tmp2; |
---|
| 144 | if (!k.hasItem()) tmp2.append (j.getItem()); |
---|
[806c18] | 145 | else |
---|
| 146 | { |
---|
| 147 | for (; k.hasItem(); k++) |
---|
[24b338] | 148 | { |
---|
[806c18] | 149 | if (k.getItem().exp() == j.getItem().exp()) |
---|
[24b338] | 150 | { |
---|
[806c18] | 151 | k.getItem()= CFFactor (k.getItem().factor()*j.getItem().factor(), |
---|
[24b338] | 152 | j.getItem().exp()); |
---|
| 153 | found= true; |
---|
| 154 | } |
---|
| 155 | } |
---|
| 156 | if (found == false) |
---|
| 157 | tmp2.append(j.getItem()); |
---|
| 158 | } |
---|
| 159 | } |
---|
| 160 | } |
---|
| 161 | } |
---|
| 162 | |
---|
| 163 | bool degcheck= false;; |
---|
| 164 | for (int i= l; i > 0; i--) |
---|
| 165 | if (degree (A, Variable(i)) >= p) |
---|
| 166 | degcheck= true; |
---|
| 167 | |
---|
| 168 | if (degcheck == false && tmp1.isEmpty() && tmp2.isEmpty()) |
---|
| 169 | return CFFList (CFFactor (F/Lc(F), 1)); |
---|
| 170 | |
---|
| 171 | CanonicalForm buffer= pthRoot (A, ipower (p, k)); |
---|
| 172 | |
---|
| 173 | tmp1= squarefreeFactorization (buffer, alpha); |
---|
| 174 | |
---|
[806c18] | 175 | CFFList result; |
---|
[24b338] | 176 | buf= alpha; |
---|
[806c18] | 177 | for (CFFListIterator i= tmp2; i.hasItem(); i++) |
---|
[24b338] | 178 | { |
---|
[806c18] | 179 | for (CFFListIterator j= tmp1; j.hasItem(); j++) |
---|
[24b338] | 180 | { |
---|
[04cdf06] | 181 | tmp= gcd (i.getItem().factor(), j.getItem().factor()); |
---|
[24b338] | 182 | i.getItem()= CFFactor (i.getItem().factor()/tmp, i.getItem().exp()); |
---|
| 183 | j.getItem()= CFFactor (j.getItem().factor()/tmp, j.getItem().exp()); |
---|
| 184 | if (degree (tmp) > 0 && tmp.level() > 0) |
---|
[806c18] | 185 | result.append (CFFactor (M (tmp), |
---|
| 186 | j.getItem().exp()*p + i.getItem().exp())); |
---|
[24b338] | 187 | } |
---|
| 188 | } |
---|
| 189 | for (CFFListIterator i= tmp2; i.hasItem(); i++) |
---|
| 190 | if (degree (i.getItem().factor()) > 0 && i.getItem().factor().level() >= 0) |
---|
| 191 | result.append (CFFactor (M (i.getItem().factor()), i.getItem().exp())); |
---|
| 192 | for (CFFListIterator j= tmp1; j.hasItem(); j++) |
---|
| 193 | if (degree (j.getItem().factor()) > 0 && j.getItem().factor().level() >= 0) |
---|
| 194 | result.append (CFFactor (M (j.getItem().factor()), j.getItem().exp()*p)); |
---|
[806c18] | 195 | return result; |
---|
[24b338] | 196 | } |
---|
| 197 | |
---|
[806c18] | 198 | CanonicalForm |
---|
| 199 | sqrfPart (const CanonicalForm& F, CanonicalForm& pthPower, |
---|
[24b338] | 200 | const Variable& alpha) |
---|
| 201 | { |
---|
| 202 | if (F.inCoeffDomain()) |
---|
| 203 | { |
---|
| 204 | pthPower= 1; |
---|
| 205 | return F; |
---|
| 206 | } |
---|
| 207 | CFMap M; |
---|
| 208 | CanonicalForm A= compress (F, M); |
---|
| 209 | Variable vBuf= alpha; |
---|
| 210 | CanonicalForm w, v, b; |
---|
| 211 | pthPower= 1; |
---|
| 212 | CanonicalForm result; |
---|
| 213 | int i= 1; |
---|
| 214 | bool allZero= true; |
---|
| 215 | for (; i <= A.level(); i++) |
---|
| 216 | { |
---|
| 217 | if (!deriv (A, Variable (i)).isZero()) |
---|
| 218 | { |
---|
| 219 | allZero= false; |
---|
| 220 | break; |
---|
[806c18] | 221 | } |
---|
[24b338] | 222 | } |
---|
| 223 | if (allZero) |
---|
| 224 | { |
---|
| 225 | pthPower= F; |
---|
| 226 | return 1; |
---|
| 227 | } |
---|
[04cdf06] | 228 | w= gcd (A, deriv (A, Variable (i))); |
---|
[24b338] | 229 | |
---|
| 230 | b= A/w; |
---|
| 231 | result= b; |
---|
| 232 | if (degree (w) < 1) |
---|
| 233 | return M (result); |
---|
| 234 | i++; |
---|
| 235 | for (; i <= A.level(); i++) |
---|
| 236 | { |
---|
| 237 | if (!deriv (w, Variable (i)).isZero()) |
---|
| 238 | { |
---|
| 239 | b= w; |
---|
[04cdf06] | 240 | w= gcd (w, deriv (w, Variable (i))); |
---|
[24b338] | 241 | b /= w; |
---|
| 242 | if (degree (b) < 1) |
---|
| 243 | break; |
---|
| 244 | CanonicalForm g; |
---|
[04cdf06] | 245 | g= gcd (b, result); |
---|
[24b338] | 246 | if (degree (g) > 0) |
---|
| 247 | result *= b/g; |
---|
| 248 | if (degree (g) <= 0) |
---|
| 249 | result *= b; |
---|
| 250 | } |
---|
| 251 | } |
---|
| 252 | result= M (result); |
---|
| 253 | return result; |
---|
| 254 | } |
---|
| 255 | |
---|