Safe Haskell | None |
---|---|
Language | Haskell98 |
Generic array API.
A Repa array is a wrapper around an underlying container structure that holds the array elements.
In the type (Array
l
a
), the l
specifies the Layout
of data,
which includes the type of the underlying container, as well as how
the elements should be arranged in that container. The a
specifies
the element type.
The operators provided by this module do not depend on any particular array representation.
- class Layout l => Bulk l a where
- type BulkI l a = (Bulk l a, Index l ~ Int)
- (!) :: Bulk l a => Array l a -> Index l -> a
- length :: Bulk l a => Array l a -> Int
- class (Bulk l1 a, Target l2 a) => Load l1 l2 a
- class Layout l => Target l a
- type TargetI l a = (Target l a, Index l ~ Int)
- computeS :: (Load lSrc lDst a, Index lSrc ~ Index lDst) => Name lDst -> Array lSrc a -> Array lDst a
- computeIntoS :: Load lSrc lDst a => lDst -> Array lSrc a -> Maybe (Array lDst a)
- fromList :: TargetI l a => Name l -> [a] -> Array l a
- fromListInto :: Target l a => l -> [a] -> Maybe (Array l a)
- toList :: Bulk l a => Array l a -> [a]
- convert :: Convert l1 a1 l2 a2 => Name l2 -> Array l1 a1 -> Array l2 a2
- copy :: (Bulk l1 a, Target l2 a, Index l1 ~ Index l2) => Name l2 -> Array l1 a -> Array l2 a
- mapS :: (Bulk lSrc a, Target lDst b, Index lSrc ~ Index lDst) => Name lDst -> (a -> b) -> Array lSrc a -> Array lDst b
- map2S :: (Bulk lSrc1 a, Bulk lSrc2 b, Target lDst c, Index lSrc1 ~ Index lDst, Index lSrc2 ~ Index lDst) => Name lDst -> (a -> b -> c) -> Array lSrc1 a -> Array lSrc2 b -> Maybe (Array lDst c)
- merge :: (Ord k, BulkI l1 (k, a), BulkI l2 (k, b), TargetI lDst (k, c), Unpack (Buffer lDst (k, c)) t0) => Name lDst -> (k -> a -> b -> c) -> (k -> a -> c) -> (k -> b -> c) -> Array l1 (k, a) -> Array l2 (k, b) -> Array lDst (k, c)
- mergeMaybe :: (Ord k, BulkI l1 (k, a), BulkI l2 (k, b), TargetI lDst (k, c), Unpack (Buffer lDst (k, c)) t0) => Name lDst -> (k -> a -> b -> Maybe c) -> (k -> a -> Maybe c) -> (k -> b -> Maybe c) -> Array l1 (k, a) -> Array l2 (k, b) -> Array lDst (k, c)
- compact :: (BulkI lSrc a, TargetI lDst b, Unpack (Buffer lDst b) t0) => Name lDst -> (s -> a -> (Maybe b, s)) -> s -> Array lSrc a -> Array lDst b
- compactIn :: (BulkI lSrc a, TargetI lDst a, Unpack (Buffer lDst a) t0) => Name lDst -> (a -> a -> (Maybe a, a)) -> Array lSrc a -> Array lDst a
- filter :: (BulkI lSrc a, TargetI lDst a) => Name lDst -> (a -> Bool) -> Array lSrc a -> Array lDst a
- insert :: (BulkI lSrc a, TargetI lDst a, Unpack (Buffer lDst a) t0) => Name lDst -> (Int -> Maybe a) -> Array lSrc a -> Array lDst a
- findIndex :: BulkI l a => (a -> Bool) -> Array l a -> Maybe Int
- concat :: ConcatDict lOut lIn tIn lDst a => Name lDst -> Array lOut (Array lIn a) -> Array lDst a
- concatWith :: (ConcatDict lOut lIn tIn lDst a, BulkI lSep a) => Name lDst -> Array lSep a -> Array lOut (Array lIn a) -> Array lDst a
- unlines :: ConcatDict lOut lIn tIn lDst Char => Name lDst -> Array lOut (Array lIn Char) -> Array lDst Char
- intercalate :: (ConcatDict lOut lIn tIn lDst a, BulkI lSep a) => Name lDst -> Array lSep a -> Array lOut (Array lIn a) -> Array lDst a
- type ConcatDict lOut lIn tIn lDst a = (BulkI lOut (Array lIn a), BulkI lIn a, TargetI lDst a, Unpack (Array lIn a) tIn)
- groups :: (GroupsDict lElt lGrp tGrp lLen tLen n, Eq n) => Name lGrp -> Name lLen -> Array lElt n -> (Array (T2 lGrp lLen) (n, Int), Maybe (n, Int))
- groupsWith :: GroupsDict lElt lGrp tGrp lLen tLen n => Name lGrp -> Name lLen -> (n -> n -> Bool) -> Maybe (n, Int) -> Array lElt n -> (Array (T2 lGrp lLen) (n, Int), Maybe (n, Int))
- type GroupsDict lElt lGrp tGrp lLen tLen n = (Bulk lElt n, Target lGrp n, Target lLen Int, Index lGrp ~ Index lLen, Unpack (Buffer lLen Int) tLen, Unpack (Buffer lGrp n) tGrp)
- foldl :: (Bulk l b, Index l ~ Int) => (a -> b -> a) -> a -> Array l b -> a
- sum :: (BulkI l a, Num a) => Array l a -> a
- prod :: (BulkI l a, Num a) => Array l a -> a
- mean :: (BulkI l a, Fractional a) => Array l a -> a
- std :: (BulkI l a, Floating a) => Array l a -> a
- correlate :: (BulkI l1 a, BulkI l2 a, Floating a) => Array l1 a -> Array l2 a -> a
- folds :: FoldsDict lSeg lElt lGrp tGrp lRes tRes n a b => Name lGrp -> Name lRes -> (a -> b -> b) -> b -> Array lSeg (n, Int) -> Array lElt a -> (Array (T2 lGrp lRes) (n, b), Folds Int Int n a b)
- foldsWith :: FoldsDict lSeg lElt lGrp tGrp lRes tRes n a b => Name lGrp -> Name lRes -> (a -> b -> b) -> b -> Maybe (n, Int, b) -> Array lSeg (n, Int) -> Array lElt a -> (Array (T2 lGrp lRes) (n, b), Folds Int Int n a b)
- data Folds sLens sVals n a b :: * -> * -> * -> * -> * -> * = Folds {
- _stateLens :: !sLens
- _stateVals :: !sVals
- _nameSeg :: !(Option n)
- _lenSeg :: !Int
- _valSeg :: !b
- type FoldsDict lSeg lElt lGrp tGrp lRes tRes n a b = (Bulk lSeg (n, Int), Bulk lElt a, Target lGrp n, Target lRes b, Index lGrp ~ Index lRes, Unpack (Buffer lGrp n) tGrp, Unpack (Buffer lRes b) tRes)
Array Access
class Layout l => Bulk l a where Source
Class of array representations that we can read elements from in a random-access manner.
An Array supplies an element of type a
to each position in the
index space associated with layout l
.
layout :: Array l a -> l Source
O(1). Get the layout of an array.
index :: Array l a -> Index l -> a Source
O(1). Get an element from an array. If the provided index is outside the extent of the array then the result depends on the layout.
Storable a => Bulk S a | |
Bulk L Int | Linear arrays. |
Bulk B a | Boxed arrays. |
Storable a => Bulk F a | Foreign arrays. |
Unbox a => Bulk U a | Unboxed arrays. |
Bulk A Char | |
Bulk A Double | |
Bulk A Float | |
Bulk A Int | |
Bulk A Int8 | |
Bulk A Int16 | |
Bulk A Int32 | |
Bulk A Int64 | |
Bulk A Word8 | |
Bulk A Date32 | |
Bulk A a => Bulk A [a] | |
(BulkI l a, Windowable l a) => Bulk N (Array l a) | Nested arrays. |
(Bulk A a, Bulk A b) => Bulk A (a, b) | |
(Bulk A a, Bulk A b) => Bulk A ((:*:) a b) | |
(Bulk A a, Windowable r a, (~) * (Index r) Int) => Bulk A (Array r a) | |
Bulk l a => Bulk (W l) a | Windowed arrays. |
(Layout (RW sh), (~) * (Index (RW sh)) sh) => Bulk (RW sh) sh | Row-wise arrays. |
Layout l => Bulk (D l) a | Delayed arrays. |
(Layout l1, Layout l2, (~) * (Index l1) (Index l2)) => Bulk (D2 l1 l2) a | Delayed arrays. |
((~) * (Index r) Int, Layout l, Bulk r a) => Bulk (E r l) a | Dense arrays. |
(Bulk l1 a, Bulk l2 b, (~) * (Index l1) (Index l2)) => Bulk (T2 l1 l2) (a, b) | Tupled arrays. |
type BulkI l a = (Bulk l a, Index l ~ Int) Source
Constraint synonym that requires an integer index space.
Array Computation
class (Bulk l1 a, Target l2 a) => Load l1 l2 a Source
Compute all elements defined by a delayed array and write them to a manifest target representation.
The instances of this class require that the source array has a delayed
representation. If you want to use a pre-existing manifest array as the
source then delay
it first.
class Layout l => Target l a Source
Class of manifest array representations that can be constructed in a random-access manner.
unsafeNewBuffer, unsafeReadBuffer, unsafeWriteBuffer, unsafeGrowBuffer, unsafeSliceBuffer, unsafeFreezeBuffer, unsafeThawBuffer, touchBuffer, bufferLayout
Target B a | Boxed buffers. |
Storable a => Target F a | Foreign buffers |
Unbox a => Target U a | Unboxed buffers. |
Target A Char | |
Target A Double | |
Target A Float | |
Target A Int | |
Target A Int8 | |
Target A Int16 | |
Target A Int32 | |
Target A Int64 | |
Target A Word8 | |
Target A Date32 | |
Target A [a] | |
(Bulk l a, Target l a, (~) * (Index l) Int) => Target N (Array l a) | |
(Target A a, Target A b) => Target A (a, b) | |
(Target A a, Target A b) => Target A ((:*:) a b) | |
(Bulk l a, Target l a, (~) * (Index l) Int) => Target A (Array l a) | |
(Layout l, (~) * (Index r) Int, Target r a) => Target (E r l) a | Dense buffers. |
(Target l1 a, Target l2 b, (~) * (Index l1) (Index l2)) => Target (T2 l1 l2) (a, b) | Tupled buffers. |
type TargetI l a = (Target l a, Index l ~ Int) Source
Constraint synonym that requires an integer index space.
computeS :: (Load lSrc lDst a, Index lSrc ~ Index lDst) => Name lDst -> Array lSrc a -> Array lDst a Source
Sequential computation of delayed array elements.
Elements of the source array are computed sequentially and written to a new array of the specified layout.
Operators
Conversion
fromList :: TargetI l a => Name l -> [a] -> Array l a Source
O(length src). Construct a linear array from a list of elements.
fromListInto :: Target l a => l -> [a] -> Maybe (Array l a) Source
convert :: Convert l1 a1 l2 a2 => Name l2 -> Array l1 a1 -> Array l2 a2 Source
O(1). Constant time conversion of one array representation to another.
copy :: (Bulk l1 a, Target l2 a, Index l1 ~ Index l2) => Name l2 -> Array l1 a -> Array l2 a Source
O(n). Linear time copy of one array representation to another.
This function must be used instead of convert
when the bit-wise
layout of the two array representations are different.
Mapping
Merging
:: (Ord k, BulkI l1 (k, a), BulkI l2 (k, b), TargetI lDst (k, c), Unpack (Buffer lDst (k, c)) t0) | |
=> Name lDst | Name of destination layout. |
-> (k -> a -> b -> c) | Combine two values with the same key. |
-> (k -> a -> c) | Handle a left value without a right value. |
-> (k -> b -> c) | Handle a right value without a left value. |
-> Array l1 (k, a) | Array of keys and left values. |
-> Array l2 (k, b) | Array of keys and right values. |
-> Array lDst (k, c) | Array of keys and results. |
Merge two sorted key-value streams.
:: (Ord k, BulkI l1 (k, a), BulkI l2 (k, b), TargetI lDst (k, c), Unpack (Buffer lDst (k, c)) t0) | |
=> Name lDst | |
-> (k -> a -> b -> Maybe c) | Combine two values with the same key. |
-> (k -> a -> Maybe c) | Handle a left value without a right value. |
-> (k -> b -> Maybe c) | Handle a right value without a left value. |
-> Array l1 (k, a) | Array of keys and left values. |
-> Array l2 (k, b) | Array of keys and right values. |
-> Array lDst (k, c) | Array of keys and results. |
Splitting
compact :: (BulkI lSrc a, TargetI lDst b, Unpack (Buffer lDst b) t0) => Name lDst -> (s -> a -> (Maybe b, s)) -> s -> Array lSrc a -> Array lDst b Source
Combination of fold
and filter
.
We walk over the stream front to back, maintaining an accumulator. At each point we can chose to emit an element (or not)
compactIn :: (BulkI lSrc a, TargetI lDst a, Unpack (Buffer lDst a) t0) => Name lDst -> (a -> a -> (Maybe a, a)) -> Array lSrc a -> Array lDst a Source
Like compact
but use the first value of the stream as the
initial state, and add the final state to the end of the output.
Filtering
filter :: (BulkI lSrc a, TargetI lDst a) => Name lDst -> (a -> Bool) -> Array lSrc a -> Array lDst a Source
Keep the elements of an array that match the given predicate.
Inserting
:: (BulkI lSrc a, TargetI lDst a, Unpack (Buffer lDst a) t0) | |
=> Name lDst | Name of destination layout. |
-> (Int -> Maybe a) | Produce an element for this index. |
-> Array lSrc a | Array of source elements. |
-> Array lDst a |
Insert elements produced by the given function in to an array.
Searching
Sloshing
Sloshing operators copy array elements into a different arrangement, but do not create new element values.
:: ConcatDict lOut lIn tIn lDst a | |
=> Name lDst | Layout for destination. |
-> Array lOut (Array lIn a) | Arrays to concatenate. |
-> Array lDst a |
O(len result) Concatenate nested arrays.
> import Data.Repa.Array.Material > let arrs = fromList B [fromList U [1, 2, 3], fromList U [5, 6, 7 :: Int]] > toList $ concat U arrs [1,2,3,5,6,7]
:: (ConcatDict lOut lIn tIn lDst a, BulkI lSep a) | |
=> Name lDst | Result representation. |
-> Array lSep a | Separator array. |
-> Array lOut (Array lIn a) | Arrays to concatenate. |
-> Array lDst a |
O(len result) Concatenate the elements of some nested vector, inserting a copy of the provided separator array between each element.
> import Data.Repa.Array.Material > let sep = fromList U [0, 0, 0] > let arrs = fromList B [fromList U [1, 2, 3], fromList U [5, 6, 7 :: Int]] > toList $ concatWith U sep arrs [1,2,3,0,0,0,5,6,7,0,0,0]
:: ConcatDict lOut lIn tIn lDst Char | |
=> Name lDst | Result representation. |
-> Array lOut (Array lIn Char) | Arrays to concatenate. |
-> Array lDst Char |
O(len result). Perform a concatWith
, adding a newline character to the
end of each inner array.
:: (ConcatDict lOut lIn tIn lDst a, BulkI lSep a) | |
=> Name lDst | Result representation. |
-> Array lSep a | Separator array. |
-> Array lOut (Array lIn a) | Arrays to concatenate. |
-> Array lDst a |
O(len result) Insert a copy of the separator array between the elements of the second and concatenate the result.
> import Data.Repa.Array.Material > let sep = fromList U [0, 0, 0] > let arrs = fromList B [fromList U [1, 2, 3], fromList U [5, 6, 7 :: Int]] > toList $ intercalate U sep arrs [1,2,3,0,0,0,5,6,7]
type ConcatDict lOut lIn tIn lDst a = (BulkI lOut (Array lIn a), BulkI lIn a, TargetI lDst a, Unpack (Array lIn a) tIn) Source
Dictionaries needed to perform a concatenation.
Grouping
:: (GroupsDict lElt lGrp tGrp lLen tLen n, Eq n) | |
=> Name lGrp | Layout for group names. |
-> Name lLen | Layout gor group lengths. |
-> Array lElt n | Input elements. |
-> (Array (T2 lGrp lLen) (n, Int), Maybe (n, Int)) |
From a stream of values which has consecutive runs of idential values, produce a stream of the lengths of these runs.
> import Data.Repa.Array.Material > import Data.Repa.Nice > nice $ groups U U (fromList U "waaabllle") ([(w
,1),(a
,3),(b
,1),(l
,3)],Just (e
,1))
:: GroupsDict lElt lGrp tGrp lLen tLen n | |
=> Name lGrp | Layout for group names. |
-> Name lLen | Layout for group lengths. |
-> (n -> n -> Bool) | Comparison function. |
-> Maybe (n, Int) | Starting element and count. |
-> Array lElt n | Input elements. |
-> (Array (T2 lGrp lLen) (n, Int), Maybe (n, Int)) |
Like groups
, but use the given function to determine whether two
consecutive elements should be in the same group. Also take
an initial starting group and count.
> import Data.Repa.Array.Material > import Data.Repa.Nice > nice $ groupsWith U U (==) (Just (w
, 5)) (fromList U "waaabllle") ([(w
,6),(a
,3),(b
,1),(l
,3)],Just (e
,1))
type GroupsDict lElt lGrp tGrp lLen tLen n = (Bulk lElt n, Target lGrp n, Target lLen Int, Index lGrp ~ Index lLen, Unpack (Buffer lLen Int) tLen, Unpack (Buffer lGrp n) tGrp) Source
Dictionaries need to perform a grouping.
Folding
Complete fold
foldl :: (Bulk l b, Index l ~ Int) => (a -> b -> a) -> a -> Array l b -> a Source
Left fold of all elements in an array, sequentially.
mean :: (BulkI l a, Fractional a) => Array l a -> a Source
Yield the mean value of the elements of an array.
std :: (BulkI l a, Floating a) => Array l a -> a Source
Yield the standard deviation of the elements of an array
correlate :: (BulkI l1 a, BulkI l2 a, Floating a) => Array l1 a -> Array l2 a -> a Source
Compute the Pearson correlation of two arrays.
If the arrays differ in length then only the common prefix is correlated.
Segmented fold
:: FoldsDict lSeg lElt lGrp tGrp lRes tRes n a b | |
=> Name lGrp | Layout for group names. |
-> Name lRes | Layout for fold results. |
-> (a -> b -> b) | Worker function. |
-> b | Initial state when folding segments. |
-> Array lSeg (n, Int) | Segment names and lengths. |
-> Array lElt a | Elements. |
-> (Array (T2 lGrp lRes) (n, b), Folds Int Int n a b) |
Segmented fold over vectors of segment lengths and input values.
- The total lengths of all segments need not match the length of the
input elements vector. The returned
Folds
state can be inspected to determine whether all segments were completely folded, or the vector of segment lengths or elements was too short relative to the other.
> import Data.Repa.Array.Material > import Data.Repa.Nice > let segs = fromList B [("red", 3), ("green", 5)] > let vals = fromList U [0..100 :: Int] > nice $ fst $ folds B U (+) 0 segs vals [("red",3),("green",25)]
:: FoldsDict lSeg lElt lGrp tGrp lRes tRes n a b | |
=> Name lGrp | Layout for group names. |
-> Name lRes | Layout for fold results. |
-> (a -> b -> b) | Worker function. |
-> b | Initial state when folding segments. |
-> Maybe (n, Int, b) | Name, length and initial state for first segment. |
-> Array lSeg (n, Int) | Segment names and lengths. |
-> Array lElt a | Elements. |
-> (Array (T2 lGrp lRes) (n, b), Folds Int Int n a b) |
Like folds
, but take an initial state for the first segment.
> import Data.Repa.Array.Material > import Data.Repa.Nice > let state = Just ("white", 4, 100) > let segs = fromList B [("red", 3), ("green", 5)] > let vals = fromList U [0..100 :: Int] > nice $ fst $ foldsWith B U (+) 0 state segs vals [("white",106),("red",15),("green",45)]
data Folds sLens sVals n a b :: * -> * -> * -> * -> * -> *
Return state of a folds operation.
Folds | |
|