source: git/Singular/maps_ip.cc @ 762407

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