matrix-sized: Haskell matrix library with interface to C++ linear algebra libraries.

[ bsd3, library, math ] [ Propose Tags ]

A Haskell implementation of matrices with statically known sizes. The library also comes with the bindings to high performance C++ linear algebra libraries such as Eigen and Spectra.

[Skip to Readme]
Versions [RSS] [faq] 0.0.1, 0.0.2, 0.0.3, 0.0.4, 0.1.0, 0.1.1
Change log
Dependencies base (>=4.10 && <5), bytestring, bytestring-lexing, conduit, double-conversion, primitive (>=, singletons, store, vector (>=0.11) [details]
License BSD-3-Clause
Copyright (c) 2020 Kai Zhang
Author Kai Zhang
Category Math
Home page
Bug tracker
Source repo head: git clone
Uploaded by kaizhang at 2020-06-22T14:29:56Z
Distributions NixOS:0.1.1
Downloads 802 total (13 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Hackage Matrix CI
Docs uploaded by user
Build status unknown [no reports yet]



Manual Flags


Enable multithreading

Automatic Flags

Use -f <flag> to enable a flag, or -f -<flag> to disable that flag. More info


Maintainer's Corner

For package maintainers and hackage trustees


Readme for matrix-sized-0.1.1

[back to package description]

Type-safe linear algebra library

  • General matrix types are implemented in native Haskell.

  • The dimensions of matrices are statically typed.

  • Provides bindings to high performance C++ linear algebra libraries such Eigen and Spectra.

Following GHC extensions may be needed:

  • ScopedTypeVariables
  • RankNTypes
  • TypeFamilies
  • DataKinds


{-# LANGUAGE DataKinds #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE Rank2Types #-}
{-# LANGUAGE TypeOperators #-}

import Data.Matrix.Static.LinearAlgebra
import qualified Data.Matrix.Static.Generic as G
import qualified Data.Matrix.Static.Dense as D
import qualified Data.Matrix.Static.Sparse as S
import Data.Singletons.Prelude hiding ((@@))
import Data.Singletons.TypeLits
import Data.Complex
import System.Random
import Control.Monad
import Data.Type.Equality

f :: (SingI n, (2 <= n - 2) ~ 'True)
  => Matrix n n Double -> Matrix n n (Complex Double)
f m = let (d, v) = eigS (sing :: Sing 2) m
      in v @@ S.diag d @@ G.transpose v

main :: IO ()
main = do
    n <- randomRIO (2, 6)
    vals <- replicateM (n*n) $ randomRIO (-100,100) :: IO [Double]

    withSomeSing (fromIntegral n) $ \sn@(SNat :: Sing n) ->
        let s0 = SNat :: Sing 2
            sn2 = sn %- s0
        in case testEquality (s0 %<= sn2) STrue of
            Just Refl -> do
                let mat = G.fromList vals :: Matrix n n Double
                print $ f mat
            Nothing -> error $ "Requiring Matrix size >= 4, but got: " <> show n