I am trying to get a usable version of multidimensional arrays in Haskell, comparable to that of numpy
arrays in Python and other languages.
I found other questions about how to write custom functions for arrays of specific dimensions, but my objective it more to the point, get a similar behavior of Data.Vector
's slice
function, which is intuitive and does the job of bracket-indexed arrays of other languages.
Vector
's slice
function has type
V.slice :: Int -> Int -> V.Vector a -> V.Vector a
so slicing a vector v
is as simple as
import Data.Vector as V
let v = V.fromList [1..10]
i = 1
j = 5
V.slice i j v
Repa
's slice
on the other hand has type
R.slice :: (R.Slice sl, R.Shape (R.FullShape sl), R.Source r e) => R.Array r (R.FullShape sl) e -> sl -> R.Array R.D (R.SliceShape sl) e
so it takes a Repa array and a shape and return a delayed array.
I understand that Repa does not take Integers as indices, but I am looking for a general use of the slice
function for arbitrary dimension, whether using Repa
's (Z :. i)
or ixn
dimension specification.
I'm not looking for a dimension-dependent function using traverse
, and would prefer not to go into any template haskell to generalize that, although if it's not possible to use the slice
function generally it is what it is.
The question is, then: is it possible to use Repa
's slice
function to get arbitrary slices of multidimensional arrays, like in numpy
's v[x1:x2,y1:y2] or C++ Eigen
's matrix.block<p,q>(i,j)
?
I believe the closest equivalent is extract, which takes a starting index (like
ix2 x1 y1
) and a size (likeix2 (x2-x1) (y2-y1)
).The result of
extract
always has the same number of dimensions as the input, but can have different size in each dimension. The result ofslice
can have a different number of dimensions, but in any given dimension it takes either all elements or exactly one. (Based on the instances forShape
andSlice
.)