diff options
-rw-r--r-- | examples/vector.hs | 29 | ||||
-rw-r--r-- | hmatrix.cabal | 3 | ||||
-rw-r--r-- | lib/Data/Packed/Development.hs | 5 | ||||
-rw-r--r-- | lib/Data/Packed/Internal/Vector.hs | 12 |
4 files changed, 45 insertions, 4 deletions
diff --git a/examples/vector.hs b/examples/vector.hs new file mode 100644 index 0000000..12cbc42 --- /dev/null +++ b/examples/vector.hs | |||
@@ -0,0 +1,29 @@ | |||
1 | -- conversion to/from Data.Vector.Storable | ||
2 | -- from Roman Leshchinskiy "vector" package | ||
3 | -- | ||
4 | -- In the future Data.Packed.Vector will be replaced by Data.Vector.Storable | ||
5 | |||
6 | ------------------------------------------- | ||
7 | |||
8 | import Data.Packed as H | ||
9 | import Data.Packed.Development(unsafeFromForeignPtr, unsafeToForeignPtr) | ||
10 | import Foreign.Storable | ||
11 | import qualified Data.Vector.Storable as V | ||
12 | |||
13 | fromVector :: Storable t => V.Vector t -> H.Vector t | ||
14 | fromVector v = unsafeFromForeignPtr p i n where | ||
15 | (p,i,n) = V.unsafeToForeignPtr (V.copy v) | ||
16 | |||
17 | toVector :: H.Vector t -> V.Vector t | ||
18 | toVector v = V.unsafeFromForeignPtr p i n where | ||
19 | (p,i,n) = unsafeToForeignPtr v | ||
20 | |||
21 | ------------------------------------------- | ||
22 | |||
23 | v = V.slice 5 10 (V.fromList [1 .. 10::Double] V.++ V.replicate 10 7) | ||
24 | |||
25 | w = linspace 5 (0,2) | ||
26 | |||
27 | main = do | ||
28 | print $ fromVector v | ||
29 | print $ toVector w | ||
diff --git a/hmatrix.cabal b/hmatrix.cabal index b99c109..290515e 100644 --- a/hmatrix.cabal +++ b/hmatrix.cabal | |||
@@ -1,5 +1,5 @@ | |||
1 | Name: hmatrix | 1 | Name: hmatrix |
2 | Version: 0.8.3.2 | 2 | Version: 0.8.4.0 |
3 | License: GPL | 3 | License: GPL |
4 | License-file: LICENSE | 4 | License-file: LICENSE |
5 | Author: Alberto Ruiz | 5 | Author: Alberto Ruiz |
@@ -42,6 +42,7 @@ extra-source-files: examples/tests.hs | |||
42 | examples/devel/ej2/wrappers.hs | 42 | examples/devel/ej2/wrappers.hs |
43 | examples/devel/ej2/functions.c | 43 | examples/devel/ej2/functions.c |
44 | examples/Real.hs | 44 | examples/Real.hs |
45 | examples/vector.hs | ||
45 | 46 | ||
46 | extra-source-files: lib/Numeric/LinearAlgebra/LAPACK/lapack-aux.h, | 47 | extra-source-files: lib/Numeric/LinearAlgebra/LAPACK/lapack-aux.h, |
47 | lib/Numeric/LinearAlgebra/LAPACK/clapack.h | 48 | lib/Numeric/LinearAlgebra/LAPACK/clapack.h |
diff --git a/lib/Data/Packed/Development.hs b/lib/Data/Packed/Development.hs index 56e7b8f..a9e6ac2 100644 --- a/lib/Data/Packed/Development.hs +++ b/lib/Data/Packed/Development.hs | |||
@@ -9,8 +9,7 @@ | |||
9 | -- Stability : provisional | 9 | -- Stability : provisional |
10 | -- Portability : portable | 10 | -- Portability : portable |
11 | -- | 11 | -- |
12 | -- The implementation of the 'Vector' and 'Matrix' types is not exposed, | 12 | -- The library can be easily extended with additional foreign functions |
13 | -- but the library can be easily extended with additional foreign functions | ||
14 | -- using the tools in this module. Illustrative usage examples can be found | 13 | -- using the tools in this module. Illustrative usage examples can be found |
15 | -- in the @examples\/devel@ folder included in the package. | 14 | -- in the @examples\/devel@ folder included in the package. |
16 | -- | 15 | -- |
@@ -22,6 +21,8 @@ module Data.Packed.Development ( | |||
22 | vec, mat, | 21 | vec, mat, |
23 | app1, app2, app3, app4, | 22 | app1, app2, app3, app4, |
24 | MatrixOrder(..), orderOf, cmat, fmat, | 23 | MatrixOrder(..), orderOf, cmat, fmat, |
24 | unsafeFromForeignPtr, | ||
25 | unsafeToForeignPtr | ||
25 | ) where | 26 | ) where |
26 | 27 | ||
27 | import Data.Packed.Internal | 28 | import Data.Packed.Internal |
diff --git a/lib/Data/Packed/Internal/Vector.hs b/lib/Data/Packed/Internal/Vector.hs index 66acd87..51c0d92 100644 --- a/lib/Data/Packed/Internal/Vector.hs +++ b/lib/Data/Packed/Internal/Vector.hs | |||
@@ -23,7 +23,9 @@ module Data.Packed.Internal.Vector ( | |||
23 | createVector, withVector, vec, | 23 | createVector, withVector, vec, |
24 | asComplex, asReal, | 24 | asComplex, asReal, |
25 | fwriteVector, freadVector, fprintfVector, fscanfVector, | 25 | fwriteVector, freadVector, fprintfVector, fscanfVector, |
26 | cloneVector | 26 | cloneVector, |
27 | unsafeToForeignPtr, | ||
28 | unsafeFromForeignPtr | ||
27 | ) where | 29 | ) where |
28 | 30 | ||
29 | import Data.Packed.Internal.Common | 31 | import Data.Packed.Internal.Common |
@@ -51,6 +53,14 @@ data Vector t = | |||
51 | , fptr :: {-# UNPACK #-} !(ForeignPtr t) -- ^ foreign pointer to the memory block | 53 | , fptr :: {-# UNPACK #-} !(ForeignPtr t) -- ^ foreign pointer to the memory block |
52 | } | 54 | } |
53 | 55 | ||
56 | unsafeToForeignPtr :: Vector a -> (ForeignPtr a, Int, Int) | ||
57 | unsafeToForeignPtr v = (fptr v, 0, idim v) | ||
58 | |||
59 | -- | Same convention as in Roman Leshchinskiy's vector package. | ||
60 | unsafeFromForeignPtr :: ForeignPtr a -> Int -> Int -> Vector a | ||
61 | unsafeFromForeignPtr fp i n | i == 0 = V {idim = n, fptr = fp} | ||
62 | | otherwise = error "unsafeFromForeignPtr with nonzero offset" | ||
63 | |||
54 | -- | Number of elements | 64 | -- | Number of elements |
55 | dim :: Vector t -> Int | 65 | dim :: Vector t -> Int |
56 | dim = idim | 66 | dim = idim |