Thursday, May 16, 2013

Matrix multiplication using mysql

Assume that there are two tables t1 and t2 containing cell values 
of two matrices on each.

For example : 
In table, the data is stored as follows :
 
row  col  value
0    0    5
0    1    2
1    0    5
1    1    9
 
This query can be used to do matrix multiplication:
 
select t1.r row,t2.c col,sum(t1.v*t2.v) value
from t1 join t2 on (t1.c=t2.r) 
group by t1.r,t2.c;