| Copyright | David Johnson (c) 2019-2026 |
|---|---|
| License | BSD 3 |
| Maintainer | David Johnson <code@dmj.io> |
| Stability | Experimental |
| Portability | GHC |
| Safe Haskell | None |
| Language | Haskell2010 |
ArrayFire.Algorithm
Description
Functions for aggregation, manipulation of Array
module Main where import qualified ArrayFire as A main :: IO () main = print $ A.sum (A.vector @Double 10 [1..]) 0 -- ArrayFire Array -- [1 1 1 1] -- 55.0000
Synopsis
- sum :: AFType a => Array a -> Int -> Array a
- sumNaN :: (Fractional a, AFType a) => Array a -> Int -> Double -> Array a
- product :: AFType a => Array a -> Int -> Array a
- productNaN :: (AFType a, Fractional a) => Array a -> Int -> Double -> Array a
- min :: AFType a => Array a -> Int -> Array a
- max :: AFType a => Array a -> Int -> Array a
- allTrue :: AFType a => Array a -> Int -> Array CBool
- anyTrue :: AFType a => Array a -> Int -> Array CBool
- count :: AFType a => Array a -> Int -> Array Int
- sumAll :: AFResult a => Array a -> Scalar a
- sumNaNAll :: (AFResult a, Fractional a) => Array a -> Double -> Scalar a
- productAll :: AFResult a => Array a -> Scalar a
- productNaNAll :: (AFResult a, Fractional a) => Array a -> Double -> Scalar a
- minAll :: AFResult a => Array a -> Scalar a
- maxAll :: AFResult a => Array a -> Scalar a
- allTrueAll :: AFResult a => Array a -> Scalar a
- anyTrueAll :: AFResult a => Array a -> Scalar a
- countAll :: AFResult a => Array a -> Scalar a
- imin :: AFType a => Array a -> Int -> (Array a, Array Word32)
- imax :: AFType a => Array a -> Int -> (Array a, Array Word32)
- iminAll :: AFResult a => Array a -> (Scalar a, Int)
- imaxAll :: AFResult a => Array a -> (Scalar a, Int)
- accum :: AFType a => Array a -> Int -> Array a
- scan :: AFType a => Array a -> Int -> BinaryOp -> Bool -> Array a
- scanByKey :: (AFType a, AFType k) => Array k -> Array a -> Int -> BinaryOp -> Bool -> Array a
- where' :: AFType a => Array a -> Array Word32
- diff1 :: AFType a => Array a -> Int -> Array a
- diff2 :: AFType a => Array a -> Int -> Array a
- sort :: AFType a => Array a -> Int -> Order -> Array a
- sortIndex :: AFType a => Array a -> Int -> Order -> (Array a, Array Word32)
- data Order
- sortByKey :: AFType a => Array a -> Array a -> Int -> Order -> (Array a, Array a)
- setUnique :: AFType a => Array a -> Bool -> Array a
- setUnion :: AFType a => Array a -> Array a -> Bool -> Array a
- setIntersect :: AFType a => Array a -> Array a -> Bool -> Array a
- sumByKey :: AFType a => Array Int -> Array a -> Int -> (Array Int, Array a)
- sumByKeyNaN :: AFType a => Array Int -> Array a -> Int -> Double -> (Array Int, Array a)
- productByKey :: AFType a => Array Int -> Array a -> Int -> (Array Int, Array a)
- productByKeyNaN :: AFType a => Array Int -> Array a -> Int -> Double -> (Array Int, Array a)
- minByKey :: AFType a => Array Int -> Array a -> Int -> (Array Int, Array a)
- maxByKey :: AFType a => Array Int -> Array a -> Int -> (Array Int, Array a)
- allTrueByKey :: AFType a => Array Int -> Array a -> Int -> (Array Int, Array CBool)
- anyTrueByKey :: AFType a => Array Int -> Array a -> Int -> (Array Int, Array CBool)
- countByKey :: AFType a => Array Int -> Array a -> Int -> (Array Int, Array Word32)
Documentation
Arguments
| :: AFType a | |
| => Array a | Array to sum |
| -> Int | 0-based Dimension along which to perform sum |
| -> Array a | Will return the sum of all values in the input array along the specified dimension |
Sum all of the elements in Array along the specified dimension
>>>A.sum (A.vector @Double 10 [1..]) 0ArrayFire Array [1 1 1 1] 55.0000
>>>A.sum (A.matrix @Double (10,10) $ replicate 10 [1..]) 1ArrayFire Array [10 1 1 1] 10.0000 20.0000 30.0000 40.0000 50.0000 60.0000 70.0000 80.0000 90.0000 100.0000
Arguments
| :: (Fractional a, AFType a) | |
| => Array a | Array to sum |
| -> Int | Dimension along which to perform sum |
| -> Double | Default value to use in the case of NaN |
| -> Array a | Will return the sum of all values in the input array along the specified dimension, substituted with the default value |
Sum all of the elements in Array along the specified dimension, using a default value for NaN
>>>let nan = 0/0 in A.sumNaN (A.vector @Double 10 (nan : [1..])) 0 10.0ArrayFire Array [1 1 1 1] 55.0000
Arguments
| :: AFType a | |
| => Array a | Array to product |
| -> Int | Dimension along which to perform product |
| -> Array a | Will return the product of all values in the input array along the specified dimension |
Product all of the elements in Array along the specified dimension
>>>A.product (A.vector @Double 10 [1..]) 0ArrayFire Array [1 1 1 1] 3628800.0000
Arguments
| :: (AFType a, Fractional a) | |
| => Array a | Array to product |
| -> Int | Dimension along which to perform product |
| -> Double | Default value to use in the case of NaN |
| -> Array a | Will return the product of all values in the input array along the specified dimension, substituted with the default value |
Product all of the elements in Array along the specified dimension, using a default value for NaN
>>>let nan = 0/0 in A.productNaN (A.vector @Double 10 (nan : [1..])) 0 2.0ArrayFire Array [1 1 1 1] 3628800.0000
Arguments
| :: AFType a | |
| => Array a | Array input |
| -> Int | Dimension along which to retrieve the min element |
| -> Array a | Will contain the minimum of all values in the input array along dim |
Take the minimum of an Array along a specific dimension
>>>A.min (A.vector @Double 10 [1..]) 0ArrayFire Array [1 1 1 1] 1.0000
Arguments
| :: AFType a | |
| => Array a | Array input |
| -> Int | Dimension along which to retrieve the max element |
| -> Array a | Will contain the maximum of all values in the input array along dim |
Take the maximum of an Array along a specific dimension
>>>A.max (A.vector @Double 10 [1..]) 0ArrayFire Array [1 1 1 1] 10.0000
Arguments
| :: AFType a | |
| => Array a | Array input |
| -> Int | Dimension along which to count |
| -> Array Int | Count of all elements along dimension |
Count elements in an Array along a dimension
>>>A.count (A.vector @Double 10 [1..]) 0ArrayFire Array [1 1 1 1] 10
Sum all elements in an Array along all dimensions
>>>A.sumAll (A.vector @Double 10 [1..])(55.0,0.0)
Arguments
| :: (AFResult a, Fractional a) | |
| => Array a | Input array |
| -> Double | NaN substitute |
| -> Scalar a | imaginary and real part |
Sum all elements in an Array along all dimensions, using a default value for NaN
>>>let nan = 0/0 in A.sumNaNAll (A.vector @Double 10 (nan : [1..])) 0.0(55.0,0.0)
Product all elements in an Array along all dimensions, using a default value for NaN
>>>A.productAll (A.vector @Double 10 [1..])(3628800.0,0.0)
Arguments
| :: (AFResult a, Fractional a) | |
| => Array a | Input array |
| -> Double | NaN substitute |
| -> Scalar a | imaginary and real part |
Product all elements in an Array along all dimensions, using a default value for NaN
>>>A.productNaNAll (A.vector @Double 10 [1..]) 1.0(3628800.0,0.0)
Take the minimum across all elements along all dimensions in Array
>>>A.minAll (A.vector @Double 10 [1..])(1.0,0.0)
Take the maximum across all elements along all dimensions in Array
>>>A.maxAll (A.vector @Double 10 [1..])(10.0,0.0)
Decide if all elements along all dimensions in Array are True
>>>A.allTrueAll (A.vector @CBool 10 (repeat 1))(1.0, 0.0)
Decide if any elements along all dimensions in Array are True
>>>A.anyTrueAll $ A.vector @CBool 10 (repeat 0)(0.0,0.0)
Count all elements along all dimensions in Array
>>>A.countAll (A.matrix @Double (100,100) (replicate 100 [1..]))(10000.0,0.0)
Arguments
| :: AFType a | |
| => Array a | Input array |
| -> Int | The dimension along which the minimum value is extracted |
| -> (Array a, Array Word32) | will contain the minimum of all values along dim, will also contain the location of minimum of all values in in along dim |
Find the minimum element along a specified dimension in Array
>>>A.imin (A.vector @Double 10 [1..]) 0(ArrayFire Array [1 1 1 1] 1.0000 ,ArrayFire Array [1 1 1 1] 0 )
Arguments
| :: AFType a | |
| => Array a | Input array |
| -> Int | The dimension along which the minimum value is extracted |
| -> (Array a, Array Word32) | will contain the maximum of all values in in along dim, will also contain the location of maximum of all values in in along dim |
Find the maximum element along a specified dimension in Array
>>>A.imax (A.vector @Double 10 [1..]) 0(ArrayFire Array [1 1 1 1] 10.0000 ,ArrayFire Array [1 1 1 1] 9 )
Arguments
| :: AFResult a | |
| => Array a | Input array |
| -> (Scalar a, Int) | will contain the real part of minimum value of all elements in input in, also will contain the imaginary part of minimum value of all elements in input in, will contain the location of minimum of all values in |
Find the minimum element along all dimensions in Array
>>>A.iminAll (A.vector @Double 10 [1..])(1.0,0.0,0)
Arguments
| :: AFResult a | |
| => Array a | Input array |
| -> (Scalar a, Int) | will contain the real part of maximum value of all elements in input in, also will contain the imaginary part of maximum value of all elements in input in, will contain the location of maximum of all values in |
Find the maximum element along all dimensions in Array
>>>A.imaxAll (A.vector @Double 10 [1..])(10.0,0.0,9)
Arguments
| :: AFType a | |
| => Array a | Input array |
| -> Int | Dimension along which to calculate the sum |
| -> Array a | Contains inclusive sum |
Calculate sum of Array across specified dimension
>>>A.accum (A.vector @Double 10 [1..]) 0ArrayFire Array [10 1 1 1] 1.0000 3.0000 6.0000 10.0000 15.0000 21.0000 28.0000 36.0000 45.0000 55.0000
Arguments
| :: AFType a | |
| => Array a | Is the input array. |
| -> Array Word32 | Indices where input array is non-zero |
Find indices where input Array is non zero
>>>A.where' (A.vector @Double 10 (repeat 0))ArrayFire Array [0 1 1 1] <empty>
Arguments
| :: AFType a | |
| => Array a | Input array |
| -> Int | Dimension along which numerical difference is performed |
| -> Array a | Will contain first order numerical difference |
First order numerical difference along specified dimension.
>>>A.diff1 (A.vector @Double 4 [10,35,65,95]) 0ArrayFire Array [3 1 1 1] 25.0000 30.0000 30.0000
Arguments
| :: AFType a | |
| => Array a | Input array |
| -> Int | Dimension along which numerical difference is performed |
| -> Array a | Will contain second order numerical difference |
Second order numerical difference along specified dimension.
>>>A.diff2 (A.vector @Double 5 [1.0,20,55,89,44]) 0ArrayFire Array [3 1 1 1] 16.0000 -1.0000 -79.0000
Arguments
| :: AFType a | |
| => Array a | Input array |
| -> Int | Dimension along |
| -> Order | Return results in ascending order |
| -> Array a | Will contain sorted input |
Sort an Array along a specified dimension, specifying ordering of results (ascending / descending)
>>>A.sort (A.vector @Double 4 [ 2,4,3,1 ]) 0 AscArrayFire Array [4 1 1 1] 1.0000 2.0000 3.0000 4.0000
>>>A.sort (A.vector @Double 4 [ 2,4,3,1 ]) 0 DescArrayFire Array [4 1 1 1] 4.0000 3.0000 2.0000 1.0000
Arguments
| :: AFType a | |
| => Array a | Input array |
| -> Int | Dimension along |
| -> Order | Return results in ascending order |
| -> (Array a, Array Word32) | Contains the sorted, contains indices for original input |
Sort an Array along a specified dimension, specifying ordering of results (ascending / descending), returns indices of sorted results
>>>A.sortIndex (A.vector @Double 4 [3,2,1,4]) 0 Asc(ArrayFire Array [4 1 1 1] 1.0000 2.0000 3.0000 4.0000 ,ArrayFire Array [4 1 1 1] 2 1 0 3 )
Data type for expressing sort order
Arguments
| :: AFType a | |
| => Array a | Keys input array |
| -> Array a | Values input array |
| -> Int | Dimension along which to perform the operation |
| -> Order | Return results in ascending order |
| -> (Array a, Array a) |
Sort an Array along a specified dimension by keys, specifying ordering of results (ascending / descending)
>>>A.sortByKey (A.vector @Double 4 [2,1,4,3]) (A.vector @Double 4 [10,9,8,7]) 0 True(ArrayFire Array [4 1 1 1] 1.0000 2.0000 3.0000 4.0000 ,ArrayFire Array [4 1 1 1] 9.0000 10.0000 7.0000 8.0000 )
Arguments
| :: AFType a | |
| => Array a | input array |
| -> Bool | if true, skips the sorting steps internally |
| -> Array a | Will contain the unique values from in |
Finds the unique values in an Array, specifying if sorting should occur.
>>>A.setUnique (A.vector @Double 2 [1.0,1.0]) TrueArrayFire Array [1 1 1 1] 1.0000
Arguments
| :: AFType a | |
| => Array Int | Keys array (contiguous equal keys form a group) |
| -> Array a | Values array |
| -> Int | Dimension along which to reduce |
| -> (Array Int, Array a) | (reduced keys, reduced values) |
Sum values in Array grouped by keys along a dimension.
Each contiguous run of equal keys in keys produces one output element.
Returns (keys_out, vals_out).
>>>sumByKey (vector @Int 5 [1,1,2,2,2]) (vector @Double 5 [10,20,1,2,3]) 0(ArrayFire Array [2 1 1 1] 1 2, ArrayFire Array [2 1 1 1] 30.0000 6.0000)
Arguments
| :: AFType a | |
| => Array Int | Keys array |
| -> Array a | Values array |
| -> Int | Dimension |
| -> Double | Substitute for NaN values |
| -> (Array Int, Array a) | (reduced keys, reduced values) |
sumByKey replacing NaN values with a substitute before summing.
Arguments
| :: AFType a | |
| => Array Int | Keys array |
| -> Array a | Values array |
| -> Int | Dimension |
| -> (Array Int, Array a) |
Product of values in Array grouped by keys along a dimension.
Arguments
| :: AFType a | |
| => Array Int | Keys array |
| -> Array a | Values array |
| -> Int | Dimension |
| -> Double | Substitute for NaN values |
| -> (Array Int, Array a) |
productByKey replacing NaN values with a substitute before multiplying.
Arguments
| :: AFType a | |
| => Array Int | Keys array |
| -> Array a | Values array |
| -> Int | Dimension |
| -> (Array Int, Array a) |
Minimum of values in Array grouped by keys along a dimension.
Arguments
| :: AFType a | |
| => Array Int | Keys array |
| -> Array a | Values array |
| -> Int | Dimension |
| -> (Array Int, Array a) |
Maximum of values in Array grouped by keys along a dimension.
Arguments
| :: AFType a | |
| => Array Int | Keys array |
| -> Array a | Values array (treated as boolean) |
| -> Int | Dimension |
| -> (Array Int, Array CBool) |
True if all values are true within each key group.
The value output is always boolean (b8) regardless of the input value type.