matrix multiplication

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

  1. robix

    robix Guest

    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!
    robix, Nov 12, 2003
    #1
    1. Advertising

  2. robix

    robix Guest

    Thanks for the tips. It works now!
    robix, Nov 12, 2003
    #2
    1. Advertising

  3. 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
    #3
  4. 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
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. walala
    Replies:
    2
    Views:
    2,314
    joris
    Mar 24, 2010
  2. Michael Bader
    Replies:
    11
    Views:
    12,046
    Michael Bader
    Mar 3, 2004
  3. Sssasss

    matrix Multiplication

    Sssasss, Oct 18, 2006, in forum: Python
    Replies:
    7
    Views:
    368
    Sssasss
    Oct 18, 2006
  4. Replies:
    14
    Views:
    850
    Mark McIntyre
    Jan 26, 2006
  5. William Hughes
    Replies:
    13
    Views:
    1,216
    Ben Bacarisse
    Mar 15, 2010
Loading...

Share This Page