My Project
clapconv.cc
Go to the documentation of this file.
1 // emacs edit mode for this file is -*- C++ -*-
2 /****************************************
3 * Computer Algebra System SINGULAR *
4 ****************************************/
5 /*
6 * ABSTRACT: convert data between Singular and factory
7 */
8 
9 
10 
11 
12 
13 #include "misc/auxiliary.h"
14 
15 #include "factory/factory.h"
16 
17 #include "coeffs/coeffs.h"
18 
19 #include "coeffs/longrat.h" // snumber is necessary
20 
22 #include "polys/sbuckets.h"
23 #include "polys/clapconv.h"
24 
25 #include "simpleideals.h"
26 
27 #define TRANSEXT_PRIVATES
29 
30 void out_cf(const char *s1,const CanonicalForm &f,const char *s2);
31 
32 static void conv_RecPP ( const CanonicalForm & f, int * exp, sBucket_pt result, ring r );
33 
34 static void convRecTrP ( const CanonicalForm & f, int * exp, poly & result, int offs, const ring r );
35 
36 //static void convRecGFGF ( const CanonicalForm & f, int * exp, poly & result );
37 
38 static number convFactoryNSingAN( const CanonicalForm &f, const ring r);
39 
40 poly convFactoryPSingP ( const CanonicalForm & f, const ring r )
41 {
42  if (f.isZero()) return NULL;
43  int n = rVar(r)+1;
44  /* ASSERT( level( f ) <= pVariables, "illegal number of variables" ); */
45  int * exp = (int*)omAlloc0(n*sizeof(int));
46  sBucket_pt result_bucket=sBucketCreate(r);
47  conv_RecPP( f, exp, result_bucket, r );
48  poly result; int dummy;
49  sBucketDestroyMerge(result_bucket,&result,&dummy);
50  omFreeSize((ADDRESS)exp,n*sizeof(int));
51  return result;
52 }
53 
54 static void conv_RecPP ( const CanonicalForm & f, int * exp, sBucket_pt result, ring r )
55 {
56  // assume f!=0
57  if ( ! f.inCoeffDomain() )
58  {
59  int l = f.level();
60  for ( CFIterator i = f; i.hasTerms(); i++ )
61  {
62  exp[l] = i.exp();
63  conv_RecPP( i.coeff(), exp, result, r );
64  }
65  exp[l] = 0;
66  }
67  else
68  {
69  number n=r->cf->convFactoryNSingN(f, r->cf);
70  if ( n_IsZero(n, r->cf) )
71  {
72  n_Delete(&n,r->cf);
73  }
74  else
75  {
76  poly term = p_Init(r);
77  //pNext( term ) = NULL; // done by p_Init
78  pGetCoeff(term)=n;
79  p_SetExpV(term,exp,r);
81  }
82  }
83 }
84 
85 static inline void convPhalf(poly p,int l,poly &p1,poly &p2)
86 {
87  p1=p;
88  l=l/2;
89  while(l>1) { p=pNext(p); l--; }
90  p2=pNext(p);
91  pNext(p)=NULL;
92 }
93 
94 static inline poly convPunhalf(poly p1,poly p2)
95 {
96  poly p=p1;
97  while(pNext(p1)!=NULL) { p1=pNext(p1);}
98  pNext(p1)=p2;
99  return p;
100 }
101 
102 #define MIN_CONV_LEN 7
103 static CanonicalForm convSingPFactoryP_intern( poly p, int l, BOOLEAN & setChar,const ring r )
104 {
105  CanonicalForm result = 0;
106  int e, n = rVar(r);
107  assume(l==(int)pLength(p));
108 
109  if (l>MIN_CONV_LEN)
110  {
111  poly p1,p2;
112  convPhalf(p,l,p1,p2);
113  CanonicalForm P=convSingPFactoryP_intern(p1,l/2,setChar,r);
114  P+=convSingPFactoryP_intern(p2,l-l/2,setChar,r);
115  convPunhalf(p1,p2);
116  return P;
117  }
118  BOOLEAN setChar_loc=setChar;
119  setChar=FALSE;
120  while ( p!=NULL )
121  {
122  CanonicalForm term=r->cf->convSingNFactoryN(pGetCoeff( p ),setChar_loc, r->cf);
123  if (errorreported) break;
124  setChar_loc=FALSE;
125  for ( int i = 1; i <=n; i++ )
126  {
127  if ( (e = p_GetExp( p, i, r)) != 0 )
128  term *= CanonicalForm( Variable( i ), e );
129  }
130  result += term;
131  pIter( p );
132  }
133  return result;
134 }
135 
136 CanonicalForm convSingPFactoryP( poly p, const ring r )
137 {
138  BOOLEAN setChar=TRUE;
139  return convSingPFactoryP_intern(p,pLength(p),setChar,r);
140 }
141 
143 {
144  if (!f.isImm()) WerrorS("int overflow in det");
145  return f.intval();
146 }
147 
148 CanonicalForm convSingAPFactoryAP ( poly p , const Variable & a, const ring r)
149 {
150  CanonicalForm result = 0;
151  int e, n = r-> N;
152  int off=rPar(r);
153 
154  if (!rField_is_Zp_a(r))
155  On(SW_RATIONAL);
156  while ( p!=NULL)
157  {
158  CanonicalForm term=convSingAFactoryA(((poly)p_GetCoeff(p, r->cf->extRing)),a, r);
159  for ( int i = 1; i <= n; i++ )
160  {
161  if ( (e = p_GetExp( p, i, r )) != 0 )
162  term *= CanonicalForm( Variable( i + off), e );
163  }
164  result += term;
165  pIter( p );
166  }
167  return result;
168 }
169 
170 static void
171 convRecAP_R ( const CanonicalForm & f, int * exp, poly & result, int par_start, int var_start, const ring r) ;
172 
173 poly convFactoryAPSingAP_R ( const CanonicalForm & f, int par_start, int var_start, const ring r )
174 {
175  if (f.isZero()) return NULL;
176  int n = rVar(r)+rPar(r)+1;
177  int * exp = (int *)omAlloc0(n*sizeof(int));
178  poly result = NULL;
179  convRecAP_R( f, exp, result,par_start, var_start, r );
180  omFreeSize((ADDRESS)exp,n*sizeof(int));
181  return result;
182 }
183 
184 poly convFactoryAPSingAP ( const CanonicalForm & f, const ring r )
185 {
186  return convFactoryAPSingAP_R(f,0,rPar(r),r);
187 }
188 
189 static void convRecAP_R ( const CanonicalForm & f, int * exp, poly & result, int par_start, int var_start, const ring r )
190 {
191  // assume f!=0
192  if ( ! f.inCoeffDomain() )
193  {
194  int l = f.level();
195  for ( CFIterator i = f; i.hasTerms(); i++ )
196  {
197  exp[l] = i.exp();
198  convRecAP_R( i.coeff(), exp, result, par_start, var_start, r);
199  }
200  exp[l] = 0;
201  }
202  else
203  {
204  poly z=(poly)convFactoryASingA( f,r );
205  if (z!=NULL)
206  {
207  poly term = p_Init(r);
208  //pNext( term ) = NULL; // done by p_Init
209  int i;
210  for ( i = rVar(r); i>0 ; i-- )
211  p_SetExp( term, i , exp[i+var_start],r);
212  //if (rRing_has_Comp(currRing->extRing)) p_SetComp(term, 0, currRing->extRing); // done by pInit
213  if (par_start==0)
214  {
215  for ( i = 1; i <= var_start; i++ )
216  //z->e[i-1]+=exp[i];
217  p_AddExp(z,i,exp[i],r->cf->extRing);
218  }
219  else
220  {
221  for ( i = par_start+1; i <= var_start+rPar(r); i++ )
222  //z->e[i-1]+=exp[i];
223  p_AddExp(z,i,exp[i-par_start],r->cf->extRing);
224  }
225  p_GetCoeff(term, r->cf->extRing)=(number) z;
226  p_Setm( term,r );
227  result = p_Add_q( result, term, r );
228  }
229  }
230 }
231 
232 CanonicalForm convSingAFactoryA ( poly p , const Variable & a, const ring r )
233 {
234  CanonicalForm result = 0;
235  int e;
236 
237  while ( p!=NULL )
238  {
240  if ( rField_is_Zp_a(r) )
241  {
242  term = n_Int( p_GetCoeff( p, r->cf->extRing ), r->cf->extRing->cf );
243  }
244  else
245  {
246  if ( SR_HDL(p_GetCoeff( p, r->cf->extRing )) & SR_INT )
247  term = SR_TO_INT(p_GetCoeff( p, r->cf->extRing )) ;
248  else
249  {
250  if ( p_GetCoeff( p, r->cf->extRing )->s == 3 )
251  {
252  mpz_t dummy;
253  mpz_init_set( dummy, (p_GetCoeff( p,r->cf->extRing )->z) );
254  term = make_cf( dummy );
255  }
256  else
257  {
258  // assume s==0 or s==1
259  mpz_t num, den;
260  On(SW_RATIONAL);
261  mpz_init_set( num, (p_GetCoeff( p, r->cf->extRing )->z) );
262  mpz_init_set( den, (p_GetCoeff( p, r->cf->extRing )->n) );
263  term = make_cf( num, den, ( p_GetCoeff( p, r->cf->extRing )->s != 1 ));
264  }
265  }
266  }
267  if ( (e = p_GetExp( p, 1, r->cf->extRing )) != 0 )
268  term *= power( a , e );
269  result += term;
270  p = pNext( p );
271  }
272  return result;
273 }
274 
275 static number convFactoryNSingAN( const CanonicalForm &f, const ring r)
276 {
277  assume (r != NULL);
278  assume (r->cf != NULL);
279  assume (r->cf->extRing != NULL);
280  // r->cf->extRing->cf has to be Q or Z/p (the supported types of factory)
281  return n_convFactoryNSingN( f, r->cf->extRing->cf );
282 }
283 
284 poly convFactoryASingA ( const CanonicalForm & f, const ring r )
285 {
286  poly a=NULL;
287  for( CFIterator i=f; i.hasTerms(); i++)
288  {
289  number n= convFactoryNSingAN( i.coeff(), r );
290  if (n_IsZero(n,r->cf->extRing->cf))
291  {
292  n_Delete(&n,r->cf->extRing->cf);
293  }
294  else
295  {
296  poly t= p_Init (r->cf->extRing);
297  pGetCoeff(t)=n;
298  p_SetExp(t,1,i.exp(),r->cf->extRing);
299  //p_Setm(t,r->cf->extRing);// not needed for rings with 1 variable
300  a=p_Add_q(a,t,r->cf->extRing);
301  }
302  }
303  if (a!=NULL)
304  {
305  if( r->cf->extRing != NULL )
306  if (r->cf->extRing->qideal->m[0]!=NULL)
307  {
308  poly l=r->cf->extRing->qideal->m[0];
309  if (p_GetExp(a,1,r->cf->extRing) >= p_GetExp(l,1,r->cf->extRing))
310  a = p_PolyDiv (a, l, FALSE, r->cf->extRing); // ???
311  }
312  }
313  return a;
314 }
315 
316 CanonicalForm convSingTrPFactoryP ( poly p, const ring r )
317 {
318  CanonicalForm result = 0;
319  int e, n = rVar(r);
320  int offs = rPar(r);
321 
322  while ( p!=NULL )
323  {
324  //n_Normalize(p_GetCoeff(p, r), r->cf);
325 
326  // test if denominator is constant
327  if (!errorreported && !p_IsConstant(DEN ((fraction)p_GetCoeff (p,r)),r->cf->extRing))
328  WerrorS("conversion error: denominator!= 1");
329 
330  CanonicalForm term=convSingPFactoryP(NUM ((fraction)p_GetCoeff(p, r)),r->cf->extRing);
331 
332  // if denominator is not NULL it should be a constant at this point
333  if (DEN ((fraction)p_GetCoeff(p,r)) != NULL)
334  {
335  CanonicalForm den= convSingPFactoryP(DEN ((fraction)p_GetCoeff(p, r)),r->cf->extRing);
336  if (rChar (r) == 0)
337  On (SW_RATIONAL);
338  term /= den;
339  }
340 
341  for ( int i = n; i > 0; i-- )
342  {
343  if ( (e = p_GetExp( p, i,r )) != 0 )
344  term = term * power( Variable( i + offs ), e );
345  }
346  result += term;
347  p = pNext( p );
348  }
349  return result;
350 }
351 
352 BOOLEAN convSingTrP(poly p, const ring r )
353 {
354  while ( p!=NULL )
355  {
356  n_Normalize(p_GetCoeff(p, r), r->cf);
357 
358  // test if denominator is constant
359  if (!p_IsConstant(DEN ((fraction)p_GetCoeff (p,r)),r->cf->extRing))
360  return FALSE;
361  pIter(p);
362  }
363  return TRUE;
364 }
365 
366 poly convFactoryPSingTrP ( const CanonicalForm & f, const ring r )
367 {
368  if (f.isZero()) return NULL;
369  int n = rVar(r)+1;
370  int * exp = (int*)omAlloc0(n*sizeof(int));
371  poly result = NULL;
372  convRecTrP( f, exp, result , rPar(r), r );
373  omFreeSize((ADDRESS)exp,n*sizeof(int));
374  return result;
375 }
376 
377 static void
378 convRecTrP ( const CanonicalForm & f, int * exp, poly & result , int offs, const ring r)
379 {
380  // assume f!= 0
381  if ( f.level() > offs )
382  {
383  int l = f.level();
384  for ( CFIterator i = f; i.hasTerms(); i++ )
385  {
386  exp[l-offs] = i.exp();
387  convRecTrP( i.coeff(), exp, result, offs, r );
388  }
389  exp[l-offs] = 0;
390  }
391  else
392  {
393  poly term = p_Init(r);
394  //pNext( term ) = NULL; // done by p_Init
395  for ( int i = rVar(r); i>0; i-- )
396  p_SetExp( term, i ,exp[i], r);
397  //if (rRing_has_Comp(currRing)) p_SetComp(term, 0, currRing); // done by pInit
398  pGetCoeff(term)=ntInit(convFactoryPSingP( f, r->cf->extRing ), r->cf);
399  p_Setm( term,r );
400  result = p_Add_q( result, term,r );
401  }
402 }
403 
404 #if 0
406 convSingGFFactoryGF( poly p )
407 {
409  int e, n = pVariables;
410 
411  while ( p != NULL )
412  {
414  term = make_cf_from_gf( (int)(long)pGetCoeff( p ) );
415  //int * A=(int *)&term;
416  //Print("term=%x, == 0 ?: %d\n",*A, term.isZero());
417  for ( int i = 1; i <= n; i++ )
418  {
419  if ( (e = pGetExp( p, i )) != 0 )
420  term *= power( Variable( i ), e );
421  }
422  result += term;
423  p = pNext( p );
424  }
425  return result;
426 }
427 
428 poly
429 convFactoryGFSingGF ( const CanonicalForm & f )
430 {
431 // cerr << " f = " << f << endl;
432  int n = pVariables+1;
433  /* ASSERT( level( f ) <= pVariables, "illegal number of variables" ); */
434  int * exp = (int*)omAlloc0(n*sizeof(int));
435  poly result = NULL;
436  convRecGFGF( f, exp, result );
437  omFreeSize((ADDRESS)exp,n*sizeof(int));
438  return result;
439 }
440 
441 static void
442 convRecGFGF ( const CanonicalForm & f, int * exp, poly & result )
443 {
444  if (f.isZero())
445  return;
446  if ( ! f.inCoeffDomain() )
447  {
448  int l = f.level();
449  for ( CFIterator i = f; i.hasTerms(); i++ )
450  {
451  exp[l] = i.exp();
452  convRecGFGF( i.coeff(), exp, result );
453  }
454  exp[l] = 0;
455  }
456  else
457  {
458  poly term = pInit();
459  pNext( term ) = NULL;
460  for ( int i = 1; i <= pVariables; i++ )
461  pSetExp( term, i, exp[i]);
462  //if (rRing_has_Comp(currRing)) p_SetComp(term, 0, currRing); // done by pInit
463  pGetCoeff( term ) = (number) gf_value (f);
464  pSetm( term );
465  result = pAdd( result, term );
466  }
467 }
468 
469 #endif
All the auxiliary stuff.
int BOOLEAN
Definition: auxiliary.h:87
#define TRUE
Definition: auxiliary.h:100
#define FALSE
Definition: auxiliary.h:96
void * ADDRESS
Definition: auxiliary.h:119
void On(int sw)
switches
CanonicalForm power(const CanonicalForm &f, int n)
exponentiation
CanonicalForm num(const CanonicalForm &f)
CanonicalForm den(const CanonicalForm &f)
const CanonicalForm CFMap CFMap & N
Definition: cfEzgcd.cc:56
int l
Definition: cfEzgcd.cc:100
int i
Definition: cfEzgcd.cc:132
int p
Definition: cfModGcd.cc:4078
static const int SW_RATIONAL
set to 1 for computations over Q
Definition: cf_defs.h:31
FILE * f
Definition: checklibs.c:9
CanonicalForm convSingPFactoryP(poly p, const ring r)
Definition: clapconv.cc:136
BOOLEAN convSingTrP(poly p, const ring r)
Definition: clapconv.cc:352
poly convFactoryPSingTrP(const CanonicalForm &f, const ring r)
Definition: clapconv.cc:366
poly convFactoryAPSingAP(const CanonicalForm &f, const ring r)
Definition: clapconv.cc:184
static CanonicalForm convSingPFactoryP_intern(poly p, int l, BOOLEAN &setChar, const ring r)
Definition: clapconv.cc:103
CanonicalForm convSingAFactoryA(poly p, const Variable &a, const ring r)
Definition: clapconv.cc:232
static void convPhalf(poly p, int l, poly &p1, poly &p2)
Definition: clapconv.cc:85
CanonicalForm convSingAPFactoryAP(poly p, const Variable &a, const ring r)
Definition: clapconv.cc:148
poly convFactoryASingA(const CanonicalForm &f, const ring r)
Definition: clapconv.cc:284
poly convFactoryPSingP(const CanonicalForm &f, const ring r)
Definition: clapconv.cc:40
static number convFactoryNSingAN(const CanonicalForm &f, const ring r)
Definition: clapconv.cc:275
int convFactoryISingI(const CanonicalForm &f)
Definition: clapconv.cc:142
static void convRecTrP(const CanonicalForm &f, int *exp, poly &result, int offs, const ring r)
Definition: clapconv.cc:378
static void conv_RecPP(const CanonicalForm &f, int *exp, sBucket_pt result, ring r)
Definition: clapconv.cc:54
void out_cf(const char *s1, const CanonicalForm &f, const char *s2)
Definition: cf_factor.cc:99
#define MIN_CONV_LEN
Definition: clapconv.cc:102
CanonicalForm convSingTrPFactoryP(poly p, const ring r)
Definition: clapconv.cc:316
static void convRecAP_R(const CanonicalForm &f, int *exp, poly &result, int par_start, int var_start, const ring r)
Definition: clapconv.cc:189
static poly convPunhalf(poly p1, poly p2)
Definition: clapconv.cc:94
poly convFactoryAPSingAP_R(const CanonicalForm &f, int par_start, int var_start, const ring r)
Definition: clapconv.cc:173
class to iterate through CanonicalForm's
Definition: cf_iter.h:44
factory's main class
Definition: canonicalform.h:86
factory's class for variables
Definition: factory.h:127
Definition: int_poly.h:33
Coefficient rings, fields and other domains suitable for Singular polynomials.
static FORCE_INLINE long n_Int(number &n, const coeffs r)
conversion of n to an int; 0 if not possible in Z/pZ: the representing int lying in (-p/2 ....
Definition: coeffs.h:547
static FORCE_INLINE number n_convFactoryNSingN(const CanonicalForm n, const coeffs r)
Definition: coeffs.h:978
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 void n_Normalize(number &n, const coeffs r)
inplace-normalization of n; produces some canonical representation of n;
Definition: coeffs.h:578
return result
Definition: facAbsBiFact.cc:75
CanonicalForm FACTORY_PUBLIC make_cf(const mpz_ptr n)
Definition: singext.cc:66
CanonicalForm make_cf_from_gf(const int z)
Definition: singext.cc:76
int gf_value(const CanonicalForm &f)
Definition: singext.cc:60
VAR short errorreported
Definition: feFopen.cc:23
void WerrorS(const char *s)
Definition: feFopen.cc:24
#define SR_INT
Definition: longrat.h:67
#define SR_TO_INT(SR)
Definition: longrat.h:69
#define assume(x)
Definition: mod2.h:389
#define pIter(p)
Definition: monomials.h:37
#define pNext(p)
Definition: monomials.h:36
static number & pGetCoeff(poly p)
return an alias to the leading coefficient of p assumes that p != NULL NOTE: not copy
Definition: monomials.h:44
#define p_GetCoeff(p, r)
Definition: monomials.h:50
gmp_float exp(const gmp_float &a)
Definition: mpr_complex.cc:357
#define omFreeSize(addr, size)
Definition: omAllocDecl.h:260
#define omAlloc0(size)
Definition: omAllocDecl.h:211
#define NULL
Definition: omList.c:12
poly p_PolyDiv(poly &p, const poly divisor, const BOOLEAN needResult, const ring r)
assumes that p and divisor are univariate polynomials in r, mentioning the same variable; assumes div...
Definition: p_polys.cc:1866
static poly p_Add_q(poly p, poly q, const ring r)
Definition: p_polys.h:938
static long p_AddExp(poly p, int v, long ee, ring r)
Definition: p_polys.h:608
static void p_SetExpV(poly p, int *ev, const ring r)
Definition: p_polys.h:1546
static unsigned long p_SetExp(poly p, const unsigned long e, const unsigned long iBitmask, const int VarOffset)
set a single variable exponent @Note: VarOffset encodes the position in p->exp
Definition: p_polys.h:490
static void p_Setm(poly p, const ring r)
Definition: p_polys.h:235
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:471
static BOOLEAN p_IsConstant(const poly p, const ring r)
Definition: p_polys.h:2005
static unsigned pLength(poly a)
Definition: p_polys.h:191
static poly p_Init(const ring r, omBin bin)
Definition: p_polys.h:1322
#define pAdd(p, q)
Definition: polys.h:203
#define pSetm(p)
Definition: polys.h:271
#define pGetExp(p, i)
Exponent.
Definition: polys.h:41
#define pInit()
allocates a new monomial and initializes everything to 0
Definition: polys.h:61
#define pSetExp(p, i, v)
Definition: polys.h:42
@ NUM
Definition: readcf.cc:170
int rChar(ring r)
Definition: ring.cc:713
static BOOLEAN rField_is_Zp_a(const ring r)
Definition: ring.h:530
static int rPar(const ring r)
(r->cf->P)
Definition: ring.h:600
static short rVar(const ring r)
#define rVar(r) (r->N)
Definition: ring.h:593
sBucket_pt sBucketCreate(const ring r)
Definition: sbuckets.cc:96
void sBucket_Merge_m(sBucket_pt bucket, poly p)
Definition: sbuckets.cc:127
void sBucketDestroyMerge(sBucket_pt bucket, poly *p, int *length)
Definition: sbuckets.h:61
#define SR_HDL(A)
Definition: tgb.cc:35
number ntInit(long i, const coeffs cf)
Definition: transext.cc:704