Python module to calculate matrix and minor-matrix determinants from NumPy arrays.
This project is for exploration purposes only. Same results, but much faster can be achieved by using sympy.Matrix.det()
The module is intended to allow as-generic-as-posible determinant calculation, that does not rely on specific data-type. NumPy arrays of any dtype can be used as the source matrix.
The source ndarray.dtype
objects should only support product (*
) operation, sum()
and negative()
are optional.
This includes generic Python objects, like:
- Fractions
- Decimals
- Numbers - numeric abstract classes
- NumPy polynomials
- Python integers PEP 237 - limited by available memory
The module can be installed from the GitHub repo:
pip install "git+https://github.com/trundev/numpy-determinant.git@main"
The main numpy_determinant.det()
can be used just like the numpy.linalg.det()
:
>>> import numpy as np
>>> import numpy_determinant
>>> a = np.arange(9).reshape(3, 3) # Some linearly dependent data
>>> a += a != 0 # Increase all elements, but the first one
>>> a
array([[0, 2, 3],
[4, 5, 6],
[7, 8, 9]])
>>> numpy_determinant.det(a)
3
Use Python unbounded integer objects:
>>> a = a.astype(object) # Convert to unbounded integer objects
>>> a += 10**50 # Increase all elements by a huge number
array([[100000000000000000000000000000000000000000000000000,
100000000000000000000000000000000000000000000000002,
100000000000000000000000000000000000000000000000003],
[100000000000000000000000000000000000000000000000004,
100000000000000000000000000000000000000000000000005,
100000000000000000000000000000000000000000000000006],
[100000000000000000000000000000000000000000000000007,
100000000000000000000000000000000000000000000000008,
100000000000000000000000000000000000000000000000009]],
dtype=object)
>>> numpy_determinant.det(a)
3
Compare to numpy
and sympy
>>> np.linalg.det(a.astype(float)) # Determinant via numpy needs float input
np.float64(0.0) # Wrong result, after float conversion and loss of significance
>>> import sympy
>>> sympy.Matrix(a).det()
3 # Correct result, as SymPy can handle arbitrarily large integers
See test folder for more examples