or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

arithmetic.mdbit-operations.mdcontext.mddata-types.mdindex.mdmath-functions.mdnumber-theory.mdrandom.mdutilities.md
tile.json

tessl/pypi-gmpy2

Multiple-precision arithmetic library providing fast GMP, MPFR, and MPC interfaces for Python

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
pypipkg:pypi/gmpy2@2.2.x

To install, run

npx @tessl/cli install tessl/pypi-gmpy2@2.2.0

index.mddocs/

gmpy2

gmpy2 is an optimized, C-coded Python extension module that provides fast multiple-precision arithmetic operations. It interfaces with the GNU Multiple Precision Arithmetic Library (GMP), the Multiple Precision Floating-Point Reliable Library (MPFR), and the Multiple Precision Complex Library (MPC) to deliver high-performance arbitrary-precision arithmetic for integers, rational numbers, real numbers, and complex numbers.

Package Information

  • Package Name: gmpy2
  • Language: Python
  • Installation: pip install gmpy2
  • Documentation: https://gmpy2.readthedocs.io/en/latest/
  • Version: 2.2.1

Core Imports

import gmpy2

Import specific data types and functions:

from gmpy2 import mpz, mpq, mpfr, mpc, get_context, set_context

Import everything:

from gmpy2 import *

Basic Usage

import gmpy2
from gmpy2 import mpz, mpq, mpfr, mpc

# Multiple-precision integers
big_int = mpz("123456789012345678901234567890")
print(big_int * 2)  # 246913578024691357802469135780

# Rational numbers with exact representation
rational = mpq(22, 7)  # Approximation of pi
print(rational)  # 22/7
print(float(rational))  # 3.142857142857143

# High-precision floating-point numbers
with gmpy2.local_context(precision=100):  # 100-bit precision
    x = mpfr("1.23456789012345678901234567890")
    y = gmpy2.sqrt(x)
    print(y)  # High-precision square root

# Complex numbers with arbitrary precision
z = mpc("3.14159+2.71828j")
print(gmpy2.phase(z))  # Complex phase/argument

Architecture

gmpy2 provides a comprehensive multi-precision arithmetic system built on three main components:

  • GMP Integration: Fast arbitrary-precision integers and rationals through mpz and mpq types
  • MPFR Integration: Correctly rounded floating-point arithmetic with configurable precision via mpfr type
  • MPC Integration: Complex number arithmetic with independent real/imaginary precision control via mpc type
  • Context System: Thread-safe precision, rounding mode, and exception control for all operations
  • Python Integration: Seamless integration with Python's numeric protocols, operators, and built-in functions

This design enables gmpy2 to serve as the foundation for computational mathematics, scientific computing, and cryptographic applications requiring precision beyond Python's native numeric types.

Capabilities

Multiple-Precision Data Types

Core data types providing arbitrary-precision arithmetic: integers (mpz), mutable integers (xmpz), rationals (mpq), floating-point numbers (mpfr), and complex numbers (mpc).

class mpz:
    def __init__(self, x=0, base=10): ...
    def bit_length(self) -> int: ...
    def is_prime(self, n=25) -> bool: ...

class mpq:
    def __init__(self, x=0, y=1): ...
    @property
    def numerator(self) -> mpz: ...
    @property
    def denominator(self) -> mpz: ...

class mpfr:
    def __init__(self, x=0.0, precision=None): ...
    @property
    def precision(self) -> int: ...
    def is_finite(self) -> bool: ...

class mpc:
    def __init__(self, real=0, imag=0, precision=None): ...
    @property
    def real(self) -> mpfr: ...
    @property
    def imag(self) -> mpfr: ...

Data Types

Arithmetic Operations

Basic and advanced arithmetic operations with context-aware precision and rounding control for all numeric types.

def add(x, y): ...
def sub(x, y): ...
def mul(x, y): ...
def div(x, y): ...
def powmod(x, y, z): ...
def square(x): ...

Arithmetic

Number Theory

Comprehensive number theory functions including GCD/LCM, modular arithmetic, primality testing, factorization helpers, and integer sequences.

def gcd(*args): ...
def lcm(*args): ...
def is_prime(x, n=25) -> bool: ...
def next_prime(x): ...
def invert(x, y): ...
def bincoef(n, k): ...
def fac(n): ...
def fib(n): ...

Number Theory

Mathematical Functions

Extensive collection of mathematical functions including trigonometry, logarithms, exponentials, special functions, and constants with configurable precision.

def sqrt(x): ...
def sin(x): ...
def cos(x): ...
def exp(x): ...
def log(x): ...
def gamma(x): ...
def const_pi(precision=None): ...
def const_euler(precision=None): ...

Mathematical Functions

Bit Operations

Efficient bit manipulation operations for integer types including setting, clearing, testing bits, and bitwise logical operations.

def bit_set(x, n): ...
def bit_clear(x, n): ...
def bit_test(x, n) -> bool: ...
def bit_count(x) -> int: ...
def bit_length(x) -> int: ...
def hamdist(x, y) -> int: ...

Bit Operations

Context Management

Precision and rounding control system providing thread-safe computation contexts with configurable precision, rounding modes, and exception handling.

class context:
    def __init__(self, precision=53, round=RoundToNearest, **kwargs): ...
    def __enter__(self): ...
    def __exit__(self, *args): ...

def get_context() -> context: ...
def set_context(ctx: context): ...
def local_context(**kwargs): ...

Context Management

Random Numbers

Random number generation for all gmpy2 types with multiple algorithms and configurable random state management.

class random_state:
    def __init__(self, seed=0): ...

def mpz_random(state, n): ...
def mpz_urandomb(state, n): ...  
def mpfr_random(state): ...
def mpc_random(state): ...

Random Numbers

Utility Functions

Version information, binary serialization, floating-point utilities, and system information functions.

def version() -> str: ...
def mp_version() -> str: ...
def mpfr_version() -> str: ...
def mpc_version() -> str: ...
def mp_limbsize() -> int: ...
def license() -> str: ...

def to_binary(x): ...
def from_binary(s): ...
def pack(x, precision, emin, emax): ...
def unpack(s, precision, emin, emax): ...

def get_max_precision() -> int: ...
def get_emax_max() -> int: ...
def get_emin_min() -> int: ...
def free_cache(): ...

Utility Functions

Constants

# Rounding modes
RoundToNearest: int
RoundToZero: int
RoundUp: int
RoundDown: int
RoundAwayZero: int
Default: int

# Version information
__version__: str

Exceptions

class DivisionByZeroError(ArithmeticError): ...
class InexactResultError(ArithmeticError): ...
class InvalidOperationError(ArithmeticError): ...
class OverflowResultError(ArithmeticError): ...
class UnderflowResultError(ArithmeticError): ...
class RangeError(ArithmeticError): ...