{-# LANGUAGE FunctionalDependencies, UndecidableInstances #-}
module Data.Array.Repa.Operators.Mapping
(
map
, zipWith
, (+^), (-^), (*^), (/^)
, Structured(..))
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.HintInterleave
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, Source 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)
{-# INLINE [3] map #-}
zipWith :: (Shape sh, Source r1 a, Source 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)
{-# INLINE get #-}
in fromFunction
(intersectDim (extent arr1) (extent arr2))
get
{-# INLINE [2] zipWith #-}
infixl 7 *^, /^
infixl 6 +^, -^
(+^) = zipWith (+)
{-# INLINE (+^) #-}
(-^) = zipWith (-)
{-# INLINE (-^) #-}
(*^) = zipWith (*)
{-# INLINE (*^) #-}
(/^) = zipWith (/)
{-# INLINE (/^) #-}
class Structured r1 a b where
type TR r1
smap :: Shape sh
=> (a -> b)
-> Array r1 sh a
-> Array (TR r1) sh b
szipWith
:: (Shape sh, Source r c)
=> (c -> a -> b)
-> Array r sh c
-> Array r1 sh a
-> Array (TR r1) sh b
instance Structured B Word8 b where
type TR B = D
smap = map
szipWith = zipWith
instance Structured C a b where
type TR C = C
smap f (ACursored sh makec shiftc loadc)
= ACursored sh makec shiftc (f . loadc)
{-# INLINE [3] smap #-}
szipWith f arr1 (ACursored sh makec shiftc loadc)
= let makec' ix = (ix, makec ix)
{-# INLINE makec' #-}
shiftc' off (ix, cur) = (addDim off ix, shiftc off cur)
{-# INLINE shiftc' #-}
load' (ix, cur) = f (arr1 `unsafeIndex` ix) (loadc cur)
{-# INLINE load' #-}
in ACursored
(intersectDim (extent arr1) sh)
makec' shiftc' load'
{-# INLINE [2] szipWith #-}
instance Structured D a b where
type TR D = D
smap = map
szipWith = zipWith
instance Storable a => Structured F a b where
type TR F = D
smap = map
szipWith = zipWith
instance (Structured r1 a b
, Structured r2 a b)
=> Structured (P r1 r2) a b where
type TR (P r1 r2) = P (TR r1) (TR r2)
smap f (APart sh range arr1 arr2)
= APart sh range (smap f arr1) (smap f arr2)
{-# INLINE [3] smap #-}
szipWith f arr1 (APart sh range arr21 arr22)
= APart sh range (szipWith f arr1 arr21)
(szipWith f arr1 arr22)
{-# INLINE [2] szipWith #-}
instance Structured r1 a b
=> Structured (S r1) a b where
type TR (S r1) = S (TR r1)
smap f (ASmall arr1)
= ASmall (smap f arr1)
{-# INLINE [3] smap #-}
szipWith f arr1 (ASmall arr2)
= ASmall (szipWith f arr1 arr2)
{-# INLINE [3] szipWith #-}
instance Structured r1 a b
=> Structured (I r1) a b where
type TR (I r1) = I (TR r1)
smap f (AInterleave arr1)
= AInterleave (smap f arr1)
{-# INLINE [3] smap #-}
szipWith f arr1 (AInterleave arr2)
= AInterleave (szipWith f arr1 arr2)
{-# INLINE [3] szipWith #-}
instance Unbox a => Structured U a b where
type TR U = D
smap = map
szipWith = zipWith
instance Structured X a b where
type TR X = X
smap _ (AUndefined sh) = AUndefined sh
szipWith _ _ (AUndefined sh) = AUndefined sh