source: git/factory/cf_map.cc @ 493c477

spielwiese
Last change on this file since 493c477 was 493c477, checked in by Jens Schmidt <schmidt@…>, 27 years ago
o header fixed git-svn-id: file:///usr/local/Singular/svn/trunk@404 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 4.4 KB
Line 
1/* emacs edit mode for this file is -*- C++ -*- */
2/* $Id: cf_map.cc,v 1.4 1997-06-19 12:24:17 schmidt Exp $ */
3
4#include <config.h>
5
6#include "assert.h"
7
8#include "cf_defs.h"
9#include "cf_map.h"
10#include "cf_iter.h"
11#include "templates/functions.h"
12
13
14static int cmpfunc ( const MapPair & p1, const MapPair & p2 );
15static void insfunc ( MapPair & orgp, const MapPair & newp );
16static CanonicalForm subsrec( const CanonicalForm & f, const ListIterator<MapPair> & i );
17
18MapPair&
19MapPair::operator= ( const MapPair & p )
20{
21    if ( this != &p ) {
22        V = p.V;
23        S = p.S;
24    }
25    return *this;
26}
27
28#ifndef NOSTREAMIO
29ostream&
30operator << ( ostream& s, const MapPair & p )
31{
32    s << p.var() << " -> " << p.subst();
33    return s;
34}
35#endif /* NOSTREAMIO */
36
37CFMap::CFMap ( const List<CanonicalForm> & L )
38{
39    ListIterator<CanonicalForm> i;
40    int j;
41    for ( i = L, j = 1; i.hasItem(); i++, j++ )
42        P.append( MapPair( Variable(j), i.getItem() ) );
43}
44
45CFMap&
46CFMap::operator= ( const CFMap & m )
47{
48    if ( this != &m )
49        P = m.P;
50    return *this;
51}
52
53void
54CFMap::newpair( const Variable & v, const CanonicalForm & s )
55{
56    P.insert( MapPair( v, s ), cmpfunc, insfunc );
57}
58
59CanonicalForm
60CFMap::operator() ( const CanonicalForm & f ) const
61{
62    ListIterator<MapPair> i = P;
63    return subsrec( f, i );
64}
65
66#ifndef NOSTREAMIO
67ostream&
68operator<< ( ostream& s, const CFMap & m )
69{
70    if ( m.P.isEmpty() )
71        s << "( )";
72    else {
73        ListIterator<MapPair> i = m.P;
74        s << "( " << i.getItem();
75        i++;
76        while ( i.hasItem() ) {
77            s << ", " << i.getItem();
78            i++;
79        }
80        s << " )";
81    }
82    return s;
83}
84#endif /* NOSTREAMIO */
85
86CanonicalForm
87compress ( 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
107void
108compress ( 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
144void 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
178int
179cmpfunc ( 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
186void
187insfunc ( MapPair & orgp, const MapPair & newp )
188{
189    orgp = newp;
190}
191
192CanonicalForm
193subsrec( 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}
Note: See TracBrowser for help on using the repository browser.