My goal is to make a tridiagonal matrix in an efficient way using either list comprehensions or a more efficient algorithm but I am fairly new to Haskell. I am attempting to solve boundary value problems using finite difference methods.
So I have the following function:
createTriDiagonalMatrixList :: [Double] -> [Double]
createTriDiagonalMatrixList [] = []
createTriDiagonalMatrixList (x:y:z:zs) = (replicate rep1 0) ++ [x, y, z] ++ (replicate rep2 0) ++ (createTriDiagonalMatrixList $ zs)
where
len = length $ zs
rep1 = if n - len `div` 3 < n then n - len `div` 3 else (n - 1)
rep2 = if rep1 < 0 then (n + 2) - 3 else (n + 2) - (rep1 + 3)
where n is the internal grid points so the matrix A should be (n+2)x(n+2). This function will take a list of diagonal entries (i.e. [1,2,3,4,5,6]) and turn it into [1,2,3,0,0,4,5,6] such that I can then feed it to hmatrix's "constructor" to make a matrix.
I, however, do not need it to be in this form but rather any form such as [[1,2,3,0],[0,4,5,6]] or the original form.
So, I would really appreciate tips or a list comprehension and explanation because I am not really sure how to do this since it seems I can only do list comprehensions with [1..n] and I need the other parts of the matrix to be 0 for it to be tridiagonal.
Any other pointers on my code would be very helpful because I am new to Haskell.