Jos*_*h.F 5 haskell matrix hmatrix
HaskellHMatrix可以让你方便地获取切片:
m ?? (All, Take 3)
Run Code Online (Sandbox Code Playgroud)
但是如何设置切片,尤其是非矩形切片呢?例如,在 Python 的 Numpy 中我会这样做:
>>> x = np.arange(12).reshape(3, 4)
>>> x
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
>>> x[[0,0], [1,3]] += x[[2,2], [1,3]] # to ix (0, 1) add (2, 1) and to ix (0, 3) add (2, 3)
>>> x
array([[ 0, 10, 2, 14],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
Run Code Online (Sandbox Code Playgroud)
我可以自己编写这个辅助函数,但是,它确实丑陋且不完整,并且需要我添加zipWith3M_它应该存在于vector库中但不存在:
setSlice :: (Element t, Num t) => Matrix t -> (Extractor, Extractor) -> Vector t -> Matrix t
setSlice parent (Pos (V.map fromIntegral -> irows), Pos (V.map fromIntegral -> icols)) sub = runST $ do
tparent <- thawMatrix parent
zipWith3M_ (writeMatrix tparent) irows icols sub
freezeMatrix tparent
Run Code Online (Sandbox Code Playgroud)