After the long-winded discussion at Write this Scala Matrix multiplication in Haskell, I was left wondering...what would a type-safe matrix multiplication look like? So here's your challenge: either link to a Haskell implementation, or implement yourself, the following:
data Matrix ... = ...
matrixMult :: Matrix ... -> Matrix ... -> Matrix ...
matrixMult ... = ...
Where matrixMult
produces a type error at compile time if you try to multiply two matricies with incompatible dimensions. Brownie points if you link to papers or books that discuss this precise topic, and/or discuss yourself how useful/useless this functionality is.
There are two types of multiplication for matrices: scalar multiplication and matrix multiplication.
In linear algebra, the Strassen algorithm, named after Volker Strassen, is an algorithm for matrix multiplication. It is faster than the standard matrix multiplication algorithm for large matrices, with a better asymptotic complexity, although the naive algorithm is often better for smaller matrices.
Let A and B be two matrices with size n×n and C be the result matrix. Step 1: Distribute ith row of matrix A and ith column of matrix B to PEi where 1≤i≤n. Step 2: Initialize C vector to ) in all PEs. Step 3: At every PEi do the following n times.
For matrix multiplication, the number of columns in the first matrix must be equal to the number of rows in the second matrix. The resulting matrix, known as the matrix product, has the number of rows of the first and the number of columns of the second matrix. The product of matrices A and B is denoted as AB.
There are a number of packages that implement this:
The Repa papers in particular have a really nice discussion of the design space and choices made: http://repa.ouroborus.net/
Of historical interest is McBride's "Faking It" from 2001 which describes strongly typed vectors. The techniques he employs are fairly similar to those used in the above packages. They were obviously known in circles doing dependently typed programming, but my impression is that the "Faking It" paper is one of the earlier instances where these were used in Haskell. Oleg's 2005 Monad Reader article on number-parameterized types has some good discussion on the history of these techniques as well.
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