source: git/factory/cf_map.cc @ 62b35b6

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