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 | |
---|
61 | class InternalCF; |
---|
62 | |
---|
63 | inline 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 | **/ |
---|
78 | class FACTORY_PUBLIC CanonicalForm |
---|
79 | #ifdef HAVE_OMALLOC |
---|
80 | #ifndef XMEMORY_H |
---|
81 | : public omallocClass |
---|
82 | #endif |
---|
83 | #endif |
---|
84 | { |
---|
85 | private: |
---|
86 | InternalCF *value; |
---|
87 | public: |
---|
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 FACTORY_PUBLIC 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 | |
---|
203 | CF_INLINE CanonicalForm |
---|
204 | operator + ( const CanonicalForm&, const CanonicalForm& ); |
---|
205 | |
---|
206 | CF_NO_INLINE CanonicalForm |
---|
207 | operator - ( const CanonicalForm&, const CanonicalForm& ); |
---|
208 | |
---|
209 | CF_INLINE CanonicalForm |
---|
210 | operator * ( const CanonicalForm&, const CanonicalForm& ); |
---|
211 | |
---|
212 | CF_NO_INLINE CanonicalForm |
---|
213 | operator / ( const CanonicalForm&, const CanonicalForm& ); |
---|
214 | |
---|
215 | CF_NO_INLINE CanonicalForm |
---|
216 | operator % ( const CanonicalForm&, const CanonicalForm& ); |
---|
217 | |
---|
218 | CF_NO_INLINE CanonicalForm |
---|
219 | div ( const CanonicalForm&, const CanonicalForm& ); |
---|
220 | |
---|
221 | CF_NO_INLINE CanonicalForm |
---|
222 | mod ( 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 |
---|
233 | CanonicalForm blcm ( const CanonicalForm & f, const CanonicalForm & g ); |
---|
234 | |
---|
235 | CanonicalForm FACTORY_PUBLIC power ( const CanonicalForm & f, int n ); |
---|
236 | |
---|
237 | CanonicalForm FACTORY_PUBLIC power ( const Variable & v, int n ); |
---|
238 | //}}} |
---|
239 | |
---|
240 | //{{{ function declarations from cf_gcd.cc |
---|
241 | CanonicalForm FACTORY_PUBLIC gcd ( const CanonicalForm&, const CanonicalForm& ); |
---|
242 | |
---|
243 | CanonicalForm gcd_poly ( const CanonicalForm & f, const CanonicalForm & g ); |
---|
244 | |
---|
245 | CanonicalForm lcm ( const CanonicalForm&, const CanonicalForm& ); |
---|
246 | |
---|
247 | CanonicalForm pp ( const CanonicalForm& ); |
---|
248 | |
---|
249 | CanonicalForm content ( const CanonicalForm& ); |
---|
250 | |
---|
251 | CanonicalForm content ( const CanonicalForm&, const Variable& ); |
---|
252 | |
---|
253 | CanonicalForm icontent ( const CanonicalForm & f ); |
---|
254 | |
---|
255 | CanonicalForm vcontent ( const CanonicalForm & f, const Variable & x ); |
---|
256 | //}}} |
---|
257 | |
---|
258 | //{{{ function declarations from cf_ops.cc |
---|
259 | CanonicalForm swapvar ( const CanonicalForm &, const Variable &, const Variable & ); |
---|
260 | |
---|
261 | CanonicalForm FACTORY_PUBLIC replacevar ( const CanonicalForm &, const Variable &, const Variable & ); |
---|
262 | |
---|
263 | int getNumVars( const CanonicalForm & f ); |
---|
264 | |
---|
265 | CanonicalForm getVars( const CanonicalForm & f ); |
---|
266 | |
---|
267 | CanonicalForm apply ( const CanonicalForm & f, void (*mf)( CanonicalForm &, int & ) ); |
---|
268 | |
---|
269 | CanonicalForm mapdomain ( const CanonicalForm & f, CanonicalForm (*mf)( const CanonicalForm & ) ); |
---|
270 | |
---|
271 | int * degrees ( const CanonicalForm & f, int * degs = 0 ); |
---|
272 | |
---|
273 | int totaldegree ( const CanonicalForm & f ); |
---|
274 | |
---|
275 | int totaldegree ( const CanonicalForm & f, const Variable & v1, const Variable & v2 ); |
---|
276 | |
---|
277 | int size ( const CanonicalForm & f, const Variable & v ); |
---|
278 | |
---|
279 | int size ( const CanonicalForm & f ); |
---|
280 | |
---|
281 | int size_maxexp ( const CanonicalForm & f, int& maxexp ); |
---|
282 | |
---|
283 | CanonicalForm reduce ( const CanonicalForm& f, const CanonicalForm & M); |
---|
284 | |
---|
285 | bool hasFirstAlgVar( const CanonicalForm & f, Variable & a); |
---|
286 | |
---|
287 | CanonicalForm 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 | //}}} |
---|
299 | inline CanonicalForm |
---|
300 | lc ( const CanonicalForm & f ) { return f.lc(); } |
---|
301 | |
---|
302 | inline CanonicalForm |
---|
303 | Lc ( const CanonicalForm & f ) { return f.Lc(); } |
---|
304 | |
---|
305 | inline CanonicalForm |
---|
306 | LC ( const CanonicalForm & f ) { return f.LC(); } |
---|
307 | |
---|
308 | inline CanonicalForm |
---|
309 | LC ( const CanonicalForm & f, const Variable & v ) { return f.LC( v ); } |
---|
310 | |
---|
311 | inline int |
---|
312 | degree ( const CanonicalForm & f ) { return f.degree(); } |
---|
313 | |
---|
314 | inline int |
---|
315 | degree ( const CanonicalForm & f, const Variable & v ) { return f.degree( v ); } |
---|
316 | |
---|
317 | inline int |
---|
318 | taildegree ( const CanonicalForm & f ) { return f.taildegree(); } |
---|
319 | |
---|
320 | inline CanonicalForm |
---|
321 | tailcoeff ( const CanonicalForm & f ) { return f.tailcoeff(); } |
---|
322 | |
---|
323 | inline CanonicalForm |
---|
324 | tailcoeff (const CanonicalForm& f, const Variable& v) { return f.tailcoeff(v); } |
---|
325 | |
---|
326 | inline int |
---|
327 | level ( const CanonicalForm & f ) { return f.level(); } |
---|
328 | |
---|
329 | inline Variable |
---|
330 | mvar ( const CanonicalForm & f ) { return f.mvar(); } |
---|
331 | |
---|
332 | inline CanonicalForm |
---|
333 | num ( const CanonicalForm & f ) { return f.num(); } |
---|
334 | |
---|
335 | inline CanonicalForm |
---|
336 | den ( const CanonicalForm & f ) { return f.den(); } |
---|
337 | |
---|
338 | inline int |
---|
339 | sign ( const CanonicalForm & a ) { return a.sign(); } |
---|
340 | |
---|
341 | inline CanonicalForm |
---|
342 | deriv ( const CanonicalForm & f, const Variable & x ) { return f.deriv( x ); } |
---|
343 | |
---|
344 | inline CanonicalForm |
---|
345 | sqrt ( const CanonicalForm & a ) { return a.sqrt(); } |
---|
346 | |
---|
347 | inline int |
---|
348 | ilog2 ( const CanonicalForm & a ) { return a.ilog2(); } |
---|
349 | |
---|
350 | inline CanonicalForm |
---|
351 | mapinto ( const CanonicalForm & f ) { return f.mapinto(); } |
---|
352 | //}}} |
---|
353 | |
---|
354 | //{{{ inline functions |
---|
355 | inline CanonicalForm |
---|
356 | head ( 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 | |
---|
364 | inline int |
---|
365 | headdegree ( const CanonicalForm & f ) { return totaldegree( head( f ) ); } |
---|
366 | |
---|
367 | |
---|
368 | //}}} |
---|
369 | |
---|
370 | //{{{ other function declarations |
---|
371 | void FACTORY_PUBLIC setCharacteristic( int c ); // -> Fp && Q |
---|
372 | void setCharacteristic( int c, int n ); // -> PrimePower |
---|
373 | void setCharacteristic( int c, int n, char name ); // -> GF(q) |
---|
374 | |
---|
375 | int FACTORY_PUBLIC getCharacteristic(); |
---|
376 | int getGFDegree(); |
---|
377 | CanonicalForm getGFGenerator(); |
---|
378 | |
---|
379 | void FACTORY_PUBLIC On( int ); |
---|
380 | void FACTORY_PUBLIC Off( int ); |
---|
381 | bool FACTORY_PUBLIC isOn( int ); |
---|
382 | //}}} |
---|
383 | |
---|
384 | //{{{ type definitions |
---|
385 | typedef AFactor<CanonicalForm> CFAFactor; |
---|
386 | typedef List <CFAFactor> CFAFList; |
---|
387 | typedef ListIterator<CFAFactor> CFAFListIterator; |
---|
388 | typedef Factor<CanonicalForm> CFFactor; |
---|
389 | typedef List<CFFactor> CFFList; |
---|
390 | typedef ListIterator<CFFactor> CFFListIterator; |
---|
391 | typedef List<CanonicalForm> CFList; |
---|
392 | typedef ListIterator<CanonicalForm> CFListIterator; |
---|
393 | typedef Array<CanonicalForm> CFArray; |
---|
394 | typedef Matrix<CanonicalForm> CFMatrix; |
---|
395 | typedef List<CFList> ListCFList; |
---|
396 | typedef ListIterator<CFList> ListCFListIterator ; |
---|
397 | typedef List<int> IntList; |
---|
398 | typedef ListIterator<int> IntListIterator; |
---|
399 | typedef List<Variable> Varlist; |
---|
400 | typedef ListIterator<Variable> VarlistIterator; |
---|
401 | typedef Array<int> Intarray; |
---|
402 | //}}} |
---|
403 | |
---|
404 | /*ENDPUBLIC*/ |
---|
405 | #endif /* ! INCL_CANONICALFORM_H */ |
---|