From 2ebf15f4085f633fc2f22c05684391aa9d1c4cd9 Mon Sep 17 00:00:00 2001 From: Tom Smeding Date: Thu, 16 May 2024 21:52:58 +0200 Subject: Convert arrays <-> XArray --- src/Data/Array/Nested.hs | 6 ++++++ src/Data/Array/Nested/Internal.hs | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) (limited to 'src/Data') diff --git a/src/Data/Array/Nested.hs b/src/Data/Array/Nested.hs index f451920..4b455da 100644 --- a/src/Data/Array/Nested.hs +++ b/src/Data/Array/Nested.hs @@ -12,6 +12,8 @@ module Data.Array.Nested ( rslice, rrev1, rreshape, -- ** Lifting orthotope operations to 'Ranked' arrays rlift, + -- ** Conversions + rasXArrayPrim, rfromXArrayPrim, -- * Shaped arrays Shaped, @@ -24,6 +26,8 @@ module Data.Array.Nested ( sslice, srev1, sreshape, -- ** Lifting orthotope operations to 'Shaped' arrays slift, + -- ** Conversions + sasXArrayPrim, sfromXArrayPrim, -- * Mixed arrays Mixed, @@ -31,6 +35,8 @@ module Data.Array.Nested ( KnownShapeX(..), StaticShX(..), mgenerate, mtranspose, mappend, mfromVector, mtoVector, munScalar, mconstant, mfromList, mtoList, mslice, mrev1, mreshape, + -- ** Conversions + masXArrayPrim, mfromXArrayPrim, -- * Array elements Elt(mshape, mindex, mindexPartial, mscalar, mfromList1, mtoList1, mlift, mlift2), diff --git a/src/Data/Array/Nested/Internal.hs b/src/Data/Array/Nested/Internal.hs index de27336..65c5419 100644 --- a/src/Data/Array/Nested/Internal.hs +++ b/src/Data/Array/Nested/Internal.hs @@ -605,6 +605,18 @@ mreshape :: forall sh sh' a. (KnownShapeX sh, KnownShapeX sh', Elt a) mreshape sh' = mlift $ \(_ :: Proxy shIn) -> X.reshapePartial (knownShapeX @sh) (knownShapeX @shIn) sh' +masXArrayPrimP :: Mixed sh (Primitive a) -> XArray sh a +masXArrayPrimP (M_Primitive arr) = arr + +masXArrayPrim :: PrimElt a => Mixed sh a -> XArray sh a +masXArrayPrim = masXArrayPrimP . toPrimitive + +mfromXArrayPrimP :: XArray sh a -> Mixed sh (Primitive a) +mfromXArrayPrimP = M_Primitive + +mfromXArrayPrim :: PrimElt a => XArray sh a -> Mixed sh a +mfromXArrayPrim = fromPrimitive . mfromXArrayPrimP + mliftPrim :: (KnownShapeX sh, Storable a) => (a -> a) -> Mixed sh (Primitive a) -> Mixed sh (Primitive a) @@ -1161,6 +1173,18 @@ rreshape sh' (Ranked arr) , Dict <- lemKnownReplicate (Proxy @n') = Ranked (mreshape (shCvtRX sh') arr) +rasXArrayPrimP :: Ranked n (Primitive a) -> XArray (Replicate n Nothing) a +rasXArrayPrimP (Ranked arr) = masXArrayPrimP arr + +rasXArrayPrim :: PrimElt a => Ranked n a -> XArray (Replicate n Nothing) a +rasXArrayPrim (Ranked arr) = masXArrayPrim arr + +rfromXArrayPrimP :: XArray (Replicate n Nothing) a -> Ranked n (Primitive a) +rfromXArrayPrimP = Ranked . mfromXArrayPrimP + +rfromXArrayPrim :: PrimElt a => XArray (Replicate n Nothing) a -> Ranked n a +rfromXArrayPrim = Ranked . mfromXArrayPrim + -- ====== API OF SHAPED ARRAYS ====== -- @@ -1424,3 +1448,15 @@ sreshape sh' (Shaped arr) | Dict <- lemKnownMapJust (Proxy @sh) , Dict <- lemKnownMapJust (Proxy @sh') = Shaped (mreshape (shCvtSX sh') arr) + +sasXArrayPrimP :: Shaped sh (Primitive a) -> XArray (MapJust sh) a +sasXArrayPrimP (Shaped arr) = masXArrayPrimP arr + +sasXArrayPrim :: PrimElt a => Shaped sh a -> XArray (MapJust sh) a +sasXArrayPrim (Shaped arr) = masXArrayPrim arr + +sfromXArrayPrimP :: XArray (MapJust sh) a -> Shaped sh (Primitive a) +sfromXArrayPrimP = Shaped . mfromXArrayPrimP + +sfromXArrayPrim :: PrimElt a => XArray (MapJust sh) a -> Shaped sh a +sfromXArrayPrim = Shaped . mfromXArrayPrim -- cgit v1.2.3-70-g09d2