Is there usage of Haskell Repa slice function analogous to Vector and other languages?












0















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










share|improve this question

























  • numpy's v[x1:x2,y1:y2] means to get the elements of index (1,1), (1,2), (2,1), (2,2) and return the result as 2 x 2 metrix?

    – assembly.jc
    Nov 26 '18 at 10:46













  • It means getting all elements in the first dimension (which I'm calling x) between indices x1 and x2, and the second dimension (y) between y1 and y2. In Python getting those elements you mention would be then x1,x2 = y1,y2 = 1,3, or v[1:3,1:3], which would be a 2x2 matrix indeed since numpy arrays don't include the last index. In Haskell V.slice 1 2 v would do the analogous operation for a single dimension, but doing that for 2 or more is not straightforwardly described in Repa's tutorial or documentation

    – Twisted Mersenne
    Nov 26 '18 at 16:48
















0















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










share|improve this question

























  • numpy's v[x1:x2,y1:y2] means to get the elements of index (1,1), (1,2), (2,1), (2,2) and return the result as 2 x 2 metrix?

    – assembly.jc
    Nov 26 '18 at 10:46













  • It means getting all elements in the first dimension (which I'm calling x) between indices x1 and x2, and the second dimension (y) between y1 and y2. In Python getting those elements you mention would be then x1,x2 = y1,y2 = 1,3, or v[1:3,1:3], which would be a 2x2 matrix indeed since numpy arrays don't include the last index. In Haskell V.slice 1 2 v would do the analogous operation for a single dimension, but doing that for 2 or more is not straightforwardly described in Repa's tutorial or documentation

    – Twisted Mersenne
    Nov 26 '18 at 16:48














0












0








0








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










share|improve this question
















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







arrays haskell vector repa






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 26 '18 at 4:18









Alexis King

32.3k1196166




32.3k1196166










asked Nov 26 '18 at 1:40









Twisted MersenneTwisted Mersenne

274




274













  • numpy's v[x1:x2,y1:y2] means to get the elements of index (1,1), (1,2), (2,1), (2,2) and return the result as 2 x 2 metrix?

    – assembly.jc
    Nov 26 '18 at 10:46













  • It means getting all elements in the first dimension (which I'm calling x) between indices x1 and x2, and the second dimension (y) between y1 and y2. In Python getting those elements you mention would be then x1,x2 = y1,y2 = 1,3, or v[1:3,1:3], which would be a 2x2 matrix indeed since numpy arrays don't include the last index. In Haskell V.slice 1 2 v would do the analogous operation for a single dimension, but doing that for 2 or more is not straightforwardly described in Repa's tutorial or documentation

    – Twisted Mersenne
    Nov 26 '18 at 16:48



















  • numpy's v[x1:x2,y1:y2] means to get the elements of index (1,1), (1,2), (2,1), (2,2) and return the result as 2 x 2 metrix?

    – assembly.jc
    Nov 26 '18 at 10:46













  • It means getting all elements in the first dimension (which I'm calling x) between indices x1 and x2, and the second dimension (y) between y1 and y2. In Python getting those elements you mention would be then x1,x2 = y1,y2 = 1,3, or v[1:3,1:3], which would be a 2x2 matrix indeed since numpy arrays don't include the last index. In Haskell V.slice 1 2 v would do the analogous operation for a single dimension, but doing that for 2 or more is not straightforwardly described in Repa's tutorial or documentation

    – Twisted Mersenne
    Nov 26 '18 at 16:48

















numpy's v[x1:x2,y1:y2] means to get the elements of index (1,1), (1,2), (2,1), (2,2) and return the result as 2 x 2 metrix?

– assembly.jc
Nov 26 '18 at 10:46







numpy's v[x1:x2,y1:y2] means to get the elements of index (1,1), (1,2), (2,1), (2,2) and return the result as 2 x 2 metrix?

– assembly.jc
Nov 26 '18 at 10:46















It means getting all elements in the first dimension (which I'm calling x) between indices x1 and x2, and the second dimension (y) between y1 and y2. In Python getting those elements you mention would be then x1,x2 = y1,y2 = 1,3, or v[1:3,1:3], which would be a 2x2 matrix indeed since numpy arrays don't include the last index. In Haskell V.slice 1 2 v would do the analogous operation for a single dimension, but doing that for 2 or more is not straightforwardly described in Repa's tutorial or documentation

– Twisted Mersenne
Nov 26 '18 at 16:48





It means getting all elements in the first dimension (which I'm calling x) between indices x1 and x2, and the second dimension (y) between y1 and y2. In Python getting those elements you mention would be then x1,x2 = y1,y2 = 1,3, or v[1:3,1:3], which would be a 2x2 matrix indeed since numpy arrays don't include the last index. In Haskell V.slice 1 2 v would do the analogous operation for a single dimension, but doing that for 2 or more is not straightforwardly described in Repa's tutorial or documentation

– Twisted Mersenne
Nov 26 '18 at 16:48












1 Answer
1






active

oldest

votes


















1














I believe the closest equivalent is extract, which takes a starting index (like ix2 x1 y1) and a size (like ix2 (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 of slice can have a different number of dimensions, but in any given dimension it takes either all elements or exactly one. (Based on the instances for Shape and Slice.)






share|improve this answer
























  • Thanks. That seems to work alright.. Still pretty cumbersome compared to other languages and even to Vector.slice. I can deal with the array keeping the same number of dimensions and size 1 along the dimension of the slice, it's not ideal but workable in the absence of analog usage of Repa.slice.

    – Twisted Mersenne
    Dec 1 '18 at 4:24











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53473751%2fis-there-usage-of-haskell-repa-slice-function-analogous-to-vector-and-other-lang%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














I believe the closest equivalent is extract, which takes a starting index (like ix2 x1 y1) and a size (like ix2 (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 of slice can have a different number of dimensions, but in any given dimension it takes either all elements or exactly one. (Based on the instances for Shape and Slice.)






share|improve this answer
























  • Thanks. That seems to work alright.. Still pretty cumbersome compared to other languages and even to Vector.slice. I can deal with the array keeping the same number of dimensions and size 1 along the dimension of the slice, it's not ideal but workable in the absence of analog usage of Repa.slice.

    – Twisted Mersenne
    Dec 1 '18 at 4:24
















1














I believe the closest equivalent is extract, which takes a starting index (like ix2 x1 y1) and a size (like ix2 (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 of slice can have a different number of dimensions, but in any given dimension it takes either all elements or exactly one. (Based on the instances for Shape and Slice.)






share|improve this answer
























  • Thanks. That seems to work alright.. Still pretty cumbersome compared to other languages and even to Vector.slice. I can deal with the array keeping the same number of dimensions and size 1 along the dimension of the slice, it's not ideal but workable in the absence of analog usage of Repa.slice.

    – Twisted Mersenne
    Dec 1 '18 at 4:24














1












1








1







I believe the closest equivalent is extract, which takes a starting index (like ix2 x1 y1) and a size (like ix2 (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 of slice can have a different number of dimensions, but in any given dimension it takes either all elements or exactly one. (Based on the instances for Shape and Slice.)






share|improve this answer













I believe the closest equivalent is extract, which takes a starting index (like ix2 x1 y1) and a size (like ix2 (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 of slice can have a different number of dimensions, but in any given dimension it takes either all elements or exactly one. (Based on the instances for Shape and Slice.)







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 26 '18 at 18:14









bergeybergey

2,443717




2,443717













  • Thanks. That seems to work alright.. Still pretty cumbersome compared to other languages and even to Vector.slice. I can deal with the array keeping the same number of dimensions and size 1 along the dimension of the slice, it's not ideal but workable in the absence of analog usage of Repa.slice.

    – Twisted Mersenne
    Dec 1 '18 at 4:24



















  • Thanks. That seems to work alright.. Still pretty cumbersome compared to other languages and even to Vector.slice. I can deal with the array keeping the same number of dimensions and size 1 along the dimension of the slice, it's not ideal but workable in the absence of analog usage of Repa.slice.

    – Twisted Mersenne
    Dec 1 '18 at 4:24

















Thanks. That seems to work alright.. Still pretty cumbersome compared to other languages and even to Vector.slice. I can deal with the array keeping the same number of dimensions and size 1 along the dimension of the slice, it's not ideal but workable in the absence of analog usage of Repa.slice.

– Twisted Mersenne
Dec 1 '18 at 4:24





Thanks. That seems to work alright.. Still pretty cumbersome compared to other languages and even to Vector.slice. I can deal with the array keeping the same number of dimensions and size 1 along the dimension of the slice, it's not ideal but workable in the absence of analog usage of Repa.slice.

– Twisted Mersenne
Dec 1 '18 at 4:24


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53473751%2fis-there-usage-of-haskell-repa-slice-function-analogous-to-vector-and-other-lang%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Contact image not getting when fetch all contact list from iPhone by CNContact

count number of partitions of a set with n elements into k subsets

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks