Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to extract a diagonal from a matrix in Haskell?

I was asked to write a function that would extract the diagonal of a matrix stored as a list of lists. The first version was to extract the number by indexing the lists, but I soon concluded it isn't a good algorithm for Haskell and wrote another function:

getDiagonal :: (Num a) => [[a]] -> [a]
getDiagonal [[]]       = []
getDiagonal (xs:[])    = [head xs]
getDiagonal (x:xs)     = head x : getDiagonal (map tail xs)

Since I've only started learning Haskell I'm not sure if it's written in an idiomatic way or if it would perform well.

So my question is is there any better way to extract a diagonal from matrix stored in such a representation or if there isn't is there a better algorithm that could be constructed if the matrix was represented using higher order Haskell concepts, like algebraic types? Also is there any performance difference between deconstructing the list in pattern matching like so ((x:_):xs) or with the head function like shown above?

EDIT: Actually more of curious inquiry than a homework, they don't teach functional programming at technical universities here (which is a pitty I think), but I'll leave the tag.

like image 845
Jaen Avatar asked Oct 22 '10 16:10

Jaen


1 Answers

sdcwc has answered the original question. I'd like to note that representing the matrix as a list of lists is usually inefficient. Lists are good where the length is unknown, matrices are usually of the fixed size. You may consider using a flat associative list or map to build the matrix, and anything with constant element access time when you actually run calculations with this matrix. Data.Array is a good choice (see Piet's answer).

If you run numerical calculations in Haskell, you can use hmatrix package. It has its own matrix data type, Data.Packed.Matrix, and it has a takeDiag function to extract the diagonal.

Data.Packed.Matrix example

For example, if m is your matrix

ghci> let m = (3><3) [1..]
ghci> :t m
m :: Matrix Double
ghci> m
(3><3)
 [ 1.0, 2.0, 3.0
 , 4.0, 5.0, 6.0
 , 7.0, 8.0, 9.0 ]

then you can extract its diagonal like this:

ghci> takeDiag m
3 |> [1.0,5.0,9.0]
like image 118
sastanin Avatar answered Sep 28 '22 03:09

sastanin