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