[493c477] | 1 | /* emacs edit mode for this file is -*- C++ -*- */ |
---|
[181148] | 2 | /* $Id: int_int.cc,v 1.18 2006-05-15 09:03:06 Singular Exp $ */ |
---|
[2dd068] | 3 | |
---|
[86aed6] | 4 | #include <config.h> |
---|
| 5 | |
---|
[a472b9] | 6 | #include "canonicalform.h" |
---|
[fc732a9] | 7 | #include "imm.h" |
---|
[2dd068] | 8 | #include "int_int.h" |
---|
| 9 | #include "int_rat.h" |
---|
[fc732a9] | 10 | #include "cf_gmp.h" |
---|
[2dd068] | 11 | #include "gmpext.h" |
---|
[54a019] | 12 | |
---|
[f79b94c] | 13 | #ifdef HAVE_OMALLOC |
---|
| 14 | const omBin InternalInteger::InternalInteger_bin = omGetSpecBin(sizeof(InternalInteger)); |
---|
| 15 | #endif |
---|
| 16 | |
---|
[2dd068] | 17 | InternalInteger::InternalInteger() |
---|
| 18 | { |
---|
| 19 | mpz_init( &thempi ); |
---|
| 20 | } |
---|
| 21 | |
---|
| 22 | InternalInteger::InternalInteger( const int i ) |
---|
| 23 | { |
---|
| 24 | mpz_init_set_si( &thempi, i ); |
---|
| 25 | } |
---|
| 26 | |
---|
| 27 | InternalInteger::InternalInteger( const MP_INT & mpi) : thempi( mpi ) {} |
---|
| 28 | |
---|
[d07137] | 29 | InternalInteger::InternalInteger( const char * str, const int base ) |
---|
[2dd068] | 30 | { |
---|
[d07137] | 31 | mpz_init_set_str( &thempi, str, base ); |
---|
[2dd068] | 32 | } |
---|
| 33 | |
---|
| 34 | InternalInteger::~InternalInteger() |
---|
| 35 | { |
---|
| 36 | mpz_clear( &thempi ); |
---|
| 37 | } |
---|
| 38 | |
---|
| 39 | InternalCF* InternalInteger::deepCopyObject() const |
---|
| 40 | { |
---|
| 41 | MP_INT dummy; |
---|
| 42 | mpz_init_set( &dummy, &thempi ); |
---|
| 43 | return new InternalInteger( dummy ); |
---|
| 44 | } |
---|
| 45 | |
---|
[718e670] | 46 | #ifndef NOSTREAMIO |
---|
[181148] | 47 | void InternalInteger::print( OSTREAM & os, char * c ) |
---|
[2dd068] | 48 | { |
---|
| 49 | if ( *c == '*' && mpz_cmp_si( &thempi, 1 ) == 0 ) |
---|
[54a019] | 50 | os << c+1; |
---|
[2dd068] | 51 | else if ( *c == '*' && mpz_cmp_si( &thempi, -1 ) == 0 ) |
---|
[54a019] | 52 | os << '-' << c+1; |
---|
[2dd068] | 53 | else { |
---|
[54a019] | 54 | char * str = new char[mpz_sizeinbase( &thempi, 10 ) + 2]; |
---|
| 55 | str = mpz_get_str( str, 10, &thempi ); |
---|
| 56 | os << str << c; |
---|
| 57 | delete [] str; |
---|
[2dd068] | 58 | } |
---|
| 59 | } |
---|
[718e670] | 60 | #endif /* NOSTREAMIO */ |
---|
[2dd068] | 61 | |
---|
| 62 | bool InternalInteger::is_imm() const |
---|
| 63 | { |
---|
| 64 | return mpz_is_imm( &thempi ); |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | InternalCF* InternalInteger::genZero() |
---|
| 68 | { |
---|
| 69 | if ( isZero() ) |
---|
[54a019] | 70 | return copyObject(); |
---|
[2dd068] | 71 | else |
---|
[54a019] | 72 | return new InternalInteger(); |
---|
[2dd068] | 73 | } |
---|
| 74 | |
---|
| 75 | InternalCF* InternalInteger::genOne() |
---|
| 76 | { |
---|
| 77 | if ( isOne() ) |
---|
[54a019] | 78 | return copyObject(); |
---|
[2dd068] | 79 | else |
---|
[54a019] | 80 | return new InternalInteger( 1 ); |
---|
[2dd068] | 81 | } |
---|
| 82 | |
---|
[f1295f] | 83 | //{{{ InternalCF * InternalInteger::neg () |
---|
| 84 | // docu: see CanonicalForm::operator -() |
---|
| 85 | InternalCF * |
---|
| 86 | InternalInteger::neg () |
---|
[2dd068] | 87 | { |
---|
| 88 | if ( getRefCount() > 1 ) { |
---|
[54a019] | 89 | decRefCount(); |
---|
| 90 | MP_INT dummy; |
---|
| 91 | mpz_init_set( &dummy, &thempi ); |
---|
| 92 | mpz_neg( &dummy, &dummy ); |
---|
| 93 | return new InternalInteger( dummy ); |
---|
[f1295f] | 94 | } else { |
---|
[54a019] | 95 | mpz_neg( &thempi, &thempi ); |
---|
| 96 | return this; |
---|
[2dd068] | 97 | } |
---|
| 98 | } |
---|
[f1295f] | 99 | //}}} |
---|
[2dd068] | 100 | |
---|
| 101 | |
---|
| 102 | InternalCF* InternalInteger::addsame( InternalCF * c ) |
---|
| 103 | { |
---|
| 104 | if ( getRefCount() > 1 ) { |
---|
[54a019] | 105 | decRefCount(); |
---|
| 106 | MP_INT dummy; |
---|
| 107 | mpz_init( &dummy ); |
---|
| 108 | mpz_add( &dummy, &thempi, &MPI( c ) ); |
---|
| 109 | if ( mpz_is_imm( &dummy ) ) { |
---|
| 110 | InternalCF * res = int2imm( mpz_get_si( &dummy ) ); |
---|
| 111 | mpz_clear( &dummy ); |
---|
| 112 | return res; |
---|
| 113 | } |
---|
| 114 | else |
---|
| 115 | return new InternalInteger( dummy ); |
---|
[2dd068] | 116 | } |
---|
| 117 | else { |
---|
[54a019] | 118 | mpz_add( &thempi, &thempi, &MPI( c ) ); |
---|
| 119 | if ( mpz_is_imm( &thempi ) ) { |
---|
| 120 | InternalCF * res = int2imm( mpz_get_si( &thempi ) ); |
---|
| 121 | delete this; |
---|
| 122 | return res; |
---|
| 123 | } |
---|
| 124 | else |
---|
| 125 | return this; |
---|
[2dd068] | 126 | } |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | InternalCF* InternalInteger::subsame( InternalCF * c ) |
---|
| 130 | { |
---|
| 131 | if ( getRefCount() > 1 ) { |
---|
[54a019] | 132 | decRefCount(); |
---|
| 133 | MP_INT dummy; |
---|
| 134 | mpz_init( &dummy ); |
---|
| 135 | mpz_sub( &dummy, &thempi, &MPI( c ) ); |
---|
| 136 | if ( mpz_is_imm( &dummy ) ) { |
---|
| 137 | InternalCF * res = int2imm( mpz_get_si( &dummy ) ); |
---|
| 138 | mpz_clear( &dummy ); |
---|
| 139 | return res; |
---|
| 140 | } |
---|
| 141 | else |
---|
| 142 | return new InternalInteger( dummy ); |
---|
[2dd068] | 143 | } |
---|
| 144 | else { |
---|
[54a019] | 145 | mpz_sub( &thempi, &thempi, &MPI( c ) ); |
---|
| 146 | if ( mpz_is_imm( &thempi ) ) { |
---|
| 147 | InternalCF * res = int2imm( mpz_get_si( &thempi ) ); |
---|
| 148 | delete this; |
---|
| 149 | return res; |
---|
| 150 | } |
---|
| 151 | else |
---|
| 152 | return this; |
---|
[2dd068] | 153 | } |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | InternalCF* InternalInteger::mulsame( InternalCF * c ) |
---|
| 157 | { |
---|
| 158 | if ( getRefCount() > 1 ) { |
---|
[54a019] | 159 | decRefCount(); |
---|
| 160 | MP_INT dummy; |
---|
| 161 | mpz_init( &dummy ); |
---|
| 162 | mpz_mul( &dummy, &thempi, &MPI( c ) ); |
---|
| 163 | if ( mpz_is_imm( &dummy ) ) { |
---|
| 164 | InternalCF * res = int2imm( mpz_get_si( &dummy ) ); |
---|
| 165 | mpz_clear( &dummy ); |
---|
| 166 | return res; |
---|
| 167 | } |
---|
| 168 | else |
---|
| 169 | return new InternalInteger( dummy ); |
---|
[2dd068] | 170 | } |
---|
| 171 | else { |
---|
[54a019] | 172 | mpz_mul( &thempi, &thempi, &MPI( c ) ); |
---|
| 173 | if ( mpz_is_imm( &thempi ) ) { |
---|
| 174 | InternalCF * res = int2imm( mpz_get_si( &thempi ) ); |
---|
| 175 | delete this; |
---|
| 176 | return res; |
---|
| 177 | } |
---|
| 178 | else |
---|
| 179 | return this; |
---|
[2dd068] | 180 | } |
---|
| 181 | } |
---|
| 182 | |
---|
[398652] | 183 | //{{{ int InternalInteger::comparesame, comparecoeff ( InternalCF * c ) |
---|
| 184 | // docu: see CanonicalForm::operator <(), CanonicalForm::operator ==() |
---|
| 185 | int |
---|
| 186 | InternalInteger::comparesame ( InternalCF * c ) |
---|
| 187 | { |
---|
| 188 | ASSERT( ! ::is_imm( c ) && c->levelcoeff() == IntegerDomain, "incompatible base coefficients" ); |
---|
| 189 | return mpz_cmp( &thempi, &MPI( c ) ); |
---|
| 190 | } |
---|
| 191 | |
---|
| 192 | int |
---|
| 193 | InternalInteger::comparecoeff ( InternalCF * c ) |
---|
[2dd068] | 194 | { |
---|
| 195 | ASSERT( ::is_imm( c ) == INTMARK, "incompatible base coefficients" ); |
---|
| 196 | return mpz_cmp_si( &thempi, imm2int( c ) ); |
---|
| 197 | } |
---|
[398652] | 198 | //}}} |
---|
[2dd068] | 199 | |
---|
| 200 | InternalCF* InternalInteger::addcoeff( InternalCF* c ) |
---|
| 201 | { |
---|
| 202 | ASSERT( ::is_imm( c ) == INTMARK, "incompatible base coefficients" ); |
---|
| 203 | int cc = imm2int( c ); |
---|
| 204 | if ( getRefCount() > 1 ) { |
---|
[54a019] | 205 | decRefCount(); |
---|
| 206 | MP_INT dummy; |
---|
| 207 | mpz_init( &dummy ); |
---|
| 208 | if ( cc < 0 ) |
---|
| 209 | mpz_sub_ui( &dummy, &thempi, -cc ); |
---|
| 210 | else |
---|
| 211 | mpz_add_ui( &dummy, &thempi, cc ); |
---|
| 212 | if ( mpz_is_imm( &dummy ) ) { |
---|
| 213 | InternalCF * res = int2imm( mpz_get_si( &dummy ) ); |
---|
| 214 | mpz_clear( &dummy ); |
---|
| 215 | return res; |
---|
| 216 | } |
---|
| 217 | else |
---|
| 218 | return new InternalInteger( dummy ); |
---|
[2dd068] | 219 | } |
---|
| 220 | else { |
---|
[54a019] | 221 | if ( cc < 0 ) |
---|
| 222 | mpz_sub_ui( &thempi, &thempi, -cc ); |
---|
| 223 | else |
---|
| 224 | mpz_add_ui( &thempi, &thempi, cc ); |
---|
| 225 | if ( mpz_is_imm( &thempi ) ) { |
---|
| 226 | InternalCF * res = int2imm( mpz_get_si( &thempi ) ); |
---|
| 227 | delete this; |
---|
| 228 | return res; |
---|
| 229 | } |
---|
| 230 | else |
---|
| 231 | return this; |
---|
[2dd068] | 232 | } |
---|
| 233 | } |
---|
| 234 | |
---|
| 235 | InternalCF* InternalInteger::subcoeff( InternalCF* c, bool negate ) |
---|
| 236 | { |
---|
| 237 | ASSERT( ::is_imm( c ) == INTMARK, "incompatible base coefficients" ); |
---|
| 238 | int cc = imm2int( c ); |
---|
| 239 | if ( getRefCount() > 1 ) { |
---|
[54a019] | 240 | decRefCount(); |
---|
| 241 | MP_INT dummy; |
---|
| 242 | if ( negate ) { |
---|
| 243 | mpz_init_set_si( &dummy, cc ); |
---|
| 244 | mpz_sub( &dummy, &dummy, &thempi ); |
---|
| 245 | } |
---|
| 246 | else { |
---|
| 247 | mpz_init( &dummy ); |
---|
| 248 | if ( cc < 0 ) |
---|
| 249 | mpz_add_ui( &dummy, &thempi, -cc ); |
---|
| 250 | else |
---|
| 251 | mpz_sub_ui( &dummy, &thempi, cc ); |
---|
| 252 | } |
---|
| 253 | if ( mpz_is_imm( &dummy ) ) { |
---|
| 254 | InternalCF * res = int2imm( mpz_get_si( &dummy ) ); |
---|
| 255 | mpz_clear( &dummy ); |
---|
| 256 | return res; |
---|
| 257 | } |
---|
| 258 | else |
---|
| 259 | return new InternalInteger( dummy ); |
---|
[2dd068] | 260 | } |
---|
| 261 | else { |
---|
[54a019] | 262 | if ( negate ) { |
---|
| 263 | MP_INT dummy; |
---|
| 264 | mpz_init_set_si( &dummy, cc ); |
---|
| 265 | mpz_sub( &thempi, &dummy, &thempi ); |
---|
| 266 | mpz_clear( &dummy ); |
---|
| 267 | } |
---|
| 268 | else |
---|
| 269 | if ( cc < 0 ) |
---|
| 270 | mpz_add_ui( &thempi, &thempi, -cc ); |
---|
| 271 | else |
---|
| 272 | mpz_sub_ui( &thempi, &thempi, cc ); |
---|
| 273 | if ( mpz_is_imm( &thempi ) ) { |
---|
| 274 | InternalCF * res = int2imm( mpz_get_si( &thempi ) ); |
---|
| 275 | delete this; |
---|
| 276 | return res; |
---|
| 277 | } |
---|
| 278 | else |
---|
| 279 | return this; |
---|
[2dd068] | 280 | } |
---|
| 281 | } |
---|
| 282 | |
---|
| 283 | InternalCF* InternalInteger::mulcoeff( InternalCF* c ) |
---|
| 284 | { |
---|
| 285 | ASSERT( ::is_imm( c ) == INTMARK, "incompatible base coefficients" ); |
---|
| 286 | int cc = imm2int( c ); |
---|
| 287 | if ( getRefCount() > 1 ) { |
---|
[54a019] | 288 | decRefCount(); |
---|
| 289 | MP_INT dummy; |
---|
| 290 | mpz_init( &dummy ); |
---|
| 291 | if ( cc < 0 ) { |
---|
| 292 | mpz_mul_ui( &dummy, &thempi, -cc ); |
---|
| 293 | mpz_neg( &dummy, &dummy ); |
---|
| 294 | } |
---|
| 295 | else |
---|
| 296 | mpz_mul_ui( &dummy, &thempi, cc ); |
---|
| 297 | if ( mpz_is_imm( &dummy ) ) { |
---|
| 298 | InternalCF * res = int2imm( mpz_get_si( &dummy ) ); |
---|
| 299 | mpz_clear( &dummy ); |
---|
| 300 | return res; |
---|
| 301 | } |
---|
| 302 | else |
---|
| 303 | return new InternalInteger( dummy ); |
---|
[2dd068] | 304 | } |
---|
| 305 | else { |
---|
[54a019] | 306 | if ( cc < 0 ) { |
---|
| 307 | mpz_mul_ui( &thempi, &thempi, -cc ); |
---|
| 308 | mpz_neg( &thempi, &thempi ); |
---|
| 309 | } |
---|
| 310 | else |
---|
| 311 | mpz_mul_ui( &thempi, &thempi, cc ); |
---|
| 312 | if ( mpz_is_imm( &thempi ) ) { |
---|
| 313 | InternalCF * res = int2imm( mpz_get_si( &thempi ) ); |
---|
| 314 | delete this; |
---|
| 315 | return res; |
---|
| 316 | } |
---|
| 317 | else |
---|
| 318 | return this; |
---|
[2dd068] | 319 | } |
---|
| 320 | } |
---|
| 321 | |
---|
[05d0b3] | 322 | //{{{ InternalCF * InternalInteger::bgcdsame, bgcdcoeff ( const InternalCF * const c ) |
---|
| 323 | // docu: see CanonicalForm::bgcd() |
---|
| 324 | InternalCF * |
---|
| 325 | InternalInteger::bgcdsame ( const InternalCF * const c ) const |
---|
| 326 | { |
---|
| 327 | ASSERT( ! ::is_imm( c ) && c->levelcoeff() == IntegerDomain, "incompatible base coefficients" ); |
---|
| 328 | |
---|
| 329 | // simply return 1 if we are calculating over the rationals |
---|
| 330 | if ( cf_glob_switches.isOn( SW_RATIONAL ) ) |
---|
[54a019] | 331 | return int2imm( 1 ); |
---|
[05d0b3] | 332 | |
---|
| 333 | // calculate gcd |
---|
| 334 | MP_INT result; |
---|
| 335 | mpz_init( &result ); |
---|
| 336 | mpz_gcd( &result, &thempi, &MPI( c ) ); |
---|
| 337 | mpz_abs( &result, &result ); |
---|
| 338 | |
---|
| 339 | // check for immediate result |
---|
| 340 | if ( mpz_is_imm( &result ) ) { |
---|
[54a019] | 341 | InternalCF * res = int2imm( mpz_get_si( &result ) ); |
---|
| 342 | mpz_clear( &result ); |
---|
| 343 | return res; |
---|
[05d0b3] | 344 | } |
---|
| 345 | else |
---|
[54a019] | 346 | return new InternalInteger( result ); |
---|
[05d0b3] | 347 | } |
---|
| 348 | |
---|
| 349 | InternalCF * |
---|
| 350 | InternalInteger::bgcdcoeff ( const InternalCF * const c ) |
---|
| 351 | { |
---|
| 352 | ASSERT( ::is_imm( c ) == INTMARK, "incompatible base coefficients" ); |
---|
| 353 | |
---|
| 354 | // simply return 1 if we are calculating over the rationals |
---|
| 355 | if ( cf_glob_switches.isOn( SW_RATIONAL ) ) |
---|
[54a019] | 356 | return int2imm( 1 ); |
---|
[05d0b3] | 357 | |
---|
| 358 | int cInt = imm2int( c ); |
---|
| 359 | |
---|
| 360 | // trivial cases |
---|
| 361 | if ( cInt == 1 || cInt == -1 ) |
---|
[54a019] | 362 | return int2imm( 1 ); |
---|
[05d0b3] | 363 | else if ( cInt == 0 ) |
---|
[54a019] | 364 | return copyObject(); |
---|
[05d0b3] | 365 | |
---|
| 366 | // calculate gcd. We need a positive operand since |
---|
| 367 | // `mpz_gcd_ui()' operates an unsigned int's only. |
---|
| 368 | if ( cInt < 0 ) cInt = -cInt; |
---|
| 369 | MP_INT dummy; |
---|
| 370 | mpz_init( &dummy ); |
---|
| 371 | // we do not need dummy since we know that cInt != 0 |
---|
| 372 | cInt = mpz_gcd_ui( &dummy, &thempi, cInt ); |
---|
| 373 | mpz_clear( &dummy ); |
---|
| 374 | if ( cInt < 0 ) cInt = -cInt; |
---|
| 375 | return int2imm( cInt ); |
---|
| 376 | } |
---|
| 377 | //}}} |
---|
| 378 | |
---|
| 379 | //{{{ InternalCF * InternalInteger::bextgcdsame( InternalCF * c, CanonicalForm & a, CanonicalForm & b ) |
---|
| 380 | InternalCF * |
---|
| 381 | InternalInteger::bextgcdsame( InternalCF * c, CanonicalForm & a, CanonicalForm & b ) |
---|
| 382 | { |
---|
| 383 | ASSERT( ! ::is_imm( c ) && c->levelcoeff() == IntegerDomain, "incompatible base coefficients" ); |
---|
| 384 | |
---|
| 385 | // simply return 1 if we are calculating over the rationals |
---|
| 386 | if ( cf_glob_switches.isOn( SW_RATIONAL ) ) { |
---|
[54a019] | 387 | a = 1/CanonicalForm( copyObject() ); b = 0; |
---|
| 388 | return int2imm( 1 ); |
---|
[05d0b3] | 389 | } |
---|
| 390 | |
---|
| 391 | // calculate extended gcd |
---|
| 392 | MP_INT result; |
---|
| 393 | MP_INT aMPI; |
---|
| 394 | MP_INT bMPI; |
---|
| 395 | mpz_init( &result ); |
---|
| 396 | mpz_init( &aMPI ); |
---|
| 397 | mpz_init( &bMPI ); |
---|
| 398 | mpz_gcdext( &result, &aMPI, &bMPI, &thempi, &MPI( c ) ); |
---|
[54a019] | 399 | |
---|
[05d0b3] | 400 | // check and modify signs |
---|
| 401 | if ( mpz_sgn( &result ) < 0 ) { |
---|
[54a019] | 402 | mpz_neg( &result, &result ); |
---|
| 403 | mpz_neg( &aMPI, &aMPI ); |
---|
| 404 | mpz_neg( &bMPI, &bMPI ); |
---|
[05d0b3] | 405 | } |
---|
| 406 | |
---|
| 407 | // postconditioning of result |
---|
| 408 | if ( mpz_is_imm( &aMPI ) ) { |
---|
[54a019] | 409 | a = CanonicalForm( int2imm( mpz_get_si( &aMPI ) ) ); |
---|
| 410 | mpz_clear( &aMPI ); |
---|
[05d0b3] | 411 | } else |
---|
[54a019] | 412 | a = CanonicalForm( new InternalInteger( aMPI ) ); |
---|
[05d0b3] | 413 | if ( mpz_is_imm( &bMPI ) ) { |
---|
[54a019] | 414 | b = CanonicalForm( int2imm( mpz_get_si( &bMPI ) ) ); |
---|
| 415 | mpz_clear( &bMPI ); |
---|
[05d0b3] | 416 | } else |
---|
[54a019] | 417 | b = CanonicalForm( new InternalInteger( bMPI ) ); |
---|
[05d0b3] | 418 | if ( mpz_is_imm( &result ) ) { |
---|
[54a019] | 419 | InternalCF * res = int2imm( mpz_get_si( &result ) ); |
---|
| 420 | mpz_clear( &result ); |
---|
| 421 | return res; |
---|
[05d0b3] | 422 | } |
---|
| 423 | else |
---|
[54a019] | 424 | return new InternalInteger( result ); |
---|
[05d0b3] | 425 | } |
---|
| 426 | |
---|
| 427 | InternalCF * |
---|
| 428 | InternalInteger::bextgcdcoeff( InternalCF * c, CanonicalForm & a, CanonicalForm & b ) |
---|
| 429 | { |
---|
| 430 | ASSERT( ::is_imm( c ) == INTMARK, "incompatible base coefficients" ); |
---|
| 431 | |
---|
| 432 | // simply return 1 if we are calculating over the rationals |
---|
| 433 | if ( cf_glob_switches.isOn( SW_RATIONAL ) ) { |
---|
[54a019] | 434 | a = 1/CanonicalForm( copyObject() ); b = 0; |
---|
| 435 | return int2imm( 1 ); |
---|
[05d0b3] | 436 | } |
---|
| 437 | |
---|
| 438 | int cInt = imm2int( c ); |
---|
| 439 | |
---|
| 440 | // trivial cases |
---|
| 441 | if ( cInt == 1 || cInt == -1 ) { |
---|
[54a019] | 442 | a = 0; b = cInt; |
---|
| 443 | return int2imm( 1 ); |
---|
[05d0b3] | 444 | } else if ( cInt == 0 ) { |
---|
[54a019] | 445 | a = 1; b = 0; |
---|
| 446 | return copyObject(); |
---|
[05d0b3] | 447 | } |
---|
| 448 | |
---|
| 449 | // calculate q and r such that CO = q*cInt + r |
---|
| 450 | InternalCF * q = 0, * r = 0; |
---|
| 451 | divremcoeff( c, q, r, false ); |
---|
| 452 | |
---|
| 453 | // we do not repeat all the code to calculate the gcd of two |
---|
| 454 | // immediates. Note that r is an immediate since c != 0, so |
---|
| 455 | // we do not have to destroy it. q is destroyed by the |
---|
| 456 | // CanonicalForm destructor, hence we do not need to worry |
---|
| 457 | // about it, either. |
---|
| 458 | CanonicalForm aPrime, bPrime; |
---|
| 459 | CanonicalForm result = bextgcd( c, r, aPrime, bPrime ); |
---|
| 460 | a = bPrime; |
---|
| 461 | b = aPrime - CanonicalForm( q ) * bPrime; |
---|
| 462 | |
---|
| 463 | return result.getval(); |
---|
| 464 | } |
---|
| 465 | //}}} |
---|
| 466 | |
---|
[2dd068] | 467 | int InternalInteger::intval() const |
---|
| 468 | { |
---|
| 469 | return (int)mpz_get_si( &thempi ); |
---|
| 470 | } |
---|
| 471 | |
---|
| 472 | int InternalInteger::intmod( int p ) const |
---|
| 473 | { |
---|
| 474 | return (int)mpz_mmod_ui( 0, &thempi, (unsigned long)p ); |
---|
| 475 | } |
---|
| 476 | |
---|
[c62f38] | 477 | //{{{ int InternalInteger::sign () const |
---|
| 478 | // docu: see CanonicalForm::sign() |
---|
| 479 | int |
---|
| 480 | InternalInteger::sign () const |
---|
[2dd068] | 481 | { |
---|
[c62f38] | 482 | return mpz_sgn( &thempi ); |
---|
[2dd068] | 483 | } |
---|
[c62f38] | 484 | //}}} |
---|
[2dd068] | 485 | |
---|
[c556da] | 486 | //{{{ InternalCF * InternalInteger::sqrt () |
---|
[48c934] | 487 | // docu: see CanonicalForm::sqrt() |
---|
[c556da] | 488 | InternalCF * |
---|
[48c934] | 489 | InternalInteger::sqrt () |
---|
[2dd068] | 490 | { |
---|
[0dcb70] | 491 | ASSERT( mpz_cmp_si( &thempi, 0 ) >= 0, "sqrt() argument < 0" ); |
---|
[2dd068] | 492 | MP_INT result; |
---|
| 493 | mpz_init( &result ); |
---|
| 494 | mpz_sqrt( &result, &thempi ); |
---|
| 495 | if ( mpz_is_imm( &result ) ) { |
---|
[54a019] | 496 | InternalCF * res = int2imm( mpz_get_si( &result ) ); |
---|
| 497 | mpz_clear( &result ); |
---|
| 498 | return res; |
---|
[2dd068] | 499 | } |
---|
| 500 | else |
---|
[54a019] | 501 | return new InternalInteger( result ); |
---|
[2dd068] | 502 | } |
---|
[48c934] | 503 | //}}} |
---|
[6e60d7] | 504 | |
---|
[48c934] | 505 | //{{{ int InternalInteger::ilog2 () |
---|
[6e60d7] | 506 | // docu: see CanonicalForm::ilog2() |
---|
| 507 | int |
---|
[48c934] | 508 | InternalInteger::ilog2 () |
---|
[6e60d7] | 509 | { |
---|
[0dcb70] | 510 | ASSERT( mpz_cmp_si( &thempi, 0 ) > 0, "log() argument <= 0" ); |
---|
[6e60d7] | 511 | return mpz_sizeinbase( &thempi, 2 ) - 1; |
---|
| 512 | } |
---|
| 513 | //}}} |
---|