[35aab3] | 1 | // emacs edit mode for this file is -*- C++ -*- |
---|
| 2 | /**************************************** |
---|
| 3 | * Computer Algebra System SINGULAR * |
---|
| 4 | ****************************************/ |
---|
[341696] | 5 | // $Id$ |
---|
[35aab3] | 6 | /* |
---|
| 7 | * ABSTRACT: convert data between Singular and factory |
---|
| 8 | */ |
---|
| 9 | |
---|
| 10 | |
---|
[22a09d] | 11 | #include "config.h" |
---|
[abb4787] | 12 | #include <misc/auxiliary.h> |
---|
[fea2af] | 13 | |
---|
[35aab3] | 14 | #ifdef HAVE_FACTORY |
---|
| 15 | #define SI_DONT_HAVE_GLOBAL_VARS |
---|
[22a09d] | 16 | #include <factory/factory.h> |
---|
| 17 | |
---|
| 18 | #include <omalloc/omalloc.h> |
---|
[abb4787] | 19 | #include <coeffs/coeffs.h> |
---|
[ce2576] | 20 | #include <coeffs/longrat.h> |
---|
| 21 | #include <coeffs/modulop.h> |
---|
[abb4787] | 22 | #include <polys/monomials/p_polys.h> |
---|
| 23 | #include <polys/sbuckets.h> |
---|
| 24 | #include <polys/clapconv.h> |
---|
| 25 | |
---|
[ce2576] | 26 | #include "simpleideals.h" |
---|
| 27 | |
---|
| 28 | #define TRANSEXT_PRIVATES |
---|
| 29 | #include <polys/ext_fields/transext.h> |
---|
| 30 | |
---|
[5d68a6] | 31 | void out_cf(char *s1,const CanonicalForm &f,char *s2); |
---|
[35aab3] | 32 | |
---|
[46f6af6] | 33 | static void conv_RecPP ( const CanonicalForm & f, int * exp, sBucket_pt result, ring r ); |
---|
[35aab3] | 34 | |
---|
[ce2576] | 35 | static void convRecTrP ( const CanonicalForm & f, int * exp, poly & result, int offs, const ring r ); |
---|
| 36 | |
---|
| 37 | static void convRecGFGF ( const CanonicalForm & f, int * exp, poly & result ); |
---|
| 38 | |
---|
[aaf310] | 39 | static number convFactoryNSingAN( const CanonicalForm &f, const ring r); |
---|
[35aab3] | 40 | |
---|
[ab33f6e] | 41 | poly convFactoryPSingP ( const CanonicalForm & f, const ring r ) |
---|
[35aab3] | 42 | { |
---|
[6620d75] | 43 | int n = rVar(r)+1; |
---|
[35aab3] | 44 | /* ASSERT( level( f ) <= pVariables, "illegal number of variables" ); */ |
---|
[46f6af6] | 45 | int * exp = (int*)omAlloc0(n*sizeof(int)); |
---|
| 46 | sBucket_pt result_bucket=sBucketCreate(r); |
---|
| 47 | conv_RecPP( f, exp, result_bucket, r ); |
---|
| 48 | poly result; int dummy; |
---|
| 49 | sBucketDestroyMerge(result_bucket,&result,&dummy); |
---|
| 50 | omFreeSize((ADDRESS)exp,n*sizeof(int)); |
---|
[35aab3] | 51 | return result; |
---|
| 52 | } |
---|
| 53 | |
---|
[46f6af6] | 54 | static void conv_RecPP ( const CanonicalForm & f, int * exp, sBucket_pt result, ring r ) |
---|
[35aab3] | 55 | { |
---|
[46f6af6] | 56 | if (f.isZero()) |
---|
[35aab3] | 57 | return; |
---|
| 58 | if ( ! f.inCoeffDomain() ) |
---|
| 59 | { |
---|
| 60 | int l = f.level(); |
---|
| 61 | for ( CFIterator i = f; i.hasTerms(); i++ ) |
---|
| 62 | { |
---|
| 63 | exp[l] = i.exp(); |
---|
| 64 | conv_RecPP( i.coeff(), exp, result, r ); |
---|
| 65 | } |
---|
| 66 | exp[l] = 0; |
---|
| 67 | } |
---|
| 68 | else |
---|
| 69 | { |
---|
| 70 | poly term = p_Init(r); |
---|
| 71 | pNext( term ) = NULL; |
---|
| 72 | for ( int i = 1; i <= r->N; i++ ) |
---|
[ce2576] | 73 | p_SetExp( term, i, exp[i], r); |
---|
[abb4787] | 74 | pGetCoeff( term )=r->cf->convFactoryNSingN(f, r->cf); |
---|
[35aab3] | 75 | p_Setm( term, r ); |
---|
[abb4787] | 76 | if ( n_IsZero(pGetCoeff(term), r->cf) ) |
---|
[35aab3] | 77 | { |
---|
| 78 | p_Delete(&term,r); |
---|
| 79 | } |
---|
| 80 | else |
---|
| 81 | { |
---|
[46f6af6] | 82 | sBucket_Merge_p(result,term,1); |
---|
[35aab3] | 83 | } |
---|
| 84 | } |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | |
---|
[64a88e] | 88 | CanonicalForm convSingPFactoryP( poly p, const ring r ) |
---|
[35aab3] | 89 | { |
---|
| 90 | CanonicalForm result = 0; |
---|
[6620d75] | 91 | int e, n = rVar(r); |
---|
[abb4787] | 92 | BOOLEAN setChar=TRUE; |
---|
[35aab3] | 93 | |
---|
[abb4787] | 94 | while ( p!=NULL ) |
---|
[35aab3] | 95 | { |
---|
| 96 | CanonicalForm term; |
---|
[abb4787] | 97 | term=r->cf->convSingNFactoryN(pGetCoeff( p ),setChar, r->cf); |
---|
| 98 | if (errorreported) break; |
---|
| 99 | setChar=FALSE; |
---|
[46f6af6] | 100 | for ( int i = n; i >0; i-- ) |
---|
[35aab3] | 101 | { |
---|
[6620d75] | 102 | if ( (e = p_GetExp( p, i, r)) != 0 ) |
---|
[ce2576] | 103 | term *= power( Variable( i ), e ); |
---|
| 104 | } |
---|
| 105 | result += term; |
---|
| 106 | pIter( p ); |
---|
| 107 | } |
---|
| 108 | return result; |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | int convFactoryISingI( const CanonicalForm & f) |
---|
| 112 | { |
---|
| 113 | if (!f.isImm()) WerrorS("int overflow in det"); |
---|
| 114 | return f.intval(); |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | CanonicalForm convSingAPFactoryAP ( poly p , const Variable & a, const ring r) |
---|
| 118 | { |
---|
| 119 | CanonicalForm result = 0; |
---|
| 120 | int e, n = r-> N; |
---|
| 121 | int off=rPar(r); |
---|
| 122 | |
---|
| 123 | On(SW_RATIONAL); |
---|
| 124 | while ( p!=NULL) |
---|
| 125 | { |
---|
| 126 | CanonicalForm term=convSingAFactoryA(((poly)p_GetCoeff(p, r->cf->extRing)),a, r); |
---|
| 127 | for ( int i = 1; i <= n; i++ ) |
---|
| 128 | { |
---|
| 129 | if ( (e = p_GetExp( p, i, r )) != 0 ) |
---|
| 130 | term *= power( Variable( i + off), e ); |
---|
[35aab3] | 131 | } |
---|
| 132 | result += term; |
---|
[3d6c38] | 133 | pIter( p ); |
---|
[35aab3] | 134 | } |
---|
| 135 | return result; |
---|
| 136 | } |
---|
| 137 | |
---|
[ce2576] | 138 | static void |
---|
| 139 | convRecAP_R ( const CanonicalForm & f, int * exp, poly & result, int par_start, int var_start, const ring r) ; |
---|
| 140 | |
---|
| 141 | poly convFactoryAPSingAP_R ( const CanonicalForm & f, int par_start, int var_start, const ring r ) |
---|
| 142 | { |
---|
| 143 | int n = rVar(r)+rPar(r)+1; |
---|
| 144 | int * exp = (int *)omAlloc0(n*sizeof(int)); |
---|
| 145 | poly result = NULL; |
---|
| 146 | convRecAP_R( f, exp, result,par_start, var_start, r ); |
---|
| 147 | omFreeSize((ADDRESS)exp,n*sizeof(int)); |
---|
| 148 | return result; |
---|
| 149 | } |
---|
| 150 | |
---|
| 151 | poly convFactoryAPSingAP ( const CanonicalForm & f, const ring r ) |
---|
| 152 | { |
---|
| 153 | return convFactoryAPSingAP_R(f,0,rPar(r),r); |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | static void convRecAP_R ( const CanonicalForm & f, int * exp, poly & result, int par_start, int var_start, const ring r ) |
---|
| 157 | { |
---|
| 158 | if (f.isZero()) |
---|
| 159 | return; |
---|
| 160 | if ( ! f.inCoeffDomain() ) |
---|
| 161 | { |
---|
| 162 | int l = f.level(); |
---|
| 163 | for ( CFIterator i = f; i.hasTerms(); i++ ) |
---|
| 164 | { |
---|
| 165 | exp[l] = i.exp(); |
---|
| 166 | convRecAP_R( i.coeff(), exp, result, par_start, var_start, r); |
---|
| 167 | } |
---|
| 168 | exp[l] = 0; |
---|
| 169 | } |
---|
| 170 | else |
---|
| 171 | { |
---|
| 172 | poly z=(poly)convFactoryASingA( f,r ); |
---|
| 173 | if (z!=NULL) |
---|
| 174 | { |
---|
| 175 | poly term = p_Init(r); |
---|
| 176 | pNext( term ) = NULL; |
---|
| 177 | int i; |
---|
| 178 | for ( i = rVar(r); i>0 ; i-- ) |
---|
| 179 | p_SetExp( term, i , exp[i+var_start],r); |
---|
| 180 | //if (rRing_has_Comp(currRing->extRing)) p_SetComp(term, 0, currRing->extRing); // done by pInit |
---|
| 181 | if (par_start==0) |
---|
| 182 | { |
---|
| 183 | for ( i = 1; i <= var_start; i++ ) |
---|
| 184 | //z->e[i-1]+=exp[i]; |
---|
| 185 | p_AddExp(z,i,exp[i],r->cf->extRing); |
---|
| 186 | } |
---|
| 187 | else |
---|
| 188 | { |
---|
| 189 | for ( i = par_start+1; i <= var_start+rPar(r); i++ ) |
---|
| 190 | //z->e[i-1]+=exp[i]; |
---|
| 191 | p_AddExp(z,i,exp[i-par_start],r->cf->extRing); |
---|
| 192 | } |
---|
| 193 | pGetCoeff(term)=(number)ALLOC0_RNUMBER(); |
---|
| 194 | p_GetCoeff(term, r->cf->extRing)=(number) z; |
---|
| 195 | p_Setm( term,r ); |
---|
| 196 | result = p_Add_q( result, term, r ); |
---|
| 197 | } |
---|
| 198 | } |
---|
| 199 | } |
---|
| 200 | |
---|
| 201 | CanonicalForm convSingAFactoryA ( poly p , const Variable & a, const ring r ) |
---|
[35aab3] | 202 | { |
---|
| 203 | CanonicalForm result = 0; |
---|
| 204 | int e; |
---|
| 205 | |
---|
| 206 | while ( p!=NULL ) |
---|
| 207 | { |
---|
| 208 | CanonicalForm term; |
---|
[ce2576] | 209 | if ( rField_is_Zp_a(r) ) |
---|
| 210 | { |
---|
| 211 | term = n_Int( p_GetCoeff( p, r->cf->extRing ), r->cf->extRing->cf ); |
---|
| 212 | } |
---|
| 213 | else |
---|
| 214 | { |
---|
| 215 | if ( SR_HDL(p_GetCoeff( p, r->cf->extRing )) & SR_INT ) |
---|
| 216 | term = SR_TO_INT(p_GetCoeff( p, r->cf->extRing )) ; |
---|
| 217 | else |
---|
| 218 | { |
---|
| 219 | if ( p_GetCoeff( p, r->cf->extRing )->s == 3 ) |
---|
| 220 | { |
---|
[8b46459] | 221 | mpz_t dummy; |
---|
| 222 | mpz_init_set( dummy, (p_GetCoeff( p,r->cf->extRing )->z) ); |
---|
[ce2576] | 223 | term = make_cf( dummy ); |
---|
| 224 | } |
---|
| 225 | else |
---|
| 226 | { |
---|
| 227 | // assume s==0 or s==1 |
---|
[8b46459] | 228 | mpz_t num, den; |
---|
[ce2576] | 229 | On(SW_RATIONAL); |
---|
[8b46459] | 230 | mpz_init_set( num, (p_GetCoeff( p, r->cf->extRing )->z) ); |
---|
| 231 | mpz_init_set( den, (p_GetCoeff( p, r->cf->extRing )->n) ); |
---|
[ce2576] | 232 | term = make_cf( num, den, ( p_GetCoeff( p, r->cf->extRing )->s != 1 )); |
---|
| 233 | } |
---|
| 234 | } |
---|
| 235 | } |
---|
| 236 | if ( (e = p_GetExp( p, 1, r->cf->extRing )) != 0 ) |
---|
| 237 | term *= power( a , e ); |
---|
[35aab3] | 238 | result += term; |
---|
[ce2576] | 239 | p = pNext( p ); |
---|
[35aab3] | 240 | } |
---|
| 241 | return result; |
---|
| 242 | } |
---|
| 243 | |
---|
[ce2576] | 244 | static number convFactoryNSingAN( const CanonicalForm &f, const ring r) |
---|
| 245 | { |
---|
| 246 | if ( f.isImm() ) |
---|
| 247 | return n_Init( f.intval(), r->cf->extRing->cf); |
---|
| 248 | else |
---|
| 249 | { |
---|
| 250 | number z=ALLOC_RNUMBER(); |
---|
| 251 | #if defined(LDEBUG) |
---|
| 252 | z->debug=123456; |
---|
| 253 | #endif |
---|
| 254 | gmp_numerator( f, z->z ); |
---|
| 255 | if ( f.den().isOne() ) |
---|
| 256 | { |
---|
| 257 | z->s = 3; |
---|
| 258 | } |
---|
| 259 | else |
---|
| 260 | { |
---|
| 261 | gmp_denominator( f, z->n ); |
---|
| 262 | z->s = 0; |
---|
| 263 | nlNormalize(z,r->cf->extRing->cf); |
---|
| 264 | } |
---|
| 265 | /*#ifdef LDEBUG |
---|
| 266 | nlTest(z,r->cf->extRing->cf); |
---|
| 267 | #endif*/ |
---|
| 268 | return z; |
---|
| 269 | } |
---|
| 270 | } |
---|
| 271 | |
---|
| 272 | poly convFactoryASingA ( const CanonicalForm & f, const ring r ) |
---|
[35aab3] | 273 | { |
---|
[aaf310] | 274 | poly a=NULL; |
---|
| 275 | poly t; |
---|
[35aab3] | 276 | for( CFIterator i=f; i.hasTerms(); i++) |
---|
| 277 | { |
---|
[ce2576] | 278 | t= p_Init (r->cf->extRing); |
---|
| 279 | p_GetCoeff(t, r->cf->extRing)= convFactoryNSingAN( i.coeff(), r ); |
---|
| 280 | if (n_IsZero(p_GetCoeff(t,r->cf->extRing),r->cf->extRing->cf)) |
---|
[35aab3] | 281 | { |
---|
[ce2576] | 282 | p_Delete(&t,r->cf->extRing); |
---|
[35aab3] | 283 | } |
---|
| 284 | else |
---|
| 285 | { |
---|
[ce2576] | 286 | p_SetExp(t,1,i.exp(),r->cf->extRing); |
---|
| 287 | a=p_Add_q(a,t,r->cf->extRing); |
---|
| 288 | } |
---|
| 289 | } |
---|
| 290 | if (a!=NULL) |
---|
| 291 | { |
---|
[dd668f] | 292 | if (r->cf->extRing->qideal->m[0]!=NULL) |
---|
[ce2576] | 293 | { |
---|
[dd668f] | 294 | poly l=r->cf->extRing->qideal->m[0]; |
---|
[ce2576] | 295 | if (p_GetExp(a,1,r->cf->extRing) >= p_GetExp(l,1,r->cf->extRing)) |
---|
| 296 | a = p_PolyDiv (a, l, FALSE, r->cf->extRing); |
---|
| 297 | } |
---|
| 298 | } |
---|
| 299 | return a; |
---|
| 300 | } |
---|
| 301 | |
---|
| 302 | CanonicalForm convSingTrPFactoryP ( poly p, const ring r ) |
---|
| 303 | { |
---|
| 304 | CanonicalForm result = 0; |
---|
| 305 | int e, n = rVar(r); |
---|
| 306 | int offs = rPar(r); |
---|
| 307 | |
---|
| 308 | while ( p!=NULL ) |
---|
| 309 | { |
---|
| 310 | n_Normalize(p_GetCoeff(p, r), r->cf); |
---|
| 311 | CanonicalForm term=convSingPFactoryP(NUM(p_GetCoeff(p, r)),r->cf->extRing); |
---|
| 312 | |
---|
| 313 | if ((DEN(p_GetCoeff(p,r))!=NULL) |
---|
| 314 | && (!errorreported)) |
---|
| 315 | { |
---|
| 316 | WerrorS("conversion error: denominator!= 1"); |
---|
[a89a115] | 317 | } |
---|
[ce2576] | 318 | |
---|
| 319 | for ( int i = n; i > 0; i-- ) |
---|
| 320 | { |
---|
| 321 | if ( (e = p_GetExp( p, i,r )) != 0 ) |
---|
| 322 | term = term * power( Variable( i + offs ), e ); |
---|
| 323 | } |
---|
| 324 | result += term; |
---|
| 325 | p = pNext( p ); |
---|
[35aab3] | 326 | } |
---|
[ce2576] | 327 | return result; |
---|
[35aab3] | 328 | } |
---|
| 329 | |
---|
[ce2576] | 330 | poly convFactoryPSingTrP ( const CanonicalForm & f, const ring r ) |
---|
| 331 | { |
---|
| 332 | int n = rVar(r)+1; |
---|
| 333 | int * exp = (int*)omAlloc0(n*sizeof(int)); |
---|
| 334 | poly result = NULL; |
---|
| 335 | convRecTrP( f, exp, result , rPar(r), r ); |
---|
| 336 | omFreeSize((ADDRESS)exp,n*sizeof(int)); |
---|
| 337 | return result; |
---|
| 338 | } |
---|
[35aab3] | 339 | |
---|
[ce2576] | 340 | static void |
---|
| 341 | convRecTrP ( const CanonicalForm & f, int * exp, poly & result , int offs, const ring r) |
---|
[35aab3] | 342 | { |
---|
[ce2576] | 343 | if (f.isZero()) |
---|
| 344 | return; |
---|
| 345 | if ( f.level() > offs ) |
---|
| 346 | { |
---|
| 347 | int l = f.level(); |
---|
| 348 | for ( CFIterator i = f; i.hasTerms(); i++ ) |
---|
| 349 | { |
---|
| 350 | exp[l-offs] = i.exp(); |
---|
| 351 | convRecTrP( i.coeff(), exp, result, offs, r ); |
---|
| 352 | } |
---|
| 353 | exp[l-offs] = 0; |
---|
| 354 | } |
---|
| 355 | else |
---|
| 356 | { |
---|
| 357 | poly term = p_Init(r); |
---|
| 358 | pNext( term ) = NULL; |
---|
| 359 | for ( int i = rVar(r); i>0; i-- ) |
---|
| 360 | p_SetExp( term, i ,exp[i], r); |
---|
| 361 | //if (rRing_has_Comp(currRing)) p_SetComp(term, 0, currRing); // done by pInit |
---|
| 362 | pGetCoeff(term) = ALLOC0_RNUMBER(); // Q!? |
---|
| 363 | NUM(pGetCoeff(term))=convFactoryPSingP( f, r->cf->extRing ); |
---|
| 364 | p_Setm( term,r ); |
---|
| 365 | result = p_Add_q( result, term,r ); |
---|
| 366 | } |
---|
[35aab3] | 367 | } |
---|
[ce2576] | 368 | |
---|
| 369 | #if 0 |
---|
| 370 | CanonicalForm |
---|
| 371 | convSingGFFactoryGF( poly p ) |
---|
| 372 | { |
---|
| 373 | CanonicalForm result=CanonicalForm(0); |
---|
| 374 | int e, n = pVariables; |
---|
| 375 | |
---|
| 376 | while ( p != NULL ) |
---|
| 377 | { |
---|
| 378 | CanonicalForm term; |
---|
| 379 | term = make_cf_from_gf( (int)(long)pGetCoeff( p ) ); |
---|
| 380 | //int * A=(int *)&term; |
---|
| 381 | //Print("term=%x, == 0 ?: %d\n",*A, term.isZero()); |
---|
| 382 | for ( int i = 1; i <= n; i++ ) |
---|
| 383 | { |
---|
| 384 | if ( (e = pGetExp( p, i )) != 0 ) |
---|
| 385 | term *= power( Variable( i ), e ); |
---|
| 386 | } |
---|
| 387 | result += term; |
---|
| 388 | p = pNext( p ); |
---|
| 389 | } |
---|
| 390 | return result; |
---|
| 391 | } |
---|
| 392 | |
---|
| 393 | poly |
---|
| 394 | convFactoryGFSingGF ( const CanonicalForm & f ) |
---|
| 395 | { |
---|
| 396 | // cerr << " f = " << f << endl; |
---|
| 397 | int n = pVariables+1; |
---|
| 398 | /* ASSERT( level( f ) <= pVariables, "illegal number of variables" ); */ |
---|
| 399 | int * exp = (int*)omAlloc0(n*sizeof(int)); |
---|
| 400 | poly result = NULL; |
---|
| 401 | convRecGFGF( f, exp, result ); |
---|
| 402 | omFreeSize((ADDRESS)exp,n*sizeof(int)); |
---|
| 403 | return result; |
---|
| 404 | } |
---|
| 405 | |
---|
| 406 | static void |
---|
| 407 | convRecGFGF ( const CanonicalForm & f, int * exp, poly & result ) |
---|
| 408 | { |
---|
| 409 | if (f.isZero()) |
---|
| 410 | return; |
---|
| 411 | if ( ! f.inCoeffDomain() ) |
---|
| 412 | { |
---|
| 413 | int l = f.level(); |
---|
| 414 | for ( CFIterator i = f; i.hasTerms(); i++ ) |
---|
| 415 | { |
---|
| 416 | exp[l] = i.exp(); |
---|
| 417 | convRecGFGF( i.coeff(), exp, result ); |
---|
| 418 | } |
---|
| 419 | exp[l] = 0; |
---|
| 420 | } |
---|
| 421 | else |
---|
| 422 | { |
---|
| 423 | poly term = pInit(); |
---|
| 424 | pNext( term ) = NULL; |
---|
| 425 | for ( int i = 1; i <= pVariables; i++ ) |
---|
| 426 | pSetExp( term, i, exp[i]); |
---|
| 427 | //if (rRing_has_Comp(currRing)) p_SetComp(term, 0, currRing); // done by pInit |
---|
| 428 | pGetCoeff( term ) = (number) gf_value (f); |
---|
| 429 | pSetm( term ); |
---|
| 430 | result = pAdd( result, term ); |
---|
| 431 | } |
---|
| 432 | } |
---|
| 433 | |
---|
| 434 | #endif |
---|
[fea2af] | 435 | #endif /* HAVE_FACTORY */ |
---|