diff options
Diffstat (limited to 'lib/Data')
-rw-r--r-- | lib/Data/Packed/Internal/Vector.hs | 6 | ||||
-rw-r--r-- | lib/Data/Packed/Vector.hs | 6 |
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 | ||
83 | safeRead 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 | -} |
89 | toList :: Storable a => Vector a -> [a] | 91 | toList :: Storable a => Vector a -> [a] |
90 | toList v = unsafePerformIO $ peekArray (dim v) (ptr v) | 92 | toList 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 |
98 | at' :: Storable a => Vector a -> Int -> a | 100 | at' :: Storable a => Vector a -> Int -> a |
99 | at' v n = unsafePerformIO $ peekElemOff (ptr v) n | 101 | at' 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. |
102 | at :: Storable a => Vector a -> Int -> a | 104 | at :: 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 | |||
32 | 5 |> [-3.0,-0.5,2.0,4.5,7.0]@ | 32 | 5 |> [-3.0,-0.5,2.0,4.5,7.0]@ |
33 | -} | 33 | -} |
34 | linspace :: Int -> (Double, Double) -> Vector Double | 34 | linspace :: Int -> (Double, Double) -> Vector Double |
35 | linspace n (a,b) = fromList [a, a+delta .. b] | 35 | linspace 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 | ||
38 | vectorMax :: Vector Double -> Double | 40 | vectorMax :: Vector Double -> Double |
39 | vectorMax = toScalarR Max | 41 | vectorMax = toScalarR Max |