source: git/factory/fac_util.cc @ ccf43d5

spielwiese
Last change on this file since ccf43d5 was c6caf1, checked in by Jens Schmidt <schmidt@…>, 26 years ago
* fac_util.cc (maxCoeff): function removed. Declarations adapted. All references replaced by `maxNorm()'. git-svn-id: file:///usr/local/Singular/svn/trunk@1223 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 5.1 KB
Line 
1/* emacs edit mode for this file is -*- C++ -*- */
2/* $Id: fac_util.cc,v 1.8 1998-03-12 14:31:02 schmidt Exp $ */
3
4#include <config.h>
5
6#include "assert.h"
7
8#include "cf_defs.h"
9#include "canonicalform.h"
10#include "cf_iter.h"
11#include "fac_util.h"
12
13static CanonicalForm PK, PKHALF;
14
15static CanonicalForm mappk ( const CanonicalForm& );
16
17static CanonicalForm mappksymmetric ( const CanonicalForm& );
18
19
20modpk::modpk()
21{
22    p = 3;
23    k = 1;
24    pk = power( CanonicalForm( p ), k );
25    pkhalf = pk / 2;
26}
27
28modpk::modpk( int q, int l )
29{
30    p = q;
31    k = l;
32    pk = power( CanonicalForm( p ), k );
33    pkhalf = pk / 2;
34}
35
36modpk::modpk( const modpk & m )
37{
38    p = m.p;
39    k = m.k;
40    pk = m.pk;
41    pkhalf = m.pkhalf;
42}
43
44modpk&
45modpk::operator= ( const modpk & m )
46{
47    if ( this != &m ) {
48        p = m.p;
49        k = m.k;
50        pk = m.pk;
51        pkhalf = m.pkhalf;
52    }
53    return *this;
54}
55
56CanonicalForm
57modpk::inverse( const CanonicalForm & f, bool symmetric ) const
58{
59    CanonicalForm u, r0 = this->operator()( f, false ), r1 = pk, q0 = 1, q1 = 0;
60    while ( ( r0 > 0 ) && ( r1 > 0 ) ) {
61        u = r0 / r1;
62        r0 = r0 % r1;
63        q0 = u*q1 + q0;
64        if ( r0 > 0 ) {
65            u = r1 / r0;
66            r1 = r1 % r0;
67            q1 = u*q0 + q1;
68        }
69    }
70    if ( r0 == 0 )
71        return this->operator()( pk-q1, symmetric );
72    else
73        return this->operator()( q0, symmetric );
74}
75
76CanonicalForm
77modpk::operator() ( const CanonicalForm & f, bool symmetric ) const
78{
79    PKHALF = pkhalf;
80    PK = pk;
81    if ( symmetric )
82        return mapdomain( f, mappksymmetric );
83    else
84        return mapdomain( f, mappk );
85};
86
87CanonicalForm
88replaceLc( const CanonicalForm & f, const CanonicalForm & c )
89{
90    if ( f.inCoeffDomain() )
91        return c;
92    else
93        return f + ( c - LC( f ) ) * power( f.mvar(), degree( f ) );
94}
95
96CanonicalForm
97remainder( const CanonicalForm & f, const CanonicalForm & g, const modpk & pk )
98{
99    ASSERT( (f.inCoeffDomain() || f.isUnivariate()) && (g.inCoeffDomain() || g.isUnivariate()) && (f.inCoeffDomain() || g.inCoeffDomain() || f.mvar() == g.mvar()), "can not build remainder" );
100    if ( f.inCoeffDomain() )
101        if ( g.inCoeffDomain() )
102            return pk( f % g );
103        else
104            return pk( f );
105    else {
106        Variable x = f.mvar();
107        CanonicalForm invlcg = pk.inverse( g.lc() );
108        CanonicalForm result = f;
109        int degg = g.degree();
110        while ( result.degree() >= degg ) {
111            result = pk( result - lc( result ) * invlcg * g * power( x, result.degree() - degg ) );
112        }
113        return result;
114    }
115}
116
117void
118divremainder( const CanonicalForm & f, const CanonicalForm & g, CanonicalForm & quot, CanonicalForm & rem, const modpk & pk )
119{
120    ASSERT( (f.inCoeffDomain() || f.isUnivariate()) && (g.inCoeffDomain() || g.isUnivariate()) && (f.inCoeffDomain() || g.inCoeffDomain() || f.mvar() == g.mvar()), "can not build remainder" );
121    if ( f.inCoeffDomain() )
122        if ( g.inCoeffDomain() ) {
123            divrem( f, g, quot, rem );
124            quot = pk( quot );
125            rem = pk( rem );
126        }
127        else {
128            quot = 0;
129            rem = pk( f );
130        }
131    else {
132        Variable x = f.mvar();
133        CanonicalForm invlcg = pk.inverse( g.lc() );
134        rem = f;
135        int degg = g.degree();
136        while ( rem.degree() >= degg ) {
137            quot += pk( lc( rem ) * invlcg ) * power( x, rem.degree() - degg );
138            rem = pk( rem - lc( rem ) * invlcg * g * power( x, rem.degree() - degg ) );
139        }
140    }
141}
142
143CanonicalForm
144mappksymmetric ( const CanonicalForm & f )
145{
146    CanonicalForm result = mod( f, PK );
147    if ( result > PKHALF )
148        return result - PK;
149    else
150        return result;
151}
152
153CanonicalForm
154mappk ( const CanonicalForm & f )
155{
156    return mod( f, PK );
157}
158
159void
160extgcd ( const CanonicalForm & a, const CanonicalForm & b, CanonicalForm & S, CanonicalForm & T, const modpk & pk )
161{
162    int p = pk.getp(), k = pk.getk(), j;
163    CanonicalForm amodp, bmodp, smodp, tmodp, s, t, sigma, tau, e;
164    CanonicalForm modulus = p, sigmat, taut, q;
165
166    setCharacteristic( p );
167    {
168        amodp = mapinto( a ); bmodp = mapinto( b );
169        (void)extgcd( amodp, bmodp, smodp, tmodp );
170    }
171    setCharacteristic( 0 );
172    s = mapinto( smodp ); t = mapinto( tmodp );
173
174    for ( j = 1; j < k; j++ ) {
175        e = ( 1 - s * a - t * b ) / modulus;
176        setCharacteristic( p );
177        {
178            e = mapinto( e );
179            sigmat = smodp * e;
180            taut = tmodp * e;
181            divrem( sigmat, bmodp, q, sigma );
182            tau = taut + q * amodp;
183        }
184        setCharacteristic( 0 );
185        s += mapinto( sigma ) * modulus;
186        t += mapinto( tau ) * modulus;
187        modulus *= p;
188    }
189    S = s; T = t;
190}
191
192CanonicalForm
193sum ( const CFArray & a, int f, int l )
194{
195    if ( f < a.min() ) f = a.min();
196    if ( l > a.max() ) l = a.max();
197    CanonicalForm s = 0;
198    for ( int i = f; i <= l; i++ )
199        s += a[i];
200    return s;
201}
202
203CanonicalForm
204prod ( const CFArray & a, int f, int l )
205{
206    if ( f < a.min() ) f = a.min();
207    if ( l > a.max() ) l = a.max();
208    CanonicalForm p = 1;
209    for ( int i = f; i <= l; i++ )
210        p *= a[i];
211    return p;
212}
213
214CanonicalForm
215sum ( const CFArray & a )
216{
217    return sum( a, a.min(), a.max() );
218}
219
220CanonicalForm
221prod ( const CFArray & a )
222{
223    return prod( a, a.min(), a.max() );
224}
225
226CanonicalForm
227crossprod ( const CFArray & a, const CFArray & b )
228{
229    ASSERT( a.size() == b.size(), "array size mismatch" );
230    CanonicalForm s = 0;
231    int fa = a.min();
232    int fb = b.min();
233    int n = a.max();
234    for ( ; fa <= n; fa++, fb++ )
235        s += a[fa] * b[fb];
236    return s;
237}
Note: See TracBrowser for help on using the repository browser.