source: git/factory/cf_map.cc @ 160f8f7

fieker-DuValspielwiese
Last change on this file since 160f8f7 was b973c0, checked in by Jens Schmidt <schmidt@…>, 27 years ago
#include <config.h> added git-svn-id: file:///usr/local/Singular/svn/trunk@136 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 4.8 KB
Line 
1// emacs edit mode for this file is -*- C++ -*-
2// $Id: cf_map.cc,v 1.3 1997-04-07 16:02:21 schmidt Exp $
3
4/*
5$Log: not supported by cvs2svn $
6Revision 1.2  1997/03/26 16:47:32  schmidt
7stream-io wrapped by NOSTREAMIO
8
9Revision 1.1  1996/07/08 08:17:22  stobbe
10"New function compress( f, g, M, N ) that maps the common variables to be
11the ones with the lowest level.
12"
13
14Revision 1.0  1996/05/17 10:59:44  stobbe
15Initial revision
16
17*/
18
19#include <config.h>
20
21#include "assert.h"
22
23#include "cf_defs.h"
24#include "cf_map.h"
25#include "cf_iter.h"
26#include "templates/functions.h"
27
28
29static int cmpfunc ( const MapPair & p1, const MapPair & p2 );
30static void insfunc ( MapPair & orgp, const MapPair & newp );
31static CanonicalForm subsrec( const CanonicalForm & f, const ListIterator<MapPair> & i );
32
33MapPair&
34MapPair::operator= ( const MapPair & p )
35{
36    if ( this != &p ) {
37        V = p.V;
38        S = p.S;
39    }
40    return *this;
41}
42
43#ifndef NOSTREAMIO
44ostream&
45operator << ( ostream& s, const MapPair & p )
46{
47    s << p.var() << " -> " << p.subst();
48    return s;
49}
50#endif /* NOSTREAMIO */
51
52CFMap::CFMap ( const List<CanonicalForm> & L )
53{
54    ListIterator<CanonicalForm> i;
55    int j;
56    for ( i = L, j = 1; i.hasItem(); i++, j++ )
57        P.append( MapPair( Variable(j), i.getItem() ) );
58}
59
60CFMap&
61CFMap::operator= ( const CFMap & m )
62{
63    if ( this != &m )
64        P = m.P;
65    return *this;
66}
67
68void
69CFMap::newpair( const Variable & v, const CanonicalForm & s )
70{
71    P.insert( MapPair( v, s ), cmpfunc, insfunc );
72}
73
74CanonicalForm
75CFMap::operator() ( const CanonicalForm & f ) const
76{
77    ListIterator<MapPair> i = P;
78    return subsrec( f, i );
79}
80
81#ifndef NOSTREAMIO
82ostream&
83operator<< ( ostream& s, const CFMap & m )
84{
85    if ( m.P.isEmpty() )
86        s << "( )";
87    else {
88        ListIterator<MapPair> i = m.P;
89        s << "( " << i.getItem();
90        i++;
91        while ( i.hasItem() ) {
92            s << ", " << i.getItem();
93            i++;
94        }
95        s << " )";
96    }
97    return s;
98}
99#endif /* NOSTREAMIO */
100
101CanonicalForm
102compress ( const CanonicalForm & f, CFMap & m )
103{
104    CanonicalForm result = f;
105    int i, n;
106    int * degs = degrees( f );
107
108    m = CFMap();
109    n = i = 1;
110    while ( i <= level( f ) ) {
111        while( degs[i] == 0 ) i++;
112        if ( i != n ) {
113            m.newpair( Variable( n ), Variable( i ) );
114            result = swapvar( result, Variable( i ), Variable( n ) );
115        }
116        n++; i++;
117    }
118    delete [] degs;
119    return result;
120}
121
122void
123compress ( const CFArray & a, CFMap & M, CFMap & N )
124{
125    M = N = CFMap();
126    if ( a.size() == 0 )
127        return;
128    int maxlevel = level( a[a.min()] );
129    int i, j;
130    for ( i = a.min() + 1; i <= a.max(); i++ )
131        if ( level( a[i] ) > maxlevel )
132            maxlevel = level( a[i] );
133    if ( maxlevel <= 0 )
134        return;
135    int * degs = new int[maxlevel+1];
136    int * tmp = new int[maxlevel+1];
137    for ( i = 1; i <= maxlevel; i++ )
138        degs[i] = 0;
139    for ( i = a.min(); i <= a.max(); i++ ) {
140        tmp = degrees( a[i], tmp );
141        for ( j = 1; j <= level( a[i] ); j++ )
142            if ( tmp[j] != 0 )
143                degs[j] = 1;
144    }
145    i = 1;
146    j = 1;
147    while ( i <= maxlevel ) {
148        if ( degs[i] != 0 ) {
149            M.newpair( Variable(i), Variable(j) );
150            N.newpair( Variable(j), Variable(i) );
151            j++;
152        }
153        i++;
154    }
155    delete [] tmp;
156    delete [] degs;
157}
158
159void compress ( const CanonicalForm & f, const CanonicalForm & g, CFMap & M, CFMap & N )
160{
161    int n = tmax( f.level(), g.level() );
162    int i, k, m;
163    int * degsf = new int[n+1];
164    int * degsg = new int[n+1];
165
166    for ( i = 0; i <= n; i++ ) {
167        degsf[i] = degsg[i] = 0;
168    }
169    degsf = degrees( f, degsf );
170    degsg = degrees( g, degsg );
171    i = 1; k = 1; m = n;
172    while ( i <= n ) {
173        if ( degsf[i] > 0 && degsg[i] > 0 ) {
174            if ( i != k ) {
175                M.newpair( Variable(i), Variable(k) );
176                N.newpair( Variable(k), Variable(i) );
177            }
178            k++;
179        }
180        else {
181            M.newpair( Variable(i), Variable(m) );
182            N.newpair( Variable(m), Variable(i) );
183            m--;
184        }
185        i++;
186    }
187    delete [] degsf;
188    delete [] degsg;
189}
190
191// static functions
192
193int
194cmpfunc ( const MapPair & p1, const MapPair & p2 )
195{
196    if ( p1.var() > p2.var() ) return -1;
197    else if ( p1.var() == p2.var() ) return 0;
198    else return 1;
199}
200
201void
202insfunc ( MapPair & orgp, const MapPair & newp )
203{
204    orgp = newp;
205}
206
207CanonicalForm
208subsrec( const CanonicalForm & f, const ListIterator<MapPair> & i )
209{
210    if ( f.inBaseDomain() ) return f;
211    ListIterator<MapPair> j = i;
212    while ( j.hasItem() && j.getItem().var() > f.mvar() ) j++;
213    if ( j.hasItem() )
214        if ( j.getItem().var() != f.mvar() ) {
215            CanonicalForm result = 0;
216            CFIterator I;
217            for ( I = f; I.hasTerms(); I++ )
218                result += power( f.mvar(), I.exp() ) * subsrec( I.coeff(), j );
219            return result;
220        }
221        else {
222            CanonicalForm result = 0, s = j.getItem().subst();
223            CFIterator I;
224            j++;
225            for ( I = f; I.hasTerms(); I++ )
226                result += subsrec( I.coeff(), j ) * power( s, I.exp() );
227            return result;
228        }
229    else
230        return f;
231}
Note: See TracBrowser for help on using the repository browser.