algorithm - Scala中nxm和mxp矩阵的乘法算法

标签 algorithm scala matrix multiplication

我想知道为什么这个矩阵乘法在我的 Scala 程序中不起作用,而我在使用 Python 时收到的结果却相反。我正在使用这个数学描述的矩阵乘法算法:Matrix Multiplication我有两个矩阵 a = n x m 和 b = m x p。我为该算法编写的代码是(每个矩阵都是一个二维 double 组):

def dot(other: Matrix2D): Matrix2D ={
if (this.shape(1) != other.shape(0)){
  throw new IndexOutOfBoundsException("Matrices were not the right shape! [" + this.shape(1) + " != " + other.shape(0) + "]")
}
val n = this.shape(1) //returns the number of columns, shape(0) returns number of rows
var a = matrix.clone()
var b = other.matrix.clone()
var c = Array.ofDim[Double](this.shape(0), other.shape(1))

for(i <- 0 until c.length){
  for (j <- 0 until c(0).length){
    for (k <- 0 until n){
      c(i)(j) += a(i)(k) * b(k)(j)
    }
  }
}
  Matrix2D(c)
}

我输入到 Scala 和 Python 代码中的输入是:

a = [[1.0 1.0 1.0 1.0 0.0 0.0 0.0]
    [1.0 1.0 0.0 1.0 0.0 0.0 0.0 ]
    [1.0 1.0 1.0 1.0 1.0 1.0 1.0 ]
    [1.0 0.0 0.0 0.0 1.0 1.0 1.0 ]
    [1.0 0.0 0.0 0.0 1.0 0.0 1.0 ]
    [1.0 0.0 0.0 0.0 0.0 0.0 0.0 ]]

b = [[0.0 0.0                  0.0                  ]
    [0.0 -0.053430398509053074 0.021149859549078387 ]
    [0.0 -0.010785871994186721 0.04942555653681449  ]
    [0.0  0.04849323245519227 -0.0393881161667335   ]
    [0.0 -0.03871752673999099  0.05228579488821056  ]
    [0.0  0.07935206375269452  0.06511344235965408  ]
    [0.0 -0.02462677123918247  1.723607966539059E-4 ]]

我从这个函数收到的输出是:

[[0.0 -0.015723038048047533    0.031187299919159375]
[0.0  -0.0049371660538608045  -0.018238256617655116]
[0.0   2.84727725473527E-4     0.14875889796367792 ]
[0.0   0.01600776577352106     0.11757159804451854 ]
[0.0  -0.06334429797917346     0.05245815568486446 ]
[0.0   0.0                     0.0                ]]

与python的numpy.dot算法相比:

[[ 0.         -0.01572304  0.0311873 ]
 [ 0.         -0.00493717 -0.01823826]
 [ 0.         -0.01572304  0.0311873 ]
 [ 0.          0.08912777  0.07801112]
 [ 0.          0.00977571  0.01289768]
 [ 0.          0.08912777  0.07801112]]

我想知道为什么这个算法不能完全填满我需要的输出算法...我一直在搞乱 for 循环等等,一直没能弄清楚哪里出了问题。

最佳答案

你能展示一下你的 Python 代码吗?

我在 Numpy 中试过这个并得到与你的 Scala 代码相同的结果:

import numpy as np

a = np.array([[1.0,1.0,1.0,1.0,0.0,0.0,0.0],
    [1.0, 1.0, 0.0, 1.0, 0.0,0.0,0.0 ],
    [1.0, 1.0, 1.0, 1.0, 1.0,1.0,1.0 ],
    [1.0, 0.0, 0.0, 0.0, 1.0 ,1.0,1.0 ],
    [1.0, 0.0, 0.0, 0.0, 1.0, 0.0,1.0 ],
    [1.0, 0.0, 0.0, 0.0, 0.0, 0.0,0.0 ]])
b=np.array([[0.0 ,0.0                  ,0.0                  ],
    [0.0 ,-0.053430398509053074 ,0.021149859549078387 ],
    [0.0 ,-0.010785871994186721, 0.04942555653681449  ],
    [0.0 , 0.04849323245519227 ,-0.0393881161667335   ],
    [0.0 ,-0.03871752673999099 , 0.05228579488821056  ],
    [0.0 , 0.07935206375269452 , 0.06511344235965408  ],
    [0.0 ,-0.02462677123918247  ,1.723607966539059E-4 ]])
print a.dot(b)

打印:

[[ 0.         -0.01572304  0.0311873 ]
 [ 0.         -0.00493717 -0.01823826]
 [ 0.          0.00028473  0.1487589 ]
 [ 0.          0.01600777  0.1175716 ]
 [ 0.         -0.0633443   0.05245816]
 [ 0.          0.          0.        ]]

关于algorithm - Scala中nxm和mxp矩阵的乘法算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49680369/

相关文章:

python - 使用python删除矩阵中的相应行/列

php - 如何以简单/高效的方式进行 PHP 矩阵运算

algorithm - 最小方差生成树的多项式时间算法

c++ - 如何将谓词传递给算法

algorithm - 在给定经纬度、日期和时间的情况下查找阴影的方向

java - 如何在scala akka(spray)中编写休息服务的测试用例

matrix - (顶点着色器,mat4)均匀矩阵是否应该在CPU上预乘?

php - 计算预定义日期范围之间的天数

scala - 为大型 json 数据文件创建加特林自定义 feeder

scala - @karate如何将参数传递给 Gatling 模拟类中的特征文件?