/src/gmpy2.c
C | 1134 lines | 618 code | 88 blank | 428 comment | 53 complexity | d0637f12d60938be72aed290a315ad65 MD5 | raw file
Large files files are truncated, but you can click here to view the full file
1/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 2 * gmpy2.c * 3 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 4 * Python interface to the GMP or MPIR, MPFR, and MPC multiple precision * 5 * libraries. * 6 * * 7 * Copyright 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, * 8 * 2008, 2009 Alex Martelli * 9 * * 10 * Copyright 2008, 2009, 2010, 2011, 2012, 2013, 2014 Case Van Horsen * 11 * * 12 * This file is part of GMPY2. * 13 * * 14 * GMPY2 is free software: you can redistribute it and/or modify it under * 15 * the terms of the GNU Lesser General Public License as published by the * 16 * Free Software Foundation, either version 3 of the License, or (at your * 17 * option) any later version. * 18 * * 19 * GMPY2 is distributed in the hope that it will be useful, but WITHOUT * 20 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * 21 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * 22 * License for more details. * 23 * * 24 * You should have received a copy of the GNU Lesser General Public * 25 * License along with GMPY2; if not, see <http://www.gnu.org/licenses/> * 26 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ 27 28 /* Todo list 29 * --------- 30 * Add all MPFR and MPC functions as context methods. 31 * All MPFR and MPC functions need to set exponent range on entry. The 32 * current approach where only set_context() and context.__enter__ set 33 * the exponent range fails for context methods. 34 * Should a read-only (or template) context prevent the setting of 35 * exception flags? 36 * Add context option to control the result of integer division: 37 * integer (mpz), exact (mpq), or true (mpfr). 38 * Add modular arithmetic functions. 39 * Implement Chinese Remainder Theorem. 40 * Update PRP code. 41 */ 42 43/* 44 * originally written for GMP-2.0 (by AMK...?) 45 * Rewritten by Niels Möller, May 1996 46 * 47 * Version for GMP-4, Python 2.X, with support for MSVC++6, 48 * addition of mpf's, &c: Alex Martelli (now aleaxit@gmail.com, Nov 2000). 49 * cleanups & reorgs leading to 1.0: Alex Martelli (until Aug 2003) 50 * further cleanups and bugfixes leading to 1.01, Alex Martelli (Nov 2005) 51 * minor bugfixes+new decimal (&c) support to 1.02, Alex Martelli (Feb 2006) 52 * various bugfixes for 64-bit platforms, 1.03, aleaxit and casevh (Jun 2008) 53 * rich comparisons, 1.04, aleaxit and casevh (Jan 2009) 54 * support for Python 3.x, 1.10, casevh (Oct 2009) 55 * 56 * Some hacks by Gustavo Niemeyer <niemeyer@conectiva.com>. 57 * 58 * 0.1, pre-alpha; date: 2000-11-06 first placed on sourceforge 59 * 60 * 0.2, still pre-alpha: 2000-11-15: bugfixes re formatting (tx, Peanu!) 61 * no tags on oct() and hex() of mpz's 62 * insert 'tagoff' in options (gmpy.mpz() vs mpz() in repr) (for Peanu!) 63 * speedups for _nonzero & _cmp (tx, Peanu!) 64 * slight speedup (7/8%?) for excess reallocs 4<->8 bytes (Peanu's help!) 65 * added copy/fcopy; bin; fib; remove 66 * 67 * 0.3, still pre-alpha, but...: 68 * performance tweaks via mpz-caching & fixed-constants 69 * added get/set functions for zcache, zco min/max 70 * added get-only function for versions (of gmp, and of gmpy) 71 * removed all 'traces' of mutability (to be re-done... much later!) 72 * cleaned up all of the mpz_cmp_ui(X,0) to mpz_sgn(X) 73 * cleaned up Py_BuildValue usage (N vs O, explicit-() for tuples) 74 * added numdigits, lowbits, root, next_prime, invert, popcount, 75 * hamdist, scan0, scan1 76 * renamed bin to bincoef 77 * 78 * 0.4: 79 * split gmpy.c/gmpy.h introducing C-API interface (Pearu's suggestion) 80 * cleanup some casts using Pearu's new macros 81 * further cache-tweaks at Pearu's suggestion (macros introduced) 82 * added sign (Pearu's request), getbit, setbit 83 * added docstrings 84 * renamed copy functions to start with _ ('internal, private') 85 * added .comb as a synonym of .bincoef 86 * 87 * 0.5: 88 * added jacobi, legendre, kronecker 89 * added random-number generation, seed set/save, shuffling 90 * added mpq (at last!-) 91 * 92 * 0.6: (lots of good ideas from Pearu once more!-): 93 * fixed silly bugs in kronecker and mpq_abs 94 * gmpy-level workaround for scan0/scan1 bugs (?) in GMP 3.1.1 95 * added qdiv; anynum->mpq substituted for all such conversions 96 * (also anynum->mpz and anynum->mpf by analogy, with care!) 97 * added global.fcoform for optional use of intermediate string in 98 * float2mpf (used for any float->mpf conversion) 99 * added set_fcoform function for global.fcoform access 100 * general cleanup of sources; added alloca for MSVC++; 101 * many sundry minor bugfixes & uniformization; 102 * a little useful refactoring (more would be good...) 103 * added caching of mpq objects 104 * power for mpq 105 * Stern-Brocot algorithm for mpf->mpq (also exposed as f2q) 106 * also used for float->mpq 107 * with stricter tracking of mpf's requested-precision 108 * added getrprec method to mpf, getrprec module-function 109 * exposed ceil, floor and trunc methods/functions for mpf's 110 * changed a couple exceptions from Value to ZeroDivision 111 * added 'qual' and 'floa' options to gmpy.rand 112 * 113 * 0.7: (good feedback from Keith Briggs, some advice from Tim Peters 114 * and Fred Lundh -- thanks all!): 115 * fixed bug of '"%d" where "%ld" was meant' in many places 116 * and other sundry minor warnings given by gcc 117 * fixed hash (delegating to Python) so mp[nqz](x) will 118 * produce the same value as hash(x) for any Python number x 119 * workaround for GMP 3.1.1 bug, mpz_root wrongly returning 120 * 'exact' for non-exact root if dest==source, which stopped 121 * needed value-error for inexact mpq**mpq operations 122 * determined correct 'actual precision' of floats 123 * explicitly stored precision with binary-form mpf's 124 * extended explicit-bits request to all ->mpf operations 125 * (good in itself, plus, preparing for future MPFR) 126 * removed the limitation of no binary-form for <0 mpz 127 * introduced macros to parse args, for conciseness 128 * 129 * 0.8: (again, requests & suggestions by great Pearu!) 130 * raise test coverage 72.5% -> 90.0% 131 * introduced callbacks (not documented/tested for now; 132 * Pearu will test/support/document in PySymbolic) 133 * some errors went undiagnosed, caused crash: now fixed 134 * workaround for GMP bug(?s?) in mpz_fits_... (?) 135 * added exposure of mpf_ sqrt and pow_ui 136 * 137 * 0.9: (ditto) 138 * change ValueError to OverflowError for 'too-large' errors 139 * fix bug in mpq_pow (negative base, exp. with odd denominator) 140 * (fix now corrected -- _even_ denominator is the error!) 141 * fixed gcc warnings reported by K. Briggs 142 * 143 * 0.9b: 144 * support GMP 4 (but added no GMP4-only functionality yet) 145 * 146 * 0.9c: 147 * updated tests to 0.9, better coverage 148 * 149 * 1.0: 150 * minor cleanups, ensure support for Python 2.3 151 * fixed misdiagnosis of some argument counts in macro 152 * SELF_ONE_ARG_CONVERTED (tx to Paul Rubin!) 153 * 154 * 1.01: 155 * cleanups, ensure support for Python 2.4.1 on MacOSX 10.4/XCode 2.1 156 * as well as Python 2.2 and 2.3 (on MacOSX and Linux) 157 * fixed memory leak on divm (thanks to mensanator@aol.com) 158 * fixed bug on mpq('123') [[str2mpq on string w/o a slash]] 159 * added floordiv and truediv operators, and tests for them 160 * NOT tested on GMP 3 (have none left around...), ONLY on GMP 4.* 161 * 162 * 1.02: 163 * fix warning in comparison of mpq's 164 * added support of mpq('12.34') [[string w/o a slash, but with a dot]] 165 * fixes for 64-bit build (thanks to a patch by dmcooke) 166 * added experimental support for decimal.Decimal (and user-coded types) 167 * via wider use of special conversion methods (if present) and their 168 * sly insertion on-the-fly into the decimal.Decimal class (!) 169 * two bugfixes, thanks to Simon Burton 170 * Brought back into C89 compliance (thanks to Chip Turner), had 171 * drifted to C99 (declarations in the middle of the code). 172 * Python 2.5 support (Py_ssize_t, __index__) thanks to Chip Turner 173 * Pushed coverage to 93.3% (missing only "sanity check" level error 174 * tests [mostly for out-of-memory conditions], output to stderr 175 * conditioned by global.debug, & a couple of very obscure cases) 176 * 177 * 1.03: 178 * Fixed the bug that caused crashes on gmpy.mpf(float('inf')) and 179 * other such conversions, implicit and explicit 180 * Fixed a bug in get_zconst's prototype affecting 64-bit machines, 181 * thanks to Gary Bunting 182 * Fixed a bug in hashing on 64-bit systems. hash(long) now equals 183 * hash(mpz) for large values. (casevh) 184 * Changed int() to return a long value instead of OverFlowError. 185 * Complies with PEP 237. (casevh) 186 * Added support in setup.py for darwinports/macports build of GMP 187 * on MacOSX. (aleaxit) 188 * 189 * 1.04: 190 * Avoid GMP/mingw32 bug when converting very small floats to mpz. 191 * (casevh) 192 * Significant performance improvement for long->mpz and mpz->long. 193 * (casevh) 194 * Added "rich comparisons" to mpz, mpq and mpf types (aleaxit) 195 * Added additional tests (casevh, aleaxit) 196 * Fixed bug when converting very large mpz to str (casevh) 197 * Faster conversion from mpz->binary and binary->mpz (casevh) 198 * Added support for pickling (casevh) 199 * Added divexact (casevh) 200 * Fixed mpf comparisons by rounding mpf results when GMP returns 201 * a longer result. Added fround() (casevh) 202 * Added bit_length (Thanks Mario Pernici) 203 * Added helper functions for mpmath (casevh) 204 * Faster conversion from mpq->binary and binary->mpq (casevh) 205 * Recognize MPIR, mpir_version() (casevh) 206 * 207 * 1.10: 208 * Remove dependancy on pymemcompat.h (casevh) 209 * Remove callback (casevh) 210 * Added support for -DMPIR to include MPIR instead of GMP (casevh) 211 * Major code revisions to add support for Python 3.x (casevh) 212 * Fixed bug in binary() and qbinary() (casevh) 213 * Fixed bug in rich comparisons (casevh) 214 * Added % and divmod support to mpq and mpf (casevh) 215 * Changed memory allocation functions to use PyMem (casevh) 216 * Removed small number interning (casevh) 217 * Added tdivmod, cdivmod, and fdivmod (casevh) 218 * Added more helper functions for mpmath (casevh) 219 * Faster mpz<>PyLong conversion (casevh) 220 * Faster hash(mpz) (casevh) 221 * 222 * 1.11: 223 * Recognize True/False (bug in 1.10) (casevh) 224 * Optimize argument handling (casevh) 225 * Added caching for mpz (casevh) 226 * 227 ************************************************************************ 228 * 229 * 2.0.0 alpha and b1: 230 * Added caching for mpq (casevh) 231 * Added rootrem, fib2, lucas, lucas2 (casevh) 232 * Removed mpf.setprec(), use mpf.round() (casevh) 233 * Fix test compatibility with Python 3.1.2 and 3.2 (casevh) 234 * Support changed hash function in Python 3.2 (casevh) 235 * Added is_even, is_odd (casevh) 236 * Rename to gmpy2 to allow backwards incompatible changes (casevh) 237 * Remove old random number functions, to be replaced later (casevh) 238 * Add caching of the calculated hash value (casevh) 239 * Add xmpz (mutable mpz) type (casevh) 240 * Fix mpq formatting issue (casevh) 241 * Add read/write bit access using slices to xmpz (casevh) 242 * Add read-only bit access using slices to mpz (casevh) 243 * Add pack()/unpack() methods (casevh) 244 * Remove tagoff option (casevh) 245 * Add support for MPFR (casevh) 246 * Debug messages only available if compiled with -DDEBUG (casevh) 247 * Removed fcoform float conversion modifier (casevh) 248 * Add support for MPC (casevh) 249 * Renamed 'mpf' to 'mpfr' to reflect use of MPFR (casevh) 250 * Added context manager (casevh) 251 * Allow building with just GMP/MPIR if MPFR not available (casevh) 252 * Allow building with GMP/MPIR and MPFR if MPC not available (casevh) 253 * Removed most instance methods in favor of gmpy2.method (casevh) 254 * Added __ceil__, __floor__, and __trunc__ methods (casevh) 255 * Removed gmpy2.pow to avoid conflicts (casevh) 256 * Removed gmpy2._copy and added xmpz.copy (casevh) 257 * Added support for __format__ (casevh) 258 * Completed support for MPC (casevh) 259 * Added as_integer_ratio, as_mantissa_exp, as_simple_fraction (casevh) 260 * Update rich_compare (casevh) 261 * Require MPFR 3.1.0+ to get divby0 support (casevh) 262 * Added fsum(), degrees(), radians() (casevh) 263 * Renamed context() -> local_context(), new_context() -> context() (casevh) 264 * Added get_context() (casevh) 265 * Added random number generation support (casevh) 266 * Changed license to LGPL 3+ (casevh) 267 * Added lucasu, lucasu_mod, lucasv, and lucasv_mod (casevh) 268 * (Based on code contributed by David Cleaver.) 269 * Added probable-prime tests (casevh) 270 * (Based on code contributed by David Cleaver.) 271 * Added to_binary()/from_binary (casevh) 272 * Renamed numdigits() to num_digits() (casevh) 273 * Added keyword precision to constants (casevh) 274 * Added addmul() and submul() (casevh) 275 * Added __round__(), round2(), round_away() for mpfr (casevh) 276 * round() is no longer a module level function (casevh) 277 * pow() is no longer a module level function (casevh) 278 * Renamed module functions min()/max() to min2()/max2() (casevh) 279 * No longer conflicts with builtin min() and max() 280 * Removed set_debug() and related functionality (casevh) 281 * Released as 2.0.0b1 282 * 283 * 2.0.0b2 284 * Allow xmpz slice assignment to increase length of xmpz instance by 285 * specifying a value for stop (casevh) 286 * Fixed ref-count bug in several is_xxx_prp tests (casevh) 287 * Added iter_bits, iter_clear, iter_set methods to xmpz (casevh) 288 * Added powmod() for easy access to three argument pow() (casevh) 289 * Removed addmul() and submul() since they are slower than (casevh) 290 * just using Python code 291 * Bug fix in gcd_ext when both arguments are not mpz (casevh) 292 * Added ieee() to create contexts for 32, 64, or 128 bit floats (casevh) 293 * Bug fix in context() not setting emax/emin correctly if they (casevh) 294 * had been changed earlier 295 * Contexts can be directly used in with statement without (casevh) 296 * requiring set_context()/local_context() sequence 297 * local_context() now accepts an optional context (casevh) 298 * 299 * 2.0.0b3 300 * mp_version(), mpc_version(), and mpfr_version() shouldn't (casevh) 301 * return Unicode on Python 2.x 302 * Fix warnings when shifting 32-bit integer by 32 bits (casevh) 303 * Faster conversion of Fraction to gmpy2 types (casevh) 304 * Fix conversion with Decimal, especially on Python 3.3 (casevh) 305 * Consistently return OverflowError when converting "inf" (casevh) 306 * Fix mpz.__format__() with # code (casevh) 307 * Add is_infinite(), deprecate is_inf() (casevh) 308 * Add is_finite(), deprecate is_number() (casevh) 309 * Fixed issues with mpc() and various is_XXX() functions (casevh) 310 * Fixed error handling with mpc(); mpc(1,"nan") is properly (casevh) 311 * handled 312 * Added caching for mpc objects; faster when real and (casevh) 313 * imaginary precisions are equal 314 * Add optimal path for mpfr/mpc + - * / when both operands (casevh) 315 * have the same type 316 * Fix mpfr + float segmentation fault (casevh) 317 * 318 * 2.0.0b4 319 * Add __ceil__, __floor__, __trunc__, __round__ to mpz & mpq (casevh) 320 * Add __complex__ to mpc (casevh) 321 * round(mpfr) now correctly returns an mpz (casevh) 322 * Add mpz.denominator and mpz.numerator (casevh) 323 * mpz() returns mpz(0); also xmpz, mpq, mpfr, and mpc (casevh) 324 * Fix bug when comparing mpz to mpq (with mpz on left) (casevh) 325 * Add __sizeof__ (casevh) 326 * 327 * 2.0.0 328 * Fix segfault in _mpmath_normalize if rnd not specified (casevh) 329 * Improved setup.py (casevh) 330 * Fix issues encountered when compiled without MPFR support (casevh) 331 * Conversion of too large an mpz to float now raises OverflowError (casevh) 332 * Renamed module functions min2()/max2() to minnum()/maxnum() (casevh) 333 * Added copy() method to contexts (casevh) 334 * get_context() no longer supports keyword arguments (casevh) 335 * 336 ************************************************************************ 337 * 338 * 2.1.0 339 * Improvements to setup.py (casevh) 340 * Add thread-safe contexts (casevh) 341 * MPFR and MPC are now required (casevh) 342 * Invalid Operation exception now raised for addition, etc. (casevh) 343 * inverse() now raises exception if inverse does not exist (casevh) 344 * Add context methods (casevh) 345 * Major code refactoring required to properly support thread-safe 346 * contexts. (casevh) 347 * __str__ and __repr__ no longer append "L" on Python 2 (casevh) 348 * mpq(mpfr) now returns the exact result (casevh) 349 * Fix repr(mpc) for precision >325 bits. 350 * 351 ************************************************************************ 352 * 353 * Discussion on sizes of C integer types. 354 * 355 * GMP, MPIR, MPFR, and MPC use typedef to create integer objects with 356 * different sizes. It can become confusing to map the different types 357 * onto the standard C types used by Python's C API. Below are external 358 * types and how they map to C types. The assumptions are verified when 359 * the module is initialized. 360 * 361 * mp_limb_t: This is usually an 'unsigned long' but is an 'unsigned 362 * long long' on MPIR/64-bit Windows. 363 * 364 * mp_bitcnt_t: This is usually an 'unsigned long' but is an 'unsigned 365 * long long' on MPIR/64-bit Windows. 'size_t' is the best match. 366 * 367 * mp_size_t: This is a 'long'. 368 * 369 * mpfr_rnd_t: This is an 'int'. 370 * 371 * mpfr_prec_t: This is a 'long'. 372 * 373 * mpfr_sign_t: This is an 'int'. 374 * 375 * mpfr_exp_t: This is currently the same as mp_exp_t but will change 376 * to a signed 64-bit integer in the future. 377 * 378 * mpc_rnd_t: This is an 'int'. 379 * 380 * mpc_prec_t: See mpfr_exp_t. 381 * 382 ************************************************************************ 383 */ 384#define PY_SSIZE_T_CLEAN 385#include "Python.h" 386 387/* 388 * we do have a dependence on Python's internals, specifically: 389 * how Python "long int"s are internally represented. 390 */ 391#include "longintrepr.h" 392 393#include <assert.h> 394#include <math.h> 395#include <float.h> 396#include <stdio.h> 397#include <ctype.h> 398 399#include "gmpy2.h" 400 401/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 402 * Global data declarations begin here. * 403 * NOTE: Because of these global declarations, GMPY2 is not thread-safe! * 404 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ 405 406/* The following global strings are used by gmpy_misc.c. */ 407 408char gmpy_version[] = "2.1.0a0"; 409 410char _gmpy_cvs[] = "$Id: gmpy2.c 1028 2015-02-11 06:16:56Z casevh $"; 411 412char gmpy_license[] = "\ 413The GMPY2 source code is licensed under LGPL 3 or later. The supported \ 414versions of the GMP/MPIR, MPFR, and MPC libraries are also licensed under \ 415LGPL 3 or later."; 416 417/* The following global structures are used by gmpy_cache.c. 418 */ 419 420static struct gmpy_global { 421 int cache_size; /* size of cache, for all caches */ 422 int cache_obsize; /* maximum size of the objects that are cached */ 423} global = { 424 100, /* cache_size */ 425 128, /* cache_obsize */ 426}; 427 428static mpz_t* zcache; 429static int in_zcache; 430 431static MPZ_Object **gmpympzcache; 432static int in_gmpympzcache; 433 434static XMPZ_Object **gmpyxmpzcache; 435static int in_gmpyxmpzcache; 436 437static MPQ_Object **gmpympqcache; 438static int in_gmpympqcache; 439 440static MPFR_Object **gmpympfrcache; 441static int in_gmpympfrcache; 442 443static MPC_Object **gmpympccache; 444static int in_gmpympccache; 445 446/* Support for context manager. */ 447 448#ifdef WITHOUT_THREADS 449/* Use a module-level context. */ 450static CTXT_Object *module_context = NULL; 451#else 452/* Key for thread state dictionary */ 453static PyObject *tls_context_key = NULL; 454/* Invariant: NULL or the most recently accessed thread local context */ 455static CTXT_Object *cached_context = NULL; 456#endif 457 458 459/* Define gmpy2 specific errors for mpfr and mpc data types. No change will 460 * be made the exceptions raised by mpz, xmpz, and mpq. 461 */ 462 463static PyObject *GMPyExc_GmpyError = NULL; 464static PyObject *GMPyExc_DivZero = NULL; 465static PyObject *GMPyExc_Inexact = NULL; 466static PyObject *GMPyExc_Invalid = NULL; 467static PyObject *GMPyExc_Overflow = NULL; 468static PyObject *GMPyExc_Underflow = NULL; 469static PyObject *GMPyExc_Erange = NULL; 470 471 472/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 473 * End of global data declarations. * 474 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ 475 476/* The code for object creation, deletion, and caching is in gmpy_cache.c. */ 477 478#include "gmpy2_cache.c" 479 480/* Miscellaneous helper functions and simple methods are in gmpy_misc.c. */ 481 482#include "gmpy2_misc.c" 483 484/* Include fast mpz to/from PyLong conversion from sage. */ 485 486#include "mpz_pylong.c" 487 488/* Support for conversion to/from binary representation. */ 489 490#include "gmpy2_binary.c" 491 492/* Support for conversions to/from numeric types. */ 493 494#include "gmpy2_convert.c" 495#include "gmpy2_convert_utils.c" 496#include "gmpy2_convert_gmp.c" 497#include "gmpy2_convert_mpfr.c" 498#include "gmpy2_convert_mpc.c" 499 500/* Support for random numbers. */ 501 502#include "gmpy2_random.c" 503 504/* Support for Lucas sequences. */ 505 506#include "gmpy_mpz_lucas.c" 507 508/* Support for probable-prime tests. */ 509 510#include "gmpy_mpz_prp.c" 511 512/* Include helper functions for mpmath. */ 513 514#include "gmpy2_mpmath.c" 515 516#include "gmpy2_mpz_divmod.c" 517#include "gmpy2_mpz_divmod2exp.c" 518#include "gmpy2_mpz_pack.c" 519#include "gmpy2_mpz_bitops.c" 520#include "gmpy2_mpz_inplace.c" 521#include "gmpy2_xmpz_inplace.c" 522 523/* Begin includes of refactored code. */ 524 525#include "gmpy2_abs.c" 526#include "gmpy2_add.c" 527#include "gmpy2_divmod.c" 528#include "gmpy2_floordiv.c" 529#include "gmpy2_minus.c" 530#include "gmpy2_mod.c" 531#include "gmpy2_mul.c" 532#include "gmpy2_plus.c" 533#include "gmpy2_pow.c" 534#include "gmpy2_sub.c" 535#include "gmpy2_truediv.c" 536#include "gmpy2_math.c" 537#include "gmpy2_const.c" 538#include "gmpy2_square.c" 539#include "gmpy2_format.c" 540#include "gmpy2_hash.c" 541#include "gmpy2_fused.c" 542#include "gmpy2_muldiv_2exp.c" 543#include "gmpy2_predicate.c" 544#include "gmpy2_sign.c" 545#include "gmpy2_richcompare.c" 546#include "gmpy2_mpc_misc.c" 547#include "gmpy2_mpfr_misc.c" 548#include "gmpy2_mpq_misc.c" 549#include "gmpy2_mpz_misc.c" 550#include "gmpy2_xmpz_misc.c" 551 552/* Include gmpy_context last to avoid adding doc names to .h files. */ 553 554#include "gmpy2_mpz.c" 555#include "gmpy2_xmpz.c" 556#include "gmpy2_mpq.c" 557#include "gmpy2_mpfr.c" 558#include "gmpy2_mpc.c" 559 560#include "gmpy2_context.c" 561 562static PyMethodDef Pygmpy_methods [] = 563{ 564 { "_cvsid", GMPy_get_cvsid, METH_NOARGS, GMPy_doc_cvsid }, 565 { "_printf", GMPy_printf, METH_VARARGS, GMPy_doc_function_printf }, 566 { "add", GMPy_Context_Add, METH_VARARGS, GMPy_doc_function_add }, 567 { "bit_clear", GMPy_MPZ_bit_clear_function, METH_VARARGS, doc_bit_clear_function }, 568 { "bit_flip", GMPy_MPZ_bit_flip_function, METH_VARARGS, doc_bit_flip_function }, 569 { "bit_length", GMPy_MPZ_bit_length_function, METH_O, doc_bit_length_function }, 570 { "bit_mask", GMPy_MPZ_bit_mask, METH_O, doc_bit_mask }, 571 { "bit_scan0", GMPy_MPZ_bit_scan0_function, METH_VARARGS, doc_bit_scan0_function }, 572 { "bit_scan1", GMPy_MPZ_bit_scan1_function, METH_VARARGS, doc_bit_scan1_function }, 573 { "bit_set", GMPy_MPZ_bit_set_function, METH_VARARGS, doc_bit_set_function }, 574 { "bit_test", GMPy_MPZ_bit_test_function, METH_VARARGS, doc_bit_test_function }, 575 { "bincoef", GMPy_MPZ_Function_Bincoef, METH_VARARGS, GMPy_doc_mpz_function_bincoef }, 576 { "comb", GMPy_MPZ_Function_Bincoef, METH_VARARGS, GMPy_doc_mpz_function_comb }, 577 { "c_div", GMPy_MPZ_c_div, METH_VARARGS, doc_c_div }, 578 { "c_div_2exp", GMPy_MPZ_c_div_2exp, METH_VARARGS, doc_c_div_2exp }, 579 { "c_divmod", GMPy_MPZ_c_divmod, METH_VARARGS, doc_c_divmod }, 580 { "c_divmod_2exp", GMPy_MPZ_c_divmod_2exp, METH_VARARGS, doc_c_divmod_2exp }, 581 { "c_mod", GMPy_MPZ_c_mod, METH_VARARGS, doc_c_mod }, 582 { "c_mod_2exp", GMPy_MPZ_c_mod_2exp, METH_VARARGS, doc_c_mod_2exp }, 583 { "denom", GMPy_MPQ_Function_Denom, METH_O, GMPy_doc_mpq_function_denom }, 584 { "digits", GMPy_Context_Digits, METH_VARARGS, GMPy_doc_context_digits }, 585 { "div", GMPy_Context_TrueDiv, METH_VARARGS, GMPy_doc_truediv }, 586 { "divexact", GMPy_MPZ_Function_Divexact, METH_VARARGS, GMPy_doc_mpz_function_divexact }, 587 { "divm", GMPy_MPZ_Function_Divm, METH_VARARGS, GMPy_doc_mpz_function_divm }, 588 { "div_mod", GMPy_Context_DivMod, METH_VARARGS, GMPy_doc_divmod }, 589 { "double_fac", GMPy_MPZ_Function_DoubleFac, METH_O, GMPy_doc_mpz_function_double_fac }, 590 { "fac", GMPy_MPZ_Function_Fac, METH_O, GMPy_doc_mpz_function_fac }, 591 { "fib", GMPy_MPZ_Function_Fib, METH_O, GMPy_doc_mpz_function_fib }, 592 { "fib2", GMPy_MPZ_Function_Fib2, METH_O, GMPy_doc_mpz_function_fib2 }, 593 { "floor_div", GMPy_Context_FloorDiv, METH_VARARGS, GMPy_doc_floordiv }, 594 { "from_binary", GMPy_MPANY_From_Binary, METH_O, doc_from_binary }, 595 { "f_div", GMPy_MPZ_f_div, METH_VARARGS, doc_f_div }, 596 { "f_div_2exp", GMPy_MPZ_f_div_2exp, METH_VARARGS, doc_f_div_2exp }, 597 { "f_divmod", GMPy_MPZ_f_divmod, METH_VARARGS, doc_f_divmod }, 598 { "f_divmod_2exp", GMPy_MPZ_f_divmod_2exp, METH_VARARGS, doc_f_divmod_2exp }, 599 { "f_mod", GMPy_MPZ_f_mod, METH_VARARGS, doc_f_mod }, 600 { "f_mod_2exp", GMPy_MPZ_f_mod_2exp, METH_VARARGS, doc_f_mod_2exp }, 601 { "gcd", GMPy_MPZ_Function_GCD, METH_VARARGS, GMPy_doc_mpz_function_gcd }, 602 { "gcdext", GMPy_MPZ_Function_GCDext, METH_VARARGS, GMPy_doc_mpz_function_gcdext }, 603 { "get_cache", GMPy_get_cache, METH_NOARGS, GMPy_doc_get_cache }, 604 { "hamdist", GMPy_MPZ_hamdist, METH_VARARGS, doc_hamdist }, 605 { "invert", GMPy_MPZ_Function_Invert, METH_VARARGS, GMPy_doc_mpz_function_invert }, 606 { "iroot", GMPy_MPZ_Function_Iroot, METH_VARARGS, GMPy_doc_mpz_function_iroot }, 607 { "iroot_rem", GMPy_MPZ_Function_IrootRem, METH_VARARGS, GMPy_doc_mpz_function_iroot_rem }, 608 { "isqrt", GMPy_MPZ_Function_Isqrt, METH_O, GMPy_doc_mpz_function_isqrt }, 609 { "isqrt_rem", GMPy_MPZ_Function_IsqrtRem, METH_O, GMPy_doc_mpz_function_isqrt_rem }, 610 { "is_bpsw_prp", GMPY_mpz_is_bpsw_prp, METH_VARARGS, doc_mpz_is_bpsw_prp }, 611 { "is_congruent", GMPy_MPZ_Function_IsCongruent, METH_VARARGS, GMPy_doc_mpz_function_is_congruent }, 612 { "is_divisible", GMPy_MPZ_Function_IsDivisible, METH_VARARGS, GMPy_doc_mpz_function_is_divisible }, 613 { "is_even", GMPy_MPZ_Function_IsEven, METH_O, GMPy_doc_mpz_function_is_even }, 614 { "is_euler_prp", GMPY_mpz_is_euler_prp, METH_VARARGS, doc_mpz_is_euler_prp }, 615 { "is_extra_strong_lucas_prp", GMPY_mpz_is_extrastronglucas_prp, METH_VARARGS, doc_mpz_is_extrastronglucas_prp }, 616 { "is_fermat_prp", GMPY_mpz_is_fermat_prp, METH_VARARGS, doc_mpz_is_fermat_prp }, 617 { "is_fibonacci_prp", GMPY_mpz_is_fibonacci_prp, METH_VARARGS, doc_mpz_is_fibonacci_prp }, 618 { "is_lucas_prp", GMPY_mpz_is_lucas_prp, METH_VARARGS, doc_mpz_is_lucas_prp }, 619 { "is_odd", GMPy_MPZ_Function_IsOdd, METH_O, GMPy_doc_mpz_function_is_odd }, 620 { "is_power", GMPy_MPZ_Function_IsPower, METH_O, GMPy_doc_mpz_function_is_power }, 621 { "is_prime", GMPy_MPZ_Function_IsPrime, METH_VARARGS, GMPy_doc_mpz_function_is_prime }, 622 { "is_selfridge_prp", GMPY_mpz_is_selfridge_prp, METH_VARARGS, doc_mpz_is_selfridge_prp }, 623 { "is_square", GMPy_MPZ_Function_IsSquare, METH_O, GMPy_doc_mpz_function_is_square }, 624 { "is_strong_prp", GMPY_mpz_is_strong_prp, METH_VARARGS, doc_mpz_is_strong_prp }, 625 { "is_strong_bpsw_prp", GMPY_mpz_is_strongbpsw_prp, METH_VARARGS, doc_mpz_is_strongbpsw_prp }, 626 { "is_strong_lucas_prp", GMPY_mpz_is_stronglucas_prp, METH_VARARGS, doc_mpz_is_stronglucas_prp }, 627 { "is_strong_selfridge_prp", GMPY_mpz_is_strongselfridge_prp, METH_VARARGS, doc_mpz_is_strongselfridge_prp }, 628 { "jacobi", GMPy_MPZ_Function_Jacobi, METH_VARARGS, GMPy_doc_mpz_function_jacobi }, 629 { "kronecker", GMPy_MPZ_Function_Kronecker, METH_VARARGS, GMPy_doc_mpz_function_kronecker }, 630 { "lcm", GMPy_MPZ_Function_LCM, METH_VARARGS, GMPy_doc_mpz_function_lcm }, 631 { "legendre", GMPy_MPZ_Function_Legendre, METH_VARARGS, GMPy_doc_mpz_function_legendre }, 632 { "license", GMPy_get_license, METH_NOARGS, GMPy_doc_license }, 633 { "lucas", GMPy_MPZ_Function_Lucas, METH_O, GMPy_doc_mpz_function_lucas }, 634 { "lucasu", GMPY_mpz_lucasu, METH_VARARGS, doc_mpz_lucasu }, 635 { "lucasu_mod", GMPY_mpz_lucasu_mod, METH_VARARGS, doc_mpz_lucasu_mod }, 636 { "lucasv", GMPY_mpz_lucasv, METH_VARARGS, doc_mpz_lucasv }, 637 { "lucasv_mod", GMPY_mpz_lucasv_mod, METH_VARARGS, doc_mpz_lucasv_mod }, 638 { "lucas2", GMPy_MPZ_Function_Lucas2, METH_O, GMPy_doc_mpz_function_lucas2 }, 639 { "mod", GMPy_Context_Mod, METH_VARARGS, GMPy_doc_mod }, 640 { "mp_version", GMPy_get_mp_version, METH_NOARGS, GMPy_doc_mp_version }, 641 { "mp_limbsize", GMPy_get_mp_limbsize, METH_NOARGS, GMPy_doc_mp_limbsize }, 642 { "mpc_version", GMPy_get_mpc_version, METH_NOARGS, GMPy_doc_mpc_version }, 643 { "mpfr_version", GMPy_get_mpfr_version, METH_NOARGS, GMPy_doc_mpfr_version }, 644 { "mpq", (PyCFunction)GMPy_MPQ_Factory, METH_VARARGS | METH_KEYWORDS, GMPy_doc_mpq_factory }, 645 { "mpq_from_old_binary", GMPy_MPQ_From_Old_Binary, METH_O, doc_mpq_from_old_binary }, 646 { "mpz", (PyCFunction)GMPy_MPZ_Factory, METH_VARARGS | METH_KEYWORDS, GMPy_doc_mpz_factory }, 647 { "mpz_from_old_binary", GMPy_MPZ_From_Old_Binary, METH_O, doc_mpz_from_old_binary }, 648 { "mpz_random", GMPy_MPZ_random_Function, METH_VARARGS, GMPy_doc_mpz_random_function }, 649 { "mpz_rrandomb", GMPy_MPZ_rrandomb_Function, METH_VARARGS, GMPy_doc_mpz_rrandomb_function }, 650 { "mpz_urandomb", GMPy_MPZ_urandomb_Function, METH_VARARGS, GMPy_doc_mpz_urandomb_function }, 651 { "mul", GMPy_Context_Mul, METH_VARARGS, GMPy_doc_function_mul }, 652 { "multi_fac", GMPy_MPZ_Function_MultiFac, METH_VARARGS, GMPy_doc_mpz_function_multi_fac }, 653 { "next_prime", GMPy_MPZ_Function_NextPrime, METH_O, GMPy_doc_mpz_function_next_prime }, 654 { "numer", GMPy_MPQ_Function_Numer, METH_O, GMPy_doc_mpq_function_numer }, 655 { "num_digits", GMPy_MPZ_Function_NumDigits, METH_VARARGS, GMPy_doc_mpz_function_num_digits }, 656 { "pack", GMPy_MPZ_pack, METH_VARARGS, doc_pack }, 657 { "popcount", GMPy_MPZ_popcount, METH_O, doc_popcount }, 658 { "powmod", GMPy_Integer_PowMod, METH_VARARGS, GMPy_doc_integer_powmod }, 659 { "primorial", GMPy_MPZ_Function_Primorial, METH_O, GMPy_doc_mpz_function_primorial }, 660 { "qdiv", GMPy_MPQ_Function_Qdiv, METH_VARARGS, GMPy_doc_function_qdiv }, 661 { "remove", GMPy_MPZ_Function_Remove, METH_VARARGS, GMPy_doc_mpz_function_remove }, 662 { "random_state", GMPy_RandomState_Factory, METH_VARARGS, GMPy_doc_random_state_factory }, 663 { "set_cache", GMPy_set_cache, METH_VARARGS, GMPy_doc_set_cache }, 664 { "sign", GMPy_Context_Sign, METH_O, GMPy_doc_function_sign }, 665 { "square", GMPy_Context_Square, METH_O, GMPy_doc_function_square }, 666 { "sub", GMPy_Context_Sub, METH_VARARGS, GMPy_doc_sub }, 667 { "to_binary", GMPy_MPANY_To_Binary, METH_O, doc_to_binary }, 668 { "t_div", GMPy_MPZ_t_div, METH_VARARGS, doc_t_div }, 669 { "t_div_2exp", GMPy_MPZ_t_div_2exp, METH_VARARGS, doc_t_div_2exp }, 670 { "t_divmod", GMPy_MPZ_t_divmod, METH_VARARGS, doc_t_divmod }, 671 { "t_divmod_2exp", GMPy_MPZ_t_divmod_2exp, METH_VARARGS, doc_t_divmod_2exp }, 672 { "t_mod", GMPy_MPZ_t_mod, METH_VARARGS, doc_t_mod }, 673 { "t_mod_2exp", GMPy_MPZ_t_mod_2exp, METH_VARARGS, doc_t_mod_2exp }, 674 { "unpack", GMPy_MPZ_unpack, METH_VARARGS, doc_unpack }, 675 { "version", GMPy_get_version, METH_NOARGS, GMPy_doc_version }, 676 { "xbit_mask", GMPy_XMPZ_Function_XbitMask, METH_O, GMPy_doc_xmpz_function_xbit_mask }, 677 { "xmpz", (PyCFunction)GMPy_XMPZ_Factory, METH_VARARGS | METH_KEYWORDS, GMPy_doc_xmpz_factory }, 678 { "_mpmath_normalize", Pympz_mpmath_normalize, METH_VARARGS, doc_mpmath_normalizeg }, 679 { "_mpmath_create", Pympz_mpmath_create, METH_VARARGS, doc_mpmath_createg }, 680 681 { "acos", GMPy_Context_Acos, METH_O, GMPy_doc_function_acos }, 682 { "acosh", GMPy_Context_Acosh, METH_O, GMPy_doc_function_acosh }, 683 { "ai", GMPy_Context_Ai, METH_O, GMPy_doc_function_ai }, 684 { "agm", GMPy_Context_AGM, METH_VARARGS, GMPy_doc_function_agm }, 685 { "asin", GMPy_Context_Asin, METH_O, GMPy_doc_function_asin }, 686 { "asinh", GMPy_Context_Asinh, METH_O, GMPy_doc_function_asinh }, 687 { "atan", GMPy_Context_Atan, METH_O, GMPy_doc_function_atan }, 688 { "atanh", GMPy_Context_Atanh, METH_O, GMPy_doc_function_atanh }, 689 { "atan2", GMPy_Context_Atan2, METH_VARARGS, GMPy_doc_function_atan2 }, 690 { "can_round", GMPy_MPFR_Can_Round, METH_VARARGS, GMPy_doc_mpfr_can_round }, 691 { "cbrt", GMPy_Context_Cbrt, METH_O, GMPy_doc_function_cbrt }, 692 { "ceil", GMPy_Context_Ceil, METH_O, GMPy_doc_function_ceil }, 693 { "check_range", GMPy_Context_CheckRange, METH_O, GMPy_doc_function_check_range }, 694 { "const_catalan", (PyCFunction)GMPy_Function_Const_Catalan, METH_VARARGS | METH_KEYWORDS, GMPy_doc_function_const_catalan }, 695 { "const_euler", (PyCFunction)GMPy_Function_Const_Euler, METH_VARARGS | METH_KEYWORDS, GMPy_doc_function_const_euler }, 696 { "const_log2", (PyCFunction)GMPy_Function_Const_Log2, METH_VARARGS | METH_KEYWORDS, GMPy_doc_function_const_log2 }, 697 { "const_pi", (PyCFunction)GMPy_Function_Const_Pi, METH_VARARGS | METH_KEYWORDS, GMPy_doc_function_const_pi }, 698 { "context", (PyCFunction)GMPy_CTXT_Context, METH_VARARGS | METH_KEYWORDS, GMPy_doc_context }, 699 { "copy_sign", GMPy_MPFR_copy_sign, METH_VARARGS, GMPy_doc_mpfr_copy_sign }, 700 { "cos", GMPy_Context_Cos, METH_O, GMPy_doc_function_cos }, 701 { "cosh", GMPy_Context_Cosh, METH_O, GMPy_doc_function_cosh }, 702 { "cot", GMPy_Context_Cot, METH_O, GMPy_doc_function_cot }, 703 { "coth", GMPy_Context_Coth, METH_O, GMPy_doc_function_coth }, 704 { "csc", GMPy_Context_Csc, METH_O, GMPy_doc_function_csc }, 705 { "csch", GMPy_Context_Csch, METH_O, GMPy_doc_function_csch }, 706 { "degrees", GMPy_Context_Degrees, METH_O, GMPy_doc_function_degrees }, 707 { "digamma", GMPy_Context_Digamma, METH_O, GMPy_doc_function_digamma }, 708 { "div_2exp", GMPy_Context_Div_2exp, METH_VARARGS, GMPy_doc_function_div_2exp }, 709 { "eint", GMPy_Context_Eint, METH_O, GMPy_doc_function_eint }, 710 { "erf", GMPy_Context_Erf, METH_O, GMPy_doc_function_erf }, 711 { "erfc", GMPy_Context_Erfc, METH_O, GMPy_doc_function_erfc }, 712 { "exp", GMPy_Context_Exp, METH_O, GMPy_doc_function_exp }, 713 { "expm1", GMPy_Context_Expm1, METH_O, GMPy_doc_function_expm1 }, 714 { "exp10", GMPy_Context_Exp10, METH_O, GMPy_doc_function_exp10 }, 715 { "exp2", GMPy_Context_Exp2, METH_O, GMPy_doc_function_exp2 }, 716 { "f2q", GMPy_Context_F2Q, METH_VARARGS, GMPy_doc_function_f2q }, 717 { "factorial", GMPy_Context_Factorial, METH_O, GMPy_doc_function_factorial }, 718 { "floor", GMPy_Context_Floor, METH_O, GMPy_doc_function_floor }, 719 { "fma", GMPy_Context_FMA, METH_VARARGS, GMPy_doc_function_fma }, 720 { "fms", GMPy_Context_FMS, METH_VARARGS, GMPy_doc_function_fms }, 721 { "fmod", GMPy_Context_Fmod, METH_VARARGS, GMPy_doc_function_fmod }, 722 { "frac", GMPy_Context_Frac, METH_O, GMPy_doc_function_frac }, 723 { "free_cache", GMPy_MPFR_Free_Cache, METH_NOARGS, GMPy_doc_mpfr_free_cache }, 724 { "frexp", GMPy_Context_Frexp, METH_O, GMPy_doc_function_frexp }, 725 { "fsum", GMPy_Context_Fsum, METH_O, GMPy_doc_function_fsum }, 726 { "gamma", GMPy_Context_Gamma, METH_O, GMPy_doc_function_gamma }, 727 { "get_context", GMPy_CTXT_Get, METH_NOARGS, GMPy_doc_get_context }, 728 { "get_emax_max", GMPy_MPFR_get_emax_max, METH_NOARGS, GMPy_doc_mpfr_get_emax_max }, 729 { "get_emin_min", GMPy_MPFR_get_emin_min, METH_NOARGS, GMPy_doc_mpfr_get_emin_min }, 730 { "get_exp", GMPy_MPFR_get_exp, METH_O, GMPy_doc_mpfr_get_exp }, 731 { "get_max_precision", GMPy_MPFR_get_max_precision, METH_NOARGS, GMPy_doc_mpfr_get_max_precision }, 732 { "hypot", GMPy_Context_Hypot, METH_VARARGS, GMPy_doc_function_hypot }, 733 { "ieee", GMPy_CTXT_ieee, METH_O, GMPy_doc_context_ieee }, 734 { "inf", GMPy_MPFR_set_inf, METH_VARARGS, GMPy_doc_mpfr_set_inf }, 735 { "is_finite", GMPy_Context_Is_Finite, METH_O, GMPy_doc_function_is_finite }, 736 { "is_infinite", GMPy_Context_Is_Infinite, METH_O, GMPy_doc_function_is_infinite }, 737 { "is_integer", GMPy_Context_Is_Integer, METH_O, GMPy_doc_function_is_integer }, 738 { "is_lessgreater", GMPy_Context_Is_LessGreater, METH_VARARGS, GMPy_doc_function_is_lessgreater }, 739 { "is_nan", GMPy_Context_Is_NAN, METH_O, GMPy_doc_function_is_nan }, 740 { "is_regular", GMPy_Context_Is_Regular, METH_O, GMPy_doc_function_is_regular }, 741 { "is_signed", GMPy_Context_Is_Signed, METH_O, GMPy_doc_function_is_signed }, 742 { "is_unordered", GMPy_Context_Is_Unordered, METH_VARARGS, GMPy_doc_function_is_unordered }, 743 { "is_zero", GMPy_Context_Is_Zero, METH_O, GMPy_doc_function_is_zero }, 744 { "jn", GMPy_Context_Jn, METH_VARARGS, GMPy_doc_function_jn }, 745 { "j0", GMPy_Context_J0, METH_O, GMPy_doc_function_j0 }, 746 { "j1", GMPy_Context_J1, METH_O, GMPy_doc_function_j1 }, 747 { "lgamma", GMPy_Context_Lgamma, METH_O, GMPy_doc_function_lgamma }, 748 { "li2", GMPy_Context_Li2, METH_O, GMPy_doc_function_li2 }, 749 { "lngamma", GMPy_Context_Lngamma, METH_O, GMPy_doc_function_lngamma }, 750 { "local_context", (PyCFunction)GMPy_CTXT_Local, METH_VARARGS | METH_KEYWORDS, GMPy_doc_local_context }, 751 { "log", GMPy_Context_Log, METH_O, GMPy_doc_function_log }, 752 { "log1p", GMPy_Context_Log1p, METH_O, GMPy_doc_function_log1p }, 753 { "log10", GMPy_Context_Log10, METH_O, GMPy_doc_function_log10 }, 754 { "log2", GMPy_Context_Log2, METH_O, GMPy_doc_function_log2 }, 755 { "maxnum", GMPy_Context_Maxnum, METH_VARARGS, GMPy_doc_function_maxnum }, 756 { "minnum", GMPy_Context_Minnum, METH_VARARGS, GMPy_doc_function_minnum }, 757 { "modf", GMPy_Context_Modf, METH_O, GMPy_doc_function_modf }, 758 { "mpfr", (PyCFunction)GMPy_MPFR_Factory, METH_VARARGS | METH_KEYWORDS, GMPy_doc_mpfr_factory }, 759 { "mpfr_from_old_binary", GMPy_MPFR_From_Old_Binary, METH_O, doc_mpfr_from_old_binary }, 760 { "mpfr_random", GMPy_MPFR_random_Function, METH_VARARGS, GMPy_doc_mpfr_random_function }, 761 { "mpfr_grandom", GMPy_MPFR_grandom_Function, METH_VARARGS, GMPy_doc_mpfr_grandom_function }, 762 { "mul_2exp", GMPy_Context_Mul_2exp, METH_VARARGS, GMPy_doc_function_mul_2exp }, 763 { "nan", GMPy_MPFR_set_nan, METH_NOARGS, GMPy_doc_mpfr_set_nan }, 764 { "next_above", GMPy_Context_NextAbove, METH_O, GMPy_doc_function_next_above }, 765 { "next_below", GMPy_Context_NextBelow, METH_O, GMPy_doc_function_next_below }, 766 { "next_toward", GMPy_Context_NextToward, METH_VARARGS, GMPy_doc_function_next_toward }, 767 { "radians", GMPy_Context_Radians, METH_O, GMPy_doc_function_radians }, 768 { "rec_sqrt", GMPy_Context_RecSqrt, METH_O, GMPy_doc_function_rec_sqrt }, 769 { "reldiff", GMPy_Context_RelDiff, METH_VARARGS, GMPy_doc_function_reldiff }, 770 { "remainder", GMPy_Context_Remainder, METH_VARARGS, GMPy_doc_function_remainder }, 771 { "remquo", GMPy_Context_RemQuo, METH_VARARGS, GMPy_doc_function_remquo }, 772 { "rint", GMPy_Context_Rint, METH_O, GMPy_doc_function_rint }, 773 { "rint_ceil", GMPy_Context_RintCeil, METH_O, GMPy_doc_function_rint_ceil }, 774 { "rint_floor", GMPy_Context_RintFloor, METH_O, GMPy_doc_function_rint_floor }, 775 { "rint_round", GMPy_Context_RintRound, METH_O, GMPy_doc_function_rint_round }, 776 { "rint_trunc", GMPy_Context_RintTrunc, METH_O, GMPy_doc_function_rint_trunc }, 777 { "root", GMPy_Context_Root, METH_VARARGS, GMPy_doc_function_root }, 778 { "round_away", GMPy_Context_RoundAway, METH_O, GMPy_doc_function_round_away }, 779 { "round2", GMPy_Context_Round2, METH_VARARGS, GMPy_doc_function_round2 }, 780 { "sec", GMPy_Context_Sec, METH_O, GMPy_doc_function_sec }, 781 { "sech", GMPy_Context_Sech, METH_O, GMPy_doc_function_sech }, 782 { "set_context", GMPy_CTXT_Set, METH_O, GMPy_doc_set_context }, 783 { "set_exp", GMPy_MPFR_set_exp, METH_VARARGS, GMPy_doc_mpfr_set_exp }, 784 { "set_sign", GMPy_MPFR_set_sign, METH_VARARGS, GMPy_doc_mpfr_set_sign }, 785 { "sin", GMPy_Context_Sin, METH_O, GMPy_doc_function_sin }, 786 { "sin_cos", GMPy_Context_Sin_Cos, METH_O, GMPy_doc_function_sin_cos }, 787 { "sinh", GMPy_Context_Sinh, METH_O, GMPy_doc_function_sinh }, 788 { "sinh_cosh", GMPy_Context_Sinh_Cosh, METH_O, GMPy_doc_function_sinh_cosh }, 789 { "sqrt", GMPy_Context_Sqrt, METH_O, GMPy_doc_function_sqrt }, 790 { "tan", GMPy_Context_Tan, METH_O, GMPy_doc_function_tan }, 791 { "tanh", GMPy_Context_Tanh, METH_O, GMPy_doc_function_tanh }, 792 { "trunc", GMPy_Context_Trunc, METH_O, GMPy_doc_function_trunc}, 793 { "yn", GMPy_Context_Yn, METH_VARARGS, GMPy_doc_function_yn }, 794 { "y0", GMPy_Context_Y0, METH_O, GMPy_doc_function_y0 }, 795 { "y1", GMPy_Context_Y1, METH_O, GMPy_doc_function_y1 }, 796 { "zero", GMPy_MPFR_set_zero, METH_VARARGS, GMPy_doc_mpfr_set_zero }, 797 { "zeta", GMPy_Context_Zeta, METH_O, GMPy_doc_function_zeta }, 798 799 { "mpc", (PyCFunction)GMPy_MPC_Factory, METH_VARARGS | METH_KEYWORDS, GMPy_doc_mpc_factory }, 800 { "mpc_random", GMPy_MPC_random_Function, METH_VARARGS, GMPy_doc_mpc_random_function }, 801 { "norm", GMPy_Context_Norm, METH_O, GMPy_doc_function_norm }, 802 { "polar", GMPy_Context_Polar, METH_O, GMPy_doc_function_polar }, 803 { "phase", GMPy_Context_Phase, METH_O, GMPy_doc_function_phase }, 804 { "proj", GMPy_Context_Proj, METH_O, GMPy_doc_function_proj }, 805 { "rect", GMPy_Context_Rect, METH_VARARGS, GMPy_doc_function_rect }, 806 { NULL, NULL, 1} 807}; 808 809/* The custom memory allocation routines either use PyMem_* or the standard 810 * libraries. See gmpy.h for defines. 811 */ 812 813static void * 814gmpy_allocate(size_t size) 815{ 816 void *res; 817 818 if (!(res = GMPY_MALLOC(size))) 819 Py_FatalError("Insufficient memory"); 820 821 return res; 822} 823 824static void * 825gmpy_reallocate(void *ptr, size_t old_size, size_t new_size) 826{ 827 void *res; 828 829 if (!(res = GMPY_REALLOC(ptr, new_size))) 830 Py_FatalError("Insufficient memory"); 831 832 return res; 833} 834 835static void 836gmpy_free( void *ptr, size_t size) 837{ 838 GMPY_FREE(ptr); 839} 840 841static char _gmpy_docs[] = 842"gmpy2 2.1.0a0 - General Multiple-precision arithmetic for Python\n" 843"\n" 844"gmpy2 supports several multiple-precision libraries. Integer and\n" 845"rational arithmetic is provided by either the GMP or MPIR libraries.\n" 846"Real floating-point arithmetic is provided by the MPFR library.\n" 847"Complex floating-point arithmetic is provided by the MPC library.\n" 848"\n" 849"The integer type 'mpz' has comparable functionality to Python's\n" 850"builtin integers, but is faster for operations on large numbers.\n" 851"A wide variety of additional functions are provided:\n" 852" - bit manipulations\n" 853" - GCD, Extended GCD, LCM\n" 854" - Fibonacci and Lucas sequences\n" 855" - primality testing\n" 856" - powers and integer Nth roots\n" 857"\n" 858"The rational type 'mpq' is equivalent to Python's fractions\n" 859"module, but is faster.\n" 860"\n" 861"The real type 'mpfr' and complex type 'mpc' provide multiple-\n" 862"precision real and complex numbers with user-definable precision,\n" 863"rounding, and exponent range. All the advanced functions from the\n" 864"MPFR and MPC libraries are available.\n\ 865"; 866 867/* Notes on Python 3.x support: Full support for PEP-3121 has not been 868 * implemented. No per-module state has been defined. 869 */ 870 871#ifdef PY3 872#define INITERROR return NULL 873static struct PyModuleDef moduledef = { 874 PyModuleDef_HEAD_INIT, 875 "gmpy2", 876 _gmpy_docs, 877 -1, /*sizeof(struct module_state) */ 878 Pygmpy_methods, 879 NULL, 880 NULL, /* gmpy_traverse */ 881 NULL, /* gmpy_clear */ 882 NULL 883}; 884#else 885#define INITERROR return 886#endif 887 888#ifdef PY3 889PyMODINIT_FUNC PyInit_gmpy2(void) 890#else 891PyMODINIT_FUNC initgmpy2(void) 892#endif 893{ 894 PyObject* gmpy_module = NULL; 895 PyObject* copy_reg_module = NULL; 896 PyObject *temp = NULL; 897 898 /* Validate the sizes of the various typedef'ed integer types. */ 899 900#if defined _WIN64 && (MPIR || MSYS2) 901 if (sizeof(mp_bitcnt_t) != sizeof(PY_LONG_LONG)) { 902 SYSTEM_ERROR("Size of PY_LONG_LONG and mp_bitcnt_t not compatible (_WIN64 && MPIR)"); 903 INITERROR; 904 } 905#else 906 if (sizeof(mp_bitcnt_t) != sizeof(long)) { 907 SYSTEM_ERROR("Size of long and mp_bitcnt_t not compatible"); 908 INITERROR; 909 } 910#endif 911 912 if (sizeof(mp_bitcnt_t) > sizeof(size_t)) { 913 SYSTEM_ERROR("Size of size_t and mp_bitcnt_t not compatible"); 914 INITERROR; 915 } 916 917 if (sizeof(mpfr_prec_t) != sizeof(long)) { 918 SYSTEM_ERROR("Size of mpfr_prec_t and long not compatible"); 919 INITERROR; 920 } 921 922 if (sizeof(mpfr_exp_t) != sizeof(long)) { 923 SYSTEM_ERROR("Size of mpfr_exp_t and long not compatible"); 924 INITERROR; 925 } 926 927 /* Initialize the types. */ 928 if (PyType_Ready(&MPZ_Type) < 0) 929 INITERROR; 930 if (PyType_Ready(&MPQ_Type) < 0) 931 INITERROR; 932 if (PyType_Ready(&XMPZ_Type) < 0) 933 INITERROR; 934 if (PyType_Ready(&GMPy_Iter_Type) < 0) 935 INITERROR; 936 if (PyType_Ready(&MPFR_Type) < 0) 937 INITERROR; 938 if (PyType_Ready(&CTXT_Type) < 0) 939 INITERROR; 940 if (PyType_Ready(&CTXT_Manager_Type) < 0) 941 INITERROR; 942 if (PyType_Ready(&MPC_Type) < 0) 943 INITERROR; 944 945 /* Initialize the custom memory handlers. */ 946 mp_set_memory_functions(gmpy_allocate, gmpy_reallocate, gmpy_free); 947 948 /* Initialize object caching. */ 949 set_zcache(); 950 set_gmpympzcache(); 951 set_gmpympqcache(); 952 set_gmpyxmpzcache(); 953 set_gmpympfrcache(); 954 set_gmpympccache(); 955 956 /* Initialize exceptions. */ 957 GMPyExc_GmpyError = PyErr_NewException("gmpy2.gmpyError", 958 PyExc_ArithmeticError, NULL); 959 if (!GMPyExc_GmpyError) 960 INITERROR; 961 962 GMPyExc_Erange = PyErr_NewException("gmpy2.RangeError", 963 GMPyExc_GmpyError, NULL); 964 if (!GMPyExc_Erange) 965 INITERROR; 966 967 GMPyExc_Inexact = PyErr_NewException("gmpy2.InexactResultError", 968 GMPyExc_GmpyError, NULL); 969 if (!GMPyExc_Inexact) 970 INITERROR; 971 972 GMPyExc_Overflow = PyErr_NewException("gmpy2.OverflowResultError", 973 GMPyExc_Inexact, NULL); 974 if (!GMPyExc_Overflow) 975 INITERROR; 976 977 GMPyExc_Underflow = PyErr_NewException("gmpy2.UnderflowResultError", 978 GMPyExc_Inexact, NULL); 979 if (!GMPyExc_Underflow) 980 INITERROR; 981 982 temp = PyTuple_Pack(2, GMPyExc_GmpyError, PyExc_ValueError); 983 if (!temp) 984 INITERROR; 985 GMPyExc_Invalid = PyErr_NewException("gmpy2.InvalidOperationError", 986 temp, NULL); 987 Py_DECREF(temp); 988 if (!GMPyExc_Invalid) 989 INITERROR; 990 991 temp = PyTuple_Pack(2, GMPyExc_GmpyError, PyExc_ZeroDivisionError); 992 if (!temp) 993 INITERROR; 994 GMPyExc_DivZero = PyErr_NewException("gmpy2.DivisionByZeroError", 995 temp, NULL); 996 Py_DECREF(temp); 997 if (!GMPyExc_DivZero) 998 INITERROR; 999 1000 1001#ifdef PY3 1002 gmpy_module = PyModule_Create(&moduledef); 1003#else 1004 gmpy_module = Py_InitModule3("gmpy2", Pygmpy_methods, _gmpy_docs); 1005#endif 1006 1007 if (gmpy_module == NULL) 1008 INITERROR; 1009 1010 /* Initialize thread local contexts. */ 1011#ifdef WITHOUT_THREADS 1012 module_context = (CTXT_Object*)GMPy_CTXT_New(); 1013 if (!module_context) 1014 INITERROR; 1015 Py_INCREF(Py_False); 1016 if (PyModule_AddObject(gmpy_module, "HAVE_THREADS", Py_False) < 0) { 1017 Py_DECREF(Py_False); 1018 INITERROR; 1019 } 1020#else 1021 tls_context_key = PyUnicode_FromString("__GMPY2_CTX__"); 1022 Py_INCREF(Py_True); 1023 if (PyModule_AddObject(gmpy_module, "HAVE_THREADS", Py_True) < 0) { 1024 Py_DECREF(Py_True); 1025 INITERROR; 1026 } 1027#endif 1028 1029 /* Add the constants for defining rounding modes. */ 1030 if (PyModule_AddIntConstant(gmpy_module, "RoundToNearest", MPFR_RNDN) < 0) 1031 INITERROR; 1032 if (PyModule_AddIntConstant(gmpy_module, "RoundToZero", MPFR_RNDZ) < 0) 1033 INITERROR; 1034 if (PyModule_AddIntConstant(gmpy_module, "RoundUp", MPFR_RNDU) < 0) 1035 INITERROR; 1036 if (PyModule_AddIntConstant(gmpy_module, "RoundDown", MPFR_RNDD) < 0) 1037 INITERROR; 1038 if (PyModule_AddIntConstant(gmpy_module, "RoundAwayZero", MPFR_RNDA) < 0) 1039 INITERROR; 1040 if (PyModule_AddIntConstant(gmpy_module, "Default", GMPY_DEFAULT) < 0) 1041 INITERROR; 1042 1043 /* Add the exceptions. */ 1044 Py_INCREF(GMPyExc_DivZero); 1045 if (PyModule_AddObject(gmpy_module, "DivisionByZeroError", GMPyExc_DivZero) < 0) { 1046 Py_DECREF(GMPyExc_DivZero); 1047 INITERROR; 1048 } 1049 Py_INCREF(GMPyExc_Inexact); 1050 if (PyModule_AddObject(gmpy_module, "InexactResultError", GMPyExc_Inexact) < 0) { 1051 Py_DECREF(GMPyExc_Inexact); 1052 INITERROR; 1053 } 1054 Py_INCREF(GMPyExc_Invalid); 1055 if (PyModule_AddObject(gmpy_module, "InvalidOperationError", GMPyExc_Invalid) < 0 ) { 1056 Py_DECREF(GMPyExc_Invalid); 1057 INITERROR; 1058 } 1059 Py_INCREF(GMPyExc_Overflow); 1060 if (PyModule_AddObject(gmpy_module, "OverflowResultError", GMPyExc_Overflow) < 0) { 1061 Py_DECREF(GMPyExc_Overflow); 1062 INITERROR; 1063 } 1064 Py_INCREF(GMPyExc_Underflow); 1065 if (PyModule_AddObject(gmpy_module, "UnderflowResultError", GMPyExc_Underflow) < 0) { 1066 Py_DECREF(GMPyExc_Underflow); 1067 INITERROR; 1068 } 1069 Py_INCREF(GMPyExc_Erange); 1070 if (PyModule_AddObject(gmpy_module, "RangeError", GMPyExc_Erange) < 0) { 1071…
Large files files are truncated, but you can click here to view the full file