HORIZON HASKELLDocslts/ghc-10.0.x8c2f8e82026-10-19Search names, modules, packages, or :: a typeCtrl K

GHC 10.0.0.20260917 · lts/ghc-10.0.x · 8c2f8e8 · 2026-10-19

Modulemassiv-1.0.5.0Haskell2010

Data.Massiv.Array

Massiv is a library, that allows creation and manipulation of arrays in parallel and sequentially. Depending on the representation (r), an Array r ix e will have certain properties that are unique to that particular representation, but all of them will share the same trait, that an array is simply a mapping from an index (ix) of an arbitrary dimension to an element (e) of some value. Which means that some representations describe classic arrays and are backed by a contiguous chunk of memory reserved for the elements (or pointers to elements), namely arrays with Manifest representations:

  • B - The most basic type of array that can hold any type of element in a boxed form, i.e. each element is a pointer to the actual value, therefore it is also the slowest representation. Elements are kept in a Weak Head Normal Form (WHNF).

  • BN - Similar to B, it is also a boxed type, except its elements are always kept in a Normal Form (NF). This property is very useful for parallel processing, i.e. when calling compute you do want all of your elements to be fully evaluated.

  • BL - Similar to B, it is also a boxed type, but lazy. Its elements are not evaluated when array is computed.

  • S - Is a type of array that is backed by pinned memory, therefore pointers to those arrays can be passed to FFI calls, because Garbage Collector (GC) is guaranteed not to move it. Elements must be an instance of Storable class. It is just as efficient as P and U arrays, except it is subject to fragmentation.

  • U - Unboxed representation. Elements must be an instance of Unbox class.

  • P - Array that can hold Haskell primitives, such as Int, Word, Double, etc. Any element must be an instance of Prim class.

There are also array representations that only describe how values for its elements can be computed or loaded into memory, as such, they are represented by functions and do not impose the memory overhead, that is normally associated with arrays. They are needed for proper fusion and parallelization of computation.

  • D - delayed array that is a mere function from an index to an element. Also known as Pull array. Crucial representation for fusing computation. Use computeAs in order to load array into Manifest representation.

  • DL - delayed load array representation that describes how an array can be loaded. Also known as Push array. Useful for fusing various array combining functions. Use computeAs in order to load array into Manifest representation.

  • DS - delayed stream vector representation that describes how to handle a vector with possibility of unknown length. Useful for filtering and unfolding. Use computeAs in order to load such vector into Manifest representation.

  • DI - delayed interleaved array. Same as D, but performs better with unbalanced computation, when evaluation of one element takes much longer than of its neighbor.

  • DW - delayed windowed array. This peculiar representation allows for very fast Stencil computation.

Other Array types:

  • L - this type isn't particularly useful on its own, but because it has unique ability to be converted to and from nested lists in constant time, it provides a perfect intermediary for conversion of nested lists into manifest arrays.

Most of the Manifest arrays are capable of in-place mutation. Check out Data.Massiv.Array.Mutable module for available functionality.

Many of the function names exported by this package will clash with the ones from Prelude, hence it can be more convenient to import like this:

import Prelude as P
import Data.Massiv.Array as A
  • 243 values
  • Packagemassiv-1.0.5.0
  • Exports250
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourceArray.hs

Construct

40 declarations
value(...) :: Index ix => ix -> ix -> Array D ix ix
#

Handy synonym for rangeInclusive Seq. Similar to .. for list.

Example1 expression
Ix1 4 ... 10Array D Seq (Sz1 7)  [ 4, 5, 6, 7, 8, 9, 10 ]
value(..:) :: Index ix => ix -> ix -> Array D ix ix
#

Handy synonym for range Seq

Example1 expression
Ix1 4 ..: 10Array D Seq (Sz1 6)  [ 4, 5, 6, 7, 8, 9 ]
valueenumFromN
  1. :: Num e
  2. => Comp
  3. -> e

    x - start value

  4. -> Sz1

    n - length of resulting vector.

  5. -> Vector D e
#

Same as enumFromStepN with step dx = 1.

Related: senumFromN, senumFromStepN, enumFromStepN, rangeSize, rangeStepSize, range

Examples
Example2 expressions
import Data.Massiv.ArrayenumFromN Seq (5 :: Double) 3Array D Seq (Sz1 3)  [ 5.0, 6.0, 7.0 ]

Similar:

Prelude.Prelude.enumFromTo

Very similar to

[i .. i + n - 1]

, except that

enumFromN

is faster, but it only works for

Num

and not for

Enum

elements

Data.Vector.Generic.enumFromN
valueenumFromStepN
  1. :: Num e
  2. => Comp
  3. -> e

    x - start number

  4. -> e

    dx - step number

  5. -> Sz1

    n - length of resulting vector

  6. -> Vector D e
#

Enumerate from a starting number x exactly n times with a custom step value dx. Unlike senumFromStepN, there is no dependency on neigboring elements therefore enumFromStepN is parallelizable.

Related: senumFromN, senumFromStepN, enumFromN, rangeSize, rangeStepSize, range, rangeStepM

Examples
Example3 expressions
import Data.Massiv.ArrayenumFromStepN Seq 1 (0.1 :: Double) 5Array D Seq (Sz1 5)  [ 1.0, 1.1, 1.2, 1.3, 1.4 ]enumFromStepN Seq (-pi :: Float) (pi/4) 9Array D Seq (Sz1 9)  [ -3.1415927, -2.3561945, -1.5707964, -0.78539824, 0.0, 0.78539824, 1.5707963, 2.3561947, 3.1415927 ]

Similar:

Prelude.enumFrom

Similar to

take n [x, x + dx ..]

, except that

enumFromStepN

is parallelizable and it only works for

Num

and not for

Enum

elements. Floating point value will be slightly different as well.

Data.Vector.Generic.enumFromStepN

Similar in the outcome, but very different in the way it works.

valueexpandWithin
  1. :: (IsIndexDimension ix n, Index (Lower ix), Manifest r a)
  2. => Dimension n
  3. -> Sz1
  4. -> a -> Ix1 -> e
  5. -> Array r (Lower ix) a
  6. -> Array D ix e
#

Function that expands an array to one with a higher dimension.

This is useful for constructing arrays where there is shared computation between multiple cells. The makeArray method of constructing arrays:

makeArray :: Construct r ix e => Comp -> ix -> (ix -> e) -> Array r ix e

...runs a function ix -> e at every array index. This is inefficient if there is a substantial amount of repeated computation that could be shared while constructing elements on the same dimension. The expand functions make this possible. First you construct an Array r (Lower ix) a of one fewer dimensions where a is something like Array r Ix1 a or Array r Ix2 a. Then you use expandWithin and a creation function a -> Int -> b to create an Array D Ix2 b or Array D Ix3 b respectfully.

Examples
Example5 expressions
import Data.Massiv.Arraya = makeArrayR U Seq (Sz1 6) (+10) -- Imagine (+10) is some expensive functionaArray U Seq (Sz1 6)  [ 10, 11, 12, 13, 14, 15 ]expandWithin Dim1 5 (\ e j -> (j + 1) * 100 + e) a :: Array D Ix2 IntArray D Seq (Sz (6 :. 5))  [ [ 110, 210, 310, 410, 510 ]  , [ 111, 211, 311, 411, 511 ]  , [ 112, 212, 312, 412, 512 ]  , [ 113, 213, 313, 413, 513 ]  , [ 114, 214, 314, 414, 514 ]  , [ 115, 215, 315, 415, 515 ]  ]expandWithin Dim2 5 (\ e j -> (j + 1) * 100 + e) a :: Array D Ix2 IntArray D Seq (Sz (5 :. 6))  [ [ 110, 111, 112, 113, 114, 115 ]  , [ 210, 211, 212, 213, 214, 215 ]  , [ 310, 311, 312, 313, 314, 315 ]  , [ 410, 411, 412, 413, 414, 415 ]  , [ 510, 511, 512, 513, 514, 515 ]  ]
valueiterateN :: Index ix => Sz ix -> (e -> e) -> e -> Array DL ix e
#

Sequentially iterate over each cell in the array in the row-major order while continuously aplying the accumulator at each step.

Example
Example2 expressions
import Data.Massiv.ArrayiterateN (Sz2 2 10) succ (10 :: Int)Array DL Seq (Sz (2 :. 10))  [ [ 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 ]  , [ 21, 22, 23, 24, 25, 26, 27, 28, 29, 30 ]  ]
valueiunfoldlS_ :: Index ix => Sz ix -> (ix -> a -> (a, e)) -> a -> Array DL ix e
#

Unfold sequentially from the right with an index aware function.

valueiunfoldrS_ :: Index ix => Sz ix -> (a -> ix -> (e, a)) -> a -> Array DL ix e
#

Right unfold of a delayed load array with index aware function

valuemakeArrayR :: Load r ix e => r -> Comp -> Sz ix -> (ix -> e) -> Array r ix e
#

Just like makeArray but with ability to specify the result representation as an argument. Note the Data.Massiv.Array.Unboxed type constructor in the below example.

Examples
Example2 expressions
import Data.Massiv.ArraymakeArrayR U Par (Sz (2 :> 3 :. 4)) (\ (i :> j :. k) -> i * i + j * j == k * k)Array U Par (Sz (2 :> 3 :. 4))  [ [ [ True, False, False, False ]    , [ False, True, False, False ]    , [ False, False, True, False ]    ]  , [ [ False, True, False, False ]    , [ False, False, False, False ]    , [ False, False, False, False ]    ]  ]
valuemakeSplitSeedArray
  1. :: (Iterator it, Index ix)
  2. => it

    Iterator

  3. -> g

    Initial seed

  4. -> (g -> (g, g))

    A function that can split a seed into two independent seeds. It will be called the same number of times as the number of jobs that will get scheduled during parallelization. Eg. only once for the sequential case.

  5. -> Comp

    Computation strategy.

  6. -> Sz ix

    Resulting size of the array.

  7. -> (Ix1 -> ix -> g -> (e, g))

    A function that produces a value and the next seed. It takes both versions of the index, in linear and in multi-dimensional forms, as well as the current seeding value.

  8. -> Array DL ix e
#

Create a delayed array with an initial seed and a splitting function. It is somewhat similar to iunfoldlS_ function, but it is capable of parallelizing computation and iterating over the array accoriding to the supplied Iterator. Upon parallelization every job will get the second part of the result produced by the split function, while the first part will be used for subsequent splits. This function is similar to Data.Massiv.Array.Manifest.generateSplitSeedArray

valuerandomArray
  1. :: Index ix
  2. => g

    Initial random value generator

  3. -> (g -> (g, g))

    A function that can split a generator into two independent generators. It will only be called if supplied computation strategy needs more than one worker threads.

  4. -> (g -> (e, g))

    A function that produces a random value and the next generator

  5. -> Comp

    Computation strategy.

  6. -> Sz ix

    Resulting size of the array.

  7. -> Array DL ix e
#

Create an array with random values by using a pure splittable random number generator such as one provided by either splitmix or random packages. If you don't have a splittable generator consider using randomArrayS or randomArrayWS instead.

Because of the pure nature of the generator and its splitability we are not only able to parallelize the random value generation, but also guarantee that it will be deterministic, granted none of the arguments have changed.

Note: Starting with massiv-1.1.0 this function will be deprecated in favor of a more general genSplitArray

Examples
Example4 expressions
import Data.Massiv.Arrayimport System.Random.SplitMix as SplitMixgen = SplitMix.mkSMGen 217randomArray gen SplitMix.splitSMGen SplitMix.nextDouble (ParN 2) (Sz2 2 3) :: Array DL Ix2 DoubleArray DL (ParN 2) (Sz (2 :. 3))  [ [ 0.7383156058619669, 0.39904053166835896, 0.5617584038393628 ]  , [ 0.7218718218678238, 0.7006722805067258, 0.7225894731396042 ]  ]
Example4 expressions
import Data.Massiv.Arrayimport System.Random as Randomgen = Random.mkStdGen 217randomArray gen Random.split Random.random (ParN 2) (Sz2 2 3) :: Array DL Ix2 DoubleArray DL (ParN 2) (Sz (2 :. 3))  [ [ 0.2616843941380331, 0.600959468331641, 0.4382415961606372 ]  , [ 0.27812817813217605, 0.2993277194932741, 0.2774105268603957 ]  ]
valuerandomArrayS
  1. :: (Manifest r e, Index ix)
  2. => g

    Initial random value generator

  3. -> Sz ix

    Resulting size of the array.

  4. -> (g -> (e, g))

    A function that produces a random value and the next generator

  5. -> (g, Array r ix e)
#

Similar to randomArray but performs generation sequentially, which means it doesn't require splitability property. Another consequence is that it returns the new generator together with manifest array of random values.

Examples
Example4 expressions
import Data.Massiv.Arrayimport System.Random.SplitMix as SplitMixgen = SplitMix.mkSMGen 217snd $ randomArrayS gen (Sz2 2 3) SplitMix.nextDouble :: Array P Ix2 DoubleArray P Seq (Sz (2 :. 3))  [ [ 0.8878273949359751, 0.11290807610140963, 0.7383156058619669 ]  , [ 0.39904053166835896, 0.5617584038393628, 0.16248374266020216 ]  ]
Example4 expressions
import Data.Massiv.Arrayimport System.Random.Mersenne.Pure64 as MTgen = MT.pureMT 217snd $ randomArrayS gen (Sz2 2 3) MT.randomDouble :: Array P Ix2 DoubleArray P Seq (Sz (2 :. 3))  [ [ 0.5504018416543631, 0.22504666452851707, 0.4480480867867128 ]  , [ 0.7139711572975297, 0.49401087853770953, 0.9397201599368645 ]  ]
Example4 expressions
import Data.Massiv.Arrayimport System.Random as Systemgen = System.mkStdGen 217snd $ randomArrayS gen (Sz2 2 3) System.random :: Array P Ix2 DoubleArray P Seq (Sz (2 :. 3))  [ [ 0.11217260506402493, 0.8870919238985904, 0.2616843941380331 ]  , [ 0.600959468331641, 0.4382415961606372, 0.8375162573397977 ]  ]
valuerandomArrayWS
  1. :: (Manifest r e, Index ix, MonadUnliftIO m, PrimMonad m)
  2. => WorkerStates g

    Use initWorkerStates to initialize you per thread generators

  3. -> Sz ix

    Resulting size of the array

  4. -> (g -> m e)

    Generate the value using the per thread generator.

  5. -> m (Array r ix e)
#

This is a stateful approach of generating random values. If your generator is pure and splittable, it is better to use randomArray instead, which will give you a pure, deterministic and parallelizable generation of arrays. On the other hand, if your generator is not thread safe, which is most likely the case, instead of using some sort of global mutex, WorkerStates allows you to keep track of individual state per worker (thread), which fits parallelization of random value generation perfectly. All that needs to be done is generators need to be initialized once per worker and then they can be reused as many times as necessary.

Examples

In the example below we take a stateful random number generator from wmc-random, which is not thread safe, and safely parallelize it by giving each thread it's own generator. There is a caveat of course, statistical independence will depend on the entropy in your initial seeds, so do not use the example below verbatim, since initial seeds are sequential numbers.

Example8 expressions
import Data.Massiv.Array as Aimport System.Random.MWC as MWC (initialize)import System.Random.Stateful (uniformRM)import Control.Scheduler (initWorkerStates, getWorkerId):set -XTypeApplicationsgens <- initWorkerStates Par (MWC.initialize . A.toPrimitiveVector . A.singleton @P @Ix1 . fromIntegral . getWorkerId)randomArrayWS gens (Sz2 2 3) (uniformRM (0, 9)) :: IO (Matrix P Double)Array P Par (Sz (2 :. 3))  [ [ 8.999240522095299, 6.832223390653755, 3.065728078741671 ]  , [ 7.242581103346686, 2.4565807301968623, 0.4514262066689775 ]  ]randomArrayWS gens (Sz1 6) (uniformRM (0, 9)) :: IO (Vector P Int)Array P Par (Sz1 6)  [ 8, 8, 7, 1, 1, 2 ]
valuerange :: Index ix => Comp -> ix -> ix -> Array D ix ix
#

Create an array of indices with a range from start to finish (not-including), where indices are incremeted by one.

Examples
Example3 expressions
import Data.Massiv.Arrayrange Seq (Ix1 1) 6Array D Seq (Sz1 5)  [ 1, 2, 3, 4, 5 ]fromIx2 <$> range Seq (-1) (2 :. 2)Array D Seq (Sz (3 :. 3))  [ [ (-1,-1), (-1,0), (-1,1) ]  , [ (0,-1), (0,0), (0,1) ]  , [ (1,-1), (1,0), (1,1) ]  ]
valuerangeSize
  1. :: Index ix
  2. => Comp

    Computation strategy

  3. -> ix

    x - start value

  4. -> Sz ix

    sz - Size of resulting array

  5. -> Array D ix ix
#

Create an array of specified size with indices starting with some index at position 0 and incremented by 1 until the end of the array is reached

valuerangeStep'
  1. :: (HasCallStack, Index ix)
  2. => Comp
  3. -> ix
  4. -> ix
  5. -> ix
  6. -> Array D ix ix
#

Same as rangeStepM, but will throw an error whenever step contains zeros.

Example
Example2 expressions
import Data.Massiv.ArrayrangeStep' Seq (Ix1 1) 2 6Array D Seq (Sz1 3)  [ 1, 3, 5 ]
valuerangeStepM
  1. :: (Index ix, MonadThrow m)
  2. => Comp

    Computation strategy

  3. -> ix

    Start

  4. -> ix

    Step. Negative and positive values are ok, but can't have zeros

  5. -> ix

    End

  6. -> m (Array D ix ix)
#

Same as range, but with a custom step.

Throws Exceptions: IndexZeroException

Examples
Example3 expressions
import Data.Massiv.ArrayrangeStepM Seq (Ix1 1) 2 8Array D Seq (Sz1 4)  [ 1, 3, 5, 7 ]rangeStepM Seq (Ix1 1) 0 8*** Exception: IndexZeroException: 0
valuerangeStepSize
  1. :: Index ix
  2. => Comp

    Computation strategy

  3. -> ix

    x - start value

  4. -> ix

    delta - step value

  5. -> Sz ix

    sz - Size of resulting array

  6. -> Array D ix ix
#

Same as rangeSize, but with ability to specify the step.

valueunfoldlS_ :: Index ix => Sz ix -> (a -> (a, e)) -> a -> Array DL ix e
#

Unfold sequentially from the end. There is no way to save the accumulator after unfolding is done, since resulting array is delayed, but it's possible to use unfoldlPrimM to achieve such effect.

valueunfoldrS_ :: Index ix => Sz ix -> (a -> (e, a)) -> a -> Array DL ix e
#

Right unfold into a delayed load array. For the opposite direction use unfoldlS_.

Examples
Example2 expressions
import Data.Massiv.ArrayunfoldrS_ (Sz1 10) (\xs -> (Prelude.head xs, Prelude.tail xs)) ([10 ..] :: [Int])Array DL Seq (Sz1 10)  [ 10, 11, 12, 13, 14, 15, 16, 17, 18, 19 ]
valueuniformArray
  1. :: (Index ix, RandomGen g, Uniform e)
  2. => g

    Initial random value generator.

  3. -> Comp

    Computation strategy.

  4. -> Sz ix

    Resulting size of the array.

  5. -> Array DL ix e
#

Generate a random array where all elements are sampled from a uniform distribution.

valueempty :: Load r ix e => Array r ix e
#

Create an Array with no elements. By itself it is not particularly useful, but it serves as a nice base for constructing larger arrays.

Examples
Example4 expressions
import Data.Massiv.Array as A:set -XTypeApplicationsxs = empty @DL @Ix1 @Doublesnoc (cons 4 (cons 5 xs)) 22Array DL Seq (Sz1 3)  [ 4.0, 5.0, 22.0 ]
valuesingleton
  1. :: Load r ix e
  2. => e

    The only element

  3. -> Array r ix e
#

Create an Array with a single element.

Examples
Example2 expressions
import Data.Massiv.Array as Asingleton 7 :: Array D Ix4 DoubleArray D Seq (Sz (1 :> 1 :> 1 :. 1))  [ [ [ [ 7.0 ]      ]    ]  ]

Instead of specifying type signature we could use TypeApplications

Example2 expressions
:set -XTypeApplicationssingleton @U @Ix4 @Double 7Array U Seq (Sz (1 :> 1 :> 1 :. 1))  [ [ [ [ 7.0 ]      ]    ]  ]
methodmakeArray :: Comp -> Sz ix -> (ix -> e) -> Array r ix e
#

Construct an Array. Resulting type either has to be unambiguously inferred or restricted manually, like in the example below. Use "Data.Massiv.Array.makeArrayR" if you'd like to specify representation as an argument.

Example2 expressions
import Data.Massiv.ArraymakeArray Seq (Sz (3 :. 4)) (\ (i :. j) -> if i == j then i else 0) :: Array D Ix2 IntArray D Seq (Sz (3 :. 4))  [ [ 0, 0, 0, 0 ]  , [ 0, 1, 0, 0 ]  , [ 0, 0, 2, 0 ]  ]

Instead of restricting the full type manually we can use TypeApplications as convenience:

Example2 expressions
:set -XTypeApplicationsmakeArray @P @_ @Double Seq (Sz2 3 4) $ \(i :. j) -> logBase (fromIntegral i) (fromIntegral j)Array P Seq (Sz (3 :. 4))  [ [ NaN, -0.0, -0.0, -0.0 ]  , [ -Infinity, NaN, Infinity, Infinity ]  , [ -Infinity, 0.0, 1.0, 1.5849625007211563 ]  ]
methodmakeArrayLinear :: Comp -> Sz ix -> (Int -> e) -> Array r ix e
#

Same as makeArray, but produce elements using linear row-major index.

Example2 expressions
import Data.Massiv.ArraymakeArrayLinear Seq (Sz (2 :. 4)) id :: Array D Ix2 IntArray D Seq (Sz (2 :. 4))  [ [ 0, 1, 2, 3 ]  , [ 4, 5, 6, 7 ]  ]
methodreplicate :: Comp -> Sz ix -> e -> Array r ix e
#

Construct an array of the specified size that contains the same element in all of the cells.

Compute

19 declarations
methodsetComp :: Comp -> Array r ix e -> Array r ix e
#

Set computation strategy for this array

Example
Example5 expressions
:set -XTypeApplicationsimport Data.Massiv.Arraya = singleton @DL @Ix1 @Int 0aArray DL Seq (Sz1 1)  [ 0 ]setComp (ParN 6) a -- use 6 capabilitiesArray DL (ParN 6) (Sz1 1)  [ 0 ]
valuecompute :: (Manifest r e, Load r' ix e) => Array r' ix e -> Array r ix e
#

Ensure that Array is computed, i.e. represented with concrete elements in memory, hence is the Mutable type class restriction. Use setComp if you'd like to change computation strategy before calling compute

valuecomputeP :: (Manifest r e, Load r' ix e) => Array r' ix e -> Array r ix e
#

Compute array in parallel using all cores disregarding predefined computation strategy. Computation stategy of the resulting array will match the source, despite that it is diregarded.

valuecomputeIO
  1. :: (Manifest r e, Load r' ix e, MonadIO m)
  2. => Array r' ix e
  3. -> m (Array r ix e)
#

Very similar to compute, but computes an array inside the IO monad. Despite being deterministic and referentially transparent, because this is an IO action it can be very useful for enforcing the order of evaluation. Should be a prefered way of computing an array during benchmarking.

valuecomputeAs
  1. :: (Manifest r e, Load r' ix e)
  2. => r
  3. -> Array r' ix e
  4. -> Array r ix e
#

Just as compute, but let's you supply resulting representation type as an argument.

Examples
Example2 expressions
import Data.Massiv.ArraycomputeAs P $ range Seq (Ix1 0) 10Array P Seq (Sz1 10)  [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
valuecomputeProxy
  1. :: (Manifest r e, Load r' ix e)
  2. => proxy r
  3. -> Array r' ix e
  4. -> Array r ix e
#

Same as compute and computeAs, but let's you supply resulting representation type as a proxy argument.

Examples

Useful only really for cases when representation constructor or TypeApplications extension aren't desireable for some reason:

Example3 expressions
import Data.Proxyimport Data.Massiv.ArraycomputeProxy (Proxy :: Proxy P) $ (^ (2 :: Int)) <$> range Seq (Ix1 0) 10Array P Seq (Sz1 10)  [ 0, 1, 4, 9, 16, 25, 36, 49, 64, 81 ]
valueconvert :: (Manifest r e, Load r' ix e) => Array r' ix e -> Array r ix e
#

O(n) - conversion between array types. A full copy will occur, unless when the source and result arrays are of the same representation, in which case it is an O(1) operation.

Vector

0 declarations

Size

6 declarations
methodsize :: Array r ix e -> Sz ix
#

O(1) - Get the exact size of an immutabe array. Most of the time will produce the size in constant time, except for Data.Massiv.Array.DS representation, which could result in evaluation of the whole stream. See maxLinearSize and slength for more info.

valueelemsCount :: (Index ix, Size r) => Array r ix e -> Int
#

O(1) - Get the number of elements in the array.

Examples
Example2 expressions
import Data.Massiv.ArrayelemsCount $ range Seq (Ix1 10) 155
valueisEmpty :: (Index ix, Size r) => Array r ix e -> Bool
#

O(1) - Check if array has elements.

Examples
Example3 expressions
import Data.Massiv.ArrayisEmpty (singleton 1 :: Array D Ix2 Int)FalseisEmpty (empty :: Array D Ix2 Int)True
valueisNotEmpty :: (Index ix, Size r) => Array r ix e -> Bool
#

O(1) - Check if array has elements.

Examples
Example3 expressions
import Data.Massiv.ArrayisNotEmpty (singleton 1 :: Array D Ix2 Int)TrueisNotEmpty (empty :: Array D Ix2 Int)False
methodisNull :: Array r ix e -> Bool
#

O(1) - Check whether an array is empty or not.

Examples
Example5 expressions
import Data.Massiv.ArrayisNull $ range Seq (Ix2 10 20) (11 :. 21)FalseisNull $ range Seq (Ix2 10 20) (10 :. 21)TrueisNull (empty :: Array D Ix5 Int)TrueisNull $ sfromList []True
valueisNotNull :: Shape r ix => Array r ix e -> Bool
#

O(1) - Check if array has elements.

Examples
Example3 expressions
import Data.Massiv.ArrayisNotNull (singleton 1 :: Array D Ix2 Int)TrueisNotNull (empty :: Array D Ix2 Int)False

Indexing

10 declarations
value(!?) :: (Index ix, Manifest r e, MonadThrow m) => Array r ix e -> ix -> m e
#

O(1) - Infix version of indexM.

Exceptions: IndexOutOfBoundsException

Examples
Example7 expressions
import Data.Massiv.Array as A:set -XTypeApplicationsa <- fromListsM @U @Ix2 @Int Seq [[1,2,3],[4,5,6]]aArray U Seq (Sz (2 :. 3))  [ [ 1, 2, 3 ]  , [ 4, 5, 6 ]  ]a !? 0 :. 23a !? 0 :. 3*** Exception: IndexOutOfBoundsException: (0 :. 3) is not safe for (Sz (2 :. 3))a !? 0 :. 3 :: Maybe IntNothing
value(!) :: (HasCallStack, Manifest r e, Index ix) => Array r ix e -> ix -> e
#

O(1) - Infix version of index'.

Examples
Example4 expressions
import Data.Massiv.Array as Aa = computeAs U $ iterateN (Sz (2 :. 3)) succ (0 :: Int)aArray U Seq (Sz (2 :. 3))  [ [ 1, 2, 3 ]  , [ 4, 5, 6 ]  ]a ! 0 :. 23
value(??)
  1. :: (Index ix, Manifest r e, MonadThrow m)
  2. => m (Array r ix e)
  3. -> ix
  4. -> m e
#

O(1) - Lookup an element in the array, where array itself is wrapped with MonadThrow. This operator is useful when used together with slicing or other functions that can fail.

Exceptions: IndexOutOfBoundsException

Examples
Example7 expressions
import Data.Massiv.Array as A:set -XTypeApplicationsma = fromListsM @U @Ix3 @Int @Maybe Seq [[[1,2,3]],[[4,5,6]]]maJust (Array U Seq (Sz (2 :> 1 :. 3))  [ [ [ 1, 2, 3 ]    ]  , [ [ 4, 5, 6 ]    ]  ])ma ??> 1Just (Array U Seq (Sz (1 :. 3))  [ [ 4, 5, 6 ]  ])ma ??> 1 ?? 0 :. 2Just 6ma ?? 1 :> 0 :. 2Just 6
valueindex :: (Index ix, Manifest r e) => Array r ix e -> ix -> Maybe e
#

O(1) - Lookup an element in the array. Returns Nothing, when index is out of bounds and returns the element at the supplied index otherwise. Use indexM instead, since it is more general and it can just as well be used with Maybe.

valueindex' :: (HasCallStack, Index ix, Manifest r e) => Array r ix e -> ix -> e
#

O(1) - Lookup an element in the array. This is a partial function and it will throw an error when index is out of bounds. It is safer to use indexM instead.

Examples
Example4 expressions
import Data.Massiv.Array:set -XOverloadedListsxs = [0..100] :: Array U Ix1 Intindex' xs 5050
valuedefaultIndex :: (Index ix, Manifest r e) => e -> Array r ix e -> ix -> e
#

O(1) - Lookup an element in the array, while using default element when index is out of bounds.

Examples
Example5 expressions
import Data.Massiv.Array:set -XOverloadedListsxs = [0..100] :: Array P Ix1 IntdefaultIndex 999 xs 100100defaultIndex 999 xs 101999
valueborderIndex
  1. :: (Index ix, Manifest r e)
  2. => Border e
  3. -> Array r ix e
  4. -> ix
  5. -> e
#

O(1) - Lookup an element in the array. Use a border resolution technique when index is out of bounds.

Examples
Example4 expressions
import Data.Massiv.Array as A:set -XOverloadedListsxs = [0..100] :: Array U Ix1 IntborderIndex Wrap xs <$> range Seq 99 104Array D Seq (Sz1 5)  [ 99, 100, 0, 1, 2 ]
valueevaluateM
  1. :: (Index ix, Source r e, MonadThrow m)
  2. => Array r ix e
  3. -> ix
  4. -> m e
#

This is just like indexM function, but it allows getting values from delayed arrays as well as Manifest. As the name suggests, indexing into a delayed array at the same index multiple times will cause evaluation of the value each time and can destroy the performace if used without care.

Examples
Example4 expressions
import Control.Exceptionimport Data.Massiv.ArrayevaluateM (range Seq (Ix2 10 20) (100 :. 210)) 50 :: Either SomeException Ix2Right (60 :. 70)evaluateM (range Seq (Ix2 10 20) (100 :. 210)) 150 :: Either SomeException Ix2Left (IndexOutOfBoundsException: (150 :. 150) is not safe for (Sz (90 :. 190)))
valueevaluate' :: (HasCallStack, Index ix, Source r e) => Array r ix e -> ix -> e
#

Similar to evaluateM, but will throw an error on out of bounds indices.

Examples
Example2 expressions
import Data.Massiv.Arrayevaluate' (range Seq (Ix2 10 20) (100 :. 210)) 5060 :. 70

Mapping

51 declarations
valueforIO_
  1. :: (Load r ix e, MonadUnliftIO m)
  2. => Array r ix e
  3. -> e -> m a
  4. -> m ()
#

Same as mapIO_ but with arguments flipped.

Example

This is the same example as in forM_, with important difference that accumulator ref will be modified concurrently by as many threads as there are capabilities.

Example5 expressions
import Data.Massiv.Arrayimport Data.IORefref <- newIORef 0 :: IO (IORef Int)forIO_ (range Par (Ix1 0) 1000) $ \ i -> atomicModifyIORef' ref (\v -> (v+i, ()))readIORef ref499500
valueforM_
  1. :: (Source r a, Index ix, Monad m)
  2. => Array r ix a
  3. -> a -> m b
  4. -> m ()
#

Just like mapM_, except with flipped arguments.

Examples

Here is a common way of iterating N times using a for loop in an imperative language with mutation being an obvious side effect:

Example5 expressions
import Data.Massiv.Array as Aimport Data.IORefref <- newIORef 0 :: IO (IORef Int)A.forM_ (range Seq (Ix1 0) 1000) $ \ i -> modifyIORef' ref (+i)readIORef ref499500
valueimapM_
  1. :: (Index ix, Source r a, Monad m)
  2. => ix -> a -> m b
  3. -> Array r ix a
  4. -> m ()
#

Map a monadic index aware function over an array sequentially, while discarding the result.

Examples
Example2 expressions
import Data.Massiv.ArrayimapM_ (curry print) $ range Seq (Ix1 10) 15(0,10)(1,11)(2,12)(3,13)(4,14)
valuemapIO
  1. :: (Size r', Load r' ix a, Manifest r b, MonadUnliftIO m)
  2. => a -> m b
  3. -> Array r' ix a
  4. -> m (Array r ix b)
#

Map an IO action over an Array. Underlying computation strategy is respected and will be parallelized when requested. Unfortunately no fusion is possible and new array will be create upon each call.

valuemapIO_
  1. :: (Load r ix e, MonadUnliftIO m)
  2. => e -> m a
  3. -> Array r ix e
  4. -> m ()
#

Similar to mapIO, but ignores the result of mapping action and does not create a resulting array, therefore it is faster. Use this instead of mapIO when result is irrelevant. Most importantly it will follow the iteration logic outlined by the supplied array.

valuemapM_
  1. :: (Source r a, Index ix, Monad m)
  2. => a -> m b
  3. -> Array r ix a
  4. -> m ()
#

Map a monadic function over an array sequentially, while discarding the result.

Examples
Example2 expressions
import Data.Massiv.Array as ArangeStepM Par (Ix1 10) 12 60 >>= A.mapM_ print1022344658
valuezipWith
  1. :: (Index ix, Source r1 e1, Source r2 e2)
  2. => e1 -> e2 -> e
  3. -> Array r1 ix e1
  4. -> Array r2 ix e2
  5. -> Array D ix e
#

Zip two arrays with a function. Resulting array will be an intersection of source arrays in case their dimensions do not match.

Folding

58 declarations

All folding is done in a row-major order.

valuecompareArrays
  1. :: (Index ix, Source r1 e1, Source r2 e2)
  2. => e1 -> e2 -> Ordering
  3. -> Array r1 ix e1
  4. -> Array r2 ix e2
  5. -> Ordering
#

Compute array ordering by applying a comparing function to each element. The exact ordering is unspecified so this is only intended for use in maps and the like where you need an ordering but do not care about which one is used.

valueeqArrays
  1. :: (Index ix, Source r1 e1, Source r2 e2)
  2. => e1 -> e2 -> Bool
  3. -> Array r1 ix e1
  4. -> Array r2 ix e2
  5. -> Bool
#

Compute array equality by applying a comparing function to each element. Empty arrays are always equal, regardless of their size.

valueall :: (Index ix, Source r e) => (e -> Bool) -> Array r ix e -> Bool
#

O(n) - Determines whether all elements of the array satisfy a predicate.

valueelem :: (Eq e, Index ix, Source r e) => e -> Array r ix e -> Bool
#

O(n) - Determines whether an element is present in the array.

valuefoldInnerSlice
  1. :: (Source r e, Index ix, Monoid m)
  2. => Array D (Lower ix) e -> m
  3. -> Array r ix e
  4. -> m
#

Reduce each inner slice into a monoid and mappend results together

Example
Example6 expressions
import Data.Massiv.Array as Aimport Data.Monoid (Product(..))arr = computeAs P $ iterateN (Sz2 2 3) (+1) (10 :: Int)arrArray P Seq (Sz (2 :. 3))  [ [ 11, 12, 13 ]  , [ 14, 15, 16 ]  ]getProduct $ foldInnerSlice (\column -> Product (A.sum column)) arr19575(11 + 14) * (12 + 15) * (13 + 16) :: Int19575
valuefoldOuterSlice
  1. :: (Index ix, Index (Lower ix), Source r e, Monoid m)
  2. => Array r (Lower ix) e -> m
  3. -> Array r ix e
  4. -> m
#

Reduce each outer slice into a monoid and mappend results together

Example
Example6 expressions
import Data.Massiv.Array as Aimport Data.Monoid (Product(..))arr = computeAs P $ iterateN (Sz2 2 3) (+1) (10 :: Int)arrArray P Seq (Sz (2 :. 3))  [ [ 11, 12, 13 ]  , [ 14, 15, 16 ]  ]getProduct $ foldOuterSlice (\row -> Product (A.sum row)) arr1620(11 + 12 + 13) * (14 + 15 + 16) :: Int1620
valuefoldSemi
  1. :: (Index ix, Source r e, Semigroup m)
  2. => (e -> m)

    Convert each element of an array to an appropriate Semigroup.

  3. -> m

    Initial element that must be neutral to the (<>) function.

  4. -> Array r ix e

    Source array

  5. -> m
#

O(n) - Semigroup fold over an array.

valuefoldlWithin
  1. :: (Index (Lower ix), IsIndexDimension ix n, Source r e)
  2. => Dimension n
  3. -> a -> e -> a
  4. -> a
  5. -> Array r ix e
  6. -> Array D (Lower ix) a
#

Left fold along a specified dimension.

Example
Example6 expressions
import Data.Massiv.Array:set -XTypeApplicationsarr = makeArrayLinear @U Seq (Sz (2 :. 5)) idarrArray U Seq (Sz (2 :. 5))  [ [ 0, 1, 2, 3, 4 ]  , [ 5, 6, 7, 8, 9 ]  ]foldlWithin Dim1 (flip (:)) [] arrArray D Seq (Sz1 2)  [ [4,3,2,1,0], [9,8,7,6,5] ]foldlWithin Dim2 (flip (:)) [] arrArray D Seq (Sz1 5)  [ [5,0], [6,1], [7,2], [8,3], [9,4] ]
valueifoldMono
  1. :: (Index ix, Source r e, Monoid m)
  2. => (ix -> e -> m)

    Convert each element of an array to an appropriate Monoid.

  3. -> Array r ix e

    Source array

  4. -> m
#

O(n) - Monoidal fold over an array with an index aware function. Also known as reduce.

valueifoldSemi
  1. :: (Index ix, Source r e, Semigroup m)
  2. => (ix -> e -> m)

    Convert each element of an array to an appropriate Semigroup.

  3. -> m

    Initial element that must be neutral to the (<>) function.

  4. -> Array r ix e

    Source array

  5. -> m
#

O(n) - Semigroup fold over an array with an index aware function.

valueany :: (Index ix, Source r e) => (e -> Bool) -> Array r ix e -> Bool
#

O(n) - Determines whether any element of the array satisfies a predicate.

valuefoldMono
  1. :: (Index ix, Source r e, Monoid m)
  2. => (e -> m)

    Convert each element of an array to an appropriate Monoid.

  3. -> Array r ix e

    Source array

  4. -> m
#

O(n) - This is exactly like foldMap, but for arrays. Fold over an array, while converting each element into a Monoid. Also known as map-reduce. If elements of the array are already a Monoid you can use fold instead.

valuefoldlM_
  1. :: (Index ix, Source r e, Monad m)
  2. => a -> e -> m a
  3. -> a
  4. -> Array r ix e
  5. -> m ()
#

O(n) - Monadic left fold, that discards the result.

valuefoldlP
  1. :: (MonadIO m, Index ix, Source r e)
  2. => (a -> e -> a)

    Folding function g.

  3. -> a

    Accumulator. Will be applied to g multiple times, thus must be neutral.

  4. -> (b -> a -> b)

    Chunk results folding function f.

  5. -> b

    Accumulator for results of chunks folding.

  6. -> Array r ix e
  7. -> m b
#

O(n) - Left fold, computed with respect of array's computation strategy. Because we do potentially split the folding among many threads, we also need a combining function and an accumulator for the results. Depending on the number of threads being used, results can be different, hence is the MonadIO constraint.

Examples
Example5 expressions
import Data.Massiv.ArrayfoldlP (flip (:)) [] (flip (:)) [] $ makeArrayR D Seq (Sz1 6) id[[5,4,3,2,1,0]]foldlP (flip (:)) [] (++) [] $ makeArrayR D Seq (Sz1 6) id[5,4,3,2,1,0]foldlP (flip (:)) [] (flip (:)) [] $ makeArrayR D (ParN 3) (Sz1 6) id[[5,4],[3,2],[1,0]]foldlP (flip (:)) [] (++) [] $ makeArrayR D (ParN 3) (Sz1 6) id[1,0,3,2,5,4]
valuefoldlS :: (Index ix, Source r e) => (a -> e -> a) -> a -> Array r ix e -> a
#

O(n) - Left fold, computed sequentially.

valuefoldrFB :: (Index ix, Source r e) => (e -> b -> b) -> b -> Array r ix e -> b
#

Version of foldr that supports foldr/build list fusion implemented by GHC.

valuefoldrM_
  1. :: (Index ix, Source r e, Monad m)
  2. => e -> a -> m a
  3. -> a
  4. -> Array r ix e
  5. -> m ()
#

O(n) - Monadic right fold, that discards the result.

valuefoldrP
  1. :: (MonadIO m, Index ix, Source r e)
  2. => e -> a -> a
  3. -> a
  4. -> a -> b -> b
  5. -> b
  6. -> Array r ix e
  7. -> m b
#

O(n) - Right fold, computed with respect to computation strategy. Same as foldlP, except directed from the last element in the array towards beginning.

Examples
Example4 expressions
import Data.Massiv.ArrayfoldrP (:) [] (++) [] $ makeArrayR D (ParN 2) (Sz2 2 3) fromIx2[(0,0),(0,1),(0,2),(1,0),(1,1),(1,2)]foldrP (:) [] (:) [] $ makeArrayR D Seq (Sz1 6) id[[0,1,2,3,4,5]]foldrP (:) [] (:) [] $ makeArrayR D (ParN 3) (Sz1 6) id[[0,1],[2,3],[4,5]]
valuefoldrS :: (Index ix, Source r e) => (e -> a -> a) -> a -> Array r ix e -> a
#

O(n) - Right fold, computed sequentially.

valueifoldlIO
  1. :: (MonadUnliftIO m, Index ix, Source r e)
  2. => (a -> ix -> e -> m a)

    Index aware folding IO action

  3. -> a

    Accumulator

  4. -> (b -> a -> m b)

    Folding action that is applied to the results of a parallel fold

  5. -> b

    Accumulator for chunks folding

  6. -> Array r ix e
  7. -> m b
#

Similar to ifoldlP, except that folding functions themselves do live in IO

valueifoldlM
  1. :: (Index ix, Source r e, Monad m)
  2. => a -> ix -> e -> m a
  3. -> a
  4. -> Array r ix e
  5. -> m a
#

O(n) - Monadic left fold with an index aware function.

valueifoldlM_
  1. :: (Index ix, Source r e, Monad m)
  2. => a -> ix -> e -> m a
  3. -> a
  4. -> Array r ix e
  5. -> m ()
#

O(n) - Monadic left fold with an index aware function, that discards the result.

valueifoldlP
  1. :: (MonadIO m, Index ix, Source r e)
  2. => a -> ix -> e -> a
  3. -> a
  4. -> b -> a -> b
  5. -> b
  6. -> Array r ix e
  7. -> m b
#

O(n) - Left fold with an index aware function, computed in parallel. Just like foldlP, except that folding function will receive an index of an element it is being applied to.

valueifoldlS
  1. :: (Index ix, Source r e)
  2. => a -> ix -> e -> a
  3. -> a
  4. -> Array r ix e
  5. -> a
#

O(n) - Left fold with an index aware function, computed sequentially.

valueifoldrM
  1. :: (Index ix, Source r e, Monad m)
  2. => ix -> e -> a -> m a
  3. -> a
  4. -> Array r ix e
  5. -> m a
#

O(n) - Monadic right fold with an index aware function.

valueifoldrM_
  1. :: (Index ix, Source r e, Monad m)
  2. => ix -> e -> a -> m a
  3. -> a
  4. -> Array r ix e
  5. -> m ()
#

O(n) - Monadic right fold with an index aware function, that discards the result.

valueifoldrP
  1. :: (MonadIO m, Index ix, Source r e)
  2. => ix -> e -> a -> a
  3. -> a
  4. -> a -> b -> b
  5. -> b
  6. -> Array r ix e
  7. -> m b
#

O(n) - Right fold with an index aware function, while respecting the computation strategy. Same as ifoldlP, except directed from the last element in the array towards beginning, but also row-major.

valueifoldrS
  1. :: (Index ix, Source r e)
  2. => ix -> e -> a -> a
  3. -> a
  4. -> Array r ix e
  5. -> a
#

O(n) - Right fold with an index aware function, computed sequentially.

valuelazyFoldlS
  1. :: (Index ix, Source r e)
  2. => a -> e -> a
  3. -> a
  4. -> Array r ix e
  5. -> a
#

O(n) - Left fold, computed sequentially with lazy accumulator.

valuelazyFoldrS
  1. :: (Index ix, Source r e)
  2. => e -> a -> a
  3. -> a
  4. -> Array r ix e
  5. -> a
#

O(n) - Right fold, computed sequentially with lazy accumulator.

Transforming

40 declarations
valueappendM
  1. :: (MonadThrow m, Index ix, Source r1 e, Source r2 e)
  2. => Dim
  3. -> Array r1 ix e
  4. -> Array r2 ix e
  5. -> m (Array DL ix e)
#

Append two arrays together along a particular dimension. Sizes of both arrays must match, with an allowed exception of the dimension they are being appended along, otherwise Nothing is returned.

Examples

Append two 2D arrays along both dimensions. Note that they do agree on inner dimensions.

Example5 expressions
import Data.Massiv.ArrayarrA = makeArrayR U Seq (Sz2 2 3) (\(i :. j) -> ('A', i, j))arrB = makeArrayR U Seq (Sz2 2 3) (\(i :. j) -> ('B', i, j))appendM 1 arrA arrBArray DL Seq (Sz (2 :. 6))  [ [ ('A',0,0), ('A',0,1), ('A',0,2), ('B',0,0), ('B',0,1), ('B',0,2) ]  , [ ('A',1,0), ('A',1,1), ('A',1,2), ('B',1,0), ('B',1,1), ('B',1,2) ]  ]appendM 2 arrA arrBArray DL Seq (Sz (4 :. 3))  [ [ ('A',0,0), ('A',0,1), ('A',0,2) ]  , [ ('A',1,0), ('A',1,1), ('A',1,2) ]  , [ ('B',0,0), ('B',0,1), ('B',0,2) ]  , [ ('B',1,0), ('B',1,1), ('B',1,2) ]  ]

Now appending arrays with different sizes:

Example3 expressions
arrC = makeArrayR U Seq (Sz (2 :. 4)) (\(i :. j) -> ('C', i, j))appendM 1 arrA arrCArray DL Seq (Sz (2 :. 7))  [ [ ('A',0,0), ('A',0,1), ('A',0,2), ('C',0,0), ('C',0,1), ('C',0,2), ('C',0,3) ]  , [ ('A',1,0), ('A',1,1), ('A',1,2), ('C',1,0), ('C',1,1), ('C',1,2), ('C',1,3) ]  ]appendM 2 arrA arrC*** Exception: SizeMismatchException: (Sz (2 :. 3)) vs (Sz (2 :. 4))
valuebackpermute'
  1. :: (HasCallStack, Source r e, Index ix, Index ix')
  2. => Sz ix'

    Size of the result array

  3. -> (ix' -> ix)

    A function that maps indices of the new array into the source one.

  4. -> Array r ix e

    Source array.

  5. -> Array D ix' e
#

Similar to backpermuteM, with a few notable differences:

  • Creates a delayed array, instead of manifest, therefore it can be fused

  • Respects computation strategy, so it can be parallelized

  • Throws a runtime IndexOutOfBoundsException from pure code.

valuebackpermuteM
  1. :: (Manifest r e, Index ix, Source r' e, Index ix', MonadUnliftIO m, PrimMonad m, MonadThrow m)
  2. => Sz ix

    Size of the result array

  3. -> (ix -> ix')

    A function that maps indices of the new array into the source one.

  4. -> Array r' ix' e

    Source array.

  5. -> m (Array r ix e)
#

Rearrange elements of an array into a new one by using a function that maps indices of the newly created one into the old one. This function can throw IndexOutOfBoundsException.

Examples
Example5 expressions
import Data.Massiv.Array:set -XTypeApplicationsarr = makeArrayLinear @D Seq (Sz (2 :> 3 :. 4)) idarrArray D Seq (Sz (2 :> 3 :. 4))  [ [ [ 0, 1, 2, 3 ]    , [ 4, 5, 6, 7 ]    , [ 8, 9, 10, 11 ]    ]  , [ [ 12, 13, 14, 15 ]    , [ 16, 17, 18, 19 ]    , [ 20, 21, 22, 23 ]    ]  ]backpermuteM @U (Sz (4 :. 2)) (\(i :. j) -> j :> j :. i) arrArray U Seq (Sz (4 :. 2))  [ [ 0, 16 ]  , [ 1, 17 ]  , [ 2, 18 ]  , [ 3, 19 ]  ]
valuedeleteColumnsM
  1. :: (MonadThrow m, Index ix, Source r e)
  2. => Ix1
  3. -> Sz Ix1
  4. -> Array r ix e
  5. -> m (Array DL ix e)
#

Similar to deleteRegionM, but drop a specified number of columns an array.

Example
Example4 expressions
import Data.Massiv.Arrayarr = fromIx2 <$> (0 :. 0 ..: 3 :. 6)arrArray D Seq (Sz (3 :. 6))  [ [ (0,0), (0,1), (0,2), (0,3), (0,4), (0,5) ]  , [ (1,0), (1,1), (1,2), (1,3), (1,4), (1,5) ]  , [ (2,0), (2,1), (2,2), (2,3), (2,4), (2,5) ]  ]deleteColumnsM 2 3 arrArray DL Seq (Sz (3 :. 3))  [ [ (0,0), (0,1), (0,5) ]  , [ (1,0), (1,1), (1,5) ]  , [ (2,0), (2,1), (2,5) ]  ]
valuedeleteRegionM
  1. :: (MonadThrow m, Index ix, Source r e)
  2. => Dim

    Along which axis should the removal happen

  3. -> Ix1

    At which index to start dropping slices

  4. -> Sz Ix1

    Number of slices to drop

  5. -> Array r ix e

    Array that will have it's subarray removed

  6. -> m (Array DL ix e)
#

Delete a region from an array along the specified dimension.

Examples
Example5 expressions
import Data.Massiv.Arrayarr = fromIx3 <$> (0 :> 0 :. 0 ..: 3 :> 2 :. 6)deleteRegionM 1 2 3 arrArray DL Seq (Sz (3 :> 2 :. 3))  [ [ [ (0,0,0), (0,0,1), (0,0,5) ]    , [ (0,1,0), (0,1,1), (0,1,5) ]    ]  , [ [ (1,0,0), (1,0,1), (1,0,5) ]    , [ (1,1,0), (1,1,1), (1,1,5) ]    ]  , [ [ (2,0,0), (2,0,1), (2,0,5) ]    , [ (2,1,0), (2,1,1), (2,1,5) ]    ]  ]v = Ix1 0 ... 10deleteRegionM 1 3 5 vArray DL Seq (Sz1 6)  [ 0, 1, 2, 8, 9, 10 ]
valuedeleteRowsM
  1. :: (MonadThrow m, Index ix, Index (Lower ix), Source r e)
  2. => Ix1
  3. -> Sz Ix1
  4. -> Array r ix e
  5. -> m (Array DL ix e)
#

Similar to deleteRegionM, but drop a specified number of rows from an array that has at least 2 dimensions.

Example
Example4 expressions
import Data.Massiv.Arrayarr = fromIx2 <$> (0 :. 0 ..: 3 :. 6)arrArray D Seq (Sz (3 :. 6))  [ [ (0,0), (0,1), (0,2), (0,3), (0,4), (0,5) ]  , [ (1,0), (1,1), (1,2), (1,3), (1,4), (1,5) ]  , [ (2,0), (2,1), (2,2), (2,3), (2,4), (2,5) ]  ]deleteRowsM 1 1 arrArray DL Seq (Sz (2 :. 6))  [ [ (0,0), (0,1), (0,2), (0,3), (0,4), (0,5) ]  , [ (2,0), (2,1), (2,2), (2,3), (2,4), (2,5) ]  ]
valueextractFromToM
  1. :: (MonadThrow m, Index ix, Source r e)
  2. => ix

    Starting index

  3. -> ix

    Index up to which elements should be extracted.

  4. -> Array r ix e

    Source array.

  5. -> m (Array D ix e)
#

Similar to extractM, except it takes starting and ending index. Result array will not include the ending index.

Examples
Example3 expressions
a <- resizeM (Sz (3 :> 2 :. 4)) $ Ix1 11 ... 34aArray D Seq (Sz (3 :> 2 :. 4))  [ [ [ 11, 12, 13, 14 ]    , [ 15, 16, 17, 18 ]    ]  , [ [ 19, 20, 21, 22 ]    , [ 23, 24, 25, 26 ]    ]  , [ [ 27, 28, 29, 30 ]    , [ 31, 32, 33, 34 ]    ]  ]extractFromToM (1 :> 0 :. 1) (3 :> 2 :. 4) aArray D Seq (Sz (2 :> 2 :. 3))  [ [ [ 20, 21, 22 ]    , [ 24, 25, 26 ]    ]  , [ [ 28, 29, 30 ]    , [ 32, 33, 34 ]    ]  ]
valueextractM
  1. :: (MonadThrow m, Index ix, Source r e)
  2. => ix

    Starting index

  3. -> Sz ix

    Size of the resulting array

  4. -> Array r ix e

    Source array

  5. -> m (Array D ix e)
#

Extract a sub-array from within a larger source array. Array that is being extracted must be fully encapsulated in a source array, otherwise SizeSubregionException will be thrown.

Examples
Example7 expressions
import Data.Massiv.Array as Am <- resizeM (Sz (3 :. 3)) $ Ix1 1 ... 9mArray D Seq (Sz (3 :. 3))  [ [ 1, 2, 3 ]  , [ 4, 5, 6 ]  , [ 7, 8, 9 ]  ]extractM (0 :. 1) (Sz (2 :. 2)) mArray D Seq (Sz (2 :. 2))  [ [ 2, 3 ]  , [ 5, 6 ]  ]a <- resizeM (Sz (3 :> 2 :. 4)) $ Ix1 11 ... 34aArray D Seq (Sz (3 :> 2 :. 4))  [ [ [ 11, 12, 13, 14 ]    , [ 15, 16, 17, 18 ]    ]  , [ [ 19, 20, 21, 22 ]    , [ 23, 24, 25, 26 ]    ]  , [ [ 27, 28, 29, 30 ]    , [ 31, 32, 33, 34 ]    ]  ]extractM (0 :> 1 :. 1) (Sz (3 :> 1 :. 2)) aArray D Seq (Sz (3 :> 1 :. 2))  [ [ [ 16, 17 ]    ]  , [ [ 24, 25 ]    ]  , [ [ 32, 33 ]    ]  ]
valuereplaceOuterSlice
  1. :: (MonadThrow m, Index ix, Source r e, Load r (Lower ix) e)
  2. => Ix1
  3. -> Array r (Lower ix) e
  4. -> Array r ix e
  5. -> m (Array DL ix e)
#

Replace an outer slice of an array with another one

Example
Example4 expressions
import Data.Massiv.Arrayarr = makeArrayR U Seq (Sz3 3 4 5) fromIx3arr' = makeArrayR U Seq (Sz3 3 4 5) (fromIx3 . liftIndex (* 100))replaceOuterSlice 1 (arr' !> 2) arrArray DL Seq (Sz (3 :> 4 :. 5))  [ [ [ (0,0,0), (0,0,1), (0,0,2), (0,0,3), (0,0,4) ]    , [ (0,1,0), (0,1,1), (0,1,2), (0,1,3), (0,1,4) ]    , [ (0,2,0), (0,2,1), (0,2,2), (0,2,3), (0,2,4) ]    , [ (0,3,0), (0,3,1), (0,3,2), (0,3,3), (0,3,4) ]    ]  , [ [ (200,0,0), (200,0,100), (200,0,200), (200,0,300), (200,0,400) ]    , [ (200,100,0), (200,100,100), (200,100,200), (200,100,300), (200,100,400) ]    , [ (200,200,0), (200,200,100), (200,200,200), (200,200,300), (200,200,400) ]    , [ (200,300,0), (200,300,100), (200,300,200), (200,300,300), (200,300,400) ]    ]  , [ [ (2,0,0), (2,0,1), (2,0,2), (2,0,3), (2,0,4) ]    , [ (2,1,0), (2,1,1), (2,1,2), (2,1,3), (2,1,4) ]    , [ (2,2,0), (2,2,1), (2,2,2), (2,2,3), (2,2,4) ]    , [ (2,3,0), (2,3,1), (2,3,2), (2,3,3), (2,3,4) ]    ]  ]
valuereplaceSlice
  1. :: (MonadThrow m, Source r e, Source r' e, Index ix, Index (Lower ix))
  2. => Dim
  3. -> Ix1
  4. -> Array r' (Lower ix) e
  5. -> Array r ix e
  6. -> m (Array DL ix e)
#

Replace a slice of an array with another one

Example
Example4 expressions
import Data.Massiv.Arrayarr = makeArrayR U Seq (Sz3 3 4 5) fromIx3arr' = makeArrayR U Seq (Sz3 3 4 5) (fromIx3 . liftIndex (* 100))replaceSlice 2 1 (arr' <!> (2, 3)) arrArray DL Seq (Sz (3 :> 4 :. 5))  [ [ [ (0,0,0), (0,0,1), (0,0,2), (0,0,3), (0,0,4) ]    , [ (0,300,0), (0,300,100), (0,300,200), (0,300,300), (0,300,400) ]    , [ (0,2,0), (0,2,1), (0,2,2), (0,2,3), (0,2,4) ]    , [ (0,3,0), (0,3,1), (0,3,2), (0,3,3), (0,3,4) ]    ]  , [ [ (1,0,0), (1,0,1), (1,0,2), (1,0,3), (1,0,4) ]    , [ (100,300,0), (100,300,100), (100,300,200), (100,300,300), (100,300,400) ]    , [ (1,2,0), (1,2,1), (1,2,2), (1,2,3), (1,2,4) ]    , [ (1,3,0), (1,3,1), (1,3,2), (1,3,3), (1,3,4) ]    ]  , [ [ (2,0,0), (2,0,1), (2,0,2), (2,0,3), (2,0,4) ]    , [ (200,300,0), (200,300,100), (200,300,200), (200,300,300), (200,300,400) ]    , [ (2,2,0), (2,2,1), (2,2,2), (2,2,3), (2,2,4) ]    , [ (2,3,0), (2,3,1), (2,3,2), (2,3,3), (2,3,4) ]    ]  ]
valuereverse
  1. :: (IsIndexDimension ix n, Index ix, Source r e)
  2. => Dimension n
  3. -> Array r ix e
  4. -> Array D ix e
#

Reverse an array along some dimension. Dimension supplied is checked at compile time.

Example
Example5 expressions
import Data.Massiv.Array as Aarr = makeArrayLinear Seq (Sz2 4 5) (+10) :: Array D Ix2 IntarrArray D Seq (Sz (4 :. 5))  [ [ 10, 11, 12, 13, 14 ]  , [ 15, 16, 17, 18, 19 ]  , [ 20, 21, 22, 23, 24 ]  , [ 25, 26, 27, 28, 29 ]  ]A.reverse Dim1 arrArray D Seq (Sz (4 :. 5))  [ [ 14, 13, 12, 11, 10 ]  , [ 19, 18, 17, 16, 15 ]  , [ 24, 23, 22, 21, 20 ]  , [ 29, 28, 27, 26, 25 ]  ]A.reverse Dim2 arrArray D Seq (Sz (4 :. 5))  [ [ 25, 26, 27, 28, 29 ]  , [ 20, 21, 22, 23, 24 ]  , [ 15, 16, 17, 18, 19 ]  , [ 10, 11, 12, 13, 14 ]  ]
valuesplitExtractM
  1. :: (MonadThrow m, Index ix, Source r e)
  2. => Dim

    Dimension along which to do the extraction

  3. -> Ix1

    Start index along the dimension that needs to be extracted

  4. -> Sz Ix1

    Size of the extracted array along the dimension that it will be extracted

  5. -> Array r ix e
  6. -> m (Array D ix e, Array D ix e, Array D ix e)
#

Split an array in three parts across some dimension

valuestackInnerSlicesM
  1. :: (Foldable f, MonadThrow m, Index (Lower ix), Source r e, Index ix)
  2. => f (Array r (Lower ix) e)
  3. -> m (Array DL ix e)
#

Specialized stackSlicesM to handling stacking from the inside. It is the inverse of innerSlices.

Exceptions: SizeMismatchException

Examples

In this example we stack vectors as columns of a matrix from left to right:

Example6 expressions
import Data.Massiv.Array as Ax = compute (iterateN 3 succ 0) :: Matrix P IntxArray P Seq (Sz (3 :. 3))  [ [ 1, 2, 3 ]  , [ 4, 5, 6 ]  , [ 7, 8, 9 ]  ]columns = innerSlices xA.mapM_ print columnsArray D Seq (Sz1 3)  [ 1, 4, 7 ]Array D Seq (Sz1 3)  [ 2, 5, 8 ]Array D Seq (Sz1 3)  [ 3, 6, 9 ]stackInnerSlicesM columns :: IO (Matrix DL Int)Array DL Seq (Sz (3 :. 3))  [ [ 1, 2, 3 ]  , [ 4, 5, 6 ]  , [ 7, 8, 9 ]  ]
valuestackOuterSlicesM
  1. :: (Foldable f, MonadThrow m, Index (Lower ix), Source r e, Index ix)
  2. => f (Array r (Lower ix) e)
  3. -> m (Array DL ix e)
#

Specialized stackSlicesM to handling stacking from the outside. It is the inverse of outerSlices.

Exceptions: SizeMismatchException

Examples

In this example we stack vectors as row of a matrix from top to bottom:

Example6 expressions
import Data.Massiv.Array as Ax = compute (iterateN 3 succ 0) :: Matrix P IntxArray P Seq (Sz (3 :. 3))  [ [ 1, 2, 3 ]  , [ 4, 5, 6 ]  , [ 7, 8, 9 ]  ]rows = outerSlices xA.mapM_ print rowsArray P Seq (Sz1 3)  [ 1, 2, 3 ]Array P Seq (Sz1 3)  [ 4, 5, 6 ]Array P Seq (Sz1 3)  [ 7, 8, 9 ]stackOuterSlicesM rows :: IO (Matrix DL Int)Array DL Seq (Sz (3 :. 3))  [ [ 1, 2, 3 ]  , [ 4, 5, 6 ]  , [ 7, 8, 9 ]  ]
valuestackSlicesM
  1. :: (Foldable f, MonadThrow m, Index (Lower ix), Source r e, Index ix)
  2. => Dim
  3. -> f (Array r (Lower ix) e)
  4. -> m (Array DL ix e)
#

Stack slices on top of each other along the specified dimension.

Exceptions: IndexDimensionException, SizeMismatchException

Examples

Here are the three different ways to stack up two 2D Matrix pages into a 3D array.

Example8 expressions
import Data.Massiv.Array as Ax = compute (iterateN 3 succ 0) :: Matrix P Inty = compute (iterateN 3 succ 9) :: Matrix P IntxArray P Seq (Sz (3 :. 3))  [ [ 1, 2, 3 ]  , [ 4, 5, 6 ]  , [ 7, 8, 9 ]  ]yArray P Seq (Sz (3 :. 3))  [ [ 10, 11, 12 ]  , [ 13, 14, 15 ]  , [ 16, 17, 18 ]  ]stackSlicesM 1 [x, y] :: IO (Array DL Ix3 Int)Array DL Seq (Sz (3 :> 3 :. 2))  [ [ [ 1, 10 ]    , [ 2, 11 ]    , [ 3, 12 ]    ]  , [ [ 4, 13 ]    , [ 5, 14 ]    , [ 6, 15 ]    ]  , [ [ 7, 16 ]    , [ 8, 17 ]    , [ 9, 18 ]    ]  ]stackSlicesM 2 [x, y] :: IO (Array DL Ix3 Int)Array DL Seq (Sz (3 :> 2 :. 3))  [ [ [ 1, 2, 3 ]    , [ 10, 11, 12 ]    ]  , [ [ 4, 5, 6 ]    , [ 13, 14, 15 ]    ]  , [ [ 7, 8, 9 ]    , [ 16, 17, 18 ]    ]  ]stackSlicesM 3 [x, y] :: IO (Array DL Ix3 Int)Array DL Seq (Sz (2 :> 3 :. 3))  [ [ [ 1, 2, 3 ]    , [ 4, 5, 6 ]    , [ 7, 8, 9 ]    ]  , [ [ 10, 11, 12 ]    , [ 13, 14, 15 ]    , [ 16, 17, 18 ]    ]  ]
valuetranspose :: Source r e => Matrix r e -> Matrix D e
#

Transpose a 2-dimensional array

Examples
Example4 expressions
import Data.Massiv.Arrayarr = makeArrayLinearR D Seq (Sz (2 :. 3)) idarrArray D Seq (Sz (2 :. 3))  [ [ 0, 1, 2 ]  , [ 3, 4, 5 ]  ]transpose arrArray D Seq (Sz (3 :. 2))  [ [ 0, 3 ]  , [ 1, 4 ]  , [ 2, 5 ]  ]
valuetransposeInner
  1. :: (Index (Lower ix), Index ix, Source r e)
  2. => Array r ix e
  3. -> Array D ix e
#

Transpose inner two dimensions of at least rank-2 array.

Examples
Example4 expressions
import Data.Massiv.Arrayarr = makeArrayLinearR U Seq (Sz (2 :> 3 :. 4)) idarrArray U Seq (Sz (2 :> 3 :. 4))  [ [ [ 0, 1, 2, 3 ]    , [ 4, 5, 6, 7 ]    , [ 8, 9, 10, 11 ]    ]  , [ [ 12, 13, 14, 15 ]    , [ 16, 17, 18, 19 ]    , [ 20, 21, 22, 23 ]    ]  ]transposeInner arrArray D Seq (Sz (3 :> 2 :. 4))  [ [ [ 0, 1, 2, 3 ]    , [ 12, 13, 14, 15 ]    ]  , [ [ 4, 5, 6, 7 ]    , [ 16, 17, 18, 19 ]    ]  , [ [ 8, 9, 10, 11 ]    , [ 20, 21, 22, 23 ]    ]  ]
valuetransposeOuter
  1. :: (Index (Lower ix), Index ix, Source r e)
  2. => Array r ix e
  3. -> Array D ix e
#

Transpose outer two dimensions of at least rank-2 array.

Examples
Example5 expressions
import Data.Massiv.Array:set -XTypeApplicationsarr = makeArrayLinear @U Seq (Sz (2 :> 3 :. 4)) idarrArray U Seq (Sz (2 :> 3 :. 4))  [ [ [ 0, 1, 2, 3 ]    , [ 4, 5, 6, 7 ]    , [ 8, 9, 10, 11 ]    ]  , [ [ 12, 13, 14, 15 ]    , [ 16, 17, 18, 19 ]    , [ 20, 21, 22, 23 ]    ]  ]transposeOuter arrArray D Seq (Sz (2 :> 4 :. 3))  [ [ [ 0, 4, 8 ]    , [ 1, 5, 9 ]    , [ 2, 6, 10 ]    , [ 3, 7, 11 ]    ]  , [ [ 12, 16, 20 ]    , [ 13, 17, 21 ]    , [ 14, 18, 22 ]    , [ 15, 19, 23 ]    ]  ]
valueupsample
  1. :: Load r ix e
  2. => e

    Element to use for filling the newly added cells

  3. -> Stride ix

    Fill cells according to this stride

  4. -> Array r ix e

    Array that will have cells added to

  5. -> Array DL ix e
#

Insert the same element into a Loadable array according to the supplied stride.

Examples
Example5 expressions
import Data.Massiv.Array as Aarr = iterateN (Sz2 3 2) succ (0 :: Int)arrArray DL Seq (Sz (3 :. 2))  [ [ 1, 2 ]  , [ 3, 4 ]  , [ 5, 6 ]  ]upsample 0 (Stride (2 :. 3)) arrArray DL Seq (Sz (6 :. 6))  [ [ 1, 0, 0, 2, 0, 0 ]  , [ 0, 0, 0, 0, 0, 0 ]  , [ 3, 0, 0, 4, 0, 0 ]  , [ 0, 0, 0, 0, 0, 0 ]  , [ 5, 0, 0, 6, 0, 0 ]  , [ 0, 0, 0, 0, 0, 0 ]  ]upsample 9 (Stride (1 :. 2)) arrArray DL Seq (Sz (3 :. 4))  [ [ 1, 9, 2, 9 ]  , [ 3, 9, 4, 9 ]  , [ 5, 9, 6, 9 ]  ]
valuezoom
  1. :: (Index ix, Source r e)
  2. => Stride ix

    Scaling factor

  3. -> Array r ix e

    Source array

  4. -> Array DL ix e
#

Increaze the size of the array accoridng to the stride multiplier while replicating the same element to fill the neighbors. It is exactly the same as zoomWithGrid, but without the grid.

Example
Example4 expressions
import Data.Massiv.Array as Aarr = resize' (Sz3 1 3 2) (Ix1 1 ... 6)arrArray D Seq (Sz (1 :> 3 :. 2))  [ [ [ 1, 2 ]    , [ 3, 4 ]    , [ 5, 6 ]    ]  ]zoom (Stride (2 :> 2 :. 3)) arrArray DL Seq (Sz (2 :> 6 :. 6))  [ [ [ 1, 1, 1, 2, 2, 2 ]    , [ 1, 1, 1, 2, 2, 2 ]    , [ 3, 3, 3, 4, 4, 4 ]    , [ 3, 3, 3, 4, 4, 4 ]    , [ 5, 5, 5, 6, 6, 6 ]    , [ 5, 5, 5, 6, 6, 6 ]    ]  , [ [ 1, 1, 1, 2, 2, 2 ]    , [ 1, 1, 1, 2, 2, 2 ]    , [ 3, 3, 3, 4, 4, 4 ]    , [ 3, 3, 3, 4, 4, 4 ]    , [ 5, 5, 5, 6, 6, 6 ]    , [ 5, 5, 5, 6, 6, 6 ]    ]  ]
valuezoomWithGrid
  1. :: (Index ix, Source r e)
  2. => e

    Value to use for the grid

  3. -> Stride ix

    Scaling factor

  4. -> Array r ix e

    Source array

  5. -> Array DL ix e
#

Replicate each element of the array by a factor in stride along each dimension and surround each such group with a box of supplied grid value. It will essentially zoom up an array and create a grid around each element from the original array. Very useful for zooming up images to inspect individual pixels.

Example
Example4 expressions
import Data.Massiv.Array as Aarr = resize' (Sz2 3 2) (Ix1 1 ... 6)arrArray D Seq (Sz (3 :. 2))  [ [ 1, 2 ]  , [ 3, 4 ]  , [ 5, 6 ]  ]zoomWithGrid 0 (Stride (2 :. 3)) arrArray DL Seq (Sz (10 :. 9))  [ [ 0, 0, 0, 0, 0, 0, 0, 0, 0 ]  , [ 0, 1, 1, 1, 0, 2, 2, 2, 0 ]  , [ 0, 1, 1, 1, 0, 2, 2, 2, 0 ]  , [ 0, 0, 0, 0, 0, 0, 0, 0, 0 ]  , [ 0, 3, 3, 3, 0, 4, 4, 4, 0 ]  , [ 0, 3, 3, 3, 0, 4, 4, 4, 0 ]  , [ 0, 0, 0, 0, 0, 0, 0, 0, 0 ]  , [ 0, 5, 5, 5, 0, 6, 6, 6, 0 ]  , [ 0, 5, 5, 5, 0, 6, 6, 6, 0 ]  , [ 0, 0, 0, 0, 0, 0, 0, 0, 0 ]  ]

Slicing

13 declarations
value(!>)
  1. :: (HasCallStack, Index ix, Index (Lower ix), Source r e)
  2. => Array r ix e
  3. -> Int
  4. -> Array r (Lower ix) e
#

O(1) - Slices the array from the outside. For 2-dimensional array this will be equivalent of taking a row. Throws an error when index is out of bounds.

Examples

You could say that slicing from outside is synonymous to slicing from the end or slicing at the highermost dimension. For example with rank-3 arrays outer slice would be equivalent to getting a page:

Example4 expressions
import Data.Massiv.Arrayarr = makeArrayR U Seq (Sz (3 :> 2 :. 4)) fromIx3arrArray U Seq (Sz (3 :> 2 :. 4))  [ [ [ (0,0,0), (0,0,1), (0,0,2), (0,0,3) ]    , [ (0,1,0), (0,1,1), (0,1,2), (0,1,3) ]    ]  , [ [ (1,0,0), (1,0,1), (1,0,2), (1,0,3) ]    , [ (1,1,0), (1,1,1), (1,1,2), (1,1,3) ]    ]  , [ [ (2,0,0), (2,0,1), (2,0,2), (2,0,3) ]    , [ (2,1,0), (2,1,1), (2,1,2), (2,1,3) ]    ]  ]arr !> 2Array U Seq (Sz (2 :. 4))  [ [ (2,0,0), (2,0,1), (2,0,2), (2,0,3) ]  , [ (2,1,0), (2,1,1), (2,1,2), (2,1,3) ]  ]

There is nothing wrong with chaining, mixing and matching slicing operators:

Example3 expressions
arr !> 2 !> 0 ! 3(2,0,3)evaluateM (arr !> 2 <! 3) 0(2,0,3)(arr !> 2 !> 0 ! 3) == (arr ! 2 :> 0 :. 3)True
value(??>)
  1. :: (MonadThrow m, Index ix, Index (Lower ix), Source r e)
  2. => m (Array r ix e)
  3. -> Int
  4. -> m (Array r (Lower ix) e)
#

O(1) - Safe slicing continuation from the outside. Similarly to (!>) slices the array from the outside, but takes Maybe array as input and returns Nothing when index is out of bounds.

Examples
Example5 expressions
import Data.Massiv.Arrayarr = makeArrayR U Seq (Sz (3 :> 2 :. 4)) fromIx3arr !?> 2 ??> 0 ?? 3 :: Maybe Ix3TJust (2,0,3)arr !?> 2 ??> 0 ?? -1 :: Maybe Ix3TNothingarr !?> 2 ??> -10 ?? 1*** Exception: IndexOutOfBoundsException: -10 is not safe for (Sz1 2)
valueinnerSlices
  1. :: (Index ix, Source r e)
  2. => Array r ix e
  3. -> Array D Ix1 (Array D (Lower ix) e)
#

Create a delayed array of inner slices.

Examples
Example2 expressions
import Data.Massiv.Array as AA.mapM_ print $ innerSlices (0 ..: (3 :. 2))Array D Seq (Sz1 3)  [ 0 :. 0, 1 :. 0, 2 :. 0 ]Array D Seq (Sz1 3)  [ 0 :. 1, 1 :. 1, 2 :. 1 ]
valueouterSlices
  1. :: (Index ix, Index (Lower ix), Source r e)
  2. => Array r ix e
  3. -> Array D Ix1 (Array r (Lower ix) e)
#

Create a delayed array of outer slices.

Examples
Example2 expressions
import Data.Massiv.Array as AA.mapM_ print $ outerSlices (0 ..: (3 :. 2))Array D Seq (Sz1 2)  [ 0 :. 0, 0 :. 1 ]Array D Seq (Sz1 2)  [ 1 :. 0, 1 :. 1 ]Array D Seq (Sz1 2)  [ 2 :. 0, 2 :. 1 ]
valuewithinSlices
  1. :: (IsIndexDimension ix n, Index (Lower ix), Source r e)
  2. => Dimension n
  3. -> Array r ix e
  4. -> Array D Ix1 (Array D (Lower ix) e)
#

Create a delayed array of slices from within. Checks dimension at compile time.

Examples
Example4 expressions
import Data.Massiv.Array as Aarr = fromIx3 <$> (0 ..: (4 :> 3 :. 2))print arrArray D Seq (Sz (4 :> 3 :. 2))  [ [ [ (0,0,0), (0,0,1) ]    , [ (0,1,0), (0,1,1) ]    , [ (0,2,0), (0,2,1) ]    ]  , [ [ (1,0,0), (1,0,1) ]    , [ (1,1,0), (1,1,1) ]    , [ (1,2,0), (1,2,1) ]    ]  , [ [ (2,0,0), (2,0,1) ]    , [ (2,1,0), (2,1,1) ]    , [ (2,2,0), (2,2,1) ]    ]  , [ [ (3,0,0), (3,0,1) ]    , [ (3,1,0), (3,1,1) ]    , [ (3,2,0), (3,2,1) ]    ]  ]A.mapM_ print $ withinSlices Dim2 arrArray D Seq (Sz (4 :. 2))  [ [ (0,0,0), (0,0,1) ]  , [ (1,0,0), (1,0,1) ]  , [ (2,0,0), (2,0,1) ]  , [ (3,0,0), (3,0,1) ]  ]Array D Seq (Sz (4 :. 2))  [ [ (0,1,0), (0,1,1) ]  , [ (1,1,0), (1,1,1) ]  , [ (2,1,0), (2,1,1) ]  , [ (3,1,0), (3,1,1) ]  ]Array D Seq (Sz (4 :. 2))  [ [ (0,2,0), (0,2,1) ]  , [ (1,2,0), (1,2,1) ]  , [ (2,2,0), (2,2,1) ]  , [ (3,2,0), (3,2,1) ]  ]

Algorithms

0 declarations

Sorting

valuequicksort :: (Manifest r e, Ord e) => Vector r e -> Vector r e
#

This is an implementation of Quicksort, which is an efficient, but unstable sort. This implementation uses Median-of-three for pivot choosing, as such it performs very well not only for random values, but also for common edge cases like already sorted, reversed sorted and arrays with many duplicate elements. It will also respect the computation strategy and will result in a nice speed up for systems with multiple CPUs.

valuetally
  1. :: (Manifest r e, Load r ix e, Ord e)
  2. => Array r ix e
  3. -> Vector DS (e, Int)
#

Count number of occurrences of each element in the array. Results will be sorted in ascending order of the element.

Example
Example4 expressions
import Data.Massiv.Array as Axs = fromList Seq [2, 4, 3, 2, 4, 5, 2, 1] :: Array P Ix1 IntxsArray P Seq (Sz1 8)  [ 2, 4, 3, 2, 4, 5, 2, 1 ]tally xsArray DS Seq (Sz1 5)  [ (1,1), (2,3), (3,1), (4,2), (5,1) ]

Iterations

valueiterateUntil
  1. :: (Load r' ix e, Manifest r e, NFData (Array r ix e))
  2. => (Int -> Array r ix e -> Array r ix e -> Bool)

    Convergence condition. Accepts current iteration counter, array at the previous state and at the current state.

  3. -> (Int -> Array r ix e -> Array r' ix e)

    A modifying function to apply at each iteration. The size of resulting array may differ if necessary

  4. -> Array r ix e

    Initial source array

  5. -> Array r ix e
#

Efficiently iterate a function until a convergence condition is satisfied. If the size of array doesn't change between iterations then no more than two new arrays will be allocated, regardless of the number of iterations. If the size does change from one iteration to another, an attempt will be made to grow/shrink the intermediate mutable array instead of allocating a new one.

Example
Example6 expressions
import Data.Massiv.Arraylet arr = computeAs P $ makeLoadArrayS (Sz2 8 8) (0 :: Int) $ \ w -> () <$ w (0 :. 0) 1arrArray P Seq (Sz (8 :. 8))  [ [ 1, 0, 0, 0, 0, 0, 0, 0 ]  , [ 0, 0, 0, 0, 0, 0, 0, 0 ]  , [ 0, 0, 0, 0, 0, 0, 0, 0 ]  , [ 0, 0, 0, 0, 0, 0, 0, 0 ]  , [ 0, 0, 0, 0, 0, 0, 0, 0 ]  , [ 0, 0, 0, 0, 0, 0, 0, 0 ]  , [ 0, 0, 0, 0, 0, 0, 0, 0 ]  , [ 0, 0, 0, 0, 0, 0, 0, 0 ]  ]let nextPascalRow cur above = if cur == 0 then above else curlet pascal = makeStencil (Sz2 2 2) 1 $ \ get -> nextPascalRow (get (0 :. 0)) (get (-1 :. -1) + get (-1 :. 0))iterateUntil (\_ _ a -> (a ! (7 :. 7)) /= 0) (\ _ -> mapStencil (Fill 0) pascal) arrArray P Seq (Sz (8 :. 8))  [ [ 1, 0, 0, 0, 0, 0, 0, 0 ]  , [ 1, 1, 0, 0, 0, 0, 0, 0 ]  , [ 1, 2, 1, 0, 0, 0, 0, 0 ]  , [ 1, 3, 3, 1, 0, 0, 0, 0 ]  , [ 1, 4, 6, 4, 1, 0, 0, 0 ]  , [ 1, 5, 10, 10, 5, 1, 0, 0 ]  , [ 1, 6, 15, 20, 15, 6, 1, 0 ]  , [ 1, 7, 21, 35, 35, 21, 7, 1 ]  ]

Conversion

8 declarations
valuefromLists'
  1. :: (HasCallStack, Ragged L ix e, Manifest r e)
  2. => Comp

    Computation startegy to use

  3. -> [ListItem ix e]

    Nested list

  4. -> Array r ix e
#

Same as fromListsM, but will throw an error on irregular shaped lists.

Note: This function is the same as if you would turn on {-# LANGUAGE OverloadedLists #-} extension. For that reason you can also use GHC.Exts.fromList.

Property
\xs -> fromLists' Seq xs == (fromList Seq xs :: Vector P Int)
Examples

Convert a list of lists into a 2D Array

Example2 expressions
import Data.Massiv.Array as AfromLists' Seq [[1,2,3],[4,5,6]] :: Array U Ix2 IntArray U Seq (Sz (2 :. 3))  [ [ 1, 2, 3 ]  , [ 4, 5, 6 ]  ]

Above example implemented using GHC's OverloadedLists extension:

Example2 expressions
:set -XOverloadedLists[[1,2,3],[4,5,6]] :: Array U Ix2 IntArray U Seq (Sz (2 :. 3))  [ [ 1, 2, 3 ]  , [ 4, 5, 6 ]  ]
valuefromListsM
  1. :: (Ragged L ix e, Manifest r e, MonadThrow m)
  2. => Comp
  3. -> [ListItem ix e]
  4. -> m (Array r ix e)
#

O(n) - Convert a nested list into an array. Nested list must be of a rectangular shape, otherwise a runtime error will occur. Also, nestedness must match the rank of resulting array, which should be specified through an explicit type signature.

Examples
Example2 expressions
import Data.Massiv.Array as AfromListsM Seq [[1,2,3],[4,5,6]] :: Maybe (Array U Ix2 Int)Just (Array U Seq (Sz (2 :. 3))  [ [ 1, 2, 3 ]  , [ 4, 5, 6 ]  ])
Example1 expression
fromListsM Par [[[1,2,3]],[[4,5,6]]] :: Maybe (Array U Ix3 Int)Just (Array U Par (Sz (2 :> 1 :. 3))  [ [ [ 1, 2, 3 ]    ]  , [ [ 4, 5, 6 ]    ]  ])

Elements of a boxed array could be lists themselves if necessary, but cannot be ragged:

Example4 expressions
fromListsM Seq [[[1,2,3]],[[4,5]]] :: Maybe (Array B Ix2 [Int])Just (Array B Seq (Sz (2 :. 1))  [ [ [1,2,3] ]  , [ [4,5] ]  ])fromListsM Seq [[[1,2,3]],[[4,5]]] :: Maybe (Array B Ix3 Integer)NothingfromListsM Seq [[[1,2,3]],[[4,5,6],[7,8,9]]] :: IO (Array B Ix3 Integer)*** Exception: DimTooLongException for (Dim 2): expected (Sz1 1), got (Sz1 2)fromListsM Seq [[1,2,3,4],[5,6,7]] :: IO (Matrix B Integer)*** Exception: DimTooShortException for (Dim 1): expected (Sz1 4), got (Sz1 3)
valuetoList :: (Index ix, Source r e) => Array r ix e -> [e]
#

Convert any array to a flat list.

Examples
Example2 expressions
import Data.Massiv.ArraytoList $ makeArrayR U Seq (Sz (2 :. 3)) fromIx2[(0,0),(0,1),(0,2),(1,0),(1,1),(1,2)]
valuetoLists
  1. :: (Ragged L ix e, Shape r ix, Source r e)
  2. => Array r ix e

    Array to be converted to nested lists

  3. -> [ListItem ix e]
#

O(n) - Convert an array into a nested list. Number of array dimensions and list nestedness will always match, but you can use toList, toLists2, etc. if flattening of inner dimensions is desired.

Note: This function is almost the same as toList.

Examples
Example4 expressions
import Data.Massiv.Arrayarr = makeArrayR U Seq (Sz (2 :> 1 :. 3)) idarrArray U Seq (Sz (2 :> 1 :. 3))  [ [ [ 0 :> 0 :. 0, 0 :> 0 :. 1, 0 :> 0 :. 2 ]    ]  , [ [ 1 :> 0 :. 0, 1 :> 0 :. 1, 1 :> 0 :. 2 ]    ]  ]toLists arr[[[0 :> 0 :. 0,0 :> 0 :. 1,0 :> 0 :. 2]],[[1 :> 0 :. 0,1 :> 0 :. 1,1 :> 0 :. 2]]]
valuetoLists2
  1. :: (Source r e, Index ix, Index (Lower ix))
  2. => Array r ix e
  3. -> [[e]]
#

Convert an array with at least 2 dimensions into a list of lists. Inner dimensions will get flattened.

Examples
Example3 expressions
import Data.Massiv.ArraytoLists2 $ makeArrayR U Seq (Sz2 2 3) fromIx2[[(0,0),(0,1),(0,2)],[(1,0),(1,1),(1,2)]]toLists2 $ makeArrayR U Seq (Sz3 2 1 3) fromIx3[[(0,0,0),(0,0,1),(0,0,2)],[(1,0,0),(1,0,1),(1,0,2)]]

Mutable

0 declarations

Core

0 declarations

Representations

0 declarations

Stencil

0 declarations

Numeric Operations

0 declarations