[493c477] | 1 | /* emacs edit mode for this file is -*- C++ -*- */ |
---|
[8519cfb] | 2 | |
---|
[b52d27] | 3 | /** |
---|
| 4 | * |
---|
| 5 | * @file cf_ops.cc |
---|
| 6 | * |
---|
| 7 | * simple structural algorithms. |
---|
| 8 | * |
---|
| 9 | * A 'structural' algorithm is an algorithm which gives |
---|
| 10 | * structural information on polynomials in contrast to a |
---|
| 11 | * 'mathematical' algorithm which calculates some mathematical |
---|
| 12 | * function. |
---|
| 13 | * |
---|
| 14 | * Compare these functions with the functions in cf_algorithm.cc, |
---|
| 15 | * which are mathematical algorithms. |
---|
| 16 | * |
---|
| 17 | * |
---|
| 18 | * Header file: canonicalform.h |
---|
| 19 | * |
---|
| 20 | **/ |
---|
[9bab9f] | 21 | |
---|
[9f7665] | 22 | |
---|
[e4fe2b] | 23 | #include "config.h" |
---|
[9f7665] | 24 | |
---|
[b973c0] | 25 | |
---|
[650f2d8] | 26 | #include "cf_assert.h" |
---|
[b973c0] | 27 | |
---|
[9bab9f] | 28 | #include "canonicalform.h" |
---|
[8519cfb] | 29 | #include "variable.h" |
---|
[9bab9f] | 30 | #include "cf_iter.h" |
---|
| 31 | |
---|
[b52d27] | 32 | /** static Variable sv_x1, sv_x2; |
---|
| 33 | * |
---|
| 34 | * sv_x1, sv_x2 - variables to swap by swapvar() and replacevar. |
---|
| 35 | * |
---|
| 36 | * These variables are initialized by swapvar() such that sv_x1 < |
---|
| 37 | * sv_x2. They are used by swapvar_between() and swapvar_rec() |
---|
| 38 | * to swap variables efficiently. |
---|
| 39 | * Furthermore, sv_x1 and sv_x2 are used by replacevar() and |
---|
| 40 | * replacevar_between(). |
---|
| 41 | * |
---|
| 42 | **/ |
---|
[8519cfb] | 43 | static Variable sv_x1, sv_x2; |
---|
[b52d27] | 44 | |
---|
| 45 | /** static void swapvar_between ( const CanonicalForm & f, CanonicalForm & result, const CanonicalForm & term, int expx2 ) |
---|
| 46 | * |
---|
| 47 | * swapvar_between() - replace occurences of sv_x1 in f with sv_x2. |
---|
| 48 | * |
---|
| 49 | * If Psi denotes the map which maps sv_x1 to sv_x2, this |
---|
| 50 | * function returns |
---|
| 51 | * |
---|
| 52 | * result + Psi(f) * term * sv_x1^expx2 |
---|
| 53 | * |
---|
| 54 | * Used by: swapvar() |
---|
| 55 | * |
---|
| 56 | **/ |
---|
[8519cfb] | 57 | static void |
---|
| 58 | swapvar_between ( const CanonicalForm & f, CanonicalForm & result, const CanonicalForm & term, int expx2 ) |
---|
[9bab9f] | 59 | { |
---|
[8519cfb] | 60 | if ( f.inCoeffDomain() || f.mvar() < sv_x1 ) |
---|
[1758fb] | 61 | // in this case, we do not have to replace anything |
---|
| 62 | result += term * power( sv_x1, expx2 ) * f; |
---|
[8519cfb] | 63 | else if ( f.mvar() == sv_x1 ) |
---|
[1758fb] | 64 | // this is where the real work is done: this iterator |
---|
| 65 | // replaces sv_x1 with sv_x2 |
---|
| 66 | for ( CFIterator i = f; i.hasTerms(); i++ ) |
---|
| 67 | result += power( sv_x2, i.exp() ) * term * power( sv_x1, expx2 ) * i.coeff(); |
---|
[e1a1ca] | 68 | else |
---|
[1758fb] | 69 | // f's level is larger than sv_x1: descend down |
---|
| 70 | for ( CFIterator i = f; i.hasTerms(); i++ ) |
---|
| 71 | swapvar_between( i.coeff(), result, term * power( f.mvar(), i.exp() ), expx2 ); |
---|
[9bab9f] | 72 | } |
---|
[4b91aa] | 73 | #if 0 |
---|
[8519cfb] | 74 | static CanonicalForm |
---|
| 75 | swapvar_between1 ( const CanonicalForm & f ) |
---|
[9bab9f] | 76 | { |
---|
[8519cfb] | 77 | if ( f.inCoeffDomain() || f.mvar() < sv_x1 ) |
---|
[1758fb] | 78 | // in this case, we do not have to replace anything |
---|
| 79 | return f; |
---|
| 80 | else if ( f.mvar() == sv_x1 ) |
---|
| 81 | { |
---|
| 82 | // this is where the real work is done: this iterator |
---|
| 83 | // replaces sv_x1 with sv_x2 |
---|
| 84 | CanonicalForm result; |
---|
| 85 | for ( CFIterator i = f; i.hasTerms(); i++ ) |
---|
| 86 | result += power( sv_x2, i.exp() ) * i.coeff(); |
---|
| 87 | return result; |
---|
[8519cfb] | 88 | } |
---|
[1758fb] | 89 | else |
---|
| 90 | { |
---|
| 91 | // f's level is larger than sv_x1: descend down |
---|
| 92 | CanonicalForm result; |
---|
| 93 | for ( CFIterator i = f; i.hasTerms(); i++ ) |
---|
| 94 | result += swapvar_between1( i.coeff() ) * power( f.mvar(), i.exp() ); |
---|
| 95 | return result; |
---|
[8519cfb] | 96 | } |
---|
[9bab9f] | 97 | } |
---|
[4b91aa] | 98 | #endif |
---|
[b52d27] | 99 | |
---|
| 100 | /** |
---|
| 101 | * |
---|
| 102 | * swapvar_between() - swap occurences of sv_x1 and sv_x2 in f. |
---|
| 103 | * |
---|
| 104 | * If Psi denotes the map which swaps sv_x1 and sv_x2, this |
---|
| 105 | * function returns |
---|
| 106 | * |
---|
| 107 | * result + Psi(f) * term |
---|
| 108 | * |
---|
| 109 | * Used by: swapvar() |
---|
| 110 | * |
---|
| 111 | **/ |
---|
[8519cfb] | 112 | static void |
---|
| 113 | swapvar_rec ( const CanonicalForm & f, CanonicalForm & result, const CanonicalForm & term ) |
---|
[9bab9f] | 114 | { |
---|
[8519cfb] | 115 | if ( f.inCoeffDomain() || f.mvar() < sv_x1 ) |
---|
[1758fb] | 116 | // in this case, we do not have to swap anything |
---|
| 117 | result += term * f; |
---|
[8519cfb] | 118 | else if ( f.mvar() == sv_x2 ) |
---|
[1758fb] | 119 | // this is where the real work is done: this iterator |
---|
| 120 | // replaces sv_x1 with sv_x2 in the coefficients of f and |
---|
| 121 | // remembers the exponents of sv_x2 in the last argument |
---|
| 122 | // of the call to swapvar_between() |
---|
| 123 | for ( CFIterator i = f; i.hasTerms(); i++ ) |
---|
| 124 | swapvar_between( i.coeff(), result, term, i.exp() ); |
---|
[8519cfb] | 125 | else if ( f.mvar() < sv_x2 ) |
---|
[1758fb] | 126 | // sv_x2 does not occur in f, but sv_x1 does. Replace it. |
---|
| 127 | swapvar_between( f, result, term, 0 ); |
---|
[8519cfb] | 128 | else |
---|
[1758fb] | 129 | // f's level is larger than sv_x2: descend down |
---|
| 130 | for ( CFIterator i = f; i.hasTerms(); i++ ) |
---|
| 131 | swapvar_rec( i.coeff(), result, term * power( f.mvar(), i.exp() ) ); |
---|
[9bab9f] | 132 | } |
---|
[4b91aa] | 133 | #if 0 |
---|
[8519cfb] | 134 | static CanonicalForm |
---|
| 135 | swapvar_rec1 ( const CanonicalForm & f ) |
---|
| 136 | { |
---|
| 137 | if ( f.inCoeffDomain() || f.mvar() < sv_x1 ) |
---|
[1758fb] | 138 | return f; |
---|
| 139 | else if ( f.mvar() == sv_x2 ) |
---|
| 140 | { |
---|
| 141 | CanonicalForm result; |
---|
| 142 | for ( CFIterator i = f; i.hasTerms(); i++ ) |
---|
| 143 | result += swapvar_between1( i.coeff() ) * power( sv_x1, i.exp() ); |
---|
| 144 | return result; |
---|
[8519cfb] | 145 | } |
---|
| 146 | else if ( f.mvar() < sv_x2 ) |
---|
[1758fb] | 147 | return swapvar_between1( f ); |
---|
| 148 | else |
---|
| 149 | { |
---|
| 150 | CanonicalForm result; |
---|
| 151 | for ( CFIterator i = f; i.hasTerms(); i++ ) |
---|
| 152 | result += swapvar_rec1( i.coeff() ) * power( f.mvar(), i.exp() ); |
---|
| 153 | return result; |
---|
[8519cfb] | 154 | } |
---|
| 155 | } |
---|
[4b91aa] | 156 | #endif |
---|
[b52d27] | 157 | |
---|
| 158 | /** |
---|
| 159 | * |
---|
| 160 | * swapvar() - swap variables x1 and x2 in f. |
---|
| 161 | * |
---|
| 162 | * Returns the image of f under the map which maps x1 to x2 and |
---|
| 163 | * x2 to x1. This is done quite efficiently because it is used |
---|
| 164 | * really often. x1 and x2 should be polynomial variables. |
---|
| 165 | * |
---|
| 166 | **/ |
---|
[9bab9f] | 167 | CanonicalForm |
---|
[8519cfb] | 168 | swapvar ( const CanonicalForm & f, const Variable & x1, const Variable & x2 ) |
---|
[9bab9f] | 169 | { |
---|
| 170 | ASSERT( x1.level() > 0 && x2.level() > 0, "cannot swap algebraic Variables" ); |
---|
| 171 | if ( f.inCoeffDomain() || x1 == x2 || ( x1 > f.mvar() && x2 > f.mvar() ) ) |
---|
[1758fb] | 172 | return f; |
---|
| 173 | else |
---|
| 174 | { |
---|
| 175 | CanonicalForm result = 0; |
---|
| 176 | if ( x1 > x2 ) |
---|
| 177 | { |
---|
| 178 | sv_x1 = x2; sv_x2 = x1; |
---|
| 179 | } |
---|
| 180 | else |
---|
| 181 | { |
---|
| 182 | sv_x1 = x1; sv_x2 = x2; |
---|
| 183 | } |
---|
| 184 | if ( f.mvar() < sv_x2 ) |
---|
| 185 | // we only have to replace sv_x1 by sv_x2 |
---|
| 186 | swapvar_between( f, result, 1, 0 ); |
---|
| 187 | else |
---|
| 188 | // we really have to swap variables |
---|
| 189 | swapvar_rec( f, result, 1 ); |
---|
| 190 | return result; |
---|
[9bab9f] | 191 | } |
---|
| 192 | } |
---|
[4b91aa] | 193 | #if 0 |
---|
[8519cfb] | 194 | CanonicalForm |
---|
| 195 | swapvar1 ( const CanonicalForm & f, const Variable & x1, const Variable & x2 ) |
---|
| 196 | { |
---|
| 197 | ASSERT( x1.level() > 0 && x2.level() > 0, "cannot swap algebraic variables" ); |
---|
| 198 | if ( f.inCoeffDomain() || x1 == x2 || ( x1 > f.mvar() && x2 > f.mvar() ) ) |
---|
[1758fb] | 199 | return f; |
---|
| 200 | else |
---|
| 201 | { |
---|
| 202 | CanonicalForm result = 0; |
---|
| 203 | if ( x1 > x2 ) { |
---|
| 204 | sv_x1 = x2; sv_x2 = x1; |
---|
| 205 | } |
---|
| 206 | else |
---|
| 207 | { |
---|
| 208 | sv_x1 = x1; sv_x2 = x2; |
---|
| 209 | } |
---|
| 210 | if ( f.mvar() < sv_x2 ) |
---|
| 211 | // we only have to replace sv_x1 by sv_x2 |
---|
| 212 | return swapvar_between1( f ); |
---|
| 213 | else |
---|
| 214 | // we really have to swap variables |
---|
| 215 | return swapvar_rec1( f ); |
---|
[8519cfb] | 216 | } |
---|
| 217 | } |
---|
[4b91aa] | 218 | #endif |
---|
[b52d27] | 219 | |
---|
| 220 | /** |
---|
| 221 | * |
---|
| 222 | * replacevar_between() - replace occurences of sv_x1 in f with sv_x2. |
---|
| 223 | * |
---|
| 224 | * This is allmost the same as swapvar_between() except that |
---|
| 225 | * sv_x1 may be an algebraic variable, so we have to test on |
---|
| 226 | * 'f.inBaseDomain()' instead of 'f.inCoeffDomain()' in the |
---|
| 227 | * beginning. |
---|
| 228 | * |
---|
| 229 | * Used by: replacevar() |
---|
| 230 | * |
---|
| 231 | **/ |
---|
[8519cfb] | 232 | static CanonicalForm |
---|
| 233 | replacevar_between ( const CanonicalForm & f ) |
---|
[9bab9f] | 234 | { |
---|
[8519cfb] | 235 | if ( f.inBaseDomain() ) |
---|
[1758fb] | 236 | return f; |
---|
[8519cfb] | 237 | |
---|
| 238 | Variable x = f.mvar(); |
---|
| 239 | |
---|
| 240 | if ( x < sv_x1 ) |
---|
[1758fb] | 241 | // in this case, we do not have to replace anything |
---|
| 242 | return f; |
---|
| 243 | else if ( x == sv_x1 ) |
---|
| 244 | { |
---|
| 245 | // this is where the real work is done: this iterator |
---|
| 246 | // replaces sv_x1 with sv_x2 |
---|
| 247 | CanonicalForm result; |
---|
| 248 | for ( CFIterator i = f; i.hasTerms(); i++ ) |
---|
| 249 | result += power( sv_x2, i.exp() ) * i.coeff(); |
---|
| 250 | return result; |
---|
[8519cfb] | 251 | } |
---|
[1758fb] | 252 | else |
---|
| 253 | { |
---|
| 254 | // f's level is larger than sv_x1: descend down |
---|
| 255 | CanonicalForm result; |
---|
| 256 | for ( CFIterator i = f; i.hasTerms(); i++ ) |
---|
| 257 | result += replacevar_between( i.coeff() ) * power( x, i.exp() ); |
---|
| 258 | return result; |
---|
[8519cfb] | 259 | } |
---|
[9bab9f] | 260 | } |
---|
[b52d27] | 261 | |
---|
| 262 | /** CanonicalForm replacevar ( const CanonicalForm & f, const Variable & x1, const Variable & x2 ) |
---|
| 263 | * |
---|
| 264 | * replacevar() - replace all occurences of x1 in f by x2. |
---|
| 265 | * |
---|
| 266 | * In contrast to swapvar(), x1 may be an algebraic variable, but |
---|
| 267 | * x2 must be a polynomial variable. |
---|
| 268 | * |
---|
| 269 | **/ |
---|
[8519cfb] | 270 | CanonicalForm |
---|
| 271 | replacevar ( const CanonicalForm & f, const Variable & x1, const Variable & x2 ) |
---|
[9bab9f] | 272 | { |
---|
[dd0d504] | 273 | //ASSERT( x2.level() > 0, "cannot replace with algebraic variable" ); |
---|
[8519cfb] | 274 | if ( f.inBaseDomain() || x1 == x2 || ( x1 > f.mvar() ) ) |
---|
[1758fb] | 275 | return f; |
---|
| 276 | else |
---|
| 277 | { |
---|
| 278 | sv_x1 = x1; |
---|
| 279 | sv_x2 = x2; |
---|
| 280 | return replacevar_between( f ); |
---|
[8519cfb] | 281 | } |
---|
| 282 | } |
---|
[b52d27] | 283 | |
---|
| 284 | /** static void fillVarsRec ( const CanonicalForm & f, int * vars ) |
---|
| 285 | * |
---|
| 286 | * fillVarsRec - fill array describing occurences of variables in f. |
---|
| 287 | * |
---|
| 288 | * Only polynomial variables are looked up. The information is |
---|
| 289 | * stored in the arrary vars. vars should be large enough to |
---|
| 290 | * hold all information, i.e. larger than the level of f. |
---|
| 291 | * |
---|
| 292 | * Used by getVars() and getNumVars(). |
---|
| 293 | * |
---|
| 294 | **/ |
---|
[8519cfb] | 295 | static void |
---|
| 296 | fillVarsRec ( const CanonicalForm & f, int * vars ) |
---|
| 297 | { |
---|
| 298 | int n; |
---|
[1758fb] | 299 | if ( (n = f.level()) > 0 ) |
---|
| 300 | { |
---|
| 301 | vars[n] = 1; |
---|
| 302 | CFIterator i; |
---|
| 303 | for ( i = f; i.hasTerms(); ++i ) |
---|
| 304 | fillVarsRec( i.coeff(), vars ); |
---|
[8519cfb] | 305 | } |
---|
| 306 | } |
---|
[b52d27] | 307 | |
---|
| 308 | /** int getNumVars ( const CanonicalForm & f ) |
---|
| 309 | * |
---|
| 310 | * getNumVars() - get number of polynomial variables in f. |
---|
| 311 | * |
---|
| 312 | **/ |
---|
[8519cfb] | 313 | int |
---|
| 314 | getNumVars ( const CanonicalForm & f ) |
---|
| 315 | { |
---|
| 316 | int n; |
---|
| 317 | if ( f.inCoeffDomain() ) |
---|
[1758fb] | 318 | return 0; |
---|
[8519cfb] | 319 | else if ( (n = f.level()) == 1 ) |
---|
[1758fb] | 320 | return 1; |
---|
| 321 | else |
---|
| 322 | { |
---|
| 323 | int * vars = new int[ n+1 ]; |
---|
| 324 | int i; |
---|
| 325 | for ( i = 0; i < n; i++ ) vars[i] = 0; |
---|
[8519cfb] | 326 | |
---|
[1758fb] | 327 | // look for variables |
---|
| 328 | for ( CFIterator I = f; I.hasTerms(); ++I ) |
---|
| 329 | fillVarsRec( I.coeff(), vars ); |
---|
[8519cfb] | 330 | |
---|
[1758fb] | 331 | // count them |
---|
| 332 | int m = 0; |
---|
| 333 | for ( i = 1; i < n; i++ ) |
---|
| 334 | if ( vars[i] != 0 ) m++; |
---|
[8519cfb] | 335 | |
---|
[1758fb] | 336 | delete [] vars; |
---|
| 337 | // do not forget to count our own variable |
---|
| 338 | return m+1; |
---|
[8519cfb] | 339 | } |
---|
| 340 | } |
---|
[b52d27] | 341 | |
---|
| 342 | /** CanonicalForm getVars ( const CanonicalForm & f ) |
---|
| 343 | * |
---|
| 344 | * getVars() - get polynomial variables of f. |
---|
| 345 | * |
---|
| 346 | * Return the product of all of them, 1 if there are not any. |
---|
| 347 | * |
---|
| 348 | **/ |
---|
[8519cfb] | 349 | CanonicalForm |
---|
| 350 | getVars ( const CanonicalForm & f ) |
---|
| 351 | { |
---|
| 352 | int n; |
---|
| 353 | if ( f.inCoeffDomain() ) |
---|
[1758fb] | 354 | return 1; |
---|
[8519cfb] | 355 | else if ( (n = f.level()) == 1 ) |
---|
[1758fb] | 356 | return Variable( 1 ); |
---|
| 357 | else |
---|
| 358 | { |
---|
| 359 | int * vars = new int[ n+1 ]; |
---|
| 360 | int i; |
---|
| 361 | for ( i = 0; i <= n; i++ ) vars[i] = 0; |
---|
[8519cfb] | 362 | |
---|
[1758fb] | 363 | // look for variables |
---|
| 364 | for ( CFIterator I = f; I.hasTerms(); ++I ) |
---|
| 365 | fillVarsRec( I.coeff(), vars ); |
---|
[8519cfb] | 366 | |
---|
[1758fb] | 367 | // multiply them all |
---|
| 368 | CanonicalForm result = 1; |
---|
| 369 | for ( i = n; i > 0; i-- ) |
---|
| 370 | if ( vars[i] != 0 ) result *= Variable( i ); |
---|
[8519cfb] | 371 | |
---|
[1758fb] | 372 | delete [] vars; |
---|
| 373 | // do not forget our own variable |
---|
| 374 | return f.mvar() * result; |
---|
[8519cfb] | 375 | } |
---|
[9bab9f] | 376 | } |
---|
[b52d27] | 377 | |
---|
| 378 | /** CanonicalForm apply ( const CanonicalForm & f, void (*mf)( CanonicalForm &, int & ) ) |
---|
| 379 | * |
---|
| 380 | * apply() - apply mf to terms of f. |
---|
| 381 | * |
---|
| 382 | * Calls mf( f[i], i ) for each term f[i]*x^i of f and builds a |
---|
| 383 | * new term from the result. If f is in a coefficient domain, |
---|
| 384 | * mf( f, i ) should result in an i == 0, since otherwise it is |
---|
| 385 | * not clear which variable to use for the resulting term. |
---|
| 386 | * |
---|
| 387 | * An example: |
---|
| 388 | * |
---|
| 389 | ~~~~~~~~~~~~~~~~~~~~~{.c} |
---|
| 390 | void |
---|
| 391 | diff( CanonicalForm & f, int & i ) |
---|
| 392 | { |
---|
| 393 | f = f * i; |
---|
| 394 | if ( i > 0 ) i--; |
---|
| 395 | } |
---|
| 396 | ~~~~~~~~~~~~~~~~~~~~~ |
---|
| 397 | * Then apply( f, diff ) is differentation of f with respect to the |
---|
| 398 | * main variable of f. |
---|
| 399 | * |
---|
| 400 | **/ |
---|
[9bab9f] | 401 | CanonicalForm |
---|
| 402 | apply ( const CanonicalForm & f, void (*mf)( CanonicalForm &, int & ) ) |
---|
| 403 | { |
---|
[1758fb] | 404 | if ( f.inCoeffDomain() ) |
---|
| 405 | { |
---|
| 406 | int exp = 0; |
---|
| 407 | CanonicalForm result = f; |
---|
| 408 | mf( result, exp ); |
---|
| 409 | ASSERT( exp == 0, "illegal result, do not know what variable to use" ); |
---|
| 410 | return result; |
---|
[9bab9f] | 411 | } |
---|
[1758fb] | 412 | else |
---|
| 413 | { |
---|
| 414 | CanonicalForm result, coeff; |
---|
| 415 | CFIterator i; |
---|
| 416 | int exp; |
---|
| 417 | Variable x = f.mvar(); |
---|
| 418 | for ( i = f; i.hasTerms(); i++ ) |
---|
| 419 | { |
---|
| 420 | coeff = i.coeff(); |
---|
| 421 | exp = i.exp(); |
---|
| 422 | mf( coeff, exp ); |
---|
| 423 | if ( ! coeff.isZero() ) |
---|
| 424 | result += power( x, exp ) * coeff; |
---|
| 425 | } |
---|
| 426 | return result; |
---|
[9bab9f] | 427 | } |
---|
| 428 | } |
---|
[b52d27] | 429 | |
---|
| 430 | /** CanonicalForm mapdomain ( const CanonicalForm & f, CanonicalForm (*mf)( const CanonicalForm & ) ) |
---|
| 431 | * |
---|
| 432 | * mapdomain() - map all coefficients of f through mf. |
---|
| 433 | * |
---|
| 434 | * Recursively descends down through f to the coefficients which |
---|
| 435 | * are in a coefficient domain mapping each such coefficient |
---|
| 436 | * through mf and returns the result. |
---|
| 437 | * |
---|
| 438 | **/ |
---|
[8519cfb] | 439 | CanonicalForm |
---|
| 440 | mapdomain ( const CanonicalForm & f, CanonicalForm (*mf)( const CanonicalForm & ) ) |
---|
| 441 | { |
---|
[24c6177] | 442 | if ( f.inBaseDomain() ) |
---|
[1758fb] | 443 | return mf( f ); |
---|
| 444 | else |
---|
| 445 | { |
---|
| 446 | CanonicalForm result = 0; |
---|
| 447 | CFIterator i; |
---|
| 448 | Variable x = f.mvar(); |
---|
| 449 | for ( i = f; i.hasTerms(); i++ ) |
---|
| 450 | result += power( x, i.exp() ) * mapdomain( i.coeff(), mf ); |
---|
| 451 | return result; |
---|
[8519cfb] | 452 | } |
---|
| 453 | } |
---|
[b52d27] | 454 | |
---|
| 455 | /** static void degreesRec ( const CanonicalForm & f, int * degs ) |
---|
| 456 | * |
---|
| 457 | * degreesRec() - recursively get degrees of f. |
---|
| 458 | * |
---|
| 459 | * Used by degrees(). |
---|
| 460 | * |
---|
| 461 | **/ |
---|
[13dc2e] | 462 | static void |
---|
[9bab9f] | 463 | degreesRec ( const CanonicalForm & f, int * degs ) |
---|
| 464 | { |
---|
[1758fb] | 465 | if ( ! f.inCoeffDomain() ) |
---|
| 466 | { |
---|
| 467 | int level = f.level(); |
---|
| 468 | int deg = f.degree(); |
---|
| 469 | // calculate the maximum degree of all coefficients which |
---|
| 470 | // are in the same level |
---|
| 471 | if ( degs[level] < deg ) |
---|
| 472 | degs[level] = f.degree(); |
---|
| 473 | for ( CFIterator i = f; i.hasTerms(); i++ ) |
---|
| 474 | degreesRec( i.coeff(), degs ); |
---|
[9bab9f] | 475 | } |
---|
[b973c0] | 476 | } |
---|
[b52d27] | 477 | |
---|
| 478 | /** int * degrees ( const CanonicalForm & f, int * degs ) |
---|
| 479 | * |
---|
| 480 | * degress() - return the degrees of all polynomial variables in f. |
---|
| 481 | * |
---|
| 482 | * Returns 0 if f is in a coefficient domain, the degrees of f in |
---|
| 483 | * all its polynomial variables in an array of int otherwise: |
---|
| 484 | * |
---|
| 485 | * degrees( f, 0 )[i] = degree( f, Variable(i) ) |
---|
| 486 | * |
---|
| 487 | * If degs is not the zero pointer the degrees are stored in this |
---|
| 488 | * array. In this case degs should be larger than the level of |
---|
| 489 | * f. If degs is the zero pointer, an array of sufficient size |
---|
| 490 | * is allocated automatically. |
---|
| 491 | * |
---|
| 492 | **/ |
---|
[2955ba] | 493 | int * degrees ( const CanonicalForm & f, int * degs ) |
---|
[9bab9f] | 494 | { |
---|
| 495 | if ( f.inCoeffDomain() ) |
---|
[8b3624e] | 496 | { |
---|
| 497 | if (degs != 0) |
---|
| 498 | return degs; |
---|
| 499 | else |
---|
| 500 | return 0; |
---|
| 501 | } |
---|
[1758fb] | 502 | else |
---|
| 503 | { |
---|
| 504 | int level = f.level(); |
---|
| 505 | if ( degs == 0 ) |
---|
| 506 | degs = new int[level+1]; |
---|
| 507 | for ( int i = 0; i <= level; i++ ) |
---|
| 508 | degs[i] = 0; |
---|
| 509 | degreesRec( f, degs ); |
---|
| 510 | return degs; |
---|
[9bab9f] | 511 | } |
---|
| 512 | } |
---|
[b52d27] | 513 | |
---|
| 514 | /** int totaldegree ( const CanonicalForm & f ) |
---|
| 515 | * |
---|
| 516 | * totaldegree() - return the total degree of f. |
---|
| 517 | * |
---|
| 518 | * If f is zero, return -1. If f is in a coefficient domain, |
---|
| 519 | * return 0. Otherwise return the total degree of f in all |
---|
| 520 | * polynomial variables. |
---|
| 521 | * |
---|
| 522 | **/ |
---|
[2955ba] | 523 | int totaldegree ( const CanonicalForm & f ) |
---|
[9bab9f] | 524 | { |
---|
| 525 | if ( f.isZero() ) |
---|
[1758fb] | 526 | return -1; |
---|
[9bab9f] | 527 | else if ( f.inCoeffDomain() ) |
---|
[1758fb] | 528 | return 0; |
---|
[110718] | 529 | else |
---|
| 530 | { |
---|
[1758fb] | 531 | CFIterator i; |
---|
| 532 | int cdeg = 0, dummy; |
---|
| 533 | // calculate maximum over all coefficients of f, taking |
---|
| 534 | // in account our own exponent |
---|
| 535 | for ( i = f; i.hasTerms(); i++ ) |
---|
| 536 | if ( (dummy = totaldegree( i.coeff() ) + i.exp()) > cdeg ) |
---|
| 537 | cdeg = dummy; |
---|
| 538 | return cdeg; |
---|
[9bab9f] | 539 | } |
---|
| 540 | } |
---|
[b52d27] | 541 | |
---|
| 542 | /** int totaldegree ( const CanonicalForm & f, const Variable & v1, const Variable & v2 ) |
---|
| 543 | * |
---|
| 544 | * totaldegree() - return the total degree of f as a polynomial |
---|
| 545 | * in the polynomial variables between v1 and v2 (inclusively). |
---|
| 546 | * |
---|
| 547 | * If f is zero, return -1. If f is in a coefficient domain, |
---|
| 548 | * return 0. Also, return 0 if v1 > v2. Otherwise, take f to be |
---|
| 549 | * a polynomial in the polynomial variables between v1 and v2 and |
---|
| 550 | * return its total degree. |
---|
| 551 | * |
---|
| 552 | **/ |
---|
[9bab9f] | 553 | int |
---|
| 554 | totaldegree ( const CanonicalForm & f, const Variable & v1, const Variable & v2 ) |
---|
| 555 | { |
---|
| 556 | if ( f.isZero() ) |
---|
[1758fb] | 557 | return -1; |
---|
[9bab9f] | 558 | else if ( v1 > v2 ) |
---|
[1758fb] | 559 | return 0; |
---|
[9bab9f] | 560 | else if ( f.inCoeffDomain() ) |
---|
[1758fb] | 561 | return 0; |
---|
[9bab9f] | 562 | else if ( f.mvar() < v1 ) |
---|
[1758fb] | 563 | return 0; |
---|
[9bab9f] | 564 | else if ( f.mvar() == v1 ) |
---|
[1758fb] | 565 | return f.degree(); |
---|
| 566 | else if ( f.mvar() > v2 ) |
---|
| 567 | { |
---|
| 568 | // v2's level is larger than f's level, descend down |
---|
| 569 | CFIterator i; |
---|
| 570 | int cdeg = 0, dummy; |
---|
| 571 | // calculate maximum over all coefficients of f |
---|
| 572 | for ( i = f; i.hasTerms(); i++ ) |
---|
| 573 | if ( (dummy = totaldegree( i.coeff(), v1, v2 )) > cdeg ) |
---|
| 574 | cdeg = dummy; |
---|
| 575 | return cdeg; |
---|
[9bab9f] | 576 | } |
---|
[1758fb] | 577 | else |
---|
| 578 | { |
---|
| 579 | // v1 < f.mvar() <= v2 |
---|
| 580 | CFIterator i; |
---|
| 581 | int cdeg = 0, dummy; |
---|
| 582 | // calculate maximum over all coefficients of f, taking |
---|
| 583 | // in account our own exponent |
---|
| 584 | for ( i = f; i.hasTerms(); i++ ) |
---|
| 585 | if ( (dummy = totaldegree( i.coeff(), v1, v2 ) + i.exp()) > cdeg ) |
---|
| 586 | cdeg = dummy; |
---|
| 587 | return cdeg; |
---|
[9bab9f] | 588 | } |
---|
| 589 | } |
---|
[b52d27] | 590 | |
---|
| 591 | /** int size ( const CanonicalForm & f, const Variable & v ) |
---|
| 592 | * |
---|
| 593 | * size() - count number of monomials of f with level higher |
---|
| 594 | * or equal than level of v. |
---|
| 595 | * |
---|
| 596 | * Returns one if f is in an base domain. |
---|
| 597 | * |
---|
| 598 | **/ |
---|
[424e5c] | 599 | int |
---|
| 600 | size ( const CanonicalForm & f, const Variable & v ) |
---|
| 601 | { |
---|
| 602 | if ( f.inBaseDomain() ) |
---|
[1758fb] | 603 | return 1; |
---|
[424e5c] | 604 | |
---|
| 605 | if ( f.mvar() < v ) |
---|
[1758fb] | 606 | // polynomials with level < v1 are counted as coefficients |
---|
| 607 | return 1; |
---|
| 608 | else |
---|
| 609 | { |
---|
| 610 | CFIterator i; |
---|
| 611 | int result = 0; |
---|
| 612 | // polynomials with level > v2 are not counted al all |
---|
| 613 | for ( i = f; i.hasTerms(); i++ ) |
---|
| 614 | result += size( i.coeff(), v ); |
---|
| 615 | return result; |
---|
[424e5c] | 616 | } |
---|
| 617 | } |
---|
[b52d27] | 618 | |
---|
| 619 | /** int size ( const CanonicalForm & f ) |
---|
| 620 | * |
---|
| 621 | * size() - return number of monomials in f which are in an |
---|
| 622 | * coefficient domain. |
---|
| 623 | * |
---|
| 624 | * Returns one if f is in an coefficient domain. |
---|
| 625 | * |
---|
| 626 | **/ |
---|
[424e5c] | 627 | int |
---|
| 628 | size ( const CanonicalForm & f ) |
---|
| 629 | { |
---|
| 630 | if ( f.inCoeffDomain() ) |
---|
[1758fb] | 631 | return 1; |
---|
| 632 | else |
---|
| 633 | { |
---|
| 634 | int result = 0; |
---|
| 635 | CFIterator i; |
---|
| 636 | for ( i = f; i.hasTerms(); i++ ) |
---|
| 637 | result += size( i.coeff() ); |
---|
| 638 | return result; |
---|
[424e5c] | 639 | } |
---|
| 640 | } |
---|
[276c3f] | 641 | |
---|
[b52d27] | 642 | /** polynomials in M.mvar() are considered coefficients |
---|
| 643 | * M univariate monic polynomial |
---|
| 644 | * the coefficients of f are reduced modulo M |
---|
| 645 | **/ |
---|
[276c3f] | 646 | CanonicalForm reduce(const CanonicalForm & f, const CanonicalForm & M) |
---|
[b52d27] | 647 | { |
---|
[276c3f] | 648 | if(f.inBaseDomain() || f.level() < M.level()) |
---|
| 649 | return f; |
---|
| 650 | if(f.level() == M.level()) |
---|
| 651 | { |
---|
| 652 | if(f.degree() < M.degree()) |
---|
| 653 | return f; |
---|
| 654 | CanonicalForm tmp = mod (f, M); |
---|
| 655 | return tmp; |
---|
| 656 | } |
---|
| 657 | // here: f.level() > M.level() |
---|
| 658 | CanonicalForm result = 0; |
---|
| 659 | for(CFIterator i=f; i.hasTerms(); i++) |
---|
| 660 | result += reduce(i.coeff(),M) * power(f.mvar(),i.exp()); |
---|
| 661 | return result; |
---|
| 662 | } |
---|
[baa2c3a] | 663 | |
---|
| 664 | /** check if poly f contains an algebraic variable a **/ |
---|
| 665 | bool hasFirstAlgVar( const CanonicalForm & f, Variable & a ) |
---|
| 666 | { |
---|
| 667 | if( f.inBaseDomain() ) // f has NO alg. variable |
---|
| 668 | return false; |
---|
| 669 | if( f.level()<0 ) // f has only alg. vars, so take the first one |
---|
| 670 | { |
---|
| 671 | a = f.mvar(); |
---|
| 672 | return true; |
---|
| 673 | } |
---|
| 674 | for(CFIterator i=f; i.hasTerms(); i++) |
---|
| 675 | if( hasFirstAlgVar( i.coeff(), a )) |
---|
| 676 | return true; // 'a' is already set |
---|
| 677 | return false; |
---|
| 678 | } |
---|
[1a4c80b] | 679 | |
---|
| 680 | /** left shift the main variable of F by n |
---|
| 681 | * @return if x is the main variable of F the result is F(x^n) |
---|
| 682 | **/ |
---|
| 683 | CanonicalForm leftShift (const CanonicalForm& F, int n) |
---|
| 684 | { |
---|
| 685 | ASSERT (n >= 0, "cannot left shift by negative number"); |
---|
| 686 | if (F.inBaseDomain()) |
---|
| 687 | return F; |
---|
| 688 | if (n == 0) |
---|
| 689 | return F; |
---|
| 690 | Variable x=F.mvar(); |
---|
| 691 | CanonicalForm result= 0; |
---|
| 692 | for (CFIterator i= F; i.hasTerms(); i++) |
---|
| 693 | result += i.coeff()*power (x, i.exp()*n); |
---|
| 694 | return result; |
---|
| 695 | } |
---|