summaryrefslogtreecommitdiff
path: root/examples/devel/ej1/wrappers.hs
diff options
context:
space:
mode:
authorAlberto Ruiz <aruiz@um.es>2010-02-20 17:08:59 +0000
committerAlberto Ruiz <aruiz@um.es>2010-02-20 17:08:59 +0000
commitdedd74ee85ee1bf468552107760541b31e6f1878 (patch)
tree2726f637f48498eef1af22ce03741a3353d8ec08 /examples/devel/ej1/wrappers.hs
parentdc9cdee87db0181774cb230610e4b24f9ef3f89a (diff)
added simpler devel example
Diffstat (limited to 'examples/devel/ej1/wrappers.hs')
-rw-r--r--examples/devel/ej1/wrappers.hs44
1 files changed, 44 insertions, 0 deletions
diff --git a/examples/devel/ej1/wrappers.hs b/examples/devel/ej1/wrappers.hs
new file mode 100644
index 0000000..b329464
--- /dev/null
+++ b/examples/devel/ej1/wrappers.hs
@@ -0,0 +1,44 @@
1{-# LANGUAGE ForeignFunctionInterface #-}
2
3-- $ ghc -O2 --make wrappers.hs functions.c
4
5import Numeric.LinearAlgebra
6import Data.Packed.Development
7import Foreign(Ptr,unsafePerformIO)
8import Foreign.C.Types(CInt)
9
10-----------------------------------------------------
11
12main = do
13 print $ myScale 3.0 (fromList [1..10])
14 print $ myDiag $ (3><5) [1..]
15
16-----------------------------------------------------
17
18foreign import ccall "c_scale_vector"
19 cScaleVector :: Double -- scale
20 -> CInt -> Ptr Double -- argument
21 -> CInt -> Ptr Double -- result
22 -> IO CInt -- exit code
23
24myScale s x = unsafePerformIO $ do
25 y <- createVector (dim x)
26 app2 (cScaleVector s) vec x vec y "cScaleVector"
27 return y
28
29-----------------------------------------------------
30-- forcing row order
31
32foreign import ccall "c_diag"
33 cDiag :: CInt -> CInt -> Ptr Double -- argument
34 -> CInt -> Ptr Double -- result1
35 -> CInt -> CInt -> Ptr Double -- result2
36 -> IO CInt -- exit code
37
38myDiag m = unsafePerformIO $ do
39 y <- createVector (min r c)
40 z <- createMatrix RowMajor r c
41 app3 cDiag mat (cmat m) vec y mat z "cDiag"
42 return (y,z)
43 where r = rows m
44 c = cols m