#define N ... double A[N][N]; |
#define N ... double v[N]; |
double A[N][N]; double v[N]; double result[N]; // Initialized result to [0,0,...,0] for (i = 0; i < N; i++) result[i] = 0.0; // Multiply matrix row with vector for (i = 0; i < N; i++) // row i { for (j = 0; j < N; j++) result[i] = result[i] + A[i][j] * v[j]; } |
|
|
Because the output vector is NOT a single value , the output value must be returned in a array variable
// m = # rows // n = # columns void MatrixVectorMult( double *out[], double *A[], double *in[], int m, int n) { int i, j; // Set output vector to ZERO for (i = 0; i < m; i = i + 1) { out[i] = 0.0; } // Matrix vector multiply algorithm for (i = 0; i < m; i = i + 1) { for (j = 0; j < n; j = j + 1) { out[i] = out[i] + A[i*n+j]*in[j]; } } } |
int main(int argc, char ** argv) { double A[8][8]; double u[8], v[8]; ... MatrixVectorMult(v, (double *)A, u, 8, 8); } |
Although sparse matrices can be stored using a two-dimensional array, it is a very bad idea to do so for several reasons:
|
|
Sparse Matrix -------------------------- 0 1 2 3 4 5 6 7 0 [ 11 12 14 ] [ 3 ] [3*11 + 5*12 + 2*0 + 1*14 + 0*0 + 1*0 + 2*0 + 4*0] 1 [ 22 23 25 ] [ 5 ] [... 2 [ 31 33 34 ] [ 2 ] [... 3 [ 42 45 46 ] [ 1 ] [... 4 [ 55 ] x [ 0 ] = [... 5 [ 65 66 67 ] [ 1 ] [... 6 [ 75 77 78 ] [ 2 ] [... 7 [ 87 88 ] [ 4 ] [... A[8][8] v[8] |
|
Depending on what operations you want to perform with the sparse matrix, you pick the best representation...