source: git/Singular/maps_ip.cc @ 92d684

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