Copyright  (c) Christopher Chalmers 

License  BSD3 
Maintainer  Christopher Chalmers 
Stability  provisional 
Portability  nonportable 
Safe Haskell  None 
Language  Haskell2010 
Boxed multidimensional arrays.
Synopsis
 type BArray = Array Vector
 class (Eq1 f, Additive f, Traversable f) => Shape f
 class Shape f => HasLayout f a  a > f where
 type Layout f = f Int
 extent :: HasLayout f a => a > f Int
 size :: HasLayout f a => a > Int
 indexes :: HasLayout f a => IndexedFold Int a (f Int)
 indexesFrom :: HasLayout f a => f Int > IndexedFold Int a (f Int)
 indexesBetween :: HasLayout f a => f Int > f Int > IndexedFold Int a (f Int)
 vector :: IndexedLens (Layout f) (BArray f a) (BArray f b) (Vector a) (Vector b)
 values :: Shape f => IndexedTraversal (f Int) (BArray f a) (BArray f b) a b
 values' :: Shape f => IndexedTraversal (f Int) (BArray f a) (BArray f b) a b
 valuesBetween :: Shape f => f Int > f Int > IndexedTraversal' (f Int) (BArray f a) a
 flat :: Iso (BArray V1 a) (BArray V1 b) (Vector a) (Vector b)
 fromList :: [a] > BArray V1 a
 fromListInto :: Shape f => Layout f > [a] > Maybe (BArray f a)
 fromListInto_ :: Shape f => Layout f > [a] > BArray f a
 fromVectorInto :: Shape f => Layout f > Vector a > Maybe (BArray f a)
 fromVectorInto_ :: Shape f => Layout f > Vector a > BArray f a
 replicate :: Shape f => f Int > a > BArray f a
 generate :: Shape f => Layout f > (f Int > a) > BArray f a
 linearGenerate :: Shape f => Layout f > (Int > a) > BArray f a
 create :: (forall s. ST s (BMArray f s a)) > BArray f a
 replicateM :: (Monad m, Shape f) => Layout f > m a > m (BArray f a)
 generateM :: (Monad m, Shape f) => Layout f > (f Int > m a) > m (BArray f a)
 linearGenerateM :: (Monad m, Shape f) => Layout f > (Int > m a) > m (BArray f a)
 empty :: Additive f => BArray f a
 null :: Foldable f => BArray f a > Bool
 (!) :: Shape f => BArray f a > f Int > a
 (!?) :: Shape f => BArray f a > f Int > Maybe a
 unsafeIndex :: Shape f => BArray f a > f Int > a
 linearIndex :: BArray f a > Int > a
 unsafeLinearIndex :: BArray f a > Int > a
 indexM :: (Shape f, Monad m) => BArray f a > f Int > m a
 unsafeIndexM :: (Shape f, Monad m) => BArray f a > f Int > m a
 linearIndexM :: (Shape f, Monad m) => BArray f a > Int > m a
 unsafeLinearIndexM :: Monad m => BArray f a > Int > m a
 (//) :: Shape f => BArray f a > [(f Int, a)] > BArray f a
 accum :: Shape f => (a > b > a) > BArray f a > [(f Int, b)] > BArray f a
 map :: (a > b) > BArray f a > BArray f b
 imap :: Shape f => (f Int > a > b) > BArray f a > BArray f b
 zip :: Shape f => BArray f a > BArray f b > BArray f (a, b)
 zip3 :: Shape f => BArray f a > BArray f b > BArray f c > BArray f (a, b, c)
 zipWith :: Shape f => (a > b > c) > BArray f a > BArray f b > BArray f c
 zipWith3 :: Shape f => (a > b > c > d) > BArray f a > BArray f b > BArray f c > BArray f d
 izipWith :: Shape f => (f Int > a > b > c) > BArray f a > BArray f b > BArray f c
 izipWith3 :: Shape f => (f Int > a > b > c > d) > BArray f a > BArray f b > BArray f c > BArray f d
 ixRow :: Int > IndexedTraversal' Int (BArray V2 a) (Vector a)
 rows :: IndexedTraversal Int (BArray V2 a) (BArray V2 b) (Vector a) (Vector b)
 ixColumn :: Int > IndexedTraversal' Int (BArray V2 a) (Vector a)
 columns :: IndexedTraversal Int (BArray V2 a) (BArray V2 b) (Vector a) (Vector b)
 ixPlane :: ALens' (V3 Int) (V2 Int) > Int > IndexedTraversal' Int (BArray V3 a) (BArray V2 a)
 planes :: ALens' (V3 Int) (V2 Int) > IndexedTraversal Int (BArray V3 a) (BArray V3 b) (BArray V2 a) (BArray V2 b)
 flattenPlane :: ALens' (V3 Int) (V2 Int) > (Vector a > b) > BArray V3 a > BArray V2 b
 unsafeOrdinals :: Shape f => [f Int] > IndexedTraversal' (f Int) (BArray f a) a
 type BMArray = MArray MVector
 thaw :: PrimMonad m => BArray f a > m (BMArray f (PrimState m) a)
 freeze :: PrimMonad m => BMArray f (PrimState m) a > m (BArray f a)
 unsafeThaw :: PrimMonad m => BArray f a > m (BMArray f (PrimState m) a)
 unsafeFreeze :: PrimMonad m => BMArray f (PrimState m) a > m (BArray f a)
 data Delayed f a
 delayed :: (Shape f, Shape k) => Iso (BArray f a) (BArray k b) (Delayed f a) (Delayed k b)
 seqDelayed :: (Shape f, Shape k) => Iso (BArray f a) (BArray k b) (Delayed f a) (Delayed k b)
 delay :: Shape f => BArray f a > Delayed f a
 manifest :: Shape f => Delayed f a > BArray f a
 seqManifest :: Shape f => Delayed f a > BArray f a
 genDelayed :: Layout f > (f Int > a) > Delayed f a
 indexDelayed :: Shape f => Delayed f a > f Int > a
 affirm :: Shape f => Delayed f a > Delayed f a
 seqAffirm :: Shape f => Delayed f a > Delayed f a
 data Focused f a
 focusOn :: f Int > Delayed f a > Focused f a
 unfocus :: Focused f a > Delayed f a
 unfocused :: IndexedLens (f Int) (Focused f a) (Focused f b) (Delayed f a) (Delayed f b)
 extendFocus :: Shape f => (Focused f a > b) > Delayed f a > Delayed f b
 locale :: ComonadStore s w => Lens' (w a) s
 shiftFocus :: Applicative f => f Int > Focused f a > Focused f a
BArray types
class (Eq1 f, Additive f, Traversable f) => Shape f Source #
Class for types that can be converted to and from linear indexes.
Instances
Layout of an array
class Shape f => HasLayout f a  a > f where Source #
Class of things that have a Layout
. This means we can use the
same functions for the various different arrays in the library.
Nothing
Instances
i ~ Int => HasLayout V0 (V0 i) Source #  
i ~ Int => HasLayout V4 (V4 i) Source #  
i ~ Int => HasLayout V3 (V3 i) Source #  
i ~ Int => HasLayout V2 (V2 i) Source #  
i ~ Int => HasLayout V1 (V1 i) Source #  
Shape f => HasLayout f (Focused f a) Source #  The 
Shape f => HasLayout f (Delayed f a) Source #  The 
Shape f => HasLayout f (Array v f a) Source #  The 
Shape f => HasLayout f (MArray v f s a) Source #  
type Layout f = f Int Source #
A Layout
is the full size of an array. This alias is used to help
distinguish between the layout of an array and an index (usually
just l Int
) in a type signature.
Extracting size
Folds over indexes
indexes :: HasLayout f a => IndexedFold Int a (f Int) Source #
Indexed fold for all the indexes in the layout.
indexesFrom :: HasLayout f a => f Int > IndexedFold Int a (f Int) Source #
Indexed fold starting starting from some point, where the index is the linear index for the original layout.
indexesBetween :: HasLayout f a => f Int > f Int > IndexedFold Int a (f Int) Source #
Indexed fold between the two indexes where the index is the linear index for the original layout.
Underlying vector
Traversals
values :: Shape f => IndexedTraversal (f Int) (BArray f a) (BArray f b) a b Source #
Same as values
but restrictive in the vector type.
values' :: Shape f => IndexedTraversal (f Int) (BArray f a) (BArray f b) a b Source #
Same as values
but restrictive in the vector type.
valuesBetween :: Shape f => f Int > f Int > IndexedTraversal' (f Int) (BArray f a) a Source #
Same as values
but restrictive in the vector type.
Construction
Flat arrays
From lists
fromListInto :: Shape f => Layout f > [a] > Maybe (BArray f a) Source #
O(n) Convert the first n
elements of a list to an BArrayith the
given shape. Returns Nothing
if there are not enough elements in
the list.
fromListInto_ :: Shape f => Layout f > [a] > BArray f a Source #
O(n) Convert the first n
elements of a list to an BArrayith the
given shape. Throw an error if the list is not long enough.
From vectors
Initialisation
replicate :: Shape f => f Int > a > BArray f a Source #
O(n) BArray of the given shape with the same value in each position.
generate :: Shape f => Layout f > (f Int > a) > BArray f a Source #
O(n) Construct an array of the given shape by applying the function to each index.
linearGenerate :: Shape f => Layout f > (Int > a) > BArray f a Source #
O(n) Construct an array of the given shape by applying the function to each index.
Monadic initialisation
create :: (forall s. ST s (BMArray f s a)) > BArray f a Source #
Execute the monadic action and freeze the resulting array.
replicateM :: (Monad m, Shape f) => Layout f > m a > m (BArray f a) Source #
O(n) Construct an array of the given shape by filling each position with the monadic value.
generateM :: (Monad m, Shape f) => Layout f > (f Int > m a) > m (BArray f a) Source #
O(n) Construct an array of the given shape by applying the monadic function to each index.
linearGenerateM :: (Monad m, Shape f) => Layout f > (Int > m a) > m (BArray f a) Source #
O(n) Construct an array of the given shape by applying the monadic function to each index.
Functions on arrays
Empty arrays
Indexing
(!) :: Shape f => BArray f a > f Int > a Source #
Index an element of an array. Throws IndexOutOfBounds
if the
index is out of bounds.
unsafeIndex :: Shape f => BArray f a > f Int > a Source #
Index an element of an array without bounds checking.
linearIndex :: BArray f a > Int > a Source #
Index an element of an array while ignoring its shape.
unsafeLinearIndex :: BArray f a > Int > a Source #
Index an element of an array while ignoring its shape, without bounds checking.
Monadic indexing
indexM :: (Shape f, Monad m) => BArray f a > f Int > m a Source #
O(1) Indexing in a monad.
The monad allows operations to be strict in the vector when necessary. Suppose vector copying is implemented like this:
copy mv v = ... write mv i (v ! i) ...
For lazy vectors, v ! i
would not be evaluated which means that
mv
would unnecessarily retain a reference to v
in each element
written.
With indexM
, copying can be implemented like this instead:
copy mv v = ... do x < indexM v i write mv i x
Here, no references to v
are retained because indexing (but not
the elements) is evaluated eagerly.
Throws an error if the index is out of range.
unsafeIndexM :: (Shape f, Monad m) => BArray f a > f Int > m a Source #
O(1) Indexing in a monad without bounds checks. See indexM
for an
explanation of why this is useful.
linearIndexM :: (Shape f, Monad m) => BArray f a > Int > m a Source #
O(1) Indexing in a monad. Throws an error if the index is out of range.
unsafeLinearIndexM :: Monad m => BArray f a > Int > m a Source #
O(1) Indexing in a monad without bounds checks. See indexM
for an
explanation of why this is useful.
Modifying arrays
Bulk updates
(//) :: Shape f => BArray f a > [(f Int, a)] > BArray f a Source #
For each pair (i,a) from the list, replace the array element at position i by a.
Accumulations
:: Shape f  
=> (a > b > a)  accumulating function 
> BArray f a  initial array 
> [(f Int, b)]  list of index/value pairs (of length 
> BArray f a 
O(m+n) For each pair (i,b)
from the list, replace the array element
a
at position i
by f a b
.
Mapping
imap :: Shape f => (f Int > a > b) > BArray f a > BArray f b Source #
O(n) Apply a function to every element of a vector and its index
Zipping
Tuples
zip :: Shape f => BArray f a > BArray f b > BArray f (a, b) Source #
Zip two arrays element wise. If the array's don't have the same shape, the new array with be the intersection of the two shapes.
zip3 :: Shape f => BArray f a > BArray f b > BArray f c > BArray f (a, b, c) Source #
Zip three arrays element wise. If the array's don't have the same shape, the new array with be the intersection of the two shapes.
Zip with function
zipWith :: Shape f => (a > b > c) > BArray f a > BArray f b > BArray f c Source #
Zip two arrays using the given function. If the array's don't have the same shape, the new array with be the intersection of the two shapes.
zipWith3 :: Shape f => (a > b > c > d) > BArray f a > BArray f b > BArray f c > BArray f d Source #
Zip three arrays using the given function. If the array's don't have the same shape, the new array with be the intersection of the two shapes.
izipWith :: Shape f => (f Int > a > b > c) > BArray f a > BArray f b > BArray f c Source #
Zip two arrays using the given function with access to the index. If the array's don't have the same shape, the new array with be the intersection of the two shapes.
izipWith3 :: Shape f => (f Int > a > b > c > d) > BArray f a > BArray f b > BArray f c > BArray f d Source #
Zip two arrays using the given function with access to the index. If the array's don't have the same shape, the new array with be the intersection of the two shapes.
Slices
Matrix
ixRow :: Int > IndexedTraversal' Int (BArray V2 a) (Vector a) Source #
Affine traversal over a single row in a matrix.
>>>
traverseOf_ rows print $ m & ixRow 1 . each *~ 2
[a,b,c,d] [e * 2,f * 2,g * 2,h * 2] [i,j,k,l]
The row vector should remain the same size to satisfy traversal laws but give reasonable behaviour if the size differs:
>>>
traverseOf_ rows print $ m & ixRow 1 .~ V.fromList [0,1]
[a,b,c,d] [0,1,g,h] [i,j,k,l]
>>>
traverseOf_ rows print $ m & ixRow 1 .~ V.fromList [0..100]
[a,b,c,d] [0,1,2,3] [i,j,k,l]
rows :: IndexedTraversal Int (BArray V2 a) (BArray V2 b) (Vector a) (Vector b) Source #
Indexed traversal over the rows of a matrix. Each row is an
efficient slice
of the original vector.
>>>
traverseOf_ rows print m
[a,b,c,d] [e,f,g,h] [i,j,k,l]
ixColumn :: Int > IndexedTraversal' Int (BArray V2 a) (Vector a) Source #
Affine traversal over a single column in a matrix.
>>>
traverseOf_ rows print $ m & ixColumn 2 . each +~ 1
[a,b,c + 1,d] [e,f,g + 1,h] [i,j,k + 1,l]
columns :: IndexedTraversal Int (BArray V2 a) (BArray V2 b) (Vector a) (Vector b) Source #
Indexed traversal over the columns of a matrix. Unlike rows
, each
column is a new separate vector.
>>>
traverseOf_ columns print m
[a,e,i] [b,f,j] [c,g,k] [d,h,l]
>>>
traverseOf_ rows print $ m & columns . indices odd . each .~ 0
[a,0,c,0] [e,0,g,0] [i,0,k,0]
The vectors should be the same size to be a valid traversal. If the vectors are different sizes, the number of rows in the new array will be the length of the smallest vector.
3D
ixPlane :: ALens' (V3 Int) (V2 Int) > Int > IndexedTraversal' Int (BArray V3 a) (BArray V2 a) Source #
planes :: ALens' (V3 Int) (V2 Int) > IndexedTraversal Int (BArray V3 a) (BArray V3 b) (BArray V2 a) (BArray V2 b) Source #
flattenPlane :: ALens' (V3 Int) (V2 Int) > (Vector a > b) > BArray V3 a > BArray V2 b Source #
Flatten a plane by reducing a vector in the third dimension to a single value.
Ordinals
unsafeOrdinals :: Shape f => [f Int] > IndexedTraversal' (f Int) (BArray f a) a Source #
This Traversal
should not have any duplicates in the list of
indices.
Mutable
thaw :: PrimMonad m => BArray f a > m (BMArray f (PrimState m) a) Source #
O(n) Yield an immutable copy of the mutable array.
freeze :: PrimMonad m => BMArray f (PrimState m) a > m (BArray f a) Source #
O(n) Yield a mutable copy of the immutable vector.
unsafeThaw :: PrimMonad m => BArray f a > m (BMArray f (PrimState m) a) Source #
O(1) Unsafely convert an immutable array to a mutable one without copying. The immutable array may not be used after this operation.
unsafeFreeze :: PrimMonad m => BMArray f (PrimState m) a > m (BArray f a) Source #
O(1) Unsafe convert a mutable array to an immutable one without copying. The mutable array may not be used after this operation.
Delayed
A delayed representation of an array. This useful for mapping over an array in parallel.
Instances
Shape f => HasLayout f (Delayed f a) Source #  The 
Functor (Delayed f) Source #  
Shape f => Foldable (Delayed f) Source # 

Defined in Data.Dense.Base fold :: Monoid m => Delayed f m > m # foldMap :: Monoid m => (a > m) > Delayed f a > m # foldMap' :: Monoid m => (a > m) > Delayed f a > m # foldr :: (a > b > b) > b > Delayed f a > b # foldr' :: (a > b > b) > b > Delayed f a > b # foldl :: (b > a > b) > b > Delayed f a > b # foldl' :: (b > a > b) > b > Delayed f a > b # foldr1 :: (a > a > a) > Delayed f a > a # foldl1 :: (a > a > a) > Delayed f a > a # toList :: Delayed f a > [a] # length :: Delayed f a > Int # elem :: Eq a => a > Delayed f a > Bool # maximum :: Ord a => Delayed f a > a # minimum :: Ord a => Delayed f a > a #  
Shape f => Traversable (Delayed f) Source #  
Shape f => Apply (Delayed f) Source #  
Shape f => Metric (Delayed f) Source #  
Defined in Data.Dense.Base  
Shape f => Additive (Delayed f) Source #  
Defined in Data.Dense.Base zero :: Num a => Delayed f a # (^+^) :: Num a => Delayed f a > Delayed f a > Delayed f a # (^^) :: Num a => Delayed f a > Delayed f a > Delayed f a # lerp :: Num a => a > Delayed f a > Delayed f a > Delayed f a # liftU2 :: (a > a > a) > Delayed f a > Delayed f a > Delayed f a # liftI2 :: (a > b > c) > Delayed f a > Delayed f b > Delayed f c #  
FunctorWithIndex (f Int) (Delayed f) Source #  
Shape f => FoldableWithIndex (f Int) (Delayed f) Source # 

Defined in Data.Dense.Base ifoldMap :: Monoid m => (f Int > a > m) > Delayed f a > m # ifolded :: IndexedFold (f Int) (Delayed f a) a # ifoldr :: (f Int > a > b > b) > b > Delayed f a > b # ifoldl :: (f Int > b > a > b) > b > Delayed f a > b # ifoldr' :: (f Int > a > b > b) > b > Delayed f a > b # ifoldl' :: (f Int > b > a > b) > b > Delayed f a > b #  
Shape f => TraversableWithIndex (f Int) (Delayed f) Source #  
Defined in Data.Dense.Base itraverse :: Applicative f0 => (f Int > a > f0 b) > Delayed f a > f0 (Delayed f b) # itraversed :: IndexedTraversal (f Int) (Delayed f a) (Delayed f b) a b #  
(Shape f, Show1 f, Show a) => Show (Delayed f a) Source #  
Shape f => Ixed (Delayed f a) Source #  
Defined in Data.Dense.Base  
Shape f => AsEmpty (Delayed f a) Source #  
Defined in Data.Dense.Base  
Shape f => Each (Delayed f a) (Delayed f b) a b Source #  
type Index (Delayed f a) Source #  
Defined in Data.Dense.Base  
type IxValue (Delayed f a) Source #  
Defined in Data.Dense.Base 
Generating delayed
delayed :: (Shape f, Shape k) => Iso (BArray f a) (BArray k b) (Delayed f a) (Delayed k b) Source #
Isomorphism between an array and its delayed representation. Conversion to the array is done in parallel.
seqDelayed :: (Shape f, Shape k) => Iso (BArray f a) (BArray k b) (Delayed f a) (Delayed k b) Source #
Isomorphism between an array and its delayed representation. Conversion to the array is done in sequence.
delay :: Shape f => BArray f a > Delayed f a Source #
Turn a material array into a delayed one with the same shape.
manifest :: Shape f => Delayed f a > BArray f a Source #
Parallel manifestation of a delayed array into a material one.
seqManifest :: Shape f => Delayed f a > BArray f a Source #
Sequential manifestation of a delayed array.
indexDelayed :: Shape f => Delayed f a > f Int > a Source #
Index a delayed array, returning a IndexOutOfBounds
exception if
the index is out of range.
seqAffirm :: Shape f => Delayed f a > Delayed f a Source #
seqManifest
an array to a BArray
and delay again.
Focused
A delayed representation of an array with a focus on a single
element. This element is the target of extract
.
Instances
Shape f => HasLayout f (Focused f a) Source #  The 
Functor (Focused f) Source #  
Shape f => Foldable (Focused f) Source #  
Defined in Data.Dense.Base fold :: Monoid m => Focused f m > m # foldMap :: Monoid m => (a > m) > Focused f a > m # foldMap' :: Monoid m => (a > m) > Focused f a > m # foldr :: (a > b > b) > b > Focused f a > b # foldr' :: (a > b > b) > b > Focused f a > b # foldl :: (b > a > b) > b > Focused f a > b # foldl' :: (b > a > b) > b > Focused f a > b # foldr1 :: (a > a > a) > Focused f a > a # foldl1 :: (a > a > a) > Focused f a > a # toList :: Focused f a > [a] # length :: Focused f a > Int # elem :: Eq a => a > Focused f a > Bool # maximum :: Ord a => Focused f a > a # minimum :: Ord a => Focused f a > a #  
Shape f => Traversable (Focused f) Source #  
Shape f => Comonad (Focused f) Source #  
Shape f => Extend (Focused f) Source #  
Shape f => ComonadStore (f Int) (Focused f) Source #  
Defined in Data.Dense.Base  
Shape f => FunctorWithIndex (f Int) (Focused f) Source #  Index relative to focus. 
Shape f => FoldableWithIndex (f Int) (Focused f) Source #  Index relative to focus. 
Defined in Data.Dense.Base ifoldMap :: Monoid m => (f Int > a > m) > Focused f a > m # ifolded :: IndexedFold (f Int) (Focused f a) a # ifoldr :: (f Int > a > b > b) > b > Focused f a > b # ifoldl :: (f Int > b > a > b) > b > Focused f a > b # ifoldr' :: (f Int > a > b > b) > b > Focused f a > b # ifoldl' :: (f Int > b > a > b) > b > Focused f a > b #  
Shape f => TraversableWithIndex (f Int) (Focused f) Source #  Index relative to focus. 
Defined in Data.Dense.Base itraverse :: Applicative f0 => (f Int > a > f0 b) > Focused f a > f0 (Focused f b) # itraversed :: IndexedTraversal (f Int) (Focused f a) (Focused f b) a b #  
(Shape f, Show1 f, Show a) => Show (Focused f a) Source #  
Shape f => Ixed (Focused f a) Source #  Index relative to focus. 
Defined in Data.Dense.Base  
type Index (Focused f a) Source #  
Defined in Data.Dense.Base  
type IxValue (Focused f a) Source #  
Defined in Data.Dense.Base 
Generating focused
focusOn :: f Int > Delayed f a > Focused f a Source #
Focus on a particular element of a delayed array.
unfocused :: IndexedLens (f Int) (Focused f a) (Focused f b) (Delayed f a) (Delayed f b) Source #
Indexed lens onto the delayed array, indexed at the focus.
Focus location
locale :: ComonadStore s w => Lens' (w a) s Source #
Lens onto the position of a ComonadStore
.
locale
::Lens'
(Focused
l a) (lInt
)
shiftFocus :: Applicative f => f Int > Focused f a > Focused f a Source #
Focus on a neighbouring element, relative to the current focus.