haskellmatrixhmatrix

How can I compute an exact (integer) determinant in Haskell?


I wanted to use hmatrix but it insists on Double and this gives rounding errors sometimes. Looking at the source, I tried

type instance DoubleOf Rational = Rational

but DoubleOf is not exported (Numeric.ContainerBool is hidden), and that's probably for a reason.


Solution

  • You can perform exact operations on matrices using bed-and-breakfast package.

    Here's sample ghci session:

    >>> import Numeric.Matrix
    >>> import Data.Ratio
    >>> let m = fromList [[1 % 2, 2 % 3], [3 % 4, 4 % 5]] :: Matrix Rational
    >>> det m
    (-1) % 10
    >>> m * m
     3 % 4  13 % 15
     39 % 40  57 % 50
    
    >>> m^4
     563 % 400  819 % 500
     7371 % 4000  10723 % 5000