1

I'm trying to multiply two upper triangular matrices together. The matrices are stored in single dimensional arrays instead of the usual 2-D arrays by omitting the zeros that would be under the diagonal (to conserve space). I've figured out how to map elements given a pair of indices to the index for the single array. but I'm having trouble with the actual calculation (The calculation works for smaller n x n square matrices, but for some reason gives incorrect results for larger n x n matrices). I believe that I might be passing in incorrect parameters to the getValue() function, but I think they should be right considering the general formula for matrix multiplication. Any help will be appreciated!

Here's my relevant code:

// mat is an array containing the upper triangle data for a square matrix of size n
// returns element at (i,j), or 0 for the lower triangle
int val(int *mat, int n, int i, int j)
{
  if (i > j) {
    return 0; // lower triangle
  } else {
    return mat[j + (i*n) - i*(i+1)/2];
  }
}
user101279
  • 67
  • 7

2 Answers2

1

user101263,

You might not want to map your 2D array into a 1D array for simplification, beacuse in reality, it just complicates the simple matrix-multiplication algorithm.

Here is an implementation of the MM algorithm:

   int main()
   {
      int[5][5] result;
      /* omitted: create your 2 2D arrays a & b */
      matrixMulitplcation(a,b, result)
   }

  int** matrixMultiplcation(int a[5][5], int b[5][5], result[5][5])
  {       
     for(int R=0;R<5;R++)
     {
       for(int C=0;C<5;C++)
       {
          result[R][C]=0;
          for(int T=0;T<5;T++)
            result[R][C]+=a[R][T]*b[T][C];
       }
     }
     return result;
  }

Please let me know if you have any questions!

Devarsh Desai
  • 5,984
  • 3
  • 19
  • 21
  • Thanks for the suggestion Devarsh however, considering that unnecessary space will be wasted holding zeros in large matrices, I need to implement the multiplication using a 1D array for work. – JustAnotherUser Oct 13 '14 at 23:08
  • 1
    hey @user101263, I understand. What you're referring to is a common problem called, "sparse matrix multiplication" (http://stackoverflow.com/questions/15693584/fast-sparse-matrix-multiplication). We typically try to approach the storage of the sparse matrix rather than transposing it into a 1D matrix. In my time in academia and industry, I've never come across flattening the matrix as an option (because this will inevitably increase your time cost, especially if your matrix is SO LARGE that it's considered sparse and you don't want to waste space holding zeros); – Devarsh Desai Oct 13 '14 at 23:17
  • I'm not an expert on this, but I don't think spaces in linear algebra hold valid under the transposition you're trying to achieve. – Devarsh Desai Oct 13 '14 at 23:18
0
for(int i = 0; i < n; i++)
{
    for(int j = 0; j < m; j++)
    {
        for(int k = 0;k < n; k++)
        {
            C[ij_to_k(i,j,n)] += A[ij_to_k(i,k,n)] * B[ij_to_k(k,j,n)];
        }
    }
}

Just create the ij_to_k() function and you are good to go.

dshin
  • 2,354
  • 19
  • 29
  • Can you elaborate on what you mean by the `ij_to_k()` function? I have already figured out how to convert between 2D array indices and my 1D array. – JustAnotherUser Oct 13 '14 at 23:10
  • ij_to_k(i,j,n) should return a value k such that matA[k] = A[i][j] – dshin Oct 13 '14 at 23:46
  • See the thing is my program already does that from what testing it shows (at least I hope so). – JustAnotherUser Oct 13 '14 at 23:59
  • I see no such function in your code. Also, you need to pass you in your vectors as references, you are just modifying local variables as is. – dshin Oct 14 '14 at 01:15