1

I want to set the column of a sparse matrix using a sparse matrix and a sparse vector. Tried to check the relevant documents but I got stuck on this part. Is this possible? If it helps I'm building with cabal. I'm ok with changing/adding libraries as long as they can handle sparse matrices and vectors combined.

import Data.Sparse.SpMatrix
import Data.Sparse.SpVector 
import Numeric.LinearAlgebra.Sparse

main :: IO ()
main = do
let m1 = fromListSM (3,3) 
        [(0,0,1)
        ,(1,1,2)
        ,(2,2,3)
        ] :: SpMatrix Double
    let v = fromListSV 3 [(0,1),(1,4),(2,9)] :: SpVector Double
    let m2 = ...

How can I define m2 here for this to work? It should be just like m1, but with the middle column exchanged for v.

In this example the expected outcome for m2 is

1 1 _
_ 4 _ 
_ 9 3
2
  • Which package(s) are you using? Commented Feb 3, 2024 at 13:44
  • My .cabal contains: build depends: base, hmatrix, hmatrix-gsl, sparse-linear-algebra, prettyprinter, vector I'm ok with removing/adding to make it work. Commented Feb 3, 2024 at 14:42

2 Answers 2

0

I don't see an obviously great way to do this operation using the matrix-based APIs in sparse-linear-algebra. However, the constructors are exported, and you can clearly see an SpMatrix is just an IntMap (IntMap a) with a dimension, and an SpVector is just an IntMap a with a dimension. So if you can't find any cleverer approach, you can just reach inside and insert the row you want into the underlying map.

Sign up to request clarification or add additional context in comments.

2 Comments

The insert you linked only works for single elements, right? I have found the somewhat equivalent insertSpMatrix But I was wondering if there was something like InsertSpMatrix for an entire row or column
@Pompan Ah, I somehow missed that you wanted to edit a column. My suggestion would work to replace a row.
0

I was missing a related module which contained what I wanted.

import Data.Sparse.Common

Now all I needed was to define m2 as this:

let m2 = insertCol m1 v 1

2 Comments

Are you sure this does what you want? The question says you want to "set" a column, which sounds to me like it means "replace" or "overwrite": set a column of the matrix, ignoring what was there before. But my reading of insertCol is that it adds elements to a column. It will overwrite any conflicts, but if an entrt is present in the original matrixs' column and not present in the replacement vector, it stays in the result vector.
@amalloy I tried to catch what you mean but it doesn't seem like it adds, but replaces: if it were to add, the middle, which is 4 here, would be 2 + 4 = 6. Oh wait I see what you mean. Yes, if for example the third index of the vector is empty ( _ ) and that the third row in the matrix (in the targeted column) isn't empty the value of the matrix will be kept. For me this is still okay as in my case the column will be empty.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.