source: git/factory/int_pp.cc @ 22743e

fieker-DuValspielwiese
Last change on this file since 22743e was 929cf9, checked in by Jens Schmidt <schmidt@…>, 27 years ago
* int_pp.cc: superfluous arguments removed git-svn-id: file:///usr/local/Singular/svn/trunk@804 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 8.8 KB
Line 
1/* emacs edit mode for this file is -*- C++ -*- */
2/* $Id: int_pp.cc,v 1.6 1997-10-10 10:57:48 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 *
272InternalPrimePower::modulosame ( InternalCF * )
273{
274    if ( deleteObject() ) delete this;
275    return CFFactory::basic( 0 );
276}
277
278InternalCF *
279InternalPrimePower::modsame ( InternalCF * )
280{
281    if ( deleteObject() ) delete this;
282    return CFFactory::basic( 0 );
283}
284
285void
286InternalPrimePower::divremsame ( InternalCF * c, InternalCF * & quot, InternalCF * & rem )
287{
288    if ( c == this ) {
289        quot = CFFactory::basic( 1 );
290        rem = CFFactory::basic( 0 );
291    }
292    else {
293        MP_INT dummy, a, b;
294        mpz_init( &dummy ); mpz_init( &a ); mpz_init( &b );
295        mpz_gcdext( &dummy, &a, &b, &primepow, &MPI( c ) );
296        ASSERT( mpz_cmp_si( &dummy, 1 ) == 0, "illegal inversion" );
297        mpz_clear( &dummy ); mpz_clear( &a );
298        if ( mpz_cmp_si( &b, 0 ) < 0 )
299            mpz_add( &b, &b, &primepow );
300        mpz_mul( &b, &b, &thempi );
301        mpz_mod( &b, &b, &primepow );
302        quot = new InternalPrimePower( b );
303        rem = CFFactory::basic( 0 );
304    }
305}
306
307bool
308InternalPrimePower::divremsamet ( InternalCF * c, InternalCF * & quot, InternalCF * & rem )
309{
310    divremsame( c, quot, rem );
311    return true;
312}
313
314int
315InternalPrimePower::comparecoeff ( InternalCF * )
316{
317    ASSERT( 0, "this function should never be called" );
318    return 0;
319}
320
321InternalCF *
322InternalPrimePower::addcoeff ( InternalCF * )
323{
324    ASSERT( 0, "this function should never be called" );
325    return this;
326}
327
328InternalCF *
329InternalPrimePower::subcoeff ( InternalCF *, bool )
330{
331    ASSERT( 0, "this function should never be called" );
332    return this;
333}
334
335InternalCF *
336InternalPrimePower::mulcoeff ( InternalCF * )
337{
338    ASSERT( 0, "this function should never be called" );
339    return this;
340}
341
342InternalCF *
343InternalPrimePower::dividecoeff ( InternalCF *, bool )
344{
345    ASSERT( 0, "this function should never be called" );
346    return this;
347}
348
349InternalCF *
350InternalPrimePower::divcoeff ( InternalCF *, bool )
351{
352    ASSERT( 0, "this function should never be called" );
353    return this;
354}
355
356InternalCF *
357InternalPrimePower::modcoeff ( InternalCF *, bool )
358{
359    ASSERT( 0, "this function should never be called" );
360    return this;
361}
362
363InternalCF *
364InternalPrimePower::modulocoeff ( InternalCF *, bool )
365{
366    ASSERT( 0, "this function should never be called" );
367    return this;
368}
369
370void
371InternalPrimePower::divremcoeff ( InternalCF *, InternalCF * &, InternalCF * &, bool )
372{
373    ASSERT( 0, "this function should never be called" );
374}
375
376bool
377InternalPrimePower::divremcoefft ( InternalCF *, InternalCF * &, InternalCF * &, bool )
378{
379    ASSERT( 0, "this function should never be called" );
380    return true;
381}
382
383int
384InternalPrimePower::intval () const
385{
386  return (int)mpz_get_si( &thempi );
387}
388
389int
390InternalPrimePower::intmod( int p ) const
391{
392  return (int)mpz_mmod_ui( 0, &thempi, (unsigned long)p );
393}
394
395//{{{ int InternalPrimePower::sign () const
396// docu: see CanonicalForm::sign()
397int
398InternalPrimePower::sign () const
399{
400    return mpz_sgn( &thempi );
401}
402//}}}
Note: See TracBrowser for help on using the repository browser.