How to calculate the inverse of ndarray holding variables of type Fraction in python?
Fraction is a Python type but not a native numpy dtype. Said differently, for a numpy point of view, Fractions are (opaque) objects. If you manage to do operations over them with numpy methods, chances are that they will be automatically converted to floating point types (which are native in numpy).
Long story short, no numpy vectorized method will be able to process Fraction objects, so you should not use np.array to store them but stick to a plain old list, or to lists of lists if you want to process 2D arrays.
I am trying to calculate the inverse of a 2d array in python that holds fractions and cannot convert it to float because I need to maintain the ratio correctly between numerator and denominator.
A = np.array([[Fraction(1), Fraction(-0.5)],
[Fraction(-4/9), Fraction(1)]])
When I try np.linalg.inv(A) I get the following error:
'TypeError: No loop matching the specified signature and casting was found for ufunc inv'