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