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 | |
---|
13 | MP_INT InternalPrimePower::primepow; |
---|
14 | MP_INT InternalPrimePower::primepowhalf; |
---|
15 | int InternalPrimePower::prime; |
---|
16 | int InternalPrimePower::exp; |
---|
17 | int InternalPrimePower::initialized = InternalPrimePower::initialize(); |
---|
18 | |
---|
19 | |
---|
20 | InternalPrimePower::InternalPrimePower() |
---|
21 | { |
---|
22 | mpz_init( &thempi ); |
---|
23 | } |
---|
24 | |
---|
25 | InternalPrimePower::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 | |
---|
37 | InternalPrimePower::InternalPrimePower( const MP_INT & mpi ) : thempi( mpi ) {} |
---|
38 | |
---|
39 | InternalPrimePower::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 | |
---|
51 | InternalPrimePower::~InternalPrimePower() |
---|
52 | { |
---|
53 | mpz_clear( &thempi ); |
---|
54 | } |
---|
55 | |
---|
56 | InternalCF* InternalPrimePower::deepCopyObject() const |
---|
57 | { |
---|
58 | MP_INT dummy; |
---|
59 | mpz_init_set( &dummy, &thempi ); |
---|
60 | return new InternalPrimePower( dummy ); |
---|
61 | } |
---|
62 | |
---|
63 | InternalCF * 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 | |
---|
76 | int 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 | |
---|
85 | void |
---|
86 | InternalPrimePower::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 | |
---|
98 | int |
---|
99 | InternalPrimePower::getp() |
---|
100 | { |
---|
101 | return prime; |
---|
102 | } |
---|
103 | |
---|
104 | int |
---|
105 | InternalPrimePower::getk() |
---|
106 | { |
---|
107 | return exp; |
---|
108 | } |
---|
109 | |
---|
110 | #ifndef NOSTREAMIO |
---|
111 | void 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 | |
---|
126 | bool InternalPrimePower::isZero() const |
---|
127 | { |
---|
128 | return mpz_cmp_si( &thempi, 0 ) == 0; |
---|
129 | } |
---|
130 | |
---|
131 | bool InternalPrimePower::isOne() const |
---|
132 | { |
---|
133 | return mpz_cmp_si( &thempi, 1 ) == 0; |
---|
134 | } |
---|
135 | |
---|
136 | bool InternalPrimePower::is_imm() const |
---|
137 | { |
---|
138 | return false; |
---|
139 | } |
---|
140 | |
---|
141 | InternalCF* InternalPrimePower::genZero() |
---|
142 | { |
---|
143 | if ( isZero() ) |
---|
144 | return copyObject(); |
---|
145 | else |
---|
146 | return new InternalPrimePower(); |
---|
147 | } |
---|
148 | |
---|
149 | InternalCF* InternalPrimePower::genOne() |
---|
150 | { |
---|
151 | if ( isOne() ) |
---|
152 | return copyObject(); |
---|
153 | else |
---|
154 | return new InternalPrimePower(); |
---|
155 | } |
---|
156 | |
---|
157 | InternalCF* 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 | |
---|
173 | int InternalPrimePower::comparesame( InternalCF * c ) |
---|
174 | { |
---|
175 | return mpz_cmp( &thempi, &MPI( c ) ); |
---|
176 | } |
---|
177 | |
---|
178 | InternalCF* 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 | |
---|
197 | InternalCF* 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 | |
---|
216 | InternalCF* 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 | |
---|
233 | InternalCF* InternalPrimePower::dividesame( InternalCF * c ) |
---|
234 | { |
---|
235 | return divsame( c ); |
---|
236 | } |
---|
237 | |
---|
238 | InternalCF* 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 | |
---|
271 | InternalCF * |
---|
272 | InternalPrimePower::modulosame ( InternalCF * ) |
---|
273 | { |
---|
274 | if ( deleteObject() ) delete this; |
---|
275 | return CFFactory::basic( 0 ); |
---|
276 | } |
---|
277 | |
---|
278 | InternalCF * |
---|
279 | InternalPrimePower::modsame ( InternalCF * ) |
---|
280 | { |
---|
281 | if ( deleteObject() ) delete this; |
---|
282 | return CFFactory::basic( 0 ); |
---|
283 | } |
---|
284 | |
---|
285 | void |
---|
286 | InternalPrimePower::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 | |
---|
307 | bool |
---|
308 | InternalPrimePower::divremsamet ( InternalCF * c, InternalCF * & quot, InternalCF * & rem ) |
---|
309 | { |
---|
310 | divremsame( c, quot, rem ); |
---|
311 | return true; |
---|
312 | } |
---|
313 | |
---|
314 | int |
---|
315 | InternalPrimePower::comparecoeff ( InternalCF * ) |
---|
316 | { |
---|
317 | ASSERT( 0, "this function should never be called" ); |
---|
318 | return 0; |
---|
319 | } |
---|
320 | |
---|
321 | InternalCF * |
---|
322 | InternalPrimePower::addcoeff ( InternalCF * ) |
---|
323 | { |
---|
324 | ASSERT( 0, "this function should never be called" ); |
---|
325 | return this; |
---|
326 | } |
---|
327 | |
---|
328 | InternalCF * |
---|
329 | InternalPrimePower::subcoeff ( InternalCF *, bool ) |
---|
330 | { |
---|
331 | ASSERT( 0, "this function should never be called" ); |
---|
332 | return this; |
---|
333 | } |
---|
334 | |
---|
335 | InternalCF * |
---|
336 | InternalPrimePower::mulcoeff ( InternalCF * ) |
---|
337 | { |
---|
338 | ASSERT( 0, "this function should never be called" ); |
---|
339 | return this; |
---|
340 | } |
---|
341 | |
---|
342 | InternalCF * |
---|
343 | InternalPrimePower::dividecoeff ( InternalCF *, bool ) |
---|
344 | { |
---|
345 | ASSERT( 0, "this function should never be called" ); |
---|
346 | return this; |
---|
347 | } |
---|
348 | |
---|
349 | InternalCF * |
---|
350 | InternalPrimePower::divcoeff ( InternalCF *, bool ) |
---|
351 | { |
---|
352 | ASSERT( 0, "this function should never be called" ); |
---|
353 | return this; |
---|
354 | } |
---|
355 | |
---|
356 | InternalCF * |
---|
357 | InternalPrimePower::modcoeff ( InternalCF *, bool ) |
---|
358 | { |
---|
359 | ASSERT( 0, "this function should never be called" ); |
---|
360 | return this; |
---|
361 | } |
---|
362 | |
---|
363 | InternalCF * |
---|
364 | InternalPrimePower::modulocoeff ( InternalCF *, bool ) |
---|
365 | { |
---|
366 | ASSERT( 0, "this function should never be called" ); |
---|
367 | return this; |
---|
368 | } |
---|
369 | |
---|
370 | void |
---|
371 | InternalPrimePower::divremcoeff ( InternalCF *, InternalCF * &, InternalCF * &, bool ) |
---|
372 | { |
---|
373 | ASSERT( 0, "this function should never be called" ); |
---|
374 | } |
---|
375 | |
---|
376 | bool |
---|
377 | InternalPrimePower::divremcoefft ( InternalCF *, InternalCF * &, InternalCF * &, bool ) |
---|
378 | { |
---|
379 | ASSERT( 0, "this function should never be called" ); |
---|
380 | return true; |
---|
381 | } |
---|
382 | |
---|
383 | int |
---|
384 | InternalPrimePower::intval () const |
---|
385 | { |
---|
386 | return (int)mpz_get_si( &thempi ); |
---|
387 | } |
---|
388 | |
---|
389 | int |
---|
390 | InternalPrimePower::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() |
---|
397 | int |
---|
398 | InternalPrimePower::sign () const |
---|
399 | { |
---|
400 | return mpz_sgn( &thempi ); |
---|
401 | } |
---|
402 | //}}} |
---|