source: git/factory/int_pp.cc @ de12f8

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