[7bf145] | 1 | /*****************************************************************************\ |
---|
[806c18] | 2 | * Computer Algebra System SINGULAR |
---|
[7bf145] | 3 | \*****************************************************************************/ |
---|
| 4 | /** @file facFqFactorizeUtil.cc |
---|
[806c18] | 5 | * |
---|
[7bf145] | 6 | * This file provides utility functions for multivariate factorization |
---|
| 7 | * |
---|
| 8 | * @author Martin Lee |
---|
| 9 | * |
---|
| 10 | **/ |
---|
| 11 | /*****************************************************************************/ |
---|
| 12 | |
---|
[e4fe2b] | 13 | #include "config.h" |
---|
[7bf145] | 14 | |
---|
| 15 | #include "canonicalform.h" |
---|
| 16 | #include "cf_map.h" |
---|
[8336c9] | 17 | #include "cf_algorithm.h" |
---|
[7bf145] | 18 | |
---|
[806c18] | 19 | static inline |
---|
[7bf145] | 20 | void appendSwap (CFList& factors1, const CFList& factors2, const int |
---|
[806c18] | 21 | swapLevel1, const int swapLevel2, const Variable& x) |
---|
[7bf145] | 22 | { |
---|
[806c18] | 23 | for (CFListIterator i= factors2; i.hasItem(); i++) |
---|
| 24 | { |
---|
| 25 | if (swapLevel1) |
---|
[7bf145] | 26 | { |
---|
| 27 | if (swapLevel2) |
---|
[806c18] | 28 | factors1.append (swapvar (swapvar (i.getItem(), x, |
---|
[7bf145] | 29 | Variable (swapLevel2)), Variable (swapLevel1), x)); |
---|
| 30 | else |
---|
[806c18] | 31 | factors1.append (swapvar (i.getItem(), Variable (swapLevel1), x)); |
---|
[7bf145] | 32 | } |
---|
[806c18] | 33 | else |
---|
[7bf145] | 34 | { |
---|
| 35 | if (swapLevel2) |
---|
[806c18] | 36 | factors1.append (swapvar (i.getItem(), x, Variable (swapLevel2))); |
---|
| 37 | else |
---|
| 38 | factors1.append (i.getItem()); |
---|
[7bf145] | 39 | } |
---|
| 40 | } |
---|
| 41 | return; |
---|
| 42 | } |
---|
| 43 | |
---|
[d52c12] | 44 | |
---|
[7bf145] | 45 | void swap (CFList& factors, const int swapLevel1, const int swapLevel2, const |
---|
[806c18] | 46 | Variable& x) |
---|
[7bf145] | 47 | { |
---|
[806c18] | 48 | for (CFListIterator i= factors; i.hasItem(); i++) |
---|
| 49 | { |
---|
| 50 | if (swapLevel1) |
---|
[7bf145] | 51 | { |
---|
| 52 | if (swapLevel2) |
---|
[806c18] | 53 | i.getItem()= swapvar (swapvar (i.getItem(), x, Variable (swapLevel2)), |
---|
[7bf145] | 54 | Variable (swapLevel1), x); |
---|
| 55 | else |
---|
[806c18] | 56 | i.getItem()= swapvar (i.getItem(), Variable (swapLevel1), x); |
---|
[7bf145] | 57 | } |
---|
[806c18] | 58 | else |
---|
[7bf145] | 59 | { |
---|
| 60 | if (swapLevel2) |
---|
[806c18] | 61 | i.getItem()= swapvar (i.getItem(), x, Variable (swapLevel2)); |
---|
[7bf145] | 62 | } |
---|
| 63 | } |
---|
| 64 | return; |
---|
| 65 | } |
---|
| 66 | |
---|
[806c18] | 67 | void appendSwapDecompress (CFList& factors1, const CFList& factors2, |
---|
[7bf145] | 68 | const CFMap& N, const int swapLevel, const |
---|
[806c18] | 69 | Variable& x) |
---|
[7bf145] | 70 | { |
---|
[806c18] | 71 | for (CFListIterator i= factors1; i.hasItem(); i++) |
---|
[7bf145] | 72 | { |
---|
[806c18] | 73 | if (swapLevel) |
---|
[7bf145] | 74 | i.getItem()= swapvar (i.getItem(), Variable (swapLevel), x); |
---|
| 75 | i.getItem()= N(i.getItem()); |
---|
| 76 | } |
---|
[806c18] | 77 | for (CFListIterator i= factors2; i.hasItem(); i++) |
---|
[7bf145] | 78 | { |
---|
[806c18] | 79 | if (!i.getItem().inCoeffDomain()) |
---|
[7bf145] | 80 | factors1.append (N (i.getItem())); |
---|
| 81 | } |
---|
| 82 | return; |
---|
| 83 | } |
---|
| 84 | |
---|
[806c18] | 85 | void appendSwapDecompress (CFList& factors1, const CFList& factors2, |
---|
| 86 | const CFMap& N, const int swapLevel1, |
---|
| 87 | const int swapLevel2, const Variable& x) |
---|
[7bf145] | 88 | { |
---|
[806c18] | 89 | for (CFListIterator i= factors1; i.hasItem(); i++) |
---|
[7bf145] | 90 | { |
---|
[806c18] | 91 | if (swapLevel1) |
---|
[7bf145] | 92 | { |
---|
[806c18] | 93 | if (swapLevel2) |
---|
[7bf145] | 94 | i.getItem()= |
---|
| 95 | N (swapvar (swapvar (i.getItem(), Variable (swapLevel2), x), x, |
---|
| 96 | Variable (swapLevel1))); |
---|
[806c18] | 97 | else |
---|
[7bf145] | 98 | i.getItem()= N (swapvar (i.getItem(), x, Variable (swapLevel1))); |
---|
| 99 | } |
---|
[806c18] | 100 | else |
---|
[7bf145] | 101 | { |
---|
| 102 | if (swapLevel2) |
---|
| 103 | i.getItem()= N (swapvar (i.getItem(), Variable (swapLevel2), x)); |
---|
| 104 | else |
---|
| 105 | i.getItem()= N (i.getItem()); |
---|
| 106 | } |
---|
| 107 | } |
---|
[806c18] | 108 | for (CFListIterator i= factors2; i.hasItem(); i++) |
---|
| 109 | { |
---|
| 110 | if (!i.getItem().inCoeffDomain()) |
---|
[7bf145] | 111 | factors1.append (N (i.getItem())); |
---|
| 112 | } |
---|
| 113 | return; |
---|
| 114 | } |
---|
| 115 | |
---|
[806c18] | 116 | int* liftingBounds (const CanonicalForm& A, const int& bivarLiftBound) |
---|
[7bf145] | 117 | { |
---|
| 118 | int j= A.level() - 1; |
---|
| 119 | int* liftBounds= new int [j]; |
---|
| 120 | liftBounds[0]= bivarLiftBound; |
---|
[806c18] | 121 | for (int i= 1; i < j; i++) |
---|
[7bf145] | 122 | { |
---|
[806c18] | 123 | liftBounds[i]= degree (A, Variable (i + 2)) + 1 + |
---|
[7bf145] | 124 | degree (LC (A, 1), Variable (i + 2)); |
---|
| 125 | } |
---|
| 126 | return liftBounds; |
---|
| 127 | } |
---|
| 128 | |
---|
[6deedd] | 129 | CanonicalForm shift2Zero (const CanonicalForm& F, CFList& Feval, const CFList& evaluation, int l) |
---|
[7bf145] | 130 | { |
---|
| 131 | CanonicalForm A= F; |
---|
[6deedd] | 132 | int k= evaluation.length() + l - 1; |
---|
[7bf145] | 133 | for (CFListIterator i= evaluation; i.hasItem(); i++, k--) |
---|
| 134 | A= A (Variable (k) + i.getItem(), k); |
---|
| 135 | CanonicalForm buf= A; |
---|
| 136 | Feval= CFList(); |
---|
| 137 | Feval.append (buf); |
---|
| 138 | for (k= A.level(); k > 2; k--) |
---|
| 139 | { |
---|
| 140 | buf= mod (buf, Variable (k)); |
---|
| 141 | Feval.insert (buf); |
---|
| 142 | } |
---|
| 143 | return A; |
---|
| 144 | } |
---|
| 145 | |
---|
[6deedd] | 146 | CanonicalForm reverseShift (const CanonicalForm& F, const CFList& evaluation, int l) |
---|
[7bf145] | 147 | { |
---|
[6deedd] | 148 | int k= evaluation.length() + l - 1; |
---|
[7bf145] | 149 | CanonicalForm result= F; |
---|
| 150 | CFListIterator j= evaluation; |
---|
[6deedd] | 151 | for (int i= k; j.hasItem() && (i > l - 1); i--, j++) |
---|
[7bf145] | 152 | { |
---|
| 153 | if (F.level() < i) |
---|
| 154 | continue; |
---|
| 155 | result= result (Variable (i) - j.getItem(), i); |
---|
| 156 | } |
---|
| 157 | return result; |
---|
| 158 | } |
---|
| 159 | |
---|
[79592ac] | 160 | bool isOnlyLeadingCoeff (const CanonicalForm& F) |
---|
| 161 | { |
---|
| 162 | return (F-LC (F,1)*power (Variable(1),degree (F,1))).isZero(); |
---|
| 163 | } |
---|
| 164 | |
---|
[8d2c11] | 165 | /// like getVars but including multiplicities |
---|
| 166 | CanonicalForm myGetVars (const CanonicalForm& F) |
---|
| 167 | { |
---|
| 168 | CanonicalForm result= 1; |
---|
| 169 | int deg; |
---|
| 170 | for (int i= 1; i <= F.level(); i++) |
---|
| 171 | { |
---|
| 172 | if ((deg= degree (F, i)) > 0) |
---|
| 173 | result *= power (Variable (i), deg); |
---|
| 174 | } |
---|
| 175 | return result; |
---|
| 176 | } |
---|
| 177 | |
---|
| 178 | int compareByNumberOfVars (const CFFactor& F, const CFFactor& G) |
---|
| 179 | { |
---|
| 180 | return getNumVars (F.factor()) < getNumVars (G.factor()); |
---|
| 181 | } |
---|
| 182 | |
---|
| 183 | CFFList |
---|
| 184 | sortCFFListByNumOfVars (CFFList& F) |
---|
| 185 | { |
---|
| 186 | F.sort (compareByNumberOfVars); |
---|
| 187 | CFFList result= F; |
---|
| 188 | return result; |
---|
| 189 | } |
---|
| 190 | |
---|
[8336c9] | 191 | CFList evaluateAtZero (const CanonicalForm& F) |
---|
| 192 | { |
---|
| 193 | CFList result; |
---|
| 194 | CanonicalForm buf= F; |
---|
| 195 | result.insert (buf); |
---|
| 196 | for (int i= F.level(); i > 2; i--) |
---|
| 197 | { |
---|
| 198 | buf= buf (0, i); |
---|
| 199 | result.insert (buf); |
---|
| 200 | } |
---|
| 201 | return result; |
---|
| 202 | } |
---|
| 203 | |
---|
| 204 | CFList evaluateAtEval (const CanonicalForm& F, const CFArray& eval) |
---|
| 205 | { |
---|
| 206 | CFList result; |
---|
| 207 | CanonicalForm buf= F; |
---|
| 208 | result.insert (buf); |
---|
| 209 | int k= eval.size(); |
---|
| 210 | for (int i= 1; i < k; i++) |
---|
| 211 | { |
---|
| 212 | buf= buf (eval[i], i + 2); |
---|
| 213 | result.insert (buf); |
---|
| 214 | } |
---|
| 215 | return result; |
---|
| 216 | } |
---|
| 217 | |
---|
| 218 | CFList evaluateAtEval (const CanonicalForm& F, const CFList& evaluation, int l) |
---|
| 219 | { |
---|
| 220 | CFList result; |
---|
| 221 | CanonicalForm buf= F; |
---|
| 222 | result.insert (buf); |
---|
| 223 | int k= evaluation.length() + l - 1; |
---|
| 224 | CFListIterator j= evaluation; |
---|
| 225 | for (int i= k; j.hasItem() && i > l; i--, j++) |
---|
| 226 | { |
---|
| 227 | if (F.level() < i) |
---|
| 228 | continue; |
---|
| 229 | buf= buf (j.getItem(), i); |
---|
| 230 | result.insert (buf); |
---|
| 231 | } |
---|
| 232 | return result; |
---|
| 233 | } |
---|
| 234 | |
---|
[7bf145] | 235 | |
---|
[8336c9] | 236 | CFList recoverFactors (const CanonicalForm& F, const CFList& factors) |
---|
| 237 | { |
---|
| 238 | CFList result; |
---|
| 239 | CanonicalForm tmp, tmp2; |
---|
| 240 | CanonicalForm G= F; |
---|
| 241 | for (CFListIterator i= factors; i.hasItem(); i++) |
---|
| 242 | { |
---|
| 243 | tmp= i.getItem()/content (i.getItem(), 1); |
---|
| 244 | if (fdivides (tmp, G, tmp2)) |
---|
| 245 | { |
---|
| 246 | G= tmp2; |
---|
| 247 | result.append (tmp); |
---|
| 248 | } |
---|
| 249 | } |
---|
[5b390e] | 250 | if (result.length() + 1 == factors.length()) |
---|
| 251 | result.append (G/content (G,1)); |
---|
[8336c9] | 252 | return result; |
---|
| 253 | } |
---|
| 254 | |
---|
| 255 | CFList recoverFactors (const CanonicalForm& F, const CFList& factors, |
---|
| 256 | const CFList& evaluation) |
---|
| 257 | { |
---|
| 258 | CFList result; |
---|
| 259 | CanonicalForm tmp, tmp2; |
---|
| 260 | CanonicalForm G= F; |
---|
| 261 | for (CFListIterator i= factors; i.hasItem(); i++) |
---|
| 262 | { |
---|
| 263 | tmp= reverseShift (i.getItem(), evaluation, 2); |
---|
| 264 | tmp /= content (tmp, 1); |
---|
| 265 | if (fdivides (tmp, G, tmp2)) |
---|
| 266 | { |
---|
| 267 | G= tmp2; |
---|
| 268 | result.append (tmp); |
---|
| 269 | } |
---|
| 270 | } |
---|
[5b390e] | 271 | if (result.length() + 1 == factors.length()) |
---|
| 272 | result.append (G/content (G,1)); |
---|
[8336c9] | 273 | return result; |
---|
| 274 | } |
---|
| 275 | |
---|
| 276 | CFList recoverFactors (CanonicalForm& F, const CFList& factors, int* index) |
---|
| 277 | { |
---|
| 278 | CFList result; |
---|
| 279 | CanonicalForm tmp, tmp2; |
---|
| 280 | CanonicalForm G= F; |
---|
| 281 | int j= 0; |
---|
| 282 | for (CFListIterator i= factors; i.hasItem(); i++, j++) |
---|
| 283 | { |
---|
| 284 | if (i.getItem().isZero()) |
---|
| 285 | { |
---|
| 286 | index[j]= 0; |
---|
| 287 | continue; |
---|
| 288 | } |
---|
| 289 | tmp= i.getItem(); |
---|
| 290 | if (fdivides (tmp, G, tmp2)) |
---|
| 291 | { |
---|
| 292 | G= tmp2; |
---|
| 293 | tmp /=content (tmp, 1); |
---|
| 294 | result.append (tmp); |
---|
| 295 | index[j]= 1; |
---|
| 296 | } |
---|
| 297 | else |
---|
| 298 | index[j]= 0; |
---|
| 299 | } |
---|
[5b390e] | 300 | if (result.length() + 1 == factors.length()) |
---|
| 301 | { |
---|
| 302 | result.append (G/content (G,1)); |
---|
| 303 | F= G/content (G,1); |
---|
| 304 | } |
---|
| 305 | else |
---|
| 306 | F= G; |
---|
[8336c9] | 307 | return result; |
---|
| 308 | } |
---|