Changeset fc732a9 in git for factory/int_int.cc
- Timestamp:
- Mar 17, 1998, 4:59:42 PM (26 years ago)
- Branches:
- (u'spielwiese', 'fe61d9c35bf7c61f2b6cbf1b56e25e2f08d536cc')
- Children:
- e0fb8d31ff69374db7f865baedfda3e614804382
- Parents:
- bb17e813fdec9082fa78763093e18795ea0fdbb4
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
factory/int_int.cc
rbb17e8 rfc732a9 1 1 /* emacs edit mode for this file is -*- C++ -*- */ 2 /* $Id: int_int.cc,v 1.1 1 1998-01-22 10:54:15schmidt Exp $ */2 /* $Id: int_int.cc,v 1.12 1998-03-17 15:56:04 schmidt Exp $ */ 3 3 4 4 #include <config.h> 5 5 6 #include "assert.h" 7 8 #include "cf_defs.h" 9 #include "cf_globals.h" 6 #include "imm.h" 10 7 #include "int_int.h" 11 8 #include "int_rat.h" 12 #include "imm.h" 9 #include "cf_gmp.h" 10 #include "gmpext.h" 13 11 #include "canonicalform.h" 14 #include "gmpext.h" 15 12 16 13 InternalInteger::InternalInteger() 17 14 { … … 185 182 return this; 186 183 } 187 }188 189 InternalCF* InternalInteger::dividesame( InternalCF * c )190 {191 return divsame( c );192 }193 194 InternalCF* InternalInteger::divsame( InternalCF * c )195 {196 if ( c == this ) {197 if ( deleteObject() ) delete this;198 return CFFactory::basic( 1 );199 }200 if ( cf_glob_switches.isOn( SW_RATIONAL ) ) {201 MP_INT n, d;202 mpz_init_set( &n, &thempi );203 mpz_init_set( &d, &MPI( c ) );204 if ( deleteObject() ) delete this;205 InternalRational * result = new InternalRational( n, d );206 return result->normalize_myself();207 }208 if ( getRefCount() > 1 ) {209 decRefCount();210 MP_INT quot;211 MP_INT rem;212 int signmpi = mpz_cmp_si( &thempi, 0 );213 int signc = mpz_cmp_si( &MPI( c ), 0 );214 if ( signmpi < 0 )215 mpz_neg( &thempi, &thempi );216 if ( signc < 0 )217 mpz_neg( &MPI( c ), &MPI( c ) );218 mpz_init( " ); mpz_init( &rem );219 mpz_divmod( ", &rem, &thempi, &MPI( c ) );220 if ( signmpi < 0 )221 mpz_neg( &thempi, &thempi );222 if ( signc < 0 )223 mpz_neg( &MPI( c ), &MPI( c ) );224 if ( signmpi < 0 && signc < 0 ) {225 if ( mpz_cmp_si( &rem, 0 ) != 0 )226 mpz_add_ui( ", ", 1 );227 }228 else if ( signc < 0 )229 mpz_neg( ", " );230 else if ( signmpi < 0 ) {231 mpz_neg( ", " );232 if ( mpz_cmp_si( &rem, 0 ) != 0 )233 mpz_sub_ui( ", ", 1 );234 }235 mpz_clear( &rem );236 if ( mpz_is_imm( " ) ) {237 InternalCF * res = int2imm( mpz_get_si( " ) );238 mpz_clear( " );239 return res;240 }241 else242 return new InternalInteger( quot );243 }244 else {245 MP_INT rem;246 mpz_init( &rem );247 int signmpi = mpz_cmp_si( &thempi, 0 );248 int signc = mpz_cmp_si( &MPI( c ), 0 );249 if ( signmpi < 0 )250 mpz_neg( &thempi, &thempi );251 if ( signc < 0 )252 mpz_neg( &MPI( c ), &MPI( c ) );253 mpz_divmod( &thempi, &rem, &thempi, &MPI( c ) );254 if ( signc < 0 )255 mpz_neg( &MPI( c ), &MPI( c ) );256 if ( signmpi < 0 && signc < 0 ) {257 if ( mpz_cmp_si( &rem, 0 ) != 0 )258 mpz_add_ui( &thempi, &thempi, 1 );259 }260 else if ( signc < 0 )261 mpz_neg( &thempi, &thempi );262 else if ( signmpi < 0 ) {263 mpz_neg( &thempi, &thempi );264 if ( mpz_cmp_si( &rem, 0 ) != 0 )265 mpz_sub_ui( &thempi, &thempi, 1 );266 }267 mpz_clear( &rem );268 if ( mpz_is_imm( &thempi ) ) {269 InternalCF * res = int2imm( mpz_get_si( &thempi ) );270 delete this;271 return res;272 }273 else274 return this;275 }276 }277 278 InternalCF* InternalInteger::modulosame ( InternalCF * c )279 {280 return modsame( c );281 }282 283 InternalCF* InternalInteger::modsame( InternalCF * c )284 {285 if ( cf_glob_switches.isOn( SW_RATIONAL ) || c == this ) {286 if ( deleteObject() ) delete this;287 return CFFactory::basic( 0 );288 }289 if ( getRefCount() > 1 ) {290 decRefCount();291 MP_INT dummy;292 mpz_init( &dummy );293 int signmpi = mpz_cmp_si( &thempi, 0 );294 int signc = mpz_cmp_si( &MPI( c ), 0 );295 if ( signmpi < 0 )296 mpz_neg( &thempi, &thempi );297 if ( signc < 0 )298 mpz_neg( &MPI( c ), &MPI( c ) );299 mpz_mod( &dummy, &thempi, &MPI( c ) );300 if ( signmpi < 0 && mpz_cmp_si( &dummy, 0 ) != 0 ) {301 mpz_sub( &dummy, &MPI( c ), &dummy );302 }303 if ( signmpi < 0 )304 mpz_neg( &thempi, &thempi );305 if ( signc < 0 )306 mpz_neg( &MPI( c ), &MPI( c ) );307 if ( mpz_is_imm( &dummy ) ) {308 InternalCF * res = int2imm( mpz_get_si( &dummy ) );309 mpz_clear( &dummy );310 return res;311 }312 else313 return new InternalInteger( dummy );314 }315 else {316 int signmpi = mpz_cmp_si( &thempi, 0 );317 int signc = mpz_cmp_si( &MPI( c ), 0 );318 if ( signmpi < 0 )319 mpz_neg( &thempi, &thempi );320 if ( signc < 0 )321 mpz_neg( &MPI( c ), &MPI( c ) );322 mpz_mod( &thempi, &thempi, &MPI( c ) );323 if ( signmpi < 0 && mpz_cmp_si( &thempi, 0 ) != 0 ) {324 mpz_sub( &thempi, &MPI( c ), &thempi );325 }326 if ( signc < 0 )327 mpz_neg( &MPI( c ), &MPI( c ) );328 if ( mpz_is_imm( &thempi ) ) {329 InternalCF * res = int2imm( mpz_get_si( &thempi ) );330 delete this;331 return res;332 }333 else334 return this;335 }336 }337 338 void InternalInteger::divremsame( InternalCF * c, InternalCF*& quot, InternalCF*& rem )339 {340 if ( cf_glob_switches.isOn( SW_RATIONAL ) ) {341 quot = copyObject();342 quot = quot->dividesame( c );343 rem = CFFactory::basic( 0 );344 }345 else if ( c == this ) {346 quot = CFFactory::basic( 1 );347 rem = CFFactory::basic( 0 );348 }349 else {350 MP_INT q;351 MP_INT r;352 mpz_init( &q ); mpz_init( &r );353 int signmpi = mpz_cmp_si( &thempi, 0 );354 int signc = mpz_cmp_si( &MPI( c ), 0 );355 if ( signmpi < 0 )356 mpz_neg( &thempi, &thempi );357 if ( signc < 0 )358 mpz_neg( &MPI( c ), &MPI( c ) );359 mpz_divmod( &q, &r, &thempi, &MPI( c ) );360 if ( signmpi < 0 && mpz_cmp_si( &r, 0 ) != 0 ) {361 mpz_sub( &r, &MPI( c ), &r );362 }363 if ( signmpi < 0 )364 mpz_neg( &thempi, &thempi );365 if ( signc < 0 )366 mpz_neg( &MPI( c ), &MPI( c ) );367 if ( signmpi < 0 && signc < 0 ) {368 if ( mpz_cmp_si( &r, 0 ) != 0 )369 mpz_add_ui( &q, &q, 1 );370 }371 else if ( signc < 0 )372 mpz_neg( &q, &q );373 else if ( signmpi < 0 ) {374 mpz_neg( &q, &q );375 if ( mpz_cmp_si( &r, 0 ) != 0 )376 mpz_sub_ui( &q, &q, 1 );377 }378 if ( mpz_is_imm( &q ) )379 quot = int2imm( mpz_get_si( &q ) );380 else381 quot = new InternalInteger( q );382 if ( mpz_is_imm( &r ) )383 rem = int2imm( mpz_get_si( &r ) );384 else385 rem = new InternalInteger( r );386 }387 }388 389 bool InternalInteger::divremsamet( InternalCF* c, InternalCF*& quot, InternalCF*& rem )390 {391 divremsame( c, quot, rem );392 return true;393 184 } 394 185 … … 532 323 return this; 533 324 } 534 }535 536 InternalCF* InternalInteger::dividecoeff( InternalCF* c, bool invert )537 {538 return divcoeff( c, invert );539 }540 541 InternalCF* InternalInteger::divcoeff( InternalCF* c, bool invert )542 {543 ASSERT( invert || ! ::is_imm( c ) || imm2int( c ) != 0, "divide by zero" );544 int cc = imm2int( c );545 if ( cf_glob_switches.isOn( SW_RATIONAL ) ) {546 MP_INT n, d;547 if ( invert ) {548 mpz_init_set_si( &n, cc );549 mpz_init_set( &d, &thempi );550 }551 else {552 mpz_init_set_si( &d, cc );553 mpz_init_set( &n, &thempi );554 }555 if ( deleteObject() ) delete this;556 InternalRational * result = new InternalRational( n, d );557 return result->normalize_myself();558 }559 ASSERT( ::is_imm( c ) == INTMARK, "incompatible base coefficients" );560 MP_INT quot;561 MP_INT rem;562 int signmpi = mpz_cmp_si( &thempi, 0 );563 int signc = cc;564 mpz_init( " ); mpz_init( &rem );565 if ( signmpi < 0 )566 mpz_neg( &thempi, &thempi );567 if ( signc < 0 )568 cc = -cc;569 if ( invert ) {570 MP_INT ccc;571 mpz_init_set_si( &ccc, cc );572 mpz_divmod( ", &rem, &ccc, &thempi );573 mpz_clear( &ccc );574 if ( signmpi < 0 )575 mpz_neg( &thempi, &thempi );576 if ( signmpi < 0 && signc < 0 ) {577 if ( mpz_cmp_si( &rem, 0 ) != 0 )578 mpz_add_ui( ", ", 1 );579 }580 else if ( signmpi < 0 )581 mpz_neg( ", " );582 else if ( signc < 0 ) {583 mpz_neg( ", " );584 if ( mpz_cmp_si( &rem, 0 ) != 0 )585 mpz_sub_ui( ", ", 1 );586 }587 }588 else {589 mpz_divmod_ui( ", &rem, &thempi, cc );590 if ( signmpi < 0 )591 mpz_neg( &thempi, &thempi );592 if ( signmpi < 0 && signc < 0 ) {593 if ( mpz_cmp_si( &rem, 0 ) != 0 )594 mpz_add_ui( ", ", 1 );595 }596 else if ( signc < 0 )597 mpz_neg( ", " );598 else if ( signmpi < 0 ) {599 mpz_neg( ", " );600 if ( mpz_cmp_si( &rem, 0 ) != 0 )601 mpz_sub_ui( ", ", 1 );602 }603 }604 mpz_clear( &rem );605 if ( deleteObject() ) delete this;606 if ( mpz_is_imm( " ) ) {607 InternalCF * res = int2imm( mpz_get_si( " ) );608 mpz_clear( " );609 return res;610 }611 else612 return new InternalInteger( quot );613 }614 615 InternalCF* InternalInteger::modulocoeff( InternalCF * c, bool invert )616 {617 return modcoeff( c, invert );618 }619 620 InternalCF* InternalInteger::modcoeff( InternalCF* c, bool invert )621 {622 ASSERT( ::is_imm( c ) == INTMARK, "incompatible base coefficients" );623 if ( cf_glob_switches.isOn( SW_RATIONAL ) ) {624 // in rational mode remainder is zero625 if ( deleteObject() ) delete this;626 return CFFactory::basic( 0 );627 }628 int cc = imm2int( c );629 MP_INT rem;630 int signmpi = mpz_cmp_si( &thempi, 0 );631 int signc = cc;632 mpz_init( &rem );633 if ( signmpi < 0 )634 mpz_neg( &thempi, &thempi );635 if ( signc < 0 )636 cc = -cc;637 if ( invert ) {638 MP_INT ccc;639 mpz_init_set_si( &ccc, cc );640 mpz_mod( &rem, &ccc, &thempi );641 mpz_clear( &ccc );642 if ( signc < 0 && mpz_cmp_si( &rem, 0 ) != 0 ) {643 mpz_sub( &rem, &thempi, &rem );644 }645 if ( signmpi < 0 )646 mpz_neg( &thempi, &thempi );647 }648 else {649 mpz_mod_ui( &rem, &thempi, cc );650 if ( signmpi < 0 && mpz_cmp_si( &rem, 0 ) != 0 ) {651 mpz_neg( &rem, &rem );652 mpz_add_ui( &rem, &rem, cc );653 }654 if ( signmpi < 0 )655 mpz_neg( &thempi, &thempi );656 }657 if ( deleteObject() ) delete this;658 if ( mpz_is_imm( &rem ) ) {659 InternalCF * res = int2imm( mpz_get_si( &rem ) );660 mpz_clear( &rem );661 return res;662 }663 else664 return new InternalInteger( rem );665 }666 667 void InternalInteger::divremcoeff( InternalCF* c, InternalCF*& quot, InternalCF*& rem, bool invert )668 {669 ASSERT( ::is_imm( c ) == INTMARK, "incompatible base coefficients" );670 671 if ( cf_glob_switches.isOn( SW_RATIONAL ) ) {672 quot = copyObject();673 quot = quot->dividecoeff( c, invert );674 rem = CFFactory::basic( 0 );675 return;676 }677 quot = copyObject();678 quot = quot->divcoeff( c, invert );679 rem = copyObject();680 rem = rem->modcoeff( c, invert );681 return;682 int cc = imm2int( c );683 MP_INT q, r;684 int signmpi = mpz_cmp_si( &thempi, 0 );685 int signc = cc;686 687 mpz_init( &q ); mpz_init( &r );688 if ( signmpi < 0 )689 mpz_neg( &thempi, &thempi );690 if ( signc < 0 )691 cc = -cc;692 if ( invert ) {693 MP_INT ccc;694 mpz_init_set_si( &ccc, cc );695 mpz_divmod( &q, &r, &ccc, &thempi );696 mpz_clear( &ccc );697 if ( signc < 0 && signmpi < 0 ) {698 if ( mpz_cmp_si( &r, 0 ) != 0 ) {699 mpz_add_ui( &q, &q, 1 );700 mpz_sub( &r, &thempi, &r );701 }702 }703 else if ( signc < 0 ) {704 if ( mpz_cmp_si( &r, 0 ) != 0 ) {705 mpz_add_ui( &q, &q, 1 );706 mpz_neg( &q, &q );707 mpz_sub( &r, &thempi, &r );708 }709 }710 else if ( signmpi < 0 )711 mpz_neg( &q, &q );712 }713 else {714 mpz_divmod_ui( &q, &r, &thempi, cc );715 if ( signmpi < 0 && signc < 0 ) {716 if ( mpz_cmp_si( &r, 0 ) != 0 ) {717 mpz_add_ui( &q, &q, 1 );718 mpz_neg( &r, &r );719 mpz_add_ui( &r, &r, cc );720 }721 }722 else if ( signmpi < 0 ) {723 if ( mpz_cmp_si( &r, 0 ) != 0 ) {724 mpz_add_ui( &q, &q, 1 );725 mpz_neg( &q, &q );726 mpz_neg( &r, &r );727 mpz_add_ui( &r, &r, cc );728 }729 }730 else if ( signc < 0 )731 mpz_neg( &q, &q );732 }733 if ( signmpi < 0 )734 mpz_neg( &thempi, &thempi );735 if ( mpz_is_imm( &r ) ) {736 rem = int2imm( mpz_get_si( &r ) );737 mpz_clear( &r );738 }739 else740 rem = new InternalInteger( r );741 if ( mpz_is_imm( &q ) ) {742 quot = int2imm( mpz_get_si( &q ) );743 mpz_clear( &q );744 }745 else746 quot = new InternalInteger( q );747 }748 749 bool InternalInteger::divremcoefft( InternalCF* c, InternalCF*& quot, InternalCF*& rem, bool invert )750 {751 ASSERT( ::is_imm( c ) == INTMARK, "incompatible base coefficients" );752 divremcoeff( c, quot, rem, invert );753 return true;754 325 } 755 326
Note: See TracChangeset
for help on using the changeset viewer.