My Project
Loading...
Searching...
No Matches
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
30void out_cf(const char *s1,const CanonicalForm &f,const char *s2);
31
32static void conv_RecPP ( const CanonicalForm & f, int * exp, sBucket_pt result, ring r );
33
34static 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
38static number convFactoryNSingAN( const CanonicalForm &f, const ring r);
39
40poly 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
54static void conv_RecPP ( const CanonicalForm & f, int * exp, sBucket_pt result, ring r )
55{
56 // assume f!=0
57 if (LIKELY(! 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 coeffs cf=r->cf;
70 number n=cf->convFactoryNSingN(f, cf);
71 if (UNLIKELY( n_IsZero(n, cf) ))
72 {
73 n_Delete(&n,cf);
74 }
75 else
76 {
77 poly term = p_Init(r);
78 //pNext( term ) = NULL; // done by p_Init
79 pGetCoeff(term)=n;
82 }
83 }
84}
85
86static inline void convPhalf(poly p,int l,poly &p1,poly &p2)
87{
88 p1=p;
89 l=l/2;
90 while(l>1) { p=pNext(p); l--; }
91 p2=pNext(p);
92 pNext(p)=NULL;
93}
94
95static inline poly convPunhalf(poly p1,poly p2)
96{
97 poly p=p1;
98 while(pNext(p1)!=NULL) { p1=pNext(p1);}
99 pNext(p1)=p2;
100 return p;
101}
102
103#define MIN_CONV_LEN 7
104static CanonicalForm convSingPFactoryP_intern( poly p, int l, BOOLEAN & setChar,const ring r )
105{
107 int e, n = rVar(r);
108 assume(l==(int)pLength(p));
109
110 if (l>MIN_CONV_LEN)
111 {
112 poly p1,p2;
113 convPhalf(p,l,p1,p2);
114 CanonicalForm P=convSingPFactoryP_intern(p1,l/2,setChar,r);
115 P+=convSingPFactoryP_intern(p2,l-l/2,setChar,r);
116 convPunhalf(p1,p2);
117 return P;
118 }
119 BOOLEAN setChar_loc=setChar;
120 setChar=FALSE;
121 coeffs cf=r->cf;
122 while ( p!=NULL )
123 {
124 CanonicalForm term=cf->convSingNFactoryN(pGetCoeff( p ),setChar_loc, cf);
125 if (errorreported) break;
126 setChar_loc=FALSE;
127 for ( int i = 1; i <=n; i++ )
128 {
129 if (LIKELY( (e = p_GetExp( p, i, r)) != 0 ))
130 term *= CanonicalForm( Variable( i ), e );
131 }
132 result += term;
133 pIter( p );
134 }
135 return result;
136}
137
138CanonicalForm convSingPFactoryP( poly p, const ring r )
139{
140 if (rVar(r)>1)
141 {
142 BOOLEAN setChar=TRUE;
143 return convSingPFactoryP_intern(p,pLength(p),setChar,r);
144 }
145 else
146 {
148 BOOLEAN setChar=TRUE;
149 coeffs cf=r->cf;
150 int e;
151 while ( p!=NULL )
152 {
153 CanonicalForm term=cf->convSingNFactoryN(pGetCoeff( p ),setChar, cf);
154 if (errorreported) break;
155 setChar=FALSE;
156 if (LIKELY( (e = p_GetExp( p, 1, r)) != 0 ))
157 term *= CanonicalForm( Variable( 1 ), e );
158 result += term;
159 pIter( p );
160 }
161 return result;
162 }
163}
164
166{
167 if (!f.isImm()) WerrorS("int overflow in det");
168 return f.intval();
169}
170
171CanonicalForm convSingAPFactoryAP ( poly p , const Variable & a, const ring r)
172{
174 int e, n = r-> N;
175 int off=rPar(r);
176
177 if (!rField_is_Zp_a(r))
179 while ( p!=NULL)
180 {
181 CanonicalForm term=convSingAFactoryA(((poly)p_GetCoeff(p, r->cf->extRing)),a, r);
182 for ( int i = 1; i <= n; i++ )
183 {
184 if ( (e = p_GetExp( p, i, r )) != 0 )
185 term *= CanonicalForm( Variable( i + off), e );
186 }
187 result += term;
188 pIter( p );
189 }
190 return result;
191}
192
193static void
194convRecAP_R ( const CanonicalForm & f, int * exp, poly & result, int par_start, int var_start, const ring r) ;
195
196poly convFactoryAPSingAP_R ( const CanonicalForm & f, int par_start, int var_start, const ring r )
197{
198 if (f.isZero()) return NULL;
199 int n = rVar(r)+rPar(r)+1;
200 int * exp = (int *)omAlloc0(n*sizeof(int));
201 poly result = NULL;
202 convRecAP_R( f, exp, result,par_start, var_start, r );
203 omFreeSize((ADDRESS)exp,n*sizeof(int));
204 return result;
205}
206
207poly convFactoryAPSingAP ( const CanonicalForm & f, const ring r )
208{
209 return convFactoryAPSingAP_R(f,0,rPar(r),r);
210}
211
212static void convRecAP_R ( const CanonicalForm & f, int * exp, poly & result, int par_start, int var_start, const ring r )
213{
214 // assume f!=0
215 if ( ! f.inCoeffDomain() )
216 {
217 int l = f.level();
218 for ( CFIterator i = f; i.hasTerms(); i++ )
219 {
220 exp[l] = i.exp();
221 convRecAP_R( i.coeff(), exp, result, par_start, var_start, r);
222 }
223 exp[l] = 0;
224 }
225 else
226 {
227 poly z=(poly)convFactoryASingA( f,r );
228 if (z!=NULL)
229 {
230 poly term = p_Init(r);
231 //pNext( term ) = NULL; // done by p_Init
232 int i;
233 for ( i = rVar(r); i>0 ; i-- )
234 p_SetExp( term, i , exp[i+var_start],r);
235 //if (rRing_has_Comp(currRing->extRing)) p_SetComp(term, 0, currRing->extRing); // done by pInit
236 if (par_start==0)
237 {
238 for ( i = 1; i <= var_start; i++ )
239 //z->e[i-1]+=exp[i];
240 p_AddExp(z,i,exp[i],r->cf->extRing);
241 }
242 else
243 {
244 for ( i = par_start+1; i <= var_start+rPar(r); i++ )
245 //z->e[i-1]+=exp[i];
246 p_AddExp(z,i,exp[i-par_start],r->cf->extRing);
247 }
248 p_GetCoeff(term, r->cf->extRing)=(number) z;
249 p_Setm( term,r );
250 result = p_Add_q( result, term, r );
251 }
252 }
253}
254
255CanonicalForm convSingAFactoryA ( poly p , const Variable & a, const ring r )
256{
258 int e;
259
260 while ( p!=NULL )
261 {
263 if ( rField_is_Zp_a(r) )
264 {
265 term = n_Int( p_GetCoeff( p, r->cf->extRing ), r->cf->extRing->cf );
266 }
267 else
268 {
269 if ( SR_HDL(p_GetCoeff( p, r->cf->extRing )) & SR_INT )
270 term = SR_TO_INT(p_GetCoeff( p, r->cf->extRing )) ;
271 else
272 {
273 if ( p_GetCoeff( p, r->cf->extRing )->s == 3 )
274 {
275 mpz_t dummy;
276 mpz_init_set( dummy, (p_GetCoeff( p,r->cf->extRing )->z) );
277 term = make_cf( dummy );
278 }
279 else
280 {
281 // assume s==0 or s==1
282 mpz_t num, den;
284 mpz_init_set( num, (p_GetCoeff( p, r->cf->extRing )->z) );
285 mpz_init_set( den, (p_GetCoeff( p, r->cf->extRing )->n) );
286 term = make_cf( num, den, ( p_GetCoeff( p, r->cf->extRing )->s != 1 ));
287 }
288 }
289 }
290 if ( (e = p_GetExp( p, 1, r->cf->extRing )) != 0 )
291 term *= power( a , e );
292 result += term;
293 p = pNext( p );
294 }
295 return result;
296}
297
298static number convFactoryNSingAN( const CanonicalForm &f, const ring r)
299{
300 assume (r != NULL);
301 assume (r->cf != NULL);
302 assume (r->cf->extRing != NULL);
303 // r->cf->extRing->cf has to be Q or Z/p (the supported types of factory)
304 return n_convFactoryNSingN( f, r->cf->extRing->cf );
305}
306
307poly convFactoryASingA ( const CanonicalForm & f, const ring r )
308{
309 poly a=NULL;
310 for( CFIterator i=f; i.hasTerms(); i++)
311 {
312 number n= convFactoryNSingAN( i.coeff(), r );
313 if (n_IsZero(n,r->cf->extRing->cf))
314 {
315 n_Delete(&n,r->cf->extRing->cf);
316 }
317 else
318 {
319 poly t= p_Init (r->cf->extRing);
320 pGetCoeff(t)=n;
321 p_SetExp(t,1,i.exp(),r->cf->extRing);
322 //p_Setm(t,r->cf->extRing);// not needed for rings with 1 variable
323 a=p_Add_q(a,t,r->cf->extRing);
324 }
325 }
326 if (a!=NULL)
327 {
328 if( r->cf->extRing != NULL )
329 if (r->cf->extRing->qideal->m[0]!=NULL)
330 {
331 poly l=r->cf->extRing->qideal->m[0];
332 if (p_GetExp(a,1,r->cf->extRing) >= p_GetExp(l,1,r->cf->extRing))
333 a = p_PolyDiv (a, l, FALSE, r->cf->extRing); // ???
334 }
335 }
336 return a;
337}
338
339CanonicalForm convSingTrPFactoryP ( poly p, const ring r )
340{
342 int e, n = rVar(r);
343 int offs = rPar(r);
344
345 while ( p!=NULL )
346 {
347 //n_Normalize(p_GetCoeff(p, r), r->cf);
348
349 // test if denominator is constant
350 if (!errorreported && !p_IsConstant(DEN ((fraction)p_GetCoeff (p,r)),r->cf->extRing))
351 WerrorS("conversion error: denominator!= 1");
352
353 CanonicalForm term=convSingPFactoryP(NUM ((fraction)p_GetCoeff(p, r)),r->cf->extRing);
354
355 // if denominator is not NULL it should be a constant at this point
356 if (DEN ((fraction)p_GetCoeff(p,r)) != NULL)
357 {
358 CanonicalForm den= convSingPFactoryP(DEN ((fraction)p_GetCoeff(p, r)),r->cf->extRing);
359 if (rChar (r) == 0)
360 On (SW_RATIONAL);
361 term /= den;
362 }
363
364 for ( int i = n; i > 0; i-- )
365 {
366 if ( (e = p_GetExp( p, i,r )) != 0 )
367 term = term * power( Variable( i + offs ), e );
368 }
369 result += term;
370 p = pNext( p );
371 }
372 return result;
373}
374
375BOOLEAN convSingTrP(poly p, const ring r )
376{
377 while ( p!=NULL )
378 {
379 n_Normalize(p_GetCoeff(p, r), r->cf);
380
381 // test if denominator is constant
382 if (!p_IsConstant(DEN ((fraction)p_GetCoeff (p,r)),r->cf->extRing))
383 return FALSE;
384 pIter(p);
385 }
386 return TRUE;
387}
388
389poly convFactoryPSingTrP ( const CanonicalForm & f, const ring r )
390{
391 if (f.isZero()) return NULL;
392 int n = rVar(r)+1;
393 int * exp = (int*)omAlloc0(n*sizeof(int));
394 poly result = NULL;
395 convRecTrP( f, exp, result , rPar(r), r );
396 omFreeSize((ADDRESS)exp,n*sizeof(int));
397 return result;
398}
399
400static void
401convRecTrP ( const CanonicalForm & f, int * exp, poly & result , int offs, const ring r)
402{
403 // assume f!= 0
404 if ( f.level() > offs )
405 {
406 int l = f.level();
407 for ( CFIterator i = f; i.hasTerms(); i++ )
408 {
409 exp[l-offs] = i.exp();
410 convRecTrP( i.coeff(), exp, result, offs, r );
411 }
412 exp[l-offs] = 0;
413 }
414 else
415 {
416 poly term = p_Init(r);
417 //pNext( term ) = NULL; // done by p_Init
418 for ( int i = rVar(r); i>0; i-- )
419 p_SetExp( term, i ,exp[i], r);
420 //if (rRing_has_Comp(currRing)) p_SetComp(term, 0, currRing); // done by pInit
421 pGetCoeff(term)=ntInit(convFactoryPSingP( f, r->cf->extRing ), r->cf);
422 p_Setm( term,r );
423 result = p_Add_q( result, term,r );
424 }
425}
426
427#if 0
429convSingGFFactoryGF( poly p )
430{
432 int e, n = pVariables;
433
434 while ( p != NULL )
435 {
437 term = make_cf_from_gf( (int)(long)pGetCoeff( p ) );
438 //int * A=(int *)&term;
439 //Print("term=%x, == 0 ?: %d\n",*A, term.isZero());
440 for ( int i = 1; i <= n; i++ )
441 {
442 if ( (e = pGetExp( p, i )) != 0 )
443 term *= power( Variable( i ), e );
444 }
445 result += term;
446 p = pNext( p );
447 }
448 return result;
449}
450
451poly
452convFactoryGFSingGF ( const CanonicalForm & f )
453{
454// cerr << " f = " << f << endl;
455 int n = pVariables+1;
456 /* ASSERT( level( f ) <= pVariables, "illegal number of variables" ); */
457 int * exp = (int*)omAlloc0(n*sizeof(int));
458 poly result = NULL;
459 convRecGFGF( f, exp, result );
460 omFreeSize((ADDRESS)exp,n*sizeof(int));
461 return result;
462}
463
464static void
465convRecGFGF ( const CanonicalForm & f, int * exp, poly & result )
466{
467 if (f.isZero())
468 return;
469 if ( ! f.inCoeffDomain() )
470 {
471 int l = f.level();
472 for ( CFIterator i = f; i.hasTerms(); i++ )
473 {
474 exp[l] = i.exp();
475 convRecGFGF( i.coeff(), exp, result );
476 }
477 exp[l] = 0;
478 }
479 else
480 {
481 poly term = pInit();
482 pNext( term ) = NULL;
483 for ( int i = 1; i <= pVariables; i++ )
484 pSetExp( term, i, exp[i]);
485 //if (rRing_has_Comp(currRing)) p_SetComp(term, 0, currRing); // done by pInit
486 pGetCoeff( term ) = (number) gf_value (f);
487 pSetm( term );
488 result = pAdd( result, term );
489 }
490}
491
492#endif
All the auxiliary stuff.
#define UNLIKELY(X)
Definition auxiliary.h:405
int BOOLEAN
Definition auxiliary.h:88
#define TRUE
Definition auxiliary.h:101
#define FALSE
Definition auxiliary.h:97
#define LIKELY(X)
Definition auxiliary.h:404
void * ADDRESS
Definition auxiliary.h:120
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:4086
CanonicalForm cf
Definition cfModGcd.cc:4091
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:138
BOOLEAN convSingTrP(poly p, const ring r)
Definition clapconv.cc:375
poly convFactoryPSingTrP(const CanonicalForm &f, const ring r)
Definition clapconv.cc:389
poly convFactoryAPSingAP(const CanonicalForm &f, const ring r)
Definition clapconv.cc:207
static CanonicalForm convSingPFactoryP_intern(poly p, int l, BOOLEAN &setChar, const ring r)
Definition clapconv.cc:104
CanonicalForm convSingAFactoryA(poly p, const Variable &a, const ring r)
Definition clapconv.cc:255
static void convPhalf(poly p, int l, poly &p1, poly &p2)
Definition clapconv.cc:86
CanonicalForm convSingAPFactoryAP(poly p, const Variable &a, const ring r)
Definition clapconv.cc:171
poly convFactoryASingA(const CanonicalForm &f, const ring r)
Definition clapconv.cc:307
poly convFactoryPSingP(const CanonicalForm &f, const ring r)
Definition clapconv.cc:40
static number convFactoryNSingAN(const CanonicalForm &f, const ring r)
Definition clapconv.cc:298
int convFactoryISingI(const CanonicalForm &f)
Definition clapconv.cc:165
static void convRecTrP(const CanonicalForm &f, int *exp, poly &result, int offs, const ring r)
Definition clapconv.cc:401
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:103
#define MIN_CONV_LEN
Definition clapconv.cc:103
CanonicalForm convSingTrPFactoryP(poly p, const ring r)
Definition clapconv.cc:339
static void convRecAP_R(const CanonicalForm &f, int *exp, poly &result, int par_start, int var_start, const ring r)
Definition clapconv.cc:212
static poly convPunhalf(poly p1, poly p2)
Definition clapconv.cc:95
poly convFactoryAPSingAP_R(const CanonicalForm &f, int par_start, int var_start, const ring r)
Definition clapconv.cc:196
class to iterate through CanonicalForm's
Definition cf_iter.h:44
factory's main class
factory's class for variables
Definition factory.h:127
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:548
static FORCE_INLINE number n_convFactoryNSingN(const CanonicalForm n, const coeffs r)
Definition coeffs.h:975
static FORCE_INLINE BOOLEAN n_IsZero(number n, const coeffs r)
TRUE iff 'n' represents the zero element.
Definition coeffs.h:468
static FORCE_INLINE void n_Delete(number *p, const coeffs r)
delete 'p'
Definition coeffs.h:459
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
return result
‘factory.h’ is the user interface to Factory.
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
#define p_GetCoeff(p, r)
Definition monomials.h:50
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
gmp_float exp(const gmp_float &a)
The main handler for Singular numbers which are suitable for Singular polynomials.
#define omFreeSize(addr, size)
#define omAlloc0(size)
#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:1874
static int pLength(poly a)
Definition p_polys.h:190
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:1560
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:1980
static poly p_Init(const ring r, omBin bin)
Definition p_polys.h:1336
#define pAdd(p, q)
Definition polys.h:204
#define pSetm(p)
Definition polys.h:272
#define pGetExp(p, i)
Exponent.
Definition polys.h:42
#define pInit()
allocates a new monomial and initializes everything to 0
Definition polys.h:62
#define pSetExp(p, i, v)
Definition polys.h:43
#define NUM
Definition readcf.cc:180
int rChar(ring r)
Definition ring.cc:718
static BOOLEAN rField_is_Zp_a(const ring r)
Definition ring.h:535
static int rPar(const ring r)
(r->cf->P)
Definition ring.h:605
static short rVar(const ring r)
#define rVar(r) (r->N)
Definition ring.h:598
sBucket_pt sBucketCreate(const ring r)
Definition sbuckets.cc:96
void sBucket_Merge_m(sBucket_pt bucket, poly p)
Definition sbuckets.cc:127
sBucket * sBucket_pt
Definition sbuckets.h:16
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