views:

183

answers:

3

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.

+11  A: 

I think using indexing is OK, if you can assume that the argument is a square matrix. Getting diagonal with this representation is O(N2) anyway, since you have to traverse the lists.

diag x = zipWith (!!) x [0..]
sdcvvc
OK, point conceded. This is pretty neat.
Paul Johnson
Wow, as far as this representation goes this really is pretty neat.
Jaen
Sweet!! :) 15chars
st0le
The point of tagging something as "homework" is (to me, at least) that you should provide more explanation than code. An answer is incomplete if it gives code (or at least clever code like this) without explaining how it works. I'd like to see an expansion of how `zipWith` combines the `!!` function with the elements of `x` and `[0..]` to provide the result.
Gabe
@Gabe: The poster said this is not a homework; I believe that. I think it's more instructive for the reader to comprehend the definition himself, than to give a "spoonfeeding" explanation.
sdcvvc
Actually this definition is pretty much straightforward to understand for me, though it may have additional merit to explain it for others who may come across this question.
Jaen
+5  A: 

You can simplify your original definition to:

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

There isn't much wrong with using indexing for this, which lets you simplify it further to:

mainDiagonal xs = zipWith (!!) xs [0..]

Array-based representation

You can also represent matrixes using Data.Array indexed by (i,j). This lets you use the mathematical definition of the main diagonal almost verbatim:

import Data.Array

mainDiagonal :: (Ix i) => Array (i, i) e -> [e]
mainDiagonal xs = [ e | ((i,j),e) <- assocs xs, i == j ]

You can use this like:

-- n×n matrix helper
matrix n = listArray ((0,0),(n-1,n-1))

> mainDiagonal $ matrix 3 [1..]
[1,5,9]

Efficiency

The previous definition of mainDiagonal is still not efficient: it still needs O(N²) tests of i == j. Analogous to the zipWith version, it can be fixed and generalized as follows:

mainDiagonal xs = (xs !) `map` zip [n..n'] [m..m']
                      where ((n,m),(n',m')) = bounds xs

This version only indexes the array O(N) times. (As a bonus, it also works with rectangular matrices, and is independent of the indexing base.)

Piet Delport
So Data.Array is pretty much like a vanilla C array and should thusly be faster than list of lists implementation?
Jaen
Data.Array is not exactly like a vanilla C array. First, it is immutable, so while indexing is fast, all mutation operations require a full copy. Second, it holds boxed, possibly unevaluated, values, rather than raw unboxed things. So you can think of it as, roughly, a read-only array of pointers (ish).
sclv
I should add that there are plenty of other Arrays with different characteristics as well, including in particular various combinations of unboxed and mutable, as well as other libraries with different/better approaches depending on your problem domain.
sclv
Jaen: In general, array indexing is O(1) instead of O(N), yes. For this problem, it only helps if you *use* that indexing, though: i've annotated my answer with an O(N) indexing version to go with the naive O(N²) definition.
Piet Delport
Alternative version: `mainDiagonal xs = zipWith (uncurry (xs !)) [n..n'] [m..m'] where...`
sdcvvc
I think I'd go with this answer as the accepted one as it's the more comprehensive; thank you both though.
Jaen
+1  A: 

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]
jetxee
I gather this is a wrapper for some low level matrix library?
Jaen
Hmatrix can be compiled to use `Data.Vector.Storable` (build it with `-fvector` flag). By default it uses its own implementation of the vectors (also to represent matrices).
jetxee