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