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