summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--examples/vector.hs29
-rw-r--r--hmatrix.cabal3
-rw-r--r--lib/Data/Packed/Development.hs5
-rw-r--r--lib/Data/Packed/Internal/Vector.hs12
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
8import Data.Packed as H
9import Data.Packed.Development(unsafeFromForeignPtr, unsafeToForeignPtr)
10import Foreign.Storable
11import qualified Data.Vector.Storable as V
12
13fromVector :: Storable t => V.Vector t -> H.Vector t
14fromVector v = unsafeFromForeignPtr p i n where
15 (p,i,n) = V.unsafeToForeignPtr (V.copy v)
16
17toVector :: H.Vector t -> V.Vector t
18toVector v = V.unsafeFromForeignPtr p i n where
19 (p,i,n) = unsafeToForeignPtr v
20
21-------------------------------------------
22
23v = V.slice 5 10 (V.fromList [1 .. 10::Double] V.++ V.replicate 10 7)
24
25w = linspace 5 (0,2)
26
27main = 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 @@
1Name: hmatrix 1Name: hmatrix
2Version: 0.8.3.2 2Version: 0.8.4.0
3License: GPL 3License: GPL
4License-file: LICENSE 4License-file: LICENSE
5Author: Alberto Ruiz 5Author: 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
46extra-source-files: lib/Numeric/LinearAlgebra/LAPACK/lapack-aux.h, 47extra-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
27import Data.Packed.Internal 28import 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
29import Data.Packed.Internal.Common 31import 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
56unsafeToForeignPtr :: Vector a -> (ForeignPtr a, Int, Int)
57unsafeToForeignPtr v = (fptr v, 0, idim v)
58
59-- | Same convention as in Roman Leshchinskiy's vector package.
60unsafeFromForeignPtr :: ForeignPtr a -> Int -> Int -> Vector a
61unsafeFromForeignPtr 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
55dim :: Vector t -> Int 65dim :: Vector t -> Int
56dim = idim 66dim = idim