1 | /* emacs edit mode for this file is -*- C++ -*- */ |
---|
2 | |
---|
3 | #include "config.h" |
---|
4 | |
---|
5 | #include "cf_assert.h" |
---|
6 | |
---|
7 | #include "cf_defs.h" |
---|
8 | #include "int_pp.h" |
---|
9 | #include "canonicalform.h" |
---|
10 | #include "cf_factory.h" |
---|
11 | #include "imm.h" |
---|
12 | |
---|
13 | mpz_t InternalPrimePower::primepow; |
---|
14 | mpz_t 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 mpz_ptr mpi) { thempi[0]=*mpi;} |
---|
38 | |
---|
39 | InternalPrimePower::InternalPrimePower( const char * str, const int base ) |
---|
40 | { |
---|
41 | mpz_init_set_str( thempi, str, base ); |
---|
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 | mpz_t 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_fdiv_q_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::isOne, isZero () const |
---|
127 | // docu: see CanonicalForm::isOne(), CanonicalForm::isZero() |
---|
128 | bool |
---|
129 | InternalPrimePower::isOne () const |
---|
130 | { |
---|
131 | return mpz_cmp_ui( thempi, 1 ) == 0; |
---|
132 | } |
---|
133 | |
---|
134 | bool |
---|
135 | InternalPrimePower::isZero () const |
---|
136 | { |
---|
137 | return mpz_sgn( thempi ) == 0; |
---|
138 | } |
---|
139 | //}}} |
---|
140 | |
---|
141 | bool InternalPrimePower::is_imm() const |
---|
142 | { |
---|
143 | return false; |
---|
144 | } |
---|
145 | |
---|
146 | InternalCF* InternalPrimePower::genZero() |
---|
147 | { |
---|
148 | if ( isZero() ) |
---|
149 | return copyObject(); |
---|
150 | else |
---|
151 | return new InternalPrimePower(); |
---|
152 | } |
---|
153 | |
---|
154 | InternalCF* InternalPrimePower::genOne() |
---|
155 | { |
---|
156 | if ( isOne() ) |
---|
157 | return copyObject(); |
---|
158 | else |
---|
159 | return new InternalPrimePower(); |
---|
160 | } |
---|
161 | |
---|
162 | //{{{ InternalCF * InternalPrimePower::neg () |
---|
163 | // docu: see CanonicalForm::operator -() |
---|
164 | InternalCF * |
---|
165 | InternalPrimePower::neg () |
---|
166 | { |
---|
167 | if ( getRefCount() > 1 ) { |
---|
168 | decRefCount(); |
---|
169 | mpz_t dummy; |
---|
170 | mpz_init( dummy ); |
---|
171 | mpz_sub( dummy, primepow, thempi ); |
---|
172 | return new InternalPrimePower( dummy ); |
---|
173 | } else { |
---|
174 | mpz_sub( thempi, primepow, thempi ); |
---|
175 | return this; |
---|
176 | } |
---|
177 | } |
---|
178 | //}}} |
---|
179 | |
---|
180 | |
---|
181 | InternalCF* InternalPrimePower::addsame( InternalCF * c ) |
---|
182 | { |
---|
183 | if ( getRefCount() > 1 ) { |
---|
184 | decRefCount(); |
---|
185 | mpz_t dummy; |
---|
186 | mpz_init( dummy ); |
---|
187 | mpz_add( dummy, thempi, MPI( c ) ); |
---|
188 | if ( mpz_cmp( dummy, primepow ) >= 0 ) |
---|
189 | mpz_sub( dummy, dummy, primepow ); |
---|
190 | return new InternalPrimePower( dummy ); |
---|
191 | } |
---|
192 | else { |
---|
193 | mpz_add( thempi, thempi, MPI( c ) ); |
---|
194 | if ( mpz_cmp( thempi, primepow ) >= 0 ) |
---|
195 | mpz_sub( thempi, thempi, primepow ); |
---|
196 | return this; |
---|
197 | } |
---|
198 | } |
---|
199 | |
---|
200 | InternalCF* InternalPrimePower::subsame( InternalCF * c ) |
---|
201 | { |
---|
202 | if ( getRefCount() > 1 ) { |
---|
203 | decRefCount(); |
---|
204 | mpz_t dummy; |
---|
205 | mpz_init( dummy ); |
---|
206 | mpz_sub( dummy, thempi, MPI( c ) ); |
---|
207 | if ( mpz_cmp_si( dummy, 0 ) < 0 ) |
---|
208 | mpz_add( dummy, dummy, primepow ); |
---|
209 | return new InternalPrimePower( dummy ); |
---|
210 | } |
---|
211 | else { |
---|
212 | mpz_sub( thempi, thempi, MPI( c ) ); |
---|
213 | if ( mpz_cmp_si( thempi, 0 ) < 0 ) |
---|
214 | mpz_add( thempi, thempi, primepow ); |
---|
215 | return this; |
---|
216 | } |
---|
217 | } |
---|
218 | |
---|
219 | InternalCF* InternalPrimePower::mulsame( InternalCF * c ) |
---|
220 | { |
---|
221 | if ( getRefCount() > 1 ) { |
---|
222 | decRefCount(); |
---|
223 | mpz_t dummy; |
---|
224 | mpz_init( dummy ); |
---|
225 | mpz_mul( dummy, thempi, MPI( c ) ); |
---|
226 | mpz_mod( dummy, dummy, primepow ); |
---|
227 | return new InternalPrimePower( dummy ); |
---|
228 | } |
---|
229 | else { |
---|
230 | mpz_mul( thempi, thempi, MPI( c ) ); |
---|
231 | mpz_mod( thempi, thempi, primepow ); |
---|
232 | return this; |
---|
233 | } |
---|
234 | } |
---|
235 | |
---|
236 | InternalCF* InternalPrimePower::dividesame( InternalCF * c ) |
---|
237 | { |
---|
238 | return divsame( c ); |
---|
239 | } |
---|
240 | |
---|
241 | InternalCF* InternalPrimePower::divsame( InternalCF * c ) |
---|
242 | { |
---|
243 | if ( c == this ) { |
---|
244 | if ( deleteObject() ) delete this; |
---|
245 | return CFFactory::basic( 1L ); |
---|
246 | } |
---|
247 | if ( getRefCount() > 1 ) { |
---|
248 | decRefCount(); |
---|
249 | mpz_t dummy, a, b; |
---|
250 | mpz_init( dummy ); mpz_init( a ); mpz_init( b ); |
---|
251 | mpz_gcdext( dummy, a, b, primepow, MPI( c ) ); |
---|
252 | ASSERT( mpz_cmp_si( dummy, 1 ) == 0, "illegal inversion" ); |
---|
253 | mpz_clear( dummy ); mpz_clear( a ); |
---|
254 | if ( mpz_cmp_si( b, 0 ) < 0 ) |
---|
255 | mpz_add( b, b, primepow ); |
---|
256 | mpz_mul( b, b, thempi ); |
---|
257 | mpz_mod( b, b, primepow ); |
---|
258 | return new InternalPrimePower( b ); |
---|
259 | } |
---|
260 | else { |
---|
261 | mpz_t dummy, a, b; |
---|
262 | mpz_init( dummy ); mpz_init( a ); mpz_init( b ); |
---|
263 | mpz_gcdext( dummy, a, b, primepow, MPI( c ) ); |
---|
264 | ASSERT( mpz_cmp_si( dummy, 1 ) == 0, "illegal inversion" ); |
---|
265 | if ( mpz_cmp_si( b, 0 ) < 0 ) |
---|
266 | mpz_add( b, b, primepow ); |
---|
267 | mpz_mul( thempi, b, thempi ); |
---|
268 | mpz_mod( thempi, thempi, primepow ); |
---|
269 | mpz_clear( dummy ); mpz_clear( a ); mpz_clear( b ); |
---|
270 | return this; |
---|
271 | } |
---|
272 | } |
---|
273 | |
---|
274 | InternalCF * |
---|
275 | InternalPrimePower::modulosame ( InternalCF * ) |
---|
276 | { |
---|
277 | if ( deleteObject() ) delete this; |
---|
278 | return CFFactory::basic( 0L ); |
---|
279 | } |
---|
280 | |
---|
281 | InternalCF * |
---|
282 | InternalPrimePower::modsame ( InternalCF * ) |
---|
283 | { |
---|
284 | if ( deleteObject() ) delete this; |
---|
285 | return CFFactory::basic( 0L ); |
---|
286 | } |
---|
287 | |
---|
288 | void |
---|
289 | InternalPrimePower::divremsame ( InternalCF * c, InternalCF * & quot, InternalCF * & rem ) |
---|
290 | { |
---|
291 | if ( c == this ) { |
---|
292 | quot = CFFactory::basic( 1L ); |
---|
293 | rem = CFFactory::basic( 0L ); |
---|
294 | } |
---|
295 | else { |
---|
296 | mpz_t dummy, a, b; |
---|
297 | mpz_init( dummy ); mpz_init( a ); mpz_init( b ); |
---|
298 | mpz_gcdext( dummy, a, b, primepow, MPI( c ) ); |
---|
299 | ASSERT( mpz_cmp_si( dummy, 1 ) == 0, "illegal inversion" ); |
---|
300 | mpz_clear( dummy ); mpz_clear( a ); |
---|
301 | if ( mpz_cmp_si( b, 0 ) < 0 ) |
---|
302 | mpz_add( b, b, primepow ); |
---|
303 | mpz_mul( b, b, thempi ); |
---|
304 | mpz_mod( b, b, primepow ); |
---|
305 | quot = new InternalPrimePower( b ); |
---|
306 | rem = CFFactory::basic( 0L ); |
---|
307 | } |
---|
308 | } |
---|
309 | |
---|
310 | bool |
---|
311 | InternalPrimePower::divremsamet ( InternalCF * c, InternalCF * & quot, InternalCF * & rem ) |
---|
312 | { |
---|
313 | divremsame( c, quot, rem ); |
---|
314 | return true; |
---|
315 | } |
---|
316 | |
---|
317 | //{{{ int InternalPrimePower::comparesame, comparecoeff ( InternalCF * c ) |
---|
318 | // docu: see CanonicalForm::operator <(), CanonicalForm::operator ==() |
---|
319 | int |
---|
320 | InternalPrimePower::comparesame ( InternalCF * c ) |
---|
321 | { |
---|
322 | ASSERT( ! ::is_imm( c ) && c->levelcoeff() == PrimePowerDomain, "incompatible base coefficients" ); |
---|
323 | return mpz_cmp( thempi, MPI( c ) ); |
---|
324 | } |
---|
325 | |
---|
326 | int |
---|
327 | InternalPrimePower::comparecoeff ( InternalCF * ) |
---|
328 | { |
---|
329 | ASSERT1( 0, "comparecoeff() not implemented for class %s", this->classname() ); |
---|
330 | return 0; |
---|
331 | } |
---|
332 | //}}} |
---|
333 | |
---|
334 | InternalCF * |
---|
335 | InternalPrimePower::addcoeff ( InternalCF * ) |
---|
336 | { |
---|
337 | ASSERT( 0, "this function should never be called" ); |
---|
338 | return this; |
---|
339 | } |
---|
340 | |
---|
341 | InternalCF * |
---|
342 | InternalPrimePower::subcoeff ( InternalCF *, bool ) |
---|
343 | { |
---|
344 | ASSERT( 0, "this function should never be called" ); |
---|
345 | return this; |
---|
346 | } |
---|
347 | |
---|
348 | InternalCF * |
---|
349 | InternalPrimePower::mulcoeff ( InternalCF * ) |
---|
350 | { |
---|
351 | ASSERT( 0, "this function should never be called" ); |
---|
352 | return this; |
---|
353 | } |
---|
354 | |
---|
355 | InternalCF * |
---|
356 | InternalPrimePower::dividecoeff ( InternalCF *, bool ) |
---|
357 | { |
---|
358 | ASSERT( 0, "this function should never be called" ); |
---|
359 | return this; |
---|
360 | } |
---|
361 | |
---|
362 | InternalCF * |
---|
363 | InternalPrimePower::divcoeff ( InternalCF *, bool ) |
---|
364 | { |
---|
365 | ASSERT( 0, "this function should never be called" ); |
---|
366 | return this; |
---|
367 | } |
---|
368 | |
---|
369 | InternalCF * |
---|
370 | InternalPrimePower::modcoeff ( InternalCF *, bool ) |
---|
371 | { |
---|
372 | ASSERT( 0, "this function should never be called" ); |
---|
373 | return this; |
---|
374 | } |
---|
375 | |
---|
376 | InternalCF * |
---|
377 | InternalPrimePower::modulocoeff ( InternalCF *, bool ) |
---|
378 | { |
---|
379 | ASSERT( 0, "this function should never be called" ); |
---|
380 | return this; |
---|
381 | } |
---|
382 | |
---|
383 | void |
---|
384 | InternalPrimePower::divremcoeff ( InternalCF *, InternalCF * &, InternalCF * &, bool ) |
---|
385 | { |
---|
386 | ASSERT( 0, "this function should never be called" ); |
---|
387 | } |
---|
388 | |
---|
389 | bool |
---|
390 | InternalPrimePower::divremcoefft ( InternalCF *, InternalCF * &, InternalCF * &, bool ) |
---|
391 | { |
---|
392 | ASSERT( 0, "this function should never be called" ); |
---|
393 | return true; |
---|
394 | } |
---|
395 | |
---|
396 | long |
---|
397 | InternalPrimePower::intval () const |
---|
398 | { |
---|
399 | return mpz_get_si( thempi ); |
---|
400 | } |
---|
401 | |
---|
402 | int |
---|
403 | InternalPrimePower::intmod( int p ) const |
---|
404 | { |
---|
405 | return (int)mpz_fdiv_ui( thempi, (unsigned long)p ); |
---|
406 | } |
---|
407 | |
---|
408 | //{{{ int InternalPrimePower::sign () const |
---|
409 | // docu: see CanonicalForm::sign() |
---|
410 | int |
---|
411 | InternalPrimePower::sign () const |
---|
412 | { |
---|
413 | return mpz_sgn( thempi ); |
---|
414 | } |
---|
415 | //}}} |
---|