[35aab3] | 1 | // emacs edit mode for this file is -*- C++ -*- |
---|
| 2 | /**************************************** |
---|
| 3 | * Computer Algebra System SINGULAR * |
---|
| 4 | ****************************************/ |
---|
| 5 | /* |
---|
| 6 | * ABSTRACT: interface between Singular and factory |
---|
| 7 | */ |
---|
| 8 | |
---|
[b963c5] | 9 | //#define FACTORIZE2_DEBUG |
---|
[3426545] | 10 | |
---|
[aadd638] | 11 | #include "misc/auxiliary.h" |
---|
[3426545] | 12 | #include "clapsing.h" |
---|
| 13 | |
---|
[aadd638] | 14 | #include "factory/factory.h" |
---|
[fea2af] | 15 | |
---|
[aadd638] | 16 | #include "coeffs/numbers.h" |
---|
| 17 | #include "coeffs/coeffs.h" |
---|
| 18 | #include "coeffs/bigintmat.h" |
---|
[af598e] | 19 | |
---|
| 20 | #include "monomials/ring.h" |
---|
| 21 | #include "simpleideals.h" |
---|
[4abcd2] | 22 | #include "polys/flintconv.h" |
---|
| 23 | #include "polys/flint_mpoly.h" |
---|
[35aab3] | 24 | |
---|
[9144617] | 25 | |
---|
[1f414c8] | 26 | //#include "polys.h" |
---|
| 27 | #define TRANSEXT_PRIVATES |
---|
[3426545] | 28 | |
---|
[1f414c8] | 29 | #include "ext_fields/transext.h" |
---|
| 30 | |
---|
[9144617] | 31 | |
---|
| 32 | #include "clapconv.h" |
---|
[aadd638] | 33 | |
---|
| 34 | #include "polys/monomials/p_polys.h" |
---|
| 35 | #include "polys/monomials/ring.h" |
---|
| 36 | #include "polys/simpleideals.h" |
---|
| 37 | #include "misc/intvec.h" |
---|
| 38 | #include "polys/matpol.h" |
---|
| 39 | #include "coeffs/bigintmat.h" |
---|
[3426545] | 40 | |
---|
[9144617] | 41 | |
---|
[a86cda] | 42 | void out_cf(const char *s1,const CanonicalForm &f,const char *s2); |
---|
[35aab3] | 43 | |
---|
[5a4e17] | 44 | poly singclap_gcd_r ( poly f, poly g, const ring r ) |
---|
[35aab3] | 45 | { |
---|
| 46 | poly res=NULL; |
---|
| 47 | |
---|
[36586bb] | 48 | assume(f!=NULL); |
---|
| 49 | assume(g!=NULL); |
---|
| 50 | |
---|
[255be23] | 51 | if(pNext(f)==NULL) |
---|
[9b9b5a] | 52 | { |
---|
[255be23] | 53 | return p_GcdMon(f,g,r); |
---|
[5a4e17] | 54 | } |
---|
[255be23] | 55 | else if(pNext(g)==NULL) |
---|
[5a4e17] | 56 | { |
---|
[255be23] | 57 | return p_GcdMon(g,f,r); |
---|
[9b9b5a] | 58 | } |
---|
[4abcd2] | 59 | #ifdef HAVE_FLINT |
---|
| 60 | #if __FLINT_RELEASE >= 20503 |
---|
[d90ef2] | 61 | if (rField_is_Zp(r) && (r->cf->ch>500)) |
---|
[4abcd2] | 62 | { |
---|
| 63 | nmod_mpoly_ctx_t ctx; |
---|
| 64 | if (!convSingRFlintR(ctx,r)) |
---|
| 65 | { |
---|
[d90ef2] | 66 | // leading coef. 1 |
---|
[4abcd2] | 67 | return Flint_GCD_MP(f,pLength(f),g,pLength(g),ctx,r); |
---|
| 68 | } |
---|
| 69 | } |
---|
[919490] | 70 | else |
---|
| 71 | if (rField_is_Q(r)) |
---|
[4abcd2] | 72 | { |
---|
| 73 | fmpq_mpoly_ctx_t ctx; |
---|
| 74 | if (!convSingRFlintR(ctx,r)) |
---|
| 75 | { |
---|
[d90ef2] | 76 | // leading coef. positive, all coeffs in Z |
---|
[4abcd2] | 77 | poly res=Flint_GCD_MP(f,pLength(f),g,pLength(g),ctx,r); |
---|
| 78 | res=p_Cleardenom(res,r); |
---|
[d90ef2] | 79 | return res; |
---|
[4abcd2] | 80 | } |
---|
| 81 | } |
---|
| 82 | #endif |
---|
| 83 | #endif |
---|
[ea48f7d] | 84 | Off(SW_RATIONAL); |
---|
[353a42] | 85 | if (rField_is_Q(r) || rField_is_Zp(r) || rField_is_Z(r) |
---|
[417a91a] | 86 | || (rField_is_Zn(r)&&(r->cf->convSingNFactoryN!=ndConvSingNFactoryN))) |
---|
[ce2576] | 87 | { |
---|
| 88 | setCharacteristic( rChar(r) ); |
---|
| 89 | CanonicalForm F( convSingPFactoryP( f,r ) ), G( convSingPFactoryP( g, r ) ); |
---|
| 90 | res=convFactoryPSingP( gcd( F, G ) , r); |
---|
[d90ef2] | 91 | if ( rField_is_Zp(r)) |
---|
| 92 | p_Norm(res,r); // leading coef. 1 |
---|
| 93 | else if (rField_is_Q(r) && (!n_GreaterZero(pGetCoeff(res),r->cf))) |
---|
| 94 | res = p_Neg(res,r); // leading coef. positive, all coeffs in Z |
---|
[ce2576] | 95 | } |
---|
| 96 | // and over Q(a) / Fp(a) |
---|
[e72a9a] | 97 | else if ( r->cf->extRing!=NULL ) |
---|
[ce2576] | 98 | { |
---|
| 99 | if ( rField_is_Q_a(r)) setCharacteristic( 0 ); |
---|
| 100 | else setCharacteristic( rChar(r) ); |
---|
[dd668f] | 101 | if (r->cf->extRing->qideal!=NULL) |
---|
[ce2576] | 102 | { |
---|
| 103 | bool b1=isOn(SW_USE_QGCD); |
---|
| 104 | if ( rField_is_Q_a(r) ) On(SW_USE_QGCD); |
---|
[dd668f] | 105 | CanonicalForm mipo=convSingPFactoryP(r->cf->extRing->qideal->m[0], |
---|
[ce2576] | 106 | r->cf->extRing); |
---|
| 107 | Variable a=rootOf(mipo); |
---|
| 108 | CanonicalForm F( convSingAPFactoryAP( f,a,r ) ), |
---|
| 109 | G( convSingAPFactoryAP( g,a,r ) ); |
---|
| 110 | res= convFactoryAPSingAP( gcd( F, G ),r ); |
---|
[8b00ca] | 111 | prune (a); |
---|
[ce2576] | 112 | if (!b1) Off(SW_USE_QGCD); |
---|
[85e90d] | 113 | if ( rField_is_Zp_a(r)) p_Norm(res,r); // leading coef. 1 |
---|
[ce2576] | 114 | } |
---|
| 115 | else |
---|
| 116 | { |
---|
[184739] | 117 | convSingTrP(f,r); |
---|
| 118 | convSingTrP(g,r); |
---|
[ce2576] | 119 | CanonicalForm F( convSingTrPFactoryP( f,r ) ), G( convSingTrPFactoryP( g,r ) ); |
---|
| 120 | res= convFactoryPSingTrP( gcd( F, G ),r ); |
---|
| 121 | } |
---|
| 122 | } |
---|
[6462a0] | 123 | else if (r->cf->convSingNFactoryN==ndConvSingNFactoryN) |
---|
[ce2576] | 124 | WerrorS( feNotImplemented ); |
---|
[6462a0] | 125 | else |
---|
| 126 | { // handle user type coeffs: |
---|
| 127 | setCharacteristic( rChar(r) ); |
---|
| 128 | CanonicalForm F( convSingPFactoryP( f,r ) ), G( convSingPFactoryP( g, r ) ); |
---|
| 129 | res=convFactoryPSingP( gcd( F, G ) , r); |
---|
| 130 | } |
---|
[ea48f7d] | 131 | Off(SW_RATIONAL); |
---|
[35aab3] | 132 | return res; |
---|
| 133 | } |
---|
| 134 | |
---|
[2de7b0] | 135 | poly singclap_gcd_and_divide ( poly& f, poly& g, const ring r) |
---|
[91d0e6] | 136 | { |
---|
| 137 | poly res=NULL; |
---|
| 138 | |
---|
[2de7b0] | 139 | if (g == NULL) |
---|
| 140 | { |
---|
[7f7808] | 141 | res= f; |
---|
[2de7b0] | 142 | f=p_One (r); |
---|
| 143 | return res; |
---|
| 144 | } |
---|
| 145 | if (f==NULL) |
---|
| 146 | { |
---|
[7f7808] | 147 | res= g; |
---|
[2de7b0] | 148 | g=p_One (r); |
---|
| 149 | return res; |
---|
| 150 | } |
---|
[7b4ca1f] | 151 | if (pNext(g)==NULL) |
---|
[061eacd] | 152 | { |
---|
[7b4ca1f] | 153 | poly G=p_GcdMon(g,f,r); |
---|
| 154 | if (!n_IsOne(pGetCoeff(G),r->cf) |
---|
| 155 | || (!p_IsConstant(G,r))) |
---|
[061eacd] | 156 | { |
---|
[7b4ca1f] | 157 | f=p_Div_mm(f,G,r); |
---|
| 158 | g=p_Div_mm(g,G,r); |
---|
| 159 | } |
---|
| 160 | return G; |
---|
[061eacd] | 161 | } |
---|
[7b4ca1f] | 162 | else if (pNext(f)==NULL) |
---|
[061eacd] | 163 | { |
---|
[7b4ca1f] | 164 | poly G=p_GcdMon(f,g,r); |
---|
| 165 | if (!n_IsOne(pGetCoeff(G),r->cf) |
---|
| 166 | || (!p_IsConstant(G,r))) |
---|
[061eacd] | 167 | { |
---|
[7b4ca1f] | 168 | f=p_Div_mm(f,G,r); |
---|
| 169 | g=p_Div_mm(g,G,r); |
---|
| 170 | } |
---|
| 171 | return G; |
---|
[061eacd] | 172 | } |
---|
[cd964c] | 173 | |
---|
| 174 | Off(SW_RATIONAL); |
---|
| 175 | CanonicalForm F,G,GCD; |
---|
[417a91a] | 176 | if (rField_is_Q(r) || (rField_is_Zp(r)) |
---|
| 177 | || (rField_is_Zn(r)&&(r->cf->convSingNFactoryN!=ndConvSingNFactoryN))) |
---|
[cd964c] | 178 | { |
---|
| 179 | bool b1=isOn(SW_USE_EZGCD_P); |
---|
[88b655] | 180 | setCharacteristic( rChar(r) ); |
---|
[cd964c] | 181 | F=convSingPFactoryP( f,r ); |
---|
| 182 | G=convSingPFactoryP( g,r ); |
---|
| 183 | GCD=gcd(F,G); |
---|
| 184 | if (!GCD.isOne()) |
---|
| 185 | { |
---|
| 186 | p_Delete(&f,r); |
---|
| 187 | p_Delete(&g,r); |
---|
[2de7b0] | 188 | if (getCharacteristic() == 0) |
---|
| 189 | On (SW_RATIONAL); |
---|
[57f6787] | 190 | F /= GCD; |
---|
| 191 | G /= GCD; |
---|
| 192 | if (getCharacteristic() == 0) |
---|
| 193 | { |
---|
| 194 | CanonicalForm denF= bCommonDen (F); |
---|
| 195 | CanonicalForm denG= bCommonDen (G); |
---|
| 196 | G *= denG; |
---|
| 197 | F *= denF; |
---|
| 198 | Off (SW_RATIONAL); |
---|
| 199 | CanonicalForm gcddenFdenG= gcd (denG, denF); |
---|
| 200 | denG /= gcddenFdenG; |
---|
| 201 | denF /= gcddenFdenG; |
---|
| 202 | On (SW_RATIONAL); |
---|
| 203 | G *= denF; |
---|
| 204 | F *= denG; |
---|
| 205 | } |
---|
| 206 | f=convFactoryPSingP( F, r); |
---|
| 207 | g=convFactoryPSingP( G, r); |
---|
[cd964c] | 208 | } |
---|
[2de7b0] | 209 | res=convFactoryPSingP( GCD , r); |
---|
[cd964c] | 210 | if (!b1) Off (SW_USE_EZGCD_P); |
---|
| 211 | } |
---|
| 212 | // and over Q(a) / Fp(a) |
---|
[e72a9a] | 213 | else if ( r->cf->extRing ) |
---|
[cd964c] | 214 | { |
---|
| 215 | if ( rField_is_Q_a(r)) setCharacteristic( 0 ); |
---|
| 216 | else setCharacteristic( rChar(r) ); |
---|
| 217 | if (r->cf->extRing->qideal!=NULL) |
---|
| 218 | { |
---|
| 219 | bool b1=isOn(SW_USE_QGCD); |
---|
| 220 | if ( rField_is_Q_a(r) ) On(SW_USE_QGCD); |
---|
| 221 | CanonicalForm mipo=convSingPFactoryP(r->cf->extRing->qideal->m[0], |
---|
| 222 | r->cf->extRing); |
---|
| 223 | Variable a=rootOf(mipo); |
---|
| 224 | F=( convSingAPFactoryAP( f,a,r ) ); |
---|
| 225 | G=( convSingAPFactoryAP( g,a,r ) ); |
---|
| 226 | GCD=gcd(F,G); |
---|
| 227 | if (!GCD.isOne()) |
---|
| 228 | { |
---|
| 229 | p_Delete(&f,r); |
---|
| 230 | p_Delete(&g,r); |
---|
[2de7b0] | 231 | if (getCharacteristic() == 0) |
---|
| 232 | On (SW_RATIONAL); |
---|
[57f6787] | 233 | F /= GCD; |
---|
| 234 | G /= GCD; |
---|
| 235 | if (getCharacteristic() == 0) |
---|
| 236 | { |
---|
| 237 | CanonicalForm denF= bCommonDen (F); |
---|
| 238 | CanonicalForm denG= bCommonDen (G); |
---|
| 239 | G *= denG; |
---|
| 240 | F *= denF; |
---|
| 241 | Off (SW_RATIONAL); |
---|
| 242 | CanonicalForm gcddenFdenG= gcd (denG, denF); |
---|
| 243 | denG /= gcddenFdenG; |
---|
| 244 | denF /= gcddenFdenG; |
---|
| 245 | On (SW_RATIONAL); |
---|
| 246 | G *= denF; |
---|
| 247 | F *= denG; |
---|
| 248 | } |
---|
| 249 | f= convFactoryAPSingAP( F,r ); |
---|
| 250 | g= convFactoryAPSingAP( G,r ); |
---|
[cd964c] | 251 | } |
---|
[2de7b0] | 252 | res= convFactoryAPSingAP( GCD,r ); |
---|
[8b00ca] | 253 | prune (a); |
---|
[cd964c] | 254 | if (!b1) Off(SW_USE_QGCD); |
---|
| 255 | } |
---|
| 256 | else |
---|
| 257 | { |
---|
| 258 | F=( convSingTrPFactoryP( f,r ) ); |
---|
| 259 | G=( convSingTrPFactoryP( g,r ) ); |
---|
| 260 | GCD=gcd(F,G); |
---|
| 261 | if (!GCD.isOne()) |
---|
| 262 | { |
---|
| 263 | p_Delete(&f,r); |
---|
| 264 | p_Delete(&g,r); |
---|
[2de7b0] | 265 | if (getCharacteristic() == 0) |
---|
| 266 | On (SW_RATIONAL); |
---|
[57f6787] | 267 | F /= GCD; |
---|
| 268 | G /= GCD; |
---|
| 269 | if (getCharacteristic() == 0) |
---|
| 270 | { |
---|
| 271 | CanonicalForm denF= bCommonDen (F); |
---|
| 272 | CanonicalForm denG= bCommonDen (G); |
---|
| 273 | G *= denG; |
---|
| 274 | F *= denF; |
---|
| 275 | Off (SW_RATIONAL); |
---|
| 276 | CanonicalForm gcddenFdenG= gcd (denG, denF); |
---|
| 277 | denG /= gcddenFdenG; |
---|
| 278 | denF /= gcddenFdenG; |
---|
| 279 | On (SW_RATIONAL); |
---|
| 280 | G *= denF; |
---|
| 281 | F *= denG; |
---|
| 282 | } |
---|
| 283 | f= convFactoryPSingTrP( F,r ); |
---|
| 284 | g= convFactoryPSingTrP( G,r ); |
---|
[cd964c] | 285 | } |
---|
[2de7b0] | 286 | res= convFactoryPSingTrP( GCD,r ); |
---|
[cd964c] | 287 | } |
---|
| 288 | } |
---|
| 289 | else |
---|
| 290 | WerrorS( feNotImplemented ); |
---|
| 291 | Off(SW_RATIONAL); |
---|
[2de7b0] | 292 | return res; |
---|
[cd964c] | 293 | } |
---|
| 294 | |
---|
[c95632f] | 295 | /*2 find the maximal exponent of var(i) in poly p*/ |
---|
[ce3f53c] | 296 | int pGetExp_Var(poly p, int i, const ring r) |
---|
[c95632f] | 297 | { |
---|
| 298 | int m=0; |
---|
| 299 | int mm; |
---|
| 300 | while (p!=NULL) |
---|
| 301 | { |
---|
[ce3f53c] | 302 | mm=p_GetExp(p,i,r); |
---|
[c95632f] | 303 | if (mm>m) m=mm; |
---|
| 304 | pIter(p); |
---|
| 305 | } |
---|
| 306 | return m; |
---|
| 307 | } |
---|
| 308 | |
---|
[9d9078] | 309 | // destroys f,g,x |
---|
[ce3f53c] | 310 | poly singclap_resultant ( poly f, poly g , poly x, const ring r) |
---|
[35aab3] | 311 | { |
---|
[9d9078] | 312 | poly res=NULL; |
---|
[ce3f53c] | 313 | int i=p_IsPurePower(x, r); |
---|
[35aab3] | 314 | if (i==0) |
---|
| 315 | { |
---|
| 316 | WerrorS("3rd argument must be a ring variable"); |
---|
[9d9078] | 317 | goto resultant_returns_res; |
---|
[35aab3] | 318 | } |
---|
| 319 | if ((f==NULL) || (g==NULL)) |
---|
[9d9078] | 320 | goto resultant_returns_res; |
---|
[ce2576] | 321 | // for now there is only the possibility to handle polynomials over |
---|
| 322 | // Q and Fp ... |
---|
[417a91a] | 323 | if (rField_is_Zp(r) || rField_is_Q(r) |
---|
| 324 | || (rField_is_Zn(r)&&(r->cf->convSingNFactoryN!=ndConvSingNFactoryN))) |
---|
[ce2576] | 325 | { |
---|
| 326 | Variable X(i); |
---|
| 327 | setCharacteristic( rChar(r) ); |
---|
| 328 | CanonicalForm F( convSingPFactoryP( f,r ) ), G( convSingPFactoryP( g,r ) ); |
---|
| 329 | res=convFactoryPSingP( resultant( F, G, X),r ); |
---|
[35aab3] | 330 | Off(SW_RATIONAL); |
---|
[ce2576] | 331 | goto resultant_returns_res; |
---|
[35aab3] | 332 | } |
---|
[ce2576] | 333 | // and over Q(a) / Fp(a) |
---|
[e72a9a] | 334 | else if (r->cf->extRing!=NULL) |
---|
[ce2576] | 335 | { |
---|
| 336 | if (rField_is_Q_a(r)) setCharacteristic( 0 ); |
---|
| 337 | else setCharacteristic( rChar(r) ); |
---|
| 338 | Variable X(i+rPar(r)); |
---|
[dd668f] | 339 | if (r->cf->extRing->qideal!=NULL) |
---|
[ce2576] | 340 | { |
---|
| 341 | //Variable X(i); |
---|
[dd668f] | 342 | CanonicalForm mipo=convSingPFactoryP(r->cf->extRing->qideal->m[0], |
---|
[ce2576] | 343 | r->cf->extRing); |
---|
| 344 | Variable a=rootOf(mipo); |
---|
| 345 | CanonicalForm F( convSingAPFactoryAP( f,a,r ) ), |
---|
| 346 | G( convSingAPFactoryAP( g,a,r ) ); |
---|
| 347 | res= convFactoryAPSingAP( resultant( F, G, X ),r ); |
---|
[8b00ca] | 348 | prune (a); |
---|
[ce2576] | 349 | } |
---|
| 350 | else |
---|
| 351 | { |
---|
| 352 | //Variable X(i+rPar(currRing)); |
---|
| 353 | number nf,ng; |
---|
| 354 | p_Cleardenom_n(f,r,nf);p_Cleardenom_n(g,r,ng); |
---|
| 355 | int ef,eg; |
---|
| 356 | ef=pGetExp_Var(f,i,r); |
---|
| 357 | eg=pGetExp_Var(g,i,r); |
---|
| 358 | CanonicalForm F( convSingTrPFactoryP( f,r ) ), G( convSingTrPFactoryP( g,r ) ); |
---|
| 359 | res= convFactoryPSingTrP( resultant( F, G, X ),r ); |
---|
[a355723] | 360 | if ((nf!=NULL)&&(!n_IsOne(nf,r->cf))) |
---|
[ce2576] | 361 | { |
---|
| 362 | number n=n_Invers(nf,r->cf); |
---|
| 363 | while(eg>0) |
---|
| 364 | { |
---|
[3d1222a] | 365 | res=__p_Mult_nn(res,n,r); |
---|
[ce2576] | 366 | eg--; |
---|
| 367 | } |
---|
| 368 | n_Delete(&n,r->cf); |
---|
| 369 | } |
---|
| 370 | n_Delete(&nf,r->cf); |
---|
[a355723] | 371 | if ((ng!=NULL)&&(!n_IsOne(ng,r->cf))) |
---|
[ce2576] | 372 | { |
---|
| 373 | number n=n_Invers(ng,r->cf); |
---|
| 374 | while(ef>0) |
---|
| 375 | { |
---|
[3d1222a] | 376 | res=__p_Mult_nn(res,n,r); |
---|
[ce2576] | 377 | ef--; |
---|
| 378 | } |
---|
| 379 | n_Delete(&n,r->cf); |
---|
| 380 | } |
---|
| 381 | n_Delete(&ng,r->cf); |
---|
| 382 | } |
---|
| 383 | Off(SW_RATIONAL); |
---|
| 384 | goto resultant_returns_res; |
---|
| 385 | } |
---|
| 386 | else |
---|
| 387 | WerrorS( feNotImplemented ); |
---|
[9d9078] | 388 | resultant_returns_res: |
---|
[ce3f53c] | 389 | p_Delete(&f,r); |
---|
| 390 | p_Delete(&g,r); |
---|
| 391 | p_Delete(&x,r); |
---|
[9d9078] | 392 | return res; |
---|
[35aab3] | 393 | } |
---|
| 394 | //poly singclap_resultant ( poly f, poly g , poly x) |
---|
| 395 | //{ |
---|
| 396 | // int i=pVar(x); |
---|
| 397 | // if (i==0) |
---|
| 398 | // { |
---|
| 399 | // WerrorS("ringvar expected"); |
---|
| 400 | // return NULL; |
---|
| 401 | // } |
---|
| 402 | // ideal I=idInit(1,1); |
---|
| 403 | // |
---|
| 404 | // // get the coeffs von f wrt. x: |
---|
| 405 | // I->m[0]=pCopy(f); |
---|
| 406 | // matrix ffi=mpCoeffs(I,i); |
---|
| 407 | // ffi->rank=1; |
---|
| 408 | // ffi->ncols=ffi->nrows; |
---|
| 409 | // ffi->nrows=1; |
---|
| 410 | // ideal fi=(ideal)ffi; |
---|
| 411 | // |
---|
| 412 | // // get the coeffs von g wrt. x: |
---|
| 413 | // I->m[0]=pCopy(g); |
---|
| 414 | // matrix ggi=mpCoeffs(I,i); |
---|
| 415 | // ggi->rank=1; |
---|
| 416 | // ggi->ncols=ggi->nrows; |
---|
| 417 | // ggi->nrows=1; |
---|
| 418 | // ideal gi=(ideal)ggi; |
---|
| 419 | // |
---|
| 420 | // // contruct the matrix: |
---|
| 421 | // int fn=IDELEMS(fi); //= deg(f,x)+1 |
---|
| 422 | // int gn=IDELEMS(gi); //= deg(g,x)+1 |
---|
| 423 | // matrix m=mpNew(fn+gn-2,fn+gn-2); |
---|
| 424 | // if(m==NULL) |
---|
| 425 | // { |
---|
| 426 | // return NULL; |
---|
| 427 | // } |
---|
| 428 | // |
---|
| 429 | // // enter the coeffs into m: |
---|
| 430 | // int j; |
---|
| 431 | // for(i=0;i<gn-1;i++) |
---|
| 432 | // { |
---|
| 433 | // for(j=0;j<fn;j++) |
---|
| 434 | // { |
---|
| 435 | // MATELEM(m,i+1,fn-j+i)=pCopy(fi->m[j]); |
---|
| 436 | // } |
---|
| 437 | // } |
---|
| 438 | // for(i=0;i<fn-1;i++) |
---|
| 439 | // { |
---|
| 440 | // for(j=0;j<gn;j++) |
---|
| 441 | // { |
---|
| 442 | // MATELEM(m,gn+i,gn-j+i)=pCopy(gi->m[j]); |
---|
| 443 | // } |
---|
| 444 | // } |
---|
| 445 | // |
---|
| 446 | // poly r=mpDet(m); |
---|
| 447 | // |
---|
| 448 | // idDelete(&fi); |
---|
| 449 | // idDelete(&gi); |
---|
| 450 | // idDelete((ideal *)&m); |
---|
| 451 | // return r; |
---|
| 452 | //} |
---|
| 453 | |
---|
[ce3f53c] | 454 | BOOLEAN singclap_extgcd ( poly f, poly g, poly &res, poly &pa, poly &pb , const ring r) |
---|
[35aab3] | 455 | { |
---|
| 456 | // for now there is only the possibility to handle univariate |
---|
| 457 | // polynomials over |
---|
| 458 | // Q and Fp ... |
---|
| 459 | res=NULL;pa=NULL;pb=NULL; |
---|
| 460 | On(SW_SYMMETRIC_FF); |
---|
[417a91a] | 461 | if ( rField_is_Q(r) || rField_is_Zp(r) |
---|
| 462 | || (rField_is_Zn(r)&&(r->cf->convSingNFactoryN!=ndConvSingNFactoryN))) |
---|
[35aab3] | 463 | { |
---|
[ce2576] | 464 | setCharacteristic( rChar(r) ); |
---|
| 465 | CanonicalForm F( convSingPFactoryP( f,r ) ), G( convSingPFactoryP( g,r) ); |
---|
| 466 | CanonicalForm FpG=F+G; |
---|
| 467 | if (!(FpG.isUnivariate()|| FpG.inCoeffDomain())) |
---|
| 468 | //if (!F.isUnivariate() || !G.isUnivariate() || F.mvar()!=G.mvar()) |
---|
| 469 | { |
---|
| 470 | Off(SW_RATIONAL); |
---|
| 471 | WerrorS("not univariate"); |
---|
| 472 | return TRUE; |
---|
| 473 | } |
---|
| 474 | CanonicalForm Fa,Gb; |
---|
| 475 | On(SW_RATIONAL); |
---|
| 476 | res=convFactoryPSingP( extgcd( F, G, Fa, Gb ),r ); |
---|
| 477 | pa=convFactoryPSingP(Fa,r); |
---|
| 478 | pb=convFactoryPSingP(Gb,r); |
---|
| 479 | Off(SW_RATIONAL); |
---|
| 480 | } |
---|
| 481 | // and over Q(a) / Fp(a) |
---|
[e72a9a] | 482 | else if ( r->cf->extRing!=NULL ) |
---|
[ce2576] | 483 | { |
---|
| 484 | if (rField_is_Q_a(r)) setCharacteristic( 0 ); |
---|
| 485 | else setCharacteristic( rChar(r) ); |
---|
| 486 | CanonicalForm Fa,Gb; |
---|
[dd668f] | 487 | if (r->cf->extRing->qideal!=NULL) |
---|
[ce2576] | 488 | { |
---|
[dd668f] | 489 | CanonicalForm mipo=convSingPFactoryP(r->cf->extRing->qideal->m[0], |
---|
[ce2576] | 490 | r->cf->extRing); |
---|
| 491 | Variable a=rootOf(mipo); |
---|
| 492 | CanonicalForm F( convSingAPFactoryAP( f,a,r ) ), |
---|
| 493 | G( convSingAPFactoryAP( g,a,r ) ); |
---|
| 494 | CanonicalForm FpG=F+G; |
---|
| 495 | if (!(FpG.isUnivariate()|| FpG.inCoeffDomain())) |
---|
| 496 | //if (!F.isUnivariate() || !G.isUnivariate() || F.mvar()!=G.mvar()) |
---|
| 497 | { |
---|
| 498 | WerrorS("not univariate"); |
---|
| 499 | return TRUE; |
---|
| 500 | } |
---|
| 501 | res= convFactoryAPSingAP( extgcd( F, G, Fa, Gb ),r ); |
---|
| 502 | pa=convFactoryAPSingAP(Fa,r); |
---|
| 503 | pb=convFactoryAPSingAP(Gb,r); |
---|
[8b00ca] | 504 | prune (a); |
---|
[ce2576] | 505 | } |
---|
| 506 | else |
---|
| 507 | { |
---|
| 508 | CanonicalForm F( convSingTrPFactoryP( f, r ) ), G( convSingTrPFactoryP( g, r ) ); |
---|
| 509 | CanonicalForm FpG=F+G; |
---|
| 510 | if (!(FpG.isUnivariate()|| FpG.inCoeffDomain())) |
---|
| 511 | //if (!F.isUnivariate() || !G.isUnivariate() || F.mvar()!=G.mvar()) |
---|
| 512 | { |
---|
| 513 | Off(SW_RATIONAL); |
---|
| 514 | WerrorS("not univariate"); |
---|
| 515 | return TRUE; |
---|
| 516 | } |
---|
| 517 | res= convFactoryPSingTrP( extgcd( F, G, Fa, Gb ), r ); |
---|
| 518 | pa=convFactoryPSingTrP(Fa, r); |
---|
| 519 | pb=convFactoryPSingTrP(Gb, r); |
---|
| 520 | } |
---|
[35aab3] | 521 | Off(SW_RATIONAL); |
---|
[ce2576] | 522 | } |
---|
| 523 | else |
---|
| 524 | { |
---|
| 525 | WerrorS( feNotImplemented ); |
---|
[35aab3] | 526 | return TRUE; |
---|
| 527 | } |
---|
[7fe9e13] | 528 | #ifndef SING_NDEBUG |
---|
[a86cda] | 529 | // checking the result of extgcd: |
---|
| 530 | poly dummy; |
---|
[ce3f53c] | 531 | dummy=p_Sub(p_Add_q(pp_Mult_qq(f,pa,r),pp_Mult_qq(g,pb,r),r),p_Copy(res,r),r); |
---|
[a86cda] | 532 | if (dummy!=NULL) |
---|
| 533 | { |
---|
[ce3f53c] | 534 | PrintS("extgcd( ");p_Write(f,r);p_Write0(g,r);PrintS(" )\n"); |
---|
[ce2576] | 535 | PrintS("extgcd( ");p_Write(f,r);p_Write0(g,r);PrintS(" )\n"); |
---|
[ce3f53c] | 536 | p_Delete(&dummy,r); |
---|
[a86cda] | 537 | } |
---|
[9d9078] | 538 | #endif |
---|
[35aab3] | 539 | return FALSE; |
---|
| 540 | } |
---|
| 541 | |
---|
[0ec309] | 542 | poly singclap_pmult ( poly f, poly g, const ring r ) |
---|
| 543 | { |
---|
| 544 | poly res=NULL; |
---|
| 545 | On(SW_RATIONAL); |
---|
[353a42] | 546 | if (rField_is_Zp(r) || rField_is_Q(r) || rField_is_Z(r) |
---|
[417a91a] | 547 | || (rField_is_Zn(r)&&(r->cf->convSingNFactoryN!=ndConvSingNFactoryN))) |
---|
[0ec309] | 548 | { |
---|
[353a42] | 549 | if (rField_is_Z(r)) Off(SW_RATIONAL); |
---|
[0ec309] | 550 | setCharacteristic( rChar(r) ); |
---|
| 551 | CanonicalForm F( convSingPFactoryP( f,r ) ), G( convSingPFactoryP( g,r ) ); |
---|
| 552 | res = convFactoryPSingP( F * G,r ); |
---|
| 553 | } |
---|
| 554 | else if (r->cf->extRing!=NULL) |
---|
| 555 | { |
---|
| 556 | if (rField_is_Q_a(r)) setCharacteristic( 0 ); |
---|
| 557 | else setCharacteristic( rChar(r) ); |
---|
| 558 | if (r->cf->extRing->qideal!=NULL) |
---|
| 559 | { |
---|
| 560 | CanonicalForm mipo=convSingPFactoryP(r->cf->extRing->qideal->m[0], |
---|
| 561 | r->cf->extRing); |
---|
| 562 | Variable a=rootOf(mipo); |
---|
| 563 | CanonicalForm F( convSingAPFactoryAP( f,a,r ) ), |
---|
| 564 | G( convSingAPFactoryAP( g,a,r ) ); |
---|
| 565 | res= convFactoryAPSingAP( F * G, r ); |
---|
| 566 | prune (a); |
---|
| 567 | } |
---|
| 568 | else |
---|
| 569 | { |
---|
| 570 | CanonicalForm F( convSingTrPFactoryP( f,r ) ), G( convSingTrPFactoryP( g,r ) ); |
---|
| 571 | res= convFactoryPSingTrP( F * G,r ); |
---|
| 572 | } |
---|
| 573 | } |
---|
| 574 | #if 0 // not yet working |
---|
| 575 | else if (rField_is_GF()) |
---|
| 576 | { |
---|
| 577 | //Print("GF(%d^%d)\n",nfCharP,nfMinPoly[0]); |
---|
| 578 | setCharacteristic( nfCharP,nfMinPoly[0], currRing->parameter[0][0] ); |
---|
| 579 | CanonicalForm F( convSingGFFactoryGF( f ) ), G( convSingGFFactoryGF( g ) ); |
---|
| 580 | res = convFactoryGFSingGF( F * G ); |
---|
| 581 | } |
---|
| 582 | #endif |
---|
| 583 | else |
---|
| 584 | WerrorS( feNotImplemented ); |
---|
| 585 | Off(SW_RATIONAL); |
---|
| 586 | return res; |
---|
| 587 | } |
---|
| 588 | |
---|
[ce3f53c] | 589 | poly singclap_pdivide ( poly f, poly g, const ring r ) |
---|
[35aab3] | 590 | { |
---|
| 591 | poly res=NULL; |
---|
[2fb52df] | 592 | |
---|
| 593 | #ifdef HAVE_FLINT |
---|
| 594 | #if __FLINT_RELEASE >= 20503 |
---|
| 595 | /* |
---|
| 596 | If the division is not exact, control will pass to factory where the |
---|
| 597 | polynomials can be divided using the ordering that factory chooses. |
---|
| 598 | */ |
---|
| 599 | if (rField_is_Zp(r)) |
---|
| 600 | { |
---|
| 601 | nmod_mpoly_ctx_t ctx; |
---|
| 602 | if (!convSingRFlintR(ctx,r)) |
---|
| 603 | { |
---|
| 604 | res = Flint_Divide_MP(f,pLength(f),g,pLength(g),ctx,r); |
---|
| 605 | if (res != NULL) |
---|
| 606 | return res; |
---|
| 607 | } |
---|
| 608 | } |
---|
| 609 | else |
---|
| 610 | if (rField_is_Q(r)) |
---|
| 611 | { |
---|
| 612 | fmpq_mpoly_ctx_t ctx; |
---|
| 613 | if (!convSingRFlintR(ctx,r)) |
---|
| 614 | { |
---|
| 615 | res = Flint_Divide_MP(f,pLength(f),g,pLength(g),ctx,r); |
---|
| 616 | if (res != NULL) |
---|
| 617 | return res; |
---|
| 618 | } |
---|
| 619 | } |
---|
| 620 | #endif |
---|
| 621 | #endif |
---|
| 622 | |
---|
[35aab3] | 623 | On(SW_RATIONAL); |
---|
[417a91a] | 624 | if (rField_is_Zp(r) || rField_is_Q(r) |
---|
| 625 | || (rField_is_Zn(r)&&(r->cf->convSingNFactoryN!=ndConvSingNFactoryN))) |
---|
[ce2576] | 626 | { |
---|
| 627 | setCharacteristic( rChar(r) ); |
---|
| 628 | CanonicalForm F( convSingPFactoryP( f,r ) ), G( convSingPFactoryP( g,r ) ); |
---|
| 629 | res = convFactoryPSingP( F / G,r ); |
---|
| 630 | } |
---|
[4c1e770] | 631 | // div is not implemented for ZZ coeffs in factory |
---|
[e72a9a] | 632 | else if (r->cf->extRing!=NULL) |
---|
[ce2576] | 633 | { |
---|
| 634 | if (rField_is_Q_a(r)) setCharacteristic( 0 ); |
---|
| 635 | else setCharacteristic( rChar(r) ); |
---|
[dd668f] | 636 | if (r->cf->extRing->qideal!=NULL) |
---|
[ce2576] | 637 | { |
---|
[dd668f] | 638 | CanonicalForm mipo=convSingPFactoryP(r->cf->extRing->qideal->m[0], |
---|
[ce2576] | 639 | r->cf->extRing); |
---|
| 640 | Variable a=rootOf(mipo); |
---|
| 641 | CanonicalForm F( convSingAPFactoryAP( f,a,r ) ), |
---|
| 642 | G( convSingAPFactoryAP( g,a,r ) ); |
---|
| 643 | res= convFactoryAPSingAP( F / G, r ); |
---|
[8b00ca] | 644 | prune (a); |
---|
[ce2576] | 645 | } |
---|
| 646 | else |
---|
| 647 | { |
---|
| 648 | CanonicalForm F( convSingTrPFactoryP( f,r ) ), G( convSingTrPFactoryP( g,r ) ); |
---|
| 649 | res= convFactoryPSingTrP( F / G,r ); |
---|
| 650 | } |
---|
| 651 | } |
---|
[3d5596] | 652 | #if 0 // not yet working |
---|
| 653 | else if (rField_is_GF()) |
---|
| 654 | { |
---|
| 655 | //Print("GF(%d^%d)\n",nfCharP,nfMinPoly[0]); |
---|
| 656 | setCharacteristic( nfCharP,nfMinPoly[0], currRing->parameter[0][0] ); |
---|
| 657 | CanonicalForm F( convSingGFFactoryGF( f ) ), G( convSingGFFactoryGF( g ) ); |
---|
| 658 | res = convFactoryGFSingGF( F / G ); |
---|
| 659 | } |
---|
| 660 | #endif |
---|
| 661 | else |
---|
| 662 | WerrorS( feNotImplemented ); |
---|
| 663 | Off(SW_RATIONAL); |
---|
| 664 | return res; |
---|
| 665 | } |
---|
| 666 | |
---|
| 667 | poly singclap_pmod ( poly f, poly g, const ring r ) |
---|
| 668 | { |
---|
| 669 | poly res=NULL; |
---|
| 670 | On(SW_RATIONAL); |
---|
[417a91a] | 671 | if (rField_is_Zp(r) || rField_is_Q(r) |
---|
| 672 | || (rField_is_Zn(r)&&(r->cf->convSingNFactoryN!=ndConvSingNFactoryN))) |
---|
[3d5596] | 673 | { |
---|
| 674 | setCharacteristic( rChar(r) ); |
---|
| 675 | CanonicalForm F( convSingPFactoryP( f,r ) ), G( convSingPFactoryP( g,r ) ); |
---|
[8f9132] | 676 | CanonicalForm Q,R; |
---|
| 677 | divrem(F,G,Q,R); |
---|
| 678 | res = convFactoryPSingP(R,r); |
---|
| 679 | //res = convFactoryPSingP( F-(F/G)*G,r ); |
---|
[3d5596] | 680 | } |
---|
[4c1e770] | 681 | // mod is not implemented for ZZ coeffs in factory |
---|
[3d5596] | 682 | else if (r->cf->extRing!=NULL) |
---|
| 683 | { |
---|
| 684 | if (rField_is_Q_a(r)) setCharacteristic( 0 ); |
---|
| 685 | else setCharacteristic( rChar(r) ); |
---|
| 686 | if (r->cf->extRing->qideal!=NULL) |
---|
| 687 | { |
---|
| 688 | CanonicalForm mipo=convSingPFactoryP(r->cf->extRing->qideal->m[0], |
---|
| 689 | r->cf->extRing); |
---|
| 690 | Variable a=rootOf(mipo); |
---|
| 691 | CanonicalForm F( convSingAPFactoryAP( f,a,r ) ), |
---|
| 692 | G( convSingAPFactoryAP( g,a,r ) ); |
---|
[8f9132] | 693 | CanonicalForm Q,R; |
---|
| 694 | divrem(F,G,Q,R); |
---|
| 695 | res = convFactoryAPSingAP(R,r); |
---|
| 696 | //res= convFactoryAPSingAP( F-(F/G)*G, r ); |
---|
[3d5596] | 697 | prune (a); |
---|
| 698 | } |
---|
| 699 | else |
---|
| 700 | { |
---|
| 701 | CanonicalForm F( convSingTrPFactoryP( f,r ) ), G( convSingTrPFactoryP( g,r ) ); |
---|
[8f9132] | 702 | CanonicalForm Q,R; |
---|
| 703 | divrem(F,G,Q,R); |
---|
| 704 | res = convFactoryPSingTrP(R,r); |
---|
| 705 | //res= convFactoryPSingTrP( F-(F/G)*G,r ); |
---|
[3d5596] | 706 | } |
---|
| 707 | } |
---|
[3426545] | 708 | #if 0 // not yet working |
---|
[ce2576] | 709 | else if (rField_is_GF()) |
---|
| 710 | { |
---|
| 711 | //Print("GF(%d^%d)\n",nfCharP,nfMinPoly[0]); |
---|
| 712 | setCharacteristic( nfCharP,nfMinPoly[0], currRing->parameter[0][0] ); |
---|
| 713 | CanonicalForm F( convSingGFFactoryGF( f ) ), G( convSingGFFactoryGF( g ) ); |
---|
| 714 | res = convFactoryGFSingGF( F / G ); |
---|
| 715 | } |
---|
[3426545] | 716 | #endif |
---|
[ce2576] | 717 | else |
---|
| 718 | WerrorS( feNotImplemented ); |
---|
[d1f231] | 719 | Off(SW_RATIONAL); |
---|
| 720 | return res; |
---|
| 721 | } |
---|
| 722 | |
---|
[9ac00d] | 723 | #if 0 |
---|
| 724 | // unused |
---|
[ce3f53c] | 725 | void singclap_divide_content ( poly f, const ring r ) |
---|
[35aab3] | 726 | { |
---|
| 727 | if ( f==NULL ) |
---|
| 728 | { |
---|
| 729 | return; |
---|
| 730 | } |
---|
| 731 | else if ( pNext( f ) == NULL ) |
---|
| 732 | { |
---|
[ce3f53c] | 733 | p_SetCoeff( f, n_Init( 1, r->cf ), r ); |
---|
[35aab3] | 734 | return; |
---|
| 735 | } |
---|
| 736 | else |
---|
| 737 | { |
---|
[ce3f53c] | 738 | if ( rField_is_Q_a(r) ) |
---|
[35aab3] | 739 | setCharacteristic( 0 ); |
---|
[ce3f53c] | 740 | else if ( rField_is_Zp_a(r) ) |
---|
| 741 | setCharacteristic( -rChar(r) ); |
---|
[35aab3] | 742 | else |
---|
| 743 | return; /* not implemented*/ |
---|
| 744 | |
---|
| 745 | CFList L; |
---|
| 746 | CanonicalForm g, h; |
---|
| 747 | poly p = pNext(f); |
---|
| 748 | |
---|
| 749 | // first attemp: find 2 smallest g: |
---|
| 750 | |
---|
| 751 | number g1=pGetCoeff(f); |
---|
| 752 | number g2=pGetCoeff(p); // p==pNext(f); |
---|
| 753 | pIter(p); |
---|
[ce3f53c] | 754 | int sz1=n_Size(g1, r->cf); |
---|
| 755 | int sz2=n_Size(g2, r->cf); |
---|
[35aab3] | 756 | if (sz1>sz2) |
---|
| 757 | { |
---|
| 758 | number gg=g1; |
---|
| 759 | g1=g2; g2=gg; |
---|
| 760 | int sz=sz1; |
---|
| 761 | sz1=sz2; sz2=sz; |
---|
| 762 | } |
---|
| 763 | while (p!=NULL) |
---|
| 764 | { |
---|
[ce3f53c] | 765 | int n_sz=n_Size(pGetCoeff(p),r->cf); |
---|
[35aab3] | 766 | if (n_sz<sz1) |
---|
| 767 | { |
---|
| 768 | sz2=sz1; |
---|
| 769 | g2=g1; |
---|
| 770 | g1=pGetCoeff(p); |
---|
| 771 | sz1=n_sz; |
---|
| 772 | if (sz1<=3) break; |
---|
| 773 | } |
---|
| 774 | else if(n_sz<sz2) |
---|
| 775 | { |
---|
| 776 | sz2=n_sz; |
---|
| 777 | g2=pGetCoeff(p); |
---|
| 778 | sz2=n_sz; |
---|
| 779 | } |
---|
| 780 | pIter(p); |
---|
| 781 | } |
---|
[7b2b05] | 782 | g = convSingPFactoryP( NUM(((fraction)g1)), r->cf->extRing ); |
---|
| 783 | g = gcd( g, convSingPFactoryP( NUM(((fraction)g2)) , r->cf->extRing)); |
---|
[35aab3] | 784 | |
---|
| 785 | // second run: gcd's |
---|
| 786 | |
---|
| 787 | p = f; |
---|
| 788 | while ( (p != NULL) && (g != 1) && ( g != 0)) |
---|
| 789 | { |
---|
[7b2b05] | 790 | h = convSingPFactoryP( NUM(((fraction)pGetCoeff(p))), r->cf->extRing ); |
---|
[35aab3] | 791 | pIter( p ); |
---|
| 792 | |
---|
| 793 | g = gcd( g, h ); |
---|
| 794 | |
---|
| 795 | L.append( h ); |
---|
| 796 | } |
---|
| 797 | if (( g == 1 ) || (g == 0)) |
---|
| 798 | { |
---|
| 799 | // pTest(f); |
---|
| 800 | return; |
---|
| 801 | } |
---|
| 802 | else |
---|
| 803 | { |
---|
| 804 | CFListIterator i; |
---|
| 805 | for ( i = L, p = f; i.hasItem(); i++, p=pNext(p) ) |
---|
| 806 | { |
---|
[7b2b05] | 807 | fraction c=(fraction)pGetCoeff(p); |
---|
| 808 | p_Delete(&(NUM(c)),r->cf->extRing); // 2nd arg used to be nacRing |
---|
| 809 | NUM(c)=convFactoryPSingP( i.getItem() / g, r->cf->extRing ); |
---|
[35aab3] | 810 | //nTest((number)c); |
---|
| 811 | //#ifdef LDEBUG |
---|
| 812 | //number cn=(number)c; |
---|
| 813 | //StringSetS(""); nWrite(nt); StringAppend(" ==> "); |
---|
[54fc4e] | 814 | //nWrite(cn);PrintS(StringEndS("\n")); // NOTE/TODO: use StringAppendS("\n"); omFree(s); |
---|
[35aab3] | 815 | //#endif |
---|
| 816 | } |
---|
| 817 | } |
---|
| 818 | // pTest(f); |
---|
| 819 | } |
---|
| 820 | } |
---|
[9ac00d] | 821 | #endif |
---|
[35aab3] | 822 | |
---|
[ce3f53c] | 823 | BOOLEAN count_Factors(ideal I, intvec *v,int j, poly &f, poly fac, const ring r) |
---|
| 824 | { |
---|
| 825 | p_Test(f,r); |
---|
| 826 | p_Test(fac,r); |
---|
[0a55e5] | 827 | int e=0; |
---|
[ce3f53c] | 828 | if (!p_IsConstantPoly(fac,r)) |
---|
[0a55e5] | 829 | { |
---|
| 830 | #ifdef FACTORIZE2_DEBUG |
---|
[ce2576] | 831 | printf("start count_Factors(%d), Fdeg=%d, factor deg=%d\n",j,p_Totaldegree(f,r),p_Totaldegree(fac,r)); |
---|
[1db880] | 832 | p_wrp(fac,r);PrintLn(); |
---|
[0a55e5] | 833 | #endif |
---|
| 834 | On(SW_RATIONAL); |
---|
| 835 | CanonicalForm F, FAC,Q,R; |
---|
| 836 | Variable a; |
---|
[417a91a] | 837 | if (rField_is_Zp(r) || rField_is_Q(r) |
---|
| 838 | || (rField_is_Zn(r)&&(r->cf->convSingNFactoryN!=ndConvSingNFactoryN))) |
---|
[ce2576] | 839 | { |
---|
| 840 | F=convSingPFactoryP( f,r ); |
---|
| 841 | FAC=convSingPFactoryP( fac,r ); |
---|
| 842 | } |
---|
[e72a9a] | 843 | else if (r->cf->extRing!=NULL) |
---|
[ce2576] | 844 | { |
---|
[dd668f] | 845 | if (r->cf->extRing->qideal!=NULL) |
---|
[ce2576] | 846 | { |
---|
[dd668f] | 847 | CanonicalForm mipo=convSingPFactoryP(r->cf->extRing->qideal->m[0], |
---|
[ce2576] | 848 | r->cf->extRing); |
---|
| 849 | a=rootOf(mipo); |
---|
| 850 | F=convSingAPFactoryAP( f,a,r ); |
---|
| 851 | FAC=convSingAPFactoryAP( fac,a,r ); |
---|
| 852 | } |
---|
| 853 | else |
---|
| 854 | { |
---|
| 855 | F=convSingTrPFactoryP( f,r ); |
---|
| 856 | FAC=convSingTrPFactoryP( fac,r ); |
---|
| 857 | } |
---|
| 858 | } |
---|
| 859 | else |
---|
| 860 | WerrorS( feNotImplemented ); |
---|
[0a55e5] | 861 | |
---|
| 862 | poly q; |
---|
| 863 | loop |
---|
| 864 | { |
---|
| 865 | Q=F; |
---|
| 866 | Q/=FAC; |
---|
| 867 | R=Q; |
---|
| 868 | R*=FAC; |
---|
| 869 | R-=F; |
---|
| 870 | if (R.isZero()) |
---|
| 871 | { |
---|
[417a91a] | 872 | if (rField_is_Zp(r) || rField_is_Q(r) |
---|
| 873 | || (rField_is_Zn(r)&&(r->cf->convSingNFactoryN!=ndConvSingNFactoryN))) |
---|
[ce2576] | 874 | { |
---|
| 875 | q = convFactoryPSingP( Q,r ); |
---|
| 876 | } |
---|
[e72a9a] | 877 | else if (r->cf->extRing!=NULL) |
---|
[ce2576] | 878 | { |
---|
[dd668f] | 879 | if (r->cf->extRing->qideal!=NULL) |
---|
[ce2576] | 880 | { |
---|
| 881 | q= convFactoryAPSingAP( Q,r ); |
---|
| 882 | } |
---|
| 883 | else |
---|
| 884 | { |
---|
| 885 | q= convFactoryPSingTrP( Q,r ); |
---|
| 886 | } |
---|
| 887 | } |
---|
[ce3f53c] | 888 | e++; p_Delete(&f,r); f=q; q=NULL; F=Q; |
---|
[0a55e5] | 889 | } |
---|
| 890 | else |
---|
| 891 | { |
---|
| 892 | break; |
---|
| 893 | } |
---|
| 894 | } |
---|
[8b00ca] | 895 | if (r->cf->extRing!=NULL) |
---|
| 896 | if (r->cf->extRing->qideal!=NULL) |
---|
| 897 | prune (a); |
---|
[0a55e5] | 898 | if (e==0) |
---|
| 899 | { |
---|
| 900 | Off(SW_RATIONAL); |
---|
| 901 | return FALSE; |
---|
| 902 | } |
---|
| 903 | } |
---|
| 904 | else e=1; |
---|
| 905 | I->m[j]=fac; |
---|
| 906 | if (v!=NULL) (*v)[j]=e; |
---|
| 907 | Off(SW_RATIONAL); |
---|
| 908 | return TRUE; |
---|
| 909 | } |
---|
| 910 | |
---|
[a3f0fea] | 911 | VAR int singclap_factorize_retry; |
---|
[a5d9313] | 912 | |
---|
[ce3f53c] | 913 | ideal singclap_factorize ( poly f, intvec ** v , int with_exps, const ring r) |
---|
[93e7e91] | 914 | /* destroys f, sets *v */ |
---|
[35aab3] | 915 | { |
---|
[ce3f53c] | 916 | p_Test(f,r); |
---|
[0a55e5] | 917 | #ifdef FACTORIZE2_DEBUG |
---|
[ce2576] | 918 | printf("singclap_factorize, degree %ld\n",p_Totaldegree(f,r)); |
---|
[0a55e5] | 919 | #endif |
---|
[35aab3] | 920 | // with_exps: 3,1 return only true factors, no exponents |
---|
| 921 | // 2 return true factors and exponents |
---|
| 922 | // 0 return coeff, factors and exponents |
---|
[ee5d91] | 923 | BOOLEAN save_errorreported=errorreported; |
---|
[35aab3] | 924 | |
---|
| 925 | ideal res=NULL; |
---|
| 926 | |
---|
| 927 | // handle factorize(0) ========================================= |
---|
| 928 | if (f==NULL) |
---|
| 929 | { |
---|
| 930 | res=idInit(1,1); |
---|
| 931 | if (with_exps!=1) |
---|
| 932 | { |
---|
| 933 | (*v)=new intvec(1); |
---|
| 934 | (**v)[0]=1; |
---|
| 935 | } |
---|
| 936 | return res; |
---|
| 937 | } |
---|
| 938 | // handle factorize(mon) ========================================= |
---|
| 939 | if (pNext(f)==NULL) |
---|
| 940 | { |
---|
| 941 | int i=0; |
---|
| 942 | int n=0; |
---|
| 943 | int e; |
---|
[ce3f53c] | 944 | for(i=rVar(r);i>0;i--) if(p_GetExp(f,i,r)!=0) n++; |
---|
[35aab3] | 945 | if (with_exps==0) n++; // with coeff |
---|
| 946 | res=idInit(si_max(n,1),1); |
---|
| 947 | switch(with_exps) |
---|
| 948 | { |
---|
| 949 | case 0: // with coef & exp. |
---|
[ce3f53c] | 950 | res->m[0]=p_NSet(n_Copy(pGetCoeff(f),r->cf),r); |
---|
[35aab3] | 951 | // no break |
---|
| 952 | case 2: // with exp. |
---|
[54ff35] | 953 | (*v)=new intvec(si_max(1,n)); |
---|
[35aab3] | 954 | (**v)[0]=1; |
---|
| 955 | // no break |
---|
| 956 | case 1: ; |
---|
[3426545] | 957 | #ifdef TEST |
---|
[35aab3] | 958 | default: ; |
---|
[3426545] | 959 | #endif |
---|
[35aab3] | 960 | } |
---|
| 961 | if (n==0) |
---|
| 962 | { |
---|
[2956e79] | 963 | if (res->m[0]==NULL) res->m[0]=p_One(r); |
---|
[35aab3] | 964 | // (**v)[0]=1; is already done |
---|
| 965 | } |
---|
[93e7e91] | 966 | else |
---|
[35aab3] | 967 | { |
---|
[ce3f53c] | 968 | for(i=rVar(r);i>0;i--) |
---|
[35aab3] | 969 | { |
---|
[ce3f53c] | 970 | e=p_GetExp(f,i,r); |
---|
[93e7e91] | 971 | if(e!=0) |
---|
| 972 | { |
---|
| 973 | n--; |
---|
[ce3f53c] | 974 | poly p=p_One(r); |
---|
| 975 | p_SetExp(p,i,1,r); |
---|
| 976 | p_Setm(p,r); |
---|
[93e7e91] | 977 | res->m[n]=p; |
---|
| 978 | if (with_exps!=1) (**v)[n]=e; |
---|
| 979 | } |
---|
[35aab3] | 980 | } |
---|
| 981 | } |
---|
[ce3f53c] | 982 | p_Delete(&f,r); |
---|
[35aab3] | 983 | return res; |
---|
| 984 | } |
---|
[ce2576] | 985 | //PrintS("S:");p_Write(f,r);PrintLn(); |
---|
[54ff35] | 986 | // use factory/libfac in general ============================== |
---|
[2956e79] | 987 | Variable dummy(-1); prune(dummy); // remove all (tmp.) extensions |
---|
[35aab3] | 988 | Off(SW_RATIONAL); |
---|
| 989 | On(SW_SYMMETRIC_FF); |
---|
| 990 | CFFList L; |
---|
| 991 | number N=NULL; |
---|
[ffdd694] | 992 | number NN=NULL; |
---|
[ce3f53c] | 993 | number old_lead_coeff=n_Copy(pGetCoeff(f), r->cf); |
---|
[35aab3] | 994 | |
---|
[8b00ca] | 995 | Variable a; |
---|
[417a91a] | 996 | if (!rField_is_Zp(r) && !rField_is_Zp_a(r) && !rField_is_Z(r) |
---|
| 997 | && !(rField_is_Zn(r) && (r->cf->convSingNFactoryN!=ndConvSingNFactoryN))) /* Q, Q(a) */ |
---|
[0a55e5] | 998 | { |
---|
| 999 | //if (f!=NULL) // already tested at start of routine |
---|
| 1000 | { |
---|
[ce3f53c] | 1001 | number n0=n_Copy(pGetCoeff(f),r->cf); |
---|
[0a55e5] | 1002 | if (with_exps==0) |
---|
[ce3f53c] | 1003 | N=n_Copy(n0,r->cf); |
---|
| 1004 | p_Cleardenom(f, r); |
---|
[ce2576] | 1005 | //after here f should not have a denominator!! |
---|
| 1006 | //PrintS("S:");p_Write(f,r);PrintLn(); |
---|
[ce3f53c] | 1007 | NN=n_Div(n0,pGetCoeff(f),r->cf); |
---|
| 1008 | n_Delete(&n0,r->cf); |
---|
[0a55e5] | 1009 | if (with_exps==0) |
---|
| 1010 | { |
---|
[ce3f53c] | 1011 | n_Delete(&N,r->cf); |
---|
| 1012 | N=n_Copy(NN,r->cf); |
---|
[0a55e5] | 1013 | } |
---|
| 1014 | } |
---|
| 1015 | } |
---|
[ce3f53c] | 1016 | else if (rField_is_Zp_a(r)) |
---|
[35aab3] | 1017 | { |
---|
[ffdd694] | 1018 | //if (f!=NULL) // already tested at start of routine |
---|
[0a55e5] | 1019 | if (singclap_factorize_retry==0) |
---|
[35aab3] | 1020 | { |
---|
[ce3f53c] | 1021 | number n0=n_Copy(pGetCoeff(f),r->cf); |
---|
[ffdd694] | 1022 | if (with_exps==0) |
---|
[ce3f53c] | 1023 | N=n_Copy(n0,r->cf); |
---|
| 1024 | p_Norm(f,r); |
---|
| 1025 | p_Cleardenom(f, r); |
---|
| 1026 | NN=n_Div(n0,pGetCoeff(f),r->cf); |
---|
| 1027 | n_Delete(&n0,r->cf); |
---|
[ffdd694] | 1028 | if (with_exps==0) |
---|
[35aab3] | 1029 | { |
---|
[ce3f53c] | 1030 | n_Delete(&N,r->cf); |
---|
| 1031 | N=n_Copy(NN,r->cf); |
---|
[35aab3] | 1032 | } |
---|
| 1033 | } |
---|
[ffdd694] | 1034 | } |
---|
[417a91a] | 1035 | if ((rField_is_Q(r) || rField_is_Zp(r) || (rField_is_Z(r))) |
---|
| 1036 | || (rField_is_Zn(r)&&(r->cf->convSingNFactoryN!=ndConvSingNFactoryN))) |
---|
[ffdd694] | 1037 | { |
---|
[ce3f53c] | 1038 | setCharacteristic( rChar(r) ); |
---|
| 1039 | CanonicalForm F( convSingPFactoryP( f,r ) ); |
---|
[34ceab] | 1040 | L = factorize( F ); |
---|
[35aab3] | 1041 | } |
---|
| 1042 | // and over Q(a) / Fp(a) |
---|
[417a91a] | 1043 | else if ((r->cf->extRing!=NULL) |
---|
| 1044 | &&(r->cf->extRing->cf->convSingNFactoryN!=ndConvSingNFactoryN)) |
---|
[35aab3] | 1045 | { |
---|
[ce2576] | 1046 | if (rField_is_Q_a (r)) setCharacteristic (0); |
---|
| 1047 | else setCharacteristic( rChar(r) ); |
---|
[dd668f] | 1048 | if (r->cf->extRing->qideal!=NULL) |
---|
[35aab3] | 1049 | { |
---|
[dd668f] | 1050 | CanonicalForm mipo=convSingPFactoryP(r->cf->extRing->qideal->m[0], |
---|
[6ccdd3a] | 1051 | r->cf->extRing); |
---|
[8b00ca] | 1052 | a=rootOf(mipo); |
---|
[ce2576] | 1053 | CanonicalForm F( convSingAPFactoryAP( f, a, r ) ); |
---|
[ce3f53c] | 1054 | if (rField_is_Zp_a(r)) |
---|
[35aab3] | 1055 | { |
---|
| 1056 | L = factorize( F, a ); |
---|
| 1057 | } |
---|
| 1058 | else |
---|
| 1059 | { |
---|
[34ceab] | 1060 | // over Q(a) |
---|
[fcf7587] | 1061 | L= factorize (F, a); |
---|
[35aab3] | 1062 | } |
---|
[2956e79] | 1063 | prune(a); |
---|
[35aab3] | 1064 | } |
---|
| 1065 | else |
---|
| 1066 | { |
---|
[ce2576] | 1067 | CanonicalForm F( convSingTrPFactoryP( f,r ) ); |
---|
[34ceab] | 1068 | L = factorize( F ); |
---|
[35aab3] | 1069 | } |
---|
| 1070 | } |
---|
| 1071 | else |
---|
| 1072 | { |
---|
| 1073 | goto notImpl; |
---|
| 1074 | } |
---|
| 1075 | { |
---|
[ce3f53c] | 1076 | poly ff=p_Copy(f,r); // a copy for the retry stuff |
---|
[35aab3] | 1077 | // the first factor should be a constant |
---|
| 1078 | if ( ! L.getFirst().factor().inCoeffDomain() ) |
---|
| 1079 | L.insert(CFFactor(1,1)); |
---|
| 1080 | // convert into ideal |
---|
| 1081 | int n = L.length(); |
---|
[91cb92] | 1082 | if (n==0) n=1; |
---|
[35aab3] | 1083 | CFFListIterator J=L; |
---|
| 1084 | int j=0; |
---|
| 1085 | if (with_exps!=1) |
---|
| 1086 | { |
---|
| 1087 | if ((with_exps==2)&&(n>1)) |
---|
| 1088 | { |
---|
| 1089 | n--; |
---|
| 1090 | J++; |
---|
| 1091 | } |
---|
| 1092 | *v = new intvec( n ); |
---|
| 1093 | } |
---|
| 1094 | res = idInit( n ,1); |
---|
| 1095 | for ( ; J.hasItem(); J++, j++ ) |
---|
| 1096 | { |
---|
| 1097 | if (with_exps!=1) (**v)[j] = J.getItem().exp(); |
---|
[417a91a] | 1098 | if (rField_is_Zp(r) || rField_is_Q(r)|| rField_is_Z(r) |
---|
| 1099 | || (rField_is_Zn(r) && r->cf->convSingNFactoryN!=ndConvSingNFactoryN)) /* Q, Fp, Z */ |
---|
[8fc55e2] | 1100 | { |
---|
[46f6af6] | 1101 | //count_Factors(res,*v,f, j, convFactoryPSingP( J.getItem().factor() ); |
---|
[ce3f53c] | 1102 | res->m[j] = convFactoryPSingP( J.getItem().factor(),r ); |
---|
[8fc55e2] | 1103 | } |
---|
[3426545] | 1104 | #if 0 |
---|
[35aab3] | 1105 | else if (rField_is_GF()) |
---|
[46f6af6] | 1106 | res->m[j] = convFactoryGFSingGF( J.getItem().factor() ); |
---|
[3426545] | 1107 | #endif |
---|
[e72a9a] | 1108 | else if (r->cf->extRing!=NULL) /* Q(a), Fp(a) */ |
---|
[35aab3] | 1109 | { |
---|
[7fe9e13] | 1110 | #ifndef SING_NDEBUG |
---|
[5d491d] | 1111 | intvec *w=NULL; |
---|
| 1112 | if (v!=NULL) w=*v; |
---|
[ea1d44c] | 1113 | #endif |
---|
[dd668f] | 1114 | if (r->cf->extRing->qideal==NULL) |
---|
[8fc55e2] | 1115 | { |
---|
[7fe9e13] | 1116 | #ifdef SING_NDEBUG |
---|
[ea1d44c] | 1117 | res->m[j]= convFactoryPSingTrP( J.getItem().factor(),r ); |
---|
| 1118 | #else |
---|
[ce2576] | 1119 | if(!count_Factors(res,w,j,ff,convFactoryPSingTrP( J.getItem().factor(),r ),r)) |
---|
[d499d1] | 1120 | { |
---|
[a86cda] | 1121 | if (w!=NULL) |
---|
[e080ab] | 1122 | (*w)[j]=1; |
---|
[ce3f53c] | 1123 | res->m[j]=p_One(r); |
---|
[d499d1] | 1124 | } |
---|
[ea1d44c] | 1125 | #endif |
---|
[8fc55e2] | 1126 | } |
---|
[35aab3] | 1127 | else |
---|
[8fc55e2] | 1128 | { |
---|
[7fe9e13] | 1129 | #ifdef SING_NDEBUG |
---|
[ea1d44c] | 1130 | res->m[j]= convFactoryAPSingAP( J.getItem().factor(),r ); |
---|
| 1131 | #else |
---|
[ce2576] | 1132 | if (!count_Factors(res,w,j,ff,convFactoryAPSingAP( J.getItem().factor(),r ),r)) |
---|
[d499d1] | 1133 | { |
---|
[a86cda] | 1134 | if (w!=NULL) |
---|
[e080ab] | 1135 | (*w)[j]=1; |
---|
[ce3f53c] | 1136 | res->m[j]=p_One(r); |
---|
[d499d1] | 1137 | } |
---|
[ea1d44c] | 1138 | #endif |
---|
[8fc55e2] | 1139 | } |
---|
[0a55e5] | 1140 | } |
---|
| 1141 | } |
---|
[8b00ca] | 1142 | if (r->cf->extRing!=NULL) |
---|
| 1143 | if (r->cf->extRing->qideal!=NULL) |
---|
| 1144 | prune (a); |
---|
[7fe9e13] | 1145 | #ifndef SING_NDEBUG |
---|
[e72a9a] | 1146 | if ((r->cf->extRing!=NULL) && (!p_IsConstantPoly(ff,r))) |
---|
[0a55e5] | 1147 | { |
---|
| 1148 | singclap_factorize_retry++; |
---|
| 1149 | if (singclap_factorize_retry<3) |
---|
| 1150 | { |
---|
| 1151 | int jj; |
---|
[3426545] | 1152 | #ifdef FACTORIZE2_DEBUG |
---|
[0a55e5] | 1153 | printf("factorize_retry\n"); |
---|
[3426545] | 1154 | #endif |
---|
[0a55e5] | 1155 | intvec *ww=NULL; |
---|
[ce3f53c] | 1156 | id_Test(res,r); |
---|
| 1157 | ideal h=singclap_factorize ( ff, &ww , with_exps, r ); |
---|
| 1158 | id_Test(h,r); |
---|
[0a55e5] | 1159 | int l=(*v)->length(); |
---|
[91cb92] | 1160 | (*v)->resize(l+ww->length()); |
---|
[0a55e5] | 1161 | for(jj=0;jj<ww->length();jj++) |
---|
[91cb92] | 1162 | (**v)[jj+l]=(*ww)[jj]; |
---|
[0a55e5] | 1163 | delete ww; |
---|
| 1164 | ideal hh=idInit(IDELEMS(res)+IDELEMS(h),1); |
---|
| 1165 | for(jj=IDELEMS(res)-1;jj>=0;jj--) |
---|
| 1166 | { |
---|
| 1167 | hh->m[jj]=res->m[jj]; |
---|
| 1168 | res->m[jj]=NULL; |
---|
| 1169 | } |
---|
| 1170 | for(jj=IDELEMS(h)-1;jj>=0;jj--) |
---|
| 1171 | { |
---|
| 1172 | hh->m[jj+IDELEMS(res)]=h->m[jj]; |
---|
| 1173 | h->m[jj]=NULL; |
---|
| 1174 | } |
---|
[ce3f53c] | 1175 | id_Delete(&res,r); |
---|
| 1176 | id_Delete(&h,r); |
---|
[0a55e5] | 1177 | res=hh; |
---|
[ce3f53c] | 1178 | id_Test(res,r); |
---|
[0a55e5] | 1179 | ff=NULL; |
---|
| 1180 | } |
---|
| 1181 | else |
---|
| 1182 | { |
---|
| 1183 | WarnS("problem with factorize"); |
---|
[3426545] | 1184 | #if 0 |
---|
[226dd7] | 1185 | pWrite(ff); |
---|
| 1186 | idShow(res); |
---|
[3426545] | 1187 | #endif |
---|
[ce3f53c] | 1188 | id_Delete(&res,r); |
---|
[226dd7] | 1189 | res=idInit(2,1); |
---|
[ce3f53c] | 1190 | res->m[0]=p_One(r); |
---|
[226dd7] | 1191 | res->m[1]=ff; ff=NULL; |
---|
[35aab3] | 1192 | } |
---|
| 1193 | } |
---|
[ea1d44c] | 1194 | #endif |
---|
[ce3f53c] | 1195 | p_Delete(&ff,r); |
---|
[35aab3] | 1196 | if (N!=NULL) |
---|
| 1197 | { |
---|
[3d1222a] | 1198 | __p_Mult_nn(res->m[0],N,r); |
---|
[ce3f53c] | 1199 | n_Delete(&N,r->cf); |
---|
[ffdd694] | 1200 | N=NULL; |
---|
[35aab3] | 1201 | } |
---|
| 1202 | // delete constants |
---|
| 1203 | if (res!=NULL) |
---|
| 1204 | { |
---|
| 1205 | int i=IDELEMS(res)-1; |
---|
| 1206 | int j=0; |
---|
| 1207 | for(;i>=0;i--) |
---|
| 1208 | { |
---|
| 1209 | if ((res->m[i]!=NULL) |
---|
| 1210 | && (pNext(res->m[i])==NULL) |
---|
[ce3f53c] | 1211 | && (p_IsConstant(res->m[i],r))) |
---|
[35aab3] | 1212 | { |
---|
| 1213 | if (with_exps!=0) |
---|
| 1214 | { |
---|
[ce3f53c] | 1215 | p_Delete(&(res->m[i]),r); |
---|
[35aab3] | 1216 | if ((v!=NULL) && ((*v)!=NULL)) |
---|
| 1217 | (**v)[i]=0; |
---|
| 1218 | j++; |
---|
| 1219 | } |
---|
| 1220 | else if (i!=0) |
---|
| 1221 | { |
---|
[d8932ed] | 1222 | while ((v!=NULL) && ((*v)!=NULL) && ((**v)[i]>1)) |
---|
| 1223 | { |
---|
[ce3f53c] | 1224 | res->m[0]=p_Mult_q(res->m[0],p_Copy(res->m[i],r),r); |
---|
[d8932ed] | 1225 | (**v)[i]--; |
---|
| 1226 | } |
---|
[ce3f53c] | 1227 | res->m[0]=p_Mult_q(res->m[0],res->m[i],r); |
---|
[35aab3] | 1228 | res->m[i]=NULL; |
---|
| 1229 | if ((v!=NULL) && ((*v)!=NULL)) |
---|
[d499d1] | 1230 | (**v)[i]=1; |
---|
[35aab3] | 1231 | j++; |
---|
| 1232 | } |
---|
| 1233 | } |
---|
| 1234 | } |
---|
| 1235 | if (j>0) |
---|
| 1236 | { |
---|
| 1237 | idSkipZeroes(res); |
---|
| 1238 | if ((v!=NULL) && ((*v)!=NULL)) |
---|
| 1239 | { |
---|
| 1240 | intvec *w=*v; |
---|
[f12cd23] | 1241 | int len=IDELEMS(res); |
---|
| 1242 | *v = new intvec( len ); |
---|
[0a55e5] | 1243 | for (i=0,j=0;i<si_min(w->length(),len);i++) |
---|
[35aab3] | 1244 | { |
---|
| 1245 | if((*w)[i]!=0) |
---|
| 1246 | { |
---|
| 1247 | (**v)[j]=(*w)[i]; j++; |
---|
| 1248 | } |
---|
| 1249 | } |
---|
| 1250 | delete w; |
---|
| 1251 | } |
---|
| 1252 | } |
---|
| 1253 | if (res->m[0]==NULL) |
---|
| 1254 | { |
---|
[ce3f53c] | 1255 | res->m[0]=p_One(r); |
---|
[35aab3] | 1256 | } |
---|
| 1257 | } |
---|
| 1258 | } |
---|
[dd668f] | 1259 | if (rField_is_Q_a(r) && (r->cf->extRing->qideal!=NULL)) |
---|
[35aab3] | 1260 | { |
---|
| 1261 | int i=IDELEMS(res)-1; |
---|
| 1262 | int stop=1; |
---|
| 1263 | if (with_exps!=0) stop=0; |
---|
| 1264 | for(;i>=stop;i--) |
---|
| 1265 | { |
---|
[ce3f53c] | 1266 | p_Norm(res->m[i],r); |
---|
[35aab3] | 1267 | } |
---|
[ce3f53c] | 1268 | if (with_exps==0) p_SetCoeff(res->m[0],old_lead_coeff,r); |
---|
| 1269 | else n_Delete(&old_lead_coeff,r->cf); |
---|
[35aab3] | 1270 | } |
---|
| 1271 | else |
---|
[ce3f53c] | 1272 | n_Delete(&old_lead_coeff,r->cf); |
---|
[ee5d91] | 1273 | errorreported=save_errorreported; |
---|
[35aab3] | 1274 | notImpl: |
---|
[2956e79] | 1275 | prune(a); |
---|
[35aab3] | 1276 | if (res==NULL) |
---|
| 1277 | WerrorS( feNotImplemented ); |
---|
[ffdd694] | 1278 | if (NN!=NULL) |
---|
[35aab3] | 1279 | { |
---|
[ce3f53c] | 1280 | n_Delete(&NN,r->cf); |
---|
[ffdd694] | 1281 | } |
---|
| 1282 | if (N!=NULL) |
---|
| 1283 | { |
---|
[ce3f53c] | 1284 | n_Delete(&N,r->cf); |
---|
[35aab3] | 1285 | } |
---|
[ce3f53c] | 1286 | if (f!=NULL) p_Delete(&f,r); |
---|
[35aab3] | 1287 | //PrintS("......S\n"); |
---|
| 1288 | return res; |
---|
| 1289 | } |
---|
[feed237] | 1290 | |
---|
[3ef9c8] | 1291 | ideal singclap_sqrfree ( poly f, intvec ** v , int with_exps, const ring r) |
---|
[0dd641] | 1292 | { |
---|
[ce3f53c] | 1293 | p_Test(f,r); |
---|
[0dd641] | 1294 | #ifdef FACTORIZE2_DEBUG |
---|
| 1295 | printf("singclap_sqrfree, degree %d\n",pTotaldegree(f)); |
---|
| 1296 | #endif |
---|
| 1297 | // with_exps: 3,1 return only true factors, no exponents |
---|
| 1298 | // 2 return true factors and exponents |
---|
| 1299 | // 0 return coeff, factors and exponents |
---|
| 1300 | BOOLEAN save_errorreported=errorreported; |
---|
| 1301 | |
---|
| 1302 | ideal res=NULL; |
---|
| 1303 | |
---|
| 1304 | // handle factorize(0) ========================================= |
---|
| 1305 | if (f==NULL) |
---|
| 1306 | { |
---|
| 1307 | res=idInit(1,1); |
---|
[c92097b] | 1308 | if (with_exps!=1 && with_exps!=3) |
---|
[3ef9c8] | 1309 | { |
---|
| 1310 | (*v)=new intvec(1); |
---|
| 1311 | (**v)[0]=1; |
---|
| 1312 | } |
---|
[0dd641] | 1313 | return res; |
---|
| 1314 | } |
---|
| 1315 | // handle factorize(mon) ========================================= |
---|
| 1316 | if (pNext(f)==NULL) |
---|
| 1317 | { |
---|
| 1318 | int i=0; |
---|
| 1319 | int n=0; |
---|
| 1320 | int e; |
---|
[ce3f53c] | 1321 | for(i=rVar(r);i>0;i--) if(p_GetExp(f,i,r)!=0) n++; |
---|
[c92097b] | 1322 | if (with_exps==0 || with_exps==3) n++; // with coeff |
---|
[0dd641] | 1323 | res=idInit(si_max(n,1),1); |
---|
[571544] | 1324 | if(with_exps!=1) |
---|
[3ef9c8] | 1325 | { |
---|
| 1326 | (*v)=new intvec(si_max(1,n)); |
---|
| 1327 | (**v)[0]=1; |
---|
| 1328 | } |
---|
[ce3f53c] | 1329 | res->m[0]=p_NSet(n_Copy(pGetCoeff(f),r->cf),r); |
---|
[0dd641] | 1330 | if (n==0) |
---|
| 1331 | { |
---|
[ce3f53c] | 1332 | res->m[0]=p_One(r); |
---|
[0dd641] | 1333 | // (**v)[0]=1; is already done |
---|
| 1334 | } |
---|
[3ef9c8] | 1335 | else |
---|
[0dd641] | 1336 | { |
---|
[3ef9c8] | 1337 | for(i=rVar(r);i>0;i--) |
---|
[0dd641] | 1338 | { |
---|
[3ef9c8] | 1339 | e=p_GetExp(f,i,r); |
---|
| 1340 | if(e!=0) |
---|
| 1341 | { |
---|
| 1342 | n--; |
---|
| 1343 | poly p=p_One(r); |
---|
| 1344 | p_SetExp(p,i,1,r); |
---|
| 1345 | p_Setm(p,r); |
---|
| 1346 | res->m[n]=p; |
---|
| 1347 | if (with_exps!=1) (**v)[n]=e; |
---|
| 1348 | } |
---|
[0dd641] | 1349 | } |
---|
| 1350 | } |
---|
[3ef9c8] | 1351 | p_Delete(&f,r); |
---|
[0dd641] | 1352 | return res; |
---|
| 1353 | } |
---|
| 1354 | //PrintS("S:");pWrite(f);PrintLn(); |
---|
| 1355 | // use factory/libfac in general ============================== |
---|
| 1356 | Off(SW_RATIONAL); |
---|
| 1357 | On(SW_SYMMETRIC_FF); |
---|
| 1358 | CFFList L; |
---|
[3ef9c8] | 1359 | number N=NULL; |
---|
| 1360 | number NN=NULL; |
---|
[f78374] | 1361 | number old_lead_coeff=n_Copy(pGetCoeff(f), r->cf); |
---|
[8b00ca] | 1362 | Variable a; |
---|
[0dd641] | 1363 | |
---|
[ce3f53c] | 1364 | if (!rField_is_Zp(r) && !rField_is_Zp_a(r)) /* Q, Q(a) */ |
---|
[0dd641] | 1365 | { |
---|
| 1366 | //if (f!=NULL) // already tested at start of routine |
---|
[3ef9c8] | 1367 | number n0=n_Copy(pGetCoeff(f),r->cf); |
---|
[c92097b] | 1368 | if (with_exps==0 || with_exps==3) |
---|
[3ef9c8] | 1369 | N=n_Copy(n0,r->cf); |
---|
| 1370 | p_Cleardenom(f, r); |
---|
| 1371 | //after here f should not have a denominator!! |
---|
| 1372 | //PrintS("S:");p_Write(f,r);PrintLn(); |
---|
| 1373 | NN=n_Div(n0,pGetCoeff(f),r->cf); |
---|
| 1374 | n_Delete(&n0,r->cf); |
---|
[c92097b] | 1375 | if (with_exps==0 || with_exps==3) |
---|
[0dd641] | 1376 | { |
---|
[3ef9c8] | 1377 | n_Delete(&N,r->cf); |
---|
| 1378 | N=n_Copy(NN,r->cf); |
---|
[0dd641] | 1379 | } |
---|
| 1380 | } |
---|
[ce3f53c] | 1381 | else if (rField_is_Zp_a(r)) |
---|
[0dd641] | 1382 | { |
---|
| 1383 | //if (f!=NULL) // already tested at start of routine |
---|
| 1384 | if (singclap_factorize_retry==0) |
---|
| 1385 | { |
---|
[3ef9c8] | 1386 | number n0=n_Copy(pGetCoeff(f),r->cf); |
---|
[c92097b] | 1387 | if (with_exps==0 || with_exps==3) |
---|
[3ef9c8] | 1388 | N=n_Copy(n0,r->cf); |
---|
[ce3f53c] | 1389 | p_Norm(f,r); |
---|
| 1390 | p_Cleardenom(f, r); |
---|
[3ef9c8] | 1391 | NN=n_Div(n0,pGetCoeff(f),r->cf); |
---|
| 1392 | n_Delete(&n0,r->cf); |
---|
[c92097b] | 1393 | if (with_exps==0 || with_exps==3) |
---|
[3ef9c8] | 1394 | { |
---|
| 1395 | n_Delete(&N,r->cf); |
---|
| 1396 | N=n_Copy(NN,r->cf); |
---|
| 1397 | } |
---|
[0dd641] | 1398 | } |
---|
| 1399 | } |
---|
[417a91a] | 1400 | if (rField_is_Q(r) || rField_is_Zp(r) |
---|
| 1401 | || (rField_is_Zn(r)&&(r->cf->convSingNFactoryN!=ndConvSingNFactoryN))) |
---|
[0dd641] | 1402 | { |
---|
[ce3f53c] | 1403 | setCharacteristic( rChar(r) ); |
---|
| 1404 | CanonicalForm F( convSingPFactoryP( f,r ) ); |
---|
[85194f2] | 1405 | L = sqrFree( F ); |
---|
[0dd641] | 1406 | } |
---|
[e72a9a] | 1407 | else if (r->cf->extRing!=NULL) |
---|
[3ef9c8] | 1408 | { |
---|
| 1409 | if (rField_is_Q_a (r)) setCharacteristic (0); |
---|
| 1410 | else setCharacteristic( rChar(r) ); |
---|
[dd668f] | 1411 | if (r->cf->extRing->qideal!=NULL) |
---|
[3ef9c8] | 1412 | { |
---|
[dd668f] | 1413 | CanonicalForm mipo=convSingPFactoryP(r->cf->extRing->qideal->m[0], |
---|
[3ef9c8] | 1414 | r->cf->extRing); |
---|
[8b00ca] | 1415 | a=rootOf(mipo); |
---|
[3ef9c8] | 1416 | CanonicalForm F( convSingAPFactoryAP( f, a, r ) ); |
---|
| 1417 | L= sqrFree (F); |
---|
| 1418 | } |
---|
| 1419 | else |
---|
| 1420 | { |
---|
| 1421 | CanonicalForm F( convSingTrPFactoryP( f,r ) ); |
---|
| 1422 | L = sqrFree( F ); |
---|
| 1423 | } |
---|
| 1424 | } |
---|
[0dd641] | 1425 | #if 0 |
---|
| 1426 | else if (rField_is_GF()) |
---|
| 1427 | { |
---|
[ce3f53c] | 1428 | int c=rChar(r); |
---|
[0dd641] | 1429 | setCharacteristic( c, primepower(c) ); |
---|
| 1430 | CanonicalForm F( convSingGFFactoryGF( f ) ); |
---|
| 1431 | if (F.isUnivariate()) |
---|
| 1432 | { |
---|
| 1433 | L = factorize( F ); |
---|
| 1434 | } |
---|
| 1435 | else |
---|
| 1436 | { |
---|
| 1437 | goto notImpl; |
---|
| 1438 | } |
---|
| 1439 | } |
---|
| 1440 | #endif |
---|
| 1441 | else |
---|
| 1442 | { |
---|
| 1443 | goto notImpl; |
---|
| 1444 | } |
---|
| 1445 | { |
---|
| 1446 | // convert into ideal |
---|
| 1447 | int n = L.length(); |
---|
| 1448 | if (n==0) n=1; |
---|
| 1449 | CFFListIterator J=L; |
---|
| 1450 | int j=0; |
---|
[3ef9c8] | 1451 | if (with_exps!=1) |
---|
| 1452 | { |
---|
| 1453 | if ((with_exps==2)&&(n>1)) |
---|
| 1454 | { |
---|
| 1455 | n--; |
---|
| 1456 | J++; |
---|
| 1457 | } |
---|
| 1458 | *v = new intvec( n ); |
---|
| 1459 | } |
---|
[c92097b] | 1460 | else if (L.getFirst().factor().inCoeffDomain() && with_exps!=3) |
---|
[3ef9c8] | 1461 | { |
---|
| 1462 | n--; |
---|
| 1463 | J++; |
---|
| 1464 | } |
---|
[0dd641] | 1465 | res = idInit( n ,1); |
---|
| 1466 | for ( ; J.hasItem(); J++, j++ ) |
---|
| 1467 | { |
---|
[c92097b] | 1468 | if (with_exps!=1 && with_exps!=3) (**v)[j] = J.getItem().exp(); |
---|
[417a91a] | 1469 | if (rField_is_Zp(r) || rField_is_Q(r) |
---|
| 1470 | || (rField_is_Zn(r)&&(r->cf->convSingNFactoryN!=ndConvSingNFactoryN))) |
---|
[3ef9c8] | 1471 | res->m[j] = convFactoryPSingP( J.getItem().factor(),r ); |
---|
[e72a9a] | 1472 | else if (r->cf->extRing!=NULL) /* Q(a), Fp(a) */ |
---|
[3ef9c8] | 1473 | { |
---|
[dd668f] | 1474 | if (r->cf->extRing->qideal==NULL) |
---|
[3ef9c8] | 1475 | res->m[j]=convFactoryPSingTrP( J.getItem().factor(),r ); |
---|
| 1476 | else |
---|
| 1477 | res->m[j]=convFactoryAPSingAP( J.getItem().factor(),r ); |
---|
| 1478 | } |
---|
[0dd641] | 1479 | } |
---|
| 1480 | if (res->m[0]==NULL) |
---|
| 1481 | { |
---|
[ce3f53c] | 1482 | res->m[0]=p_One(r); |
---|
[0dd641] | 1483 | } |
---|
[f78374] | 1484 | if (N!=NULL) |
---|
| 1485 | { |
---|
[3d1222a] | 1486 | __p_Mult_nn(res->m[0],N,r); |
---|
[f78374] | 1487 | n_Delete(&N,r->cf); |
---|
| 1488 | N=NULL; |
---|
| 1489 | } |
---|
[0dd641] | 1490 | } |
---|
[8b00ca] | 1491 | if (r->cf->extRing!=NULL) |
---|
| 1492 | if (r->cf->extRing->qideal!=NULL) |
---|
| 1493 | prune (a); |
---|
[dd668f] | 1494 | if (rField_is_Q_a(r) && (r->cf->extRing->qideal!=NULL)) |
---|
[f78374] | 1495 | { |
---|
| 1496 | int i=IDELEMS(res)-1; |
---|
| 1497 | int stop=1; |
---|
[c92097b] | 1498 | if (with_exps!=0 || with_exps==3) stop=0; |
---|
[f78374] | 1499 | for(;i>=stop;i--) |
---|
| 1500 | { |
---|
| 1501 | p_Norm(res->m[i],r); |
---|
| 1502 | } |
---|
[c92097b] | 1503 | if (with_exps==0 || with_exps==3) p_SetCoeff(res->m[0],old_lead_coeff,r); |
---|
[f78374] | 1504 | else n_Delete(&old_lead_coeff,r->cf); |
---|
| 1505 | } |
---|
| 1506 | else |
---|
| 1507 | n_Delete(&old_lead_coeff,r->cf); |
---|
[ce3f53c] | 1508 | p_Delete(&f,r); |
---|
[0dd641] | 1509 | errorreported=save_errorreported; |
---|
| 1510 | notImpl: |
---|
| 1511 | if (res==NULL) |
---|
| 1512 | WerrorS( feNotImplemented ); |
---|
[f78374] | 1513 | if (NN!=NULL) |
---|
| 1514 | { |
---|
| 1515 | n_Delete(&NN,r->cf); |
---|
| 1516 | } |
---|
| 1517 | if (N!=NULL) |
---|
| 1518 | { |
---|
| 1519 | n_Delete(&N,r->cf); |
---|
| 1520 | } |
---|
[0dd641] | 1521 | return res; |
---|
| 1522 | } |
---|
[ce3f53c] | 1523 | |
---|
[67c1dc] | 1524 | matrix singclap_irrCharSeries ( ideal I, const ring r) |
---|
[35aab3] | 1525 | { |
---|
| 1526 | if (idIs0(I)) return mpNew(1,1); |
---|
| 1527 | |
---|
| 1528 | // for now there is only the possibility to handle polynomials over |
---|
| 1529 | // Q and Fp ... |
---|
| 1530 | matrix res=NULL; |
---|
| 1531 | int i; |
---|
| 1532 | Off(SW_RATIONAL); |
---|
| 1533 | On(SW_SYMMETRIC_FF); |
---|
| 1534 | CFList L; |
---|
| 1535 | ListCFList LL; |
---|
[417a91a] | 1536 | if (rField_is_Q(r) || rField_is_Zp(r) |
---|
| 1537 | || (rField_is_Zn(r)&&(r->cf->convSingNFactoryN!=ndConvSingNFactoryN))) |
---|
[35aab3] | 1538 | { |
---|
[ce3f53c] | 1539 | setCharacteristic( rChar(r) ); |
---|
[35aab3] | 1540 | for(i=0;i<IDELEMS(I);i++) |
---|
| 1541 | { |
---|
| 1542 | poly p=I->m[i]; |
---|
| 1543 | if (p!=NULL) |
---|
| 1544 | { |
---|
[ce3f53c] | 1545 | p=p_Copy(p,r); |
---|
| 1546 | p_Cleardenom(p, r); |
---|
| 1547 | L.append(convSingPFactoryP(p,r)); |
---|
[2956e79] | 1548 | p_Delete(&p,r); |
---|
[35aab3] | 1549 | } |
---|
| 1550 | } |
---|
| 1551 | } |
---|
| 1552 | // and over Q(a) / Fp(a) |
---|
[44f78d3] | 1553 | else if (nCoeff_is_transExt (r->cf)) |
---|
[35aab3] | 1554 | { |
---|
[44f78d3] | 1555 | setCharacteristic( rChar(r) ); |
---|
[35aab3] | 1556 | for(i=0;i<IDELEMS(I);i++) |
---|
| 1557 | { |
---|
| 1558 | poly p=I->m[i]; |
---|
| 1559 | if (p!=NULL) |
---|
| 1560 | { |
---|
[ce3f53c] | 1561 | p=p_Copy(p,r); |
---|
| 1562 | p_Cleardenom(p, r); |
---|
| 1563 | L.append(convSingTrPFactoryP(p,r)); |
---|
[2956e79] | 1564 | p_Delete(&p,r); |
---|
[35aab3] | 1565 | } |
---|
| 1566 | } |
---|
| 1567 | } |
---|
| 1568 | else |
---|
| 1569 | { |
---|
| 1570 | WerrorS( feNotImplemented ); |
---|
| 1571 | return res; |
---|
| 1572 | } |
---|
| 1573 | |
---|
| 1574 | // a very bad work-around --- FIX IT in libfac |
---|
| 1575 | // should be fixed as of 2001/6/27 |
---|
| 1576 | int tries=0; |
---|
| 1577 | int m,n; |
---|
| 1578 | ListIterator<CFList> LLi; |
---|
| 1579 | loop |
---|
| 1580 | { |
---|
[d083a8] | 1581 | LL=irrCharSeries(L); |
---|
[35aab3] | 1582 | m= LL.length(); // Anzahl Zeilen |
---|
| 1583 | n=0; |
---|
| 1584 | for ( LLi = LL; LLi.hasItem(); LLi++ ) |
---|
| 1585 | { |
---|
| 1586 | n = si_max(LLi.getItem().length(),n); |
---|
| 1587 | } |
---|
| 1588 | if ((m!=0) && (n!=0)) break; |
---|
| 1589 | tries++; |
---|
| 1590 | if (tries>=5) break; |
---|
| 1591 | } |
---|
| 1592 | if ((m==0) || (n==0)) |
---|
| 1593 | { |
---|
| 1594 | Warn("char_series returns %d x %d matrix from %d input polys (%d)", |
---|
| 1595 | m,n,IDELEMS(I)+1,LL.length()); |
---|
[fc2746] | 1596 | iiWriteMatrix((matrix)I,"I",2,r,0); |
---|
[35aab3] | 1597 | m=si_max(m,1); |
---|
| 1598 | n=si_max(n,1); |
---|
| 1599 | } |
---|
| 1600 | res=mpNew(m,n); |
---|
| 1601 | CFListIterator Li; |
---|
| 1602 | for ( m=1, LLi = LL; LLi.hasItem(); LLi++, m++ ) |
---|
| 1603 | { |
---|
| 1604 | for (n=1, Li = LLi.getItem(); Li.hasItem(); Li++, n++) |
---|
| 1605 | { |
---|
[417a91a] | 1606 | if (rField_is_Q(r) || rField_is_Zp(r) |
---|
| 1607 | || (rField_is_Zn(r)&&(r->cf->convSingNFactoryN!=ndConvSingNFactoryN))) |
---|
[ce3f53c] | 1608 | MATELEM(res,m,n)=convFactoryPSingP(Li.getItem(),r); |
---|
[35aab3] | 1609 | else |
---|
[ce3f53c] | 1610 | MATELEM(res,m,n)=convFactoryPSingTrP(Li.getItem(),r); |
---|
[35aab3] | 1611 | } |
---|
| 1612 | } |
---|
| 1613 | Off(SW_RATIONAL); |
---|
| 1614 | return res; |
---|
[67c1dc] | 1615 | } |
---|
[35aab3] | 1616 | |
---|
[3aa9b6] | 1617 | char* singclap_neworder ( ideal I, const ring r) |
---|
[35aab3] | 1618 | { |
---|
| 1619 | int i; |
---|
| 1620 | Off(SW_RATIONAL); |
---|
| 1621 | On(SW_SYMMETRIC_FF); |
---|
| 1622 | CFList L; |
---|
[417a91a] | 1623 | if (rField_is_Q(r) || rField_is_Zp(r) |
---|
| 1624 | || (rField_is_Zn(r)&&(r->cf->convSingNFactoryN!=ndConvSingNFactoryN))) |
---|
[35aab3] | 1625 | { |
---|
[ce3f53c] | 1626 | setCharacteristic( rChar(r) ); |
---|
[35aab3] | 1627 | for(i=0;i<IDELEMS(I);i++) |
---|
| 1628 | { |
---|
[44f78d3] | 1629 | poly p=I->m[i]; |
---|
| 1630 | if (p!=NULL) |
---|
| 1631 | { |
---|
| 1632 | p=p_Copy(p,r); |
---|
| 1633 | p_Cleardenom(p, r); |
---|
| 1634 | L.append(convSingPFactoryP(p,r)); |
---|
| 1635 | } |
---|
[35aab3] | 1636 | } |
---|
| 1637 | } |
---|
| 1638 | // and over Q(a) / Fp(a) |
---|
[44f78d3] | 1639 | else if (nCoeff_is_transExt (r->cf)) |
---|
[35aab3] | 1640 | { |
---|
[44f78d3] | 1641 | setCharacteristic( rChar(r) ); |
---|
[35aab3] | 1642 | for(i=0;i<IDELEMS(I);i++) |
---|
| 1643 | { |
---|
[44f78d3] | 1644 | poly p=I->m[i]; |
---|
| 1645 | if (p!=NULL) |
---|
| 1646 | { |
---|
| 1647 | p=p_Copy(p,r); |
---|
| 1648 | p_Cleardenom(p, r); |
---|
| 1649 | L.append(convSingTrPFactoryP(p,r)); |
---|
| 1650 | } |
---|
[35aab3] | 1651 | } |
---|
| 1652 | } |
---|
| 1653 | else |
---|
| 1654 | { |
---|
| 1655 | WerrorS( feNotImplemented ); |
---|
| 1656 | return NULL; |
---|
| 1657 | } |
---|
| 1658 | |
---|
| 1659 | List<int> IL=neworderint(L); |
---|
| 1660 | ListIterator<int> Li; |
---|
| 1661 | StringSetS(""); |
---|
| 1662 | Li = IL; |
---|
[ce3f53c] | 1663 | int offs=rPar(r); |
---|
| 1664 | int* mark=(int*)omAlloc0((rVar(r)+offs)*sizeof(int)); |
---|
| 1665 | int cnt=rVar(r)+offs; |
---|
[35aab3] | 1666 | loop |
---|
| 1667 | { |
---|
| 1668 | if(! Li.hasItem()) break; |
---|
| 1669 | BOOLEAN done=TRUE; |
---|
| 1670 | i=Li.getItem()-1; |
---|
| 1671 | mark[i]=1; |
---|
| 1672 | if (i<offs) |
---|
| 1673 | { |
---|
| 1674 | done=FALSE; |
---|
[ce3f53c] | 1675 | //StringAppendS(r->parameter[i]); |
---|
[35aab3] | 1676 | } |
---|
| 1677 | else |
---|
| 1678 | { |
---|
[ce3f53c] | 1679 | StringAppendS(r->names[i-offs]); |
---|
[35aab3] | 1680 | } |
---|
| 1681 | Li++; |
---|
| 1682 | cnt--; |
---|
| 1683 | if(cnt==0) break; |
---|
| 1684 | if (done) StringAppendS(","); |
---|
| 1685 | } |
---|
[ce3f53c] | 1686 | for(i=0;i<rVar(r)+offs;i++) |
---|
[35aab3] | 1687 | { |
---|
| 1688 | BOOLEAN done=TRUE; |
---|
| 1689 | if(mark[i]==0) |
---|
| 1690 | { |
---|
| 1691 | if (i<offs) |
---|
| 1692 | { |
---|
| 1693 | done=FALSE; |
---|
[ce3f53c] | 1694 | //StringAppendS(r->parameter[i]); |
---|
[35aab3] | 1695 | } |
---|
| 1696 | else |
---|
| 1697 | { |
---|
[ce3f53c] | 1698 | StringAppendS(r->names[i-offs]); |
---|
[35aab3] | 1699 | } |
---|
| 1700 | cnt--; |
---|
| 1701 | if(cnt==0) break; |
---|
| 1702 | if (done) StringAppendS(","); |
---|
| 1703 | } |
---|
| 1704 | } |
---|
[538512] | 1705 | char * s=StringEndS(); |
---|
[35aab3] | 1706 | if (s[strlen(s)-1]==',') s[strlen(s)-1]='\0'; |
---|
| 1707 | return s; |
---|
[67c1dc] | 1708 | } |
---|
[35aab3] | 1709 | |
---|
[ce3f53c] | 1710 | poly singclap_det( const matrix m, const ring s ) |
---|
[35aab3] | 1711 | { |
---|
| 1712 | int r=m->rows(); |
---|
| 1713 | if (r!=m->cols()) |
---|
| 1714 | { |
---|
| 1715 | Werror("det of %d x %d matrix",r,m->cols()); |
---|
| 1716 | return NULL; |
---|
| 1717 | } |
---|
| 1718 | poly res=NULL; |
---|
[abb4787] | 1719 | CFMatrix M(r,r); |
---|
| 1720 | int i,j; |
---|
| 1721 | for(i=r;i>0;i--) |
---|
[35aab3] | 1722 | { |
---|
[abb4787] | 1723 | for(j=r;j>0;j--) |
---|
[35aab3] | 1724 | { |
---|
[abb4787] | 1725 | M(i,j)=convSingPFactoryP(MATELEM(m,i,j),s); |
---|
[35aab3] | 1726 | } |
---|
| 1727 | } |
---|
[abb4787] | 1728 | res= convFactoryPSingP( determinant(M,r),s ) ; |
---|
[35aab3] | 1729 | Off(SW_RATIONAL); |
---|
| 1730 | return res; |
---|
| 1731 | } |
---|
| 1732 | |
---|
[2e4ec14] | 1733 | int singclap_det_i( intvec * m, const ring /*r*/) |
---|
[35aab3] | 1734 | { |
---|
[9d5ba2] | 1735 | // assume( r == currRing ); // Anything else is not guaranted to work! |
---|
[54fc4e] | 1736 | |
---|
[9d5ba2] | 1737 | setCharacteristic( 0 ); // ? |
---|
[35aab3] | 1738 | CFMatrix M(m->rows(),m->cols()); |
---|
| 1739 | int i,j; |
---|
| 1740 | for(i=m->rows();i>0;i--) |
---|
| 1741 | { |
---|
| 1742 | for(j=m->cols();j>0;j--) |
---|
| 1743 | { |
---|
| 1744 | M(i,j)=IMATELEM(*m,i,j); |
---|
| 1745 | } |
---|
| 1746 | } |
---|
[ce3f53c] | 1747 | int res= convFactoryISingI( determinant(M,m->rows()) ) ; |
---|
[24a9587] | 1748 | return res; |
---|
| 1749 | } |
---|
| 1750 | |
---|
| 1751 | number singclap_det_bi( bigintmat * m, const coeffs cf) |
---|
| 1752 | { |
---|
| 1753 | assume(m->basecoeffs()==cf); |
---|
| 1754 | CFMatrix M(m->rows(),m->cols()); |
---|
| 1755 | int i,j; |
---|
| 1756 | BOOLEAN setchar=TRUE; |
---|
| 1757 | for(i=m->rows();i>0;i--) |
---|
| 1758 | { |
---|
| 1759 | for(j=m->cols();j>0;j--) |
---|
| 1760 | { |
---|
[ca90c60] | 1761 | M(i,j)=n_convSingNFactoryN(BIMATELEM(*m,i,j),setchar,cf); |
---|
[24a9587] | 1762 | setchar=FALSE; |
---|
| 1763 | } |
---|
| 1764 | } |
---|
[ca90c60] | 1765 | number res=n_convFactoryNSingN( determinant(M,m->rows()),cf ) ; |
---|
[35aab3] | 1766 | return res; |
---|
| 1767 | } |
---|
[c74d6a] | 1768 | |
---|
[2072126] | 1769 | #ifdef HAVE_NTL |
---|
[ce3f53c] | 1770 | matrix singntl_HNF(matrix m, const ring s ) |
---|
[9d9078] | 1771 | { |
---|
[af8863] | 1772 | int r=m->rows(); |
---|
| 1773 | if (r!=m->cols()) |
---|
| 1774 | { |
---|
| 1775 | Werror("HNF of %d x %d matrix",r,m->cols()); |
---|
| 1776 | return NULL; |
---|
| 1777 | } |
---|
[54fc4e] | 1778 | |
---|
[42e2db] | 1779 | matrix res=mpNew(r,r); |
---|
[54fc4e] | 1780 | |
---|
[ce3f53c] | 1781 | if (rField_is_Q(s)) |
---|
[af8863] | 1782 | { |
---|
[9d9078] | 1783 | |
---|
[af8863] | 1784 | CFMatrix M(r,r); |
---|
| 1785 | int i,j; |
---|
| 1786 | for(i=r;i>0;i--) |
---|
| 1787 | { |
---|
| 1788 | for(j=r;j>0;j--) |
---|
| 1789 | { |
---|
[ce3f53c] | 1790 | M(i,j)=convSingPFactoryP(MATELEM(m,i,j),s ); |
---|
[af8863] | 1791 | } |
---|
| 1792 | } |
---|
| 1793 | CFMatrix *MM=cf_HNF(M); |
---|
| 1794 | for(i=r;i>0;i--) |
---|
| 1795 | { |
---|
| 1796 | for(j=r;j>0;j--) |
---|
[9d9078] | 1797 | { |
---|
[ce3f53c] | 1798 | MATELEM(res,i,j)=convFactoryPSingP((*MM)(i,j),s); |
---|
[af8863] | 1799 | } |
---|
| 1800 | } |
---|
| 1801 | delete MM; |
---|
| 1802 | } |
---|
| 1803 | return res; |
---|
| 1804 | } |
---|
[895b31c] | 1805 | |
---|
[d085e06] | 1806 | intvec* singntl_HNF(intvec* m) |
---|
[af8863] | 1807 | { |
---|
| 1808 | int r=m->rows(); |
---|
| 1809 | if (r!=m->cols()) |
---|
| 1810 | { |
---|
| 1811 | Werror("HNF of %d x %d matrix",r,m->cols()); |
---|
| 1812 | return NULL; |
---|
| 1813 | } |
---|
| 1814 | setCharacteristic( 0 ); |
---|
| 1815 | CFMatrix M(r,r); |
---|
| 1816 | int i,j; |
---|
| 1817 | for(i=r;i>0;i--) |
---|
| 1818 | { |
---|
| 1819 | for(j=r;j>0;j--) |
---|
| 1820 | { |
---|
| 1821 | M(i,j)=IMATELEM(*m,i,j); |
---|
| 1822 | } |
---|
| 1823 | } |
---|
| 1824 | CFMatrix *MM=cf_HNF(M); |
---|
| 1825 | intvec *mm=ivCopy(m); |
---|
| 1826 | for(i=r;i>0;i--) |
---|
| 1827 | { |
---|
| 1828 | for(j=r;j>0;j--) |
---|
| 1829 | { |
---|
| 1830 | IMATELEM(*mm,i,j)=convFactoryISingI((*MM)(i,j)); |
---|
| 1831 | } |
---|
| 1832 | } |
---|
| 1833 | delete MM; |
---|
| 1834 | return mm; |
---|
| 1835 | } |
---|
[895b31c] | 1836 | |
---|
[503bfc1] | 1837 | bigintmat* singntl_HNF(bigintmat* b) |
---|
| 1838 | { |
---|
| 1839 | int r=b->rows(); |
---|
| 1840 | if (r!=b->cols()) |
---|
| 1841 | { |
---|
| 1842 | Werror("HNF of %d x %d matrix",r,b->cols()); |
---|
| 1843 | return NULL; |
---|
| 1844 | } |
---|
| 1845 | setCharacteristic( 0 ); |
---|
| 1846 | CFMatrix M(r,r); |
---|
| 1847 | int i,j; |
---|
| 1848 | for(i=r;i>0;i--) |
---|
| 1849 | { |
---|
| 1850 | for(j=r;j>0;j--) |
---|
| 1851 | { |
---|
| 1852 | M(i,j)=n_convSingNFactoryN(BIMATELEM(*b,i,j),FALSE,b->basecoeffs()); |
---|
| 1853 | } |
---|
| 1854 | } |
---|
| 1855 | CFMatrix *MM=cf_HNF(M); |
---|
| 1856 | bigintmat *mm=bimCopy(b); |
---|
| 1857 | for(i=r;i>0;i--) |
---|
| 1858 | { |
---|
| 1859 | for(j=r;j>0;j--) |
---|
| 1860 | { |
---|
| 1861 | BIMATELEM(*mm,i,j)=n_convFactoryNSingN((*MM)(i,j),b->basecoeffs()); |
---|
| 1862 | } |
---|
| 1863 | } |
---|
| 1864 | delete MM; |
---|
| 1865 | return mm; |
---|
| 1866 | } |
---|
| 1867 | |
---|
[ce3f53c] | 1868 | matrix singntl_LLL(matrix m, const ring s ) |
---|
[9d9078] | 1869 | { |
---|
[9c0b20a] | 1870 | int r=m->rows(); |
---|
| 1871 | int c=m->cols(); |
---|
[42e2db] | 1872 | matrix res=mpNew(r,c); |
---|
[ce3f53c] | 1873 | if (rField_is_Q(s)) |
---|
[9c0b20a] | 1874 | { |
---|
| 1875 | CFMatrix M(r,c); |
---|
| 1876 | int i,j; |
---|
| 1877 | for(i=r;i>0;i--) |
---|
| 1878 | { |
---|
| 1879 | for(j=c;j>0;j--) |
---|
| 1880 | { |
---|
[ce3f53c] | 1881 | M(i,j)=convSingPFactoryP(MATELEM(m,i,j),s); |
---|
[9c0b20a] | 1882 | } |
---|
| 1883 | } |
---|
| 1884 | CFMatrix *MM=cf_LLL(M); |
---|
| 1885 | for(i=r;i>0;i--) |
---|
| 1886 | { |
---|
| 1887 | for(j=c;j>0;j--) |
---|
[9d9078] | 1888 | { |
---|
[ce3f53c] | 1889 | MATELEM(res,i,j)=convFactoryPSingP((*MM)(i,j),s); |
---|
[9c0b20a] | 1890 | } |
---|
| 1891 | } |
---|
| 1892 | delete MM; |
---|
| 1893 | } |
---|
| 1894 | return res; |
---|
| 1895 | } |
---|
[895b31c] | 1896 | |
---|
[d085e06] | 1897 | intvec* singntl_LLL(intvec* m) |
---|
[9c0b20a] | 1898 | { |
---|
| 1899 | int r=m->rows(); |
---|
| 1900 | int c=m->cols(); |
---|
| 1901 | setCharacteristic( 0 ); |
---|
| 1902 | CFMatrix M(r,c); |
---|
| 1903 | int i,j; |
---|
| 1904 | for(i=r;i>0;i--) |
---|
| 1905 | { |
---|
[a002227] | 1906 | for(j=c;j>0;j--) |
---|
[9c0b20a] | 1907 | { |
---|
| 1908 | M(i,j)=IMATELEM(*m,i,j); |
---|
| 1909 | } |
---|
| 1910 | } |
---|
| 1911 | CFMatrix *MM=cf_LLL(M); |
---|
| 1912 | intvec *mm=ivCopy(m); |
---|
| 1913 | for(i=r;i>0;i--) |
---|
| 1914 | { |
---|
| 1915 | for(j=c;j>0;j--) |
---|
| 1916 | { |
---|
| 1917 | IMATELEM(*mm,i,j)=convFactoryISingI((*MM)(i,j)); |
---|
| 1918 | } |
---|
| 1919 | } |
---|
| 1920 | delete MM; |
---|
| 1921 | return mm; |
---|
| 1922 | } |
---|
[3426545] | 1923 | |
---|
[d2fc749] | 1924 | ideal singclap_absFactorize ( poly f, ideal & mipos, intvec ** exps, int & numFactors, const ring r) |
---|
[3426545] | 1925 | { |
---|
| 1926 | p_Test(f, r); |
---|
| 1927 | |
---|
| 1928 | ideal res=NULL; |
---|
| 1929 | |
---|
| 1930 | int offs = rPar(r); |
---|
| 1931 | if (f==NULL) |
---|
| 1932 | { |
---|
| 1933 | res= idInit (1, 1); |
---|
| 1934 | mipos= idInit (1, 1); |
---|
| 1935 | mipos->m[0]= convFactoryPSingTrP (Variable (offs), r); //overkill |
---|
| 1936 | (*exps)=new intvec (1); |
---|
| 1937 | (**exps)[0]= 1; |
---|
| 1938 | numFactors= 0; |
---|
| 1939 | return res; |
---|
| 1940 | } |
---|
| 1941 | CanonicalForm F( convSingTrPFactoryP( f, r) ); |
---|
| 1942 | |
---|
[09dbf3] | 1943 | bool isRat= isOn (SW_RATIONAL); |
---|
| 1944 | if (!isRat) |
---|
| 1945 | On (SW_RATIONAL); |
---|
| 1946 | |
---|
[3426545] | 1947 | CFAFList absFactors= absFactorize (F); |
---|
| 1948 | |
---|
| 1949 | int n= absFactors.length(); |
---|
| 1950 | *exps=new intvec (n); |
---|
| 1951 | |
---|
| 1952 | res= idInit (n, 1); |
---|
| 1953 | |
---|
| 1954 | mipos= idInit (n, 1); |
---|
| 1955 | |
---|
| 1956 | Variable x= Variable (offs); |
---|
| 1957 | Variable alpha; |
---|
| 1958 | int i= 0; |
---|
| 1959 | numFactors= 0; |
---|
| 1960 | int count; |
---|
| 1961 | CFAFListIterator iter= absFactors; |
---|
| 1962 | CanonicalForm lead= iter.getItem().factor(); |
---|
| 1963 | if (iter.getItem().factor().inCoeffDomain()) |
---|
| 1964 | { |
---|
| 1965 | i++; |
---|
| 1966 | iter++; |
---|
| 1967 | } |
---|
| 1968 | for (; iter.hasItem(); iter++, i++) |
---|
| 1969 | { |
---|
| 1970 | (**exps)[i]= iter.getItem().exp(); |
---|
| 1971 | alpha= iter.getItem().minpoly().mvar(); |
---|
| 1972 | if (iter.getItem().minpoly().isOne()) |
---|
| 1973 | lead /= power (bCommonDen (iter.getItem().factor()), iter.getItem().exp()); |
---|
| 1974 | else |
---|
| 1975 | lead /= power (power (bCommonDen (iter.getItem().factor()), degree (iter.getItem().minpoly())), iter.getItem().exp()); |
---|
| 1976 | res->m[i]= convFactoryPSingTrP (replacevar (iter.getItem().factor()*bCommonDen (iter.getItem().factor()), alpha, x), r); |
---|
| 1977 | if (iter.getItem().minpoly().isOne()) |
---|
| 1978 | { |
---|
| 1979 | count= iter.getItem().exp(); |
---|
| 1980 | mipos->m[i]= convFactoryPSingTrP (x,r); |
---|
| 1981 | } |
---|
| 1982 | else |
---|
| 1983 | { |
---|
| 1984 | count= iter.getItem().exp()*degree (iter.getItem().minpoly()); |
---|
| 1985 | mipos->m[i]= convFactoryPSingTrP (replacevar (iter.getItem().minpoly(), alpha, x), r); |
---|
| 1986 | } |
---|
[8b00ca] | 1987 | if (!iter.getItem().minpoly().isOne()) |
---|
| 1988 | prune (alpha); |
---|
[3426545] | 1989 | numFactors += count; |
---|
| 1990 | } |
---|
| 1991 | if (!isRat) |
---|
| 1992 | Off (SW_RATIONAL); |
---|
| 1993 | |
---|
| 1994 | (**exps)[0]= 1; |
---|
| 1995 | res->m[0]= convFactoryPSingTrP (lead, r); |
---|
| 1996 | mipos->m[0]= convFactoryPSingTrP (x, r); |
---|
| 1997 | return res; |
---|
| 1998 | } |
---|
| 1999 | |
---|
[102a906] | 2000 | #else |
---|
| 2001 | matrix singntl_HNF(matrix m, const ring s ) |
---|
| 2002 | { |
---|
| 2003 | WerrorS("NTL missing"); |
---|
| 2004 | return NULL; |
---|
| 2005 | } |
---|
| 2006 | |
---|
| 2007 | intvec* singntl_HNF(intvec* m) |
---|
| 2008 | { |
---|
| 2009 | WerrorS("NTL missing"); |
---|
| 2010 | return NULL; |
---|
| 2011 | } |
---|
| 2012 | |
---|
| 2013 | matrix singntl_LLL(matrix m, const ring s ) |
---|
| 2014 | { |
---|
| 2015 | WerrorS("NTL missing"); |
---|
| 2016 | return NULL; |
---|
| 2017 | } |
---|
| 2018 | |
---|
| 2019 | intvec* singntl_LLL(intvec* m) |
---|
| 2020 | { |
---|
| 2021 | WerrorS("NTL missing"); |
---|
| 2022 | return NULL; |
---|
| 2023 | } |
---|
| 2024 | |
---|
| 2025 | ideal singclap_absFactorize ( poly f, ideal & mipos, intvec ** exps, int & numFactors, const ring r) |
---|
| 2026 | { |
---|
| 2027 | WerrorS("NTL missing"); |
---|
| 2028 | return NULL; |
---|
| 2029 | } |
---|
| 2030 | |
---|
[3426545] | 2031 | #endif /* HAVE_NTL */ |
---|
[c74d6a] | 2032 | |
---|