Changeset dd3e561 in git for factory/cf_gcd.cc
- Timestamp:
- Oct 24, 1997, 6:46:20 PM (27 years ago)
- Branches:
- (u'spielwiese', 'fe61d9c35bf7c61f2b6cbf1b56e25e2f08d536cc')
- Children:
- f2c84a8a2d37c2bf2c56dd13ca31097ff890da91
- Parents:
- c82e7910ebf6204b5d83978ce3ff54bf94d8b019
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
factory/cf_gcd.cc
rc82e791 rdd3e561 1 1 /* emacs edit mode for this file is -*- C++ -*- */ 2 /* $Id: cf_gcd.cc,v 1.1 4 1997-09-12 15:27:25schmidt Exp $ */2 /* $Id: cf_gcd.cc,v 1.15 1997-10-24 16:46:20 schmidt Exp $ */ 3 3 4 4 #include <config.h> … … 61 61 } 62 62 63 //{{{ static CanonicalForm maxnorm ( const CanonicalForm & f ) 64 //{{{ docu 65 // 66 // maxnorm() - return the maximum of the absolute values of all 67 // coefficients of f. 68 // 69 // The absolute value and the maximum are calculated with respect 70 // to operator < on canonical forms which is most meaningful for 71 // rational numbers and integers. 72 // 73 // Used by gcd_poly_univar0(). 74 // 75 //}}} 63 76 static CanonicalForm 64 77 maxnorm ( const CanonicalForm & f ) 65 78 { 66 CanonicalForm m = 0 , h;79 CanonicalForm m = 0; 67 80 CFIterator i; 68 81 for ( i = f; i.hasTerms(); i++ ) … … 70 83 return m; 71 84 } 72 73 static void 74 chinesePoly ( const CanonicalForm & f1, const CanonicalForm & q1, const CanonicalForm & f2, const CanonicalForm & q2, CanonicalForm & f, CanonicalForm & q ) 75 { 76 CFIterator i1 = f1, i2 = f2; 77 CanonicalForm c; 78 Variable x = f1.mvar(); 79 f = 0; 80 while ( i1.hasTerms() && i2.hasTerms() ) { 81 if ( i1.exp() == i2.exp() ) { 82 chineseRemainder( i1.coeff(), q1, i2.coeff(), q2, c, q ); 83 f += power( x, i1.exp() ) * c; 84 i1++; i2++; 85 } 86 else if ( i1.exp() > i2.exp() ) { 87 chineseRemainder( 0, q1, i2.coeff(), q2, c, q ); 88 f += power( x, i2.exp() ) * c; 89 i2++; 90 } 91 else { 92 chineseRemainder( i1.coeff(), q1, 0, q2, c, q ); 93 f += power( x, i1.exp() ) * c; 94 i1++; 95 } 96 } 97 while ( i1.hasTerms() ) { 98 chineseRemainder( i1.coeff(), q1, 0, q2, c, q ); 99 f += power( x, i1.exp() ) * c; 100 i1++; 101 } 102 while ( i2.hasTerms() ) { 103 chineseRemainder( 0, q1, i2.coeff(), q2, c, q ); 104 f += power( x, i2.exp() ) * c; 105 i2++; 106 } 107 } 108 85 //}}} 86 87 //{{{ static CanonicalForm balance ( const CanonicalForm & f, const CanonicalForm & q ) 88 //{{{ docu 89 // 90 // balance() - map f from positive to symmetric representation 91 // mod q. 92 // 93 // This makes sense for univariate polynomials over Z only. 94 // q should be an integer. 95 // 96 // Used by gcd_poly_univar0(). 97 // 98 //}}} 109 99 static CanonicalForm 110 100 balance ( const CanonicalForm & f, const CanonicalForm & q ) 111 101 { 112 CFIterator i;102 Variable x = f.mvar(); 113 103 CanonicalForm result = 0, qh = q / 2; 114 104 CanonicalForm c; 115 Variable x = f.mvar();105 CFIterator i; 116 106 for ( i = f; i.hasTerms(); i++ ) { 117 c = i.coeff() % q;107 c = mod( i.coeff(), q ); 118 108 if ( c > qh ) 119 109 result += power( x, i.exp() ) * (c - q); … … 123 113 return result; 124 114 } 125 115 //}}} 116 117 //{{{ CanonicalForm igcd ( const CanonicalForm & f, const CanonicalForm & g ) 118 //{{{ docu 119 // 120 // igcd() - return integer gcd of f and g. 121 // 122 // Calculates the integer gcd of f and g using the euclidean 123 // algorithm if f and g are integers and we are not calculating 124 // in Q. Returns one in all other cases. Normalizes result. 125 // 126 //}}} 126 127 CanonicalForm 127 128 igcd ( const CanonicalForm & f, const CanonicalForm & g ) … … 142 143 return 1; 143 144 } 144 145 //}}} 146 147 //{{{ static CanonicalForm icontent ( const CanonicalForm & f, const CanonicalForm & c ) 148 //{{{ docu 149 // 150 // icontent() - return gcd of c and all coefficients of f which 151 // are in a coefficient domain. 152 // 153 // Used by icontent(). 154 // 155 //}}} 145 156 static CanonicalForm 146 157 icontent ( const CanonicalForm & f, const CanonicalForm & c ) … … 155 166 } 156 167 } 157 168 //}}} 169 170 //{{{ CanonicalForm icontent ( const CanonicalForm & f ) 171 //{{{ docu 172 // 173 // icontent() - return gcd over all coefficients of f which are 174 // in a coefficient domain. 175 // 176 //}}} 158 177 CanonicalForm 159 178 icontent ( const CanonicalForm & f ) … … 161 180 return icontent( f, 0 ); 162 181 } 163 182 //}}} 183 184 //{{{ CanonicalForm iextgcd ( const CanonicalForm & f, const CanonicalForm & g, CanonicalForm & a, CanonicalForm & b ) 185 //{{{ docu 186 // 187 // iextgcd() - calculate extended integer gcd. 188 // 189 // Returns gcd(f, g) and a and b sucht that f*a+g*b=gcd(f, g). 190 // The gcd is calculated using an extended euclidean polynomial 191 // remainder sequence. Normalizes result. 192 // 193 // Note: be sure you are calculating in Z, and not in Q! 194 // 195 //}}} 164 196 CanonicalForm 165 197 iextgcd ( const CanonicalForm & f, const CanonicalForm & g, CanonicalForm & a, CanonicalForm & b ) … … 185 217 return p0; 186 218 } 187 219 //}}} 220 221 //{{{ CanonicalForm extgcd ( const CanonicalForm & f, const CanonicalForm & g, CanonicalForm & a, CanonicalForm & b ) 222 //{{{ docu 223 // 224 // extgcd() - returns polynomial extended gcd of f and g. 225 // 226 // Returns gcd(f, g) and a and b sucht that f*a+g*b=gcd(f, g). 227 // The gcd is calculated using an extended euclidean polynomial 228 // remainder sequence, so f and g should be polynomials over an 229 // euclidean domain. Normalizes result. 230 // 231 // Note: be sure that f and g have the same level! 232 // 233 //}}} 188 234 CanonicalForm 189 235 extgcd ( const CanonicalForm & f, const CanonicalForm & g, CanonicalForm & a, CanonicalForm & b ) … … 214 260 return p0; 215 261 } 262 //}}} 216 263 217 264 static CanonicalForm … … 263 310 else { 264 311 if ( Dp.degree() == D.degree() ) { 265 chinese Poly( D, q, mapinto( Dp ), p, newD, newq );312 chineseRemainder( D, q, mapinto( Dp ), p, newD, newq ); 266 313 q = newq; 267 314 D = newD; … … 291 338 } 292 339 293 294 340 static CanonicalForm 295 341 gcd_poly1( const CanonicalForm & f, const CanonicalForm & g, bool modularflag ) … … 341 387 } 342 388 343 389 //{{{ static CanonicalForm gcd_poly ( const CanonicalForm & f, const CanonicalForm & g, bool modularflag ) 390 //{{{ docu 391 // 392 // gcd_poly() - calculate polynomial gcd. 393 // 394 // This is the dispatcher for polynomial gcd calculation. We call either 395 // ezgcd(), sparsemod() or gcd_poly1() in dependecy on the current 396 // characteristic and settings of SW_USE_EZGCD and SW_USE_SPARSEMOD, resp. 397 // 398 // modularflag is reached down to gcd_poly1() without change in case of 399 // zero characteristic. 400 // 401 // Used by gcd() and gcd_poly_univar0(). 402 // 403 //}}} 344 404 static CanonicalForm 345 gcd_poly ( const CanonicalForm & f, const CanonicalForm & g, bool modularflag )405 gcd_poly ( const CanonicalForm & f, const CanonicalForm & g, bool modularflag ) 346 406 { 347 407 if ( getCharacteristic() != 0 ) { … … 353 413 return N( ezgcd( M(f), M(g) ) ); 354 414 } 355 else if ( isOn( SW_USE_SPARSEMOD ) ) {415 else if ( isOn( SW_USE_SPARSEMOD ) && ! ( f.isUnivariate() && g.isUnivariate() ) ) { 356 416 return sparsemod( f, g ); 357 417 } … … 360 420 } 361 421 } 362 363 422 //}}} 423 424 //{{{ static CanonicalForm cf_content ( const CanonicalForm & f, const CanonicalForm & g ) 425 //{{{ docu 426 // 427 // cf_content() - return gcd(g, content(f)). 428 // 429 // content(f) is calculated with respect to f's main variable. 430 // 431 // Used by gcd(), content(), content( CF, Variable ). 432 // 433 //}}} 364 434 static CanonicalForm 365 435 cf_content ( const CanonicalForm & f, const CanonicalForm & g ) … … 380 450 return f; 381 451 } 452 //}}} 382 453 383 454 //{{{ CanonicalForm content ( const CanonicalForm & f ) … … 386 457 // content() - return content(f) with respect to main variable. 387 458 // 459 // Normalizes result. 460 // 388 461 //}}} 389 462 CanonicalForm … … 394 467 //}}} 395 468 469 //{{{ CanonicalForm content ( const CanonicalForm & f, const Variable & x ) 470 //{{{ docu 471 // 472 // content() - return content(f) with respect to x. 473 // 474 // x should be a polynomial variable. 475 // 476 //}}} 396 477 CanonicalForm 397 478 content ( const CanonicalForm & f, const Variable & x ) 398 479 { 399 if ( f.mvar() == x ) 480 ASSERT( x.level() > 0, "cannot calculate content with respect to algebraic variable" ); 481 Variable y = f.mvar(); 482 483 if ( y == x ) 400 484 return cf_content( f, 0 ); 401 else if ( f.mvar()< x )485 else if ( y < x ) 402 486 return f; 403 487 else 404 return swapvar( content( swapvar( f, f.mvar(), x ), f.mvar() ), f.mvar(), x ); 405 } 406 488 return swapvar( content( swapvar( f, y, x ), y ), y, x ); 489 } 490 //}}} 491 492 //{{{ CanonicalForm vcontent ( const CanonicalForm & f, const Variable & x ) 493 //{{{ docu 494 // 495 // vcontent() - return content of f with repect to variables >= x. 496 // 497 // The content is recursively calculated over all coefficients in 498 // f having level less than x. x should be a polynomial 499 // variable. 500 // 501 //}}} 407 502 CanonicalForm 408 503 vcontent ( const CanonicalForm & f, const Variable & x ) 409 504 { 505 ASSERT( x.level() > 0, "cannot calculate vcontent with respect to algebraic variable" ); 506 410 507 if ( f.mvar() <= x ) 411 508 return content( f, x ); … … 418 515 } 419 516 } 517 //}}} 420 518 421 519 //{{{ CanonicalForm pp ( const CanonicalForm & f ) … … 423 521 // 424 522 // pp() - return primitive part of f. 523 // 524 // Returns zero if f equals zero, otherwise f / content(f). 425 525 // 426 526 //}}} … … 470 570 return g; 471 571 if ( getCharacteristic() == 0 && isOn( SW_RATIONAL ) ) { 572 CanonicalForm cdF = common_den( f ); 573 CanonicalForm cdG = common_den( g ); 472 574 Off( SW_RATIONAL ); 473 CanonicalForm l = lcm( c ommon_den( f ), common_den( g ));575 CanonicalForm l = lcm( cdF, cdG ); 474 576 On( SW_RATIONAL ); 475 577 CanonicalForm F = f * l, G = g * l; … … 496 598 } 497 599 600 //{{{ CanonicalForm lcm ( const CanonicalForm & f, const CanonicalForm & g ) 601 //{{{ docu 602 // 603 // lcm() - return least common multiple of f and g. 604 // 605 // The lcm is calculated using the formula lcm(f, g) = f * g / gcd(f, g). 606 // 607 // Returns zero if one of f or g equals zero. 608 // 609 //}}} 498 610 CanonicalForm 499 611 lcm ( const CanonicalForm & f, const CanonicalForm & g ) 500 612 { 501 return ( f / gcd( f, g ) ) * g; 502 } 613 if ( f.isZero() || g.isZero() ) 614 return f; 615 else 616 return ( f / gcd( f, g ) ) * g; 617 } 618 //}}}
Note: See TracChangeset
for help on using the changeset viewer.