{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE RecordWildCards #-}
module Data.Massiv.Array.Stencil.Unsafe (
makeUnsafeStencil,
makeUnsafeConvolutionStencil,
makeUnsafeCorrelationStencil,
unsafeTransformStencil,
) where
import Data.Massiv.Array.Stencil.Internal
import Data.Massiv.Core.Common
import GHC.Exts (inline)
makeUnsafeStencil
:: Index ix
=> Sz ix
-> ix
-> (ix -> (ix -> e) -> a)
-> Stencil ix e a
makeUnsafeStencil :: forall ix e a.
Index ix =>
Sz ix -> ix -> (ix -> (ix -> e) -> a) -> Stencil ix e a
makeUnsafeStencil !Sz ix
sSz !ix
sCenter ix -> (ix -> e) -> a
relStencil = forall ix e a.
Sz ix
-> ix -> ((ix -> e) -> (ix -> e) -> ix -> a) -> Stencil ix e a
Stencil Sz ix
sSz ix
sCenter forall {p}. (ix -> e) -> p -> ix -> a
stencil
where
stencil :: (ix -> e) -> p -> ix -> a
stencil ix -> e
unsafeGetVal p
_getVal !ix
ix =
forall a. a -> a
inline (ix -> (ix -> e) -> a
relStencil ix
ix (ix -> e
unsafeGetVal forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall ix. Index ix => (Int -> Int -> Int) -> ix -> ix -> ix
liftIndex2 forall a. Num a => a -> a -> a
(+) ix
ix))
{-# INLINE stencil #-}
{-# INLINE makeUnsafeStencil #-}
makeUnsafeConvolutionStencil
:: (Index ix, Num e)
=> Sz ix
-> ix
-> ((ix -> e -> e -> e) -> e -> e)
-> Stencil ix e e
makeUnsafeConvolutionStencil :: forall ix e.
(Index ix, Num e) =>
Sz ix -> ix -> ((ix -> e -> e -> e) -> e -> e) -> Stencil ix e e
makeUnsafeConvolutionStencil !Sz ix
sz !ix
sCenter (ix -> e -> e -> e) -> e -> e
relStencil =
forall ix e a.
Sz ix
-> ix -> ((ix -> e) -> (ix -> e) -> ix -> a) -> Stencil ix e a
Stencil Sz ix
sz ix
sInvertCenter forall {p}. (ix -> e) -> p -> ix -> e
stencil
where
!sInvertCenter :: ix
sInvertCenter = forall ix. Index ix => (Int -> Int -> Int) -> ix -> ix -> ix
liftIndex2 (-) (forall ix. Index ix => (Int -> Int) -> ix -> ix
liftIndex (forall a. Num a => a -> a -> a
subtract Int
1) (forall ix. Sz ix -> ix
unSz Sz ix
sz)) ix
sCenter
stencil :: (ix -> e) -> p -> ix -> e
stencil ix -> e
uget p
_ !ix
ix =
(forall a. a -> a
inline (ix -> e -> e -> e) -> e -> e
relStencil forall a b. (a -> b) -> a -> b
$ \ !ix
ixD !e
kVal !e
acc -> ix -> e
uget (forall ix. Index ix => (Int -> Int -> Int) -> ix -> ix -> ix
liftIndex2 (-) ix
ix ix
ixD) forall a. Num a => a -> a -> a
* e
kVal forall a. Num a => a -> a -> a
+ e
acc) e
0
{-# INLINE stencil #-}
{-# INLINE makeUnsafeConvolutionStencil #-}
makeUnsafeCorrelationStencil
:: (Index ix, Num e)
=> Sz ix
-> ix
-> ((ix -> e -> e -> e) -> e -> e)
-> Stencil ix e e
makeUnsafeCorrelationStencil :: forall ix e.
(Index ix, Num e) =>
Sz ix -> ix -> ((ix -> e -> e -> e) -> e -> e) -> Stencil ix e e
makeUnsafeCorrelationStencil !Sz ix
sSz !ix
sCenter (ix -> e -> e -> e) -> e -> e
relStencil = forall ix e a.
Sz ix
-> ix -> ((ix -> e) -> (ix -> e) -> ix -> a) -> Stencil ix e a
Stencil Sz ix
sSz ix
sCenter forall {p}. p -> (ix -> e) -> ix -> e
stencil
where
stencil :: p -> (ix -> e) -> ix -> e
stencil p
_ ix -> e
getVal !ix
ix =
(forall a. a -> a
inline (ix -> e -> e -> e) -> e -> e
relStencil forall a b. (a -> b) -> a -> b
$ \ !ix
ixD !e
kVal !e
acc -> ix -> e
getVal (forall ix. Index ix => (Int -> Int -> Int) -> ix -> ix -> ix
liftIndex2 forall a. Num a => a -> a -> a
(+) ix
ix ix
ixD) forall a. Num a => a -> a -> a
* e
kVal forall a. Num a => a -> a -> a
+ e
acc) e
0
{-# INLINE stencil #-}
{-# INLINE makeUnsafeCorrelationStencil #-}
unsafeTransformStencil
:: (Sz ix' -> Sz ix)
-> (ix' -> ix)
-> ( ((ix' -> e) -> (ix' -> e) -> ix' -> a)
-> (ix -> e)
-> (ix -> e)
-> ix
-> a
)
-> Stencil ix' e a
-> Stencil ix e a
unsafeTransformStencil :: forall ix' ix e a.
(Sz ix' -> Sz ix)
-> (ix' -> ix)
-> (((ix' -> e) -> (ix' -> e) -> ix' -> a)
-> (ix -> e) -> (ix -> e) -> ix -> a)
-> Stencil ix' e a
-> Stencil ix e a
unsafeTransformStencil Sz ix' -> Sz ix
transformSize ix' -> ix
transformIndex ((ix' -> e) -> (ix' -> e) -> ix' -> a)
-> (ix -> e) -> (ix -> e) -> ix -> a
transformFunc Stencil{ix'
Sz ix'
(ix' -> e) -> (ix' -> e) -> ix' -> a
stencilFunc :: forall ix e a. Stencil ix e a -> (ix -> e) -> (ix -> e) -> ix -> a
stencilCenter :: forall ix e a. Stencil ix e a -> ix
stencilSize :: forall ix e a. Stencil ix e a -> Sz ix
stencilFunc :: (ix' -> e) -> (ix' -> e) -> ix' -> a
stencilCenter :: ix'
stencilSize :: Sz ix'
..} =
Stencil
{ stencilSize :: Sz ix
stencilSize = Sz ix' -> Sz ix
transformSize Sz ix'
stencilSize
, stencilCenter :: ix
stencilCenter = ix' -> ix
transformIndex ix'
stencilCenter
, stencilFunc :: (ix -> e) -> (ix -> e) -> ix -> a
stencilFunc = ((ix' -> e) -> (ix' -> e) -> ix' -> a)
-> (ix -> e) -> (ix -> e) -> ix -> a
transformFunc (ix' -> e) -> (ix' -> e) -> ix' -> a
stencilFunc
}
{-# INLINE unsafeTransformStencil #-}