From f84c5b0cab7e819cdaae8288a06641973cf83437 Mon Sep 17 00:00:00 2001 From: Tom Smeding Date: Mon, 24 Feb 2025 11:04:50 +0100 Subject: WIP array AD --- src/Numeric/ADDual/VectorOps.hs | 60 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 src/Numeric/ADDual/VectorOps.hs (limited to 'src/Numeric/ADDual/VectorOps.hs') diff --git a/src/Numeric/ADDual/VectorOps.hs b/src/Numeric/ADDual/VectorOps.hs new file mode 100644 index 0000000..38063e0 --- /dev/null +++ b/src/Numeric/ADDual/VectorOps.hs @@ -0,0 +1,60 @@ +{-# LANGUAGE TypeFamilies #-} +module Numeric.ADDual.VectorOps where + +import Data.Kind (Type) +import qualified Data.Vector as V +import qualified Data.Vector.Strict as VSr +import qualified Data.Vector.Storable as VS +import qualified Data.Vector.Unboxed as VU +import Foreign.Storable (Storable) + + +class VectorOps v where + type VectorOpsScalar v :: Type + vfromListN :: Int -> [VectorOpsScalar v] -> v + vfromList :: [VectorOpsScalar v] -> v + vtoList :: v -> [VectorOpsScalar v] + vreplicate :: Int -> VectorOpsScalar v -> v + +class VectorOpsNum v where + vsum :: v -> VectorOpsScalar v + +instance VectorOps (V.Vector a) where + type VectorOpsScalar (V.Vector a) = a + vfromListN = V.fromListN + vfromList = V.fromList + vtoList = V.toList + vreplicate = V.replicate + +instance Num a => VectorOpsNum (V.Vector a) where + vsum = V.sum + +instance VectorOps (VSr.Vector a) where + type VectorOpsScalar (VSr.Vector a) = a + vfromListN = VSr.fromListN + vfromList = VSr.fromList + vtoList = VSr.toList + vreplicate = VSr.replicate + +instance Num a => VectorOpsNum (VSr.Vector a) where + vsum = VSr.sum + +instance Storable a => VectorOps (VS.Vector a) where + type VectorOpsScalar (VS.Vector a) = a + vfromListN = VS.fromListN + vfromList = VS.fromList + vtoList = VS.toList + vreplicate = VS.replicate + +instance (Storable a, Num a) => VectorOpsNum (VS.Vector a) where + vsum = VS.sum + +instance VU.Unbox a => VectorOps (VU.Vector a) where + type VectorOpsScalar (VU.Vector a) = a + vfromListN = VU.fromListN + vfromList = VU.fromList + vtoList = VU.toList + vreplicate = VU.replicate + +instance (VU.Unbox a, Num a) => VectorOpsNum (VU.Vector a) where + vsum = VU.sum -- cgit v1.2.3-70-g09d2