1 | /**************************************** |
---|
2 | * Computer Algebra System SINGULAR * |
---|
3 | ****************************************/ |
---|
4 | /* |
---|
5 | * ABSTRACT - the mapping of polynomials to other rings |
---|
6 | */ |
---|
7 | #define TRANSEXT_PRIVATES |
---|
8 | |
---|
9 | #ifdef HAVE_CONFIG_H |
---|
10 | #include "singularconfig.h" |
---|
11 | #endif /* HAVE_CONFIG_H */ |
---|
12 | #include <kernel/mod2.h> |
---|
13 | #include <omalloc/omalloc.h> |
---|
14 | |
---|
15 | #include <coeffs/numbers.h> |
---|
16 | #include <coeffs/coeffs.h> |
---|
17 | |
---|
18 | #include <polys/monomials/ring.h> |
---|
19 | #include <polys/monomials/maps.h> |
---|
20 | #include <polys/matpol.h> |
---|
21 | #include <polys/prCopy.h> |
---|
22 | #include <polys/ext_fields/transext.h> |
---|
23 | |
---|
24 | //#include <libpolys/polys/ext_fields/longtrans.h> |
---|
25 | // #include <kernel/longalg.h> |
---|
26 | |
---|
27 | #include <kernel/febase.h> |
---|
28 | #include <kernel/kstd1.h> |
---|
29 | |
---|
30 | #include "maps_ip.h" |
---|
31 | #include "ipid.h" |
---|
32 | |
---|
33 | |
---|
34 | #include "lists.h" |
---|
35 | #include "tok.h" |
---|
36 | |
---|
37 | /* debug output: Tok2Cmdname in maApplyFetch*/ |
---|
38 | #include "ipshell.h" |
---|
39 | |
---|
40 | // define this if you want to use the fast_map routine for mapping ideals |
---|
41 | //#define FAST_MAP |
---|
42 | |
---|
43 | #ifdef FAST_MAP |
---|
44 | #include <polys/monomials/maps.h> |
---|
45 | #endif |
---|
46 | |
---|
47 | |
---|
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; |
---|
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 | |
---|
70 | void *data=w->Data(); |
---|
71 | res->rtyp = w->rtyp; |
---|
72 | switch (w->rtyp) |
---|
73 | { |
---|
74 | case NUMBER_CMD: |
---|
75 | if (P!=0) |
---|
76 | { |
---|
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); |
---|
82 | #endif |
---|
83 | res->rtyp=POLY_CMD; |
---|
84 | if (nCoeff_is_Extension(currRing->cf)) |
---|
85 | res->data=(void *)p_MinPolyNormalize((poly)res->data, currRing); |
---|
86 | pTest((poly) res->data); |
---|
87 | } |
---|
88 | else |
---|
89 | { |
---|
90 | assume( nMap != NULL ); |
---|
91 | |
---|
92 | number a = nMap((number)data, preimage_r->cf, currRing->cf); |
---|
93 | |
---|
94 | |
---|
95 | if (nCoeff_is_Extension(currRing->cf)) |
---|
96 | { |
---|
97 | n_Normalize(a, currRing->cf); // ??? |
---|
98 | /* |
---|
99 | number a = (number)res->data; |
---|
100 | number one = nInit(1); |
---|
101 | number product = nMult(a, one ); |
---|
102 | nDelete(&one); |
---|
103 | nDelete(&a); |
---|
104 | res->data=(void *)product; |
---|
105 | */ |
---|
106 | } |
---|
107 | #ifdef LDEBUG |
---|
108 | n_Test(a, currRing->cf); |
---|
109 | #endif |
---|
110 | res->data=(void *)a; |
---|
111 | |
---|
112 | } |
---|
113 | break; |
---|
114 | case POLY_CMD: |
---|
115 | case VECTOR_CMD: |
---|
116 | if ((what==FETCH_CMD)&& (preimage_r->cf==currRing->cf)) |
---|
117 | res->data=(void *)prCopyR( (poly)data, preimage_r, currRing); |
---|
118 | else |
---|
119 | if ((what==IMAP_CMD) || ((what==FETCH_CMD) /* && (nMap!=nCopy)*/)) |
---|
120 | res->data=(void *)p_PermPoly((poly)data,perm,preimage_r,currRing, nMap,par_perm,P); |
---|
121 | else /*if (what==MAP_CMD)*/ |
---|
122 | { |
---|
123 | p_Test((poly)data,preimage_r); |
---|
124 | matrix s=mpNew(N,maMaxDeg_P((poly)data, preimage_r)); |
---|
125 | res->data=(void *)maEval(theMap, (poly)data, preimage_r, nMap, (ideal)s, currRing); |
---|
126 | idDelete((ideal *)&s); |
---|
127 | } |
---|
128 | if (nCoeff_is_Extension(currRing->cf)) |
---|
129 | res->data=(void *)p_MinPolyNormalize((poly)res->data, currRing); |
---|
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 | } |
---|
148 | if ((what==FETCH_CMD)&& (preimage_r->cf == currRing->cf)) |
---|
149 | { |
---|
150 | for (i=R*C-1;i>=0;i--) |
---|
151 | { |
---|
152 | m->m[i]=prCopyR(((ideal)data)->m[i], preimage_r, currRing); |
---|
153 | pTest(m->m[i]); |
---|
154 | } |
---|
155 | } |
---|
156 | else |
---|
157 | if ((what==IMAP_CMD) || ((what==FETCH_CMD) /* && (nMap!=nCopy)*/)) |
---|
158 | { |
---|
159 | for (i=R*C-1;i>=0;i--) |
---|
160 | { |
---|
161 | m->m[i]=p_PermPoly(((ideal)data)->m[i],perm,preimage_r,currRing, |
---|
162 | nMap,par_perm,P); |
---|
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 | { |
---|
171 | m->m[i]=maEval(theMap, ((ideal)data)->m[i], preimage_r, nMap, (ideal)s, currRing); |
---|
172 | pTest(m->m[i]); |
---|
173 | } |
---|
174 | idDelete((ideal *)&s); |
---|
175 | } |
---|
176 | if (nCoeff_is_Extension(currRing->cf)) |
---|
177 | { |
---|
178 | for (i=R*C-1;i>=0;i--) |
---|
179 | { |
---|
180 | m->m[i]=p_MinPolyNormalize(m->m[i], currRing); |
---|
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 | { |
---|
239 | assume( nCoeff_is_transExt(currRing->cf) ); // nCoeff_is_Extension??? |
---|
240 | const ring R = currRing->cf->extRing; |
---|
241 | |
---|
242 | ideal theMapI = idInit(rPar(currRing),1); |
---|
243 | nMapFunc nMap = n_SetMap(R->cf, currRing->cf); |
---|
244 | |
---|
245 | int i; |
---|
246 | for(i = rPar(currRing);i>0;i--) |
---|
247 | { |
---|
248 | if (i != par) |
---|
249 | theMapI->m[i-1]= p_NSet(n_Param(i, currRing), currRing); |
---|
250 | else |
---|
251 | theMapI->m[i-1] = p_Copy(image, currRing); |
---|
252 | p_Test(theMapI->m[i-1],currRing); |
---|
253 | } |
---|
254 | //iiWriteMatrix((matrix)theMapI,"map:",1,currRing,0); |
---|
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; |
---|
262 | |
---|
263 | p_Normalize(p,currRing); |
---|
264 | while (p!=NULL) |
---|
265 | { |
---|
266 | memset(v,0,sizeof(sleftv)); |
---|
267 | |
---|
268 | number d = n_GetDenom(p_GetCoeff(p, currRing), currRing); |
---|
269 | assume( p_Test((poly)NUM(d), R) ); |
---|
270 | |
---|
271 | if ( n_IsOne (d, currRing->cf) ) |
---|
272 | { |
---|
273 | n_Delete(&d, currRing); d = NULL; |
---|
274 | } |
---|
275 | else if (!p_IsConstant((poly)NUM(d), R)) |
---|
276 | { |
---|
277 | WarnS("ignoring denominators of coefficients..."); |
---|
278 | n_Delete(&d, currRing); d = NULL; |
---|
279 | } |
---|
280 | |
---|
281 | number num = n_GetNumerator(p_GetCoeff(p, currRing), currRing); |
---|
282 | assume( p_Test((poly)NUM(num), R) ); |
---|
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 |
---|
287 | |
---|
288 | p_Normalize(NUM(num),R); |
---|
289 | if (maApplyFetch(MAP_CMD,theMap,v,&tmpW,R,NULL,NULL,0,nMap)) |
---|
290 | { |
---|
291 | WerrorS("map failed"); |
---|
292 | v->data=NULL; |
---|
293 | } |
---|
294 | n_Delete(&num, currRing); |
---|
295 | |
---|
296 | //TODO check for memory leaks |
---|
297 | poly pp = pHead(p); |
---|
298 | //PrintS("map:");pWrite(pp); |
---|
299 | if( d != NULL ) |
---|
300 | { |
---|
301 | pSetCoeff(pp, n_Invers(d, currRing->cf)); |
---|
302 | n_Delete(&d, currRing); // d = NULL; |
---|
303 | } |
---|
304 | else |
---|
305 | pSetCoeff(pp, nInit(1)); |
---|
306 | |
---|
307 | //PrintS("->");pWrite((poly)(v->data)); |
---|
308 | poly ppp = pMult((poly)(v->data),pp); |
---|
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 |
---|
319 | * substitute the n-th parameter by the poly e in id |
---|
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 | { |
---|
341 | if (p==NULL) return NULL; |
---|
342 | #ifdef HAVE_PLURAL |
---|
343 | if (rIsPluralRing(currRing)) |
---|
344 | { |
---|
345 | return pSubst(pCopy(p),var,image); |
---|
346 | } |
---|
347 | #endif |
---|
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; |
---|
354 | #ifdef FAST_MAP |
---|
355 | if (pGetComp(p)==0) |
---|
356 | { |
---|
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); |
---|
372 | if (maApplyFetch(MAP_CMD,theMap,v,&tmpW,currRing,NULL,NULL,0, |
---|
373 | n_SetMap(currRing->cf, currRing->cf))) |
---|
374 | { |
---|
375 | WerrorS("map failed"); |
---|
376 | v->data=NULL; |
---|
377 | } |
---|
378 | res=(poly)(v->data); |
---|
379 | omFreeBin((ADDRESS)v, sleftv_bin); |
---|
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 | |
---|
392 | #ifdef HAVE_PLURAL |
---|
393 | if (rIsPluralRing(currRing)) |
---|
394 | { |
---|
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; |
---|
403 | } |
---|
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; |
---|
415 | if (maApplyFetch(MAP_CMD,theMap,v,&tmpW,currRing,NULL,NULL,0, |
---|
416 | n_SetMap(currRing->cf, currRing->cf))) |
---|
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); |
---|
424 | return res; |
---|
425 | } |
---|