source: git/Singular/maps_ip.cc @ 60dc849

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