amcphail / hmatrix-gsl-stats

GSL Statistics functions for Haskell hmatrix
BSD 3-Clause "New" or "Revised" License
6 stars 3 forks source link

Handling transposed matrices in FFI calls #3

Closed acowley closed 10 years ago

acowley commented 10 years ago

See the question and answer on SO.

amcphail commented 10 years ago

I'm referring this to the author of hmatrix

import Numeric.LinearAlgebra
import Numeric.GSL.Fitting.Linear

import Data.Packed.Development

test1 :: IO ()
test1 = do
  let y = fromList [1,2,3,4,5]
  let x = trans $ fromLists [[1,2,3,4,5], [1,1,1,1,1]]
  let x3 = fromLists [[1,1], [2,1], [3,1], [4,1], [5,1]]
  print $ x == x3
  print $ multifit x y
  print $ multifit x3 y

test2 :: IO ()
test2 = do
  let y = fromList [1,2,3,4,5]
  let x = cmat $ trans $ fromLists [[1,2,3,4,5], [1,1,1,1,1]]
  let x3 = fromLists [[1,1], [2,1], [3,1], [4,1], [5,1]]
  print $ x == x3
  print $ multifit x y
  print $ multifit x3 y

main :: IO ()
main = do
  test1
  putStrLn ""
  test2
amcphail commented 10 years ago

Comment from aruiz https://github.com/albertoruiz/hmatrix/issues/53

"trans" only flips the row major / column major order field, but it does not actually transpose the data:

https://github.com/albertoruiz/hmatrix/blob/master/lib/Data/Packed/Internal/Matrix.hs#L97

This is to avoid some unnecessary transpositions (e.g blas matrix multiply admits transposed arguments):

https://github.com/albertoruiz/hmatrix/blob/master/lib/Numeric/LinearAlgebra/LAPACK.hs#L61

But most wrappers need a fixed order, for instance:

https://github.com/albertoruiz/hmatrix/blob/master/lib/Numeric/LinearAlgebra/LAPACK.hs#L151

cmat and fmat are provided in Data.Packed.Development to force the required order in FFI calls. Here we have some examples:

https://github.com/albertoruiz/hmatrix/tree/master/examples/devel

acowley commented 10 years ago

Surely the requirement to put an argument in a fixed order should be the responsibility of the wrapper?

amcphail commented 10 years ago

Good point. Updated to hmatrix-gsl-stats 0.2