11

我正在尝试为 MySQL 编写矩阵乘法,但我有点卡住了:

基本上,我的矩阵以 [row#, column#, matrixID, value] 格式存储
,因此例如矩阵 [3 x 2] 将类似于:

[row#, column#, matrixID, value]
  1      1        mat01    1
  1      2        mat01    2
  1      3        mat01    3
  2      1        mat01    4
  2      2        mat01    5
  2      3        mat01    6

相当于:[[1 2 3],[4 5 6]]

以下确实很好地计算了 matrix1 * matrix2 的单个元素:

   SELECT SUM(row1.`val` * col2.`val`)
   FROM matValues row1
   INNER JOIN  `matValues` col2
   WHERE row1.`row` = 1 AND row1.`mID`='matrix1' AND 
         col2.`mID`='matrix2' AND col2.`col` = 1 AND row1.col = col2.row

将其包装到函数中,然后使用另一个函数迭代行号和列号可能会起作用,但是我在生成这组数字并使用 SQL 迭代它们时遇到问题。欢迎任何意见/建议

4

3 回答 3

19

尝试:

select m1.`row#`, m2.`column#`, sum(m1.value*m2.value) 
from matValues m1
join matValues m2 on m2.`row#` = m1.`column#` 
where m1.matrixID = 'mat01' and m2.matrixID = 'mat02'
group by m1.`row#`, m2.`column#`

这里的例子。

(将'mat01'和替换'mat02'为合适的matrixID值。)

于 2013-03-19T15:54:43.093 回答
1

您可以在 SQL 中进行整个计算。您只举一个带有单个矩阵的示例,因为它不是正方形,所以不能自行相乘。

这是想法:

SELECT mout.row, mout.col, SUM(m1.value*m2.value)
FROM (select distinct row from matValues cross join
      select distinct COL from matValues
     ) mout left outer join
     matValues m1
     on m1.row = mout.row left outer join
     matValues m2
     on m2.col = mout.col and
        m2.row = m1.col
于 2013-03-19T15:25:23.670 回答
0

我知道这是 SQL-Server 语法,但它应该让您开始了解相应的 MySql 语法。稀疏矩阵性质似乎处理得很好。

   with I as (
      select * from ( values
        (1,1, 1),
        (2,2, 1), 
        (3,3, 1)
      ) data(row,col,value)
    )
    ,z_90 as (
      select * from ( values
        (1,2, 1),
        (2,1,-1), 
        (3,3, 1)
      ) data(row,col,value)
    )
    ,xy as (
      select * from ( values
        (1,2, 1),
        (2,1, 1), 
        (3,3, 1)
      ) data(row,col,value)
    )
    ,x_90 as (
      select * from ( values
        (1,1, 1),
        (2,3, 1), 
        (3,2,-1)
      ) data(row,col,value)
    )
    select
       'I * z_90' as instance,
       a.row,
       b.col,
       sum( case when a.value is null then 0 else a.value end
          * case when b.value is null then 0 else b.value end ) as value
    from I as a
    join z_90 as b on a.col = b.row
    group by a.row, b.col
    union all
    select
       'z_90 * xy' as instance,
       a.row,
       b.col,
       sum( case when a.value is null then 0 else a.value end
          * case when b.value is null then 0 else b.value end ) as value
    from z_90 as a
    join xy as b on a.col = b.row
    group by a.row, b.col
    union all
    select
       'z_90 * x_90' as instance,
       a.row,
       b.col,
       sum( case when a.value is null then 0 else a.value end
          * case when b.value is null then 0 else b.value end ) as value
    from z_90 as a
    join x_90 as b on a.col = b.row
    group by a.row, b.col

    order by instance, a.row, b.col

产量:

instance    row         col         value
----------- ----------- ----------- -----------
I * z_90    1           2           1
I * z_90    2           1           -1
I * z_90    3           3           1
z_90 * x_90 1           3           1
z_90 * x_90 2           1           -1
z_90 * x_90 3           2           -1
z_90 * xy   1           1           1
z_90 * xy   2           2           -1
z_90 * xy   3           3           1

但是,我建议您也检查一下在您的显卡上执行此操作。NVIDIA 在 ri C Programming Guide 中有一个很好的实现矩阵乘法的例子。

于 2013-03-19T15:56:42.060 回答