qrunch.chemistry.qrackle.matrix_modules.numpy_matrix_module
Numpy implementation of the MatrixModuleProtocol.
Classes
Numpy implementation of |
|
Dense numpy implementation of |
|
Numpy implementation of |
- class NumpyMatrix
Bases:
MatrixProtocolNumpy implementation of
MatrixProtocol.- __init__(data: NDArray[float64]) None
- Parameters:
data (NDArray[float64])
- Return type:
None
- data: NDArray[float64]
- fill_from_raw_numpy_array(array: NDArray[float64]) None
Fill the matrix with data from a raw numpy array.
- Parameters:
array (NDArray[float64]) – Two-dimensional numpy array with the correct shape.
- Return type:
None
- property shape: tuple[int, int]
Shape of the matrix.
- to_raw_numpy_array() NDArray[float64]
Contents of the matrix as a dense raw numpy array.
- Return type:
NDArray[float64]
- class NumpyMatrixModule
Bases:
MatrixModuleProtocol[NumpyMatrix,NumpyVector]Dense numpy implementation of
MatrixModuleProtocol.The module also keeps track of every matrix and vector it allocates and can report the currently live objects together with the allocation call-site. Memory tracking is automatic; allocation-stack capture can be disabled to avoid the per-allocation
tracebackcost in tight loops.- __init__(*, track_allocation_stack: bool = True, stack_capture_depth: int = 12) None
Initialise the module and its allocation tracker.
- Parameters:
track_allocation_stack (bool) – When
True(default) capture a Python call-stack snapshot at every allocation. Set toFalseto only count allocations and bytes.stack_capture_depth (int) – Maximum number of stack frames captured per allocation when
track_allocation_stackis enabled.
- Return type:
None
- add(left: NumpyMatrix, right: NumpyMatrix) NumpyMatrix
Calculate the element-wise sum \(L + R\).
- Parameters:
left (NumpyMatrix) – First summand.
right (NumpyMatrix) – Second summand of identical shape.
- Return type:
- allocation_report() str
Get a human-readable, multi-line summary of all live allocations.
The summary includes per-object shape, byte count and (when enabled) the call stack captured at the allocation site.
- Return type:
str
- antisymmetrize(matrix: NumpyMatrix) NumpyMatrix
Calculate the antisymmetric projection \(\tfrac{1}{2}(M - M^{T})\).
- Parameters:
matrix (NumpyMatrix) – Square matrix to be antisymmetrized.
- Return type:
- commutator(left: NumpyMatrix, right: NumpyMatrix) NumpyMatrix
Calculate the commutator \([L, R] = L R - R L\).
- Parameters:
left (NumpyMatrix) – Left operand.
right (NumpyMatrix) – Right operand of compatible shape.
- Return type:
- copy_matrix(matrix: NumpyMatrix) NumpyMatrix
Create an independent copy of
matrix.- Parameters:
matrix (NumpyMatrix) – Matrix to copy.
- Return type:
- diagonalize(matrix: NumpyMatrix) EigenvalueSolution[NumpyMatrix, NumpyVector]
Solve \(A v = \\lambda v\) for a symmetric matrix.
- Parameters:
matrix (NumpyMatrix) – Symmetric square matrix.
- Return type:
- free_matrix(matrix: NumpyMatrix) None
Release the bookkeeping entry for
matrix.For the numpy backend the actual memory is owned by the underlying
numpy.ndarrayand is reclaimed by the Python garbage collector once no Python reference remains. This method removes the module-level reference held for allocation tracking.- Parameters:
matrix (NumpyMatrix) – Matrix previously returned by this module.
- Return type:
None
- free_vector(vector: NumpyVector) None
Release the bookkeeping entry for
vector.See
free_matrix()for semantics.- Parameters:
vector (NumpyVector) – Vector previously returned by this module.
- Return type:
None
- frobenius_inner_product(left: NumpyMatrix, right: NumpyMatrix) float
Calculate the Frobenius inner product \(\sum_{i,j} L_{i,j} R_{i,j}\).
- Parameters:
left (NumpyMatrix) – First matrix.
right (NumpyMatrix) – Second matrix of identical shape.
- Return type:
float
- frobenius_norm(matrix: NumpyMatrix) float
Calculate the Frobenius norm \(\| M \|_{F} = \sqrt{\sum_{i,j} M_{i,j}^{2}}\).
- Parameters:
matrix (NumpyMatrix) – Matrix whose Frobenius norm is requested.
- Return type:
float
- generalized_eigenvalue_solver(a_matrix: NumpyMatrix, b_matrix: NumpyMatrix) EigenvalueSolution[NumpyMatrix, NumpyVector]
Solve \(A v = \\lambda B v\) for symmetric \(A\) and SPD \(B\).
- Parameters:
a_matrix (NumpyMatrix) – Symmetric square matrix \(A\).
b_matrix (NumpyMatrix) – Symmetric positive-definite square matrix \(B\).
- Return type:
- identity(dimension: int) NumpyMatrix
Get the square identity matrix of the given dimension.
- Parameters:
dimension (int) – Number of rows and columns.
- Return type:
- init_matrix(shape: tuple[int, int], *, zeroes: bool) NumpyMatrix
Allocate a new dense numpy matrix.
- Parameters:
shape (tuple[int, int]) – Desired
(number_of_rows, number_of_columns).zeroes (bool) – If
Truethe matrix is initialized to zero.
- Return type:
- init_vector(length: int, *, zeroes: bool) NumpyVector
Allocate a new dense numpy vector.
- Parameters:
length (int) – Desired number of elements.
zeroes (bool) – If
Truethe vector is initialized to zero.
- Return type:
- inverse(matrix: NumpyMatrix) NumpyMatrix
Calculate the inverse \(M^{-1}\).
- Parameters:
matrix (NumpyMatrix) – Square non-singular matrix.
- Return type:
- linear_combination(coefficients: list[float], matrices: list[NumpyMatrix]) NumpyMatrix
Calculate the linear combination \(\sum_i c_i M_i\).
- Parameters:
coefficients (list[float]) – Real coefficients.
matrices (list[NumpyMatrix]) – Matrices of identical shape; must be non-empty and of the same length as
coefficients.
- Return type:
- lowdin_orthogonalizer(overlap: NumpyMatrix, threshold: float = 1e-09) NumpyMatrix
Construct the symmetric (Lowdin) orthogonalizer \(X = S^{-1/2}\).
Eigenvectors whose overlap eigenvalue is below
thresholdare dropped, projecting out near-linear-dependent directions.- Parameters:
overlap (NumpyMatrix) – Symmetric positive-definite overlap matrix \(S\).
threshold (float) – Eigenvalue cutoff for near-linear-dependence removal.
- Return type:
- matrix_exponential(matrix: NumpyMatrix) NumpyMatrix
Calculate the matrix exponential \(\exp(M)\) using SciPy.
- Parameters:
matrix (NumpyMatrix) – Square matrix.
- Return type:
- matrix_multiply(left: NumpyMatrix, right: NumpyMatrix) NumpyMatrix
Calculate the matrix product \(L R\).
- Parameters:
left (NumpyMatrix) – Left factor of shape
(m, k).right (NumpyMatrix) – Right factor of shape
(k, n).
- Return type:
- metadata_hash(matrix: NumpyMatrix) str
Calculate a stable metadata hash for a matrix.
- Parameters:
matrix (NumpyMatrix) – Matrix whose contents should be hashed.
- Return type:
str
- new_matrix(array: NDArray[float64]) NumpyMatrix
Allocate an independently owned matrix containing a copy of the input array.
- Parameters:
array (NDArray[float64]) – Two-dimensional numpy array whose shape and contents are copied.
- Return type:
- property number_of_live_allocations: int
Number of currently tracked (not yet freed) objects.
- resized_copy(matrix: NumpyMatrix, *, shape: tuple[int, int]) NumpyMatrix
Create a new matrix of the given shape holding the overlapping top-left block of
matrix.- Parameters:
matrix (NumpyMatrix) – Source matrix.
shape (tuple[int, int]) – Shape of the result as (rows, columns).
- Return type:
- scale(matrix: NumpyMatrix, scalar: float) NumpyMatrix
Scale the matrix by a scalar \(cM\).
- Parameters:
matrix (NumpyMatrix) – Matrix to scale.
scalar (float) – Real scaling factor.
- Return type:
- shifted_floored_divide(numerator: NumpyMatrix, denominator: NumpyMatrix, *, shift: float, denominator_floor: float) NumpyMatrix
Divide by shifted, sign-preserving floored denominators without modifying the inputs.
- Parameters:
numerator (NumpyMatrix) – Numerator matrix.
denominator (NumpyMatrix) – Denominator matrix of identical shape.
shift (float) – Finite scalar subtracted from every denominator.
denominator_floor (float) – Finite positive minimum denominator magnitude.
- Return type:
- slice_columns(matrix: NumpyMatrix, start_column: int, end_column: int) NumpyMatrix
Create a new matrix containing columns
start_columntoend_column(exclusive).- Parameters:
matrix (NumpyMatrix) – Source matrix.
start_column (int) – First column index (inclusive).
end_column (int) – Last column index (exclusive).
- Return type:
- solve_linear_system(lhs_matrix: NumpyMatrix, rhs_matrix: NumpyMatrix) NumpyMatrix
Solve \(A X = B\) for \(X\).
- Parameters:
lhs_matrix (NumpyMatrix) – Square non-singular matrix \(A\).
rhs_matrix (NumpyMatrix) – Right-hand-side matrix \(B\).
- Return type:
- subtract(left: NumpyMatrix, right: NumpyMatrix) NumpyMatrix
Calculate the element-wise difference \(L - R\).
- Parameters:
left (NumpyMatrix) – Minuend.
right (NumpyMatrix) – Subtrahend of identical shape.
- Return type:
- symmetrize(matrix: NumpyMatrix) NumpyMatrix
Calculate the symmetric projection \(\tfrac{1}{2}(M + M^{T})\).
- Parameters:
matrix (NumpyMatrix) – Square matrix to be symmetrized.
- Return type:
- property total_allocated_bytes: int
Total number of bytes used by currently tracked objects.
- trace(matrix: NumpyMatrix) float
Calculate the trace \(\mathrm{Tr}(M) = \sum_{i} M_{i,i}\).
- Parameters:
matrix (NumpyMatrix) – Square matrix.
- Return type:
float
- trace_of_product(left: NumpyMatrix, right: NumpyMatrix) float
Calculate the trace \(\mathrm{Tr}(L R) = \sum_{i,k} L_{i,k} R_{k,i}\).
- Parameters:
left (NumpyMatrix) – Left factor of shape
(m, n).right (NumpyMatrix) – Right factor of shape
(n, m).
- Return type:
float
- transpose(matrix: NumpyMatrix) NumpyMatrix
Calculate the transpose \(M^{T}\).
- Parameters:
matrix (NumpyMatrix) – Matrix to transpose.
- Return type:
- vector_outer_difference(left: NumpyVector, right: NumpyVector) NumpyMatrix
Create the matrix with elements \(L_i - R_j\).
- Parameters:
left (NumpyVector) – Vector determining the rows.
right (NumpyVector) – Vector determining the columns.
- Return type:
- class NumpyVector
Bases:
VectorProtocolNumpy implementation of
VectorProtocol.- __init__(data: NDArray[float64]) None
- Parameters:
data (NDArray[float64])
- Return type:
None
- data: NDArray[float64]
- fill_from_raw_numpy_array(array: NDArray[float64]) None
Fill the vector with data from a raw numpy array.
- Parameters:
array (NDArray[float64]) – One-dimensional numpy array with
lengthelements.- Return type:
None
- property length: int
Number of elements of the vector.
- to_raw_numpy_array() NDArray[float64]
Contents of the vector as a raw numpy array.
- Return type:
NDArray[float64]