inverse of a matrix with Fraction entries

Peter Otten __peter__ at
Wed Nov 24 15:12:44 CET 2010

Daniel Fetchinson wrote:

> I guess this is a question to folks with some numpy background (but
> not necessarily).
> I'm using fractions.Fraction as entries in a matrix because I need to
> have very high precision and fractions.Fraction provides infinite
> precision (as I've learned from advice from this list). 

"Infinite" precision only for values that can be expressed as a fraction:

>>> Fraction(2)**Fraction(1, 2)
>>> type(_)
<type 'float'>

> Now I need to
> calculate its inverse. Can numpy help in this regard? Can I tell numpy
> that the inverse matrix should also have entries in
> fractions.Fraction? Or numpy can only do floating point calculations?

I tried it, and numpy.linalg.inv() converted the Fraction values to float. 
If you aren't concerned about efficiency it should be easy to do it 
yourself, in pure python. 

You could also ask on the numpy mailing list.
> Probably it doesn't matter but the matrix has all components non-zero
> and is about a thousand by thousand in size.

Hmm, where did you get that million of exact fractions from? If some real 
world data is involved the error introduced by the floating point 
calculation may be negligable in comparison with the initial measurement 


More information about the Python-list mailing list