source: git/libpolys/polys/monomials/maps.cc @ dd668f

spielwiese
Last change on this file since dd668f was 7fee876, checked in by Oleksandr Motsak <motsak@…>, 12 years ago
moved prarameter-handling to coeffs from rings.* and related fixes chg: removed complex_parameter, m_nfParameter add: n_NumberOfParameters, n_ParameterNames, n_Param(coeffs) fix: par(1) (n_Param) for n_GF & n_long_C fix/chg: n_long_C is an Extension as well (rIsExtension) fix: n_long_C ngcCoeffWrite: additional space needed for compatibility with legacy Singular fix: complexToStr over non-C coeffs! fix: rRenameVars renames _new_ VARIABLES instead of _old_ parameters! fix: coeff construction was broken in walk.cc add/fix: nfKillChar, ngcKillChar chg: cleanup of headers & tests chg: parameter output during "make check"
  • Property mode set to 100644
File size: 7.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 <omalloc/omalloc.h>
10#include <misc/options.h>
11
12#include <coeffs/coeffs.h>
13#include <coeffs/numbers.h>
14
15#include <polys/monomials/p_polys.h>
16#include <polys/monomials/ring.h>
17#include <polys/simpleideals.h>
18#include <polys/prCopy.h>
19// #include <polys/ext_fields/longtrans.h>
20#include <polys/monomials/maps.h>
21
22#ifdef HAVE_PLURAL
23#include <polys/nc/nc.h>
24#endif
25
26// This is a very dirty way to "normalize" numbers w.r.t. a
27// MinPoly
28
29#define MAX_MAP_DEG 128
30
31/*2
32* copy a map
33*/
34map maCopy(map theMap, const ring r)
35{
36  int i;
37  map m=(map)idInit(IDELEMS(theMap),0);
38  for (i=IDELEMS(theMap)-1; i>=0; i--)
39      m->m[i] = p_Copy(theMap->m[i],r);
40  m->preimage=omStrDup(theMap->preimage);
41  return m;
42}
43
44
45/*2
46* return the image of var(v)^pExp, where var(v) maps to p
47*/
48poly maEvalVariable(poly p, int v,int pExp, ideal s, const ring dst_r)
49{
50  if (pExp==1)
51    return p_Copy(p,dst_r);
52
53  poly res;
54
55  if((s!=NULL)&&(pExp<MAX_MAP_DEG))
56  {
57    int j=2;
58    poly p0=p;
59    // find starting point
60    if(MATELEM(s,v,1)==NULL)
61    {
62      MATELEM(s,v,1)=p_Copy(p/*theMap->m[v-1]*/,dst_r);
63    }
64    else
65    {
66      while((j<=pExp)&&(MATELEM(s,v,j)!=NULL))
67      {
68        j++;
69      }
70      p0=MATELEM(s,v,j-1);
71    }
72    // multiply
73    for(;j<=pExp;j++)
74    {
75      p0=MATELEM(s,v,j)=pp_Mult_qq(p0, p,dst_r);
76      p_Normalize(p0, dst_r);
77    }
78    res=p_Copy(p0/*MATELEM(s,v,pExp)*/,dst_r);
79  }
80  else //if ((p->next!=NULL)&&(p->next->next==NULL))
81  {
82    res=p_Power(p_Copy(p,dst_r),pExp,dst_r);
83  }
84  return res;
85}
86
87static poly maEvalMonom(map theMap, poly p,ring preimage_r, ideal s, 
88           nMapFunc nMap, const ring dst_r)
89{
90    poly q=p_NSet(nMap(pGetCoeff(p),preimage_r->cf,dst_r->cf),dst_r);
91
92    int i;
93    for(i=1;i<=preimage_r->N; i++)
94    {
95      int pExp=p_GetExp( p,i,preimage_r);
96      if (pExp != 0)
97      {
98        if (theMap->m[i-1]!=NULL)
99        {
100          poly p1=theMap->m[i-1];
101          poly pp=maEvalVariable(p1,i,pExp,s,dst_r);
102          q = p_Mult_q(q,pp,dst_r);
103        }
104        else
105        {
106          p_Delete(&q,dst_r);
107          break;
108        }
109      }
110    }
111    int modulComp = p_GetComp( p,preimage_r);
112    if (q!=NULL) p_SetCompP(q,modulComp,dst_r);
113  return q;
114}
115
116poly maEval(map theMap, poly p,ring preimage_r,nMapFunc nMap, ideal s, const ring dst_r)
117{
118  poly result = NULL;
119  int i;
120
121//  for(i=1; i<=preimage_r->N; i++)
122//  {
123//    pTest(theMap->m[i-1]);
124//  }
125//  while (p!=NULL)
126//  {
127//    poly q=maEvalMonom(theMap,p,preimage_r,s);
128//    result = pAdd(result,q);
129//    pIter(p);
130//  }
131  if (p!=NULL)
132  {
133    int l = pLength(p)-1;
134    poly* monoms;
135    if (l>0)
136    {
137      monoms = (poly*) omAlloc(l*sizeof(poly));
138
139      for (i=0; i<l; i++)
140      {
141        monoms[i]=maEvalMonom(theMap,p,preimage_r,s, nMap, dst_r);
142        pIter(p);
143      }
144    }
145    result=maEvalMonom(theMap,p,preimage_r,s, nMap, dst_r);
146    if (l>0)
147    {
148      for(i = l-1; i>=0; i--)
149      {
150        result=p_Add_q(result, monoms[i], dst_r);
151      }
152      omFreeSize((ADDRESS)monoms,l*sizeof(poly));
153    }
154    if (!rMinpolyIsNULL(dst_r)) result=p_MinPolyNormalize(result, dst_r);
155  }
156  return result;
157}
158
159void maFindPerm(char const * const * const preim_names, int preim_n, char const * const * const preim_par, int preim_p,
160                char const * const * const names,       int n,       char const * const * const par,       int nop,
161                int * perm, int *par_perm, n_coeffType ch)
162{
163  int i,j;
164  /* find correspondig vars */
165  for (i=0; i<preim_n; i++)
166  {
167    for(j=0; j<n; j++)
168    {
169      if (strcmp(preim_names[i],names[j])==0)
170      {
171        if (BVERBOSE(V_IMAP))
172          Print("// var %s: nr %d -> nr %d\n",preim_names[i],i+1,j+1);
173        /* var i+1 from preimage ring is var j+1  (index j+1) from image ring */
174        perm[i+1]=j+1;
175        break;
176      }
177    }
178    if ((perm[i+1]==0)&&(par!=NULL)
179        // do not consider par of Fq
180         && (ch!=n_GF))
181    {
182      for(j=0; j<nop; j++)
183      {
184        if (strcmp(preim_names[i],par[j])==0)
185        {
186          if (BVERBOSE(V_IMAP))
187            Print("// var %s: nr %d -> par %d\n",preim_names[i],i+1,j+1);
188          /* var i+1 from preimage ring is par j+1 (index j) from image ring */
189          perm[i+1]=-(j+1);
190        }
191      }
192    }
193  }
194  if (par_perm!=NULL)
195  {
196    for (i=0; i<preim_p; i++)
197    {
198      for(j=0; j<n; j++)
199      {
200        if (strcmp(preim_par[i],names[j])==0)
201        {
202          if (BVERBOSE(V_IMAP))
203            Print("// par %s: par %d -> nr %d\n",preim_par[i],i+1,j+1);
204          /*par i+1 from preimage ring is var j+1  (index j+1) from image ring*/
205          par_perm[i]=j+1;
206          break;
207        }
208      }
209      if ((par!=NULL) && (par_perm[i]==0))
210      {
211        for(j=0; j<nop; j++)
212        {
213          if (strcmp(preim_par[i],par[j])==0)
214          {
215            if (BVERBOSE(V_IMAP))
216              Print("// par %s: nr %d -> par %d\n",preim_par[i],i+1,j+1);
217            /*par i+1 from preimage ring is par j+1 (index j) from image ring */
218            par_perm[i]=-(j+1);
219          }
220        }
221      }
222    }
223  }
224}
225
226/*2
227* embeds poly p from the subring r into the current ring
228*/
229poly maIMap(ring r, poly p, const ring dst_r)
230{
231  /* the simplest case:*/
232  if(r==dst_r) return p_Copy(p,dst_r);
233  nMapFunc nMap=n_SetMap(r->cf,dst_r->cf);
234  int *perm=(int *)omAlloc0((r->N+1)*sizeof(int));
235  //int *par_perm=(int *)omAlloc0(rPar(r)*sizeof(int));
236  maFindPerm(r->names, rVar(r), rParameter(r), rPar(r),
237             dst_r->names, rVar(dst_r),rParameter(dst_r), rPar(dst_r),
238             perm,NULL, dst_r->cf->type);
239  poly res=p_PermPoly(p,perm,r,dst_r, nMap /*,par_perm,rPar(r)*/);
240  omFreeSize((ADDRESS)perm,(r->N+1)*sizeof(int));
241  //omFreeSize((ADDRESS)par_perm,rPar(r)*sizeof(int));
242  return res;
243}
244
245/*3
246* find the max. degree in one variable, but not larger than MAX_MAP_DEG
247*/
248int maMaxDeg_Ma(ideal a,ring preimage_r)
249{
250  int i,j;
251  int N = preimage_r->N;
252  poly p;
253  int *m=(int *)omAlloc0(N*sizeof(int));
254
255  for (i=MATROWS(a)*MATCOLS(a)-1;i>=0;i--)
256  {
257    p=a->m[i];
258    //pTest(p); // cannot test p because it is from another ring
259    while(p!=NULL)
260    {
261      for(j=N-1;j>=0;j--)
262      {
263        m[j]=si_max(m[j],(int)p_GetExp( p,j+1,preimage_r));
264        if (m[j]>=MAX_MAP_DEG)
265        {
266          i=MAX_MAP_DEG;
267          goto max_deg_fertig_id;
268        }
269      }
270      pIter(p);
271    }
272  }
273  i=m[0];
274  for(j=N-1;j>0;j--)
275  {
276    i=si_max(i,m[j]);
277  }
278max_deg_fertig_id:
279  omFreeSize((ADDRESS)m,N*sizeof(int));
280  return i;
281}
282
283/*3
284* find the max. degree in one variable, but not larger than MAX_MAP_DEG
285*/
286int maMaxDeg_P(poly p,ring preimage_r)
287{
288  int i,j;
289  int N = preimage_r->N;
290  int *m=(int *)omAlloc0(N*sizeof(int));
291
292//  pTest(p);
293  while(p!=NULL)
294  {
295    for(j=N-1;j>=0;j--)
296    {
297      m[j]=si_max(m[j],(int)p_GetExp(p,j+1,preimage_r));
298      if (m[j]>=MAX_MAP_DEG)
299      {
300        i=MAX_MAP_DEG;
301        goto max_deg_fertig_p;
302      }
303    }
304    pIter(p);
305  }
306  i=m[0];
307  for(j=N-1;j>0;j--)
308  {
309    i=si_max(i,m[j]);
310  }
311max_deg_fertig_p:
312  omFreeSize((ADDRESS)m,N*sizeof(int));
313  return i;
314}
315
316// This is a very dirty way to cancel monoms whose number equals the
317// MinPoly
318poly p_MinPolyNormalize(poly p, const ring r)
319{
320  number one = n_Init(1, r->cf);
321  spolyrec rp;
322
323  poly q = &rp;
324
325  while (p != NULL)
326  {
327    // this returns 0, if p == MinPoly
328    number product = n_Mult(pGetCoeff(p), one,r->cf);
329    if ((product == NULL)||(n_IsZero(product,r->cf)))
330    {
331      p_LmDelete(&p,r);
332    }
333    else
334    {
335      p_SetCoeff(p, product,r);
336      pNext(q) = p;
337      q = p;
338      p = pNext(p);
339    }
340  }
341  pNext(q) = NULL;
342  return rp.next;
343}
Note: See TracBrowser for help on using the repository browser.