将 SpMatrix 转换为矩阵

问题描述 投票:0回答:1

我正在尝试找到一种更短或更有效的方法,将

SpMatrix
(从
Data.Sparse.SpMatrix
)转换为
Matrix
(从
Data.Matrix
)。 使用内置函数,我设法按照
SpMatrix Double
[(IxRow, IxCol, Double)]
[Double]
Matrix Double
的顺序将它们转换。 我想知道是否可以跳过这些中间步骤。

小例子:

Import Data.Sparse.Common
Import qualified Data.Matrix as M

main :: IO ()
main = do
   let a = mkDiagonal 3 [1,2,3] :: SpMatrix Double
       b = toDenseListSM a
       c = map (\(_, _, x) -> x) b
       d = M.fromList 3 3 c
   print d

输出(

d
):

1.0 0.0 0.0
0.0 2.0 0.0
0.0 0.0 3.0

因此,在上面的示例中,我正在寻找一种跳过步骤

b
c
的方法。

haskell matrix vector sparse-matrix
1个回答
0
投票

如果

Data.Matrix
来自
matrix
包并且
Data.Sparse.SpMatrix
来自
sparse-linear-algebra
包,那么你应该能够执行这样的操作(未经测试):

convert :: Num a => SpMatrix a -> Matrix a
convert sp = matrix (nrows sp) (ncols sp) (lookupWD_SM sp)

这里

matrix
来自
Data.Matrix
,而
nrows
ncols
lookupWD_SM
来自
Data.Sparse.SpMatrix

© www.soinside.com 2019 - 2024. All rights reserved.