source: git/Singular/maps_ip.cc @ 4d5437

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