1 | // emacs edit mode for this file is -*- C++ -*- |
---|
2 | // $Id: cf_map.cc,v 1.1 1996-07-08 08:17:22 stobbe Exp $ |
---|
3 | |
---|
4 | /* |
---|
5 | $Log: not supported by cvs2svn $ |
---|
6 | Revision 1.0 1996/05/17 10:59:44 stobbe |
---|
7 | Initial revision |
---|
8 | |
---|
9 | */ |
---|
10 | |
---|
11 | #include "assert.h" |
---|
12 | #include "cf_defs.h" |
---|
13 | #include "cf_map.h" |
---|
14 | #include "cf_iter.h" |
---|
15 | #include "templates/functions.h" |
---|
16 | |
---|
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 | |
---|
22 | MapPair& |
---|
23 | MapPair::operator= ( const MapPair & p ) |
---|
24 | { |
---|
25 | if ( this != &p ) { |
---|
26 | V = p.V; |
---|
27 | S = p.S; |
---|
28 | } |
---|
29 | return *this; |
---|
30 | } |
---|
31 | |
---|
32 | ostream& |
---|
33 | operator << ( ostream& s, const MapPair & p ) |
---|
34 | { |
---|
35 | s << p.var() << " -> " << p.subst(); |
---|
36 | return s; |
---|
37 | } |
---|
38 | |
---|
39 | CFMap::CFMap ( const List<CanonicalForm> & L ) |
---|
40 | { |
---|
41 | ListIterator<CanonicalForm> i; |
---|
42 | int j; |
---|
43 | for ( i = L, j = 1; i.hasItem(); i++, j++ ) |
---|
44 | P.append( MapPair( Variable(j), i.getItem() ) ); |
---|
45 | } |
---|
46 | |
---|
47 | CFMap& |
---|
48 | CFMap::operator= ( const CFMap & m ) |
---|
49 | { |
---|
50 | if ( this != &m ) |
---|
51 | P = m.P; |
---|
52 | return *this; |
---|
53 | } |
---|
54 | |
---|
55 | void |
---|
56 | CFMap::newpair( const Variable & v, const CanonicalForm & s ) |
---|
57 | { |
---|
58 | P.insert( MapPair( v, s ), cmpfunc, insfunc ); |
---|
59 | } |
---|
60 | |
---|
61 | CanonicalForm |
---|
62 | CFMap::operator() ( const CanonicalForm & f ) const |
---|
63 | { |
---|
64 | ListIterator<MapPair> i = P; |
---|
65 | return subsrec( f, i ); |
---|
66 | } |
---|
67 | |
---|
68 | ostream& |
---|
69 | operator<< ( ostream& s, const CFMap & m ) |
---|
70 | { |
---|
71 | if ( m.P.isEmpty() ) |
---|
72 | s << "( )"; |
---|
73 | else { |
---|
74 | ListIterator<MapPair> i = m.P; |
---|
75 | s << "( " << i.getItem(); |
---|
76 | i++; |
---|
77 | while ( i.hasItem() ) { |
---|
78 | s << ", " << i.getItem(); |
---|
79 | i++; |
---|
80 | } |
---|
81 | s << " )"; |
---|
82 | } |
---|
83 | return s; |
---|
84 | } |
---|
85 | |
---|
86 | CanonicalForm |
---|
87 | compress ( const CanonicalForm & f, CFMap & m ) |
---|
88 | { |
---|
89 | CanonicalForm result = f; |
---|
90 | int i, n; |
---|
91 | int * degs = degrees( f ); |
---|
92 | |
---|
93 | m = CFMap(); |
---|
94 | n = i = 1; |
---|
95 | while ( i <= level( f ) ) { |
---|
96 | while( degs[i] == 0 ) i++; |
---|
97 | if ( i != n ) { |
---|
98 | m.newpair( Variable( n ), Variable( i ) ); |
---|
99 | result = swapvar( result, Variable( i ), Variable( n ) ); |
---|
100 | } |
---|
101 | n++; i++; |
---|
102 | } |
---|
103 | delete [] degs; |
---|
104 | return result; |
---|
105 | } |
---|
106 | |
---|
107 | void |
---|
108 | compress ( const CFArray & a, CFMap & M, CFMap & N ) |
---|
109 | { |
---|
110 | M = N = CFMap(); |
---|
111 | if ( a.size() == 0 ) |
---|
112 | return; |
---|
113 | int maxlevel = level( a[a.min()] ); |
---|
114 | int i, j; |
---|
115 | for ( i = a.min() + 1; i <= a.max(); i++ ) |
---|
116 | if ( level( a[i] ) > maxlevel ) |
---|
117 | maxlevel = level( a[i] ); |
---|
118 | if ( maxlevel <= 0 ) |
---|
119 | return; |
---|
120 | int * degs = new int[maxlevel+1]; |
---|
121 | int * tmp = new int[maxlevel+1]; |
---|
122 | for ( i = 1; i <= maxlevel; i++ ) |
---|
123 | degs[i] = 0; |
---|
124 | for ( i = a.min(); i <= a.max(); i++ ) { |
---|
125 | tmp = degrees( a[i], tmp ); |
---|
126 | for ( j = 1; j <= level( a[i] ); j++ ) |
---|
127 | if ( tmp[j] != 0 ) |
---|
128 | degs[j] = 1; |
---|
129 | } |
---|
130 | i = 1; |
---|
131 | j = 1; |
---|
132 | while ( i <= maxlevel ) { |
---|
133 | if ( degs[i] != 0 ) { |
---|
134 | M.newpair( Variable(i), Variable(j) ); |
---|
135 | N.newpair( Variable(j), Variable(i) ); |
---|
136 | j++; |
---|
137 | } |
---|
138 | i++; |
---|
139 | } |
---|
140 | delete [] tmp; |
---|
141 | delete [] degs; |
---|
142 | } |
---|
143 | |
---|
144 | void compress ( const CanonicalForm & f, const CanonicalForm & g, CFMap & M, CFMap & N ) |
---|
145 | { |
---|
146 | int n = tmax( f.level(), g.level() ); |
---|
147 | int i, k, m; |
---|
148 | int * degsf = new int[n+1]; |
---|
149 | int * degsg = new int[n+1]; |
---|
150 | |
---|
151 | for ( i = 0; i <= n; i++ ) { |
---|
152 | degsf[i] = degsg[i] = 0; |
---|
153 | } |
---|
154 | degsf = degrees( f, degsf ); |
---|
155 | degsg = degrees( g, degsg ); |
---|
156 | i = 1; k = 1; m = n; |
---|
157 | while ( i <= n ) { |
---|
158 | if ( degsf[i] > 0 && degsg[i] > 0 ) { |
---|
159 | if ( i != k ) { |
---|
160 | M.newpair( Variable(i), Variable(k) ); |
---|
161 | N.newpair( Variable(k), Variable(i) ); |
---|
162 | } |
---|
163 | k++; |
---|
164 | } |
---|
165 | else { |
---|
166 | M.newpair( Variable(i), Variable(m) ); |
---|
167 | N.newpair( Variable(m), Variable(i) ); |
---|
168 | m--; |
---|
169 | } |
---|
170 | i++; |
---|
171 | } |
---|
172 | delete [] degsf; |
---|
173 | delete [] degsg; |
---|
174 | } |
---|
175 | |
---|
176 | // static functions |
---|
177 | |
---|
178 | int |
---|
179 | cmpfunc ( const MapPair & p1, const MapPair & p2 ) |
---|
180 | { |
---|
181 | if ( p1.var() > p2.var() ) return -1; |
---|
182 | else if ( p1.var() == p2.var() ) return 0; |
---|
183 | else return 1; |
---|
184 | } |
---|
185 | |
---|
186 | void |
---|
187 | insfunc ( MapPair & orgp, const MapPair & newp ) |
---|
188 | { |
---|
189 | orgp = newp; |
---|
190 | } |
---|
191 | |
---|
192 | CanonicalForm |
---|
193 | subsrec( const CanonicalForm & f, const ListIterator<MapPair> & i ) |
---|
194 | { |
---|
195 | if ( f.inBaseDomain() ) return f; |
---|
196 | ListIterator<MapPair> j = i; |
---|
197 | while ( j.hasItem() && j.getItem().var() > f.mvar() ) j++; |
---|
198 | if ( j.hasItem() ) |
---|
199 | if ( j.getItem().var() != f.mvar() ) { |
---|
200 | CanonicalForm result = 0; |
---|
201 | CFIterator I; |
---|
202 | for ( I = f; I.hasTerms(); I++ ) |
---|
203 | result += power( f.mvar(), I.exp() ) * subsrec( I.coeff(), j ); |
---|
204 | return result; |
---|
205 | } |
---|
206 | else { |
---|
207 | CanonicalForm result = 0, s = j.getItem().subst(); |
---|
208 | CFIterator I; |
---|
209 | j++; |
---|
210 | for ( I = f; I.hasTerms(); I++ ) |
---|
211 | result += subsrec( I.coeff(), j ) * power( s, I.exp() ); |
---|
212 | return result; |
---|
213 | } |
---|
214 | else |
---|
215 | return f; |
---|
216 | } |
---|
217 | |
---|
218 | |
---|