source: git/factory/canonicalform.h @ f2772b

spielwiese
Last change on this file since f2772b was 1afbfe, checked in by Hans Schoenemann <hannes@…>, 10 years ago
fix: stdint.h is needed on some systems for intptr_t
  • Property mode set to 100644
File size: 10.4 KB
Line 
1/* emacs edit mode for this file is -*- C++ -*- */
2
3#ifndef INCL_CANONICALFORM_H
4#define INCL_CANONICALFORM_H
5
6// #include "config.h"
7
8#ifndef NOSTREAMIO
9# ifdef HAVE_IOSTREAM
10#  include <iostream>
11#  define OSTREAM std::ostream
12#  define ISTREAM std::istream
13# elif defined(HAVE_IOSTREAM_H)
14#  include <iostream.h>
15#  define OSTREAM ostream
16#  define ISTREAM istream
17# endif
18#endif /* NOSTREAMIO */
19
20#include <stdint.h>
21
22#include "cf_defs.h"
23#include "variable.h"
24#include <factory/cf_gmp.h>
25#include <factory/templates/ftmpl_list.h>
26#include <factory/templates/ftmpl_array.h>
27#include <factory/templates/ftmpl_afactor.h>
28#include <factory/templates/ftmpl_factor.h>
29#include <factory/templates/ftmpl_matrix.h>
30
31/*BEGINPUBLIC*/
32
33#undef CF_INLINE
34#define CF_INLINE
35#undef CF_NO_INLINE
36#define CF_NO_INLINE
37
38/*ENDPUBLIC*/
39
40#ifdef CF_USE_INLINE
41#undef CF_INLINE
42#define CF_INLINE inline
43#else
44#undef CF_INLINE
45#define CF_INLINE
46#endif
47
48/*BEGINPUBLIC*/
49
50class InternalCF;
51
52inline int is_imm ( const InternalCF * const ptr )
53{
54    // returns 0 if ptr is not immediate
55    return ( ((int)((intptr_t)ptr)) & 3 );
56}
57
58
59int initCanonicalForm( void );
60#ifndef SI_DONT_HAVE_GLOBAL_VARS
61static int cf_is_initialized_now = initCanonicalForm();
62#endif
63
64//{{{ class CanonicalForm
65class CanonicalForm
66{
67private:
68    InternalCF *value;
69public:
70    // constructors, destructors, selectors
71    CF_INLINE CanonicalForm();
72    CF_INLINE CanonicalForm( const CanonicalForm& );
73    CF_INLINE CanonicalForm( InternalCF* );
74    CF_INLINE CanonicalForm( const int );
75    CF_INLINE CanonicalForm( const long );
76    CF_INLINE CanonicalForm( const Variable & );
77    CF_INLINE CanonicalForm( const Variable &, int );
78    CanonicalForm( const char *, const int base=10 ); // use with caution - does only handle integers !!!
79
80    CF_NO_INLINE ~CanonicalForm();
81
82    InternalCF* getval() const; // use with caution !!!
83
84    CanonicalForm deepCopy() const;
85
86    void mpzval(mpz_t val) const;
87    // predicates
88    CF_NO_INLINE bool isOne() const;
89    CF_NO_INLINE bool isZero() const;
90    inline bool isImm() const { return is_imm( value ); };
91
92    bool inZ() const;
93    bool inQ() const;
94    bool inFF() const;
95    bool inGF() const;
96    bool inPP() const;
97    bool inBaseDomain() const;
98    bool inExtension() const;
99    bool inCoeffDomain() const;
100    bool inPolyDomain() const;
101    bool inQuotDomain() const;
102
103    bool isFFinGF() const;
104    bool isUnivariate() const;
105    bool isHomogeneous() const;
106
107    // conversion functions
108    long intval() const;
109    CanonicalForm mapinto () const;
110
111    CanonicalForm lc () const;
112    CanonicalForm Lc () const;
113    CanonicalForm LC () const;
114    CanonicalForm LC ( const Variable & v ) const;
115
116    int degree () const;
117    int degree ( const Variable & v ) const;
118
119    CanonicalForm tailcoeff () const;
120    CanonicalForm tailcoeff ( const Variable & v ) const;
121    int taildegree () const;
122
123    int level () const;
124    Variable mvar () const;
125
126    CanonicalForm num () const;
127    CanonicalForm den () const;
128
129    // assignment operators
130    CF_NO_INLINE CanonicalForm& operator = ( const CanonicalForm& );
131    CF_NO_INLINE CanonicalForm& operator = ( const long );
132
133    CanonicalForm& operator += ( const CanonicalForm& );
134    CanonicalForm& operator -= ( const CanonicalForm& );
135    CanonicalForm& operator *= ( const CanonicalForm& );
136    CanonicalForm& operator /= ( const CanonicalForm& );
137    CanonicalForm& operator %= ( const CanonicalForm& );
138    CanonicalForm& div ( const CanonicalForm& );
139    CanonicalForm& tryDiv (const CanonicalForm&, const CanonicalForm&, bool& );
140    CanonicalForm& mod ( const CanonicalForm& );
141
142    // evaluation operators
143    CanonicalForm operator () ( const CanonicalForm & f ) const;
144    CanonicalForm operator () ( const CanonicalForm & f, const Variable & v ) const;
145
146    CanonicalForm operator [] ( int i ) const;
147
148    CanonicalForm deriv() const;
149    CanonicalForm deriv( const Variable & x ) const;
150
151    int sign() const;
152    CanonicalForm sqrt() const;
153    int ilog2() const;
154
155    // comparison operators
156    friend bool operator == ( const CanonicalForm&, const CanonicalForm& );
157    friend bool operator != ( const CanonicalForm&, const CanonicalForm& );
158    friend bool operator > ( const CanonicalForm&, const CanonicalForm& );
159    friend bool operator < ( const CanonicalForm&, const CanonicalForm& );
160
161    // arithmetic operators
162    friend CF_NO_INLINE CanonicalForm operator - ( const CanonicalForm& );
163
164    friend void divrem ( const CanonicalForm&, const CanonicalForm&, CanonicalForm&, CanonicalForm& );
165    friend bool divremt ( const CanonicalForm&, const CanonicalForm&, CanonicalForm&, CanonicalForm& );
166    friend bool tryDivremt ( const CanonicalForm&, const CanonicalForm&, CanonicalForm&, CanonicalForm&, const CanonicalForm&, bool& );
167
168    friend CanonicalForm bgcd ( const CanonicalForm &, const CanonicalForm & );
169    friend CanonicalForm bextgcd ( const CanonicalForm &, const CanonicalForm &, CanonicalForm &, CanonicalForm & );
170
171    // input/output
172#ifndef NOSTREAMIO
173   void print( OSTREAM&, char * ) const;
174   void print( OSTREAM& ) const;
175   friend OSTREAM& operator << ( OSTREAM&, const CanonicalForm& );
176   friend ISTREAM& operator >> ( ISTREAM&, CanonicalForm& );
177#endif /* NOSTREAMIO */
178
179    // obsolete methods
180    CanonicalForm genZero() const;
181    CanonicalForm genOne() const;
182
183    friend class CFIterator;
184};
185//}}}
186
187CF_INLINE CanonicalForm
188operator + ( const CanonicalForm&, const CanonicalForm& );
189
190CF_NO_INLINE CanonicalForm
191operator - ( const CanonicalForm&, const CanonicalForm& );
192
193CF_INLINE CanonicalForm
194operator * ( const CanonicalForm&, const CanonicalForm& );
195
196CF_NO_INLINE CanonicalForm
197operator / ( const CanonicalForm&, const CanonicalForm& );
198
199CF_NO_INLINE CanonicalForm
200operator % ( const CanonicalForm&, const CanonicalForm& );
201
202CF_NO_INLINE CanonicalForm
203div ( const CanonicalForm&, const CanonicalForm& );
204
205CF_NO_INLINE CanonicalForm
206mod ( const CanonicalForm&, const CanonicalForm& );
207
208/*ENDPUBLIC*/
209
210#ifdef CF_USE_INLINE
211#include "cf_inline.cc"
212#endif
213
214/*BEGINPUBLIC*/
215
216//{{{ function declarations from canonicalform.cc
217CanonicalForm blcm ( const CanonicalForm & f, const CanonicalForm & g );
218
219CanonicalForm power ( const CanonicalForm & f, int n );
220
221CanonicalForm power ( const Variable & v, int n );
222//}}}
223
224//{{{ function declarations from cf_gcd.cc
225CanonicalForm gcd ( const CanonicalForm&, const CanonicalForm& );
226
227CanonicalForm gcd_poly ( const CanonicalForm & f, const CanonicalForm & g );
228
229CanonicalForm extgcd ( const CanonicalForm&, const CanonicalForm&, CanonicalForm&, CanonicalForm& );
230
231CanonicalForm lcm ( const CanonicalForm&, const CanonicalForm& );
232
233CanonicalForm pp ( const CanonicalForm& );
234
235CanonicalForm content ( const CanonicalForm& );
236
237CanonicalForm content ( const CanonicalForm&, const Variable& );
238
239CanonicalForm icontent ( const CanonicalForm & f );
240
241CanonicalForm vcontent ( const CanonicalForm & f, const Variable & x );
242//}}}
243
244//{{{ function declarations from cf_ops.cc
245CanonicalForm swapvar ( const CanonicalForm &, const Variable &, const Variable & );
246
247CanonicalForm replacevar ( const CanonicalForm &, const Variable &, const Variable & );
248
249int getNumVars( const CanonicalForm & f );
250
251CanonicalForm getVars( const CanonicalForm & f );
252
253CanonicalForm apply ( const CanonicalForm & f, void (*mf)( CanonicalForm &, int & ) );
254
255CanonicalForm mapdomain ( const CanonicalForm & f, CanonicalForm (*mf)( const CanonicalForm & ) );
256
257int * degrees ( const CanonicalForm & f, int * degs = 0 );
258
259int totaldegree ( const CanonicalForm & f );
260
261int totaldegree ( const CanonicalForm & f, const Variable & v1, const Variable & v2 );
262
263int size ( const CanonicalForm & f, const Variable & v );
264
265int size ( const CanonicalForm & f );
266
267CanonicalForm reduce ( const CanonicalForm& f, const CanonicalForm & M);
268//}}}
269
270//{{{ inline functions corresponding to CanonicalForm methods
271//{{{ docu
272//
273// - inline functions corresponding to CanonicalForm methods.
274//
275// These function exist for convenience only and because it is
276// more beautiful to write 'degree( f )' than 'f.degree()'.
277//
278//}}}
279inline CanonicalForm
280lc ( const CanonicalForm & f ) { return f.lc(); }
281
282inline CanonicalForm
283Lc ( const CanonicalForm & f ) { return f.Lc(); }
284
285inline CanonicalForm
286LC ( const CanonicalForm & f ) { return f.LC(); }
287
288inline CanonicalForm
289LC ( const CanonicalForm & f, const Variable & v ) { return f.LC( v ); }
290
291inline int
292degree ( const CanonicalForm & f ) { return f.degree(); }
293
294inline int
295degree ( const CanonicalForm & f, const Variable & v ) { return f.degree( v ); }
296
297inline int
298taildegree ( const CanonicalForm & f ) { return f.taildegree(); }
299
300inline CanonicalForm
301tailcoeff ( const CanonicalForm & f ) { return f.tailcoeff(); }
302
303inline CanonicalForm
304tailcoeff (const CanonicalForm& f, const Variable& v) { return f.tailcoeff(v); }
305
306inline int
307level ( const CanonicalForm & f ) { return f.level(); }
308
309inline Variable
310mvar ( const CanonicalForm & f ) { return f.mvar(); }
311
312inline CanonicalForm
313num ( const CanonicalForm & f ) { return f.num(); }
314
315inline CanonicalForm
316den ( const CanonicalForm & f ) { return f.den(); }
317
318inline int
319sign ( const CanonicalForm & a ) { return a.sign(); }
320
321inline CanonicalForm
322deriv ( const CanonicalForm & f, const Variable & x ) { return f.deriv( x ); }
323
324inline CanonicalForm
325sqrt ( const CanonicalForm & a ) { return a.sqrt(); }
326
327inline int
328ilog2 ( const CanonicalForm & a ) { return a.ilog2(); }
329
330inline CanonicalForm
331mapinto ( const CanonicalForm & f ) { return f.mapinto(); }
332//}}}
333
334//{{{ inline functions
335inline CanonicalForm
336head ( const CanonicalForm & f )
337{
338    if ( f.level() > 0 )
339        return power( f.mvar(), f.degree() ) * f.LC();
340    else
341        return f;
342}
343
344inline int
345headdegree ( const CanonicalForm & f ) { return totaldegree( head( f ) ); }
346
347
348//}}}
349
350//{{{ other function declarations
351void setCharacteristic( int c ); // -> Fp && Q
352void setCharacteristic( int c, int n ); // -> PrimePower
353void setCharacteristic( int c, int n, char name ); // -> GF(q)
354
355int getCharacteristic();
356int getGFDegree();
357CanonicalForm getGFGenerator();
358
359void On( int );
360void Off( int );
361bool isOn( int );
362//}}}
363
364//{{{ type definitions
365typedef AFactor<CanonicalForm> CFAFactor;
366typedef List <CFAFactor> CFAFList;
367typedef ListIterator<CFAFactor> CFAFListIterator;
368typedef Factor<CanonicalForm> CFFactor;
369typedef List<CFFactor> CFFList;
370typedef ListIterator<CFFactor> CFFListIterator;
371typedef List<CanonicalForm> CFList;
372typedef ListIterator<CanonicalForm> CFListIterator;
373typedef Array<CanonicalForm> CFArray;
374typedef Matrix<CanonicalForm> CFMatrix;
375//}}}
376
377/*ENDPUBLIC*/
378
379#endif /* ! INCL_CANONICALFORM_H */
Note: See TracBrowser for help on using the repository browser.