source: git/factory/int_pp.cc @ 92843a

fieker-DuValspielwiese
Last change on this file since 92843a was 92843a, checked in by Max Horn <max@…>, 10 years ago
Remove some dead code
  • Property mode set to 100644
File size: 9.7 KB
Line 
1/* emacs edit mode for this file is -*- C++ -*- */
2
3#ifdef HAVE_CONFIG_H
4#include "config.h"
5#endif /* HAVE_CONFIG_H */
6
7#include "cf_assert.h"
8
9#include "cf_defs.h"
10#include "int_pp.h"
11#include "canonicalform.h"
12#include "cf_factory.h"
13#include "imm.h"
14
15mpz_t InternalPrimePower::primepow;
16mpz_t InternalPrimePower::primepowhalf;
17int InternalPrimePower::prime;
18int InternalPrimePower::exp;
19int InternalPrimePower::initialized = InternalPrimePower::initialize();
20
21
22InternalPrimePower::InternalPrimePower()
23{
24    mpz_init( thempi );
25}
26
27InternalPrimePower::InternalPrimePower( const int i )
28{
29    mpz_init_set_si( thempi, i );
30    if ( mpz_cmp_si( thempi, 0 ) < 0 ) {
31        mpz_neg( thempi, thempi );
32        mpz_mod( thempi, thempi, primepow );
33        mpz_sub( thempi, primepow, thempi );
34    }
35    else
36        mpz_mod( thempi, thempi, primepow );
37}
38
39InternalPrimePower::InternalPrimePower( const mpz_ptr mpi) { thempi[0]=*mpi;}
40
41InternalPrimePower::InternalPrimePower( const char * str, const int base )
42{
43    mpz_init_set_str( thempi, str, base );
44    if ( mpz_cmp_si( thempi, 0 ) < 0 ) {
45        mpz_neg( thempi, thempi );
46        mpz_mod( thempi, thempi, primepow );
47        mpz_sub( thempi, primepow, thempi );
48    }
49    else
50        mpz_mod( thempi, thempi, primepow );
51}
52
53InternalPrimePower::~InternalPrimePower()
54{
55    mpz_clear( thempi );
56}
57
58InternalCF* InternalPrimePower::deepCopyObject() const
59{
60    mpz_t dummy;
61    mpz_init_set( dummy, thempi );
62    return new InternalPrimePower( dummy );
63}
64
65InternalCF * InternalPrimePower::normalize_myself()
66{
67    ASSERT( getRefCount() == 1, "illegal operation" );
68    if ( mpz_cmp_si( thempi, 0 ) < 0 ) {
69        mpz_neg( thempi, thempi );
70        mpz_mod( thempi, thempi, primepow );
71        mpz_sub( thempi, primepow, thempi );
72    }
73    else
74        mpz_mod( thempi, thempi, primepow );
75    return this;
76}
77
78int InternalPrimePower::initialize()
79{
80    mpz_init_set_si( primepow, 3 );
81    mpz_init_set_si( primepowhalf, 1 );
82    prime = 3;
83    exp = 1;
84    return 1;
85}
86
87void
88InternalPrimePower::setPrimePower( int p, int k )
89{
90    ASSERT( p > 1 && k > 0, "illegal prime power" );
91    if ( p != prime || k != exp ) {
92        mpz_set_si( primepow, p );
93        mpz_pow_ui( primepow, primepow, (unsigned int)k );
94        mpz_fdiv_q_ui( primepowhalf, primepow, 2 );
95        prime = p;
96        exp = k;
97    }
98}
99
100#ifndef NOSTREAMIO
101void InternalPrimePower::print( OSTREAM & os, char * c )
102{
103    if ( *c == '*' && mpz_cmp_si( thempi, 1 ) == 0 )
104        os << c+1;
105    else if ( *c == '*' && mpz_cmp_si( thempi, -1 ) == 0 )
106        os << '-' << c+1;
107    else {
108        char * str = new char[mpz_sizeinbase( thempi, 10 ) + 2];
109        str = mpz_get_str( str, 10, thempi );
110        os << str << c;
111        delete [] str;
112    }
113}
114#endif /* NOSTREAMIO */
115
116//{{{ bool InternalPrimePower::isOne, isZero () const
117// docu: see CanonicalForm::isOne(), CanonicalForm::isZero()
118bool
119InternalPrimePower::isOne () const
120{
121    return mpz_cmp_ui( thempi, 1 ) == 0;
122}
123
124bool
125InternalPrimePower::isZero () const
126{
127    return mpz_sgn( thempi ) == 0;
128}
129//}}}
130
131bool InternalPrimePower::is_imm() const
132{
133    return false;
134}
135
136InternalCF* InternalPrimePower::genZero()
137{
138    if ( isZero() )
139        return copyObject();
140    else
141        return new InternalPrimePower();
142}
143
144InternalCF* InternalPrimePower::genOne()
145{
146    if ( isOne() )
147        return copyObject();
148    else
149        return new InternalPrimePower();
150}
151
152//{{{ InternalCF * InternalPrimePower::neg ()
153// docu: see CanonicalForm::operator -()
154InternalCF *
155InternalPrimePower::neg ()
156{
157    if ( getRefCount() > 1 ) {
158        decRefCount();
159        mpz_t dummy;
160        mpz_init( dummy );
161        mpz_sub( dummy, primepow, thempi );
162        return new InternalPrimePower( dummy );
163    } else {
164        mpz_sub( thempi, primepow, thempi );
165        return this;
166    }
167}
168//}}}
169
170
171InternalCF* InternalPrimePower::addsame( InternalCF * c )
172{
173    if ( getRefCount() > 1 ) {
174        decRefCount();
175        mpz_t dummy;
176        mpz_init( dummy );
177        mpz_add( dummy, thempi, MPI( c ) );
178        if ( mpz_cmp( dummy, primepow ) >= 0 )
179            mpz_sub( dummy, dummy, primepow );
180        return new InternalPrimePower( dummy );
181    }
182    else {
183        mpz_add( thempi, thempi, MPI( c ) );
184        if ( mpz_cmp( thempi, primepow ) >= 0 )
185            mpz_sub( thempi, thempi, primepow );
186        return this;
187    }
188}
189
190InternalCF* InternalPrimePower::subsame( InternalCF * c )
191{
192    if ( getRefCount() > 1 ) {
193        decRefCount();
194        mpz_t dummy;
195        mpz_init( dummy );
196        mpz_sub( dummy, thempi, MPI( c ) );
197        if ( mpz_cmp_si( dummy, 0 ) < 0 )
198            mpz_add( dummy, dummy, primepow );
199        return new InternalPrimePower( dummy );
200    }
201    else {
202        mpz_sub( thempi, thempi, MPI( c ) );
203        if ( mpz_cmp_si( thempi, 0 ) < 0 )
204            mpz_add( thempi, thempi, primepow );
205        return this;
206    }
207}
208
209InternalCF* InternalPrimePower::mulsame( InternalCF * c )
210{
211    if ( getRefCount() > 1 ) {
212        decRefCount();
213        mpz_t dummy;
214        mpz_init( dummy );
215        mpz_mul( dummy, thempi, MPI( c ) );
216        mpz_mod( dummy, dummy, primepow );
217        return new InternalPrimePower( dummy );
218    }
219    else {
220        mpz_mul( thempi, thempi, MPI( c ) );
221        mpz_mod( thempi, thempi, primepow );
222        return this;
223    }
224}
225
226InternalCF* InternalPrimePower::dividesame( InternalCF * c )
227{
228    return divsame( c );
229}
230
231InternalCF* InternalPrimePower::divsame( InternalCF * c )
232{
233    if ( c == this ) {
234        if ( deleteObject() ) delete this;
235        return CFFactory::basic( 1L );
236    }
237    if ( getRefCount() > 1 ) {
238        decRefCount();
239        mpz_t dummy, a, b;
240        mpz_init( dummy ); mpz_init( a ); mpz_init( b );
241        mpz_gcdext( dummy, a, b, primepow, MPI( c ) );
242        ASSERT( mpz_cmp_si( dummy, 1 ) == 0, "illegal inversion" );
243        mpz_clear( dummy ); mpz_clear( a );
244        if ( mpz_cmp_si( b, 0 ) < 0 )
245            mpz_add( b, b, primepow );
246        mpz_mul( b, b, thempi );
247        mpz_mod( b, b, primepow );
248        return new InternalPrimePower( b );
249    }
250    else {
251        mpz_t dummy, a, b;
252        mpz_init( dummy ); mpz_init( a ); mpz_init( b );
253        mpz_gcdext( dummy, a, b, primepow, MPI( c ) );
254        ASSERT( mpz_cmp_si( dummy, 1 ) == 0, "illegal inversion" );
255        if ( mpz_cmp_si( b, 0 ) < 0 )
256            mpz_add( b, b, primepow );
257        mpz_mul( thempi, b, thempi );
258        mpz_mod( thempi, thempi, primepow );
259        mpz_clear( dummy ); mpz_clear( a ); mpz_clear( b );
260        return this;
261    }
262}
263
264InternalCF *
265InternalPrimePower::modulosame ( InternalCF * )
266{
267    if ( deleteObject() ) delete this;
268    return CFFactory::basic( 0L );
269}
270
271InternalCF *
272InternalPrimePower::modsame ( InternalCF * )
273{
274    if ( deleteObject() ) delete this;
275    return CFFactory::basic( 0L );
276}
277
278void
279InternalPrimePower::divremsame ( InternalCF * c, InternalCF * & quot, InternalCF * & rem )
280{
281    if ( c == this ) {
282        quot = CFFactory::basic( 1L );
283        rem = CFFactory::basic( 0L );
284    }
285    else {
286        mpz_t dummy, a, b;
287        mpz_init( dummy ); mpz_init( a ); mpz_init( b );
288        mpz_gcdext( dummy, a, b, primepow, MPI( c ) );
289        ASSERT( mpz_cmp_si( dummy, 1 ) == 0, "illegal inversion" );
290        mpz_clear( dummy ); mpz_clear( a );
291        if ( mpz_cmp_si( b, 0 ) < 0 )
292            mpz_add( b, b, primepow );
293        mpz_mul( b, b, thempi );
294        mpz_mod( b, b, primepow );
295        quot = new InternalPrimePower( b );
296        rem = CFFactory::basic( 0L );
297    }
298}
299
300bool
301InternalPrimePower::divremsamet ( InternalCF * c, InternalCF * & quot, InternalCF * & rem )
302{
303    divremsame( c, quot, rem );
304    return true;
305}
306
307//{{{ int InternalPrimePower::comparesame, comparecoeff ( InternalCF * c )
308// docu: see CanonicalForm::operator <(), CanonicalForm::operator ==()
309int
310InternalPrimePower::comparesame ( InternalCF * c )
311{
312    ASSERT( ! ::is_imm( c ) && c->levelcoeff() == PrimePowerDomain, "incompatible base coefficients" );
313    return mpz_cmp( thempi, MPI( c ) );
314}
315
316int
317InternalPrimePower::comparecoeff ( InternalCF * )
318{
319    ASSERT1( 0, "comparecoeff() not implemented for class %s", this->classname() );
320    return 0;
321}
322//}}}
323
324InternalCF *
325InternalPrimePower::addcoeff ( InternalCF * )
326{
327    ASSERT( 0, "this function should never be called" );
328    return this;
329}
330
331InternalCF *
332InternalPrimePower::subcoeff ( InternalCF *, bool )
333{
334    ASSERT( 0, "this function should never be called" );
335    return this;
336}
337
338InternalCF *
339InternalPrimePower::mulcoeff ( InternalCF * )
340{
341    ASSERT( 0, "this function should never be called" );
342    return this;
343}
344
345InternalCF *
346InternalPrimePower::dividecoeff ( InternalCF *, bool )
347{
348    ASSERT( 0, "this function should never be called" );
349    return this;
350}
351
352InternalCF *
353InternalPrimePower::divcoeff ( InternalCF *, bool )
354{
355    ASSERT( 0, "this function should never be called" );
356    return this;
357}
358
359InternalCF *
360InternalPrimePower::modcoeff ( InternalCF *, bool )
361{
362    ASSERT( 0, "this function should never be called" );
363    return this;
364}
365
366InternalCF *
367InternalPrimePower::modulocoeff ( InternalCF *, bool )
368{
369    ASSERT( 0, "this function should never be called" );
370    return this;
371}
372
373void
374InternalPrimePower::divremcoeff ( InternalCF *, InternalCF * &, InternalCF * &, bool )
375{
376    ASSERT( 0, "this function should never be called" );
377}
378
379bool
380InternalPrimePower::divremcoefft ( InternalCF *, InternalCF * &, InternalCF * &, bool )
381{
382    ASSERT( 0, "this function should never be called" );
383    return true;
384}
385
386long
387InternalPrimePower::intval () const
388{
389  return mpz_get_si( thempi );
390}
391
392int
393InternalPrimePower::intmod( int p ) const
394{
395  return (int)mpz_fdiv_ui( thempi, (unsigned long)p );
396}
397
398//{{{ int InternalPrimePower::sign () const
399// docu: see CanonicalForm::sign()
400int
401InternalPrimePower::sign () const
402{
403    return mpz_sgn( thempi );
404}
405//}}}
Note: See TracBrowser for help on using the repository browser.