Skip to content

Instantly share code, notes, and snippets.

@mhuesch
Last active October 1, 2017 22:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mhuesch/1fc67e6513d162625fc37c1bc89f6e39 to your computer and use it in GitHub Desktop.
Save mhuesch/1fc67e6513d162625fc37c1bc89f6e39 to your computer and use it in GitHub Desktop.
Serialize/deserialize hmatrix matrices to/from a file.

After searching unsuccessfully for how to do this, I discovered that there's a Binary instance internal to hmatrix. The following code depends on hmatrix, binary, and bytestring.

module Main where
import Data.Binary
import qualified Data.ByteString.Lazy as BS
import Numeric.LinearAlgebra
main = do
let testMatrix = matrix 3 [1..6] :: Matrix Double
bs = encode testMatrix
fp = "testMatrix.dat"
BS.writeFile fp bs
bs' <- BS.readFile fp
let testMatrix' = decode bs' :: Matrix Double
putStrLn $ "Matrices are equal? " ++ show (testMatrix == testMatrix')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment