chessai / eigen

Haskell bindings to the Eigen C++ library
Other
16 stars 6 forks source link

[Ported Issue] Sparse Matrix Accession #4

Closed chessai closed 6 years ago

chessai commented 6 years ago

Is it possible to efficiently get a row, column, or diagonal of a sparse matrix as a sparse vector? Are map and imap available for sparse matrices and sparse vectors?

  • @GregorySchwartz

For the first question: It looks like eigen itself does not carry those operations for sparse matrices. Such a change would need to take place in eigen itself. For the second question: I think so. A function called _map exists but is not exported from Data.Eigen.SparseMatrix, here:

_map :: I.Elem a b => (a -> a) -> SparseMatrix a b -> SparseMatrix a b
_map f m = fromVector (rows m) (cols m) . VS.map g . toVector $ m where
g (I.CTriplet r c v) = I.CTriplet r c $ I.cast $ f $ I.cast v

I think an 'imap' could exist. It might look like this:

_imap :: I.Elem a b => (Int -> Int -> a -> a) -> SparseMatrix a b -> SparseMatrix a b
_imap f m = fromVector rs cs . VS.map g . toVector $ m where
rs = rows m
cs = cols m  
g n (I.CTriplet r c v) = I.CTriplet r c $ I.cast $ f r' c' $ I.cast v where
(c', r') = divMod n rs
  • @chessai

https://github.com/osidorkin/haskell-eigen/pull/16

chessai commented 6 years ago

Closed since these are currently present in eigen as of now