source: git/factory/canonicalform.h @ 0b447e

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