FFI, Foreign.Marshal.Array, etc.

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

FFI, Foreign.Marshal.Array, etc.

Alexey Beshenov
Hi!

I'm playing with the FFI.
Could anyone help me with a practical example?

I have a function

  int solve_sys (double **a, int n, double b[]);

where a is an array of size n*n and b is an array of size n
(it contains the needed result if solve_sys returns 0).

How could I wrap it in something like

  solveSys :: [[Double]] -> [Double] -> [Double]

?

--
Setting Orange, Aftermath 38 YOLD 3174
Alexey Beshenov  http://beshenov.ru/

Reply | Threaded
Open this post in threaded view
|

FFI, Foreign.Marshal.Array, etc.

Alexey Beshenov
On Wednesday 26 November 2008 23:32:58 Alexey Beshenov wrote:

> Hi!
>
> I'm playing with the FFI.
> Could anyone help me with a practical example?
>
> I have a function
>
>   int solve_sys (double **a, int n, double b[]);
>
> where a is an array of size n*n and b is an array of size n
> (it contains the needed result if solve_sys returns 0).
>
> How could I wrap it in something like
>
>   solveSys :: [[Double]] -> [Double] -> [Double]
>
> ?

Something like this:

foreign import ccall "linear.h solve_sys" lSolve :: Ptr (Double) ->
Int -> Ptr (Double) -> IO (Int)

solveSys :: [[Double]] -> [Double] -> IO()
solveSys a b = do
                  aptr <- newArray (concat a)
                  bptr <- newArray b
                  sol <- lSolve aptr n bptr
                  x <- peekArray n bptr
                  free aptr
                  free bptr
                  print x
               where n = length a

Does the trick, but I wonder is there a way to get a function
returning [Double]...

--
Setting Orange, Aftermath 38 YOLD 3174
Alexey Beshenov  http://beshenov.ru/

Reply | Threaded
Open this post in threaded view
|

FFI, Foreign.Marshal.Array, etc.

Alexey Beshenov
On Thursday 27 November 2008 01:16:17 Alexey Beshenov wrote:

> Something like this:
>
> foreign import ccall "linear.h solve_sys" lSolve :: Ptr (Double) ->
> Int -> Ptr (Double) -> IO (Int)
>
> solveSys :: [[Double]] -> [Double] -> IO()
> solveSys a b = do
>                   aptr <- newArray (concat a)
>                   bptr <- newArray b
>                   sol <- lSolve aptr n bptr
>                   x <- peekArray n bptr
>                   free aptr
>                   free bptr
>                   print x
>                where n = length a
>
> Does the trick, but I wonder is there a way to get a function
> returning [Double]...

Eh, I have to do unsafePerformIO.

Sorry about that, I'm quite new to Haskell :-I

--
Setting Orange, Aftermath 38 YOLD 3174
Alexey Beshenov  http://beshenov.ru/