source: git/factory/canonicalform.h @ c1b52b

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