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