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