Regarding your question, walking the array is not complicated I think. You 
can just walk the underlying buffer, and use a function like below if I'm 
not mistaking.

location : Int -> Int -> Strides -> Shape -> Maybe Location
location bufferIndex bufferOffset (stride1, stride2) (height, width) =
    let
        unOffset =
            bufferIndex - bufferOffset
        
        line =
            unOffset // stride1
            
        column =
            unOffset % stride1
    in
    if line < height && column < width then
        Just (line, column)
    else
        Nothing

Generalization would proceed the same, using euclidean division and modulo, 
dimension after dimension.

On Friday, December 1, 2017 at 11:22:16 PM UTC+8, Francisco Ramos wrote:
>
> Hi guys,
>
> been trying to figure out for a while now how I can solve this problem. 
> I've implemented my own type of array, it's called NdArray. The way it 
> works is as follow:
> It has a buffer (an array of something), a shape (list of dimensions), 
> strides (list of steps) and an offset. Imagine we have a NdArray with a 
> buffer of 9 numbers [1, 2, 3, 4, 5, 6, 7, 8, 9], shape [3, 3] (square 
> matrix) and this leads to a list of strides [3, 1]. This last one means, 
> there is a jump of 3 numbers for each of the first dimension. Better 
> visualised:
>
> buffer => [1, 2, 3, 4, 5, 6, 7, 8, 9]
> view of this buffer with shape [3, 3] => 
> [ 1, 2, 3
> , 4, 5, 6
> , 7, 8, 9
> ]
>
> Now, imagine I change the strides to be [2, 2], that means, I'm jumping 
> one per dimension (in this square matrix I'm jumping one column and one 
> row). The result is:
> [ 1, 3
> , 7, 9
> ]
>
> I'm jumping one number in the last dimension, and an entire row in the 
> first dimension. Shape is now [2, 2].
>
> So I have all this implemented already here 
> https://github.com/jscriptcoder/elm-ndarray. This is port of ndarray 
> by Mikola Lysenko, https://github.com/scijs/ndarray. But I got stuck how 
> to walk this array. I'm trying to implement map, filter and fold (foldl), 
> and to do so I must be able to walk this array, which is not that trivial 
> (or at least not for me). I have implemented a function "index" which takes 
> a location in the form of list of Int and calculates the index based on 
> shape, strides and offset. So I'm trying to find a way to implement this 
> functionality:
> For example, for a [3, 3] shape then
> nextLocation [0, 0] => Just [0, 1]
> nextLocation [0, 1] => Just [0, 2]
> nextLocation [0, 2] => Just [1, 0]
> nextLocation [1, 0] => Just [1, 1]
> nextLocation [1, 1] => Just [1, 2]
> ...
>  nextLocation [2, 2] => Nothing
>
> By far not an expert in functional programming, maybe someone can help me 
> to figur this one out?
>
> Thanks a lot.
>
> Fran
>

-- 
You received this message because you are subscribed to the Google Groups "Elm 
Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to