haskell-numerics / hmatrix

Linear algebra and numerical computation
381 stars 104 forks source link

Numeric.LinearAlgebra.Static: incorrect representation of compact diagonal matrices #291

Open mistransky opened 5 years ago

mistransky commented 5 years ago

Compact diagonal matrices are probably incorrectly represented. The following code demonstrates it by creating a 2x2 identity matrix, and tries to calculate its determinant, which should be 1.0. The code throws an exception that the input matrix is (1x3), non-square.

{-# Language DataKinds #-}

import Numeric.LinearAlgebra.Static

main :: IO ()
main = do
  let m = diag $ vec2 1 1
  print $ det m

produces output:

det of nonsquare (1x3) matrix CallStack (from HasCallStack): error, called at src/Internal/Algorithms.hs:664:21 in hmatrix-0.19.0.0-948a7582e6e481b024f5d19f7e12faf0d1021543381941caf40c72920d1a8233:Internal.Algorithms

GHC: 8.6.3, 8.6.4 hmatrix: 0.19.0.0 tried on Linux x86_64: Debian 9, Fedora 29

mistransky commented 5 years ago

Looking through the source code, I take back that type-level sized compact diagonal matrices are incorrectly represented. It looks like that functions like det are simply not yet implemented for such representation:

detL :: KnownNat n => Sq n -> ℝ
detL = LA.det . unwrap

What gets unwrapped is unfortunately a row vector with fill element in the first position, and the non-sized version of det is applied leading to an exception. There are probably more unimplemented functions like det for this representation. I take it, that the Static module is still very experimental..