source: git/Singular/maps_ip.cc @ 151a06

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