source: git/Singular/maps_ip.cc @ ba5e9e

spielwiese
Last change on this file since ba5e9e was ba5e9e, checked in by Oleksandr Motsak <motsak@…>, 11 years ago
Changed configure-scripts to generate individual public config files for each package: resources, libpolys, singular (main) fix: sources should include correct corresponding config headers.
  • 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#ifdef HAVE_CONFIG_H
10#include "singularconfig.h"
11#endif /* HAVE_CONFIG_H */
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/febase.h>
28#include <kernel/kstd1.h>
29
30#include "maps_ip.h"
31#include "ipid.h"
32
33
34#include "lists.h"
35#include "tok.h"
36
37/* debug output: Tok2Cmdname in maApplyFetch*/
38#include "ipshell.h"
39
40// define this if you want to use the fast_map routine for mapping ideals
41//#define FAST_MAP
42
43#ifdef FAST_MAP
44#include <polys/monomials/maps.h>
45#endif
46
47
48/*2
49* maps the expression w to res,
50* switch what: MAP_CMD: use theMap for mapping, N for preimage ring
51*              //FETCH_CMD: use pOrdPoly for mapping
52*              IMAP_CMD: use perm for mapping, N for preimage ring
53*              default: map only poly-structures,
54*                       use perm and par_perm, N and P,
55*/
56BOOLEAN maApplyFetch(int what,map theMap,leftv res, leftv w, ring preimage_r,
57                     int *perm, int *par_perm, int P, nMapFunc nMap)
58{
59  int i;
60  int N = preimage_r->N;
61#if 0
62  Print("N=%d what=%s ",N,Tok2Cmdname(what));
63  if (perm!=NULL) for(i=1;i<=N;i++) Print("%d -> %d ",i,perm[i]);
64  PrintS("\n");
65  Print("P=%d ",P);
66  if (par_perm!=NULL) for(i=0;i<P;i++) Print("%d -> %d ",i,par_perm[i]);
67  PrintS("\n");
68#endif
69
70  void *data=w->Data();
71  res->rtyp = w->rtyp;
72  switch (w->rtyp)
73  {
74    case NUMBER_CMD:
75      if (P!=0)
76      {
77//        WerrorS("Sorry 'napPermNumber' was lost in the refactoring process (due to Frank): needs to be fixed");
78//        return TRUE;
79#if 1
80// poly n_PermNumber(const number z, const int *par_perm, const int OldPar, const ring src, const ring dst);
81        res->data= (void *) n_PermNumber((number)data, par_perm, P, preimage_r, currRing);
82#endif
83        res->rtyp=POLY_CMD;
84        if (nCoeff_is_Extension(currRing->cf))
85          res->data=(void *)p_MinPolyNormalize((poly)res->data, currRing);
86        pTest((poly) res->data);
87      }
88      else
89      {
90        assume( nMap != NULL );
91
92        number a = nMap((number)data, preimage_r->cf, currRing->cf);
93
94
95        if (nCoeff_is_Extension(currRing->cf))
96        {
97          n_Normalize(a, currRing->cf); // ???
98/*
99          number a = (number)res->data;
100          number one = nInit(1);
101          number product = nMult(a, one );
102          nDelete(&one);
103          nDelete(&a);
104          res->data=(void *)product;
105 */
106        }
107        #ifdef LDEBUG
108        n_Test(a, currRing->cf);
109        #endif
110        res->data=(void *)a;
111
112      }
113      break;
114    case POLY_CMD:
115    case VECTOR_CMD:
116      if ((what==FETCH_CMD)&& (preimage_r->cf==currRing->cf))
117        res->data=(void *)prCopyR( (poly)data, preimage_r, currRing);
118      else
119      if ((what==IMAP_CMD) || ((what==FETCH_CMD) /* && (nMap!=nCopy)*/))
120        res->data=(void *)p_PermPoly((poly)data,perm,preimage_r,currRing, nMap,par_perm,P);
121      else /*if (what==MAP_CMD)*/
122      {
123        p_Test((poly)data,preimage_r);
124        matrix s=mpNew(N,maMaxDeg_P((poly)data, preimage_r));
125        res->data=(void *)maEval(theMap, (poly)data, preimage_r, nMap, (ideal)s, currRing);
126        idDelete((ideal *)&s);
127      }
128      if (nCoeff_is_Extension(currRing->cf))
129        res->data=(void *)p_MinPolyNormalize((poly)res->data, currRing);
130      pTest((poly)res->data);
131      break;
132    case MODUL_CMD:
133    case MATRIX_CMD:
134    case IDEAL_CMD:
135    case MAP_CMD:
136    {
137      int C=((matrix)data)->cols();
138      int R;
139      if (w->rtyp==MAP_CMD) R=1;
140      else R=((matrix)data)->rows();
141      matrix m=mpNew(R,C);
142      char *tmpR=NULL;
143      if(w->rtyp==MAP_CMD)
144      {
145        tmpR=((map)data)->preimage;
146        ((matrix)data)->rank=((matrix)data)->rows();
147      }
148      if ((what==FETCH_CMD)&& (preimage_r->cf == currRing->cf))
149      {
150        for (i=R*C-1;i>=0;i--)
151        {
152          m->m[i]=prCopyR(((ideal)data)->m[i], preimage_r, currRing);
153          pTest(m->m[i]);
154        }
155      }
156      else
157      if ((what==IMAP_CMD) || ((what==FETCH_CMD) /* && (nMap!=nCopy)*/))
158      {
159        for (i=R*C-1;i>=0;i--)
160        {
161          m->m[i]=p_PermPoly(((ideal)data)->m[i],perm,preimage_r,currRing,
162                          nMap,par_perm,P);
163          pTest(m->m[i]);
164        }
165      }
166      else /* if(what==MAP_CMD) */
167      {
168        matrix s=mpNew(N,maMaxDeg_Ma((ideal)data,preimage_r));
169        for (i=R*C-1;i>=0;i--)
170        {
171          m->m[i]=maEval(theMap, ((ideal)data)->m[i], preimage_r, nMap, (ideal)s, currRing);
172          pTest(m->m[i]);
173        }
174        idDelete((ideal *)&s);
175      }
176      if (nCoeff_is_Extension(currRing->cf))
177      {
178        for (i=R*C-1;i>=0;i--)
179        {
180          m->m[i]=p_MinPolyNormalize(m->m[i], currRing);
181          pTest(m->m[i]);
182        }
183      }
184      if(w->rtyp==MAP_CMD)
185      {
186        ((map)data)->preimage=tmpR;
187        ((map)m)->preimage=omStrDup(tmpR);
188      }
189      else
190      {
191        m->rank=((matrix)data)->rank;
192      }
193      res->data=(char *)m;
194      idTest((ideal) m);
195      break;
196    }
197
198    case LIST_CMD:
199    {
200      lists l=(lists)data;
201      lists ml=(lists)omAllocBin(slists_bin);
202      ml->Init(l->nr+1);
203      for(i=0;i<=l->nr;i++)
204      {
205        if (((l->m[i].rtyp>BEGIN_RING)&&(l->m[i].rtyp<END_RING))
206        ||(l->m[i].rtyp==LIST_CMD))
207        {
208          if (maApplyFetch(what,theMap,&ml->m[i],&l->m[i],
209                           preimage_r,perm,par_perm,P,nMap))
210          {
211            ml->Clean();
212            omFreeBin((ADDRESS)ml, slists_bin);
213            res->rtyp=0;
214            return TRUE;
215          }
216        }
217        else
218        {
219          ml->m[i].Copy(&l->m[i]);
220        }
221      }
222      res->data=(char *)ml;
223      break;
224    }
225    default:
226    {
227      return TRUE;
228    }
229  }
230  return FALSE;
231}
232
233/*2
234* substitutes the parameter par (from 1..N) by image,
235* does not destroy p and  image
236*/
237poly pSubstPar(poly p, int par, poly image)
238{
239  assume( nCoeff_is_transExt(currRing->cf) ); // nCoeff_is_Extension???
240  const ring R = currRing->cf->extRing;
241
242  ideal theMapI = idInit(rPar(currRing),1);
243  nMapFunc nMap = n_SetMap(R->cf, currRing->cf);
244
245  int i;
246  for(i = rPar(currRing);i>0;i--)
247  {
248    if (i != par)
249      theMapI->m[i-1]= p_NSet(n_Param(i, currRing), currRing);
250    else
251      theMapI->m[i-1] = p_Copy(image, currRing);
252    p_Test(theMapI->m[i-1],currRing);
253  }
254  //iiWriteMatrix((matrix)theMapI,"map:",1,currRing,0);
255
256  map theMap=(map)theMapI;
257  theMap->preimage=NULL;
258
259  leftv v=(leftv)omAllocBin(sleftv_bin);
260  sleftv tmpW;
261  poly res=NULL;
262
263  p_Normalize(p,currRing);
264  while (p!=NULL)
265  {
266    memset(v,0,sizeof(sleftv));
267
268    number d = n_GetDenom(p_GetCoeff(p, currRing), currRing);
269    assume( p_Test((poly)NUM(d), R) );
270
271    if ( n_IsOne (d, currRing->cf) )
272    {
273      n_Delete(&d, currRing); d = NULL;
274    }
275    else if (!p_IsConstant((poly)NUM(d), R))
276    {
277      WarnS("ignoring denominators of coefficients...");
278      n_Delete(&d, currRing); d = NULL;
279    }
280
281    number num = n_GetNumerator(p_GetCoeff(p, currRing), currRing);
282    assume( p_Test((poly)NUM(num), R) );
283
284    memset(&tmpW,0,sizeof(sleftv));
285    tmpW.rtyp = POLY_CMD;
286    tmpW.data = NUM (num); // a copy of this poly will be used
287
288    p_Normalize(NUM(num),R);
289    if (maApplyFetch(MAP_CMD,theMap,v,&tmpW,R,NULL,NULL,0,nMap))
290    {
291      WerrorS("map failed");
292      v->data=NULL;
293    }
294    n_Delete(&num, currRing);
295
296    //TODO check for memory leaks
297    poly pp = pHead(p);
298    //PrintS("map:");pWrite(pp);
299    if( d != NULL )
300    {
301      pSetCoeff(pp, n_Invers(d, currRing->cf));
302      n_Delete(&d, currRing); // d = NULL;
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.