source: git/Singular/maps_ip.cc @ 65183c4

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