source: git/Singular/maps_ip.cc @ e92b07

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