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