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

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