[59a7ca1] | 1 | // -*- c++ -*- |
---|
| 2 | //***************************************************************************** |
---|
| 3 | /** @file facAlgExt.cc |
---|
| 4 | * |
---|
| 5 | * @author Martin Lee |
---|
[806c18] | 6 | * @date |
---|
[59a7ca1] | 7 | * |
---|
[1b38e5d] | 8 | * Univariate factorization over algebraic extension of Q using Trager's |
---|
| 9 | * algorithm |
---|
[59a7ca1] | 10 | * |
---|
| 11 | * @par Copyright: |
---|
| 12 | * (c) by The SINGULAR Team, see LICENSE file |
---|
| 13 | * |
---|
| 14 | **/ |
---|
| 15 | //***************************************************************************** |
---|
| 16 | |
---|
[16f511] | 17 | #ifdef HAVE_CONFIG_H |
---|
[e4fe2b] | 18 | #include "config.h" |
---|
[16f511] | 19 | #endif /* HAVE_CONFIG_H */ |
---|
[59a7ca1] | 20 | |
---|
[650f2d8] | 21 | #include "cf_assert.h" |
---|
[59a7ca1] | 22 | #include "debug.h" |
---|
| 23 | #include "timing.h" |
---|
| 24 | |
---|
| 25 | #include "canonicalform.h" |
---|
| 26 | #include "cf_random.h" |
---|
| 27 | #include "cf_algorithm.h" |
---|
| 28 | #include "facFqBivarUtil.h" |
---|
| 29 | #include "facAlgExt.h" |
---|
[35eb6c] | 30 | #include "cfModResultant.h" |
---|
[b2929e7] | 31 | #include "fac_sqrfree.h" |
---|
[59a7ca1] | 32 | |
---|
[0851b0] | 33 | TIMING_DEFINE_PRINT(fac_alg_resultant) |
---|
| 34 | TIMING_DEFINE_PRINT(fac_alg_norm) |
---|
| 35 | TIMING_DEFINE_PRINT(fac_alg_factor_norm) |
---|
| 36 | TIMING_DEFINE_PRINT(fac_alg_gcd) |
---|
| 37 | TIMING_DEFINE_PRINT(fac_alg_sqrf) |
---|
| 38 | TIMING_DEFINE_PRINT(fac_alg_factor_sqrf) |
---|
[4f6d99] | 39 | TIMING_DEFINE_PRINT(fac_alg_time_shift) |
---|
[0851b0] | 40 | |
---|
[1b38e5d] | 41 | // squarefree part of F |
---|
[59a7ca1] | 42 | CanonicalForm |
---|
[35eb6c] | 43 | uniSqrfPart (const CanonicalForm& F) |
---|
[59a7ca1] | 44 | { |
---|
| 45 | ASSERT (F.isUnivariate(), "univariate input expected"); |
---|
| 46 | ASSERT (getCharacteristic() == 0, "characteristic 0 expected"); |
---|
| 47 | CanonicalForm G= deriv (F, F.mvar()); |
---|
| 48 | G= gcd (F, G); |
---|
[806c18] | 49 | return F/G; |
---|
[59a7ca1] | 50 | } |
---|
| 51 | |
---|
[1b38e5d] | 52 | // i is an integer such that Norm (F (x-i*alpha)) is squarefree |
---|
[59a7ca1] | 53 | CanonicalForm sqrfNorm (const CanonicalForm& F, const Variable& alpha, int& i) |
---|
| 54 | { |
---|
| 55 | Variable x= Variable (F.level() + 1); |
---|
| 56 | Variable y= F.mvar(); |
---|
| 57 | CanonicalForm g= F (x, alpha); |
---|
| 58 | CanonicalForm mipo= getMipo (alpha); |
---|
| 59 | mipo= mipo (x, alpha); |
---|
[50a2aa9] | 60 | mipo *= bCommonDen (mipo); |
---|
[806c18] | 61 | |
---|
[35eb6c] | 62 | int degg= degree (g); |
---|
| 63 | int degmipo= degree (mipo); |
---|
| 64 | CanonicalForm norm; |
---|
[0851b0] | 65 | TIMING_START (fac_alg_resultant); |
---|
[35eb6c] | 66 | if (degg >= 8 || degmipo >= 8) |
---|
| 67 | norm= resultantZ (g, mipo, x); |
---|
| 68 | else |
---|
| 69 | norm= resultant (g, mipo, x); |
---|
[0851b0] | 70 | TIMING_END_AND_PRINT (fac_alg_resultant, "time to compute resultant0: "); |
---|
[35eb6c] | 71 | |
---|
[59a7ca1] | 72 | i= 0; |
---|
| 73 | int k; |
---|
| 74 | if (degree (gcd (deriv (norm, y), norm)) <= 0) |
---|
| 75 | return norm; |
---|
| 76 | i= 1; |
---|
| 77 | do |
---|
| 78 | { |
---|
| 79 | k= 1; |
---|
| 80 | while (k < 3) |
---|
| 81 | { |
---|
| 82 | if (k == 1) |
---|
| 83 | { |
---|
| 84 | g= F (y - i*alpha, y); |
---|
[50a2aa9] | 85 | g *= bCommonDen (g); |
---|
[0851b0] | 86 | TIMING_START (fac_alg_resultant); |
---|
[35eb6c] | 87 | if (degg >= 8 || degmipo >= 8) |
---|
| 88 | norm= resultantZ (g (x, alpha), mipo, x); |
---|
| 89 | else |
---|
| 90 | norm= resultant (g (x, alpha), mipo, x); |
---|
[0851b0] | 91 | TIMING_END_AND_PRINT (fac_alg_resultant,"time to compute resultant1: "); |
---|
[59a7ca1] | 92 | } |
---|
| 93 | else |
---|
| 94 | { |
---|
| 95 | g= F (y + i*alpha, y); |
---|
[50a2aa9] | 96 | g *= bCommonDen (g); |
---|
[0851b0] | 97 | TIMING_START (fac_alg_resultant); |
---|
[35eb6c] | 98 | if (degg >= 8 || degmipo >= 8) |
---|
| 99 | norm= resultantZ (g (x, alpha), mipo, x); |
---|
| 100 | else |
---|
| 101 | norm= resultant (g (x, alpha), mipo, x); |
---|
[0851b0] | 102 | TIMING_END_AND_PRINT (fac_alg_resultant,"time to compute resultant2: "); |
---|
[59a7ca1] | 103 | } |
---|
| 104 | if (degree (gcd (deriv (norm, y), norm)) <= 0) |
---|
| 105 | { |
---|
| 106 | if (k == 2) |
---|
| 107 | i= -i; |
---|
| 108 | return norm; |
---|
| 109 | } |
---|
| 110 | k++; |
---|
| 111 | } |
---|
| 112 | i++; |
---|
| 113 | } while (1); |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | CFList |
---|
| 117 | AlgExtSqrfFactorize (const CanonicalForm& F, const Variable& alpha) |
---|
| 118 | { |
---|
| 119 | ASSERT (F.isUnivariate(), "univariate input expected"); |
---|
| 120 | ASSERT (getCharacteristic() == 0, "characteristic 0 expected"); |
---|
[806c18] | 121 | |
---|
[9fd0b1] | 122 | bool save_rat=!isOn (SW_RATIONAL); |
---|
| 123 | On (SW_RATIONAL); |
---|
[35eb6c] | 124 | CanonicalForm f= F*bCommonDen (F); |
---|
[59a7ca1] | 125 | int shift; |
---|
[0851b0] | 126 | TIMING_START (fac_alg_norm); |
---|
[59a7ca1] | 127 | CanonicalForm norm= sqrfNorm (f, alpha, shift); |
---|
[0851b0] | 128 | TIMING_END_AND_PRINT (fac_alg_norm, "time to compute sqrf norm: "); |
---|
[59a7ca1] | 129 | ASSERT (degree (norm, alpha) <= 0, "wrong norm computed"); |
---|
[0851b0] | 130 | TIMING_START (fac_alg_factor_norm); |
---|
[4f6d99] | 131 | bool save_sort= !isOn (SW_USE_NTL_SORT); |
---|
| 132 | On (SW_USE_NTL_SORT); |
---|
[50a2aa9] | 133 | CFFList normFactors= factorize (norm); |
---|
[4f6d99] | 134 | if (save_sort) |
---|
| 135 | Off (SW_USE_NTL_SORT); |
---|
[0851b0] | 136 | TIMING_END_AND_PRINT (fac_alg_factor_norm, "time to factor norm: "); |
---|
[59a7ca1] | 137 | CFList factors; |
---|
| 138 | if (normFactors.length() <= 2) |
---|
[9fd0b1] | 139 | { |
---|
| 140 | if (save_rat) Off(SW_RATIONAL); |
---|
[35eb6c] | 141 | return CFList (F); |
---|
[9fd0b1] | 142 | } |
---|
[806c18] | 143 | |
---|
[59a7ca1] | 144 | normFactors.removeFirst(); |
---|
[4f6d99] | 145 | CFFListIterator i= normFactors; |
---|
[59a7ca1] | 146 | CanonicalForm buf; |
---|
[4f6d99] | 147 | bool shiftBuf= false; |
---|
| 148 | if (!(normFactors.length() == 2 && degree (i.getItem().factor()) <= degree (f))) |
---|
| 149 | { |
---|
| 150 | TIMING_START (fac_alg_time_shift); |
---|
| 151 | if (shift != 0) |
---|
| 152 | buf= f (f.mvar() - shift*alpha, f.mvar()); |
---|
| 153 | else |
---|
| 154 | buf= f; |
---|
| 155 | shiftBuf= true; |
---|
| 156 | TIMING_END_AND_PRINT (fac_alg_time_shift, "time to shift: "); |
---|
| 157 | } |
---|
| 158 | else |
---|
| 159 | buf= f; |
---|
[59a7ca1] | 160 | CanonicalForm factor; |
---|
[6ea864] | 161 | int count= 0; |
---|
[4f6d99] | 162 | for (; i.hasItem(); i++) |
---|
[59a7ca1] | 163 | { |
---|
| 164 | ASSERT (i.getItem().exp() == 1, "norm not squarefree"); |
---|
[0851b0] | 165 | TIMING_START (fac_alg_gcd); |
---|
[4f6d99] | 166 | if (shiftBuf) |
---|
[062583] | 167 | factor= gcd (buf, i.getItem().factor()); |
---|
| 168 | else |
---|
[4f6d99] | 169 | { |
---|
| 170 | if (shift == 0) |
---|
| 171 | factor= gcd (buf, i.getItem().factor()); |
---|
| 172 | else |
---|
| 173 | factor= gcd (buf, i.getItem().factor() (f.mvar() + shift*alpha, f.mvar())); |
---|
| 174 | } |
---|
[59a7ca1] | 175 | buf /= factor; |
---|
[4f6d99] | 176 | if (shiftBuf) |
---|
| 177 | { |
---|
| 178 | if (shift != 0) |
---|
| 179 | factor= factor (f.mvar() + shift*alpha, f.mvar()); |
---|
| 180 | } |
---|
[0851b0] | 181 | TIMING_END_AND_PRINT (fac_alg_gcd, "time to recover factors: "); |
---|
[59a7ca1] | 182 | factors.append (factor); |
---|
[6ea864] | 183 | count++; |
---|
| 184 | if (normFactors.length() - 1 == count) |
---|
| 185 | { |
---|
[4f6d99] | 186 | if (shiftBuf) |
---|
| 187 | factors.append (buf (f.mvar() + shift*alpha, f.mvar())); |
---|
| 188 | else |
---|
| 189 | factors.append (buf); |
---|
[6ea864] | 190 | buf= 1; |
---|
| 191 | break; |
---|
| 192 | } |
---|
[806c18] | 193 | } |
---|
[59a7ca1] | 194 | ASSERT (degree (buf) <= 0, "incomplete factorization"); |
---|
[9fd0b1] | 195 | if (save_rat) Off(SW_RATIONAL); |
---|
[59a7ca1] | 196 | return factors; |
---|
| 197 | } |
---|
| 198 | |
---|
[806c18] | 199 | CFFList |
---|
[59a7ca1] | 200 | AlgExtFactorize (const CanonicalForm& F, const Variable& alpha) |
---|
| 201 | { |
---|
| 202 | ASSERT (F.isUnivariate(), "univariate input expected"); |
---|
| 203 | ASSERT (getCharacteristic() == 0, "characteristic 0 expected"); |
---|
[806c18] | 204 | |
---|
[35eb6c] | 205 | |
---|
[59a7ca1] | 206 | if (F.inCoeffDomain()) |
---|
| 207 | return CFFList (CFFactor (F, 1)); |
---|
| 208 | |
---|
[50a2aa9] | 209 | bool save_rat=!isOn (SW_RATIONAL); |
---|
| 210 | On (SW_RATIONAL); |
---|
[0851b0] | 211 | TIMING_START (fac_alg_sqrf); |
---|
[5f9b47] | 212 | CFFList sqrf= sqrFreeZ (F); |
---|
[0851b0] | 213 | TIMING_END_AND_PRINT (fac_alg_sqrf, "time for sqrf factors in Q(a)[x]: "); |
---|
[5f9b47] | 214 | CFList factorsSqrf; |
---|
[59a7ca1] | 215 | CFFList factors; |
---|
[5f9b47] | 216 | CFListIterator j; |
---|
| 217 | |
---|
[1682691] | 218 | CanonicalForm lcinv; |
---|
[5f9b47] | 219 | for (CFFListIterator i= sqrf; i.hasItem(); i++) |
---|
[59a7ca1] | 220 | { |
---|
[5f9b47] | 221 | if (i.getItem().factor().inCoeffDomain()) continue; |
---|
[0851b0] | 222 | TIMING_START (fac_alg_factor_sqrf); |
---|
[5f9b47] | 223 | factorsSqrf= AlgExtSqrfFactorize (i.getItem().factor(), alpha); |
---|
[0851b0] | 224 | TIMING_END_AND_PRINT (fac_alg_factor_sqrf, |
---|
| 225 | "time to factor sqrf factors in Q(a)[x]: "); |
---|
[5f9b47] | 226 | for (j= factorsSqrf; j.hasItem(); j++) |
---|
[1682691] | 227 | { |
---|
| 228 | lcinv= 1/Lc (j.getItem()); |
---|
| 229 | factors.append (CFFactor (j.getItem()*lcinv, i.getItem().exp())); |
---|
| 230 | } |
---|
[806c18] | 231 | } |
---|
[5f9b47] | 232 | |
---|
[59a7ca1] | 233 | factors.insert (CFFactor (Lc(F), 1)); |
---|
[50a2aa9] | 234 | if (save_rat) Off(SW_RATIONAL); |
---|
[806c18] | 235 | return factors; |
---|
[59a7ca1] | 236 | } |
---|
| 237 | |
---|