My Project  UNKNOWN_GIT_VERSION
maps.cc
Go to the documentation of this file.
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 #include "misc/auxiliary.h"
11 #include "misc/options.h"
12 
13 #include "coeffs/coeffs.h"
14 #include "coeffs/numbers.h"
15 
17 #include "polys/monomials/ring.h"
18 #include "polys/simpleideals.h"
19 #include "polys/prCopy.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 */
34 map 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 */
48 poly 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 
87 static poly maEvalMonom(map theMap, poly p,ring preimage_r, ideal s,
88  nMapFunc nMap, const ring dst_r)
89 {
90  p_Test(p,preimage_r);
91  poly q=p_NSet(nMap(pGetCoeff(p),preimage_r->cf,dst_r->cf),dst_r);
92 
93  int i;
94  for(i=1;i<=preimage_r->N; i++)
95  {
96  int pExp=p_GetExp( p,i,preimage_r);
97  if (pExp != 0)
98  {
99  if (theMap->m[i-1]!=NULL)
100  {
101  poly p1=theMap->m[i-1];
102  poly pp=maEvalVariable(p1,i,pExp,s,dst_r);
103  q = p_Mult_q(q,pp,dst_r);
104  }
105  else
106  {
107  p_Delete(&q,dst_r);
108  break;
109  }
110  }
111  }
112  int modulComp = p_GetComp( p,preimage_r);
113  if (q!=NULL) p_SetCompP(q,modulComp,dst_r);
114  return q;
115 }
116 
117 poly maEval(map theMap, poly p,ring preimage_r,nMapFunc nMap, ideal s, const ring dst_r)
118 {
119  poly result = NULL;
120  int i;
121 
122 // for(i=1; i<=preimage_r->N; i++)
123 // {
124 // pTest(theMap->m[i-1]);
125 // }
126 // while (p!=NULL)
127 // {
128 // poly q=maEvalMonom(theMap,p,preimage_r,s);
129 // result = pAdd(result,q);
130 // pIter(p);
131 // }
132  if (p!=NULL)
133  {
134  int l = pLength(p)-1;
135  poly* monoms;
136  if (l>0)
137  {
138  monoms = (poly*) omAlloc(l*sizeof(poly));
139 
140  for (i=0; i<l; i++)
141  {
142  monoms[i]=maEvalMonom(theMap,p,preimage_r,s, nMap, dst_r);
143  pIter(p);
144  }
145  }
146  result=maEvalMonom(theMap,p,preimage_r,s, nMap, dst_r);
147  if (l>0)
148  {
149  for(i = l-1; i>=0; i--)
150  {
151  result=p_Add_q(result, monoms[i], dst_r);
152  }
153  omFreeSize((ADDRESS)monoms,l*sizeof(poly));
154  }
155 
156  assume(dst_r != NULL);
157  assume(dst_r->cf != NULL);
158 
159  if (nCoeff_is_algExt(dst_r->cf))
160  result = p_MinPolyNormalize(result, dst_r);
161  }
162  return result;
163 }
164 
165 void maFindPerm(char const * const * const preim_names, int preim_n, char const * const * const preim_par, int preim_p,
166  char const * const * const names, int n, char const * const * const par, int nop,
167  int * perm, int *par_perm, n_coeffType ch)
168 {
169  int i,j;
170  /* find correspondig vars */
171  for (i=0; i<preim_n; i++)
172  {
173  for(j=0; j<n; j++)
174  {
175  if (strcmp(preim_names[i],names[j])==0)
176  {
177  if (BVERBOSE(V_IMAP))
178  Print("// var %s: nr %d -> nr %d\n",preim_names[i],i+1,j+1);
179  /* var i+1 from preimage ring is var j+1 (index j+1) from image ring */
180  perm[i+1]=j+1;
181  break;
182  }
183  }
184  if ((perm[i+1]==0)&&(par!=NULL)
185  // do not consider par of Fq
186  && (ch!=n_GF))
187  {
188  for(j=0; j<nop; j++)
189  {
190  if (strcmp(preim_names[i],par[j])==0)
191  {
192  if (BVERBOSE(V_IMAP))
193  Print("// var %s: nr %d -> par %d\n",preim_names[i],i+1,j+1);
194  /* var i+1 from preimage ring is par j+1 (index j) from image ring */
195  perm[i+1]=-(j+1);
196  }
197  }
198  }
199  }
200  if (par_perm!=NULL)
201  {
202  for (i=0; i<preim_p; i++)
203  {
204  for(j=0; j<n; j++)
205  {
206  if (strcmp(preim_par[i],names[j])==0)
207  {
208  if (BVERBOSE(V_IMAP))
209  Print("// par %s: par %d -> nr %d\n",preim_par[i],i+1,j+1);
210  /*par i+1 from preimage ring is var j+1 (index j+1) from image ring*/
211  par_perm[i]=j+1;
212  break;
213  }
214  }
215  if ((par!=NULL) && (par_perm[i]==0))
216  {
217  for(j=0; j<nop; j++)
218  {
219  if (strcmp(preim_par[i],par[j])==0)
220  {
221  if (BVERBOSE(V_IMAP))
222  Print("// par %s: nr %d -> par %d\n",preim_par[i],i+1,j+1);
223  /*par i+1 from preimage ring is par j+1 (index j) from image ring */
224  par_perm[i]=-(j+1);
225  }
226  }
227  }
228  }
229  }
230 }
231 
232 /*2
233 * embeds poly p from the subring r into the current ring
234 */
235 poly maIMap(ring r, poly p, const ring dst_r)
236 {
237  /* the simplest case:*/
238  if(r==dst_r) return p_Copy(p,dst_r);
239  nMapFunc nMap=n_SetMap(r->cf,dst_r->cf);
240  int *perm=(int *)omAlloc0((r->N+1)*sizeof(int));
241  //int *par_perm=(int *)omAlloc0(rPar(r)*sizeof(int));
242  maFindPerm(r->names, rVar(r), rParameter(r), rPar(r),
243  dst_r->names, rVar(dst_r),rParameter(dst_r), rPar(dst_r),
244  perm,NULL, dst_r->cf->type);
245  poly res=p_PermPoly(p,perm,r,dst_r, nMap /*,par_perm,rPar(r)*/);
246  omFreeSize((ADDRESS)perm,(r->N+1)*sizeof(int));
247  //omFreeSize((ADDRESS)par_perm,rPar(r)*sizeof(int));
248  return res;
249 }
250 
251 /*3
252 * find the max. degree in one variable, but not larger than MAX_MAP_DEG
253 */
254 int maMaxDeg_Ma(ideal a,ring preimage_r)
255 {
256  int i,j;
257  int N = preimage_r->N;
258  poly p;
259  int *m=(int *)omAlloc0(N*sizeof(int));
260 
261  for (i=MATROWS(a)*MATCOLS(a)-1;i>=0;i--)
262  {
263  p=a->m[i];
264  //pTest(p); // cannot test p because it is from another ring
265  while(p!=NULL)
266  {
267  for(j=N-1;j>=0;j--)
268  {
269  m[j]=si_max(m[j],(int)p_GetExp( p,j+1,preimage_r));
270  if (m[j]>=MAX_MAP_DEG)
271  {
272  i=MAX_MAP_DEG;
273  goto max_deg_fertig_id;
274  }
275  }
276  pIter(p);
277  }
278  }
279  i=m[0];
280  for(j=N-1;j>0;j--)
281  {
282  i=si_max(i,m[j]);
283  }
284 max_deg_fertig_id:
285  omFreeSize((ADDRESS)m,N*sizeof(int));
286  return i;
287 }
288 
289 /*3
290 * find the max. degree in one variable, but not larger than MAX_MAP_DEG
291 */
292 int maMaxDeg_P(poly p,ring preimage_r)
293 {
294  int i,j;
295  int N = preimage_r->N;
296  int *m=(int *)omAlloc0(N*sizeof(int));
297 
298 // pTest(p);
299  while(p!=NULL)
300  {
301  for(j=N-1;j>=0;j--)
302  {
303  m[j]=si_max(m[j],(int)p_GetExp(p,j+1,preimage_r));
304  if (m[j]>=MAX_MAP_DEG)
305  {
306  i=MAX_MAP_DEG;
307  goto max_deg_fertig_p;
308  }
309  }
310  pIter(p);
311  }
312  i=m[0];
313  for(j=N-1;j>0;j--)
314  {
315  i=si_max(i,m[j]);
316  }
317 max_deg_fertig_p:
318  omFreeSize((ADDRESS)m,N*sizeof(int));
319  return i;
320 }
321 
322 // This is a very dirty way to cancel monoms whose number equals the
323 // MinPoly
324 poly p_MinPolyNormalize(poly p, const ring r)
325 {
326  const coeffs C = r->cf;
327  number one = n_Init(1, C);
328  spolyrec rp;
329 
330  poly q = &rp;
331 
332  while (p != NULL)
333  {
334  // this returns 0, if p == MinPoly
335  number product = n_Mult(p_GetCoeff(p, r), one, C);
336  if ((product == NULL)||(n_IsZero(product, C)))
337  {
338  p_LmDelete(&p, r);
339  }
340  else
341  {
342  p_SetCoeff(p, product, r);
343  pNext(q) = p;
344  q = p;
345  p = pNext(p);
346  }
347  }
348  pNext(q) = NULL;
349  n_Delete(&one, C);
350  return rp.next;
351 }
All the auxiliary stuff.
static int si_max(const int a, const int b)
Definition: auxiliary.h:138
void * ADDRESS
Definition: auxiliary.h:133
CanonicalForm pp(const CanonicalForm &)
CanonicalForm pp ( const CanonicalForm & f )
Definition: cf_gcd.cc:248
const CanonicalForm CFMap CFMap & N
Definition: cfEzgcd.cc:49
int l
Definition: cfEzgcd.cc:93
int m
Definition: cfEzgcd.cc:121
int i
Definition: cfEzgcd.cc:125
int p
Definition: cfModGcd.cc:4019
CanonicalForm map(const CanonicalForm &primElem, const Variable &alpha, const CanonicalForm &F, const Variable &beta)
map from to such that is mapped onto
Definition: cf_map_ext.cc:400
Coefficient rings, fields and other domains suitable for Singular polynomials.
static FORCE_INLINE number n_Mult(number a, number b, const coeffs r)
return the product of 'a' and 'b', i.e., a*b
Definition: coeffs.h:636
n_coeffType
Definition: coeffs.h:28
@ n_GF
\GF{p^n < 2^16}
Definition: coeffs.h:33
static FORCE_INLINE nMapFunc n_SetMap(const coeffs src, const coeffs dst)
set the mapping function pointers for translating numbers from src to dst
Definition: coeffs.h:721
static FORCE_INLINE BOOLEAN n_IsZero(number n, const coeffs r)
TRUE iff 'n' represents the zero element.
Definition: coeffs.h:464
static FORCE_INLINE void n_Delete(number *p, const coeffs r)
delete 'p'
Definition: coeffs.h:455
static FORCE_INLINE number n_Init(long i, const coeffs r)
a number representing i in the given coeff field/ring r
Definition: coeffs.h:538
static FORCE_INLINE BOOLEAN nCoeff_is_algExt(const coeffs r)
TRUE iff r represents an algebraic extension field.
Definition: coeffs.h:932
number(* nMapFunc)(number a, const coeffs src, const coeffs dst)
maps "a", which lives in src, into dst
Definition: coeffs.h:73
#define Print
Definition: emacs.cc:80
return result
Definition: facAbsBiFact.cc:76
const CanonicalForm int s
Definition: facAbsFact.cc:55
CanonicalForm res
Definition: facAbsFact.cc:64
const Variable & v
< [in] a sqrfree bivariate poly
Definition: facBivar.h:37
int j
Definition: facHensel.cc:105
map maCopy(map theMap, const ring r)
Definition: maps.cc:34
poly maEval(map theMap, poly p, ring preimage_r, nMapFunc nMap, ideal s, const ring dst_r)
Definition: maps.cc:117
poly maIMap(ring r, poly p, const ring dst_r)
Definition: maps.cc:235
int maMaxDeg_P(poly p, ring preimage_r)
Definition: maps.cc:292
void maFindPerm(char const *const *const preim_names, int preim_n, char const *const *const preim_par, int preim_p, char const *const *const names, int n, char const *const *const par, int nop, int *perm, int *par_perm, n_coeffType ch)
Definition: maps.cc:165
static poly maEvalMonom(map theMap, poly p, ring preimage_r, ideal s, nMapFunc nMap, const ring dst_r)
Definition: maps.cc:87
poly maEvalVariable(poly p, int v, int pExp, ideal s, const ring dst_r)
Definition: maps.cc:48
int maMaxDeg_Ma(ideal a, ring preimage_r)
Definition: maps.cc:254
poly p_MinPolyNormalize(poly p, const ring r)
Definition: maps.cc:324
#define MAX_MAP_DEG
Definition: maps.cc:29
#define MATELEM(mat, i, j)
Definition: matpol.h:28
#define MATROWS(i)
Definition: matpol.h:26
#define MATCOLS(i)
Definition: matpol.h:27
#define assume(x)
Definition: mod2.h:390
#define p_GetComp(p, r)
Definition: monomials.h:65
poly next
Definition: monomials.h:25
#define pIter(p)
Definition: monomials.h:38
#define pNext(p)
Definition: monomials.h:37
static number & pGetCoeff(poly p)
return an alias to the leading coefficient of p assumes that p != NULL NOTE: not copy
Definition: monomials.h:45
#define p_GetCoeff(p, r)
Definition: monomials.h:51
The main handler for Singular numbers which are suitable for Singular polynomials.
#define omStrDup(s)
Definition: omAllocDecl.h:263
#define omFreeSize(addr, size)
Definition: omAllocDecl.h:260
#define omAlloc(size)
Definition: omAllocDecl.h:210
#define omAlloc0(size)
Definition: omAllocDecl.h:211
#define NULL
Definition: omList.c:10
#define BVERBOSE(a)
Definition: options.h:35
#define V_IMAP
Definition: options.h:53
poly p_PermPoly(poly p, const int *perm, const ring oldRing, const ring dst, nMapFunc nMap, const int *par_perm, int OldPar, BOOLEAN use_mult)
Definition: p_polys.cc:4028
poly p_Power(poly p, int i, const ring r)
Definition: p_polys.cc:2144
void p_Normalize(poly p, const ring r)
Definition: p_polys.cc:3723
poly p_NSet(number n, const ring r)
returns the poly representing the number n, destroys n
Definition: p_polys.cc:1435
static poly p_Add_q(poly p, poly q, const ring r)
Definition: p_polys.h:893
static void p_LmDelete(poly p, const ring r)
Definition: p_polys.h:712
static poly p_Mult_q(poly p, poly q, const ring r)
Definition: p_polys.h:1051
static void p_SetCompP(poly p, int i, ring r)
Definition: p_polys.h:255
static number p_SetCoeff(poly p, number n, ring r)
Definition: p_polys.h:413
static long p_GetExp(const poly p, const unsigned long iBitmask, const int VarOffset)
get a single variable exponent @Note: the integer VarOffset encodes:
Definition: p_polys.h:470
static void p_Delete(poly *p, const ring r)
Definition: p_polys.h:858
static unsigned pLength(poly a)
Definition: p_polys.h:193
static poly pp_Mult_qq(poly p, poly q, const ring r)
Definition: p_polys.h:1088
static poly p_Copy(poly p, const ring r)
returns a copy of p
Definition: p_polys.h:813
#define p_Test(p, r)
Definition: p_polys.h:164
static char const ** rParameter(const ring r)
(r->cf->parameter)
Definition: ring.h:615
static int rPar(const ring r)
(r->cf->P)
Definition: ring.h:589
static short rVar(const ring r)
#define rVar(r) (r->N)
Definition: ring.h:582
ideal idInit(int idsize, int rank)
initialise an ideal / module
Definition: simpleideals.cc:37
#define IDELEMS(i)
Definition: simpleideals.h:24