Python: inverse a matrix with high precision floats

930 views Asked by At

I am following a tutorial on how to work on multiple-precision arithmetic in Python.
In the end I would like to have a numpy array with floats of arbitrary high precision and I need to inverse that matrix.

Therefore we have:

import sys
import numpy as np
import gmpy2

print(sys.version)
print(np.__version__)
print(gmpy2.version)
3.6.10 | packaged by conda-forge | (default, Apr 24 2020, 16:27:41) 
[GCC Clang 9.0.1 ]
1.12.1
<built-in function version>

Followed by data generation:

A = np.ones((3,3));
B = A/gmpy2.mpfr("1.0")
print(A)
print(B)
[[ 1.  1.  1.]
 [ 1.  1.  1.]
 [ 1.  1.  1.]]
[[mpfr('1.0') mpfr('1.0') mpfr('1.0')]
 [mpfr('1.0') mpfr('1.0') mpfr('1.0')]
 [mpfr('1.0') mpfr('1.0') mpfr('1.0')]]

And the problematic part:

print(np.linalg.pinv(B))
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-23-3a70ff54e53d> in <module>
----> 1 print(np.linalg.pinv(B))

~/conda-envs/Python_Jupyter/env/lib/python3.6/site-packages/numpy/linalg/linalg.py in pinv(a, rcond)
   1660     _assertNoEmpty2d(a)
   1661     a = a.conjugate()
-> 1662     u, s, vt = svd(a, 0)
   1663     m = u.shape[0]
   1664     n = vt.shape[1]

~/conda-envs/Python_Jupyter/env/lib/python3.6/site-packages/numpy/linalg/linalg.py in svd(a, full_matrices, compute_uv)
   1402 
   1403         signature = 'D->DdD' if isComplexType(t) else 'd->ddd'
-> 1404         u, s, vt = gufunc(a, signature=signature, extobj=extobj)
   1405         u = u.astype(result_t, copy=False)
   1406         s = s.astype(_realType(result_t), copy=False)

TypeError: No loop matching the specified signature and casting
was found for ufunc svd_n_s

Would anyone know how to achieve the goal I am working towards?

1

There are 1 answers

0
maciek On BEST ANSWER

I have managed to inverse a matrix with very precise numbers with mpmath which contains a lot of built-in math functions as well as a matrix class. Thanks for the comments!