Changeset 1f28ed in git for factory/cf_map.cc
- Timestamp:
- Jul 23, 1997, 6:22:34 PM (27 years ago)
- Branches:
- (u'spielwiese', 'fe61d9c35bf7c61f2b6cbf1b56e25e2f08d536cc')
- Children:
- 95d5c608e097fb2e4b27e6ab645ceba80a0a64d1
- Parents:
- e743b32f0ead428621a515ddad45bdd5134975a5
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
factory/cf_map.cc
re743b3 r1f28ed 1 1 /* emacs edit mode for this file is -*- C++ -*- */ 2 /* $Id: cf_map.cc,v 1.6 1997-06-30 15:28:50 schmidt Exp $ */ 2 /* $Id: cf_map.cc,v 1.7 1997-07-23 16:22:34 schmidt Exp $ */ 3 4 //{{{ docu 5 // 6 // cf_map.cc - definition of class CFMap. 7 // 8 // Used by: cf_gcd.cc, fac_multivar.cc, sm_sparsemod.cc 9 // 10 //}}} 3 11 4 12 #include <config.h> 5 13 6 #include "assert.h" 7 8 #include "cf_defs.h" 14 #include "canonicalform.h" 9 15 #include "cf_map.h" 10 16 #include "cf_iter.h" … … 15 21 #endif 16 22 17 18 static int cmpfunc ( const MapPair & p1, const MapPair & p2 ); 19 static void insfunc ( MapPair & orgp, const MapPair & newp ); 20 static CanonicalForm subsrec( const CanonicalForm & f, const ListIterator<MapPair> & i ); 21 23 //{{{ static int cmpfunc ( const MapPair & p1, const MapPair & p2 ) 24 //{{{ docu 25 // 26 // cmpfunc() - compare two map pairs. 27 // 28 // Return -1 if p2's variable is less than p1's, 0 if they are 29 // equal, 1 if p2's level is greater than p1's. 30 // 31 //}}} 32 static int 33 cmpfunc ( const MapPair & p1, const MapPair & p2 ) 34 { 35 if ( p1.var() > p2.var() ) return -1; 36 else if ( p1.var() == p2.var() ) return 0; 37 else return 1; 38 } 39 //}}} 40 41 //{{{ static void insfunc ( MapPair & orgp, const MapPair & newp ) 42 //{{{ docu 43 // 44 // insfunc() - assign newp to orgp. 45 // 46 // cmpfunc() and insfunc() are used as functions for inserting a 47 // map pair into a Map. 48 // 49 //}}} 50 static void 51 insfunc ( MapPair & orgp, const MapPair & newp ) 52 { 53 orgp = newp; 54 } 55 //}}} 56 57 //{{{ static CanonicalForm subsrec( const CanonicalForm & f, const MPListIterator & i ) 58 //{{{ docu 59 // 60 // subsrec() - recursively apply the substitutions in i to f. 61 // 62 // Substitutes algebraic variables, too. The substituted 63 // expression are not subject to further substitutions. 64 // 65 //}}} 66 static CanonicalForm 67 subsrec( const CanonicalForm & f, const MPListIterator & i ) 68 { 69 if ( f.inBaseDomain() ) return f; 70 MPListIterator j = i; 71 72 // skip MapPairs larger than the main variable of f 73 while ( j.hasItem() && j.getItem().var() > f.mvar() ) j++; 74 75 if ( j.hasItem() ) 76 if ( j.getItem().var() != f.mvar() ) { 77 // simply descend if the current MapPair variable is 78 // not the main variable of f 79 CanonicalForm result = 0; 80 CFIterator I; 81 for ( I = f; I.hasTerms(); I++ ) 82 result += power( f.mvar(), I.exp() ) * subsrec( I.coeff(), j ); 83 return result; 84 } 85 else { 86 // replace the main variable of f with the image of 87 // the current variable under MapPair 88 CanonicalForm result = 0; 89 CanonicalForm s = j.getItem().subst(); 90 CFIterator I; 91 // move on to the next MapPair 92 j++; 93 for ( I = f; I.hasTerms(); I++ ) 94 result += subsrec( I.coeff(), j ) * power( s, I.exp() ); 95 return result; 96 } 97 else 98 return f; 99 } 100 //}}} 101 102 //{{{ MapPair& MapPair::operator = ( const MapPair & p ) 103 //{{{ docu 104 // 105 // MapPair::operator = - assignment operator. 106 // 107 //}}} 22 108 MapPair& 23 MapPair::operator = ( const MapPair & p )109 MapPair::operator = ( const MapPair & p ) 24 110 { 25 111 if ( this != &p ) { … … 29 115 return *this; 30 116 } 117 //}}} 31 118 32 119 #ifndef NOSTREAMIO 120 //{{{ ostream& operator << ( ostream& s, const MapPair & p ) 121 //{{{ docu 122 // 123 // operator << - print a map pair ("V -> S"). 124 // 125 //}}} 33 126 ostream& 34 127 operator << ( ostream& s, const MapPair & p ) … … 37 130 return s; 38 131 } 132 //}}} 39 133 #endif /* NOSTREAMIO */ 40 134 41 CFMap::CFMap ( const List<CanonicalForm> & L ) 42 { 43 ListIterator<CanonicalForm> i; 135 //{{{ CFMap::CFMap ( const CFList & L ) 136 //{{{ docu 137 // 138 // CFMap::CFMap() - construct a CFMap from a CFList. 139 // 140 // Variable[i] will be mapped to CFList[i] under the resulting 141 // map. 142 // 143 //}}} 144 CFMap::CFMap ( const CFList & L ) 145 { 146 CFListIterator i; 44 147 int j; 45 148 for ( i = L, j = 1; i.hasItem(); i++, j++ ) 46 P.append( MapPair( Variable(j), i.getItem() ) ); 47 } 48 149 P.insert( MapPair( Variable(j), i.getItem() ) ); 150 } 151 //}}} 152 153 //{{{ CFMap& CFMap::operator = ( const CFMap & m ) 154 //{{{ docu 155 // 156 // CFMap::operator = - assignment operator. 157 // 158 //}}} 49 159 CFMap& 50 CFMap::operator = ( const CFMap & m )160 CFMap::operator = ( const CFMap & m ) 51 161 { 52 162 if ( this != &m ) … … 54 164 return *this; 55 165 } 56 166 //}}} 167 168 //{{{ void CFMap::newpair( const Variable & v, const CanonicalForm & s ) 169 //{{{ docu 170 // 171 // CFMap::newpair() - inserts a MapPair into a CFMap. 172 // 173 //}}} 57 174 void 58 175 CFMap::newpair( const Variable & v, const CanonicalForm & s ) … … 60 177 P.insert( MapPair( v, s ), cmpfunc, insfunc ); 61 178 } 62 179 //}}} 180 181 //{{{ CanonicalForm CFMap::operator () ( const CanonicalForm & f ) const 182 //{{{ docu 183 // 184 // CFMap::operator () - apply the map to f. 185 // 186 // See subsrec() for more detailed information. 187 // 188 //}}} 63 189 CanonicalForm 64 CFMap::operator () ( const CanonicalForm & f ) const65 { 66 ListIterator<MapPair>i = P;190 CFMap::operator () ( const CanonicalForm & f ) const 191 { 192 MPListIterator i = P; 67 193 return subsrec( f, i ); 68 194 } 195 //}}} 69 196 70 197 #ifndef NOSTREAMIO 198 //{{{ ostream& operator << ( ostream& s, const CFMap & m ) 199 //{{{ docu 200 // 201 // operator << - print a CFMap ("( V[1] -> S[1], ..., V[n] -> // S[n] )". 202 // 203 //}}} 71 204 ostream& 72 operator << ( ostream& s, const CFMap & m )205 operator << ( ostream& s, const CFMap & m ) 73 206 { 74 207 if ( m.P.isEmpty() ) 75 208 s << "( )"; 76 209 else { 77 ListIterator<MapPair>i = m.P;210 MPListIterator i = m.P; 78 211 s << "( " << i.getItem(); 79 212 i++; … … 86 219 return s; 87 220 } 221 //}}} 88 222 #endif /* NOSTREAMIO */ 89 223 224 //{{{ CanonicalForm compress ( const CanonicalForm & f, CFMap & m ) 225 //{{{ docu 226 // 227 // compress() - compress the canonical form f. 228 // 229 // Compress the polynomial f such that the levels of its 230 // polynomial variables are ordered without any gaps starting 231 // from level 1. Return the compressed polynomial and a map m to 232 // undo the compression. That is, if f' = compress(f, m), than f 233 // = m(f'). 234 // 235 //}}} 90 236 CanonicalForm 91 237 compress ( const CanonicalForm & f, CFMap & m ) … … 100 246 while( degs[i] == 0 ) i++; 101 247 if ( i != n ) { 248 // swap variables and remember the swap in the map 102 249 m.newpair( Variable( n ), Variable( i ) ); 103 250 result = swapvar( result, Variable( i ), Variable( n ) ); … … 108 255 return result; 109 256 } 110 257 //}}} 258 259 //{{{ void compress ( const CFArray & a, CFMap & M, CFMap & N ) 260 //{{{ docu 261 // 262 // compress() - compress the variables occuring in an a. 263 // 264 // Compress the polynomial variables occuring in a so that their 265 // levels are ordered without any gaps starting from level 1. 266 // Return the CFMap M to realize the compression and its inverse, 267 // the CFMap N. Note that if you compress a member of a using M 268 // the result of the compression is not necessarily compressed, 269 // since the map is constructed using all variables occuring in 270 // a. 271 // 272 //}}} 111 273 void 112 274 compress ( const CFArray & a, CFMap & M, CFMap & N ) … … 117 279 int maxlevel = level( a[a.min()] ); 118 280 int i, j; 281 282 // get the maximum of levels in a 119 283 for ( i = a.min() + 1; i <= a.max(); i++ ) 120 284 if ( level( a[i] ) > maxlevel ) … … 122 286 if ( maxlevel <= 0 ) 123 287 return; 288 124 289 int * degs = new int[maxlevel+1]; 125 290 int * tmp = new int[maxlevel+1]; 126 291 for ( i = 1; i <= maxlevel; i++ ) 127 292 degs[i] = 0; 293 294 // calculate the union of all levels occuring in a 128 295 for ( i = a.min(); i <= a.max(); i++ ) { 129 296 tmp = degrees( a[i], tmp ); … … 132 299 degs[j] = 1; 133 300 } 134 i = 1; 135 j = 1; 301 302 // create the maps 303 i = 1; j = 1; 136 304 while ( i <= maxlevel ) { 137 305 if ( degs[i] != 0 ) { … … 145 313 delete [] degs; 146 314 } 147 148 void compress ( const CanonicalForm & f, const CanonicalForm & g, CFMap & M, CFMap & N ) 315 //}}} 316 317 //{{{ void compress ( const CanonicalForm & f, const CanonicalForm & g, CFMap & M, CFMap & N ) 318 //{{{ docu 319 // 320 // compress() - compress the variables occurring in f and g. 321 // 322 // Compress the polynomial variables occurring in f and g so that 323 // the levels of variables common to f and g are ordered without 324 // any gaps starting from level 1, whereas the variables occuring 325 // in only one of f or g are moved to levels higher than the 326 // levels of the common variables. Return the CFMap M to realize 327 // the compression and its inverse, the CFMap N. 328 // 329 //}}} 330 void 331 compress ( const CanonicalForm & f, const CanonicalForm & g, CFMap & M, CFMap & N ) 149 332 { 150 333 int n = tmax( f.level(), g.level() ); … … 156 339 degsf[i] = degsg[i] = 0; 157 340 } 341 158 342 degsf = degrees( f, degsf ); 159 343 degsg = degrees( g, degsg ); … … 161 345 while ( i <= n ) { 162 346 if ( degsf[i] > 0 && degsg[i] > 0 ) { 347 // store common variables at the beginning 163 348 if ( i != k ) { 164 349 M.newpair( Variable(i), Variable(k) ); … … 168 353 } 169 354 else { 355 // all others at the end 170 356 M.newpair( Variable(i), Variable(m) ); 171 357 N.newpair( Variable(m), Variable(i) ); … … 174 360 i++; 175 361 } 362 176 363 delete [] degsf; 177 364 delete [] degsg; 178 365 } 179 180 // static functions 181 182 int 183 cmpfunc ( const MapPair & p1, const MapPair & p2 ) 184 { 185 if ( p1.var() > p2.var() ) return -1; 186 else if ( p1.var() == p2.var() ) return 0; 187 else return 1; 188 } 189 190 void 191 insfunc ( MapPair & orgp, const MapPair & newp ) 192 { 193 orgp = newp; 194 } 195 196 CanonicalForm 197 subsrec( const CanonicalForm & f, const ListIterator<MapPair> & i ) 198 { 199 if ( f.inBaseDomain() ) return f; 200 ListIterator<MapPair> j = i; 201 while ( j.hasItem() && j.getItem().var() > f.mvar() ) j++; 202 if ( j.hasItem() ) 203 if ( j.getItem().var() != f.mvar() ) { 204 CanonicalForm result = 0; 205 CFIterator I; 206 for ( I = f; I.hasTerms(); I++ ) 207 result += power( f.mvar(), I.exp() ) * subsrec( I.coeff(), j ); 208 return result; 209 } 210 else { 211 CanonicalForm result = 0, s = j.getItem().subst(); 212 CFIterator I; 213 j++; 214 for ( I = f; I.hasTerms(); I++ ) 215 result += subsrec( I.coeff(), j ) * power( s, I.exp() ); 216 return result; 217 } 218 else 219 return f; 220 } 366 //}}}
Note: See TracChangeset
for help on using the changeset viewer.