[racket] math/matrix <--> FFI/array

From: Neil Toronto (neil.toronto at gmail.com)
Date: Mon Jan 21 14:42:51 EST 2013

On 01/21/2013 02:48 AM, Pierpaolo Bernardi wrote:
> Hello,
>
> what's the most convenient way to convert between math/matrices and
> FFI/arrays?
>
> To reduce the scope of the question, at the moment I'm only interested
> in conversions from and to the following FFI types:
>
>   (_array _double 3 3)
>   (_array _double 3)
>   (_array _double 2 3)

 From the FFI docs, it looks like you need to construct either:

  1. A vector of vectors for foreign functions that accept an
     (_array/vector type m n).

  2. A flat vector with elements in row-major order for foreign functions
     that accept an (_array type m n) or (_array type m).

For #1, use `matrix->vector*', and for #2, use `matrix->vector'. I don't 
know whether you can pass the vectors returned from these functions 
directly to foreign functions. I wouldn't be surprised.

If you're working in Typed Racket and you need this to be fast, do the 
conversions to and from vectors in Typed Racket. Currently, passing 
matrices across the contract boundary makes element access very slow. :/

Neil ⊥


Posted on the users mailing list.