summaryrefslogtreecommitdiff
path: root/lib/Data
diff options
context:
space:
mode:
Diffstat (limited to 'lib/Data')
-rw-r--r--lib/Data/Packed/Internal/Vector.hs6
-rw-r--r--lib/Data/Packed/Vector.hs6
2 files changed, 8 insertions, 4 deletions
diff --git a/lib/Data/Packed/Internal/Vector.hs b/lib/Data/Packed/Internal/Vector.hs
index 76bd4d1..082e09d 100644
--- a/lib/Data/Packed/Internal/Vector.hs
+++ b/lib/Data/Packed/Internal/Vector.hs
@@ -80,6 +80,8 @@ fromList l = unsafePerformIO $ do
80 f // vec v // check "fromList" [] 80 f // vec v // check "fromList" []
81 return v 81 return v
82 82
83safeRead v f = unsafePerformIO $ withForeignPtr (fptr v) $ const $ f (ptr v)
84
83{- | extracts the Vector elements to a list 85{- | extracts the Vector elements to a list
84 86
85@> toList (linspace 5 (1,10)) 87@> toList (linspace 5 (1,10))
@@ -87,7 +89,7 @@ fromList l = unsafePerformIO $ do
87 89
88-} 90-}
89toList :: Storable a => Vector a -> [a] 91toList :: Storable a => Vector a -> [a]
90toList v = unsafePerformIO $ peekArray (dim v) (ptr v) 92toList v = safeRead v $ peekArray (dim v)
91 93
92-- | an alternative to 'fromList' with explicit dimension, used also in the instances for Show (Vector a). 94-- | an alternative to 'fromList' with explicit dimension, used also in the instances for Show (Vector a).
93(|>) :: (Storable a) => Int -> [a] -> Vector a 95(|>) :: (Storable a) => Int -> [a] -> Vector a
@@ -96,7 +98,7 @@ n |> l = if length l == n then fromList l else error "|> with wrong size"
96 98
97-- | access to Vector elements without range checking 99-- | access to Vector elements without range checking
98at' :: Storable a => Vector a -> Int -> a 100at' :: Storable a => Vector a -> Int -> a
99at' v n = unsafePerformIO $ peekElemOff (ptr v) n 101at' v n = safeRead v $ flip peekElemOff n
100 102
101-- | access to Vector elements with range checking. 103-- | access to Vector elements with range checking.
102at :: Storable a => Vector a -> Int -> a 104at :: Storable a => Vector a -> Int -> a
diff --git a/lib/Data/Packed/Vector.hs b/lib/Data/Packed/Vector.hs
index 4c331ef..60fa7c1 100644
--- a/lib/Data/Packed/Vector.hs
+++ b/lib/Data/Packed/Vector.hs
@@ -32,8 +32,10 @@ import Numeric.GSL.Vector
325 |> [-3.0,-0.5,2.0,4.5,7.0]@ 325 |> [-3.0,-0.5,2.0,4.5,7.0]@
33-} 33-}
34linspace :: Int -> (Double, Double) -> Vector Double 34linspace :: Int -> (Double, Double) -> Vector Double
35linspace n (a,b) = fromList [a, a+delta .. b] 35linspace n (a,b) = add a $ scale s $ fromList [0 .. fromIntegral n-1]
36 where delta = (b-a)/(fromIntegral n -1) 36 where scale = vectorMapValR Scale
37 add = vectorMapValR AddConstant
38 s = (b-a)/fromIntegral (n-1)
37 39
38vectorMax :: Vector Double -> Double 40vectorMax :: Vector Double -> Double
39vectorMax = toScalarR Max 41vectorMax = toScalarR Max