My Project  UNKNOWN_GIT_VERSION
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 #include "omalloc/omalloc.h"
15 
16 #include "factory/factory.h"
17 
18 #include "coeffs/coeffs.h"
19 
20 #include "coeffs/longrat.h" // snumber is necessary
21 
23 #include "polys/sbuckets.h"
24 #include "polys/clapconv.h"
25 
26 #include "simpleideals.h"
27 
28 #define TRANSEXT_PRIVATES
30 
31 void out_cf(const char *s1,const CanonicalForm &f,const char *s2);
32 
33 static void conv_RecPP ( const CanonicalForm & f, int * exp, sBucket_pt result, ring r );
34 
35 static void convRecTrP ( const CanonicalForm & f, int * exp, poly & result, int offs, const ring r );
36 
37 //static void convRecGFGF ( const CanonicalForm & f, int * exp, poly & result );
38 
39 static number convFactoryNSingAN( const CanonicalForm &f, const ring r);
40 
41 poly convFactoryPSingP ( const CanonicalForm & f, const ring r )
42 {
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  if (f.isZero())
57  return;
58  if ( ! f.inCoeffDomain() )
59  {
60  int l = f.level();
61  for ( CFIterator i = f; i.hasTerms(); i++ )
62  {
63  exp[l] = i.exp();
64  conv_RecPP( i.coeff(), exp, result, r );
65  }
66  exp[l] = 0;
67  }
68  else
69  {
70  number n=r->cf->convFactoryNSingN(f, r->cf);
71  if ( n_IsZero(n, r->cf) )
72  {
73  n_Delete(&n,r->cf);
74  }
75  else
76  {
77  poly term = p_Init(r);
78  //pNext( term ) = NULL; // done by p_Init
79  pGetCoeff(term)=n;
80  p_SetExpV(term,exp,r);
82  }
83  }
84 }
85 
86 CanonicalForm convSingPFactoryP( poly p, const ring r )
87 {
89  int e, n = rVar(r);
90  BOOLEAN setChar=TRUE;
91 
92  p=pReverse(p);
93  poly op=p;
94  while ( p!=NULL )
95  {
96  CanonicalForm term=r->cf->convSingNFactoryN(pGetCoeff( p ),setChar, r->cf);
97  if (errorreported) break;
98  setChar=FALSE;
99  for ( int i = n; i >0; i-- )
100  {
101  if ( (e = p_GetExp( p, i, r)) != 0 )
102  term *= CanonicalForm( Variable( i ), e );
103  }
104  result += term;
105  pIter( p );
106  }
107  op=pReverse(op);
108  return result;
109 }
110 
112 {
113  if (!f.isImm()) WerrorS("int overflow in det");
114  return f.intval();
115 }
116 
117 CanonicalForm convSingAPFactoryAP ( poly p , const Variable & a, const ring r)
118 {
119  CanonicalForm result = 0;
120  int e, n = r-> N;
121  int off=rPar(r);
122 
123  if (!rField_is_Zp_a(r))
124  On(SW_RATIONAL);
125  while ( p!=NULL)
126  {
127  CanonicalForm term=convSingAFactoryA(((poly)p_GetCoeff(p, r->cf->extRing)),a, r);
128  for ( int i = 1; i <= n; i++ )
129  {
130  if ( (e = p_GetExp( p, i, r )) != 0 )
131  term *= power( Variable( i + off), e );
132  }
133  result += term;
134  pIter( p );
135  }
136  return result;
137 }
138 
139 static void
140 convRecAP_R ( const CanonicalForm & f, int * exp, poly & result, int par_start, int var_start, const ring r) ;
141 
142 poly convFactoryAPSingAP_R ( const CanonicalForm & f, int par_start, int var_start, const ring r )
143 {
144  int n = rVar(r)+rPar(r)+1;
145  int * exp = (int *)omAlloc0(n*sizeof(int));
146  poly result = NULL;
147  convRecAP_R( f, exp, result,par_start, var_start, r );
148  omFreeSize((ADDRESS)exp,n*sizeof(int));
149  return result;
150 }
151 
152 poly convFactoryAPSingAP ( const CanonicalForm & f, const ring r )
153 {
154  return convFactoryAPSingAP_R(f,0,rPar(r),r);
155 }
156 
157 static void convRecAP_R ( const CanonicalForm & f, int * exp, poly & result, int par_start, int var_start, const ring r )
158 {
159  if (f.isZero())
160  return;
161  if ( ! f.inCoeffDomain() )
162  {
163  int l = f.level();
164  for ( CFIterator i = f; i.hasTerms(); i++ )
165  {
166  exp[l] = i.exp();
167  convRecAP_R( i.coeff(), exp, result, par_start, var_start, r);
168  }
169  exp[l] = 0;
170  }
171  else
172  {
173  poly z=(poly)convFactoryASingA( f,r );
174  if (z!=NULL)
175  {
176  poly term = p_Init(r);
177  //pNext( term ) = NULL; // done by p_Init
178  int i;
179  for ( i = rVar(r); i>0 ; i-- )
180  p_SetExp( term, i , exp[i+var_start],r);
181  //if (rRing_has_Comp(currRing->extRing)) p_SetComp(term, 0, currRing->extRing); // done by pInit
182  if (par_start==0)
183  {
184  for ( i = 1; i <= var_start; i++ )
185  //z->e[i-1]+=exp[i];
186  p_AddExp(z,i,exp[i],r->cf->extRing);
187  }
188  else
189  {
190  for ( i = par_start+1; i <= var_start+rPar(r); i++ )
191  //z->e[i-1]+=exp[i];
192  p_AddExp(z,i,exp[i-par_start],r->cf->extRing);
193  }
194  p_GetCoeff(term, r->cf->extRing)=(number) z;
195  p_Setm( term,r );
196  result = p_Add_q( result, term, r );
197  }
198  }
199 }
200 
201 CanonicalForm convSingAFactoryA ( poly p , const Variable & a, const ring r )
202 {
203  CanonicalForm result = 0;
204  int e;
205 
206  while ( p!=NULL )
207  {
209  if ( rField_is_Zp_a(r) )
210  {
211  term = n_Int( p_GetCoeff( p, r->cf->extRing ), r->cf->extRing->cf );
212  }
213  else
214  {
215  if ( SR_HDL(p_GetCoeff( p, r->cf->extRing )) & SR_INT )
216  term = SR_TO_INT(p_GetCoeff( p, r->cf->extRing )) ;
217  else
218  {
219  if ( p_GetCoeff( p, r->cf->extRing )->s == 3 )
220  {
221  mpz_t dummy;
222  mpz_init_set( dummy, (p_GetCoeff( p,r->cf->extRing )->z) );
223  term = make_cf( dummy );
224  }
225  else
226  {
227  // assume s==0 or s==1
228  mpz_t num, den;
229  On(SW_RATIONAL);
230  mpz_init_set( num, (p_GetCoeff( p, r->cf->extRing )->z) );
231  mpz_init_set( den, (p_GetCoeff( p, r->cf->extRing )->n) );
232  term = make_cf( num, den, ( p_GetCoeff( p, r->cf->extRing )->s != 1 ));
233  }
234  }
235  }
236  if ( (e = p_GetExp( p, 1, r->cf->extRing )) != 0 )
237  term *= power( a , e );
238  result += term;
239  p = pNext( p );
240  }
241  return result;
242 }
243 
244 static number convFactoryNSingAN( const CanonicalForm &f, const ring r)
245 {
246  assume (r != NULL);
247  assume (r->cf != NULL);
248  assume (r->cf->extRing != NULL);
249  // r->cf->extRing->cf has to be Q or Z/p (the supported types of factory)
250  return n_convFactoryNSingN( f, r->cf->extRing->cf );
251 }
252 
253 poly convFactoryASingA ( const CanonicalForm & f, const ring r )
254 {
255  poly a=NULL;
256  for( CFIterator i=f; i.hasTerms(); i++)
257  {
258  number n= convFactoryNSingAN( i.coeff(), r );
259  if (n_IsZero(n,r->cf->extRing->cf))
260  {
261  n_Delete(&n,r->cf->extRing->cf);
262  }
263  else
264  {
265  poly t= p_Init (r->cf->extRing);
266  pGetCoeff(t)=n;
267  p_SetExp(t,1,i.exp(),r->cf->extRing);
268  //p_Setm(t,r->cf->extRing);// not needed for rings with 1 variable
269  a=p_Add_q(a,t,r->cf->extRing);
270  }
271  }
272  if (a!=NULL)
273  {
274  if( r->cf->extRing != NULL )
275  if (r->cf->extRing->qideal->m[0]!=NULL)
276  {
277  poly l=r->cf->extRing->qideal->m[0];
278  if (p_GetExp(a,1,r->cf->extRing) >= p_GetExp(l,1,r->cf->extRing))
279  a = p_PolyDiv (a, l, FALSE, r->cf->extRing); // ???
280  }
281  }
282  return a;
283 }
284 
285 CanonicalForm convSingTrPFactoryP ( poly p, const ring r )
286 {
287  CanonicalForm result = 0;
288  int e, n = rVar(r);
289  int offs = rPar(r);
290 
291  while ( p!=NULL )
292  {
293  n_Normalize(p_GetCoeff(p, r), r->cf);
294 
295  // test if denominator is constant
296  if (!p_IsConstantPoly(DEN ((fraction)p_GetCoeff (p,r)),r->cf->extRing) && !errorreported)
297  WerrorS("conversion error: denominator!= 1");
298 
299  CanonicalForm term=convSingPFactoryP(NUM ((fraction)p_GetCoeff(p, r)),r->cf->extRing);
300 
301  // if denominator is not NULL it should be a constant at this point
302  if (DEN ((fraction)p_GetCoeff(p,r)) != NULL)
303  {
304  CanonicalForm den= convSingPFactoryP(DEN ((fraction)p_GetCoeff(p, r)),r->cf->extRing);
305  if (rChar (r) == 0)
306  On (SW_RATIONAL);
307  term /= den;
308  }
309 
310  for ( int i = n; i > 0; i-- )
311  {
312  if ( (e = p_GetExp( p, i,r )) != 0 )
313  term = term * power( Variable( i + offs ), e );
314  }
315  result += term;
316  p = pNext( p );
317  }
318  return result;
319 }
320 
321 poly convFactoryPSingTrP ( const CanonicalForm & f, const ring r )
322 {
323  int n = rVar(r)+1;
324  int * exp = (int*)omAlloc0(n*sizeof(int));
325  poly result = NULL;
326  convRecTrP( f, exp, result , rPar(r), r );
327  omFreeSize((ADDRESS)exp,n*sizeof(int));
328  return result;
329 }
330 
331 static void
332 convRecTrP ( const CanonicalForm & f, int * exp, poly & result , int offs, const ring r)
333 {
334  if (f.isZero())
335  return;
336  if ( f.level() > offs )
337  {
338  int l = f.level();
339  for ( CFIterator i = f; i.hasTerms(); i++ )
340  {
341  exp[l-offs] = i.exp();
342  convRecTrP( i.coeff(), exp, result, offs, r );
343  }
344  exp[l-offs] = 0;
345  }
346  else
347  {
348  poly term = p_Init(r);
349  //pNext( term ) = NULL; // done by p_Init
350  for ( int i = rVar(r); i>0; i-- )
351  p_SetExp( term, i ,exp[i], r);
352  //if (rRing_has_Comp(currRing)) p_SetComp(term, 0, currRing); // done by pInit
353  pGetCoeff(term)=ntInit(convFactoryPSingP( f, r->cf->extRing ), r->cf);
354  p_Setm( term,r );
355  result = p_Add_q( result, term,r );
356  }
357 }
358 
359 #if 0
361 convSingGFFactoryGF( poly p )
362 {
364  int e, n = pVariables;
365 
366  while ( p != NULL )
367  {
369  term = make_cf_from_gf( (int)(long)pGetCoeff( p ) );
370  //int * A=(int *)&term;
371  //Print("term=%x, == 0 ?: %d\n",*A, term.isZero());
372  for ( int i = 1; i <= n; i++ )
373  {
374  if ( (e = pGetExp( p, i )) != 0 )
375  term *= power( Variable( i ), e );
376  }
377  result += term;
378  p = pNext( p );
379  }
380  return result;
381 }
382 
383 poly
384 convFactoryGFSingGF ( const CanonicalForm & f )
385 {
386 // cerr << " f = " << f << endl;
387  int n = pVariables+1;
388  /* ASSERT( level( f ) <= pVariables, "illegal number of variables" ); */
389  int * exp = (int*)omAlloc0(n*sizeof(int));
390  poly result = NULL;
391  convRecGFGF( f, exp, result );
392  omFreeSize((ADDRESS)exp,n*sizeof(int));
393  return result;
394 }
395 
396 static void
397 convRecGFGF ( const CanonicalForm & f, int * exp, poly & result )
398 {
399  if (f.isZero())
400  return;
401  if ( ! f.inCoeffDomain() )
402  {
403  int l = f.level();
404  for ( CFIterator i = f; i.hasTerms(); i++ )
405  {
406  exp[l] = i.exp();
407  convRecGFGF( i.coeff(), exp, result );
408  }
409  exp[l] = 0;
410  }
411  else
412  {
413  poly term = pInit();
414  pNext( term ) = NULL;
415  for ( int i = 1; i <= pVariables; i++ )
416  pSetExp( term, i, exp[i]);
417  //if (rRing_has_Comp(currRing)) p_SetComp(term, 0, currRing); // done by pInit
418  pGetCoeff( term ) = (number) gf_value (f);
419  pSetm( term );
420  result = pAdd( result, term );
421  }
422 }
423 
424 #endif
FALSE
#define FALSE
Definition: auxiliary.h:94
omalloc.h
SW_RATIONAL
static const int SW_RATIONAL
set to 1 for computations over Q
Definition: cf_defs.h:28
p_GetCoeff
#define p_GetCoeff(p, r)
Definition: monomials.h:51
p_GetExp
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
f
FILE * f
Definition: checklibs.c:9
errorreported
short errorreported
Definition: feFopen.cc:23
CFIterator
class to iterate through CanonicalForm's
Definition: cf_iter.h:44
out_cf
void out_cf(const char *s1, const CanonicalForm &f, const char *s2)
Definition: cf_factor.cc:90
result
return result
Definition: facAbsBiFact.cc:76
pGetExp
#define pGetExp(p, i)
Exponent.
Definition: polys.h:41
ADDRESS
void * ADDRESS
Definition: auxiliary.h:133
num
CanonicalForm num(const CanonicalForm &f)
Definition: canonicalform.h:330
gf_value
int gf_value(const CanonicalForm &f)
Definition: singext.cc:60
simpleideals.h
power
CanonicalForm power(const CanonicalForm &f, int n)
exponentiation
Definition: canonicalform.cc:1837
n_Delete
static FORCE_INLINE void n_Delete(number *p, const coeffs r)
delete 'p'
Definition: coeffs.h:455
convFactoryAPSingAP
poly convFactoryAPSingAP(const CanonicalForm &f, const ring r)
Definition: clapconv.cc:152
clapconv.h
make_cf
CanonicalForm make_cf(const mpz_ptr n)
Definition: singext.cc:66
auxiliary.h
convSingAFactoryA
CanonicalForm convSingAFactoryA(poly p, const Variable &a, const ring r)
Definition: clapconv.cc:201
N
const CanonicalForm CFMap CFMap & N
Definition: cfEzgcd.cc:49
rField_is_Zp_a
static BOOLEAN rField_is_Zp_a(const ring r)
Definition: ring.h:520
n_IsZero
static FORCE_INLINE BOOLEAN n_IsZero(number n, const coeffs r)
TRUE iff 'n' represents the zero element.
Definition: coeffs.h:464
convSingAPFactoryAP
CanonicalForm convSingAPFactoryAP(poly p, const Variable &a, const ring r)
Definition: clapconv.cc:117
n_Normalize
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
CanonicalForm
factory's main class
Definition: canonicalform.h:77
term
Definition: int_poly.h:33
p_SetExp
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:489
p_SetExpV
static void p_SetExpV(poly p, int *ev, const ring r)
Definition: p_polys.h:1472
convRecTrP
static void convRecTrP(const CanonicalForm &f, int *exp, poly &result, int offs, const ring r)
Definition: clapconv.cc:332
rVar
static short rVar(const ring r)
#define rVar(r) (r->N)
Definition: ring.h:582
conv_RecPP
static void conv_RecPP(const CanonicalForm &f, int *exp, sBucket_pt result, ring r)
Definition: clapconv.cc:54
TRUE
#define TRUE
Definition: auxiliary.h:98
i
int i
Definition: cfEzgcd.cc:125
rChar
int rChar(ring r)
Definition: ring.cc:686
n_convFactoryNSingN
number n_convFactoryNSingN(const CanonicalForm n, const coeffs r)
Definition: numbers.cc:622
omFreeSize
#define omFreeSize(addr, size)
Definition: omAllocDecl.h:260
BOOLEAN
int BOOLEAN
Definition: auxiliary.h:85
convFactoryNSingAN
static number convFactoryNSingAN(const CanonicalForm &f, const ring r)
Definition: clapconv.cc:244
convSingTrPFactoryP
CanonicalForm convSingTrPFactoryP(poly p, const ring r)
Definition: clapconv.cc:285
sBucketCreate
sBucket_pt sBucketCreate(const ring r)
Definition: sbuckets.cc:98
convFactoryISingI
int convFactoryISingI(const CanonicalForm &f)
Definition: clapconv.cc:111
make_cf_from_gf
CanonicalForm make_cf_from_gf(const int z)
Definition: singext.cc:76
pInit
#define pInit()
allocates a new monomial and initializes everything to 0
Definition: polys.h:61
pIter
#define pIter(p)
Definition: monomials.h:38
p_polys.h
rPar
static int rPar(const ring r)
(r->cf->P)
Definition: ring.h:589
p_Init
static poly p_Init(const ring r, omBin bin)
Definition: p_polys.h:1257
p_PolyDiv
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:1817
den
CanonicalForm den(const CanonicalForm &f)
Definition: canonicalform.h:333
pAdd
#define pAdd(p, q)
Definition: polys.h:198
sBucket
Definition: sbuckets.cc:31
SR_INT
#define SR_INT
Definition: longrat.h:66
convSingPFactoryP
CanonicalForm convSingPFactoryP(poly p, const ring r)
Definition: clapconv.cc:86
exp
gmp_float exp(const gmp_float &a)
Definition: mpr_complex.cc:358
SR_TO_INT
#define SR_TO_INT(SR)
Definition: longrat.h:68
ntInit
number ntInit(long i, const coeffs cf)
Definition: transext.cc:705
transext.h
p_Add_q
static poly p_Add_q(poly p, poly q, const ring r)
Definition: p_polys.h:893
Variable
factory's class for variables
Definition: factory.h:117
sBucket_Merge_m
void sBucket_Merge_m(sBucket_pt bucket, poly p)
Definition: sbuckets.cc:129
WerrorS
void WerrorS(const char *s)
Definition: feFopen.cc:24
SR_HDL
#define SR_HDL(A)
Definition: tgb.cc:35
p_AddExp
static long p_AddExp(poly p, int v, long ee, ring r)
Definition: p_polys.h:607
assume
#define assume(x)
Definition: mod2.h:390
NULL
#define NULL
Definition: omList.c:10
pSetm
#define pSetm(p)
Definition: polys.h:265
l
int l
Definition: cfEzgcd.cc:93
n_Int
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
p_Setm
static void p_Setm(poly p, const ring r)
Definition: p_polys.h:234
convFactoryPSingTrP
poly convFactoryPSingTrP(const CanonicalForm &f, const ring r)
Definition: clapconv.cc:321
pSetExp
#define pSetExp(p, i, v)
Definition: polys.h:42
p
int p
Definition: cfModGcd.cc:4019
p_IsConstantPoly
static BOOLEAN p_IsConstantPoly(const poly p, const ring r)
Definition: p_polys.h:1943
longrat.h
NUM
@ NUM
Definition: readcf.cc:173
convFactoryASingA
poly convFactoryASingA(const CanonicalForm &f, const ring r)
Definition: clapconv.cc:253
pGetCoeff
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
sbuckets.h
convRecAP_R
static void convRecAP_R(const CanonicalForm &f, int *exp, poly &result, int par_start, int var_start, const ring r)
Definition: clapconv.cc:157
convFactoryAPSingAP_R
poly convFactoryAPSingAP_R(const CanonicalForm &f, int par_start, int var_start, const ring r)
Definition: clapconv.cc:142
pNext
#define pNext(p)
Definition: monomials.h:37
pReverse
static poly pReverse(poly p)
Definition: p_polys.h:336
omAlloc0
#define omAlloc0(size)
Definition: omAllocDecl.h:211
convFactoryPSingP
poly convFactoryPSingP(const CanonicalForm &f, const ring r)
Definition: clapconv.cc:41
On
void On(int sw)
switches
Definition: canonicalform.cc:1898
coeffs.h
sBucketDestroyMerge
void sBucketDestroyMerge(sBucket_pt bucket, poly *p, int *length)
Definition: sbuckets.h:61