matrix multiplication

R

robix

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 :p 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!
 
C

Christian Bau

"robix said:
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.
 
R

Rouben Rostamian

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top