2

I'm having some trouble grasping repa's Slice module, and specifically how its types and families are used with the slice function. I believe an answer to the following would clear things up for me.

Suppose x has shape shapeOfList [m_0, m_1, …, m_{d-1}]. Given integers i < d and j < mi, how do I build something with a suitable type to be slice's second argument, that extracts a slice from x with shape shapeOfList [m_0, …, m_{i-1}, m_{i+1}, …, m_{d-1}] defined by freezing index number i at j?

Examples in the case d=2 would be:

  • i=0: extract row j
  • i=1: extract column j
gspr
  • 11,144
  • 3
  • 41
  • 74

1 Answers1

1
let arr =  fromListUnboxed (ix2 3 3) [0..8] :: Array U DIM2 Double 

shapeOfList transform a list to an index, for instance

shapeOfList [2,2] == (ix2 2 2) -- (Z:. 2 :.2)

xs ! shapeOfList [2,2] == 8.0 

But the function slice takes a range of indexes and this range of indexes is represented in REPA using the keywords Any and All. For instance, to extract the nth row from a Matrix the syntax is

nRow n =computeUnboxedS $ slice arr (Any :. n :. All) -- xs :: Array U DIM1 Double
 --   nRow 2 = fromList [6.0,7.0,8.0]

To extract the nth column the Following notation is used

nCol n = computeUnboxedS $ slice arr (Any :. n ) -- xs :: Array U DIM1 Double
--  nCol 2 = fromList [2.0,5.0,8.0]

It is confusing that in the REPA vocabulary the array index (Z:. i:. i) and the range of indexes (Any :. n :. All) are both called shapes, but one represent only one element index while the other represent an array of indexes.

EDIT

to extract the ith dimension from an array traverse seems the right function, for instance for 3-dimensions

-- k = 3 and m = 0
fun3D_i arr = traverse arr (\(Z :. _ :. j :. k ) -> (Z:. j :. k)) (\f (Z :. j :. k )  -> f (Z :. 0 :. j :. k ))`

-- k = 3 and m = 1
fun3D_j arr = traverse arr (\(Z :. i :. _ :. k ) -> (Z:. i :. k)) (\f (Z :. i :. k )  -> f (Z :. i :. 0 :. k )) 

-- k = 3 and m = 2
fun3D_k arr = traverse arr (\(Z :. i :. j :. _ ) -> (Z:. i :. j)) (\f (Z :. i :. j )  -> f (Z :. i :. j :. 0 ))

and so on for higher dimensions. On the other hand, Template Haskell could help here to select the dimension that you want to extract.

felipez
  • 1,212
  • 9
  • 21
  • Oh… I must have completely misunderstood `Any` (and probably `All`), then! What's the type of `Any :. (n :: Int)`? – gspr May 14 '15 at 14:43
  • Wait, no, I was just confused by your answer. I understand how to get the n'th row or the n'th column from a *matrix*. Slicing to get row `j` is fixing index 0 to be `j`. Getting column `j` is fixing index 1 to be `j`. But, given a `d`-array, how do I fix index `i` (< `d`) to be `j`? The case `d=2` gives matrices, as in your answer, but I get confused for higher `d`. – gspr May 14 '15 at 14:55
  • Thanks... I have to digest that for a bit! – gspr May 14 '15 at 21:37
  • I'm sure you are correct, but I'm still not quite getting my head around this. Could you be so kind as to feed me with a teaspoon and show a function `foo :: Int -> Int -> Array r sh1 a -> Array D sh2 a` with the following properties? In `foo i j x`, `x` is a `d`-dimensional array with extents `m_0, …, m_{d-1}`, `i < d` and `0 <= j < m_i`; the return value being a `(d-1)`-dimensional array defined by fixing index `i` in `x` to be `j`. I much appreciate the help :-) – gspr May 15 '15 at 11:55
  • Actually, I'm not right about the indexes, I'm rewriting things because I messed them completely. – felipez May 15 '15 at 15:13
  • If you've done your edit, I think we're still misunderstanding eachother. I don't want the n'th column of a 2D-array. I want the n'th m-index of a KD-array (m – gspr May 16 '15 at 17:32
  • 1
    I now understand what you want and such function `foo` can't be written in terms of `slice`, because the `Any` and `All` Data types does not allow to constrain the indexes in the way you want to. An alternative can be used `traverse` but you would have to write a function For each k-dimension and for different m. – felipez May 16 '15 at 23:36
  • Really? That's a shame! But thanks. Put it in your answer and I'll accept it :) – gspr May 17 '15 at 06:28