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