3

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?

5
  • The first error looks wrong: the source code is ... | numCols a /= numRows b = error "Matrix.times: `numRows a' and `numCols b' don't match." which is eyebrow-raising ...
    – chi
    Nov 25, 2015 at 21:51
  • @chi That's the source code for the most recent release, yes -- but I bet OP isn't using the most recent release! Nov 25, 2015 at 21:53
  • @DanielWagner If that's the latest release, it still looks wrong! I also checked the github and the error is still there... Am I missing anything?
    – chi
    Nov 25, 2015 at 23:10
  • 2
    @chi The text of the error is backwards (actually ambiguous, because who the heck knows which argument is a and which is b without reading the source of times?) -- but the actual check is right. And in the old release, the text of the error matched the (wrong) check -- so it was semantically wrong but at least it was accurate to what the code actually did! Nov 25, 2015 at 23:17
  • 1
    I had never heard of this library until now. I would recommend the static part of hmatrix hackage.haskell.org/package/hmatrix-0.17.0.1/docs/…. This catches row / column mismatches at compile time rather than run time. You can see an example here in an implementation of a Kalman filter: idontgetoutmuch.wordpress.com/2015/05/31/…. The plethora of type signatures are mainly there to help the reader; most can be removed. Nov 28, 2015 at 17:00

2 Answers 2

3

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 (*).

2
  • Thanks. Cabal really chose 0.1.2 version. Last time I tried to build hmatrix on windows it takes 2 days and led me to nowhere. So I'd better use Python or Matlab..
    – meownoid
    Nov 25, 2015 at 22:05
  • @user834108 You could also consider using cabal unpack bed-and-breakfast-0.1.2 to get a copy of the source, fix the check, and cabal install the result, if you're really set on "bed-and-breakfast or nothing". Nov 25, 2015 at 22:09
1

I looked at the Numeric.Matrix documentation.

The innermost lists represent the rows. This function will create a m-n-matrix, where m is the number of rows, which is the minimum length of the row lists and n is the number of columns, i.e. the length of the outer list.

This is... difficult to parse, but it means that [[1], [2], [3]] is a 1x3 matrix, not a 3x1 matrix. This matches the assertion that the number of columns is the length of the outer list--which of course implies that each inner list is itself a column.

Using the phrase "row-major order" would make things clearer. For reference, Fortran is row-major, C is column-major. Most linear algebra packages seem to follow Fortran conventions, so the bed-and-breakfast library is fairly normal in that regard.

As for the exception... sounds like a bug?

5
  • Ok, thanks. But [[1], [2], [3]] is displayed (show b) like 1\n 2\n 3\n.. It's really confusing.
    – meownoid
    Nov 25, 2015 at 21:50
  • Yep. I would probably choose a different library. Nov 25, 2015 at 21:54
  • I think fromList [[1],[2],[3]] is indeed intended to be a 3x1 matrix. Since [[1], [2], [3]] has three inner-most lists, it has three rows. This seems to mesh perfectly well with the documentation in my reading. Nov 25, 2015 at 21:55
  • Then how is "the number of columns" equal to "the length of the outer list"? Perhaps the documentation is incorrect or contradictory. At a bare minimum, the documentation is poor. Nov 25, 2015 at 21:57
  • @DietrichEpp ...you're right, that excerpt is a mess. Somehow I managed to not even read the second sentence before I wrote my comment. Having the number of rows be the minimum length of any row is... just clearly wrong. Nov 25, 2015 at 22:18

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.