Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type-safe matrix multiplication

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.

like image 642
Dan Burton Avatar asked Nov 30 '11 20:11

Dan Burton


People also ask

How many types of matrix multiplication are there?

There are two types of multiplication for matrices: scalar multiplication and matrix multiplication.

What is the best algorithm for 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.

What is SIMD matrix multiplication?

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.

What is matrix multiplication method?

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.


1 Answers

There are a number of packages that implement this:

  • http://hackage.haskell.org/package/hmatrix-static
  • http://hackage.haskell.org/packages/archive/repa-algorithms/2.2.0.1/doc/html/Data-Array-Repa-Algorithms-Matrix.html
  • http://hackage.haskell.org/packages/archive/blas/0.7.6/doc/html/Data-Matrix-Dense.html#t:Matrix
  • http://hackage.haskell.org/package/Vec

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.

like image 143
sclv Avatar answered Oct 02 '22 09:10

sclv