Haskell's bed-and-breakfast library is the first library in the Linear algebra section in https://wiki.haskell.org/Applications_and_libraries/Mathematics. So, I'm trying:
let a = Matrix.fromList [[1,2,3], [4,5,6], [7,8,9]]
let b = Matrix.fromList [[1], [2], [3]]
a * b
*** Exception: Matrix.times: `numRows a' and `numCols b' don't match.
Wat? If I multiply [m x n] matrix by [n x p] matrix I should get [m x p] matrix, not this silly exception. Ok, maybe library author doesn't know where is left and where is right.
b * a
*** Exception: Ix{Int}.index: Index (2) out of range ((1,1))
Waaaaat?
I would bet that you are using a newish GHC; and as a result cabal chose an oldish version of bed-and-breakfast. On my machine, it chose version 0.1.2 (even though the latest version is 0.4.3); this version appears to have an incorrect dimensions check in its multiplication function:
a `times` b
| numRows a /= numCols b = error "Matrix.times: `numRows a' and `numCols b' don't match."
| otherwise = fromList [ [ row i a `dotProd` col j b
| j <- [1..numCols b] ]
| i <- [1..numRows a] ]
That check should be numCols a /= numRows b. The check is fixed in later versions of the library; but these versions also (correctly) specify upper bounds on base that exclude new GHCs.
Therefore I suspect that you will either have to update the library to work with new GHCs, patch the old version of the library, or use a more actively maintained library. I've been happy with hmatrix in the past, though note that matrix multiplication is spelled (<>), not (*).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With