# matrix multiplication

Discussion in 'C Programming' started by robix, Nov 12, 2003.

1. ### robixGuest

Hi again. I'm now asking your help because of a smal problem i'm getting
with my multiplication matrix code.

I'll try to give you as many details as possible.

My matrix structure:

typedef struct {
int col; /* number of colowns */
int lin; /* number of lines*/
double *element; /* pointer do double... this is actually, a "simulated
multidymensional array", got the idea from the C FAQ from this newsgroup */
} matrix;

matrix mul_matrix(matrix mat1, matrix mat2, int lin, int col)
{
matrix M;
int i, j, k, exp;
M = init_matrix(lin, col); /* inits M(will old the result from mat1 *
mat2), no problem here. At this point i am sure that mat1 and mat2 all have
their right values
and that M has the right size*/

/* Try to multiply */
for (i=0; i<lin; i++) {
for (j=0; j<col; j++) {
exp = 0;
for (k=0; k<col; k++) {
exp += mat1.element[i * (mat1.col * mat1.lin) + k]*
mat2.element[k * (mat2.col * mat2.lin) + j]; /* big problem... give me
random results*/
}
M.element[i * (lin * col) + j] = exp;
}
}
return M;
}

After 4 hours of trying to resolve this problem i'm exaust and in need for
some sleep Maybe that will make me good cause i sinceriously can't
see the problem on the code! Until now i've only used printf to assist me as
a rudimentary debugger. I think i need something more powerfull but
gdb don't want to work with devc++...
Thanks to anyone who can help me out!

robix, Nov 12, 2003

2. ### robixGuest

Thanks for the tips. It works now!

robix, Nov 12, 2003

3. ### Christian BauGuest

In article <3fb2b96b\$0\$24739\$>,
"robix" <> wrote:

> Hi again. I'm now asking your help because of a smal problem i'm getting
> with my multiplication matrix code.
>
> I'll try to give you as many details as possible.
>
> My matrix structure:
>
> typedef struct {
> int col; /* number of colowns */
> int lin; /* number of lines*/
> double *element; /* pointer do double... this is actually, a "simulated
> multidymensional array", got the idea from the C FAQ from this newsgroup */
> } matrix;
>
>
> matrix mul_matrix(matrix mat1, matrix mat2, int lin, int col)
> {
> matrix M;
> int i, j, k, exp;
> M = init_matrix(lin, col); /* inits M(will old the result from mat1 *
> mat2), no problem here. At this point i am sure that mat1 and mat2 all have
> their right values
> and that M has the right size*/
>
>
> /* Try to multiply */
> for (i=0; i<lin; i++) {
> for (j=0; j<col; j++) {
> exp = 0;
> for (k=0; k<col; k++) {
> exp += mat1.element[i * (mat1.col * mat1.lin) + k]*
> mat2.element[k * (mat2.col * mat2.lin) + j]; /* big problem... give me
> random results*/
> }
> M.element[i * (lin * col) + j] = exp;
> }
> }
> return M;
> }

Just check which two matrix elements you access when i = 1, j = 1 and k
= 1. Then compare this with the number of elements in the matrix mat1
and mat2, and you will see that something is very badly wrong.

Christian Bau, Nov 12, 2003
4. ### Rouben RostamianGuest

In article <3fb2b96b\$0\$24739\$>,
robix <> wrote:
>
>My matrix structure:
>
>typedef struct {
> int col; /* number of colowns */
> int lin; /* number of lines*/
> double *element; /* pointer do double... this is actually, a "simulated
>multidymensional array", got the idea from the C FAQ from this newsgroup */
>} matrix;
>
>
>matrix mul_matrix(matrix mat1, matrix mat2, int lin, int col)
>{
> matrix M;
> int i, j, k, exp;
> M = init_matrix(lin, col); /* inits M(will old the result from mat1 *
>mat2), no problem here. At this point i am sure that mat1 and mat2 all have
>their right values
> and that M has the right size*/
>
>
> /* Try to multiply */
> for (i=0; i<lin; i++) {
> for (j=0; j<col; j++) {
> exp = 0;
> for (k=0; k<col; k++) {
> exp += mat1.element[i * (mat1.col * mat1.lin) + k]*
>mat2.element[k * (mat2.col * mat2.lin) + j]; /* big problem... give me
>random results*/
> }
> M.element[i * (lin * col) + j] = exp;
> }
> }
> return M;
>}

1.
Why "int exp"?

2.
Suppose A is an mxn matrix and B is a nxk matrix. Their product
will be an mxk matrix. As you see, to multiply matrices, you need
to specify three index ranges, m, n and k. Your mul_matrix() function
takes only to index ranges, lin and col. Something's badly wrong
here.

--
Rouben Rostamian <>

Rouben Rostamian, Nov 13, 2003