0

I am having the following issue:

>>> import numpy as np
>>> import mpmath as mp
>>> v1 = [mp.mpf(1), mp.mpf(2)]
>>> v2 = [mp.mpf(3), mp.mpf(4)]
>>> np.dot(v1,v2)
mpf('11.0')
>>> M2 = [[mp.mpf(1), mp.mpf(2)], [mp.mpf(3), mp.mpf(4)]]
>>> np.linalg.det(M2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "~/anaconda/lib/python2.7/site-packages/numpy/linalg/linalg.py", line 1776, in det
    r = _umath_linalg.det(a, signature=signature)
TypeError: No loop matching the specified signature and casting
was found for ufunc det

My calls to numpy's linalg.det method work fine with non-mpmath entries, yet I get the above error when giving it mp.mpfs. The old question Interoperability between Numpy and MpMath in Python indicates that np.dot will work just fine if the entries are mp.mpfs (confirmed above), so my question is why this doesn't carry over to np.linalg.det, and what I can do to get around this. The context is that I have already written a large amount of code using only numpy (no mpmath) and only just realized I will need much higher numerical precision, so ideally there is a solution that doesn't involve converting all of the numpy code.

Thanks in advance for any answers.

zjs
  • 327
  • 1
  • 9
  • As indicated by the link, `dot` has an branch that can work with object dtype arrays, though the speed is considerably less than with numeric dtypes. `det` does not have that option. It only uses compiled numeric code. Math with object dtype arrays is hit-or-miss, and always slower. – hpaulj May 17 '21 at 18:36
  • @hpaulj Okay, so it sounds like there isn't a good workaround for `mpmath`. Is there a different library for higher numerical precision which interfaces better with `numpy` methods? – zjs May 17 '21 at 18:50
  • Why not use `mpmath.det`? – Warren Weckesser May 17 '21 at 19:20
  • Ah, I didn't realize that `mp.det` was compatible with `np.array` but I see now that it should work. Computing inverses may still be an issue (since `mpmath` treats inverses as an attribute or from `**-1`) but I can probably work around that. Thank you both for pointing me in the right direction! – zjs May 19 '21 at 16:58

0 Answers0