Python library for arbitrary-precision floating-point arithmetic
—
Matrix operations, linear system solving, eigenvalue computations, matrix decompositions, and matrix functions supporting arbitrary precision arithmetic.
Creating matrices and basic operations.
class matrix:
"""
Matrix class supporting arbitrary precision arithmetic.
Args:
*args: Matrix dimensions, data, or another matrix
"""
def __init__(self, *args): ...
def __getitem__(self, key): ...
def __setitem__(self, key, value): ...
def __str__(self) -> str: ...
def __repr__(self) -> str: ...
def eye(n):
"""
Create n×n identity matrix.
Args:
n: Matrix size
Returns:
n×n identity matrix
"""
def diag(diagonal):
"""
Create diagonal matrix from list of diagonal elements.
Args:
diagonal: List of diagonal elements
Returns:
Diagonal matrix
"""
def zeros(m, n=None):
"""
Create matrix of zeros.
Args:
m: Number of rows
n: Number of columns (default: same as m)
Returns:
m×n zero matrix
"""
def ones(m, n=None):
"""
Create matrix of ones.
Args:
m: Number of rows
n: Number of columns (default: same as m)
Returns:
m×n matrix of ones
"""
def hilbert(n):
"""
Create n×n Hilbert matrix.
Args:
n: Matrix size
Returns:
Hilbert matrix H[i,j] = 1/(i+j+1)
"""
def randmatrix(m, n=None):
"""
Create random matrix.
Args:
m: Number of rows
n: Number of columns (default: same as m)
Returns:
m×n random matrix
"""
def swap_row(A, i, j):
"""
Swap rows i and j in matrix A.
Args:
A: Matrix to modify
i, j: Row indices to swap
"""
def extend(A, b):
"""
Extend matrix A with column vector b.
Args:
A: Matrix
b: Column vector
Returns:
Extended matrix [A|b]
"""Various norms for vectors and matrices.
def norm(x, p=2):
"""
Compute vector or matrix norm.
Args:
x: Vector or matrix
p: Norm type (1, 2, inf, 'fro' for Frobenius)
Returns:
Norm of x
"""
def mnorm(A, p=1):
"""
Compute matrix norm.
Args:
A: Matrix
p: Norm type (1, 2, inf, 'fro')
Returns:
Matrix norm
"""
def unitvector(n, i):
"""
Create unit vector with 1 in position i.
Args:
n: Vector length
i: Position of 1 (0-indexed)
Returns:
Unit vector
"""Functions to compute matrix properties.
def det(A):
"""
Compute matrix determinant.
Args:
A: Square matrix
Returns:
Determinant of A
"""
def cond(A, p=None):
"""
Compute condition number of matrix.
Args:
A: Matrix
p: Norm type (default: 2-norm)
Returns:
Condition number ||A|| * ||A^(-1)||
"""
def residual(A, x, b):
"""
Compute residual ||Ax - b|| of linear system.
Args:
A: Coefficient matrix
x: Solution vector
b: Right-hand side
Returns:
Residual norm
"""Matrix factorizations for solving linear systems and analysis.
def lu(A):
"""
LU decomposition with partial pivoting.
Args:
A: Matrix to decompose
Returns:
tuple: (P, L, U) where PA = LU
"""
def qr(A):
"""
QR decomposition.
Args:
A: Matrix to decompose
Returns:
tuple: (Q, R) where A = QR
"""
def cholesky(A):
"""
Cholesky decomposition of positive definite matrix.
Args:
A: Positive definite matrix
Returns:
Lower triangular matrix L such that A = LL^T
"""
def hessenberg(A):
"""
Reduce matrix to Hessenberg form.
Args:
A: Square matrix
Returns:
tuple: (P, H) where P^T A P = H (upper Hessenberg)
"""
def schur(A):
"""
Schur decomposition.
Args:
A: Square matrix
Returns:
tuple: (Q, T) where A = QTQ^T (T upper triangular)
"""Solve linear systems Ax = b using various methods.
def lu_solve(A, b):
"""
Solve linear system using LU decomposition.
Args:
A: Coefficient matrix
b: Right-hand side vector
Returns:
Solution vector x such that Ax = b
"""
def qr_solve(A, b):
"""
Solve linear system using QR decomposition.
Args:
A: Coefficient matrix
b: Right-hand side vector
Returns:
Solution vector x such that Ax = b
"""
def cholesky_solve(A, b):
"""
Solve linear system using Cholesky decomposition.
Args:
A: Positive definite coefficient matrix
b: Right-hand side vector
Returns:
Solution vector x such that Ax = b
"""
def inverse(A):
"""
Compute matrix inverse.
Args:
A: Invertible square matrix
Returns:
Inverse matrix A^(-1)
"""Compute eigenvalues and eigenvectors.
def eig(A):
"""
Compute eigenvalues and eigenvectors.
Args:
A: Square matrix
Returns:
tuple: (eigenvalues, eigenvectors)
"""
def eig_sort(A):
"""
Compute eigenvalues and eigenvectors, sorted by eigenvalue magnitude.
Args:
A: Square matrix
Returns:
tuple: (sorted_eigenvalues, sorted_eigenvectors)
"""
def eigsy(A):
"""
Compute eigenvalues of symmetric matrix.
Args:
A: Symmetric matrix
Returns:
List of eigenvalues
"""
def eighe(A):
"""
Compute eigenvalues of Hermitian matrix.
Args:
A: Hermitian matrix
Returns:
List of eigenvalues
"""
def eigh(A):
"""
Compute eigenvalues and eigenvectors of Hermitian matrix.
Args:
A: Hermitian matrix
Returns:
tuple: (eigenvalues, eigenvectors)
"""SVD and related decompositions.
def svd(A):
"""
Singular Value Decomposition.
Args:
A: Matrix to decompose
Returns:
tuple: (U, S, V) where A = U*diag(S)*V^T
"""
def svd_r(A):
"""
SVD for real matrices.
Args:
A: Real matrix
Returns:
tuple: (U, S, V) singular value decomposition
"""
def svd_c(A):
"""
SVD for complex matrices.
Args:
A: Complex matrix
Returns:
tuple: (U, S, V) singular value decomposition
"""Functions of matrices (matrix exponential, logarithm, etc.).
def expm(A):
"""
Matrix exponential exp(A).
Args:
A: Square matrix
Returns:
Matrix exponential e^A
"""
def sqrtm(A):
"""
Matrix square root.
Args:
A: Square matrix
Returns:
Matrix square root (principal branch)
"""
def powm(A, n):
"""
Matrix power A^n.
Args:
A: Square matrix
n: Exponent
Returns:
Matrix power A^n
"""
def logm(A):
"""
Matrix logarithm log(A).
Args:
A: Square matrix
Returns:
Matrix logarithm (principal branch)
"""
def sinm(A):
"""
Matrix sine sin(A).
Args:
A: Square matrix
Returns:
Matrix sine
"""
def cosm(A):
"""
Matrix cosine cos(A).
Args:
A: Square matrix
Returns:
Matrix cosine
"""Gauss quadrature rules for numerical integration.
def gauss_quadrature(n, a=-1, b=1):
"""
Generate Gauss-Legendre quadrature rule.
Args:
n: Number of quadrature points
a: Lower integration limit (default: -1)
b: Upper integration limit (default: 1)
Returns:
tuple: (nodes, weights) for quadrature rule
"""import mpmath
from mpmath import mp
# Set precision
mp.dps = 25
# Create matrices
A = mp.matrix([[1, 2], [3, 4]])
print(f"Matrix A:\n{A}")
# Identity matrix
I = mp.eye(3)
print(f"3×3 Identity:\n{I}")
# Random matrix
R = mp.randmatrix(2, 3)
print(f"Random 2×3 matrix:\n{R}")
# Matrix operations
B = mp.matrix([[5, 6], [7, 8]])
C = A + B
print(f"A + B:\n{C}")
D = A * B
print(f"A * B:\n{D}")
# Determinant
det_A = mp.det(A)
print(f"det(A) = {det_A}")
# Matrix inverse
A_inv = mp.inverse(A)
print(f"A^(-1):\n{A_inv}")
# Verify inverse
product = A * A_inv
print(f"A * A^(-1):\n{product}")
# Solve linear system Ax = b
b = mp.matrix([1, 2])
x = mp.lu_solve(A, b)
print(f"Solution to Ax = b: {x}")
# Verify solution
residual = mp.norm(A * x - b)
print(f"Residual ||Ax - b|| = {residual}")
# Eigenvalues and eigenvectors
eigenvals, eigenvecs = mp.eig(A)
print(f"Eigenvalues: {eigenvals}")
print(f"Eigenvectors:\n{eigenvecs}")
# SVD
U, S, V = mp.svd(A)
print(f"SVD - U:\n{U}")
print(f"SVD - S: {S}")
print(f"SVD - V:\n{V}")
# Matrix functions
exp_A = mp.expm(A)
print(f"Matrix exponential exp(A):\n{exp_A}")
# Norms
frobenius_norm = mp.norm(A, 'fro')
print(f"Frobenius norm of A: {frobenius_norm}")
# Condition number
cond_A = mp.cond(A)
print(f"Condition number of A: {cond_A}")
# QR decomposition
Q, R = mp.qr(A)
print(f"QR - Q:\n{Q}")
print(f"QR - R:\n{R}")
# Verify QR decomposition
QR_product = Q * R
print(f"Q * R:\n{QR_product}")
print(f"Difference from A:\n{A - QR_product}")