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 <misc/options.h>
28 #include <kernel/GBEngine/kstd1.h>
29 #include <kernel/maps/gen_maps.h>
30 
31 #include "maps_ip.h"
32 #include "ipid.h"
33 
34 
35 #include "lists.h"
36 #include "tok.h"
37 
38 /* debug output: Tok2Cmdname in maApplyFetch*/
39 #include "ipshell.h"
40 
41 /*2
42 * maps the expression w to res,
43 * switch what: MAP_CMD: use theMap for mapping, N for preimage ring
44 * //FETCH_CMD: use pOrdPoly for mapping
45 * IMAP_CMD: use perm for mapping, N for preimage ring
46 * default: map only poly-structures,
47 * use perm and par_perm, N and P,
48 */
49 BOOLEAN maApplyFetch(int what,map theMap,leftv res, leftv w, ring preimage_r,
50  int *perm, int *par_perm, int P, nMapFunc nMap)
51 {
52  BOOLEAN use_mult=FALSE;
53 #ifdef HAVE_PLURAL
54  if ((what==IMAP_CMD)
56  && rIsPluralRing(preimage_r))
57  {
58  assume(perm!=NULL);
59  int i=1;
60  while((i<currRing->N)&&(perm[i]==0)) i++;
61  if (i<currRing->N)
62  {
63  int prev_nonnull=i;
64  i++;
65  for(;i<=currRing->N;i++)
66  {
67  if (perm[prev_nonnull] > perm[i])
68  {
69  if (TEST_V_ALLWARN)
70  {
71  Warn("imap not usable for permuting variables, use map (%s <-> %s)",currRing->names[prev_nonnull-1],currRing->names[i-1]);
72  }
73  use_mult=TRUE;
74  break;
75  }
76  else
77  prev_nonnull=i;
78  }
79  }
80  }
81 #endif
82  int i;
83  int N = preimage_r->N;
84 #if 0
85  Print("N=%d what=%s ",N,Tok2Cmdname(what));
86  if (perm!=NULL) for(i=1;i<=N;i++) Print("%d -> %d ",i,perm[i]);
87  PrintS("\n");
88  Print("P=%d ",P);
89  if (par_perm!=NULL) for(i=0;i<P;i++) Print("%d -> %d ",i,par_perm[i]);
90  PrintS("\n");
91 #endif
92 
93  void *data=w->Data();
94  res->rtyp = w->rtyp;
95  switch (w->rtyp)
96  {
97  case NUMBER_CMD:
98  if (P!=0)
99  {
100 // poly n_PermNumber(const number z, const int *par_perm, const int OldPar, const ring src, const ring dst);
101  res->data= (void *) n_PermNumber((number)data, par_perm, P, preimage_r, currRing);
102  res->rtyp=POLY_CMD;
103  if (nCoeff_is_algExt(currRing->cf))
104  res->data=(void *)p_MinPolyNormalize((poly)res->data, currRing);
105  pTest((poly) res->data);
106  }
107  else
108  {
109  assume( nMap != NULL );
110  number a = nMap((number)data, preimage_r->cf, currRing->cf);
111  if (nCoeff_is_Extension(currRing->cf))
112  {
113  n_Normalize(a, currRing->cf);
114 /*
115  number a = (number)res->data;
116  number one = nInit(1);
117  number product = nMult(a, one );
118  nDelete(&one);
119  nDelete(&a);
120  res->data=(void *)product;
121  */
122  }
123  #ifdef LDEBUG
124  n_Test(a, currRing->cf);
125  #endif
126  res->data=(void *)a;
127 
128  }
129  break;
130  case POLY_CMD:
131  case VECTOR_CMD:
132  if ((what==FETCH_CMD)&& (preimage_r->cf==currRing->cf))
133  res->data=(void *)prCopyR( (poly)data, preimage_r, currRing);
134  else
135  if ( (what==IMAP_CMD) || /*(*/ (what==FETCH_CMD) /*)*/) /* && (nMap!=nCopy)*/
136  res->data=(void *)p_PermPoly((poly)data,perm,preimage_r,currRing, nMap,par_perm,P,use_mult);
137  else /*if (what==MAP_CMD)*/
138  {
139  p_Test((poly)data,preimage_r);
140  matrix s=mpNew(N,maMaxDeg_P((poly)data, preimage_r));
141  res->data=(void *)maEval(theMap, (poly)data, preimage_r, nMap, (ideal)s, currRing);
142  idDelete((ideal *)&s);
143  }
144  if (nCoeff_is_Extension(currRing->cf))
145  res->data=(void *)p_MinPolyNormalize((poly)res->data, currRing);
146  pTest((poly)res->data);
147  break;
148  case MODUL_CMD:
149  case MATRIX_CMD:
150  case IDEAL_CMD:
151  case MAP_CMD:
152  {
153  int C=((matrix)data)->cols();
154  int R;
155  if (w->rtyp==MAP_CMD) R=1;
156  else R=((matrix)data)->rows();
157  matrix m=mpNew(R,C);
158  char *tmpR=NULL;
159  if(w->rtyp==MAP_CMD)
160  {
161  tmpR=((map)data)->preimage;
162  ((matrix)data)->rank=((matrix)data)->rows();
163  }
164  if ((what==FETCH_CMD)&& (preimage_r->cf == currRing->cf))
165  {
166  for (i=R*C-1;i>=0;i--)
167  {
168  m->m[i]=prCopyR(((ideal)data)->m[i], preimage_r, currRing);
169  pTest(m->m[i]);
170  }
171  }
172  else if ((what==IMAP_CMD) || (what==FETCH_CMD))
173  {
174  for (i=R*C-1;i>=0;i--)
175  {
176  m->m[i]=p_PermPoly(((ideal)data)->m[i],perm,preimage_r,currRing,
177  nMap,par_perm,P,use_mult);
178  pTest(m->m[i]);
179  }
180  }
181  else /* (what==MAP_CMD) */
182  {
183  assume(what==MAP_CMD);
184  matrix s=mpNew(N,maMaxDeg_Ma((ideal)data,preimage_r));
185  for (i=R*C-1;i>=0;i--)
186  {
187  m->m[i]=maEval(theMap, ((ideal)data)->m[i], preimage_r, nMap, (ideal)s, currRing);
188  pTest(m->m[i]);
189  }
190  idDelete((ideal *)&s);
191  }
192  if (nCoeff_is_algExt(currRing->cf))
193  {
194  for (i=R*C-1;i>=0;i--)
195  {
196  m->m[i]=p_MinPolyNormalize(m->m[i], currRing);
197  pTest(m->m[i]);
198  }
199  }
200  if(w->rtyp==MAP_CMD)
201  {
202  ((map)data)->preimage=tmpR;
203  ((map)m)->preimage=omStrDup(tmpR);
204  }
205  else
206  {
207  m->rank=((matrix)data)->rank;
208  }
209  res->data=(char *)m;
210  idTest((ideal) m);
211  break;
212  }
213 
214  case LIST_CMD:
215  {
216  lists l=(lists)data;
218  ml->Init(l->nr+1);
219  for(i=0;i<=l->nr;i++)
220  {
221  if (((l->m[i].rtyp>BEGIN_RING)&&(l->m[i].rtyp<END_RING))
222  ||(l->m[i].rtyp==LIST_CMD))
223  {
224  if (maApplyFetch(what,theMap,&ml->m[i],&l->m[i],
225  preimage_r,perm,par_perm,P,nMap))
226  {
227  ml->Clean();
229  res->rtyp=0;
230  return TRUE;
231  }
232  }
233  else
234  {
235  ml->m[i].Copy(&l->m[i]);
236  }
237  }
238  res->data=(char *)ml;
239  break;
240  }
241  default:
242  {
243  return TRUE;
244  }
245  }
246  return FALSE;
247 }
248 
249 /*2
250 * substitutes the parameter par (from 1..N) by image,
251 * does not destroy p and image
252 */
253 poly pSubstPar(poly p, int par, poly image)
254 {
255  const ring R = currRing->cf->extRing;
256  ideal theMapI = idInit(rPar(currRing),1);
257  nMapFunc nMap = n_SetMap(R->cf, currRing->cf);
258  int i;
259  for(i = rPar(currRing);i>0;i--)
260  {
261  if (i != par)
262  theMapI->m[i-1]= p_NSet(n_Param(i, currRing), currRing);
263  else
264  theMapI->m[i-1] = p_Copy(image, currRing);
265  p_Test(theMapI->m[i-1],currRing);
266  }
267  //iiWriteMatrix((matrix)theMapI,"map:",1,currRing,0);
268 
269  map theMap=(map)theMapI;
270  theMap->preimage=NULL;
271 
273  sleftv tmpW;
274  poly res=NULL;
275 
277  if (currRing->cf->rep==n_rep_rat_fct )
278  {
279  while (p!=NULL)
280  {
281  memset(v,0,sizeof(sleftv));
282 
283  number d = n_GetDenom(pGetCoeff(p), currRing->cf);
284  p_Test((poly)NUM((fraction)d), R);
285 
286  if ( n_IsOne (d, currRing->cf) )
287  {
288  n_Delete(&d, currRing->cf); d = NULL;
289  }
290  else if (!p_IsConstant((poly)NUM((fraction)d), R))
291  {
292  WarnS("ignoring denominators of coefficients...");
293  n_Delete(&d, currRing->cf); d = NULL;
294  }
295 
296  number num = n_GetNumerator(pGetCoeff(p), currRing->cf);
297  memset(&tmpW,0,sizeof(sleftv));
298  tmpW.rtyp = POLY_CMD;
299  p_Test((poly)NUM((fraction)num), R);
300 
301  tmpW.data = NUM ((fraction)num); // a copy of this poly will be used
302 
303  p_Normalize(NUM((fraction)num),R);
304  if (maApplyFetch(MAP_CMD,theMap,v,&tmpW,R,NULL,NULL,0,nMap))
305  {
306  WerrorS("map failed");
307  v->data=NULL;
308  }
309  n_Delete(&num, currRing->cf);
310  //TODO check for memory leaks
311  poly pp = pHead(p);
312  //PrintS("map:");pWrite(pp);
313  if( d != NULL )
314  {
315  pSetCoeff(pp, n_Invers(d, currRing->cf));
316  n_Delete(&d, currRing->cf); // d = NULL;
317  }
318  else
319  pSetCoeff(pp, nInit(1));
320 
321  //PrintS("->");pWrite((poly)(v->data));
322  poly ppp = pMult((poly)(v->data),pp);
323  //PrintS("->");pWrite(ppp);
324  res=pAdd(res,ppp);
325  pIter(p);
326  }
327  }
328  else if (currRing->cf->rep==n_rep_poly )
329  {
330  while (p!=NULL)
331  {
332  memset(v,0,sizeof(sleftv));
333 
334  number num = n_GetNumerator(pGetCoeff(p), currRing->cf);
335  memset(&tmpW,0,sizeof(sleftv));
336  tmpW.rtyp = POLY_CMD;
337  p_Test((poly)num, R);
338 
339 
340  p_Normalize((poly)num,R);
341  if (num==NULL) num=(number)R->qideal->m[0];
342  tmpW.data = num; // a copy of this poly will be used
343  if (maApplyFetch(MAP_CMD,theMap,v,&tmpW,R,NULL,NULL,0,nMap))
344  {
345  WerrorS("map failed");
346  v->data=NULL;
347  }
348  if (num!=(number)R->qideal->m[0]) n_Delete(&num, currRing->cf);
349  //TODO check for memory leaks
350  poly pp = pHead(p);
351  //PrintS("map:");pWrite(pp);
352  pSetCoeff(pp,n_Init(1,currRing->cf));
353  //PrintS("cf->");pWrite((poly)(v->data));
354  poly ppp = pMult((poly)(v->data),pp);
355  //PrintS("->");pWrite(ppp);
356  res=pAdd(res,ppp);
357  pIter(p);
358  }
359  }
360  else
361  {
362  WerrorS("cannot apply subst for these coeffcients");
363  }
364  idDelete((ideal *)(&theMap));
366  return res;
367 }
368 
369 /*2
370 * substitute the n-th parameter by the poly e in id
371 * does not destroy id and e
372 */
373 ideal idSubstPar(ideal id, int n, poly e)
374 {
375  int k=MATROWS((matrix)id)*MATCOLS((matrix)id);
376  ideal res=(ideal)mpNew(MATROWS((matrix)id),MATCOLS((matrix)id));
377 
378  res->rank = id->rank;
379  for(k--;k>=0;k--)
380  {
381  res->m[k]=pSubstPar(id->m[k],n,e);
382  }
383  return res;
384 }
385 
386 /*2
387 * substitutes the variable var (from 1..N) by image,
388 * does not destroy p and image
389 */
390 poly pSubstPoly(poly p, int var, poly image)
391 {
392  if (p==NULL) return NULL;
393 #ifdef HAVE_PLURAL
394  if (rIsPluralRing(currRing))
395  {
396  return pSubst(pCopy(p),var,image);
397  }
398 #endif
399  return p_SubstPoly(p,var,image,currRing,currRing,ndCopyMap);
400 }
401 
402 /*2
403 * substitute the n-th variable by the poly e in id
404 * does not destroy id and e
405 */
406 ideal idSubstPoly(ideal id, int n, poly e)
407 {
408 
409 #ifdef HAVE_PLURAL
410  if (rIsPluralRing(currRing))
411  {
412  int k=MATROWS((matrix)id)*MATCOLS((matrix)id);
413  ideal res=(ideal)mpNew(MATROWS((matrix)id),MATCOLS((matrix)id));
414  res->rank = id->rank;
415  for(k--;k>=0;k--)
416  {
417  res->m[k]=pSubst(pCopy(id->m[k]),n,e);
418  }
419  return res;
420  }
421 #endif
422  return id_SubstPoly(id,n,e,currRing,currRing,ndCopyMap);
423 }
#define pSubst(p, n, e)
Definition: polys.h:348
#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:612
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:186
ideal idSubstPar(ideal id, int n, poly e)
Definition: maps_ip.cc:373
poly prCopyR(poly p, ring src_r, ring dest_r)
Definition: prCopy.cc:36
#define idDelete(H)
delete an ideal
Definition: ideals.h:29
Definition: lists.h:22
CanonicalForm num(const CanonicalForm &f)
if(0 > strat->sl)
Definition: myNF.cc:73
#define FALSE
Definition: auxiliary.h:94
return P p
Definition: myNF.cc:203
static FORCE_INLINE BOOLEAN n_IsOne(number n, const coeffs r)
TRUE iff &#39;n&#39; represents the one element.
Definition: coeffs.h:472
static int rPar(const ring r)
(r->cf->P)
Definition: ring.h:590
poly p_NSet(number n, const ring r)
returns the poly representing the number n, destroys n
Definition: p_polys.cc:1442
number ndCopyMap(number a, const coeffs aRing, const coeffs r)
Definition: numbers.cc:244
poly p_SubstPoly(poly p, int var, poly image, const ring preimage_r, const ring image_r, const nMapFunc nMap, matrix cache=NULL)
Definition: subst_maps.cc:39
#define pTest(p)
Definition: polys.h:398
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:542
ideal idSubstPoly(ideal id, int n, poly e)
Definition: maps_ip.cc:406
int maMaxDeg_Ma(ideal a, ring preimage_r)
Definition: maps.cc:258
#define TRUE
Definition: auxiliary.h:98
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:49
static FORCE_INLINE void n_Normalize(number &n, const coeffs r)
inplace-normalization of n; produces some canonical representation of n;
Definition: coeffs.h:582
void * ADDRESS
Definition: auxiliary.h:115
sleftv * leftv
Definition: structs.h:60
void WerrorS(const char *s)
Definition: feFopen.cc:24
int k
Definition: cfEzgcd.cc:93
(fraction), see transext.h
Definition: coeffs.h:114
static number & pGetCoeff(poly p)
return an alias to the leading coefficient of p assumes that p != NULL NOTE: not copy ...
Definition: monomials.h:51
#define WarnS
Definition: emacs.cc:81
poly pSubstPoly(poly p, int var, poly image)
Definition: maps_ip.cc:390
static poly p_Copy(poly p, const ring r)
returns a copy of p
Definition: p_polys.h:804
poly pp
Definition: myNF.cc:296
(poly), see algext.h
Definition: coeffs.h:113
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:817
#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:10
poly * m
Definition: matpol.h:19
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:3977
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:927
const CanonicalForm CFMap CFMap & N
Definition: cfEzgcd.cc:49
#define assume(x)
Definition: mod2.h:394
static BOOLEAN rIsPluralRing(const ring r)
we must always have this test!
Definition: ring.h:404
static BOOLEAN p_IsConstant(const poly p, const ring r)
Definition: p_polys.h:1876
poly pSubstPar(poly p, int par, poly image)
Definition: maps_ip.cc:253
number(* nMapFunc)(number a, const coeffs src, const coeffs dst)
maps "a", which lives in src, into dst
Definition: coeffs.h:73
const ring R
Definition: DebugPrint.cc:36
ip_smatrix * matrix
#define n_Test(a, r)
BOOLEAN n_Test(number a, const coeffs r)
Definition: coeffs.h:742
void Copy(leftv e)
Definition: subexpr.cc:688
static FORCE_INLINE number n_Invers(number a, const coeffs r)
return the multiplicative inverse of &#39;a&#39;; raise an error if &#39;a&#39; is not invertible ...
Definition: coeffs.h:568
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:284
#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:725
#define p_Test(p, r)
Definition: p_polys.h:160
INLINE_THIS void Init(int l=0)
matrix mpNew(int r, int c)
create a r x c zero-matrix
Definition: matpol.cc:47
void p_Normalize(poly p, const ring r)
Definition: p_polys.cc:3672
ideal idInit(int idsize, int rank)
initialise an ideal / module
Definition: simpleideals.cc:38
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:3873
const char * Tok2Cmdname(int tok)
Definition: gentable.cc:132
#define pMult(p, q)
Definition: polys.h:190
const CanonicalForm & w
Definition: facAbsFact.cc:55
int rtyp
Definition: subexpr.h:92
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:607
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:1137
Definition: tok.h:117
omBin slists_bin
Definition: lists.cc:23
ideal id_SubstPoly(ideal id, int var, poly image, const ring preimage_r, const ring image_r, const nMapFunc nMap)
Definition: subst_maps.cc:68
static FORCE_INLINE BOOLEAN nCoeff_is_Extension(const coeffs r)
Definition: coeffs.h:863
static FORCE_INLINE void n_Delete(number *p, const coeffs r)
delete &#39;p&#39;
Definition: coeffs.h:459
#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:85
#define pSetCoeff(p, n)
deletes old coeff before setting the new one
Definition: polys.h:31
#define TEST_V_ALLWARN
Definition: options.h:135
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:168
#define idTest(id)
Definition: ideals.h:47
#define Warn
Definition: emacs.cc:80
#define omStrDup(s)
Definition: omAllocDecl.h:263