[2f5b71] | 1 | /**************************************** |
---|
| 2 | * Computer Algebra System SINGULAR * |
---|
| 3 | ****************************************/ |
---|
| 4 | /* |
---|
| 5 | * ABSTRACT - the mapping of polynomials to other rings |
---|
| 6 | */ |
---|
[696bde] | 7 | #define TRANSEXT_PRIVATES |
---|
[2f5b71] | 8 | |
---|
[16f511] | 9 | #ifdef HAVE_CONFIG_H |
---|
[ba5e9e] | 10 | #include "singularconfig.h" |
---|
[16f511] | 11 | #endif /* HAVE_CONFIG_H */ |
---|
[b1dfaf] | 12 | #include <kernel/mod2.h> |
---|
[cf4154] | 13 | #include <omalloc/omalloc.h> |
---|
| 14 | |
---|
[0fb34ba] | 15 | #include <coeffs/numbers.h> |
---|
[5c71ae1] | 16 | #include <coeffs/coeffs.h> |
---|
| 17 | |
---|
[0fb34ba] | 18 | #include <polys/monomials/ring.h> |
---|
[5c71ae1] | 19 | #include <polys/monomials/maps.h> |
---|
[0fb34ba] | 20 | #include <polys/matpol.h> |
---|
| 21 | #include <polys/prCopy.h> |
---|
[696bde] | 22 | #include <polys/ext_fields/transext.h> |
---|
[2f5b71] | 23 | |
---|
[5c71ae1] | 24 | //#include <libpolys/polys/ext_fields/longtrans.h> |
---|
[cf4154] | 25 | // #include <kernel/longalg.h> |
---|
| 26 | |
---|
| 27 | #include <kernel/febase.h> |
---|
| 28 | #include <kernel/kstd1.h> |
---|
| 29 | |
---|
[5c71ae1] | 30 | #include "maps_ip.h" |
---|
| 31 | #include "ipid.h" |
---|
| 32 | |
---|
| 33 | |
---|
[cf4154] | 34 | #include "lists.h" |
---|
| 35 | #include "tok.h" |
---|
| 36 | |
---|
[5c71ae1] | 37 | /* debug output: Tok2Cmdname in maApplyFetch*/ |
---|
[83a1714] | 38 | #include "ipshell.h" |
---|
[5c71ae1] | 39 | |
---|
[509ea41] | 40 | // define this if you want to use the fast_map routine for mapping ideals |
---|
[6d7113] | 41 | //#define FAST_MAP |
---|
[509ea41] | 42 | |
---|
| 43 | #ifdef FAST_MAP |
---|
[0fb34ba] | 44 | #include <polys/monomials/maps.h> |
---|
[509ea41] | 45 | #endif |
---|
| 46 | |
---|
| 47 | |
---|
[2f5b71] | 48 | /*2 |
---|
| 49 | * maps the expression w to res, |
---|
| 50 | * switch what: MAP_CMD: use theMap for mapping, N for preimage ring |
---|
| 51 | * //FETCH_CMD: use pOrdPoly for mapping |
---|
| 52 | * IMAP_CMD: use perm for mapping, N for preimage ring |
---|
| 53 | * default: map only poly-structures, |
---|
| 54 | * use perm and par_perm, N and P, |
---|
| 55 | */ |
---|
| 56 | BOOLEAN maApplyFetch(int what,map theMap,leftv res, leftv w, ring preimage_r, |
---|
| 57 | int *perm, int *par_perm, int P, nMapFunc nMap) |
---|
| 58 | { |
---|
| 59 | int i; |
---|
| 60 | int N = preimage_r->N; |
---|
[83a1714] | 61 | #if 0 |
---|
| 62 | Print("N=%d what=%s ",N,Tok2Cmdname(what)); |
---|
| 63 | if (perm!=NULL) for(i=1;i<=N;i++) Print("%d -> %d ",i,perm[i]); |
---|
| 64 | PrintS("\n"); |
---|
| 65 | Print("P=%d ",P); |
---|
| 66 | if (par_perm!=NULL) for(i=0;i<P;i++) Print("%d -> %d ",i,par_perm[i]); |
---|
| 67 | PrintS("\n"); |
---|
| 68 | #endif |
---|
| 69 | |
---|
[2f5b71] | 70 | void *data=w->Data(); |
---|
| 71 | res->rtyp = w->rtyp; |
---|
| 72 | switch (w->rtyp) |
---|
| 73 | { |
---|
| 74 | case NUMBER_CMD: |
---|
| 75 | if (P!=0) |
---|
| 76 | { |
---|
[83a1714] | 77 | // WerrorS("Sorry 'napPermNumber' was lost in the refactoring process (due to Frank): needs to be fixed"); |
---|
| 78 | // return TRUE; |
---|
| 79 | #if 1 |
---|
| 80 | // poly n_PermNumber(const number z, const int *par_perm, const int OldPar, const ring src, const ring dst); |
---|
| 81 | res->data= (void *) n_PermNumber((number)data, par_perm, P, preimage_r, currRing); |
---|
[e92b07] | 82 | #endif |
---|
[2f5b71] | 83 | res->rtyp=POLY_CMD; |
---|
[3b1d9a] | 84 | if (nCoeff_is_Extension(currRing->cf)) |
---|
| 85 | res->data=(void *)p_MinPolyNormalize((poly)res->data, currRing); |
---|
[9d68fd] | 86 | pTest((poly) res->data); |
---|
[2f5b71] | 87 | } |
---|
| 88 | else |
---|
| 89 | { |
---|
[9d68fd] | 90 | assume( nMap != NULL ); |
---|
| 91 | |
---|
| 92 | number a = nMap((number)data, preimage_r->cf, currRing->cf); |
---|
| 93 | |
---|
| 94 | |
---|
[3b1d9a] | 95 | if (nCoeff_is_Extension(currRing->cf)) |
---|
[2f5b71] | 96 | { |
---|
[83a1714] | 97 | n_Normalize(a, currRing->cf); // ??? |
---|
| 98 | /* |
---|
| 99 | number a = (number)res->data; |
---|
[9d68fd] | 100 | number one = nInit(1); |
---|
[83a1714] | 101 | number product = nMult(a, one ); |
---|
[9d68fd] | 102 | nDelete(&one); |
---|
| 103 | nDelete(&a); |
---|
[ca0d043] | 104 | res->data=(void *)product; |
---|
[83a1714] | 105 | */ |
---|
[2f5b71] | 106 | } |
---|
| 107 | #ifdef LDEBUG |
---|
[83a1714] | 108 | n_Test(a, currRing->cf); |
---|
[2f5b71] | 109 | #endif |
---|
[83a1714] | 110 | res->data=(void *)a; |
---|
| 111 | |
---|
[2f5b71] | 112 | } |
---|
| 113 | break; |
---|
| 114 | case POLY_CMD: |
---|
| 115 | case VECTOR_CMD: |
---|
[3b1d9a] | 116 | if ((what==FETCH_CMD)&& (preimage_r->cf==currRing->cf)) |
---|
| 117 | res->data=(void *)prCopyR( (poly)data, preimage_r, currRing); |
---|
[2f5b71] | 118 | else |
---|
[805db88] | 119 | if ( (what==IMAP_CMD) || /*(*/ (what==FETCH_CMD) /*)*/) /* && (nMap!=nCopy)*/ |
---|
[83a1714] | 120 | res->data=(void *)p_PermPoly((poly)data,perm,preimage_r,currRing, nMap,par_perm,P); |
---|
[2f5b71] | 121 | else /*if (what==MAP_CMD)*/ |
---|
| 122 | { |
---|
[9d68fd] | 123 | p_Test((poly)data,preimage_r); |
---|
[2f5b71] | 124 | matrix s=mpNew(N,maMaxDeg_P((poly)data, preimage_r)); |
---|
[5c71ae1] | 125 | res->data=(void *)maEval(theMap, (poly)data, preimage_r, nMap, (ideal)s, currRing); |
---|
[2f5b71] | 126 | idDelete((ideal *)&s); |
---|
| 127 | } |
---|
[3b1d9a] | 128 | if (nCoeff_is_Extension(currRing->cf)) |
---|
| 129 | res->data=(void *)p_MinPolyNormalize((poly)res->data, currRing); |
---|
[2f5b71] | 130 | pTest((poly)res->data); |
---|
| 131 | break; |
---|
| 132 | case MODUL_CMD: |
---|
| 133 | case MATRIX_CMD: |
---|
| 134 | case IDEAL_CMD: |
---|
| 135 | case MAP_CMD: |
---|
| 136 | { |
---|
| 137 | int C=((matrix)data)->cols(); |
---|
| 138 | int R; |
---|
| 139 | if (w->rtyp==MAP_CMD) R=1; |
---|
| 140 | else R=((matrix)data)->rows(); |
---|
| 141 | matrix m=mpNew(R,C); |
---|
| 142 | char *tmpR=NULL; |
---|
| 143 | if(w->rtyp==MAP_CMD) |
---|
| 144 | { |
---|
| 145 | tmpR=((map)data)->preimage; |
---|
| 146 | ((matrix)data)->rank=((matrix)data)->rows(); |
---|
| 147 | } |
---|
[3b1d9a] | 148 | if ((what==FETCH_CMD)&& (preimage_r->cf == currRing->cf)) |
---|
[2f5b71] | 149 | { |
---|
| 150 | for (i=R*C-1;i>=0;i--) |
---|
| 151 | { |
---|
[3b1d9a] | 152 | m->m[i]=prCopyR(((ideal)data)->m[i], preimage_r, currRing); |
---|
[2f5b71] | 153 | pTest(m->m[i]); |
---|
| 154 | } |
---|
| 155 | } |
---|
| 156 | else |
---|
[805db88] | 157 | if ( (what==IMAP_CMD) || /*(*/ (what==FETCH_CMD) /*)*/) /* && (nMap!=nCopy)*/ |
---|
[2f5b71] | 158 | { |
---|
| 159 | for (i=R*C-1;i>=0;i--) |
---|
| 160 | { |
---|
[3b1d9a] | 161 | m->m[i]=p_PermPoly(((ideal)data)->m[i],perm,preimage_r,currRing, |
---|
[9d68fd] | 162 | nMap,par_perm,P); |
---|
[2f5b71] | 163 | pTest(m->m[i]); |
---|
| 164 | } |
---|
| 165 | } |
---|
| 166 | else /* if(what==MAP_CMD) */ |
---|
| 167 | { |
---|
| 168 | matrix s=mpNew(N,maMaxDeg_Ma((ideal)data,preimage_r)); |
---|
| 169 | for (i=R*C-1;i>=0;i--) |
---|
| 170 | { |
---|
[5c71ae1] | 171 | m->m[i]=maEval(theMap, ((ideal)data)->m[i], preimage_r, nMap, (ideal)s, currRing); |
---|
[2f5b71] | 172 | pTest(m->m[i]); |
---|
| 173 | } |
---|
| 174 | idDelete((ideal *)&s); |
---|
| 175 | } |
---|
[3b1d9a] | 176 | if (nCoeff_is_Extension(currRing->cf)) |
---|
[2f5b71] | 177 | { |
---|
| 178 | for (i=R*C-1;i>=0;i--) |
---|
| 179 | { |
---|
[3b1d9a] | 180 | m->m[i]=p_MinPolyNormalize(m->m[i], currRing); |
---|
[2f5b71] | 181 | pTest(m->m[i]); |
---|
| 182 | } |
---|
| 183 | } |
---|
| 184 | if(w->rtyp==MAP_CMD) |
---|
| 185 | { |
---|
| 186 | ((map)data)->preimage=tmpR; |
---|
| 187 | ((map)m)->preimage=omStrDup(tmpR); |
---|
| 188 | } |
---|
| 189 | else |
---|
| 190 | { |
---|
| 191 | m->rank=((matrix)data)->rank; |
---|
| 192 | } |
---|
| 193 | res->data=(char *)m; |
---|
| 194 | idTest((ideal) m); |
---|
| 195 | break; |
---|
| 196 | } |
---|
| 197 | |
---|
| 198 | case LIST_CMD: |
---|
| 199 | { |
---|
| 200 | lists l=(lists)data; |
---|
| 201 | lists ml=(lists)omAllocBin(slists_bin); |
---|
| 202 | ml->Init(l->nr+1); |
---|
| 203 | for(i=0;i<=l->nr;i++) |
---|
| 204 | { |
---|
| 205 | if (((l->m[i].rtyp>BEGIN_RING)&&(l->m[i].rtyp<END_RING)) |
---|
| 206 | ||(l->m[i].rtyp==LIST_CMD)) |
---|
| 207 | { |
---|
| 208 | if (maApplyFetch(what,theMap,&ml->m[i],&l->m[i], |
---|
| 209 | preimage_r,perm,par_perm,P,nMap)) |
---|
| 210 | { |
---|
| 211 | ml->Clean(); |
---|
| 212 | omFreeBin((ADDRESS)ml, slists_bin); |
---|
| 213 | res->rtyp=0; |
---|
| 214 | return TRUE; |
---|
| 215 | } |
---|
| 216 | } |
---|
| 217 | else |
---|
| 218 | { |
---|
| 219 | ml->m[i].Copy(&l->m[i]); |
---|
| 220 | } |
---|
| 221 | } |
---|
| 222 | res->data=(char *)ml; |
---|
| 223 | break; |
---|
| 224 | } |
---|
| 225 | default: |
---|
| 226 | { |
---|
| 227 | return TRUE; |
---|
| 228 | } |
---|
| 229 | } |
---|
| 230 | return FALSE; |
---|
| 231 | } |
---|
| 232 | |
---|
| 233 | /*2 |
---|
| 234 | * substitutes the parameter par (from 1..N) by image, |
---|
| 235 | * does not destroy p and image |
---|
| 236 | */ |
---|
| 237 | poly pSubstPar(poly p, int par, poly image) |
---|
| 238 | { |
---|
[f769fd0] | 239 | assume( nCoeff_is_transExt(currRing->cf) ); // nCoeff_is_Extension??? |
---|
| 240 | const ring R = currRing->cf->extRing; |
---|
| 241 | |
---|
[95eb6d] | 242 | ideal theMapI = idInit(rPar(currRing),1); |
---|
[f769fd0] | 243 | nMapFunc nMap = n_SetMap(R->cf, currRing->cf); |
---|
[2f5b71] | 244 | |
---|
| 245 | int i; |
---|
[95eb6d] | 246 | for(i = rPar(currRing);i>0;i--) |
---|
[2f5b71] | 247 | { |
---|
[e92b07] | 248 | if (i != par) |
---|
| 249 | theMapI->m[i-1]= p_NSet(n_Param(i, currRing), currRing); |
---|
[2f5b71] | 250 | else |
---|
[e92b07] | 251 | theMapI->m[i-1] = p_Copy(image, currRing); |
---|
[9d68fd] | 252 | p_Test(theMapI->m[i-1],currRing); |
---|
[2f5b71] | 253 | } |
---|
[9d68fd] | 254 | //iiWriteMatrix((matrix)theMapI,"map:",1,currRing,0); |
---|
[2f5b71] | 255 | |
---|
| 256 | map theMap=(map)theMapI; |
---|
| 257 | theMap->preimage=NULL; |
---|
| 258 | |
---|
| 259 | leftv v=(leftv)omAllocBin(sleftv_bin); |
---|
| 260 | sleftv tmpW; |
---|
| 261 | poly res=NULL; |
---|
[e92b07] | 262 | |
---|
[9d68fd] | 263 | p_Normalize(p,currRing); |
---|
[2f5b71] | 264 | while (p!=NULL) |
---|
| 265 | { |
---|
| 266 | memset(v,0,sizeof(sleftv)); |
---|
[696bde] | 267 | |
---|
| 268 | number d = n_GetDenom(p_GetCoeff(p, currRing), currRing); |
---|
[f769fd0] | 269 | assume( p_Test((poly)NUM(d), R) ); |
---|
| 270 | |
---|
| 271 | if ( n_IsOne (d, currRing->cf) ) |
---|
[9d68fd] | 272 | { |
---|
[f769fd0] | 273 | n_Delete(&d, currRing); d = NULL; |
---|
[9d68fd] | 274 | } |
---|
[f769fd0] | 275 | else if (!p_IsConstant((poly)NUM(d), R)) |
---|
[9d68fd] | 276 | { |
---|
[dc79bd] | 277 | WarnS("ignoring denominators of coefficients..."); |
---|
[f769fd0] | 278 | n_Delete(&d, currRing); d = NULL; |
---|
[9d68fd] | 279 | } |
---|
[696bde] | 280 | |
---|
[f769fd0] | 281 | number num = n_GetNumerator(p_GetCoeff(p, currRing), currRing); |
---|
| 282 | assume( p_Test((poly)NUM(num), R) ); |
---|
[696bde] | 283 | |
---|
| 284 | memset(&tmpW,0,sizeof(sleftv)); |
---|
| 285 | tmpW.rtyp = POLY_CMD; |
---|
| 286 | tmpW.data = NUM (num); // a copy of this poly will be used |
---|
[f769fd0] | 287 | |
---|
[9d68fd] | 288 | p_Normalize(NUM(num),R); |
---|
[f769fd0] | 289 | if (maApplyFetch(MAP_CMD,theMap,v,&tmpW,R,NULL,NULL,0,nMap)) |
---|
[2f5b71] | 290 | { |
---|
| 291 | WerrorS("map failed"); |
---|
| 292 | v->data=NULL; |
---|
| 293 | } |
---|
[696bde] | 294 | n_Delete(&num, currRing); |
---|
| 295 | |
---|
| 296 | //TODO check for memory leaks |
---|
[e92b07] | 297 | poly pp = pHead(p); |
---|
[2f5b71] | 298 | //PrintS("map:");pWrite(pp); |
---|
[f769fd0] | 299 | if( d != NULL ) |
---|
| 300 | { |
---|
| 301 | pSetCoeff(pp, n_Invers(d, currRing->cf)); |
---|
| 302 | n_Delete(&d, currRing); // d = NULL; |
---|
[4c882f] | 303 | } |
---|
| 304 | else |
---|
[f769fd0] | 305 | pSetCoeff(pp, nInit(1)); |
---|
| 306 | |
---|
[2f5b71] | 307 | //PrintS("->");pWrite((poly)(v->data)); |
---|
[e92b07] | 308 | poly ppp = pMult((poly)(v->data),pp); |
---|
[2f5b71] | 309 | //PrintS("->");pWrite(ppp); |
---|
| 310 | res=pAdd(res,ppp); |
---|
| 311 | pIter(p); |
---|
| 312 | } |
---|
| 313 | idDelete((ideal *)(&theMap)); |
---|
| 314 | omFreeBin((ADDRESS)v, sleftv_bin); |
---|
| 315 | return res; |
---|
| 316 | } |
---|
| 317 | |
---|
| 318 | /*2 |
---|
[5bdabc] | 319 | * substitute the n-th parameter by the poly e in id |
---|
[2f5b71] | 320 | * does not destroy id and e |
---|
| 321 | */ |
---|
| 322 | ideal idSubstPar(ideal id, int n, poly e) |
---|
| 323 | { |
---|
| 324 | int k=MATROWS((matrix)id)*MATCOLS((matrix)id); |
---|
| 325 | ideal res=(ideal)mpNew(MATROWS((matrix)id),MATCOLS((matrix)id)); |
---|
| 326 | |
---|
| 327 | res->rank = id->rank; |
---|
| 328 | for(k--;k>=0;k--) |
---|
| 329 | { |
---|
| 330 | res->m[k]=pSubstPar(id->m[k],n,e); |
---|
| 331 | } |
---|
| 332 | return res; |
---|
| 333 | } |
---|
| 334 | |
---|
| 335 | /*2 |
---|
| 336 | * substitutes the variable var (from 1..N) by image, |
---|
| 337 | * does not destroy p and image |
---|
| 338 | */ |
---|
| 339 | poly pSubstPoly(poly p, int var, poly image) |
---|
| 340 | { |
---|
[509ea41] | 341 | if (p==NULL) return NULL; |
---|
[5bdabc] | 342 | #ifdef HAVE_PLURAL |
---|
[e1f17c] | 343 | if (rIsPluralRing(currRing)) |
---|
| 344 | { |
---|
| 345 | return pSubst(pCopy(p),var,image); |
---|
| 346 | } |
---|
[5bdabc] | 347 | #endif |
---|
[2f5b71] | 348 | map theMap=(map)idMaxIdeal(1); |
---|
| 349 | theMap->preimage=NULL; |
---|
| 350 | pDelete(&(theMap->m[var-1])); |
---|
| 351 | theMap->m[var-1]=pCopy(image); |
---|
| 352 | |
---|
| 353 | poly res=NULL; |
---|
[509ea41] | 354 | #ifdef FAST_MAP |
---|
| 355 | if (pGetComp(p)==0) |
---|
[2f5b71] | 356 | { |
---|
[509ea41] | 357 | ideal src_id=idInit(1,1); |
---|
| 358 | src_id->m[0]=p; |
---|
| 359 | ideal res_id=fast_map(src_id,currRing,(ideal)theMap,currRing); |
---|
| 360 | res=res_id->m[0]; |
---|
| 361 | res_id->m[0]=NULL; idDelete(&res_id); |
---|
| 362 | src_id->m[0]=NULL; idDelete(&src_id); |
---|
| 363 | } |
---|
| 364 | else |
---|
| 365 | #endif |
---|
| 366 | { |
---|
| 367 | sleftv tmpW; |
---|
| 368 | memset(&tmpW,0,sizeof(sleftv)); |
---|
| 369 | tmpW.rtyp=POLY_CMD; |
---|
| 370 | tmpW.data=p; |
---|
| 371 | leftv v=(leftv)omAlloc0Bin(sleftv_bin); |
---|
[3b1d9a] | 372 | if (maApplyFetch(MAP_CMD,theMap,v,&tmpW,currRing,NULL,NULL,0, |
---|
[9d68fd] | 373 | n_SetMap(currRing->cf, currRing->cf))) |
---|
[509ea41] | 374 | { |
---|
| 375 | WerrorS("map failed"); |
---|
| 376 | v->data=NULL; |
---|
| 377 | } |
---|
| 378 | res=(poly)(v->data); |
---|
| 379 | omFreeBin((ADDRESS)v, sleftv_bin); |
---|
[2f5b71] | 380 | } |
---|
| 381 | idDelete((ideal *)(&theMap)); |
---|
| 382 | return res; |
---|
| 383 | } |
---|
| 384 | |
---|
| 385 | /*2 |
---|
| 386 | * substitute the n-th variable by the poly e in id |
---|
| 387 | * does not destroy id and e |
---|
| 388 | */ |
---|
| 389 | ideal idSubstPoly(ideal id, int n, poly e) |
---|
| 390 | { |
---|
| 391 | |
---|
[e4611fa] | 392 | #ifdef HAVE_PLURAL |
---|
| 393 | if (rIsPluralRing(currRing)) |
---|
[2f5b71] | 394 | { |
---|
[e4611fa] | 395 | int k=MATROWS((matrix)id)*MATCOLS((matrix)id); |
---|
| 396 | ideal res=(ideal)mpNew(MATROWS((matrix)id),MATCOLS((matrix)id)); |
---|
| 397 | res->rank = id->rank; |
---|
| 398 | for(k--;k>=0;k--) |
---|
| 399 | { |
---|
| 400 | res->m[k]=pSubst(pCopy(id->m[k]),n,e); |
---|
| 401 | } |
---|
| 402 | return res; |
---|
[2f5b71] | 403 | } |
---|
[e4611fa] | 404 | #endif |
---|
| 405 | map theMap=(map)idMaxIdeal(1); |
---|
| 406 | theMap->preimage=NULL; |
---|
| 407 | pDelete(&(theMap->m[n-1])); |
---|
| 408 | theMap->m[n-1]=pCopy(e); |
---|
| 409 | |
---|
| 410 | leftv v=(leftv)omAlloc0Bin(sleftv_bin); |
---|
| 411 | sleftv tmpW; |
---|
| 412 | memset(&tmpW,0,sizeof(sleftv)); |
---|
| 413 | tmpW.rtyp=IDEAL_CMD; |
---|
| 414 | tmpW.data=id; |
---|
[3b1d9a] | 415 | if (maApplyFetch(MAP_CMD,theMap,v,&tmpW,currRing,NULL,NULL,0, |
---|
[9d68fd] | 416 | n_SetMap(currRing->cf, currRing->cf))) |
---|
[e4611fa] | 417 | { |
---|
| 418 | WerrorS("map failed"); |
---|
| 419 | v->data=NULL; |
---|
| 420 | } |
---|
| 421 | ideal res=(ideal)(v->data); |
---|
| 422 | idDelete((ideal *)(&theMap)); |
---|
| 423 | omFreeBin((ADDRESS)v, sleftv_bin); |
---|
[2f5b71] | 424 | return res; |
---|
| 425 | } |
---|