Search code examples
algorithmscalamatrixmultiplication

Multiplication algorithm of nxm and mxp matrices in Scala


I am wondering why this matrix multiplication is not working in my Scala program, versus the result I am receiving when using Python. I am using the matrix multiplication algorithm described by this math: Matrix Multiplication where I have two matrices a = n x m and b = m x p. The code that I have written for this algorithm is (each matrix is a 2d array of doubles):

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)
}

The Input I put into both the Scala and Python code is:

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 ]]

The output I receive from this function is:

[[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                ]]

compared to python's numpy.dot algorithm:

[[ 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]]

I am wondering why this algorithm doesn't completely fill the output algorithm that I need...I've been messing with the for loops and such and have not been able to figure out whats wrong.


Solution

  • Can you show your Python code?

    I tried this in Numpy and get the same as your Scala code:

    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)
    

    prints:

    [[ 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.        ]]