haskell-numerics / hmatrix

Linear algebra and numerical computation
381 stars 104 forks source link

Diagonal matrices -- show error? #177

Closed sid-kap closed 6 years ago

sid-kap commented 8 years ago

I think there might be an error with the Show instance for diagonal matrices, at least for Static matrices. Here is an example in ghci:

Prelude Numeric.LinearAlgebra.Static> eye :: L 5 5
(diag 0.0 1.0,1.0,1.0] :: L 5 5)
Prelude Numeric.LinearAlgebra.Static> eye :: L 1 1
(1.0 :: L 1 1)
Prelude Numeric.LinearAlgebra.Static> eye :: L 2 2
(diag 0.0  :: L 2 2)

Note that the 1x1 instance displays correctly, but the 2x2 and 5x5 instances do not.

Can anyone else reproduce this?

albertoruiz commented 8 years ago

The Show instance of the diagonal matrix shows the internal, compact representation, including the default value and the diagonal elements. We can show the expanded matrix using disp (the argument is the number of desired decimal places, but if all elements look like integers the decimals are not shown).

λ> eye :: L 0 0
(matrix
 [] :: L 0 0)

λ> eye :: L 1 1
(1.0 :: L 1 1)
λ> disp 3 (eye :: L 1 1)
L 1 1
1

λ> eye :: L 2 2
(diag 0.0 [1.0,1.0] :: L 2 2)
λ> disp 3 (eye :: L 2 2)
L 2 2
1  0
0  1

λ> eye :: L 3 3
(diag 0.0 [1.0,1.0,1.0] :: L 3 3)
λ> disp 3 (eye :: L 3 3)
L 3 3
1  0  0
0  1  0
0  0  1

λ> eye :: Sq 7
(diag 0.0 [1.0,1.0,1.0,1.0,1.0,1.0,1.0] :: L 7 7)
λ> disp 3 (eye :: Sq 7)
L 7 7
1  0  0  0  0  0  0
0  1  0  0  0  0  0
0  0  1  0  0  0  0
0  0  0  1  0  0  0
0  0  0  0  1  0  0
0  0  0  0  0  1  0
0  0  0  0  0  0  1

But it seems that some elements and square brackets are missing in your results. Are you using iHaskell or something similar?

sid-kap commented 8 years ago

I'm using ghci, by the command stack ghci, using Stackage lts-5.2 on GHC 7.10.3. This error also happens to me in compiled programs that I build with stack (also using lts-5.2 and GHC 7.10.3).

radoye commented 7 years ago

Potentially related.

*Radoye.Megamind.Interpret.Forward> y
(vector0,5.0,11.0,19.0] :: R 5)
*Radoye.Megamind.Interpret.Forward> LAS.disp 3 y
R 5
-1  1  5  11  19
*Radoye.Megamind.Interpret.Forward> 

In my stack.yaml I have:

resolver: lts-7.13

packages:
- '.'

extra-deps:
- hmatrix-0.18.0.0

Seems like Show instances have some magic numbers hard coded. Did the underlying printer change?

idontgetoutmuch commented 6 years ago

Fixed by https://github.com/haskell-numerics/hmatrix/pull/270