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/Internal.hs | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) (limited to 'src/Data/Array/Nested/Internal.hs') 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