source: git/Singular/maps_ip.cc @ 72a01e

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