### gmpy2 Installation Guide Source: https://gmpy2.readthedocs.io/en/latest/history Instructions for installing the gmpy2 library, a Python extension for arbitrary-precision arithmetic. ```text Installation This section details how to install gmpy2, which is a Python extension module that supports arbitrary precision arithmetic. ``` -------------------------------- ### gmpy2 Tutorial Source: https://gmpy2.readthedocs.io/en/latest/search A tutorial section designed to guide users through the basic usage and features of the gmpy2 library. It helps new users get started with the library's functionalities. ```python # Tutorial content would typically include examples of: # - Creating and manipulating arbitrary-precision integers (mpz) # - Performing arithmetic operations # - Using contexts for precision control # - Working with floating-point numbers (mpfr) and complex numbers (mpc) ``` -------------------------------- ### Install gmpy2 from Git Repository Source: https://gmpy2.readthedocs.io/en/latest/install Clones the gmpy2 git repository and installs it in editable mode, including optional dependencies like 'tests' for developers. ```bash git clone git://github.com/aleaxit/gmpy.git cd gmpy pip install -e .[tests] ``` -------------------------------- ### gmpy2 Installation Guide Source: https://gmpy2.readthedocs.io/en/latest/search Instructions and information related to installing the gmpy2 library. This is crucial for users to set up the library for use in their Python projects. ```bash # pip install gmpy2 # (Installation commands are typical for Python packages) ``` -------------------------------- ### Install gmpy2 Dependencies on Debian Source: https://gmpy2.readthedocs.io/en/latest/install Installs the required development libraries (GMP, MPFR, MPC) on Debian-based systems for building gmpy2 from source. ```bash sudo apt install libgmp-dev libmpfr-dev libmpc-dev ``` -------------------------------- ### Run gmpy2 Unit Tests Source: https://gmpy2.readthedocs.io/en/latest/install Executes the full suite of unit tests for gmpy2 using pytest to verify the installation and functionality. ```bash pytest test/ ``` -------------------------------- ### Install gmpy2 using pip Source: https://gmpy2.readthedocs.io/en/latest/install Installs the latest release of the gmpy2 library using pip. This is the recommended method for most users. ```bash pip install gmpy2 ``` -------------------------------- ### gmpy2 Tutorial Source: https://gmpy2.readthedocs.io/en/latest/history A tutorial to help users get started with the gmpy2 library for arbitrary-precision arithmetic. ```text Tutorial This tutorial provides a step-by-step guide to using the gmpy2 library, covering basic operations and features. ``` -------------------------------- ### Install gmpy2 using pip Source: https://gmpy2.readthedocs.io/en/latest/_sources/install.rst Installs the latest release of the gmpy2 library using pip. This is the recommended method for most users. ```bash pip install gmpy2 ``` -------------------------------- ### Install gmpy2 from Git Repository Source: https://gmpy2.readthedocs.io/en/latest/_sources/install.rst Clones the gmpy2 git repository and installs it in editable mode using pip. This is for developers who want the latest updates. It also includes optional 'tests' dependencies. ```bash git clone git://github.com/aleaxit/gmpy.git cd gmpy pip install -e .[tests] ``` -------------------------------- ### Install gmpy2 Dependencies on Windows using MSYS2 Source: https://gmpy2.readthedocs.io/en/latest/install Installs necessary development tools and libraries (GCC, GMP, MPFR, MPC, setuptools, pip) on Windows using MSYS2 for building gmpy2 from source. ```bash pacman -S gcc gmp-devel mpfr-devel mpc-devel python-setuptools python-pip ``` -------------------------------- ### Run gmpy2 Unit Tests Source: https://gmpy2.readthedocs.io/en/latest/_sources/install.rst Executes the full suite of unit tests for gmpy2 using the pytest framework. This is a crucial step to ensure the library is functioning correctly after installation, especially when building from source or git. ```bash pytest test/ ``` -------------------------------- ### Install gmpy2 Dependencies on Debian Source: https://gmpy2.readthedocs.io/en/latest/_sources/install.rst Installs the required development libraries (GMP, MPFR, MPC) on Debian-based systems using apt. These are necessary for building gmpy2 from source. ```bash sudo apt install libgmp-dev libmpfr-dev libmpc-dev ``` -------------------------------- ### Install gmpy2 Dependencies on MSYS2 (Windows) Source: https://gmpy2.readthedocs.io/en/latest/_sources/install.rst Installs the necessary development tools and libraries (GCC, GMP, MPFR, MPC, setuptools, pip) on Windows using MSYS2's pacman package manager. This is for building gmpy2 from source on Windows. ```bash pacman -S gcc gmp-devel mpfr-devel mpc-devel python-setuptools python-pip ``` -------------------------------- ### Install Specific gmpy2 Version using pip Source: https://gmpy2.readthedocs.io/en/latest/install Installs a specific version of the gmpy2 library using pip. Useful for ensuring compatibility with other project dependencies. ```bash pip install gmpy2==2.1.5 ``` -------------------------------- ### Cython Setup for gmpy2 Extension Source: https://gmpy2.readthedocs.io/en/latest/_sources/cython.rst Provides a minimal setup.py file for compiling a Cython file that depends on the gmpy2 library. It configures the extension module, specifies the Cython file, and includes necessary libraries and include paths for gmpy2. ```python import sys from setuptools import Extension, setup from Cython.Build import cythonize ext = Extension("test_gmpy2", ["test_gmpy2.pyx"], include_dirs=sys.path, libraries=['gmp', 'mpfr', 'mpc']) setup(name="cython_gmpy_test", ext_modules=cythonize([ext], include_path=sys.path)) ``` -------------------------------- ### gmpy2 Library Overview Source: https://gmpy2.readthedocs.io/en/latest/tutorial This section provides an overview of the gmpy2 library, a Python library for arbitrary-precision arithmetic. It covers installation, tutorials, and advanced topics related to various numerical types. ```python import gmpy2 # Example usage (conceptual, actual usage depends on library functions) # gmpy2.mpz(100) + gmpy2.mpz(200) # gmpy2.mpfr('1.2345') # gmpy2.mpc(3+4j) ``` -------------------------------- ### gmpy2 API Documentation Source: https://gmpy2.readthedocs.io/en/latest/_sources/history.rst This section provides API documentation for gmpy2, including method signatures, parameter descriptions, return values, and usage examples. It covers various functionalities like context management, number conversions, prime number checks, and mathematical operations. ```APIDOC gmpy2 API Documentation: --- **Module: gmpy2** * **`gmpy2.context`** * Exposes the context type for managing gmpy2 contexts. * **`gmpy2.xmpz.bit_count()`** * Adds the `bit_count` method to `xmpz`. * **`gmpy2.mpz.as_integer_ratio()`** * Adds `as_integer_ratio` method to `mpz`. * **`gmpy2.mpz.to_bytes()`** * Adds `to_bytes` method to `mpz`. * **`gmpy2.mpz.from_bytes()`** * Adds `from_bytes` method to `mpz`. * **`gmpy2.is_probab_prime()`** * Directly exposes the GMP behavior for probabilistic primality testing. * **`gmpy2.gcd(*integers)`** * Computes the greatest common divisor of multiple integers. * Uses vectorcall protocol. * **`gmpy2.lcm(*integers)`** * Computes the least common multiple of multiple integers. * Uses vectorcall protocol. * **`gmpy2.prev_prime(n)`** * Returns the largest prime less than or equal to n. * Requires GMP version >= 6.3. * **`gmpy2.jn(n, x)`** * Computes the Bessel function of the first kind of order n at x. * Argument order changed to match MPFR. * **`gmpy2.yn(n, x)`** * Computes the Bessel function of the second kind of order n at x. * Argument order changed to match MPFR. * **`gmpy2.is_extra_strong_lucas_prp(n)`** * Fixes documentation and code for this function. * **`gmpy2.powmod_sec(base, exponent, modulus)`** * Computes modular exponentiation securely. * Releases the GIL. * **`gmpy2.powmod_base_list(base_list, exponent, modulus)`** * Computes modular exponentiation for a list of bases. * Experimental. * **`gmpy2.powmod_exp_list(base, exponent_list, modulus)`** * Computes modular exponentiation for a list of exponents. * Experimental. * **`gmpy2.cmp(x, y)`** * Compares two numbers and returns -1, 0, or 1. * **`gmpy2.cmp_abs(x, y)`** * Compares the absolute values of two numbers. * **`gmpy2.inverse(a, m)`** * Computes the modular multiplicative inverse of a modulo m. * Raises an exception if the inverse does not exist. * **`gmpy2.f2q(x)`** * Converts a float to a rational number. * Fixed incorrect results. * **`gmpy2.mpfr(x)`** * Converts x to an mpfr number. * Handles 'inf', 'nan', and '-0' correctly when subnormalization is enabled. * **`gmpy2.mpq(x)`** * Converts x to an mpq number. * Returns exact result when converting from mpfr. * **`gmpy2.mpz(x)`** * Converts x to an mpz number. * Calls `int(x)` if direct conversion is not known. * Uses `__new__` for type conversion. * **`gmpy2.is_prime(n)`** * Checks if n is prime. * Fixes `mpz(-3).is_prime()`. * `is_prime(-2)` now returns `False`. * **`gmpy2.powmod(base, exponent, modulus)`** * Computes modular exponentiation. * Releases the GIL. * **`gmpy2.iroot(x, n)`** * Computes the integer nth root of x. * Fixes error messages for large n. * **`gmpy2.factorial(n)`** * Computes the factorial of n. * Avoids MPFR bug in `mfr_fac_ui` for specific platforms and arguments. --- **Exceptions:** * **`gmpy2.InvalidOperation`** * Raised for invalid operations like addition with incompatible types. ``` -------------------------------- ### gmpy2 Library Overview Source: https://gmpy2.readthedocs.io/en/latest/search This section provides an overview of the gmpy2 library, a Python library for arbitrary-precision arithmetic. It serves as a starting point for understanding the library's purpose and capabilities. ```python import gmpy2 # Example usage (conceptual, as no specific code is provided in the text) # gmpy2.mpz(100) + gmpy2.mpz(200) # gmpy2.mpfr('1.2345') # gmpy2.mpc(1+2j) ``` -------------------------------- ### Install Specific gmpy2 Version using pip Source: https://gmpy2.readthedocs.io/en/latest/_sources/install.rst Installs a specific version of the gmpy2 library using pip. Replace '2.1.5' with the desired version number. ```bash pip install gmpy2==2.1.5 ``` -------------------------------- ### gmpy2 mpq Type Methods Source: https://gmpy2.readthedocs.io/en/latest/mpq Provides documentation for various methods associated with the mpq type in gmpy2, used for representing rational numbers. These methods cover operations like converting to integer ratios, getting digits, and checking for integer properties. ```APIDOC gmpy2.mpq.as_integer_ratio() - Returns the rational number as a pair of integers (numerator, denominator). gmpy2.mpq.conjugate() - Returns the conjugate of the rational number (which is the number itself for rationals). gmpy2.mpq.digits(n=None) - Returns the digits of the rational number. If n is provided, returns the first n digits. gmpy2.mpq.from_decimal(decimal_value) - Creates an mpq object from a decimal.Decimal object. gmpy2.mpq.from_float(float_value) - Creates an mpq object from a float. gmpy2.mpq.is_integer() - Returns True if the rational number is an integer, False otherwise. gmpy2.mpq.limit_denominator(max_denominator=1000000) - Finds and returns the closest rational to self that has denominator at most max_denominator. gmpy2.mpq.denominator - The denominator of the rational number. gmpy2.mpq.imag - The imaginary part of the rational number (always 0 for mpq). gmpy2.mpq.numerator - The numerator of the rational number. gmpy2.mpq.real - The real part of the rational number (which is the number itself for rationals). ``` -------------------------------- ### gmpy2 Cython Usage Source: https://gmpy2.readthedocs.io/en/latest/history Guide on using gmpy2 with Cython for performance optimization. ```text Cython usage This section explains how to integrate and use gmpy2 within Cython projects for enhanced performance. ``` -------------------------------- ### Arithmetic Operations with Custom Objects Source: https://gmpy2.readthedocs.io/en/latest/_sources/conversion.rst Illustrates gmpy2's support for arithmetic operations between gmpy2 number types (mpz, mpq, mpfr) and custom Python objects that implement the necessary conversion methods (__mpz__, __mpq__, __mpfr__, __mpc__). Shows examples of addition and multiplication. ```python import gmpy2 from gmpy2 import mpz, mpq, mpfr, mpc gmpy2.set_context(gmpy2.context()) class Q: def __mpz__(self): return mpz(1) def __mpq__(self): return mpq(3,2) q = Q() print(mpz(2) + q) print(mpq(1,2) * q) print(mpfr(10) * q) ``` -------------------------------- ### Minimal setup.py for Cython gmpy2 Module Source: https://gmpy2.readthedocs.io/en/latest/cython A setup.py script to compile a Cython file that uses the gmpy2 library, specifying include paths and linking necessary libraries. ```python import sys from setuptools import Extension, setup from Cython.Build import cythonize ext = Extension("test_gmpy2", ["test_gmpy2.pyx"], include_dirs=sys.path, libraries=['gmp', 'mpfr', 'mpc']) setup(name="cython_gmpy_test", ext_modules=cythonize([ext], include_path=sys.path)) ``` -------------------------------- ### Minimal Cython File for gmpy2 Source: https://gmpy2.readthedocs.io/en/latest/cython A basic Cython file demonstrating the use of gmpy2 C-API functions, including initialization and basic operations. ```cython from gmpy2cimport * cdef extern from "gmp.h": void mpz_set_si(mpz_t, long) import_gmpy2() # needed to initialize the C-API cdef mpz z = GMPy_MPZ_New(NULL) mpz_set_si(MPZ(z), -7) print(z + 3) ``` -------------------------------- ### Floating-Point Arithmetic and Context Management Source: https://gmpy2.readthedocs.io/en/latest/_sources/tutorial.rst Illustrates how to use gmpy2's arbitrary precision real (mpfr) and complex (mpc) types, control precision and rounding modes, handle division by zero, and manage context settings using context managers. ```python from gmpy2 import * # Basic mpfr usage mpfr('1.2') mpfr(float('1.2')) # Context management for precision and rounding ctx = get_context() ctx.precision ctx.precision = 100 mpfr('1.2') mpfr(float('1.2')) ctx.precision = 53 ctx.round = RoundUp const_pi() ctx.round = RoundToNearest const_pi() # Handling division by zero ctx.divzero mpfr(1)/0 ctx.trap_divzero = True # mpfr(1)/0 # This would raise gmpy2.DivisionByZeroError ctx.divzero # Complex number support ctx.allow_complex = True sqrt(mpfr(-2)) # Using context managers print(const_pi()) with context(precision=100) as ctx: print(const_pi()) ctx.precision += 20 print(const_pi()) print(const_pi()) # Setting different precisions for real and imaginary parts ctx.real_prec = 60 ctx.imag_prec = 70 sqrt(mpc('1+2j')) ``` -------------------------------- ### xmpz Bit Iteration Examples Source: https://gmpy2.readthedocs.io/en/latest/_sources/advmpz.rst Illustrates using iter_bits, iter_clear, and iter_set methods of the gmpy2 xmpz type to iterate over bit states and positions. Supports start and stop arguments for range specification. ```python >>> a=xmpz(117) >>> bin(a) '0b1110101' >>> list(a.iter_bits()) [True, False, True, False, True, True, True] >>> list(a.iter_clear()) [1, 3] >>> list(a.iter_set()) [0, 2, 4, 5, 6] >>> list(a.iter_bits(stop=12)) [True, False, True, False, True, True, True, False, False, False, False, False] ``` -------------------------------- ### xmpz Bit Manipulation Examples Source: https://gmpy2.readthedocs.io/en/latest/_sources/advmpz.rst Demonstrates setting, clearing, and reversing bits using slice notation with the gmpy2 xmpz type. Shows how to pad with zeros and invert slices. ```python >>> from gmpy2 import xmpz >>> a = xmpz(123) >>> b = a >>> a += 1 >>> a xmpz(124) >>> b xmpz(124) ``` ```python >>> a=xmpz(0) >>> a[8:16] = ~0 >>> bin(a) '0b1111111100000000' >>> a[4:12] = ~a[4:12] >>> bin(a) '0b1111000011110000' ``` ```python >>> a = xmpz(1148) >>> bin(a) '0b10001111100' >>> a[::] = a[::-1] >>> bin(a) '0b111110001' ``` -------------------------------- ### gmpy2 Default Type Initialization Source: https://gmpy2.readthedocs.io/en/latest/history Starting from version 2.0.0b4, gmpy2's arbitrary-precision types (`mpz`, `mpq`, `mpfr`, `mpc`, `xmpz`) now return 0 of their respective types when called without arguments. This change standardizes the initialization behavior. ```python If no arguments are given to `mpz`, `mpq`, `mpfr`, `mpc`, and `xmpz`, return 0 of the appropriate type. ``` -------------------------------- ### gmpy2 mpq Methods Source: https://gmpy2.readthedocs.io/en/latest/genindex Documentation for methods of the gmpy2.mpq (rational number) type. ```APIDOC gmpy2.mpq.digits() Returns the current number of digits for rational numbers. ``` -------------------------------- ### Arbitrary Precision Integer and Rational Arithmetic Source: https://gmpy2.readthedocs.io/en/latest/_sources/tutorial.rst Demonstrates basic arithmetic operations with gmpy2's arbitrary precision integer (mpz) and rational (mpq) types, including multiplication, exponentiation, GCD, LCM, division, and prime checking. ```python from gmpy2 import * mpz(99) * 43 pow(mpz(99), 37, 59) isqrt(99) isqrt_rem(99) gcd(123, 27) lcm(123, 27) (mpz(123) + 12) / 5 (mpz(123) + 12) // 5 (mpz(123) + 12) / 5.0 mpz('123') + 1 10 - mpz(1) is_prime(17) mpz('1_000_000') mpq(3, 7)/7 mpq(45, 3) * mpq(11, 8) mpq(1, 7) * 11 ``` -------------------------------- ### gmpy2 mpq Type Documentation Source: https://gmpy2.readthedocs.io/en/latest/mpq Provides comprehensive documentation for the gmpy2.mpq type, including its constructors, methods like as_integer_ratio, conjugate, digits, from_decimal, from_float, is_integer, and limit_denominator, as well as its properties like numerator, denominator, real, and imag. ```APIDOC gmpy2.mpq(_n =0_, _/_) Constructs a rational number from a non-complex number n. gmpy2.mpq(_n_ , _m_ , _/_) Constructs a rational number from a pair of Rational values n and m. gmpy2.mpq(_s_ , _/_, _base =10_) Constructs a rational number from a string s made up of digits in the given base. - If base is 0, leading characters determine the base for numerator and denominator. - If base=10, strings accepted by float constructor are also accepted. as_integer_ratio() → tuple[mpz, mpz] Returns a pair of integers (numerator, denominator) whose ratio is exactly equal to the original number. The ratio is in lowest terms and has a positive denominator. conjugate() → mpz Returns the conjugate of x (which is just a new reference to x since x is not a complex number). digits(_base =10_, _/_) → str Returns a Python string representing x in the given base (2 to 62, default is 10). A leading ‘-’ is present if x<0, but no leading ‘+’ is present if x>=0. classmethod from_decimal(_dec_) → mpq Converts a finite decimal.Decimal instance to a rational number, exactly. classmethod from_float(_f_) → mpq Converts a finite float to a rational number, exactly. is_integer() → bool Returns True if x is an integer. limit_denominator(_max_denominator =1000000_) → mpq Returns the closest fraction to self with a denominator at most max_denominator. denominator The denominator of a rational number in lowest terms. imag The imaginary part of a complex number. numerator The numerator of a rational number in lowest terms. real The real part of a complex number. ``` -------------------------------- ### gmpy2 Build and Dependency Information Source: https://gmpy2.readthedocs.io/en/latest/_sources/history.rst Provides information on build options, including allowing builds with just GMP/MPIR if MPFR is not available, or with GMP/MPIR and MPFR if MPC is not available. ```python # Allow building with just GMP/MPIR if MPFR not available # ./configure --enable-gmp --disable-mpfr # Allow building with GMP/MPIR and MPFR if MPC not available # ./configure --enable-gmp --enable-mpfr --disable-mpc ``` -------------------------------- ### gmpy2 G Functions Source: https://gmpy2.readthedocs.io/en/latest/genindex This section details gmpy2 functions starting with 'G'. It includes mathematical functions like gamma (gamma, gamma_inc), greatest common divisor (gcd, gcdext), and functions for retrieving context and configuration information (get_context, get_emax_max, get_emin_min, get_exp, get_max_precision). ```python import gmpy2 # Gamma functions gamma_result = gmpy2.gamma(x) gamma_inc_result = gmpy2.gamma_inc(a, x) # Greatest Common Divisor gcd_result = gmpy2.gcd(a, b) gcdext_result = gmpy2.gcdext(a, b) # Context and configuration context = gmpy2.get_context() emax, emin = gmpy2.get_emax_max() # Precision and exponent information max_precision = gmpy2.get_max_precision() exponent_val = gmpy2.get_exp(x) ``` -------------------------------- ### gmpy2 Context Methods Source: https://gmpy2.readthedocs.io/en/latest/genindex Documentation for various methods available within the gmpy2 context, including mathematical operations and attribute access. ```APIDOC gmpy2.context.abs() - Returns the absolute value of a number. gmpy2.context.acos() - Returns the arc cosine of a number. gmpy2.context.acosh() - Returns the inverse hyperbolic cosine of a number. gmpy2.context.add() - Adds two numbers. gmpy2.context.agm() - Computes the arithmetic-geometric mean. gmpy2.context.ai() - Computes the Airy function Ai(x). gmpy2.context.allow_complex (attribute) - Controls whether complex numbers are allowed. gmpy2.context.allow_release_gil (attribute) - Controls whether the GIL can be released during operations. gmpy2.context.asin() - Returns the arc sine of a number. gmpy2.context.asinh() - Returns the inverse hyperbolic sine of a number. gmpy2.context.atan() - Returns the arc tangent of a number. gmpy2.context.atan2() - Returns atan(y/x). gmpy2.context.atanh() - Returns the inverse hyperbolic tangent of a number. ``` -------------------------------- ### gmpy2 Context Methods Source: https://gmpy2.readthedocs.io/en/latest/genindex Documentation for various methods available on gmpy2 context objects, including numerical operations and context management. ```APIDOC gmpy2.context.maxnum() Returns the maximum representable number in the current context. gmpy2.context.minnum() Returns the minimum representable number in the current context. gmpy2.context.minus(x) Returns the negative of a number. Parameters: x: The number to negate. gmpy2.context.mod(x, y) Returns the modulus of x with respect to y. Parameters: x: The dividend. y: The divisor. gmpy2.context.modf(x) Returns the fractional and integer parts of a number. Parameters: x: The number to split. gmpy2.context.mul(x, y) Returns the product of x and y. Parameters: x: The first number. y: The second number. gmpy2.context.mul_2exp(x, n) Multiplies a number by 2 raised to the power of n. Parameters: x: The number to multiply. n: The exponent. gmpy2.context.next_above(x) Returns the next representable number above x. Parameters: x: The number. gmpy2.context.next_below(x) Returns the next representable number below x. Parameters: x: The number. gmpy2.context.next_toward(x, y) Returns the next representable number toward y from x. Parameters: x: The starting number. y: The target number. gmpy2.context.norm(x) Normalizes a number. Parameters: x: The number to normalize. ``` -------------------------------- ### gmpy2 Bit Manipulation Methods (mpz and xmpz) Source: https://gmpy2.readthedocs.io/en/latest/genindex Provides bit manipulation functionalities for gmpy2's arbitrary-precision integers (mpz) and extended arbitrary-precision integers (xmpz). These include clearing, counting, flipping, getting length, masking, scanning for zero or one bits, setting, and testing bits. ```python import gmpy2 # Example for mpz num_mpz = gmpy2.mpz(10) # Binary: 1010 # bit_clear(num_mpz, 1) # Clears the bit at position 1 # bit_count(num_mpz) # Counts the number of set bits # bit_flip(num_mpz, 0) # Flips the bit at position 0 # bit_length(num_mpz) # Returns the number of bits required to represent the number # bit_mask(num_mpz, 3) # Creates a bitmask # bit_scan0(num_mpz, 0) # Finds the first zero bit starting from position 0 # bit_scan1(num_mpz, 0) # Finds the first one bit starting from position 0 # bit_set(num_mpz, 2) # Sets the bit at position 2 # bit_test(num_mpz, 3) # Tests if the bit at position 3 is set # Example for xmpz (similar usage) # num_xmpz = gmpy2.xmpz(20) # gmpy2.xmpz.bit_clear(num_xmpz, 2) ``` -------------------------------- ### gmpy2 Context Methods Source: https://gmpy2.readthedocs.io/en/latest/genindex Documentation for methods associated with the gmpy2 context, including mathematical functions and attribute access. ```APIDOC gmpy2.context.degrees() Returns the current degree setting. gmpy2.context.digamma() Computes the digamma function. gmpy2.context.digits() Returns the current number of digits. gmpy2.context.div() Performs division. gmpy2.context.div_2exp() Divides by a power of 2. gmpy2.context.divmod() Performs division and returns quotient and remainder. gmpy2.context.eint() Converts to an integer with exponent. gmpy2.context.erf() Computes the error function. gmpy2.context.erfc() Computes the complementary error function. gmpy2.context.exp() Computes the exponential function. gmpy2.context.exp10() Computes the base-10 exponential function. gmpy2.context.exp2() Computes the base-2 exponential function. gmpy2.context.expm1() Computes exp(x) - 1. ``` -------------------------------- ### gmpy2 xmpz Methods Source: https://gmpy2.readthedocs.io/en/latest/genindex Documentation for methods of the gmpy2.xmpz (extended arbitrary precision integer) type. ```APIDOC gmpy2.xmpz.digits() Returns the current number of digits for extended integers. ``` -------------------------------- ### gmpy2 mpz Bit Manipulation Functions Source: https://gmpy2.readthedocs.io/en/latest/mpz Provides functions for manipulating bits within mpz integers. This includes clearing, flipping, setting, and testing specific bits, as well as counting set bits and finding the length of the binary representation. It also includes functions for finding the next zero or one bit starting from a given index. ```python gmpy2.bit_clear(_x_, _n_) # Return a copy of x with the n-th bit cleared. gmpy2.bit_count(_x_) # Return the number of 1-bits set in abs(x). gmpy2.bit_flip(_x_, _n_) # Return a copy of x with the n-th bit inverted. gmpy2.bit_length(_x_) # Return the number of significant bits in the radix-2 representation of x. gmpy2.bit_mask(_n_) # Return an mpz exactly n bits in length with all bits set. gmpy2.bit_scan0(_x_, _n_=0) # Return the index of the first 0-bit of x with index >= n. gmpy2.bit_scan1(_x_, _n_=0) # Return the index of the first 1-bit of x with index >= n. gmpy2.bit_set(_x_, _n_) # Return a copy of x with the n-th bit set. gmpy2.bit_test(_x_, _n_) # Return the value of the n-th bit of x. ``` -------------------------------- ### gmpy2 Rationals Source: https://gmpy2.readthedocs.io/en/latest/mpz Documentation for handling rational numbers using the gmpy2 library. This section covers the creation and manipulation of fractions. ```python Refer to the gmpy2.mpq documentation for details on rational number operations. ``` -------------------------------- ### gmpy2 F Functions Source: https://gmpy2.readthedocs.io/en/latest/genindex This section covers various functions in the gmpy2 library that start with the letter 'F'. It includes functions for fractional conversions (f2q), division and modulo operations (f_div, f_div_2exp, f_divmod, f_divmod_2exp, f_mod, f_mod_2exp), factorial calculations (fac, factorial), Fibonacci sequences (fib, fib2), floor operations (floor, floor_div), fused multiply-add operations (fma, fmma), and floating-point related functions (fmod, fms, frac, free_cache, frexp, from_binary, from_bytes, from_decimal, from_float, fsum). ```python import gmpy2 # Fractional conversion quotient, remainder = gmpy2.f2q(number) # Division and modulo operations result_div = gmpy2.f_div(a, b) result_div_2exp = gmpy2.f_div_2exp(a, b) quotient_divmod, remainder_divmod = gmpy2.f_divmod(a, b) quotient_divmod_2exp, remainder_divmod_2exp = gmpy2.f_divmod_2exp(a, b) result_mod = gmpy2.f_mod(a, b) result_mod_2exp = gmpy2.f_mod_2exp(a, b) # Factorial fact_result = gmpy2.fac(n) # Fibonacci fib_result = gmpy2.fib(n) fib2_result = gmpy2.fib2(n) # Floor operations floor_result = gmpy2.floor(x) floor_div_result = gmpy2.floor_div(a, b) # Fused multiply-add fma_result = gmpy2.fma(a, b, c) fmma_result = gmpy2.fmma(a, b, c, d) # Floating-point operations fmod_result = gmpy2.fmod(x, y) fms_result = gmpy2.fms(a, b, c) frac_result = gmpy2.frac(x) gmpy2.free_cache() exponent, significand = gmpy2.frexp(x) # Conversions from_binary_result = gmpy2.from_binary(binary_string) from_bytes_result = gmpy2.mpz.from_bytes(byte_data) from_decimal_result = gmpy2.mpq.from_decimal(decimal_number) from_float_result = gmpy2.mpq.from_float(float_number) # Summation fsum_result = gmpy2.fsum(iterable) ``` -------------------------------- ### gmpy2 mpz Methods Source: https://gmpy2.readthedocs.io/en/latest/genindex Documentation for methods of the gmpy2.mpz (arbitrary precision integer) type. ```APIDOC gmpy2.mpz.digits() Returns the current number of digits for integers. ``` -------------------------------- ### gmpy2 MPZ Methods Source: https://gmpy2.readthedocs.io/en/latest/genindex Documentation for methods related to the gmpy2.mpz class, including random number generation and limb manipulation. ```APIDOC gmpy2.mpz_random(x, y) Generates a random mpz number. Parameters: x: The upper bound for the random number. y: The lower bound for the random number. gmpy2.mpz_rrandomb(x, y) Generates a random mpz number with a specified bit length. Parameters: x: The number of bits for the random number. y: The mpz object to store the random number. gmpy2.mpz_urandomb(x, y) Generates an unsigned random mpz number with a specified bit length. Parameters: x: The number of bits for the random number. y: The mpz object to store the random number. gmpy2.mpz.num_digits(x) Returns the number of decimal digits in an mpz number. Parameters: x: The mpz number. gmpy2.mpz.numerator Attribute for the numerator of an mpz number. ``` -------------------------------- ### gmpy2 Object Creation Functions Source: https://gmpy2.readthedocs.io/en/latest/cython Functions to create new gmpy2 objects (mpz, mpq, mpfr, mpc) with optional context and precision settings. ```APIDOC GMPy_MPZ_New(void * ctx) Creates a new mpz object from a given context ctx. GMPy_MPQ_New(void * ctx) Creates a new mpq object from a given context ctx. MPFR_New(void * ctx, mpfr_prec_t prec) Creates a new mpfr object with given context ctx and precision prec. MPC_New(void * ctx, mpfr_prec_t rprec, mpfr_prec_t iprec) Creates a new mpc object with given context ctx, precisions rprec and iprec of respectively real and imaginary parts. The context can be set to NULL and controls the default behavior (e.g. precision). GMPy_MPZ_From_mpz(mpz_srcptr z) Returns a new mpz object with a given mpz_t value z. GMPy_MPQ_From_mpq(mpq_srcptr q) Returns a new mpq object from a given mpq_t value q. GMPy_MPQ_From_mpz(mpz_srcptr num, mpz_srcptr den) Returns a new mpq object with a given mpz_t numerator num and mpz_t denominator den. GMPy_MPFR_From_mpfr(mpfr_srcptr x) Returns a new mpfr object with a given mpfr_t value x. GMPy_MPC_From_mpc(mpc_srcptr c) Returns a new mpc object with a given mpc_t value c. GMPy_MPC_From_mpfr(mpfr_srcptr re, mpfr_srcptr im) Returns a new mpc object with a given mpfr_t real part re and mpfr_t imaginary part im. ``` -------------------------------- ### gmpy2 MPC Methods Source: https://gmpy2.readthedocs.io/en/latest/genindex Documentation for methods related to the gmpy2.mpc class, including random number generation and version information. ```APIDOC gmpy2.mpc_random(x, y) Generates a random mpc number. Parameters: x: The upper bound for the random number. y: The lower bound for the random number. gmpy2.mpc_version() Returns the version of the MPC library. ``` -------------------------------- ### gmpy2 Formatting and Conversion Methods Source: https://gmpy2.readthedocs.io/en/latest/history New methods for enhanced formatting and number conversions, including __format__(), as_integer_ratio(), as_mantissa_exp(), and as_simple_fraction(). ```python # Added support for __format__() # Added as_integer_ratio(), as_mantissa_exp(), as_simple_fraction() ``` -------------------------------- ### gmpy2 Contexts Source: https://gmpy2.readthedocs.io/en/latest/history Information on managing contexts for arbitrary-precision arithmetic in gmpy2. ```text Contexts This section explains how to manage and utilize contexts for controlling precision and rounding in gmpy2 operations. ``` -------------------------------- ### gmpy2 Library Overview Source: https://gmpy2.readthedocs.io/en/latest/index This section provides an overview of the gmpy2 library, its purpose, and the underlying libraries it supports. It highlights gmpy2's role as a successor to the original gmpy module and its added support for MPFR and MPC libraries. ```text gmpy2 is a C-coded Python extension module that supports multiple-precision arithmetic. gmpy2 adds support for the MPFR (correctly rounded real floating-point arithmetic) and MPC (correctly rounded complex floating-point arithmetic) libraries. Supported libraries: * GMP for integer and rational arithmetic () * MPFR () * MPC () ``` -------------------------------- ### gmpy2 xmpz Methods Source: https://gmpy2.readthedocs.io/en/latest/genindex Documentation for methods related to the gmpy2.xmpz class, an extended arbitrary precision integer type. ```APIDOC gmpy2.xmpz.make_mpz(x) Creates an xmpz object from various input types. Parameters: x: The input value. gmpy2.xmpz.num_digits() Returns the number of decimal digits in an xmpz number. gmpy2.xmpz.num_limbs() Returns the number of limbs used to store an xmpz number. gmpy2.xmpz.numerator Attribute for the numerator of an xmpz number. ``` -------------------------------- ### gmpy2 Rationals (mpq) Source: https://gmpy2.readthedocs.io/en/latest/search Documentation for gmpy2's arbitrary-precision rational number support (mpq). This includes how to create, manipulate, and perform operations on rational numbers. ```python # Examples of mpq usage: # gmpy2.mpq(3, 4) # gmpy2.mpq('1.5') # gmpy2.fdiv_q(gmpy2.mpz(5), gmpy2.mpz(2)) ``` -------------------------------- ### gmpy2 General Functions Source: https://gmpy2.readthedocs.io/en/latest/genindex Documentation for general utility functions in the gmpy2 library, including setting numerical contexts, converting numbers, and retrieving version information. ```APIDOC gmpy2.set_context(ctx) - Sets the current numerical context. - Parameters: - ctx: The gmpy2 context object to set. gmpy2.set_exp(x, exp) - Sets the exponent of a number. - Parameters: - x: The number whose exponent to set. - exp: The new exponent value. gmpy2.set_sign(x, sign) - Sets the sign of a number. - Parameters: - x: The number whose sign to set. - sign: The new sign (0 for positive, 1 for negative). gmpy2.sign(x) - Returns the sign of a number. - Parameters: - x: The number to check. - Returns: The sign of the number (0 for positive, 1 for negative). gmpy2.to_binary(x) - Converts a number to its binary string representation. - Parameters: - x: The number to convert. - Returns: A string representing the binary form of the number. gmpy2.version() - Returns the version of the gmpy2 library. - Returns: A string containing the gmpy2 version. ``` -------------------------------- ### gmpy2 0.6 Changes Source: https://gmpy2.readthedocs.io/en/latest/history Summarizes changes in gmpy2 version 0.6, including fixes for kronecker and mpq_abs, workarounds for scan0/scan1 bugs in gmp 3.1.1, addition of qdiv, global.fcoform for float2mpf, set_fcoform function, caching of mpq objects, power for mpq, stern-brocot algorithm for mpf->mpq, and exposure of mpf ceil, floor, and trunc. ```text Fixed bugs in kronecker and mpq_abs. Workaround for scan0/scan1 bugs in gmp 3.1.1. Added qdiv; anynum->mpq substituted for all such conversions. Added global.fcoform for optional use of intermediate string in float2mpf. Added set_fcoform function. General cleanup of sources; added alloca for msvc++. Added caching of mpq objects. Power for mpq. Stern-brocot algorithm for mpf->mpq (also exposed as f2q). Added getrprec method to mpf, getrprec module-function. Exposed ceil, floor and trunc methods/functions for mpf's. Changed exceptions from value to zerodivision. Added 'qual' and 'floa' options to gmpy.rand. ``` -------------------------------- ### gmpy2 mpz Formatting and Conversion Source: https://gmpy2.readthedocs.io/en/latest/mpz Provides methods for formatting mpz integers into strings with various options and converting them into their simplest fractional representation. ```APIDOC __format__(fmt) → str Formats the mpz integer 'x' using the format string 'fmt'. Format string components: - Alignment: '<' (left), '>' (right), '^' (center) - Sign: '+' (always show sign), '-' (only for negative), ' ' (space for positive) - Base Indicator: '#' (prefix with 0b, 0o, 0x) - Width: Specifies the minimum field width. - Conversion: 'd' (decimal), 'b' (binary), 'o' (octal), 'x' (hex), 'X' (uppercase hex). Default format is 'd'. as_integer_ratio() → tuple[mpz, mpz] Returns a tuple of two mpz integers representing the exact ratio of the number in lowest terms with a positive denominator. ``` -------------------------------- ### gmpy2 mpfr Methods Source: https://gmpy2.readthedocs.io/en/latest/genindex Documentation for methods of the gmpy2.mpfr (arbitrary precision floating-point) type. ```APIDOC gmpy2.mpfr.digits() Returns the current number of digits for mpfr numbers. ```