Search code examples
c#matrixconsole-applicationmultiplication

Multiply two matrices


I need some help. I have to write a method multiplying two matrices together. Here is my code but I got an IndexOutOfRangeException.

static int[,] FalkScheme(
    int[,] Matrix1,
    int[,] Matrix2,
    int rowcountmatrix1,
    int columncountmatrix1,
    int rowcountmatrix2,
    int columncountmatrix2)
{
    int[,] NewMatrix = new int[rowcountmatrix1, columncountmatrix2];

    if (columncountmatrix1 != rowcountmatrix2)
    {
        throw new Exception("the dimensions of the matrices do not fit! Please try operation with other matrices again!");            
    }
    else
    {
        for (int i = 0; i < rowcountmatrix1; i++)
        {
            for (int u = 0; u < columncountmatrix2; u++)
            {
                NewMatrix[i, u] = Matrix1[i, u] * Matrix2[i, u] + Matrix1[i, u + 1] * Matrix2[i + 1, u] + Matrix1[i, u + 2] * Matrix2[i + 2, u];
            }
        }

        return NewMatrix;
    }
}

Can you help me fix that problem?


Solution

  • You don't need any rowcountmatrix1..columncountmatrix2: ask arrays for its dimensions:

    public static int [,] FalkScheme(int[,] Matrix1, int[,] Matrix2) {
      if (Object.ReferenceEquals(null, Matrix1))
        throw new ArgumentNullException("Matrix1");
      else if (Object.ReferenceEquals(null, Matrix2))
        throw new ArgumentNullException("Matrix2");
    
      int r1 = Matrix1.GetLength(0);
      int c1 = Matrix1.GetLength(1);
    
      int r2 = Matrix2.GetLength(0);
      int c2 = Matrix2.GetLength(1);
    
      if (c1 != r2)
        throw new ArgumentOutOfRangeException("Matrix2", "Matrixes dimensions don't fit.");
    
      int[,] result = new int[r1, c2];
    
      // Naive matrix multiplication: O(n**3) 
      // Use Strassen algorithm O(n**2.81) in case of big matrices
      for (int r = 0; r < r1; ++r) 
        for (int c = 0; c < c2; ++c) {
          int s = 0;
    
          for (int z = 0; z < c1; ++z) 
            s += Matrix1[r, z] * Matrix2[z, c];
    
          result[r, c] = s;
        }
    
      return result;
    }
    

    Test

       int[,] a = new int[,] { {1, 2, 3}, {4, 5, 6}};
       int[,] b = new int[,] { { 5, 6 }, { 7, 8 } , {9, 10}};
       //  46  52
       // 109 124 
       int[,] c = Problems.FalkScheme(a, b);