aboutsummaryrefslogtreecommitdiff
path: root/src/Numeric/ADDual/VectorOps.hs
diff options
context:
space:
mode:
authorTom Smeding <tom@tomsmeding.com>2025-02-24 11:04:50 +0100
committerTom Smeding <tom@tomsmeding.com>2025-02-24 11:04:50 +0100
commitf84c5b0cab7e819cdaae8288a06641973cf83437 (patch)
tree4938a1777eb10901b2fbdb28b7e2049d0c3556ca /src/Numeric/ADDual/VectorOps.hs
parenta16185618aa6f483f587f8a0c65031fc479afac7 (diff)
WIP array AD
Diffstat (limited to 'src/Numeric/ADDual/VectorOps.hs')
-rw-r--r--src/Numeric/ADDual/VectorOps.hs60
1 files changed, 60 insertions, 0 deletions
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