module TREXIO.Internal.Marshaller where

import Data.Massiv.Array as Massiv hiding (withMArray)
import Data.Massiv.Array.Unsafe
import Data.Maybe (fromJust)
import Foreign

{- | Passes a mutable 'MArray' to a C function. The array may be deallocated
on C side or modified in place. This funciton is super dangerous. You array may
suddenly be gone!
-}
withMArray :: (Index ix) => MArray s S ix a -> (Ptr b -> IO c) -> IO c
withMArray :: forall ix s a b c.
Index ix =>
MArray s S ix a -> (Ptr b -> IO c) -> IO c
withMArray MArray s S ix a
v Ptr b -> IO c
f = do
  let (ForeignPtr a
arrFPtr, Int
_arrL) = MArray s S ix a -> (ForeignPtr a, Int)
forall ix s e. Index ix => MArray s S ix e -> (ForeignPtr e, Int)
unsafeMArrayToForeignPtr MArray s S ix a
v
  ForeignPtr a -> (Ptr a -> IO c) -> IO c
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr a
arrFPtr ((Ptr a -> IO c) -> IO c) -> (Ptr a -> IO c) -> IO c
forall a b. (a -> b) -> a -> b
$ \Ptr a
arrPtr -> Ptr b -> IO c
f (Ptr a -> Ptr b
forall a b. Ptr a -> Ptr b
castPtr Ptr a
arrPtr)

{- | Pass a 'Massiv.Array' to a C function. This function is safe, but the array
is copied to a new memory location.
-}
withArray :: (Storable a, Index ix) => Array S ix a -> (Ptr b -> IO c) -> IO c
withArray :: forall a ix b c.
(Storable a, Index ix) =>
Array S ix a -> (Ptr b -> IO c) -> IO c
withArray Array S ix a
v Ptr b -> IO c
f = do
  MArray RealWorld S ix a
mArr <- Array S ix a -> IO (MArray RealWorld S ix a)
forall r ix e (m :: * -> *).
(Manifest r e, Index ix, MonadIO m) =>
Array r ix e -> m (MArray RealWorld r ix e)
thaw Array S ix a
v
  MArray RealWorld S ix a -> (Ptr b -> IO c) -> IO c
forall ix s a b c.
Index ix =>
MArray s S ix a -> (Ptr b -> IO c) -> IO c
withMArray MArray RealWorld S ix a
mArr Ptr b -> IO c
f

-- | Get an 'MArray' from C memory. Haskell and C side use the same memory reference.
-- Be careful with this function. When the undelying pointer vanishes, the array is nonsense
unsafeToMArray :: (Index ix, Storable a) => Sz ix -> Ptr a -> IO (MArray s S ix a)
unsafeToMArray :: forall ix a s.
(Index ix, Storable a) =>
Sz ix -> Ptr a -> IO (MArray s S ix a)
unsafeToMArray Sz ix
sz Ptr a
ptr = do
  ForeignPtr a
fPtr <- Ptr a -> IO (ForeignPtr a)
forall a. Ptr a -> IO (ForeignPtr a)
newForeignPtr_ Ptr a
ptr
  let mArr :: MArray s S Int a
mArr = ForeignPtr a -> Sz1 -> MArray s S Int a
forall e s. ForeignPtr e -> Sz1 -> MArray s S Int e
unsafeMArrayFromForeignPtr0 ForeignPtr a
fPtr (Sz ix -> Sz1
forall ix. Index ix => Sz ix -> Sz1
toLinearSz Sz ix
sz)
  Sz ix -> MArray s S Int a -> IO (MArray s S ix a)
forall r e ix' ix (m :: * -> *) s.
(Manifest r e, Index ix', Index ix, MonadThrow m) =>
Sz ix' -> MArray s r ix e -> m (MArray s r ix' e)
resizeMArrayM Sz ix
sz MArray s S Int a
forall {s}. MArray s S Int a
mArr

{- | Get an 'Array' from C memory. The underlying C array is copied and the
result is safe to use.
-}
peekArray :: (Index ix, Storable a) => Sz ix -> Ptr a -> IO (Array S ix a)
peekArray :: forall ix a.
(Index ix, Storable a) =>
Sz ix -> Ptr a -> IO (Array S ix a)
peekArray Sz ix
sz Ptr a
ptr = do
  MArray RealWorld S ix a
mArr <- Sz ix -> Ptr a -> IO (MArray RealWorld S ix a)
forall ix a s.
(Index ix, Storable a) =>
Sz ix -> Ptr a -> IO (MArray s S ix a)
unsafeToMArray Sz ix
sz Ptr a
ptr
  Comp -> MArray RealWorld S ix a -> IO (Array S ix a)
forall r ix e (m :: * -> *).
(Manifest r e, Index ix, MonadIO m) =>
Comp -> MArray RealWorld r ix e -> m (Array r ix e)
freeze Comp
Par MArray RealWorld S ix a
mArr

-- | Peek an integer array from C memory and cast it into standard int types.
peekIntArray :: (Index ix, Manifest r Int) => Sz ix -> Ptr Int32 -> IO (Array r ix Int)
peekIntArray :: forall ix r.
(Index ix, Manifest r Int) =>
Sz ix -> Ptr Int32 -> IO (Array r ix Int)
peekIntArray Sz ix
sz Ptr Int32
ptr = do
  MArray RealWorld S ix Int32
mArr <- Sz ix -> Ptr Int32 -> IO (MArray RealWorld S ix Int32)
forall ix a s.
(Index ix, Storable a) =>
Sz ix -> Ptr a -> IO (MArray s S ix a)
unsafeToMArray Sz ix
sz (Ptr Int32 -> Ptr Int32
forall a b. Ptr a -> Ptr b
castPtr Ptr Int32
ptr)
  Array D ix Int -> Array r ix Int
forall r ix e r'.
(Manifest r e, Load r' ix e) =>
Array r' ix e -> Array r ix e
compute (Array D ix Int -> Array r ix Int)
-> (Array S ix Int32 -> Array D ix Int)
-> Array S ix Int32
-> Array r ix Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Int32 -> Int) -> Array S ix Int32 -> Array D ix Int
forall ix r e' e.
(Index ix, Source r e') =>
(e' -> e) -> Array r ix e' -> Array D ix e
Massiv.map (Int32 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral :: Int32 -> Int) (Array S ix Int32 -> Array r ix Int)
-> IO (Array S ix Int32) -> IO (Array r ix Int)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Comp -> MArray RealWorld S ix Int32 -> IO (Array S ix Int32)
forall r ix e (m :: * -> *).
(Manifest r e, Index ix, MonadIO m) =>
Comp -> MArray RealWorld r ix e -> m (Array r ix e)
freeze Comp
Par MArray RealWorld S ix Int32
mArr

-- | Peek 2D coordinates from C memory.
peek2DCoords :: (Manifest r Ix2) => Sz1 -> Ptr Int32 -> IO (Vector r Ix2)
peek2DCoords :: forall r. Manifest r Ix2 => Sz1 -> Ptr Int32 -> IO (Vector r Ix2)
peek2DCoords Sz1
sz Ptr Int32
ptr = do
  Matrix S Int
coordsSimple :: Matrix S Int <- Sz Ix2 -> Ptr Int32 -> IO (Matrix S Int)
forall ix r.
(Index ix, Manifest r Int) =>
Sz ix -> Ptr Int32 -> IO (Array r ix Int)
peekIntArray (Sz1 -> Sz (Lower Ix2) -> Sz Ix2
forall ix. Index ix => Sz1 -> Sz (Lower ix) -> Sz ix
consSz Sz1
sz Sz1
Sz (Lower Ix2)
2) Ptr Int32
ptr
  Vector r Ix2 -> IO (Vector r Ix2)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (Vector r Ix2 -> IO (Vector r Ix2))
-> (Matrix S Int -> Vector r Ix2)
-> Matrix S Int
-> IO (Vector r Ix2)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Array D Int Ix2 -> Vector r Ix2
forall r ix e r'.
(Manifest r e, Load r' ix e) =>
Array r' ix e -> Array r ix e
compute (Array D Int Ix2 -> Vector r Ix2)
-> (Matrix S Int -> Array D Int Ix2)
-> Matrix S Int
-> Vector r Ix2
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Array S Int Int -> Ix2)
-> Array D Int (Array S Int Int) -> Array D Int Ix2
forall ix r e' e.
(Index ix, Source r e') =>
(e' -> e) -> Array r ix e' -> Array D ix e
Massiv.map Array S Int Int -> Ix2
forall {r} {ix}.
(Manifest r Int, Index ix, Num ix) =>
Array r ix Int -> Ix2
mkIx2 (Array D Int (Array S Int Int) -> Array D Int Ix2)
-> (Matrix S Int -> Array D Int (Array S Int Int))
-> Matrix S Int
-> Array D Int Ix2
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Matrix S Int -> Array D Int (Array S Int Int)
Matrix S Int -> Array D Int (Array S (Lower Ix2) Int)
forall r ix e.
(Index ix, Index (Lower ix), Source r e) =>
Array r ix e -> Array D Int (Array r (Lower ix) e)
outerSlices (Matrix S Int -> IO (Vector r Ix2))
-> Matrix S Int -> IO (Vector r Ix2)
forall a b. (a -> b) -> a -> b
$ Matrix S Int
coordsSimple
 where
  mkIx2 :: Array r ix Int -> Ix2
mkIx2 Array r ix Int
v = Int -> Int -> Ix2
Ix2 (Array r ix Int
v Array r ix Int -> ix -> Int
forall r ix e.
(HasCallStack, Manifest r e, Index ix) =>
Array r ix e -> ix -> e
! ix
0) (Array r ix Int
v Array r ix Int -> ix -> Int
forall r ix e.
(HasCallStack, Manifest r e, Index ix) =>
Array r ix e -> ix -> e
! ix
1)

-- | Peek 3D coordinates from C memory.
peek3DCoords :: (Manifest r Ix3) => Sz1 -> Ptr Int32 -> IO (Vector r Ix3)
peek3DCoords :: forall r. Manifest r Ix3 => Sz1 -> Ptr Int32 -> IO (Vector r Ix3)
peek3DCoords Sz1
sz Ptr Int32
ptr = do
  Matrix S Int
coordsSimple :: Matrix S Int <- Sz Ix2 -> Ptr Int32 -> IO (Matrix S Int)
forall ix r.
(Index ix, Manifest r Int) =>
Sz ix -> Ptr Int32 -> IO (Array r ix Int)
peekIntArray (Sz1 -> Sz (Lower Ix2) -> Sz Ix2
forall ix. Index ix => Sz1 -> Sz (Lower ix) -> Sz ix
consSz Sz1
sz Sz1
Sz (Lower Ix2)
3) Ptr Int32
ptr
  Vector r Ix3 -> IO (Vector r Ix3)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (Vector r Ix3 -> IO (Vector r Ix3))
-> (Matrix S Int -> Vector r Ix3)
-> Matrix S Int
-> IO (Vector r Ix3)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Array D Int Ix3 -> Vector r Ix3
forall r ix e r'.
(Manifest r e, Load r' ix e) =>
Array r' ix e -> Array r ix e
compute (Array D Int Ix3 -> Vector r Ix3)
-> (Matrix S Int -> Array D Int Ix3)
-> Matrix S Int
-> Vector r Ix3
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Array S Int Int -> Ix3)
-> Array D Int (Array S Int Int) -> Array D Int Ix3
forall ix r e' e.
(Index ix, Source r e') =>
(e' -> e) -> Array r ix e' -> Array D ix e
Massiv.map Array S Int Int -> Ix3
forall {r} {ix}.
(Manifest r Int, Index ix, Num ix) =>
Array r ix Int -> Ix3
mkIx3 (Array D Int (Array S Int Int) -> Array D Int Ix3)
-> (Matrix S Int -> Array D Int (Array S Int Int))
-> Matrix S Int
-> Array D Int Ix3
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Matrix S Int -> Array D Int (Array S Int Int)
Matrix S Int -> Array D Int (Array S (Lower Ix2) Int)
forall r ix e.
(Index ix, Index (Lower ix), Source r e) =>
Array r ix e -> Array D Int (Array r (Lower ix) e)
outerSlices (Matrix S Int -> IO (Vector r Ix3))
-> Matrix S Int -> IO (Vector r Ix3)
forall a b. (a -> b) -> a -> b
$ Matrix S Int
coordsSimple
 where
  mkIx3 :: Array r ix Int -> Ix3
mkIx3 Array r ix Int
v = Int -> Int -> Int -> Ix3
Ix3 (Array r ix Int
v Array r ix Int -> ix -> Int
forall r ix e.
(HasCallStack, Manifest r e, Index ix) =>
Array r ix e -> ix -> e
! ix
0) (Array r ix Int
v Array r ix Int -> ix -> Int
forall r ix e.
(HasCallStack, Manifest r e, Index ix) =>
Array r ix e -> ix -> e
! ix
1) (Array r ix Int
v Array r ix Int -> ix -> Int
forall r ix e.
(HasCallStack, Manifest r e, Index ix) =>
Array r ix e -> ix -> e
! ix
2)

-- | Peek 4D coordinates from C memory.
peek4DCoords :: (Manifest r Ix4) => Sz1 -> Ptr Int32 -> IO (Vector r Ix4)
peek4DCoords :: forall r. Manifest r Ix4 => Sz1 -> Ptr Int32 -> IO (Vector r Ix4)
peek4DCoords Sz1
sz Ptr Int32
ptr = do
  Matrix S Int
coordsSimple :: Matrix S Int <- Sz Ix2 -> Ptr Int32 -> IO (Matrix S Int)
forall ix r.
(Index ix, Manifest r Int) =>
Sz ix -> Ptr Int32 -> IO (Array r ix Int)
peekIntArray (Sz1 -> Sz (Lower Ix2) -> Sz Ix2
forall ix. Index ix => Sz1 -> Sz (Lower ix) -> Sz ix
consSz Sz1
sz Sz1
Sz (Lower Ix2)
4) Ptr Int32
ptr
  Vector r Ix4 -> IO (Vector r Ix4)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (Vector r Ix4 -> IO (Vector r Ix4))
-> (Matrix S Int -> Vector r Ix4)
-> Matrix S Int
-> IO (Vector r Ix4)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Array D Int Ix4 -> Vector r Ix4
forall r ix e r'.
(Manifest r e, Load r' ix e) =>
Array r' ix e -> Array r ix e
compute (Array D Int Ix4 -> Vector r Ix4)
-> (Matrix S Int -> Array D Int Ix4)
-> Matrix S Int
-> Vector r Ix4
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Array S Int Int -> Ix4)
-> Array D Int (Array S Int Int) -> Array D Int Ix4
forall ix r e' e.
(Index ix, Source r e') =>
(e' -> e) -> Array r ix e' -> Array D ix e
Massiv.map Array S Int Int -> Ix4
forall {r} {ix}.
(Manifest r Int, Index ix, Num ix) =>
Array r ix Int -> Ix4
mkIx4 (Array D Int (Array S Int Int) -> Array D Int Ix4)
-> (Matrix S Int -> Array D Int (Array S Int Int))
-> Matrix S Int
-> Array D Int Ix4
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Matrix S Int -> Array D Int (Array S Int Int)
Matrix S Int -> Array D Int (Array S (Lower Ix2) Int)
forall r ix e.
(Index ix, Index (Lower ix), Source r e) =>
Array r ix e -> Array D Int (Array r (Lower ix) e)
outerSlices (Matrix S Int -> IO (Vector r Ix4))
-> Matrix S Int -> IO (Vector r Ix4)
forall a b. (a -> b) -> a -> b
$ Matrix S Int
coordsSimple
 where
  mkIx4 :: Array r ix Int -> Ix4
mkIx4 Array r ix Int
v = Int -> Int -> Int -> Int -> Ix4
Ix4 (Array r ix Int
v Array r ix Int -> ix -> Int
forall r ix e.
(HasCallStack, Manifest r e, Index ix) =>
Array r ix e -> ix -> e
! ix
0) (Array r ix Int
v Array r ix Int -> ix -> Int
forall r ix e.
(HasCallStack, Manifest r e, Index ix) =>
Array r ix e -> ix -> e
! ix
1) (Array r ix Int
v Array r ix Int -> ix -> Int
forall r ix e.
(HasCallStack, Manifest r e, Index ix) =>
Array r ix e -> ix -> e
! ix
2) (Array r ix Int
v Array r ix Int -> ix -> Int
forall r ix e.
(HasCallStack, Manifest r e, Index ix) =>
Array r ix e -> ix -> e
! ix
3)

-- | Peek 5D coordinates from C memory.
peek6DCoords :: (Manifest r (IxN 6)) => Sz1 -> Ptr Int32 -> IO (Vector r (IxN 6))
peek6DCoords :: forall r.
Manifest r (IxN 6) =>
Sz1 -> Ptr Int32 -> IO (Vector r (IxN 6))
peek6DCoords Sz1
sz Ptr Int32
ptr = do
  Matrix S Int
coordsSimple :: Matrix S Int <- Sz Ix2 -> Ptr Int32 -> IO (Matrix S Int)
forall ix r.
(Index ix, Manifest r Int) =>
Sz ix -> Ptr Int32 -> IO (Array r ix Int)
peekIntArray (Sz1 -> Sz (Lower Ix2) -> Sz Ix2
forall ix. Index ix => Sz1 -> Sz (Lower ix) -> Sz ix
consSz Sz1
sz Sz1
Sz (Lower Ix2)
6) Ptr Int32
ptr
  Vector r (IxN 6) -> IO (Vector r (IxN 6))
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (Vector r (IxN 6) -> IO (Vector r (IxN 6)))
-> (Matrix S Int -> Vector r (IxN 6))
-> Matrix S Int
-> IO (Vector r (IxN 6))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Array D Int (IxN 6) -> Vector r (IxN 6)
forall r ix e r'.
(Manifest r e, Load r' ix e) =>
Array r' ix e -> Array r ix e
compute (Array D Int (IxN 6) -> Vector r (IxN 6))
-> (Matrix S Int -> Array D Int (IxN 6))
-> Matrix S Int
-> Vector r (IxN 6)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Array S Int Int -> IxN 6)
-> Array D Int (Array S Int Int) -> Array D Int (IxN 6)
forall ix r e' e.
(Index ix, Source r e') =>
(e' -> e) -> Array r ix e' -> Array D ix e
Massiv.map Array S Int Int -> IxN 6
forall {r} {ix}.
(Manifest r Int, Index ix, Num ix) =>
Array r ix Int -> IxN 6
mkIx6 (Array D Int (Array S Int Int) -> Array D Int (IxN 6))
-> (Matrix S Int -> Array D Int (Array S Int Int))
-> Matrix S Int
-> Array D Int (IxN 6)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Matrix S Int -> Array D Int (Array S Int Int)
Matrix S Int -> Array D Int (Array S (Lower Ix2) Int)
forall r ix e.
(Index ix, Index (Lower ix), Source r e) =>
Array r ix e -> Array D Int (Array r (Lower ix) e)
outerSlices (Matrix S Int -> IO (Vector r (IxN 6)))
-> Matrix S Int -> IO (Vector r (IxN 6))
forall a b. (a -> b) -> a -> b
$ Matrix S Int
coordsSimple
 where
  mkIx6 :: Array r ix Int -> IxN 6
mkIx6 Array r ix Int
v = (Array r ix Int
v Array r ix Int -> ix -> Int
forall r ix e.
(HasCallStack, Manifest r e, Index ix) =>
Array r ix e -> ix -> e
! ix
0) Int -> Ix (6 - 1) -> IxN 6
forall (n :: Nat). Int -> Ix (n - 1) -> IxN n
:> (Array r ix Int
v Array r ix Int -> ix -> Int
forall r ix e.
(HasCallStack, Manifest r e, Index ix) =>
Array r ix e -> ix -> e
! ix
1) Int -> Ix (5 - 1) -> IxN 5
forall (n :: Nat). Int -> Ix (n - 1) -> IxN n
:> (Array r ix Int
v Array r ix Int -> ix -> Int
forall r ix e.
(HasCallStack, Manifest r e, Index ix) =>
Array r ix e -> ix -> e
! ix
2) Int -> Ix (4 - 1) -> Ix4
forall (n :: Nat). Int -> Ix (n - 1) -> IxN n
:> (Array r ix Int
v Array r ix Int -> ix -> Int
forall r ix e.
(HasCallStack, Manifest r e, Index ix) =>
Array r ix e -> ix -> e
! ix
3) Int -> Ix (3 - 1) -> Ix3
forall (n :: Nat). Int -> Ix (n - 1) -> IxN n
:> (Array r ix Int
v Array r ix Int -> ix -> Int
forall r ix e.
(HasCallStack, Manifest r e, Index ix) =>
Array r ix e -> ix -> e
! ix
4) Int -> Int -> Ix2
:. (Array r ix Int
v Array r ix Int -> ix -> Int
forall r ix e.
(HasCallStack, Manifest r e, Index ix) =>
Array r ix e -> ix -> e
! ix
5)

peek8DCoords :: (Manifest r (IxN 8)) => Sz1 -> Ptr Int32 -> IO (Vector r (IxN 8))
peek8DCoords :: forall r.
Manifest r (IxN 8) =>
Sz1 -> Ptr Int32 -> IO (Vector r (IxN 8))
peek8DCoords Sz1
sz Ptr Int32
ptr = do
  Matrix S Int
coordsSimple :: Matrix S Int <- Sz Ix2 -> Ptr Int32 -> IO (Matrix S Int)
forall ix r.
(Index ix, Manifest r Int) =>
Sz ix -> Ptr Int32 -> IO (Array r ix Int)
peekIntArray (Sz1 -> Sz (Lower Ix2) -> Sz Ix2
forall ix. Index ix => Sz1 -> Sz (Lower ix) -> Sz ix
consSz Sz1
sz Sz1
Sz (Lower Ix2)
8) Ptr Int32
ptr
  Vector r (IxN 8) -> IO (Vector r (IxN 8))
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (Vector r (IxN 8) -> IO (Vector r (IxN 8)))
-> (Matrix S Int -> Vector r (IxN 8))
-> Matrix S Int
-> IO (Vector r (IxN 8))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Array D Int (IxN 8) -> Vector r (IxN 8)
forall r ix e r'.
(Manifest r e, Load r' ix e) =>
Array r' ix e -> Array r ix e
compute (Array D Int (IxN 8) -> Vector r (IxN 8))
-> (Matrix S Int -> Array D Int (IxN 8))
-> Matrix S Int
-> Vector r (IxN 8)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Array S Int Int -> IxN 8)
-> Array D Int (Array S Int Int) -> Array D Int (IxN 8)
forall ix r e' e.
(Index ix, Source r e') =>
(e' -> e) -> Array r ix e' -> Array D ix e
Massiv.map Array S Int Int -> IxN 8
forall {r} {ix}.
(Manifest r Int, Index ix, Num ix) =>
Array r ix Int -> IxN 8
mkIx8 (Array D Int (Array S Int Int) -> Array D Int (IxN 8))
-> (Matrix S Int -> Array D Int (Array S Int Int))
-> Matrix S Int
-> Array D Int (IxN 8)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Matrix S Int -> Array D Int (Array S Int Int)
Matrix S Int -> Array D Int (Array S (Lower Ix2) Int)
forall r ix e.
(Index ix, Index (Lower ix), Source r e) =>
Array r ix e -> Array D Int (Array r (Lower ix) e)
outerSlices (Matrix S Int -> IO (Vector r (IxN 8)))
-> Matrix S Int -> IO (Vector r (IxN 8))
forall a b. (a -> b) -> a -> b
$ Matrix S Int
coordsSimple
 where
  mkIx8 :: Array r ix Int -> IxN 8
mkIx8 Array r ix Int
v = (Array r ix Int
v Array r ix Int -> ix -> Int
forall r ix e.
(HasCallStack, Manifest r e, Index ix) =>
Array r ix e -> ix -> e
! ix
0) Int -> Ix (8 - 1) -> IxN 8
forall (n :: Nat). Int -> Ix (n - 1) -> IxN n
:> (Array r ix Int
v Array r ix Int -> ix -> Int
forall r ix e.
(HasCallStack, Manifest r e, Index ix) =>
Array r ix e -> ix -> e
! ix
1) Int -> Ix (7 - 1) -> IxN 7
forall (n :: Nat). Int -> Ix (n - 1) -> IxN n
:> (Array r ix Int
v Array r ix Int -> ix -> Int
forall r ix e.
(HasCallStack, Manifest r e, Index ix) =>
Array r ix e -> ix -> e
! ix
2) Int -> Ix (6 - 1) -> IxN 6
forall (n :: Nat). Int -> Ix (n - 1) -> IxN n
:> (Array r ix Int
v Array r ix Int -> ix -> Int
forall r ix e.
(HasCallStack, Manifest r e, Index ix) =>
Array r ix e -> ix -> e
! ix
3) Int -> Ix (5 - 1) -> IxN 5
forall (n :: Nat). Int -> Ix (n - 1) -> IxN n
:> (Array r ix Int
v Array r ix Int -> ix -> Int
forall r ix e.
(HasCallStack, Manifest r e, Index ix) =>
Array r ix e -> ix -> e
! ix
4) Int -> Ix (4 - 1) -> Ix4
forall (n :: Nat). Int -> Ix (n - 1) -> IxN n
:> (Array r ix Int
v Array r ix Int -> ix -> Int
forall r ix e.
(HasCallStack, Manifest r e, Index ix) =>
Array r ix e -> ix -> e
! ix
5) Int -> Ix (3 - 1) -> Ix3
forall (n :: Nat). Int -> Ix (n - 1) -> IxN n
:> (Array r ix Int
v Array r ix Int -> ix -> Int
forall r ix e.
(HasCallStack, Manifest r e, Index ix) =>
Array r ix e -> ix -> e
! ix
6) Int -> Int -> Ix2
:. (Array r ix Int
v Array r ix Int -> ix -> Int
forall r ix e.
(HasCallStack, Manifest r e, Index ix) =>
Array r ix e -> ix -> e
! ix
7)

-- | Convert a vector of 'Ix2' to a \( m \times 2 \) matrix.
castCoords2D :: (Manifest r1 Ix2, Manifest r2 Int32) => Vector r1 Ix2 -> Matrix r2 Int32
castCoords2D :: forall r1 r2.
(Manifest r1 Ix2, Manifest r2 Int32) =>
Vector r1 Ix2 -> Matrix r2 Int32
castCoords2D Vector r1 Ix2
ixVec =
  Array DL Ix2 Int32 -> Array r2 Ix2 Int32
forall r ix e r'.
(Manifest r e, Load r' ix e) =>
Array r' ix e -> Array r ix e
compute
    (Array DL Ix2 Int32 -> Array r2 Ix2 Int32)
-> (Vector r1 Ix2 -> Array DL Ix2 Int32)
-> Vector r1 Ix2
-> Array r2 Ix2 Int32
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Maybe (Array DL Ix2 Int32) -> Array DL Ix2 Int32
forall a. HasCallStack => Maybe a -> a
fromJust
    (Maybe (Array DL Ix2 Int32) -> Array DL Ix2 Int32)
-> (Vector r1 Ix2 -> Maybe (Array DL Ix2 Int32))
-> Vector r1 Ix2
-> Array DL Ix2 Int32
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Array D Int (Array S Int Int32) -> Maybe (Array DL Ix2 Int32)
Array D Int (Array S (Lower Ix2) Int32)
-> Maybe (Array DL Ix2 Int32)
forall r ix e (f :: * -> *) (m :: * -> *).
(Foldable f, MonadThrow m, Index (Lower ix), Source r e,
 Index ix) =>
f (Array r (Lower ix) e) -> m (Array DL ix e)
stackOuterSlicesM
    (Array D Int (Array S Int Int32) -> Maybe (Array DL Ix2 Int32))
-> (Vector r1 Ix2 -> Array D Int (Array S Int Int32))
-> Vector r1 Ix2
-> Maybe (Array DL Ix2 Int32)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Ix2 -> Array S Int Int32)
-> Vector r1 Ix2 -> Array D Int (Array S Int Int32)
forall ix r e' e.
(Index ix, Source r e') =>
(e' -> e) -> Array r ix e' -> Array D ix e
Massiv.map Ix2 -> Array S Int Int32
ix2ToRow
    (Vector r1 Ix2 -> Array r2 Ix2 Int32)
-> Vector r1 Ix2 -> Array r2 Ix2 Int32
forall a b. (a -> b) -> a -> b
$ Vector r1 Ix2
ixVec
 where
  ix2ToRow :: Ix2 -> Vector S Int32
  ix2ToRow :: Ix2 -> Array S Int Int32
ix2ToRow (Ix2 Int
x Int
y) = Comp -> [Int32] -> Array S Int Int32
forall r e. Manifest r e => Comp -> [e] -> Vector r e
fromList Comp
Seq [Int -> Int32
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
x, Int -> Int32
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
y]

-- | Convert a vector of 'Ix3' to a \( m \times 3 \) matrix.
castCoords3D :: (Manifest r1 Ix3, Manifest r2 Int32) => Vector r1 Ix3 -> Matrix r2 Int32
castCoords3D :: forall r1 r2.
(Manifest r1 Ix3, Manifest r2 Int32) =>
Vector r1 Ix3 -> Matrix r2 Int32
castCoords3D Vector r1 Ix3
ixVec =
  Array DL Ix2 Int32 -> Array r2 Ix2 Int32
forall r ix e r'.
(Manifest r e, Load r' ix e) =>
Array r' ix e -> Array r ix e
compute
    (Array DL Ix2 Int32 -> Array r2 Ix2 Int32)
-> (Vector r1 Ix3 -> Array DL Ix2 Int32)
-> Vector r1 Ix3
-> Array r2 Ix2 Int32
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Maybe (Array DL Ix2 Int32) -> Array DL Ix2 Int32
forall a. HasCallStack => Maybe a -> a
fromJust
    (Maybe (Array DL Ix2 Int32) -> Array DL Ix2 Int32)
-> (Vector r1 Ix3 -> Maybe (Array DL Ix2 Int32))
-> Vector r1 Ix3
-> Array DL Ix2 Int32
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Array D Int (Array S Int Int32) -> Maybe (Array DL Ix2 Int32)
Array D Int (Array S (Lower Ix2) Int32)
-> Maybe (Array DL Ix2 Int32)
forall r ix e (f :: * -> *) (m :: * -> *).
(Foldable f, MonadThrow m, Index (Lower ix), Source r e,
 Index ix) =>
f (Array r (Lower ix) e) -> m (Array DL ix e)
stackOuterSlicesM
    (Array D Int (Array S Int Int32) -> Maybe (Array DL Ix2 Int32))
-> (Vector r1 Ix3 -> Array D Int (Array S Int Int32))
-> Vector r1 Ix3
-> Maybe (Array DL Ix2 Int32)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Ix3 -> Array S Int Int32)
-> Vector r1 Ix3 -> Array D Int (Array S Int Int32)
forall ix r e' e.
(Index ix, Source r e') =>
(e' -> e) -> Array r ix e' -> Array D ix e
Massiv.map Ix3 -> Array S Int Int32
ix3ToRow
    (Vector r1 Ix3 -> Array r2 Ix2 Int32)
-> Vector r1 Ix3 -> Array r2 Ix2 Int32
forall a b. (a -> b) -> a -> b
$ Vector r1 Ix3
ixVec
 where
  ix3ToRow :: Ix3 -> Vector S Int32
  ix3ToRow :: Ix3 -> Array S Int Int32
ix3ToRow (Ix3 Int
x Int
y Int
z) =
    Comp -> [Int32] -> Array S Int Int32
forall r e. Manifest r e => Comp -> [e] -> Vector r e
fromList
      Comp
Seq
      [ Int -> Int32
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
x
      , Int -> Int32
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
y
      , Int -> Int32
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
z
      ]

-- | Convert a vector of 'Ix4' to a \( m \times 4 \) matrix.
castCoords4D :: (Manifest r1 Ix4, Manifest r2 Int32) => Vector r1 Ix4 -> Matrix r2 Int32
castCoords4D :: forall r1 r2.
(Manifest r1 Ix4, Manifest r2 Int32) =>
Vector r1 Ix4 -> Matrix r2 Int32
castCoords4D Vector r1 Ix4
ixVec =
  Array DL Ix2 Int32 -> Array r2 Ix2 Int32
forall r ix e r'.
(Manifest r e, Load r' ix e) =>
Array r' ix e -> Array r ix e
compute
    (Array DL Ix2 Int32 -> Array r2 Ix2 Int32)
-> (Vector r1 Ix4 -> Array DL Ix2 Int32)
-> Vector r1 Ix4
-> Array r2 Ix2 Int32
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Maybe (Array DL Ix2 Int32) -> Array DL Ix2 Int32
forall a. HasCallStack => Maybe a -> a
fromJust
    (Maybe (Array DL Ix2 Int32) -> Array DL Ix2 Int32)
-> (Vector r1 Ix4 -> Maybe (Array DL Ix2 Int32))
-> Vector r1 Ix4
-> Array DL Ix2 Int32
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Array D Int (Array S Int Int32) -> Maybe (Array DL Ix2 Int32)
Array D Int (Array S (Lower Ix2) Int32)
-> Maybe (Array DL Ix2 Int32)
forall r ix e (f :: * -> *) (m :: * -> *).
(Foldable f, MonadThrow m, Index (Lower ix), Source r e,
 Index ix) =>
f (Array r (Lower ix) e) -> m (Array DL ix e)
stackOuterSlicesM
    (Array D Int (Array S Int Int32) -> Maybe (Array DL Ix2 Int32))
-> (Vector r1 Ix4 -> Array D Int (Array S Int Int32))
-> Vector r1 Ix4
-> Maybe (Array DL Ix2 Int32)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Ix4 -> Array S Int Int32)
-> Vector r1 Ix4 -> Array D Int (Array S Int Int32)
forall ix r e' e.
(Index ix, Source r e') =>
(e' -> e) -> Array r ix e' -> Array D ix e
Massiv.map Ix4 -> Array S Int Int32
ix4ToRow
    (Vector r1 Ix4 -> Array r2 Ix2 Int32)
-> Vector r1 Ix4 -> Array r2 Ix2 Int32
forall a b. (a -> b) -> a -> b
$ Vector r1 Ix4
ixVec
 where
  ix4ToRow :: Ix4 -> Vector S Int32
  ix4ToRow :: Ix4 -> Array S Int Int32
ix4ToRow (Ix4 Int
x Int
y Int
z Int
w) =
    Comp -> [Int32] -> Array S Int Int32
forall r e. Manifest r e => Comp -> [e] -> Vector r e
fromList
      Comp
Seq
      [ Int -> Int32
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
x
      , Int -> Int32
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
y
      , Int -> Int32
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
z
      , Int -> Int32
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
w
      ]

-- | Convert a vector of 'Ix6' to a \( m \times 6 \) matrix.
castCoords6D :: (Manifest r1 (IxN 6), Manifest r2 Int32) => Vector r1 (IxN 6) -> Matrix r2 Int32
castCoords6D :: forall r1 r2.
(Manifest r1 (IxN 6), Manifest r2 Int32) =>
Vector r1 (IxN 6) -> Matrix r2 Int32
castCoords6D Vector r1 (IxN 6)
ixVec =
  Array DL Ix2 Int32 -> Array r2 Ix2 Int32
forall r ix e r'.
(Manifest r e, Load r' ix e) =>
Array r' ix e -> Array r ix e
compute
    (Array DL Ix2 Int32 -> Array r2 Ix2 Int32)
-> (Vector r1 (IxN 6) -> Array DL Ix2 Int32)
-> Vector r1 (IxN 6)
-> Array r2 Ix2 Int32
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Maybe (Array DL Ix2 Int32) -> Array DL Ix2 Int32
forall a. HasCallStack => Maybe a -> a
fromJust
    (Maybe (Array DL Ix2 Int32) -> Array DL Ix2 Int32)
-> (Vector r1 (IxN 6) -> Maybe (Array DL Ix2 Int32))
-> Vector r1 (IxN 6)
-> Array DL Ix2 Int32
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Array D Int (Array S Int Int32) -> Maybe (Array DL Ix2 Int32)
Array D Int (Array S (Lower Ix2) Int32)
-> Maybe (Array DL Ix2 Int32)
forall r ix e (f :: * -> *) (m :: * -> *).
(Foldable f, MonadThrow m, Index (Lower ix), Source r e,
 Index ix) =>
f (Array r (Lower ix) e) -> m (Array DL ix e)
stackOuterSlicesM
    (Array D Int (Array S Int Int32) -> Maybe (Array DL Ix2 Int32))
-> (Vector r1 (IxN 6) -> Array D Int (Array S Int Int32))
-> Vector r1 (IxN 6)
-> Maybe (Array DL Ix2 Int32)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (IxN 6 -> Array S Int Int32)
-> Vector r1 (IxN 6) -> Array D Int (Array S Int Int32)
forall ix r e' e.
(Index ix, Source r e') =>
(e' -> e) -> Array r ix e' -> Array D ix e
Massiv.map IxN 6 -> Array S Int Int32
ix6ToRow
    (Vector r1 (IxN 6) -> Array r2 Ix2 Int32)
-> Vector r1 (IxN 6) -> Array r2 Ix2 Int32
forall a b. (a -> b) -> a -> b
$ Vector r1 (IxN 6)
ixVec
 where
  ix6ToRow :: IxN 6 -> Vector S Int32
  ix6ToRow :: IxN 6 -> Array S Int Int32
ix6ToRow (Int
x :> Int
y :> Int
z :> Int
w :> Int
u :. Int
v) =
    Comp -> [Int32] -> Array S Int Int32
forall r e. Manifest r e => Comp -> [e] -> Vector r e
fromList
      Comp
Seq
      [ Int -> Int32
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
x
      , Int -> Int32
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
y
      , Int -> Int32
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
z
      , Int -> Int32
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
w
      , Int -> Int32
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
u
      , Int -> Int32
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
v
      ]

castCoords8D :: (Manifest r1 (IxN 8), Manifest r2 Int32) => Vector r1 (IxN 8) -> Matrix r2 Int32
castCoords8D :: forall r1 r2.
(Manifest r1 (IxN 8), Manifest r2 Int32) =>
Vector r1 (IxN 8) -> Matrix r2 Int32
castCoords8D Vector r1 (IxN 8)
ixVec =
  Array DL Ix2 Int32 -> Array r2 Ix2 Int32
forall r ix e r'.
(Manifest r e, Load r' ix e) =>
Array r' ix e -> Array r ix e
compute
    (Array DL Ix2 Int32 -> Array r2 Ix2 Int32)
-> (Vector r1 (IxN 8) -> Array DL Ix2 Int32)
-> Vector r1 (IxN 8)
-> Array r2 Ix2 Int32
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Maybe (Array DL Ix2 Int32) -> Array DL Ix2 Int32
forall a. HasCallStack => Maybe a -> a
fromJust
    (Maybe (Array DL Ix2 Int32) -> Array DL Ix2 Int32)
-> (Vector r1 (IxN 8) -> Maybe (Array DL Ix2 Int32))
-> Vector r1 (IxN 8)
-> Array DL Ix2 Int32
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Array D Int (Array S Int Int32) -> Maybe (Array DL Ix2 Int32)
Array D Int (Array S (Lower Ix2) Int32)
-> Maybe (Array DL Ix2 Int32)
forall r ix e (f :: * -> *) (m :: * -> *).
(Foldable f, MonadThrow m, Index (Lower ix), Source r e,
 Index ix) =>
f (Array r (Lower ix) e) -> m (Array DL ix e)
stackOuterSlicesM
    (Array D Int (Array S Int Int32) -> Maybe (Array DL Ix2 Int32))
-> (Vector r1 (IxN 8) -> Array D Int (Array S Int Int32))
-> Vector r1 (IxN 8)
-> Maybe (Array DL Ix2 Int32)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (IxN 8 -> Array S Int Int32)
-> Vector r1 (IxN 8) -> Array D Int (Array S Int Int32)
forall ix r e' e.
(Index ix, Source r e') =>
(e' -> e) -> Array r ix e' -> Array D ix e
Massiv.map IxN 8 -> Array S Int Int32
ix8ToRow
    (Vector r1 (IxN 8) -> Array r2 Ix2 Int32)
-> Vector r1 (IxN 8) -> Array r2 Ix2 Int32
forall a b. (a -> b) -> a -> b
$ Vector r1 (IxN 8)
ixVec
 where
  ix8ToRow :: IxN 8 -> Vector S Int32
  ix8ToRow :: IxN 8 -> Array S Int Int32
ix8ToRow (Int
x :> Int
y :> Int
z :> Int
w :> Int
u :> Int
v :> Int
t :. Int
s) =
    Comp -> [Int32] -> Array S Int Int32
forall r e. Manifest r e => Comp -> [e] -> Vector r e
fromList
      Comp
Seq
      [ Int -> Int32
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
x
      , Int -> Int32
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
y
      , Int -> Int32
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
z
      , Int -> Int32
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
w
      , Int -> Int32
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
u
      , Int -> Int32
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
v
      , Int -> Int32
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
t
      , Int -> Int32
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
s
      ]