1 | // emacs edit mode for this file is -*- C++ -*- |
---|
2 | // $Id: int_int.cc,v 1.2 1997-03-27 10:05:39 schmidt Exp $ |
---|
3 | |
---|
4 | /* |
---|
5 | $Log: not supported by cvs2svn $ |
---|
6 | Revision 1.1 1996/12/20 15:35:26 schmidt |
---|
7 | Ruedigers last fryday afternoon fix |
---|
8 | |
---|
9 | Revision 1.0 1996/05/17 10:59:46 stobbe |
---|
10 | Initial revision |
---|
11 | |
---|
12 | */ |
---|
13 | |
---|
14 | #include "assert.h" |
---|
15 | |
---|
16 | #include "cf_defs.h" |
---|
17 | |
---|
18 | #include "cf_globals.h" |
---|
19 | #include "int_int.h" |
---|
20 | #include "int_rat.h" |
---|
21 | #include "imm.h" |
---|
22 | #include "canonicalform.h" |
---|
23 | #include "gmpext.h" |
---|
24 | |
---|
25 | #define IntInt InternalInteger |
---|
26 | |
---|
27 | InternalInteger::InternalInteger() |
---|
28 | { |
---|
29 | mpz_init( &thempi ); |
---|
30 | } |
---|
31 | |
---|
32 | InternalInteger::InternalInteger( const int i ) |
---|
33 | { |
---|
34 | mpz_init_set_si( &thempi, i ); |
---|
35 | } |
---|
36 | |
---|
37 | InternalInteger::InternalInteger( const MP_INT & mpi) : thempi( mpi ) {} |
---|
38 | |
---|
39 | InternalInteger::InternalInteger( const char * str ) |
---|
40 | { |
---|
41 | mpz_init_set_str( &thempi, str, 10 ); |
---|
42 | } |
---|
43 | |
---|
44 | InternalInteger::~InternalInteger() |
---|
45 | { |
---|
46 | mpz_clear( &thempi ); |
---|
47 | } |
---|
48 | |
---|
49 | InternalCF* InternalInteger::deepCopyObject() const |
---|
50 | { |
---|
51 | MP_INT dummy; |
---|
52 | mpz_init_set( &dummy, &thempi ); |
---|
53 | return new InternalInteger( dummy ); |
---|
54 | } |
---|
55 | |
---|
56 | #ifndef NOSTREAMIO |
---|
57 | void InternalInteger::print( ostream & os, char * c ) |
---|
58 | { |
---|
59 | if ( *c == '*' && mpz_cmp_si( &thempi, 1 ) == 0 ) |
---|
60 | os << c+1; |
---|
61 | else if ( *c == '*' && mpz_cmp_si( &thempi, -1 ) == 0 ) |
---|
62 | os << '-' << c+1; |
---|
63 | else { |
---|
64 | char * str = new char[mpz_sizeinbase( &thempi, 10 ) + 2]; |
---|
65 | str = mpz_get_str( str, 10, &thempi ); |
---|
66 | os << str << c; |
---|
67 | delete [] str; |
---|
68 | } |
---|
69 | } |
---|
70 | #endif /* NOSTREAMIO */ |
---|
71 | |
---|
72 | bool InternalInteger::isZero() const |
---|
73 | { |
---|
74 | return mpz_cmp_si( &thempi, 0 ) == 0; |
---|
75 | } |
---|
76 | |
---|
77 | bool InternalInteger::isOne() const |
---|
78 | { |
---|
79 | return mpz_cmp_si( &thempi, 1 ) == 0; |
---|
80 | } |
---|
81 | |
---|
82 | bool InternalInteger::is_imm() const |
---|
83 | { |
---|
84 | return mpz_is_imm( &thempi ); |
---|
85 | } |
---|
86 | |
---|
87 | InternalCF* InternalInteger::genZero() |
---|
88 | { |
---|
89 | if ( isZero() ) |
---|
90 | return copyObject(); |
---|
91 | else |
---|
92 | return new InternalInteger(); |
---|
93 | } |
---|
94 | |
---|
95 | InternalCF* InternalInteger::genOne() |
---|
96 | { |
---|
97 | if ( isOne() ) |
---|
98 | return copyObject(); |
---|
99 | else |
---|
100 | return new InternalInteger( 1 ); |
---|
101 | } |
---|
102 | |
---|
103 | InternalCF* InternalInteger::neg() |
---|
104 | { |
---|
105 | if ( getRefCount() > 1 ) { |
---|
106 | decRefCount(); |
---|
107 | MP_INT dummy; |
---|
108 | mpz_init( &dummy ); |
---|
109 | mpz_neg( &dummy, &thempi ); |
---|
110 | return new InternalInteger( dummy ); |
---|
111 | } |
---|
112 | else { |
---|
113 | mpz_neg( &thempi, &thempi ); |
---|
114 | return this; |
---|
115 | } |
---|
116 | } |
---|
117 | |
---|
118 | |
---|
119 | int InternalInteger::comparesame( InternalCF * c ) |
---|
120 | { |
---|
121 | return mpz_cmp( &thempi, &MPI( c ) ); |
---|
122 | } |
---|
123 | |
---|
124 | InternalCF* InternalInteger::addsame( InternalCF * c ) |
---|
125 | { |
---|
126 | if ( getRefCount() > 1 ) { |
---|
127 | decRefCount(); |
---|
128 | MP_INT dummy; |
---|
129 | mpz_init( &dummy ); |
---|
130 | mpz_add( &dummy, &thempi, &MPI( c ) ); |
---|
131 | if ( mpz_is_imm( &dummy ) ) { |
---|
132 | InternalCF * res = int2imm( mpz_get_si( &dummy ) ); |
---|
133 | mpz_clear( &dummy ); |
---|
134 | return res; |
---|
135 | } |
---|
136 | else |
---|
137 | return new InternalInteger( dummy ); |
---|
138 | } |
---|
139 | else { |
---|
140 | mpz_add( &thempi, &thempi, &MPI( c ) ); |
---|
141 | if ( mpz_is_imm( &thempi ) ) { |
---|
142 | InternalCF * res = int2imm( mpz_get_si( &thempi ) ); |
---|
143 | delete this; |
---|
144 | return res; |
---|
145 | } |
---|
146 | else |
---|
147 | return this; |
---|
148 | } |
---|
149 | } |
---|
150 | |
---|
151 | InternalCF* InternalInteger::subsame( InternalCF * c ) |
---|
152 | { |
---|
153 | if ( getRefCount() > 1 ) { |
---|
154 | decRefCount(); |
---|
155 | MP_INT dummy; |
---|
156 | mpz_init( &dummy ); |
---|
157 | mpz_sub( &dummy, &thempi, &MPI( c ) ); |
---|
158 | if ( mpz_is_imm( &dummy ) ) { |
---|
159 | InternalCF * res = int2imm( mpz_get_si( &dummy ) ); |
---|
160 | mpz_clear( &dummy ); |
---|
161 | return res; |
---|
162 | } |
---|
163 | else |
---|
164 | return new InternalInteger( dummy ); |
---|
165 | } |
---|
166 | else { |
---|
167 | mpz_sub( &thempi, &thempi, &MPI( c ) ); |
---|
168 | if ( mpz_is_imm( &thempi ) ) { |
---|
169 | InternalCF * res = int2imm( mpz_get_si( &thempi ) ); |
---|
170 | delete this; |
---|
171 | return res; |
---|
172 | } |
---|
173 | else |
---|
174 | return this; |
---|
175 | } |
---|
176 | } |
---|
177 | |
---|
178 | InternalCF* InternalInteger::mulsame( InternalCF * c ) |
---|
179 | { |
---|
180 | if ( getRefCount() > 1 ) { |
---|
181 | decRefCount(); |
---|
182 | MP_INT dummy; |
---|
183 | mpz_init( &dummy ); |
---|
184 | mpz_mul( &dummy, &thempi, &MPI( c ) ); |
---|
185 | if ( mpz_is_imm( &dummy ) ) { |
---|
186 | InternalCF * res = int2imm( mpz_get_si( &dummy ) ); |
---|
187 | mpz_clear( &dummy ); |
---|
188 | return res; |
---|
189 | } |
---|
190 | else |
---|
191 | return new InternalInteger( dummy ); |
---|
192 | } |
---|
193 | else { |
---|
194 | mpz_mul( &thempi, &thempi, &MPI( c ) ); |
---|
195 | if ( mpz_is_imm( &thempi ) ) { |
---|
196 | InternalCF * res = int2imm( mpz_get_si( &thempi ) ); |
---|
197 | delete this; |
---|
198 | return res; |
---|
199 | } |
---|
200 | else |
---|
201 | return this; |
---|
202 | } |
---|
203 | } |
---|
204 | |
---|
205 | InternalCF* InternalInteger::dividesame( InternalCF * c ) |
---|
206 | { |
---|
207 | return divsame( c ); |
---|
208 | } |
---|
209 | |
---|
210 | InternalCF* InternalInteger::divsame( InternalCF * c ) |
---|
211 | { |
---|
212 | if ( c == this ) { |
---|
213 | if ( deleteObject() ) delete this; |
---|
214 | return CFFactory::basic( 1 ); |
---|
215 | } |
---|
216 | if ( cf_glob_switches.isOn( SW_RATIONAL ) ) { |
---|
217 | MP_INT n, d; |
---|
218 | mpz_init_set( &n, &thempi ); |
---|
219 | mpz_init_set( &d, &MPI( c ) ); |
---|
220 | if ( deleteObject() ) delete this; |
---|
221 | InternalRational * result = new InternalRational( n, d ); |
---|
222 | return result->normalize_myself(); |
---|
223 | } |
---|
224 | if ( getRefCount() > 1 ) { |
---|
225 | decRefCount(); |
---|
226 | MP_INT quot; |
---|
227 | MP_INT rem; |
---|
228 | int signmpi = mpz_cmp_si( &thempi, 0 ); |
---|
229 | int signc = mpz_cmp_si( &MPI( c ), 0 ); |
---|
230 | if ( signmpi < 0 ) |
---|
231 | mpz_neg( &thempi, &thempi ); |
---|
232 | if ( signc < 0 ) |
---|
233 | mpz_neg( &MPI( c ), &MPI( c ) ); |
---|
234 | mpz_init( " ); mpz_init( &rem ); |
---|
235 | mpz_divmod( ", &rem, &thempi, &MPI( c ) ); |
---|
236 | if ( signmpi < 0 ) |
---|
237 | mpz_neg( &thempi, &thempi ); |
---|
238 | if ( signc < 0 ) |
---|
239 | mpz_neg( &MPI( c ), &MPI( c ) ); |
---|
240 | if ( signmpi < 0 && signc < 0 ) { |
---|
241 | if ( mpz_cmp_si( &rem, 0 ) != 0 ) |
---|
242 | mpz_add_ui( ", ", 1 ); |
---|
243 | } |
---|
244 | else if ( signc < 0 ) |
---|
245 | mpz_neg( ", " ); |
---|
246 | else if ( signmpi < 0 ) { |
---|
247 | mpz_neg( ", " ); |
---|
248 | if ( mpz_cmp_si( &rem, 0 ) != 0 ) |
---|
249 | mpz_sub_ui( ", ", 1 ); |
---|
250 | } |
---|
251 | mpz_clear( &rem ); |
---|
252 | if ( mpz_is_imm( " ) ) { |
---|
253 | InternalCF * res = int2imm( mpz_get_si( " ) ); |
---|
254 | mpz_clear( " ); |
---|
255 | return res; |
---|
256 | } |
---|
257 | else |
---|
258 | return new InternalInteger( quot ); |
---|
259 | } |
---|
260 | else { |
---|
261 | MP_INT rem; |
---|
262 | mpz_init( &rem ); |
---|
263 | int signmpi = mpz_cmp_si( &thempi, 0 ); |
---|
264 | int signc = mpz_cmp_si( &MPI( c ), 0 ); |
---|
265 | if ( signmpi < 0 ) |
---|
266 | mpz_neg( &thempi, &thempi ); |
---|
267 | if ( signc < 0 ) |
---|
268 | mpz_neg( &MPI( c ), &MPI( c ) ); |
---|
269 | mpz_divmod( &thempi, &rem, &thempi, &MPI( c ) ); |
---|
270 | if ( signc < 0 ) |
---|
271 | mpz_neg( &MPI( c ), &MPI( c ) ); |
---|
272 | if ( signmpi < 0 && signc < 0 ) { |
---|
273 | if ( mpz_cmp_si( &rem, 0 ) != 0 ) |
---|
274 | mpz_add_ui( &thempi, &thempi, 1 ); |
---|
275 | } |
---|
276 | else if ( signc < 0 ) |
---|
277 | mpz_neg( &thempi, &thempi ); |
---|
278 | else if ( signmpi < 0 ) { |
---|
279 | mpz_neg( &thempi, &thempi ); |
---|
280 | if ( mpz_cmp_si( &rem, 0 ) != 0 ) |
---|
281 | mpz_sub_ui( &thempi, &thempi, 1 ); |
---|
282 | } |
---|
283 | mpz_clear( &rem ); |
---|
284 | if ( mpz_is_imm( &thempi ) ) { |
---|
285 | InternalCF * res = int2imm( mpz_get_si( &thempi ) ); |
---|
286 | delete this; |
---|
287 | return res; |
---|
288 | } |
---|
289 | else |
---|
290 | return this; |
---|
291 | } |
---|
292 | } |
---|
293 | |
---|
294 | InternalCF* InternalInteger::modulosame ( InternalCF * c ) |
---|
295 | { |
---|
296 | return modsame( c ); |
---|
297 | } |
---|
298 | |
---|
299 | InternalCF* InternalInteger::modsame( InternalCF * c ) |
---|
300 | { |
---|
301 | if ( cf_glob_switches.isOn( SW_RATIONAL ) || c == this ) { |
---|
302 | if ( deleteObject() ) delete this; |
---|
303 | return CFFactory::basic( 0 ); |
---|
304 | } |
---|
305 | if ( getRefCount() > 1 ) { |
---|
306 | decRefCount(); |
---|
307 | MP_INT dummy; |
---|
308 | mpz_init( &dummy ); |
---|
309 | int signmpi = mpz_cmp_si( &thempi, 0 ); |
---|
310 | int signc = mpz_cmp_si( &MPI( c ), 0 ); |
---|
311 | if ( signmpi < 0 ) |
---|
312 | mpz_neg( &thempi, &thempi ); |
---|
313 | if ( signc < 0 ) |
---|
314 | mpz_neg( &MPI( c ), &MPI( c ) ); |
---|
315 | mpz_mod( &dummy, &thempi, &MPI( c ) ); |
---|
316 | if ( signmpi < 0 && mpz_cmp_si( &dummy, 0 ) != 0 ) { |
---|
317 | mpz_sub( &dummy, &MPI( c ), &dummy ); |
---|
318 | } |
---|
319 | if ( signmpi < 0 ) |
---|
320 | mpz_neg( &thempi, &thempi ); |
---|
321 | if ( signc < 0 ) |
---|
322 | mpz_neg( &MPI( c ), &MPI( c ) ); |
---|
323 | if ( mpz_is_imm( &dummy ) ) { |
---|
324 | InternalCF * res = int2imm( mpz_get_si( &dummy ) ); |
---|
325 | mpz_clear( &dummy ); |
---|
326 | return res; |
---|
327 | } |
---|
328 | else |
---|
329 | return new InternalInteger( dummy ); |
---|
330 | } |
---|
331 | else { |
---|
332 | int signmpi = mpz_cmp_si( &thempi, 0 ); |
---|
333 | int signc = mpz_cmp_si( &MPI( c ), 0 ); |
---|
334 | if ( signmpi < 0 ) |
---|
335 | mpz_neg( &thempi, &thempi ); |
---|
336 | if ( signc < 0 ) |
---|
337 | mpz_neg( &MPI( c ), &MPI( c ) ); |
---|
338 | mpz_mod( &thempi, &thempi, &MPI( c ) ); |
---|
339 | if ( signmpi < 0 && mpz_cmp_si( &thempi, 0 ) != 0 ) { |
---|
340 | mpz_sub( &thempi, &MPI( c ), &thempi ); |
---|
341 | } |
---|
342 | if ( signc < 0 ) |
---|
343 | mpz_neg( &MPI( c ), &MPI( c ) ); |
---|
344 | if ( mpz_is_imm( &thempi ) ) { |
---|
345 | InternalCF * res = int2imm( mpz_get_si( &thempi ) ); |
---|
346 | delete this; |
---|
347 | return res; |
---|
348 | } |
---|
349 | else |
---|
350 | return this; |
---|
351 | } |
---|
352 | } |
---|
353 | |
---|
354 | void InternalInteger::divremsame( InternalCF * c, InternalCF*& quot, InternalCF*& rem ) |
---|
355 | { |
---|
356 | if ( cf_glob_switches.isOn( SW_RATIONAL ) ) { |
---|
357 | quot = copyObject(); |
---|
358 | quot = quot->dividesame( c ); |
---|
359 | rem = CFFactory::basic( 0 ); |
---|
360 | } |
---|
361 | else if ( c == this ) { |
---|
362 | quot = CFFactory::basic( 1 ); |
---|
363 | rem = CFFactory::basic( 0 ); |
---|
364 | } |
---|
365 | else { |
---|
366 | MP_INT q; |
---|
367 | MP_INT r; |
---|
368 | mpz_init( &q ); mpz_init( &r ); |
---|
369 | int signmpi = mpz_cmp_si( &thempi, 0 ); |
---|
370 | int signc = mpz_cmp_si( &MPI( c ), 0 ); |
---|
371 | if ( signmpi < 0 ) |
---|
372 | mpz_neg( &thempi, &thempi ); |
---|
373 | if ( signc < 0 ) |
---|
374 | mpz_neg( &MPI( c ), &MPI( c ) ); |
---|
375 | mpz_divmod( &q, &r, &thempi, &MPI( c ) ); |
---|
376 | if ( signmpi < 0 && mpz_cmp_si( &r, 0 ) != 0 ) { |
---|
377 | mpz_sub( &r, &MPI( c ), &r ); |
---|
378 | } |
---|
379 | if ( signmpi < 0 ) |
---|
380 | mpz_neg( &thempi, &thempi ); |
---|
381 | if ( signc < 0 ) |
---|
382 | mpz_neg( &MPI( c ), &MPI( c ) ); |
---|
383 | if ( signmpi < 0 && signc < 0 ) { |
---|
384 | if ( mpz_cmp_si( &r, 0 ) != 0 ) |
---|
385 | mpz_add_ui( &q, &q, 1 ); |
---|
386 | } |
---|
387 | else if ( signc < 0 ) |
---|
388 | mpz_neg( &q, &q ); |
---|
389 | else if ( signmpi < 0 ) { |
---|
390 | mpz_neg( &q, &q ); |
---|
391 | if ( mpz_cmp_si( &r, 0 ) != 0 ) |
---|
392 | mpz_sub_ui( &q, &q, 1 ); |
---|
393 | } |
---|
394 | if ( mpz_is_imm( &q ) ) |
---|
395 | quot = int2imm( mpz_get_si( &q ) ); |
---|
396 | else |
---|
397 | quot = new InternalInteger( q ); |
---|
398 | if ( mpz_is_imm( &r ) ) |
---|
399 | rem = int2imm( mpz_get_si( &r ) ); |
---|
400 | else |
---|
401 | rem = new InternalInteger( r ); |
---|
402 | } |
---|
403 | } |
---|
404 | |
---|
405 | bool InternalInteger::divremsamet( InternalCF* c, InternalCF*& quot, InternalCF*& rem ) |
---|
406 | { |
---|
407 | divremsame( c, quot, rem ); |
---|
408 | return true; |
---|
409 | } |
---|
410 | |
---|
411 | int InternalInteger::comparecoeff( InternalCF* c ) |
---|
412 | { |
---|
413 | ASSERT( ::is_imm( c ) == INTMARK, "incompatible base coefficients" ); |
---|
414 | return mpz_cmp_si( &thempi, imm2int( c ) ); |
---|
415 | } |
---|
416 | |
---|
417 | InternalCF* InternalInteger::addcoeff( InternalCF* c ) |
---|
418 | { |
---|
419 | ASSERT( ::is_imm( c ) == INTMARK, "incompatible base coefficients" ); |
---|
420 | int cc = imm2int( c ); |
---|
421 | if ( getRefCount() > 1 ) { |
---|
422 | decRefCount(); |
---|
423 | MP_INT dummy; |
---|
424 | mpz_init( &dummy ); |
---|
425 | if ( cc < 0 ) |
---|
426 | mpz_sub_ui( &dummy, &thempi, -cc ); |
---|
427 | else |
---|
428 | mpz_add_ui( &dummy, &thempi, cc ); |
---|
429 | if ( mpz_is_imm( &dummy ) ) { |
---|
430 | InternalCF * res = int2imm( mpz_get_si( &dummy ) ); |
---|
431 | mpz_clear( &dummy ); |
---|
432 | return res; |
---|
433 | } |
---|
434 | else |
---|
435 | return new InternalInteger( dummy ); |
---|
436 | } |
---|
437 | else { |
---|
438 | if ( cc < 0 ) |
---|
439 | mpz_sub_ui( &thempi, &thempi, -cc ); |
---|
440 | else |
---|
441 | mpz_add_ui( &thempi, &thempi, cc ); |
---|
442 | if ( mpz_is_imm( &thempi ) ) { |
---|
443 | InternalCF * res = int2imm( mpz_get_si( &thempi ) ); |
---|
444 | delete this; |
---|
445 | return res; |
---|
446 | } |
---|
447 | else |
---|
448 | return this; |
---|
449 | } |
---|
450 | } |
---|
451 | |
---|
452 | InternalCF* InternalInteger::subcoeff( InternalCF* c, bool negate ) |
---|
453 | { |
---|
454 | ASSERT( ::is_imm( c ) == INTMARK, "incompatible base coefficients" ); |
---|
455 | int cc = imm2int( c ); |
---|
456 | if ( getRefCount() > 1 ) { |
---|
457 | decRefCount(); |
---|
458 | MP_INT dummy; |
---|
459 | if ( negate ) { |
---|
460 | mpz_init_set_si( &dummy, cc ); |
---|
461 | mpz_sub( &dummy, &dummy, &thempi ); |
---|
462 | } |
---|
463 | else { |
---|
464 | mpz_init( &dummy ); |
---|
465 | if ( cc < 0 ) |
---|
466 | mpz_add_ui( &dummy, &thempi, -cc ); |
---|
467 | else |
---|
468 | mpz_sub_ui( &dummy, &thempi, cc ); |
---|
469 | } |
---|
470 | if ( mpz_is_imm( &dummy ) ) { |
---|
471 | InternalCF * res = int2imm( mpz_get_si( &dummy ) ); |
---|
472 | mpz_clear( &dummy ); |
---|
473 | return res; |
---|
474 | } |
---|
475 | else |
---|
476 | return new InternalInteger( dummy ); |
---|
477 | } |
---|
478 | else { |
---|
479 | if ( negate ) { |
---|
480 | MP_INT dummy; |
---|
481 | mpz_init_set_si( &dummy, cc ); |
---|
482 | mpz_sub( &thempi, &dummy, &thempi ); |
---|
483 | mpz_clear( &dummy ); |
---|
484 | } |
---|
485 | else |
---|
486 | if ( cc < 0 ) |
---|
487 | mpz_add_ui( &thempi, &thempi, -cc ); |
---|
488 | else |
---|
489 | mpz_sub_ui( &thempi, &thempi, cc ); |
---|
490 | if ( mpz_is_imm( &thempi ) ) { |
---|
491 | InternalCF * res = int2imm( mpz_get_si( &thempi ) ); |
---|
492 | delete this; |
---|
493 | return res; |
---|
494 | } |
---|
495 | else |
---|
496 | return this; |
---|
497 | } |
---|
498 | } |
---|
499 | |
---|
500 | InternalCF* InternalInteger::mulcoeff( InternalCF* c ) |
---|
501 | { |
---|
502 | ASSERT( ::is_imm( c ) == INTMARK, "incompatible base coefficients" ); |
---|
503 | int cc = imm2int( c ); |
---|
504 | if ( getRefCount() > 1 ) { |
---|
505 | decRefCount(); |
---|
506 | MP_INT dummy; |
---|
507 | mpz_init( &dummy ); |
---|
508 | if ( cc < 0 ) { |
---|
509 | mpz_mul_ui( &dummy, &thempi, -cc ); |
---|
510 | mpz_neg( &dummy, &dummy ); |
---|
511 | } |
---|
512 | else |
---|
513 | mpz_mul_ui( &dummy, &thempi, cc ); |
---|
514 | if ( mpz_is_imm( &dummy ) ) { |
---|
515 | InternalCF * res = int2imm( mpz_get_si( &dummy ) ); |
---|
516 | mpz_clear( &dummy ); |
---|
517 | return res; |
---|
518 | } |
---|
519 | else |
---|
520 | return new InternalInteger( dummy ); |
---|
521 | } |
---|
522 | else { |
---|
523 | MP_INT dummy; |
---|
524 | mpz_init_set_si( &dummy, cc ); |
---|
525 | if ( cc < 0 ) { |
---|
526 | mpz_mul_ui( &thempi, &thempi, -cc ); |
---|
527 | mpz_neg( &thempi, &thempi ); |
---|
528 | } |
---|
529 | else |
---|
530 | mpz_mul_ui( &thempi, &thempi, cc ); |
---|
531 | if ( mpz_is_imm( &thempi ) ) { |
---|
532 | InternalCF * res = int2imm( mpz_get_si( &thempi ) ); |
---|
533 | delete this; |
---|
534 | return res; |
---|
535 | } |
---|
536 | else |
---|
537 | return this; |
---|
538 | } |
---|
539 | } |
---|
540 | |
---|
541 | InternalCF* InternalInteger::dividecoeff( InternalCF* c, bool invert ) |
---|
542 | { |
---|
543 | return divcoeff( c, invert ); |
---|
544 | } |
---|
545 | |
---|
546 | InternalCF* InternalInteger::divcoeff( InternalCF* c, bool invert ) |
---|
547 | { |
---|
548 | ASSERT( invert || ! ::is_imm( c ) || imm2int( c ) != 0, "divide by zero" ); |
---|
549 | int cc = imm2int( c ); |
---|
550 | if ( cf_glob_switches.isOn( SW_RATIONAL ) ) { |
---|
551 | MP_INT n, d; |
---|
552 | if ( invert ) { |
---|
553 | mpz_init_set_si( &n, cc ); |
---|
554 | mpz_init_set( &d, &thempi ); |
---|
555 | } |
---|
556 | else { |
---|
557 | mpz_init_set_si( &d, cc ); |
---|
558 | mpz_init_set( &n, &thempi ); |
---|
559 | } |
---|
560 | if ( deleteObject() ) delete this; |
---|
561 | InternalRational * result = new InternalRational( n, d ); |
---|
562 | return result->normalize_myself(); |
---|
563 | } |
---|
564 | ASSERT( ::is_imm( c ) == INTMARK, "incompatible base coefficients" ); |
---|
565 | MP_INT quot; |
---|
566 | MP_INT rem; |
---|
567 | int signmpi = mpz_cmp_si( &thempi, 0 ); |
---|
568 | int signc = cc; |
---|
569 | mpz_init( " ); mpz_init( &rem ); |
---|
570 | if ( signmpi < 0 ) |
---|
571 | mpz_neg( &thempi, &thempi ); |
---|
572 | if ( signc < 0 ) |
---|
573 | cc = -cc; |
---|
574 | if ( invert ) { |
---|
575 | MP_INT ccc; |
---|
576 | mpz_init_set_si( &ccc, cc ); |
---|
577 | mpz_divmod( ", &rem, &ccc, &thempi ); |
---|
578 | mpz_clear( &ccc ); |
---|
579 | if ( signmpi < 0 ) |
---|
580 | mpz_neg( &thempi, &thempi ); |
---|
581 | if ( signmpi < 0 && signc < 0 ) { |
---|
582 | if ( mpz_cmp_si( &rem, 0 ) != 0 ) |
---|
583 | mpz_add_ui( ", ", 1 ); |
---|
584 | } |
---|
585 | else if ( signmpi < 0 ) |
---|
586 | mpz_neg( ", " ); |
---|
587 | else if ( signc < 0 ) { |
---|
588 | mpz_neg( ", " ); |
---|
589 | if ( mpz_cmp_si( &rem, 0 ) != 0 ) |
---|
590 | mpz_sub_ui( ", ", 1 ); |
---|
591 | } |
---|
592 | } |
---|
593 | else { |
---|
594 | mpz_divmod_ui( ", &rem, &thempi, cc ); |
---|
595 | if ( signmpi < 0 ) |
---|
596 | mpz_neg( &thempi, &thempi ); |
---|
597 | if ( signmpi < 0 && signc < 0 ) { |
---|
598 | if ( mpz_cmp_si( &rem, 0 ) != 0 ) |
---|
599 | mpz_add_ui( ", ", 1 ); |
---|
600 | } |
---|
601 | else if ( signc < 0 ) |
---|
602 | mpz_neg( ", " ); |
---|
603 | else if ( signmpi < 0 ) { |
---|
604 | mpz_neg( ", " ); |
---|
605 | if ( mpz_cmp_si( &rem, 0 ) != 0 ) |
---|
606 | mpz_sub_ui( ", ", 1 ); |
---|
607 | } |
---|
608 | } |
---|
609 | mpz_clear( &rem ); |
---|
610 | if ( deleteObject() ) delete this; |
---|
611 | if ( mpz_is_imm( " ) ) { |
---|
612 | InternalCF * res = int2imm( mpz_get_si( " ) ); |
---|
613 | mpz_clear( " ); |
---|
614 | return res; |
---|
615 | } |
---|
616 | else |
---|
617 | return new InternalInteger( quot ); |
---|
618 | } |
---|
619 | |
---|
620 | InternalCF* InternalInteger::modulocoeff( InternalCF * c, bool invert ) |
---|
621 | { |
---|
622 | return modcoeff( c, invert ); |
---|
623 | } |
---|
624 | |
---|
625 | InternalCF* InternalInteger::modcoeff( InternalCF* c, bool invert ) |
---|
626 | { |
---|
627 | ASSERT( ::is_imm( c ) == INTMARK, "incompatible base coefficients" ); |
---|
628 | if ( cf_glob_switches.isOn( SW_RATIONAL ) ) { |
---|
629 | // in rational mode remainder is zero |
---|
630 | if ( deleteObject() ) delete this; |
---|
631 | return CFFactory::basic( 0 ); |
---|
632 | } |
---|
633 | int cc = imm2int( c ); |
---|
634 | MP_INT rem; |
---|
635 | int signmpi = mpz_cmp_si( &thempi, 0 ); |
---|
636 | int signc = cc; |
---|
637 | mpz_init( &rem ); |
---|
638 | if ( signmpi < 0 ) |
---|
639 | mpz_neg( &thempi, &thempi ); |
---|
640 | if ( signc < 0 ) |
---|
641 | cc = -cc; |
---|
642 | if ( invert ) { |
---|
643 | MP_INT ccc; |
---|
644 | mpz_init_set_si( &ccc, cc ); |
---|
645 | mpz_mod( &rem, &ccc, &thempi ); |
---|
646 | mpz_clear( &ccc ); |
---|
647 | if ( signc < 0 && mpz_cmp_si( &rem, 0 ) != 0 ) { |
---|
648 | mpz_sub( &rem, &thempi, &rem ); |
---|
649 | } |
---|
650 | if ( signmpi < 0 ) |
---|
651 | mpz_neg( &thempi, &thempi ); |
---|
652 | } |
---|
653 | else { |
---|
654 | mpz_mod_ui( &rem, &thempi, cc ); |
---|
655 | if ( signmpi < 0 && mpz_cmp_si( &rem, 0 ) != 0 ) { |
---|
656 | mpz_neg( &rem, &rem ); |
---|
657 | mpz_add_ui( &rem, &rem, cc ); |
---|
658 | } |
---|
659 | if ( signmpi < 0 ) |
---|
660 | mpz_neg( &thempi, &thempi ); |
---|
661 | } |
---|
662 | if ( deleteObject() ) delete this; |
---|
663 | if ( mpz_is_imm( &rem ) ) { |
---|
664 | InternalCF * res = int2imm( mpz_get_si( &rem ) ); |
---|
665 | mpz_clear( &rem ); |
---|
666 | return res; |
---|
667 | } |
---|
668 | else |
---|
669 | return new InternalInteger( rem ); |
---|
670 | } |
---|
671 | |
---|
672 | void InternalInteger::divremcoeff( InternalCF* c, InternalCF*& quot, InternalCF*& rem, bool invert ) |
---|
673 | { |
---|
674 | ASSERT( ::is_imm( c ) == INTMARK, "incompatible base coefficients" ); |
---|
675 | |
---|
676 | if ( cf_glob_switches.isOn( SW_RATIONAL ) ) { |
---|
677 | quot = copyObject(); |
---|
678 | quot = quot->dividecoeff( c, invert ); |
---|
679 | rem = CFFactory::basic( 0 ); |
---|
680 | return; |
---|
681 | } |
---|
682 | quot = copyObject(); |
---|
683 | quot = quot->divcoeff( c, invert ); |
---|
684 | rem = copyObject(); |
---|
685 | rem = rem->modcoeff( c, invert ); |
---|
686 | return; |
---|
687 | int cc = imm2int( c ); |
---|
688 | MP_INT q, r; |
---|
689 | int signmpi = mpz_cmp_si( &thempi, 0 ); |
---|
690 | int signc = cc; |
---|
691 | |
---|
692 | mpz_init( &q ); mpz_init( &r ); |
---|
693 | if ( signmpi < 0 ) |
---|
694 | mpz_neg( &thempi, &thempi ); |
---|
695 | if ( signc < 0 ) |
---|
696 | cc = -cc; |
---|
697 | if ( invert ) { |
---|
698 | MP_INT ccc; |
---|
699 | mpz_init_set_si( &ccc, cc ); |
---|
700 | mpz_divmod( &q, &r, &ccc, &thempi ); |
---|
701 | mpz_clear( &ccc ); |
---|
702 | if ( signc < 0 && signmpi < 0 ) { |
---|
703 | if ( mpz_cmp_si( &r, 0 ) != 0 ) { |
---|
704 | mpz_add_ui( &q, &q, 1 ); |
---|
705 | mpz_sub( &r, &thempi, &r ); |
---|
706 | } |
---|
707 | } |
---|
708 | else if ( signc < 0 ) { |
---|
709 | if ( mpz_cmp_si( &r, 0 ) != 0 ) { |
---|
710 | mpz_add_ui( &q, &q, 1 ); |
---|
711 | mpz_neg( &q, &q ); |
---|
712 | mpz_sub( &r, &thempi, &r ); |
---|
713 | } |
---|
714 | } |
---|
715 | else if ( signmpi < 0 ) |
---|
716 | mpz_neg( &q, &q ); |
---|
717 | } |
---|
718 | else { |
---|
719 | mpz_divmod_ui( &q, &r, &thempi, cc ); |
---|
720 | if ( signmpi < 0 && signc < 0 ) { |
---|
721 | if ( mpz_cmp_si( &r, 0 ) != 0 ) { |
---|
722 | mpz_add_ui( &q, &q, 1 ); |
---|
723 | mpz_neg( &r, &r ); |
---|
724 | mpz_add_ui( &r, &r, cc ); |
---|
725 | } |
---|
726 | } |
---|
727 | else if ( signmpi < 0 ) { |
---|
728 | if ( mpz_cmp_si( &r, 0 ) != 0 ) { |
---|
729 | mpz_add_ui( &q, &q, 1 ); |
---|
730 | mpz_neg( &q, &q ); |
---|
731 | mpz_neg( &r, &r ); |
---|
732 | mpz_add_ui( &r, &r, cc ); |
---|
733 | } |
---|
734 | } |
---|
735 | else if ( signc < 0 ) |
---|
736 | mpz_neg( &q, &q ); |
---|
737 | } |
---|
738 | if ( signmpi < 0 ) |
---|
739 | mpz_neg( &thempi, &thempi ); |
---|
740 | if ( mpz_is_imm( &r ) ) { |
---|
741 | rem = int2imm( mpz_get_si( &r ) ); |
---|
742 | mpz_clear( &r ); |
---|
743 | } |
---|
744 | else |
---|
745 | rem = new InternalInteger( r ); |
---|
746 | if ( mpz_is_imm( &q ) ) { |
---|
747 | quot = int2imm( mpz_get_si( &q ) ); |
---|
748 | mpz_clear( &q ); |
---|
749 | } |
---|
750 | else |
---|
751 | quot = new InternalInteger( q ); |
---|
752 | } |
---|
753 | |
---|
754 | bool InternalInteger::divremcoefft( InternalCF* c, InternalCF*& quot, InternalCF*& rem, bool invert ) |
---|
755 | { |
---|
756 | ASSERT( ::is_imm( c ) == INTMARK, "incompatible base coefficients" ); |
---|
757 | divremcoeff( c, quot, rem, invert ); |
---|
758 | return true; |
---|
759 | } |
---|
760 | |
---|
761 | int InternalInteger::intval() const |
---|
762 | { |
---|
763 | return (int)mpz_get_si( &thempi ); |
---|
764 | } |
---|
765 | |
---|
766 | int InternalInteger::intmod( int p ) const |
---|
767 | { |
---|
768 | return (int)mpz_mmod_ui( 0, &thempi, (unsigned long)p ); |
---|
769 | } |
---|
770 | |
---|
771 | int InternalInteger::sign ( ) const |
---|
772 | { |
---|
773 | return mpz_cmp_si( &thempi, 0 ); |
---|
774 | } |
---|
775 | |
---|
776 | InternalCF* |
---|
777 | InternalInteger::sqrt() |
---|
778 | { |
---|
779 | ASSERT( mpz_cmp_si( &thempi, 0 ) >= 0, "illegal instruction" ); |
---|
780 | MP_INT result; |
---|
781 | mpz_init( &result ); |
---|
782 | mpz_sqrt( &result, &thempi ); |
---|
783 | if ( mpz_is_imm( &result ) ) { |
---|
784 | InternalCF * res = int2imm( mpz_get_si( &result ) ); |
---|
785 | mpz_clear( &result ); |
---|
786 | return res; |
---|
787 | } |
---|
788 | else |
---|
789 | return new InternalInteger( result ); |
---|
790 | } |
---|