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.

**Extract non-main diagonal from scipy sparse matrix?**

Say that I have a sparse matrix in scipy.sparse format. How can I extract a diagonal other than than the main diagonal? For a numpy array, you can use numpy.diag. Is there a scipy sparse equivalent? F

**Numpy extract values on the diagonal from a matrix**

My question is similar(the expanded version) to this post:Numpy extract row, column and value from a matrix. In that post, I extract elements which are bigger than zero from the input matrix, now I wa

**Best way to randomize a binary diagonal matrix, keeping 1’s off diagonal**

Let A be an n*n diagonal matrix. Say, n=5: A <- diag(1, 5) A [,1] [,2] [,3] [,4] [,5] [1,] 1 0 0 0 0 [2,] 0 1 0 0 0 [3,] 0 0 1 0 0 [4,] 0 0 0 1 0 [5,] 0 0 0 0 1 I need to randomize A by rows, keep

**Create a sparse diagonal matrix from row of a sparse matrix**

I process rather large matrices in Python/Scipy. I need to extract rows from large matrix (which is loaded to coo_matrix) and use them as diagonal elements. Currently I do that in the following fashio

**Removing diagonal elements from matrix in R**

How can I remove the diagonal elements (diagL) from my matrix L using R? I tried using the following: subset(L, select=-diag(L)) or subset(L, select=-c(diag(L))) but I get 0 numbers… Thanks,

**What is the best perl module to extract text from a pdf? [closed]**

What is the best way to extract text from a pdf?

**What’s the best way to extract a value from a scala case class?**

What would be the best and/or easiest way to extract a value that I’ve saved in a case class? take for example the following code: abstract class Something case class Foo(input: Int) extends Something

**What’s the best way to extract a one-dimensional array from a rectangular array in C#?**

Say I have a rectangular string array (stringarray[rows, columns] – not a jagged array). What’s the best way to extract a one-dimensional array from this (either a single row or a single column)? I ca

**Is there a fast(er) way to remove all non-diagonal elements from a square matrix?**

I have a small performance bottleneck in an application that requires removing the non-diagonal elements from a large square matrix. So, the matrix x 17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21

**Best way to extract all _id from mongodb collection**

What is best way to extract all _id from mongodb collection? I am using pymongo to work with mongodb. Following code: for item in db.some_collection.find({}, {‘_id’: 1}): # do something takes some ti

## Answers

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

diag x = zipWith (!!) x [0..]

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.)

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]