source: git/factory/int_int.cc @ 2dd068

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