maps_ip.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 #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>
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 */
54 BOOLEAN 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  number a = nMap((number)data, preimage_r->cf, currRing->cf);
87  {
88  n_Normalize(a, currRing->cf);
89 /*
90  number a = (number)res->data;
91  number one = nInit(1);
92  number product = nMult(a, one );
93  nDelete(&one);
94  nDelete(&a);
95  res->data=(void *)product;
96  */
97  }
98  #ifdef LDEBUG
99  n_Test(a, currRing->cf);
100  #endif
101  res->data=(void *)a;
102 
103  }
104  break;
105  case POLY_CMD:
106  case VECTOR_CMD:
107  if ((what==FETCH_CMD)&& (preimage_r->cf==currRing->cf))
108  res->data=(void *)prCopyR( (poly)data, preimage_r, currRing);
109  else
110  if ( (what==IMAP_CMD) || /*(*/ (what==FETCH_CMD) /*)*/) /* && (nMap!=nCopy)*/
111  res->data=(void *)p_PermPoly((poly)data,perm,preimage_r,currRing, nMap,par_perm,P);
112  else /*if (what==MAP_CMD)*/
113  {
114  p_Test((poly)data,preimage_r);
115  matrix s=mpNew(N,maMaxDeg_P((poly)data, preimage_r));
116  res->data=(void *)maEval(theMap, (poly)data, preimage_r, nMap, (ideal)s, currRing);
117  idDelete((ideal *)&s);
118  }
119  if (nCoeff_is_Extension(currRing->cf))
120  res->data=(void *)p_MinPolyNormalize((poly)res->data, currRing);
121  pTest((poly)res->data);
122  break;
123  case MODUL_CMD:
124  case MATRIX_CMD:
125  case IDEAL_CMD:
126  case MAP_CMD:
127  {
128  int C=((matrix)data)->cols();
129  int R;
130  if (w->rtyp==MAP_CMD) R=1;
131  else R=((matrix)data)->rows();
132  matrix m=mpNew(R,C);
133  char *tmpR=NULL;
134  if(w->rtyp==MAP_CMD)
135  {
136  tmpR=((map)data)->preimage;
137  ((matrix)data)->rank=((matrix)data)->rows();
138  }
139  if ((what==FETCH_CMD)&& (preimage_r->cf == currRing->cf))
140  {
141  for (i=R*C-1;i>=0;i--)
142  {
143  m->m[i]=prCopyR(((ideal)data)->m[i], preimage_r, currRing);
144  pTest(m->m[i]);
145  }
146  }
147  else if ((what==IMAP_CMD) || (what==FETCH_CMD))
148  {
149  for (i=R*C-1;i>=0;i--)
150  {
151  m->m[i]=p_PermPoly(((ideal)data)->m[i],perm,preimage_r,currRing,
152  nMap,par_perm,P);
153  pTest(m->m[i]);
154  }
155  }
156  else /* (what==MAP_CMD) */
157  {
158  assume(what==MAP_CMD);
159  matrix s=mpNew(N,maMaxDeg_Ma((ideal)data,preimage_r));
160  for (i=R*C-1;i>=0;i--)
161  {
162  m->m[i]=maEval(theMap, ((ideal)data)->m[i], preimage_r, nMap, (ideal)s, currRing);
163  pTest(m->m[i]);
164  }
165  idDelete((ideal *)&s);
166  }
167  if (nCoeff_is_algExt(currRing->cf))
168  {
169  for (i=R*C-1;i>=0;i--)
170  {
171  m->m[i]=p_MinPolyNormalize(m->m[i], currRing);
172  pTest(m->m[i]);
173  }
174  }
175  if(w->rtyp==MAP_CMD)
176  {
177  ((map)data)->preimage=tmpR;
178  ((map)m)->preimage=omStrDup(tmpR);
179  }
180  else
181  {
182  m->rank=((matrix)data)->rank;
183  }
184  res->data=(char *)m;
185  idTest((ideal) m);
186  break;
187  }
188 
189  case LIST_CMD:
190  {
191  lists l=(lists)data;
193  ml->Init(l->nr+1);
194  for(i=0;i<=l->nr;i++)
195  {
196  if (((l->m[i].rtyp>BEGIN_RING)&&(l->m[i].rtyp<END_RING))
197  ||(l->m[i].rtyp==LIST_CMD))
198  {
199  if (maApplyFetch(what,theMap,&ml->m[i],&l->m[i],
200  preimage_r,perm,par_perm,P,nMap))
201  {
202  ml->Clean();
204  res->rtyp=0;
205  return TRUE;
206  }
207  }
208  else
209  {
210  ml->m[i].Copy(&l->m[i]);
211  }
212  }
213  res->data=(char *)ml;
214  break;
215  }
216  default:
217  {
218  return TRUE;
219  }
220  }
221  return FALSE;
222 }
223 
224 /*2
225 * substitutes the parameter par (from 1..N) by image,
226 * does not destroy p and image
227 */
228 poly pSubstPar(poly p, int par, poly image)
229 {
230  const ring R = currRing->cf->extRing;
231  ideal theMapI = idInit(rPar(currRing),1);
232  nMapFunc nMap = n_SetMap(R->cf, currRing->cf);
233  int i;
234  for(i = rPar(currRing);i>0;i--)
235  {
236  if (i != par)
237  theMapI->m[i-1]= p_NSet(n_Param(i, currRing), currRing);
238  else
239  theMapI->m[i-1] = p_Copy(image, currRing);
240  p_Test(theMapI->m[i-1],currRing);
241  }
242  //iiWriteMatrix((matrix)theMapI,"map:",1,currRing,0);
243 
244  map theMap=(map)theMapI;
245  theMap->preimage=NULL;
246 
248  sleftv tmpW;
249  poly res=NULL;
250 
252  if (currRing->cf->rep==n_rep_rat_fct )
253  {
254  while (p!=NULL)
255  {
256  memset(v,0,sizeof(sleftv));
257 
258  number d = n_GetDenom(p_GetCoeff(p, currRing), currRing);
259  p_Test((poly)NUM((fraction)d), R);
260 
261  if ( n_IsOne (d, currRing->cf) )
262  {
263  n_Delete(&d, currRing); d = NULL;
264  }
265  else if (!p_IsConstant((poly)NUM((fraction)d), R))
266  {
267  WarnS("ignoring denominators of coefficients...");
268  n_Delete(&d, currRing); d = NULL;
269  }
270 
272  memset(&tmpW,0,sizeof(sleftv));
273  tmpW.rtyp = POLY_CMD;
274  p_Test((poly)NUM((fraction)num), R);
275 
276  tmpW.data = NUM ((fraction)num); // a copy of this poly will be used
277 
278  p_Normalize(NUM((fraction)num),R);
279  if (maApplyFetch(MAP_CMD,theMap,v,&tmpW,R,NULL,NULL,0,nMap))
280  {
281  WerrorS("map failed");
282  v->data=NULL;
283  }
284  n_Delete(&num, currRing);
285  //TODO check for memory leaks
286  poly pp = pHead(p);
287  //PrintS("map:");pWrite(pp);
288  if( d != NULL )
289  {
290  pSetCoeff(pp, n_Invers(d, currRing->cf));
291  n_Delete(&d, currRing); // d = NULL;
292  }
293  else
294  pSetCoeff(pp, nInit(1));
295 
296  //PrintS("->");pWrite((poly)(v->data));
297  poly ppp = pMult((poly)(v->data),pp);
298  //PrintS("->");pWrite(ppp);
299  res=pAdd(res,ppp);
300  pIter(p);
301  }
302  }
303  else if (currRing->cf->rep==n_rep_poly )
304  {
305  while (p!=NULL)
306  {
307  memset(v,0,sizeof(sleftv));
308 
310  memset(&tmpW,0,sizeof(sleftv));
311  tmpW.rtyp = POLY_CMD;
312  p_Test((poly)num, R);
313 
314 
315  p_Normalize((poly)num,R);
316  if (num==NULL) num=(number)R->qideal->m[0];
317  tmpW.data = num; // a copy of this poly will be used
318  if (maApplyFetch(MAP_CMD,theMap,v,&tmpW,R,NULL,NULL,0,nMap))
319  {
320  WerrorS("map failed");
321  v->data=NULL;
322  }
323  if (num!=(number)R->qideal->m[0]) n_Delete(&num, currRing);
324  //TODO check for memory leaks
325  poly pp = pHead(p);
326  //PrintS("map:");pWrite(pp);
327  pSetCoeff(pp,n_Init(1,currRing));
328  //PrintS("cf->");pWrite((poly)(v->data));
329  poly ppp = pMult((poly)(v->data),pp);
330  //PrintS("->");pWrite(ppp);
331  res=pAdd(res,ppp);
332  pIter(p);
333  }
334  }
335  else
336  {
337  WerrorS("cannot apply subst for these coeffcients");
338  }
339  idDelete((ideal *)(&theMap));
341  return res;
342 }
343 
344 /*2
345 * substitute the n-th parameter by the poly e in id
346 * does not destroy id and e
347 */
348 ideal idSubstPar(ideal id, int n, poly e)
349 {
350  int k=MATROWS((matrix)id)*MATCOLS((matrix)id);
351  ideal res=(ideal)mpNew(MATROWS((matrix)id),MATCOLS((matrix)id));
352 
353  res->rank = id->rank;
354  for(k--;k>=0;k--)
355  {
356  res->m[k]=pSubstPar(id->m[k],n,e);
357  }
358  return res;
359 }
360 
361 /*2
362 * substitutes the variable var (from 1..N) by image,
363 * does not destroy p and image
364 */
365 poly pSubstPoly(poly p, int var, poly image)
366 {
367  if (p==NULL) return NULL;
368 #ifdef HAVE_PLURAL
369  if (rIsPluralRing(currRing))
370  {
371  return pSubst(pCopy(p),var,image);
372  }
373 #endif
374  map theMap=(map)idMaxIdeal(1);
375  theMap->preimage=NULL;
376  pDelete(&(theMap->m[var-1]));
377  theMap->m[var-1]=pCopy(image);
378 
379  poly res=NULL;
380 #ifdef FAST_MAP
381  if (pGetComp(p)==0)
382  {
383  ideal src_id=idInit(1,1);
384  src_id->m[0]=p;
385 
386  char *tmp = theMap->preimage;
387  theMap->preimagei=(char*)1L; // map gets 1 as its rank (as an ideal)
388  ideal res_id=fast_map(src_id,currRing,(ideal)theMap,currRing);
389  theMap->preimage=tmp; // map gets its preimage back
390 
391  res=res_id->m[0];
392  res_id->m[0]=NULL; idDelete(&res_id);
393  src_id->m[0]=NULL; idDelete(&src_id);
394  }
395  else
396 #endif
397  {
398  sleftv tmpW;
399  memset(&tmpW,0,sizeof(sleftv));
400  tmpW.rtyp=POLY_CMD;
401  tmpW.data=p;
403  if (maApplyFetch(MAP_CMD,theMap,v,&tmpW,currRing,NULL,NULL,0,
404  n_SetMap(currRing->cf, currRing->cf)))
405  {
406  WerrorS("map failed");
407  v->data=NULL;
408  }
409  res=(poly)(v->data);
411  }
412  idDelete((ideal *)(&theMap));
413  return res;
414 }
415 
416 /*2
417 * substitute the n-th variable by the poly e in id
418 * does not destroy id and e
419 */
420 ideal idSubstPoly(ideal id, int n, poly e)
421 {
422 
423 #ifdef HAVE_PLURAL
424  if (rIsPluralRing(currRing))
425  {
426  int k=MATROWS((matrix)id)*MATCOLS((matrix)id);
427  ideal res=(ideal)mpNew(MATROWS((matrix)id),MATCOLS((matrix)id));
428  res->rank = id->rank;
429  for(k--;k>=0;k--)
430  {
431  res->m[k]=pSubst(pCopy(id->m[k]),n,e);
432  }
433  return res;
434  }
435 #endif
436  map theMap=(map)idMaxIdeal(1);
437  theMap->preimage=NULL;
438  pDelete(&(theMap->m[n-1]));
439  theMap->m[n-1]=pCopy(e);
440 
442  sleftv tmpW;
443  memset(&tmpW,0,sizeof(sleftv));
444  tmpW.rtyp=IDEAL_CMD;
445  tmpW.data=id;
446  if (maApplyFetch(MAP_CMD,theMap,v,&tmpW,currRing,NULL,NULL,0,
447  n_SetMap(currRing->cf, currRing->cf)))
448  {
449  WerrorS("map failed");
450  v->data=NULL;
451  }
452  ideal res=(ideal)(v->data);
453  idDelete((ideal *)(&theMap));
455  return res;
456 }
#define pSubst(p, n, e)
Definition: polys.h:337
#define omAllocBin(bin)
Definition: omAllocDecl.h:205
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
static FORCE_INLINE number n_GetNumerator(number &n, const coeffs r)
return the numerator of n (if elements of r are by nature not fractional, result is n) ...
Definition: coeffs.h:609
#define idMaxIdeal(D)
initialise the maximal ideal (at 0)
Definition: ideals.h:38
const CanonicalForm int s
Definition: facAbsFact.cc:55
sleftv * m
Definition: lists.h:45
Class used for (list of) interpreter objects.
Definition: subexpr.h:83
const poly a
Definition: syzextra.cc:212
#define Print
Definition: emacs.cc:83
#define pAdd(p, q)
Definition: polys.h:174
ideal idSubstPar(ideal id, int n, poly e)
Definition: maps_ip.cc:348
poly prCopyR(poly p, ring src_r, ring dest_r)
Definition: prCopy.cc:36
Definition: lists.h:22
CanonicalForm num(const CanonicalForm &f)
#define FALSE
Definition: auxiliary.h:140
return P p
Definition: myNF.cc:203
static FORCE_INLINE BOOLEAN n_IsOne(number n, const coeffs r)
TRUE iff 'n' represents the one element.
Definition: coeffs.h:469
static int rPar(const ring r)
(r->cf->P)
Definition: ring.h:547
poly p_NSet(number n, const ring r)
returns the poly representing the number n, destroys n
Definition: p_polys.cc:1448
#define pTest(p)
Definition: polys.h:387
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:539
ideal idSubstPoly(ideal id, int n, poly e)
Definition: maps_ip.cc:420
const CanonicalForm CFMap CFMap int &both_non_zero int n
Definition: cfEzgcd.cc:52
int maMaxDeg_Ma(ideal a, ring preimage_r)
Definition: maps.cc:258
#define TRUE
Definition: auxiliary.h:144
BOOLEAN maApplyFetch(int what, map theMap, leftv res, leftv w, ring preimage_r, int *perm, int *par_perm, int P, nMapFunc nMap)
Definition: maps_ip.cc:54
static FORCE_INLINE void n_Normalize(number &n, const coeffs r)
inplace-normalization of n; produces some canonical representation of n;
Definition: coeffs.h:579
void * ADDRESS
Definition: auxiliary.h:161
sleftv * leftv
Definition: structs.h:60
void WerrorS(const char *s)
Definition: feFopen.cc:23
int k
Definition: cfEzgcd.cc:93
(fraction), see transext.h
Definition: coeffs.h:113
#define WarnS
Definition: emacs.cc:81
static bool rIsPluralRing(const ring r)
we must always have this test!
Definition: ring.h:361
poly pSubstPoly(poly p, int var, poly image)
Definition: maps_ip.cc:365
#define pGetComp(p)
Component.
Definition: polys.h:37
int int kStrategy strat if(h==NULL) return NULL
static poly p_Copy(poly p, const ring r)
returns a copy of p
Definition: p_polys.h:811
poly pp
Definition: myNF.cc:296
(poly), see algext.h
Definition: coeffs.h:112
void * data
Definition: subexpr.h:89
static FORCE_INLINE number n_Param(const int iParameter, const coeffs r)
return the (iParameter^th) parameter as a NEW number NOTE: parameter numbering: 1..n_NumberOfParameters(...)
Definition: coeffs.h:801
#define pIter(p)
Definition: monomials.h:44
poly res
Definition: myNF.cc:322
ring currRing
Widely used global variable which specifies the current polynomial ring for Singular interpreter and ...
Definition: polys.cc:12
poly * m
Definition: matpol.h:19
Coefficient rings, fields and other domains suitable for Singular polynomials.
static FORCE_INLINE BOOLEAN nCoeff_is_algExt(const coeffs r)
TRUE iff r represents an algebraic extension field.
Definition: coeffs.h:911
const CanonicalForm CFMap CFMap & N
Definition: cfEzgcd.cc:49
#define assume(x)
Definition: mod2.h:405
static BOOLEAN p_IsConstant(const poly p, const ring r)
Definition: p_polys.h:1784
poly pSubstPar(poly p, int par, poly image)
Definition: maps_ip.cc:228
number(* nMapFunc)(number a, const coeffs src, const coeffs dst)
maps "a", which lives in src, into dst
Definition: coeffs.h:72
ip_smatrix * matrix
#define n_Test(a, r)
BOOLEAN n_Test(number a, const coeffs r)
Definition: coeffs.h:923
void Copy(leftv e)
Definition: subexpr.cc:643
static FORCE_INLINE number n_Invers(number a, const coeffs r)
return the multiplicative inverse of 'a'; raise an error if 'a' is not invertible ...
Definition: coeffs.h:565
int m
Definition: cfEzgcd.cc:119
omBin sleftv_bin
Definition: subexpr.cc:50
int i
Definition: cfEzgcd.cc:123
void PrintS(const char *s)
Definition: reporter.cc:294
#define pHead(p)
returns newly allocated copy of Lm(p), coef is copied, next=NULL, p might be NULL ...
Definition: polys.h:67
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:720
#define p_Test(p, r)
Definition: p_polys.h:160
INLINE_THIS void Init(int l=0)
Definition: lists.h:66
matrix mpNew(int r, int c)
create a r x c zero-matrix
Definition: matpol.cc:48
void p_Normalize(poly p, const ring r)
Definition: p_polys.cc:3621
#define omAlloc0Bin(bin)
Definition: omAllocDecl.h:206
ideal idInit(int idsize, int rank)
initialise an ideal / module
Definition: simpleideals.cc:38
poly p_PermPoly(poly p, const int *perm, const ring oldRing, const ring dst, nMapFunc nMap, const int *par_perm, int OldPar)
Definition: p_polys.cc:3928
const Variable & v
< [in] a sqrfree bivariate poly
Definition: facBivar.h:37
poly p_MinPolyNormalize(poly p, const ring r)
Definition: maps.cc:328
int nr
Definition: lists.h:43
#define MATCOLS(i)
Definition: matpol.h:28
#define NULL
Definition: omList.c:10
slists * lists
Definition: mpr_numeric.h:146
poly n_PermNumber(const number z, const int *par_perm, const int, const ring src, const ring dst)
Definition: p_polys.cc:3824
const char * Tok2Cmdname(int tok)
Definition: gentable.cc:128
#define pMult(p, q)
Definition: polys.h:178
#define R
Definition: sirandom.c:26
const CanonicalForm & w
Definition: facAbsFact.cc:55
#define pDelete(p_ptr)
Definition: polys.h:157
int rtyp
Definition: subexpr.h:92
ideal fast_map(ideal map_id, ring map_r, ideal image_id, ring image_r)
Definition: fast_maps.cc:354
static FORCE_INLINE number n_GetDenom(number &n, const coeffs r)
return the denominator of n (if elements of r are by nature not fractional, result is 1) ...
Definition: coeffs.h:604
int maMaxDeg_P(poly p, ring preimage_r)
Definition: maps.cc:296
void Clean(ring r=currRing)
Definition: lists.h:25
void * Data()
Definition: subexpr.cc:1097
Definition: tok.h:96
#define p_GetCoeff(p, r)
Definition: monomials.h:57
omBin slists_bin
Definition: lists.cc:23
static FORCE_INLINE BOOLEAN nCoeff_is_Extension(const coeffs r)
Definition: coeffs.h:844
static FORCE_INLINE void n_Delete(number *p, const coeffs r)
delete 'p'
Definition: coeffs.h:456
#define MATROWS(i)
Definition: matpol.h:27
kBucketDestroy & P
Definition: myNF.cc:191
polyrec * poly
Definition: hilb.h:10
#define omFreeBin(addr, bin)
Definition: omAllocDecl.h:259
#define nInit(i)
Definition: numbers.h:24
int perm[100]
int BOOLEAN
Definition: auxiliary.h:131
#define pSetCoeff(p, n)
deletes old coeff before setting the new one
Definition: polys.h:31
void idDelete(ideal *h)
delete an ideal
Definition: ideals.h:31
int l
Definition: cfEzgcd.cc:94
poly maEval(map theMap, poly p, ring preimage_r, nMapFunc nMap, ideal s, const ring dst_r)
Definition: maps.cc:121
long rank
Definition: matpol.h:20
#define pCopy(p)
return a copy of the poly
Definition: polys.h:156
#define idTest(id)
Definition: ideals.h:63
#define omStrDup(s)
Definition: omAllocDecl.h:263