Linear system solution with fractions in numpy Linear system solution with fractions in numpy numpy numpy

Linear system solution with fractions in numpy


It does not appear to be possible to invert a matrix of rationals using pure numpy according to this thread on the python mailing list. The response suggests that you can use sympy for matrices of rationals up to size 4x4. If you are tied to numpy for some reason, you can consider taking and using the inverse of a 3x3 matrix "manually". Step by step tutorials on how to do this can be found on http://www.mathsisfun.com/algebra/matrix-inverse-minors-cofactors-adjugate.html, as well as a large multitude of other tutorials on matrix inversion.


IMHO, there is no hope. A solution that work in many cases :

y = np.zeros(3, dtype=fractions.Fraction)....X= np.linalg.solve(A,y)s=[fractions.Fraction.from_float(x).limit_denominator(6**9) for x in X]print(s,y==dot(A,s))

It uses the property that the solution is nearly a fraction with little numerator and denominator, and find it.