HORIZON HASKELLDocslts/ghc-10.0.x8fef28d2026-11-02Search packagesCtrl K
Theme: |

GHC 10.0.0.20260917 · lts/ghc-10.0.x · 8fef28d · 2026-11-02

Modulemassiv-1.0.5.0Haskell2010

Data.Massiv.Array.Numeric

Contents
  1. Numeric
  2. Pointwise addition
  3. Pointwise subtraction
  4. Pointwise multiplication
  5. Dot product
  6. Matrix multiplication
  7. Norms
  8. Simple matrices
  9. Integral
  10. Fractional
  11. Floating
  12. RealFrac
  13. RealFloat
Synopsis
  • Packagemassiv-1.0.5.0
  • Exports73
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourceNumeric.hs

Numeric

classclass FoldNumeric r e => Numeric r e where
#
Instances7Numeric, …
  • Num e => Numeric D eDefined in massiv-1.0.5.0 · Data.Massiv.Array.Delayed.Pull
  • Num e => Numeric B eDefined in massiv-1.0.5.0 · Data.Massiv.Array.Manifest.Boxed
  • Num e => Numeric BL eDefined in massiv-1.0.5.0 · Data.Massiv.Array.Manifest.Boxed
  • (NFData e, Num e) => Numeric BN eDefined in massiv-1.0.5.0 · Data.Massiv.Array.Manifest.Boxed
  • (Storable e, Num e) => Numeric S eDefined in massiv-1.0.5.0 · Data.Massiv.Array.Manifest.Storable
  • (Prim e, Num e) => Numeric P eDefined in massiv-1.0.5.0 · Data.Massiv.Array.Manifest.Primitive
  • (Unbox e, Num e) => Numeric U eDefined in massiv-1.0.5.0 · Data.Massiv.Array.Manifest.Unboxed

Pointwise addition

value(!+!)
  1. :: (HasCallStack, Index ix, Numeric r e)
  2. => Array r ix e
  3. -> Array r ix e
  4. -> Array r ix e

Add two arrays together pointwise. Prefer to use monadic version of this function .+. whenever possible, because it is better to avoid partial functions.

Partial

Mismatched array sizes will result in an impure exception being thrown.

Example
Example3 expressions
let a1 = Ix1 0 ... 10let a2 = Ix1 20 ... 30a1 !+! a2Array D Seq (Sz1 11)  [ 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40 ]
valuesumArraysM
  1. :: (Foldable t, Load r ix e, Numeric r e, MonadThrow m)
  2. => t (Array r ix e)
  3. -> m (Array r ix e)

Compute sum of arrays pointwise. All arrays must have the same size.

Examples
Example6 expressions
import Data.Massiv.Array as AsumArraysM [] :: IO (Array P Ix3 Int)Array P Seq (Sz (0 :> 0 :. 0))  [  ]arr = A.makeArrayR P Seq (Sz3 4 5 6) $ \(i :> j :. k) -> i + j * karrArray P Seq (Sz (4 :> 5 :. 6))  [ [ [ 0, 0, 0, 0, 0, 0 ]    , [ 0, 1, 2, 3, 4, 5 ]    , [ 0, 2, 4, 6, 8, 10 ]    , [ 0, 3, 6, 9, 12, 15 ]    , [ 0, 4, 8, 12, 16, 20 ]    ]  , [ [ 1, 1, 1, 1, 1, 1 ]    , [ 1, 2, 3, 4, 5, 6 ]    , [ 1, 3, 5, 7, 9, 11 ]    , [ 1, 4, 7, 10, 13, 16 ]    , [ 1, 5, 9, 13, 17, 21 ]    ]  , [ [ 2, 2, 2, 2, 2, 2 ]    , [ 2, 3, 4, 5, 6, 7 ]    , [ 2, 4, 6, 8, 10, 12 ]    , [ 2, 5, 8, 11, 14, 17 ]    , [ 2, 6, 10, 14, 18, 22 ]    ]  , [ [ 3, 3, 3, 3, 3, 3 ]    , [ 3, 4, 5, 6, 7, 8 ]    , [ 3, 5, 7, 9, 11, 13 ]    , [ 3, 6, 9, 12, 15, 18 ]    , [ 3, 7, 11, 15, 19, 23 ]    ]  ]sumArraysM $ outerSlices arrArray P Seq (Sz (5 :. 6))  [ [ 6, 6, 6, 6, 6, 6 ]  , [ 6, 10, 14, 18, 22, 26 ]  , [ 6, 14, 22, 30, 38, 46 ]  , [ 6, 18, 30, 42, 54, 66 ]  , [ 6, 22, 38, 54, 70, 86 ]  ]sumArraysM $ innerSlices arrArray D Seq (Sz (4 :. 5))  [ [ 0, 15, 30, 45, 60 ]  , [ 6, 21, 36, 51, 66 ]  , [ 12, 27, 42, 57, 72 ]  , [ 18, 33, 48, 63, 78 ]  ]

Pointwise subtraction

value(.-) :: (Index ix, Numeric r e) => Array r ix e -> e -> Array r ix e

Subtract a scalar from each element of the array. Array is on the left.

value(-.) :: (Index ix, Numeric r e) => e -> Array r ix e -> Array r ix e

Subtract each element of the array from a scalar. Array is on the right.

value(!-!)
  1. :: (Index ix, Numeric r e)
  2. => Array r ix e
  3. -> Array r ix e
  4. -> Array r ix e

Subtract one array from another pointwise. Prefer to use monadic version of this function .-. whenever possible, because it is better to avoid partial functions.

Partial

Mismatched array sizes will result in an impure exception being thrown.

Example
Example3 expressions
let a1 = Ix1 0 ... 10let a2 = Ix1 20 ... 30a1 !-! a2Array D Seq (Sz1 11)  [ -20, -20, -20, -20, -20, -20, -20, -20, -20, -20, -20 ]

Pointwise multiplication

value(.*) :: (Index ix, Numeric r e) => Array r ix e -> e -> Array r ix e

Multiply each element of the array by a scalar value. Scalar is on the right.

Example
Example3 expressions
let arr = Ix1 20 ..: 25arrArray D Seq (Sz1 5)  [ 20, 21, 22, 23, 24 ]arr .* 10Array D Seq (Sz1 5)  [ 200, 210, 220, 230, 240 ]
value(*.) :: (Index ix, Numeric r e) => e -> Array r ix e -> Array r ix e

Multiply each element of the array by a scalar value. Scalar is on the left.

Example
Example3 expressions
let arr = Ix1 20 ..: 25arrArray D Seq (Sz1 5)  [ 20, 21, 22, 23, 24 ]10 *. arrArray D Seq (Sz1 5)  [ 200, 210, 220, 230, 240 ]
value(!*!)
  1. :: (Index ix, Numeric r e)
  2. => Array r ix e
  3. -> Array r ix e
  4. -> Array r ix e

Multiplication of two arrays pointwise, i.e. Hadamard product. Prefer to use monadic version of this function .*. whenever possible, because it is better to avoid partial functions.

Partial

Mismatched array sizes will result in an impure exception being thrown.

Example
Example3 expressions
let a1 = Ix1 0 ... 10let a2 = Ix1 20 ... 30a1 !*! a2Array D Seq (Sz1 11)  [ 0, 21, 44, 69, 96, 125, 156, 189, 224, 261, 300 ]
value(.^) :: (Index ix, Numeric r e) => Array r ix e -> Int -> Array r ix e

Raise each element of the array to a power.

Example
Example3 expressions
let arr = Ix1 20 ..: 25arrArray D Seq (Sz1 5)  [ 20, 21, 22, 23, 24 ]arr .^ 3Array D Seq (Sz1 5)  [ 8000, 9261, 10648, 12167, 13824 ]
valueproductArraysM
  1. :: (Foldable t, Load r ix e, Numeric r e, MonadThrow m)
  2. => t (Array r ix e)
  3. -> m (Array r ix e)

Compute product of arrays pointwise. All arrays must have the same size.

Examples
Example6 expressions
import Data.Massiv.Array as AproductArraysM [] :: IO (Array P Ix3 Int)Array P Seq (Sz (0 :> 0 :. 0))  [  ]arr = A.makeArrayR P Seq (Sz3 4 5 6) $ \(i :> j :. k) -> i + j * karrArray P Seq (Sz (4 :> 5 :. 6))  [ [ [ 0, 0, 0, 0, 0, 0 ]    , [ 0, 1, 2, 3, 4, 5 ]    , [ 0, 2, 4, 6, 8, 10 ]    , [ 0, 3, 6, 9, 12, 15 ]    , [ 0, 4, 8, 12, 16, 20 ]    ]  , [ [ 1, 1, 1, 1, 1, 1 ]    , [ 1, 2, 3, 4, 5, 6 ]    , [ 1, 3, 5, 7, 9, 11 ]    , [ 1, 4, 7, 10, 13, 16 ]    , [ 1, 5, 9, 13, 17, 21 ]    ]  , [ [ 2, 2, 2, 2, 2, 2 ]    , [ 2, 3, 4, 5, 6, 7 ]    , [ 2, 4, 6, 8, 10, 12 ]    , [ 2, 5, 8, 11, 14, 17 ]    , [ 2, 6, 10, 14, 18, 22 ]    ]  , [ [ 3, 3, 3, 3, 3, 3 ]    , [ 3, 4, 5, 6, 7, 8 ]    , [ 3, 5, 7, 9, 11, 13 ]    , [ 3, 6, 9, 12, 15, 18 ]    , [ 3, 7, 11, 15, 19, 23 ]    ]  ]productArraysM $ outerSlices arrArray P Seq (Sz (5 :. 6))  [ [ 0, 0, 0, 0, 0, 0 ]  , [ 0, 24, 120, 360, 840, 1680 ]  , [ 0, 120, 840, 3024, 7920, 17160 ]  , [ 0, 360, 3024, 11880, 32760, 73440 ]  , [ 0, 840, 7920, 32760, 93024, 212520 ]  ]productArraysM $ innerSlices arrArray D Seq (Sz (4 :. 5))  [ [ 0, 0, 0, 0, 0 ]  , [ 1, 720, 10395, 58240, 208845 ]  , [ 64, 5040, 46080, 209440, 665280 ]  , [ 729, 20160, 135135, 524880, 1514205 ]  ]

Dot product

value(!.!) :: (Numeric r e, Source r e) => Vector r e -> Vector r e -> e

Dot product of two vectors.

Partial

Throws an impure exception when lengths of vectors do not match

Matrix multiplication

value(!><)
  1. :: (Numeric r e, Source r e)
  2. => Matrix r e

    Matrix

  3. -> Vector r e

    Column vector (Used many times, so make sure it is computed)

  4. -> Vector D e

Multiply a matrix by a column vector

Partial

Throws impure exception when inner dimensions do not agree

value(><!)
  1. :: (Numeric r e, Manifest r e)
  2. => Vector r e

    Row vector (Used many times, so make sure it is computed)

  3. -> Matrix r e

    Matrix

  4. -> Vector r e

Multiply a row vector by a matrix.

Partial

Throws impure exception when inner dimensions do not agree

value(!><!)
  1. :: (Numeric r e, Manifest r e)
  2. => Matrix r e
  3. -> Matrix r e
  4. -> Matrix r e

Multiply two matrices together.

Partial

Inner dimension must agree

Examples
Example4 expressions
import Data.Massiv.Arraya1 = makeArrayR P Seq (Sz2 5 6) $ \(i :. j) -> i + ja2 = makeArrayR P Seq (Sz2 6 5) $ \(i :. j) -> i - ja1 !><! a2Array P Seq (Sz (5 :. 5))  [ [ 55, 40, 25, 10, -5 ]  , [ 70, 49, 28, 7, -14 ]  , [ 85, 58, 31, 4, -23 ]  , [ 100, 67, 34, 1, -32 ]  , [ 115, 76, 37, -2, -41 ]  ]

Norms

Simple matrices

valueidentityMatrix :: Num e => Sz1 -> Matrix DL e

Create an indentity matrix.

Example
Example2 expressions
import Data.Massiv.ArrayidentityMatrix 5Array DL Seq (Sz (5 :. 5))  [ [ 1, 0, 0, 0, 0 ]  , [ 0, 1, 0, 0, 0 ]  , [ 0, 0, 1, 0, 0 ]  , [ 0, 0, 0, 1, 0 ]  , [ 0, 0, 0, 0, 1 ]  ]
valuelowerTriangular :: Num e => Comp -> Sz1 -> (Ix2 -> e) -> Matrix DL e

Create a lower triangular (L in LU decomposition) matrix of size NxN

Example
Example2 expressions
import Data.Massiv.ArraylowerTriangular Seq 5 (\(i :. j) -> i + j)Array DL Seq (Sz (5 :. 5))  [ [ 0, 0, 0, 0, 0 ]  , [ 1, 2, 0, 0, 0 ]  , [ 2, 3, 4, 0, 0 ]  , [ 3, 4, 5, 6, 0 ]  , [ 4, 5, 6, 7, 8 ]  ]
valueupperTriangular :: Num e => Comp -> Sz1 -> (Ix2 -> e) -> Matrix DL e

Create an upper triangular (U in LU decomposition) matrix of size NxN

Example
Example2 expressions
import Data.Massiv.ArrayupperTriangular Par 5 (\(i :. j) -> i + j)Array DL Par (Sz (5 :. 5))  [ [ 0, 1, 2, 3, 4 ]  , [ 0, 2, 3, 4, 5 ]  , [ 0, 0, 4, 5, 6 ]  , [ 0, 0, 0, 6, 7 ]  , [ 0, 0, 0, 0, 8 ]  ]

Integral

valuequotA
  1. :: (HasCallStack, Index ix, Source r1 e, Source r2 e, Integral e)
  2. => Array r1 ix e
  3. -> Array r2 ix e
  4. -> Array D ix e

Perform a pointwise quotient where first array contains numerators and the second one denominators

quotA arr1 arr2 == zipWith quot arr1 arr2
Partial

Mismatched array sizes will result in an impure exception being thrown.

valuedivA
  1. :: (HasCallStack, Index ix, Source r1 e, Source r2 e, Integral e)
  2. => Array r1 ix e
  3. -> Array r2 ix e
  4. -> Array D ix e

Perform a pointwise integer division where first array contains numerators and the second one denominators

divA arr1 arr2 == zipWith div arr1 arr2
Partial

Mismatched array sizes will result in an impure exception being thrown.

valuequotRemA
  1. :: (HasCallStack, Index ix, Source r1 e, Source r2 e, Integral e)
  2. => Array r1 ix e
  3. -> Array r2 ix e
  4. -> (Array D ix e, Array D ix e)

Perform a pointwise quotient with remainder where first array contains numerators and the second one denominators

quotRemA arr1 arr2 == zipWith quotRem arr1 arr2
Partial

Mismatched array sizes will result in an impure exception being thrown.

valuedivModA
  1. :: (HasCallStack, Index ix, Source r1 e, Source r2 e, Integral e)
  2. => Array r1 ix e
  3. -> Array r2 ix e
  4. -> (Array D ix e, Array D ix e)

Perform a pointwise integer division with modulo where first array contains numerators and the second one denominators

divModA arr1 arr2 == zipWith divMod arr1 arr2
Partial

Mismatched array sizes will result in an impure exception being thrown.

Fractional

value(./) :: (Index ix, NumericFloat r e) => Array r ix e -> e -> Array r ix e

Divide each element of the array by a scalar value.

Example
Example3 expressions
let arr = fromIntegral <$> (Ix1 20 ..: 25) :: Array D Ix1 FloatarrArray D Seq (Sz1 5)  [ 20.0, 21.0, 22.0, 23.0, 24.0 ]arr ./ 100Array D Seq (Sz1 5)  [ 0.2, 0.21, 0.22, 0.23, 0.24 ]
value(/.) :: (Index ix, NumericFloat r e) => e -> Array r ix e -> Array r ix e

Divide a scalar value by each element of the array.

e /. arr == e *. recipA arr
Example
Example3 expressions
let arr = fromIntegral <$> (Ix1 20 ..: 25) :: Array D Ix1 FloatarrArray D Seq (Sz1 5)  [ 20.0, 21.0, 22.0, 23.0, 24.0 ]100 /. arrArray D Seq (Sz1 5)  [ 5.0, 4.7619047, 4.5454545, 4.347826, 4.1666665 ]
value(!/!)
  1. :: (Index ix, NumericFloat r e)
  2. => Array r ix e
  3. -> Array r ix e
  4. -> Array r ix e

Divide two arrays pointwise. Prefer to use monadic version of this function ./. whenever possible, because it is better to avoid partial functions.

Partial

Mismatched array sizes will result in an impure exception being thrown.

Example
Example3 expressions
let arr1 = fromIntegral <$> (Ix1 20 ..: 25) :: Array D Ix1 Floatlet arr2 = fromIntegral <$> (Ix1 100 ..: 105) :: Array D Ix1 Floatarr1 !/! arr2Array D Seq (Sz1 5)  [ 0.2, 0.20792079, 0.21568628, 0.22330096, 0.23076923 ]

Floating

value(.**)
  1. :: (Index ix, Source r1 e, Source r2 e, Floating e)
  2. => Array r1 ix e
  3. -> Array r2 ix e
  4. -> Array D ix e

Apply power to each element of the array where the power value is in the same cell in the second array.

arr1 .** arr2 == zipWith (**) arr1 arr2
Partial

Throws an error when arrays do not have matching sizes

valuelogBaseA
  1. :: (Index ix, Source r1 e, Source r2 e, Floating e)
  2. => Array r1 ix e
  3. -> Array r2 ix e
  4. -> Array D ix e

Apply logarithm to each element of the array where the base is in the same cell in the second array.

logBaseA arr1 arr2 == zipWith logBase arr1 arr2
Partial

Throws an error when arrays do not have matching sizes

RealFrac

RealFloat