[493c477] | 1 | /* emacs edit mode for this file is -*- C++ -*- */ |
---|
[2dd068] | 2 | |
---|
[b52d27] | 3 | /** |
---|
| 4 | * |
---|
| 5 | * @file cf_map.cc |
---|
[abddbe] | 6 | * |
---|
[b52d27] | 7 | * definition of class CFMap. |
---|
| 8 | * |
---|
| 9 | * Used by: cf_gcd.cc, fac_multivar.cc |
---|
| 10 | * |
---|
| 11 | **/ |
---|
[b973c0] | 12 | |
---|
[9f7665] | 13 | |
---|
[e4fe2b] | 14 | #include "config.h" |
---|
[9f7665] | 15 | |
---|
[997ae52] | 16 | |
---|
[1f28ed] | 17 | #include "canonicalform.h" |
---|
[2dd068] | 18 | #include "cf_map.h" |
---|
| 19 | #include "cf_iter.h" |
---|
[6db552] | 20 | #include "templates/ftmpl_functions.h" |
---|
[2dd068] | 21 | |
---|
[b52d27] | 22 | /** MapPair & MapPair::operator = ( const MapPair & p ) |
---|
| 23 | * |
---|
| 24 | * MapPair::operator = - assignment operator. |
---|
| 25 | * |
---|
| 26 | **/ |
---|
[62b35b6] | 27 | MapPair & |
---|
| 28 | MapPair::operator = ( const MapPair & p ) |
---|
| 29 | { |
---|
| 30 | if ( this != &p ) { |
---|
[2955ba] | 31 | V = p.V; |
---|
| 32 | S = p.S; |
---|
[62b35b6] | 33 | } |
---|
| 34 | return *this; |
---|
| 35 | } |
---|
| 36 | |
---|
| 37 | #ifndef NOSTREAMIO |
---|
[b52d27] | 38 | /** OSTREAM & operator << ( OSTREAM & s, const MapPair & p ) |
---|
| 39 | * |
---|
| 40 | * operator << - print a map pair ("V -> S"). |
---|
| 41 | * |
---|
| 42 | **/ |
---|
[181148] | 43 | OSTREAM & |
---|
| 44 | operator << ( OSTREAM & s, const MapPair & p ) |
---|
[62b35b6] | 45 | { |
---|
| 46 | s << p.var() << " -> " << p.subst(); |
---|
| 47 | return s; |
---|
| 48 | } |
---|
[6400f5] | 49 | |
---|
[181148] | 50 | void MapPair::print( OSTREAM&) const |
---|
[6400f5] | 51 | { |
---|
| 52 | } |
---|
[62b35b6] | 53 | #endif /* NOSTREAMIO */ |
---|
| 54 | |
---|
[b52d27] | 55 | /** CFMap::CFMap ( const CFList & L ) |
---|
| 56 | * |
---|
| 57 | * CFMap::CFMap() - construct a CFMap from a CFList. |
---|
| 58 | * |
---|
| 59 | * Variable[i] will be mapped to CFList[i] under the resulting |
---|
| 60 | * map. |
---|
| 61 | * |
---|
| 62 | **/ |
---|
[62b35b6] | 63 | CFMap::CFMap ( const CFList & L ) |
---|
| 64 | { |
---|
| 65 | CFListIterator i; |
---|
| 66 | int j; |
---|
| 67 | for ( i = L, j = 1; i.hasItem(); i++, j++ ) |
---|
[2955ba] | 68 | P.insert( MapPair( Variable(j), i.getItem() ) ); |
---|
[62b35b6] | 69 | } |
---|
[b52d27] | 70 | |
---|
| 71 | /** CFMap & CFMap::operator = ( const CFMap & m ) |
---|
| 72 | * |
---|
| 73 | * CFMap::operator = - assignment operator. |
---|
| 74 | * |
---|
| 75 | **/ |
---|
[62b35b6] | 76 | CFMap & |
---|
| 77 | CFMap::operator = ( const CFMap & m ) |
---|
| 78 | { |
---|
| 79 | if ( this != &m ) |
---|
[2955ba] | 80 | P = m.P; |
---|
[62b35b6] | 81 | return *this; |
---|
| 82 | } |
---|
[b52d27] | 83 | |
---|
| 84 | /** static int cmpfunc ( const MapPair & p1, const MapPair & p2 ) |
---|
| 85 | * |
---|
| 86 | * cmpfunc() - compare two map pairs. |
---|
| 87 | * |
---|
| 88 | * Return -1 if p2's variable is less than p1's, 0 if they are |
---|
| 89 | * equal, 1 if p2's level is greater than p1's. |
---|
| 90 | * |
---|
| 91 | **/ |
---|
[1f28ed] | 92 | static int |
---|
| 93 | cmpfunc ( const MapPair & p1, const MapPair & p2 ) |
---|
| 94 | { |
---|
| 95 | if ( p1.var() > p2.var() ) return -1; |
---|
| 96 | else if ( p1.var() == p2.var() ) return 0; |
---|
| 97 | else return 1; |
---|
| 98 | } |
---|
[b52d27] | 99 | |
---|
| 100 | /** static void insfunc ( MapPair & orgp, const MapPair & newp ) |
---|
| 101 | * |
---|
| 102 | * insfunc() - assign newp to orgp. |
---|
| 103 | * |
---|
| 104 | * cmpfunc() and insfunc() are used as functions for inserting a |
---|
| 105 | * map pair into a map by CFMap::newpair(). |
---|
| 106 | * |
---|
| 107 | **/ |
---|
[1f28ed] | 108 | static void |
---|
| 109 | insfunc ( MapPair & orgp, const MapPair & newp ) |
---|
| 110 | { |
---|
| 111 | orgp = newp; |
---|
| 112 | } |
---|
[b52d27] | 113 | |
---|
| 114 | /** void CFMap::newpair ( const Variable & v, const CanonicalForm & s ) |
---|
| 115 | * |
---|
| 116 | * CFMap::newpair() - insert a MapPair into a CFMap. |
---|
| 117 | * |
---|
| 118 | **/ |
---|
[62b35b6] | 119 | void |
---|
| 120 | CFMap::newpair ( const Variable & v, const CanonicalForm & s ) |
---|
| 121 | { |
---|
| 122 | P.insert( MapPair( v, s ), cmpfunc, insfunc ); |
---|
| 123 | } |
---|
[b52d27] | 124 | |
---|
| 125 | /** static CanonicalForm subsrec ( const CanonicalForm & f, const MPListIterator & i ) |
---|
| 126 | * |
---|
| 127 | * subsrec() - recursively apply the substitutions in i to f. |
---|
| 128 | * |
---|
| 129 | * Substitutes algebraic variables, too. The substituted |
---|
| 130 | * expression are not subject to further substitutions. |
---|
| 131 | * |
---|
| 132 | * Used by: CFMap::operator ()(). |
---|
| 133 | * |
---|
| 134 | **/ |
---|
[1f28ed] | 135 | static CanonicalForm |
---|
[62b35b6] | 136 | subsrec ( const CanonicalForm & f, const MPListIterator & i ) |
---|
[1f28ed] | 137 | { |
---|
| 138 | if ( f.inBaseDomain() ) return f; |
---|
| 139 | MPListIterator j = i; |
---|
| 140 | |
---|
| 141 | // skip MapPairs larger than the main variable of f |
---|
| 142 | while ( j.hasItem() && j.getItem().var() > f.mvar() ) j++; |
---|
[2dd068] | 143 | |
---|
[1f28ed] | 144 | if ( j.hasItem() ) |
---|
[2955ba] | 145 | if ( j.getItem().var() != f.mvar() ) { |
---|
| 146 | // simply descend if the current MapPair variable is |
---|
| 147 | // not the main variable of f |
---|
| 148 | CanonicalForm result = 0; |
---|
| 149 | CFIterator I; |
---|
| 150 | for ( I = f; I.hasTerms(); I++ ) |
---|
| 151 | result += power( f.mvar(), I.exp() ) * subsrec( I.coeff(), j ); |
---|
| 152 | return result; |
---|
| 153 | } |
---|
| 154 | else { |
---|
| 155 | // replace the main variable of f with the image of |
---|
| 156 | // the current variable under MapPair |
---|
| 157 | CanonicalForm result = 0; |
---|
| 158 | CanonicalForm s = j.getItem().subst(); |
---|
| 159 | CFIterator I; |
---|
| 160 | // move on to the next MapPair |
---|
| 161 | j++; |
---|
| 162 | for ( I = f; I.hasTerms(); I++ ) |
---|
| 163 | result += subsrec( I.coeff(), j ) * power( s, I.exp() ); |
---|
| 164 | return result; |
---|
| 165 | } |
---|
[1f28ed] | 166 | else |
---|
[2955ba] | 167 | return f; |
---|
[1f28ed] | 168 | } |
---|
[b52d27] | 169 | |
---|
| 170 | /** CanonicalForm CFMap::operator () ( const CanonicalForm & f ) const |
---|
| 171 | * |
---|
| 172 | * CFMap::operator () - apply CO to f. |
---|
| 173 | * |
---|
| 174 | * See subsrec() for more detailed information. |
---|
| 175 | * |
---|
| 176 | **/ |
---|
[2dd068] | 177 | CanonicalForm |
---|
[1f28ed] | 178 | CFMap::operator () ( const CanonicalForm & f ) const |
---|
[2dd068] | 179 | { |
---|
[1f28ed] | 180 | MPListIterator i = P; |
---|
[2dd068] | 181 | return subsrec( f, i ); |
---|
| 182 | } |
---|
| 183 | |
---|
[997ae52] | 184 | #ifndef NOSTREAMIO |
---|
[b52d27] | 185 | /** OSTREAM & operator << ( OSTREAM & s, const CFMap & m ) |
---|
| 186 | * |
---|
| 187 | * operator << - print a CFMap ("( V[1] -> S[1], ..., V[n] -> * S[n] )". |
---|
| 188 | * |
---|
| 189 | **/ |
---|
[181148] | 190 | OSTREAM & |
---|
| 191 | operator << ( OSTREAM & s, const CFMap & m ) |
---|
[2dd068] | 192 | { |
---|
[6400f5] | 193 | m.P.print(s); |
---|
| 194 | return s; |
---|
[2dd068] | 195 | } |
---|
[997ae52] | 196 | #endif /* NOSTREAMIO */ |
---|
[2dd068] | 197 | |
---|
[b52d27] | 198 | /** CanonicalForm compress ( const CanonicalForm & f, CFMap & m ) |
---|
| 199 | * |
---|
| 200 | * compress() - compress the canonical form f. |
---|
| 201 | * |
---|
| 202 | * Compress the polynomial f such that the levels of its |
---|
| 203 | * polynomial variables are ordered without any gaps starting |
---|
| 204 | * from level 1. Return the compressed polynomial and a map m to |
---|
| 205 | * undo the compression. That is, if f' = compress(f, m), than f |
---|
| 206 | * = m(f'). |
---|
| 207 | * |
---|
| 208 | **/ |
---|
[2dd068] | 209 | CanonicalForm |
---|
| 210 | compress ( const CanonicalForm & f, CFMap & m ) |
---|
| 211 | { |
---|
| 212 | CanonicalForm result = f; |
---|
| 213 | int i, n; |
---|
| 214 | int * degs = degrees( f ); |
---|
| 215 | |
---|
| 216 | m = CFMap(); |
---|
| 217 | n = i = 1; |
---|
| 218 | while ( i <= level( f ) ) { |
---|
[2955ba] | 219 | while( degs[i] == 0 ) i++; |
---|
| 220 | if ( i != n ) { |
---|
| 221 | // swap variables and remember the swap in the map |
---|
| 222 | m.newpair( Variable( n ), Variable( i ) ); |
---|
| 223 | result = swapvar( result, Variable( i ), Variable( n ) ); |
---|
| 224 | } |
---|
| 225 | n++; i++; |
---|
[2dd068] | 226 | } |
---|
| 227 | delete [] degs; |
---|
| 228 | return result; |
---|
| 229 | } |
---|
[b52d27] | 230 | |
---|
| 231 | /** void compress ( const CFArray & a, CFMap & M, CFMap & N ) |
---|
| 232 | * |
---|
| 233 | * compress() - compress the variables occuring in an a. |
---|
| 234 | * |
---|
| 235 | * Compress the polynomial variables occuring in a so that their |
---|
| 236 | * levels are ordered without any gaps starting from level 1. |
---|
| 237 | * Return the CFMap M to realize the compression and its inverse, |
---|
| 238 | * the CFMap N. Note that if you compress a member of a using M |
---|
| 239 | * the result of the compression is not necessarily compressed, |
---|
| 240 | * since the map is constructed using all variables occuring in |
---|
| 241 | * a. |
---|
| 242 | * |
---|
| 243 | **/ |
---|
[2dd068] | 244 | void |
---|
| 245 | compress ( const CFArray & a, CFMap & M, CFMap & N ) |
---|
| 246 | { |
---|
| 247 | M = N = CFMap(); |
---|
| 248 | if ( a.size() == 0 ) |
---|
[2955ba] | 249 | return; |
---|
[2dd068] | 250 | int maxlevel = level( a[a.min()] ); |
---|
| 251 | int i, j; |
---|
[1f28ed] | 252 | |
---|
| 253 | // get the maximum of levels in a |
---|
[2dd068] | 254 | for ( i = a.min() + 1; i <= a.max(); i++ ) |
---|
[2955ba] | 255 | if ( level( a[i] ) > maxlevel ) |
---|
| 256 | maxlevel = level( a[i] ); |
---|
[2dd068] | 257 | if ( maxlevel <= 0 ) |
---|
[2955ba] | 258 | return; |
---|
[1f28ed] | 259 | |
---|
[8d25143] | 260 | int * degs = NEW_ARRAY(int,maxlevel+1); |
---|
| 261 | int * tmp = NEW_ARRAY(int,maxlevel+1); |
---|
| 262 | for ( i = maxlevel; i >= 1; i-- ) |
---|
[2955ba] | 263 | degs[i] = 0; |
---|
[1f28ed] | 264 | |
---|
| 265 | // calculate the union of all levels occuring in a |
---|
[8d25143] | 266 | for ( i = a.min(); i <= a.max(); i++ ) |
---|
| 267 | { |
---|
[2955ba] | 268 | tmp = degrees( a[i], tmp ); |
---|
| 269 | for ( j = 1; j <= level( a[i] ); j++ ) |
---|
| 270 | if ( tmp[j] != 0 ) |
---|
| 271 | degs[j] = 1; |
---|
[2dd068] | 272 | } |
---|
[1f28ed] | 273 | |
---|
| 274 | // create the maps |
---|
| 275 | i = 1; j = 1; |
---|
[8d25143] | 276 | while ( i <= maxlevel ) |
---|
| 277 | { |
---|
| 278 | if ( degs[i] != 0 ) |
---|
| 279 | { |
---|
[2955ba] | 280 | M.newpair( Variable(i), Variable(j) ); |
---|
| 281 | N.newpair( Variable(j), Variable(i) ); |
---|
| 282 | j++; |
---|
| 283 | } |
---|
| 284 | i++; |
---|
[2dd068] | 285 | } |
---|
[8d25143] | 286 | DELETE_ARRAY(degs); |
---|
| 287 | DELETE_ARRAY(tmp); |
---|
[2dd068] | 288 | } |
---|
| 289 | |
---|
[ae2e227] | 290 | /* |
---|
| 291 | * compute positions p1 and pe of optimal variables: |
---|
| 292 | * pe is used in "ezgcd" and |
---|
| 293 | * p1 in "gcd_poly1" |
---|
| 294 | */ |
---|
| 295 | static |
---|
| 296 | void optvalues ( const int * df, const int * dg, const int n, int & p1, int &pe ) |
---|
| 297 | { |
---|
| 298 | int i, o1, oe; |
---|
| 299 | i = p1 = pe = 0; |
---|
| 300 | do |
---|
| 301 | { |
---|
| 302 | i++; |
---|
| 303 | if ( i > n ) return; |
---|
| 304 | } while ( ( df[i] == 0 ) || ( dg[i] == 0 ) ); |
---|
| 305 | p1 = pe = i; |
---|
| 306 | if ( df[i] > dg[i] ) |
---|
| 307 | { |
---|
| 308 | o1 = df[i]; oe = dg[i]; |
---|
| 309 | } |
---|
| 310 | else |
---|
| 311 | { |
---|
| 312 | o1 = dg[i]; oe = df[i]; |
---|
| 313 | } |
---|
| 314 | while ( i < n ) |
---|
| 315 | { |
---|
| 316 | i++; |
---|
| 317 | if ( ( df[i] != 0 ) && ( dg[i] != 0 ) ) |
---|
| 318 | { |
---|
| 319 | if ( df[i] > dg[i] ) |
---|
| 320 | { |
---|
| 321 | if ( o1 >= df[i]) { o1 = df[i]; p1 = i; } |
---|
| 322 | if ( oe < dg[i]) { oe = dg[i]; pe = i; } |
---|
| 323 | } |
---|
| 324 | else |
---|
| 325 | { |
---|
| 326 | if ( o1 >= dg[i]) { o1 = dg[i]; p1 = i; } |
---|
| 327 | if ( oe < df[i]) { oe = df[i]; pe = i; } |
---|
| 328 | } |
---|
| 329 | } |
---|
| 330 | } |
---|
| 331 | } |
---|
| 332 | |
---|
| 333 | |
---|
[b52d27] | 334 | /** void compress ( const CanonicalForm & f, const CanonicalForm & g, CFMap & M, CFMap & N ) |
---|
| 335 | * |
---|
| 336 | * compress() - compress the variables occurring in f and g with respect |
---|
| 337 | * to optimal variables |
---|
| 338 | * |
---|
| 339 | * Compress the polynomial variables occurring in f and g so that |
---|
| 340 | * the levels of variables common to f and g are ordered without |
---|
| 341 | * any gaps starting from level 1, whereas the variables occuring |
---|
| 342 | * in only one of f or g are moved to levels higher than the |
---|
| 343 | * levels of the common variables. Return the CFMap M to realize |
---|
| 344 | * the compression and its inverse, the CFMap N. |
---|
| 345 | * N needs only variables common to f and g. |
---|
| 346 | * |
---|
| 347 | **/ |
---|
[1f28ed] | 348 | void |
---|
| 349 | compress ( const CanonicalForm & f, const CanonicalForm & g, CFMap & M, CFMap & N ) |
---|
[cc3e5e] | 350 | { |
---|
| 351 | int n = tmax( f.level(), g.level() ); |
---|
[ae2e227] | 352 | int i, k, p1, pe; |
---|
[8d25143] | 353 | int * degsf = NEW_ARRAY(int,n+1); |
---|
| 354 | int * degsg = NEW_ARRAY(int,n+1); |
---|
[cc3e5e] | 355 | |
---|
[2955ba] | 356 | for ( i = 0; i <= n; i++ ) |
---|
| 357 | { |
---|
| 358 | degsf[i] = degsg[i] = 0; |
---|
[cc3e5e] | 359 | } |
---|
[1f28ed] | 360 | |
---|
[cc3e5e] | 361 | degsf = degrees( f, degsf ); |
---|
| 362 | degsg = degrees( g, degsg ); |
---|
[ae2e227] | 363 | optvalues( degsf, degsg, n, p1, pe ); |
---|
[806c18] | 364 | |
---|
[ae2e227] | 365 | i = 1; k = 1; |
---|
[2955ba] | 366 | if ( pe > 1 ) |
---|
| 367 | { |
---|
| 368 | M.newpair( Variable(pe), Variable(k) ); |
---|
| 369 | N.newpair( Variable(k), Variable(pe) ); |
---|
[ae2e227] | 370 | k++; |
---|
| 371 | } |
---|
[2955ba] | 372 | while ( i <= n ) |
---|
| 373 | { |
---|
| 374 | if ( degsf[i] > 0 && degsg[i] > 0 ) |
---|
| 375 | { |
---|
| 376 | if ( ( i != k ) && ( i != pe ) && ( i != p1 ) ) |
---|
| 377 | { |
---|
| 378 | M.newpair( Variable(i), Variable(k) ); |
---|
| 379 | N.newpair( Variable(k), Variable(i) ); |
---|
| 380 | } |
---|
| 381 | k++; |
---|
| 382 | } |
---|
| 383 | i++; |
---|
[ae2e227] | 384 | } |
---|
[2955ba] | 385 | if ( p1 != pe ) |
---|
| 386 | { |
---|
| 387 | M.newpair( Variable(p1), Variable(k) ); |
---|
| 388 | N.newpair( Variable(k), Variable(p1) ); |
---|
[ae2e227] | 389 | k++; |
---|
| 390 | } |
---|
| 391 | i = 1; |
---|
[2955ba] | 392 | while ( i <= n ) |
---|
| 393 | { |
---|
| 394 | if ( degsf[i] > 0 && degsg[i] == 0 ) { |
---|
| 395 | if ( i != k ) |
---|
| 396 | { |
---|
| 397 | M.newpair( Variable(i), Variable(k) ); |
---|
| 398 | k++; |
---|
[ae2e227] | 399 | } |
---|
[2955ba] | 400 | } |
---|
| 401 | else if ( degsf[i] == 0 && degsg[i] > 0 ) |
---|
| 402 | { |
---|
| 403 | if ( i != k ) |
---|
| 404 | { |
---|
| 405 | M.newpair( Variable(i), Variable(k) ); |
---|
| 406 | k++; |
---|
[ae2e227] | 407 | } |
---|
[2955ba] | 408 | } |
---|
| 409 | i++; |
---|
[cc3e5e] | 410 | } |
---|
[1f28ed] | 411 | |
---|
[8d25143] | 412 | DELETE_ARRAY(degsf); |
---|
| 413 | DELETE_ARRAY(degsg); |
---|
[cc3e5e] | 414 | } |
---|