source: git/Singular/maps_ip.cc @ 06abb07

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