module Data.Array.Repa.Operators.Mapping
(
map
, zipWith
, (+^), (-^), (*^), (/^)
, Combine(..))
where
import Data.Array.Repa.Shape
import Data.Array.Repa.Base
import Data.Array.Repa.Repr.ByteString
import Data.Array.Repa.Repr.Cursored
import Data.Array.Repa.Repr.Delayed
import Data.Array.Repa.Repr.ForeignPtr
import Data.Array.Repa.Repr.HintSmall
import Data.Array.Repa.Repr.Partitioned
import Data.Array.Repa.Repr.Unboxed
import Data.Array.Repa.Repr.Undefined
import Prelude hiding (map, zipWith)
import Foreign.Storable
import Data.Word
map :: (Shape sh, Repr r a)
=> (a -> b) -> Array r sh a -> Array D sh b
map f arr
= case delay arr of
ADelayed sh g -> ADelayed sh (f . g)
zipWith :: (Shape sh, Repr r1 a, Repr r2 b)
=> (a -> b -> c)
-> Array r1 sh a -> Array r2 sh b
-> Array D sh c
zipWith f arr1 arr2
= let
get ix = f (arr1 `unsafeIndex` ix) (arr2 `unsafeIndex` ix)
in fromFunction
(intersectDim (extent arr1) (extent arr2))
get
(+^) = zipWith (+)
(-^) = zipWith ()
(*^) = zipWith (*)
(/^) = zipWith (/)
class Combine r1 a r2 b | r1 -> r2 where
cmap :: Shape sh
=> (a -> b)
-> Array r1 sh a
-> Array r2 sh b
czipWith
:: (Shape sh, Repr r c)
=> (c -> a -> b)
-> Array r sh c
-> Array r1 sh a
-> Array r2 sh b
instance Combine B Word8 D b where
cmap = map
czipWith = zipWith
instance Combine C a C b where
cmap f (ACursored sh makec shiftc loadc)
= ACursored sh makec shiftc (f . loadc)
czipWith f arr1 (ACursored sh makec shiftc loadc)
= let
makec' ix = (ix, makec ix)
shiftc' off (ix, cur) = (addDim off ix, shiftc off cur)
load' (ix, cur) = f (arr1 `unsafeIndex` ix) (loadc cur)
in ACursored
(intersectDim (extent arr1) sh)
makec' shiftc' load'
instance Combine D a D b where
cmap = map
czipWith = zipWith
instance Storable a => Combine F a D b where
cmap = map
czipWith = zipWith
instance (Combine r11 a r21 b
, Combine r12 a r22 b)
=> Combine (P r11 r12) a (P r21 r22) b where
cmap f (APart sh range arr1 arr2)
= APart sh range (cmap f arr1) (cmap f arr2)
czipWith f arr1 (APart sh range arr21 arr22)
= APart sh range (czipWith f arr1 arr21)
(czipWith f arr1 arr22)
instance Combine r1 a r2 b
=> Combine (S r1) a (S r2) b where
cmap f (ASmall arr1)
= ASmall (cmap f arr1)
czipWith f arr1 (ASmall arr2)
= ASmall (czipWith f arr1 arr2)
instance Unbox a => Combine U a D b where
cmap = map
czipWith = zipWith
instance Combine X a X b where
cmap _ (AUndefined sh) = AUndefined sh
czipWith _ _ (AUndefined sh) = AUndefined sh