source: git/Singular/maps_ip.cc @ a4b31c

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