How to Set Slices in Haskell's HMatrix?

139 views Asked by At

Haskell's HMatrix allows you to conveniently get slices:

m ?? (All, Take 3)

But how can you set slices, especially non-rectangular ones? For instance, in Python's Numpy I'd do:

>>> 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]])

I'm able to write this helper function myself, but, it's sure ugly, and partial, and required I add zipWith3M_ which should exist in the vector library but doesn't:

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
0

There are 0 answers