Matrix multiplication code

Discussion in 'C Programming' started by LL, Mar 14, 2009.

  1. LL

    LL Guest

    /* matrix_mult.h */
    #include <stdio.h>
    #include <stdlib.h>

    typedef double matrix[50][50];

    // Matrix multiplication
    matrix* mm(matrix a, matrix b, int m, int r, int n) {
    matrix *c=(matrix*)malloc(sizeof(matrix));

    int i,j,k;
    for (i=0; i<m; i++) {
    for (k=0; k<n; k++) {
    (*c)[k]=0;
    for (j=0; j<r; j++) {
    (*c)[k]+=a[j]*b[j][k];
    }
    }
    }
    return c;
    }

    void printm(matrix *a, int m, int n) {
    int i,j;
    for (i=0; i<m; i++) {
    for (j=0; j<n; j++) {
    printf("%lf ", (*a)[j]);
    }
    printf("\n");
    }
    }

    /* sample_mm.c */
    #include "matrix_mult.h"

    int main() {
    matrix sample_a={{1,0,0},{0,1,0},{0,0,1}}; // Identity matrix
    matrix sample_b={{1,2,3},{4,5,6},{7,8,9}};
    matrix *sample_m;

    sample_m=mm(sample_a, sample_b, 3, 3, 3);
    printm(sample_m, 3, 3);
    }

    // Output
    1.000000 2.000000 3.000000
    4.000000 5.000000 6.000000
    7.000000 8.000000 9.000000
    LL, Mar 14, 2009
    #1
    1. Advertising

  2. LL <> writes:
    > /* matrix_mult.h */

    [30 lines deleted]
    >
    > /* sample_mm.c */

    [10 lines deleted]
    >
    > // Output

    [3 lines deleted]

    Yes, and ...?

    Did you have a question about the code? Why did you post it? I don't
    object to your posting it, I just don't know what you expect us to do
    with it.

    One comment: putting function definitions in a header file is a bad
    idea. You probably want to have the function *declarations* in
    matrix_mult.h, and the *definitions* in matrix_mult.c, which itself
    should have a #include "matrix_mult.h". And matrix_mult.h should have
    include guards, so the compiler won't choke if it's included more than
    once.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Mar 14, 2009
    #2
    1. Advertising

  3. LL

    user923005 Guest

    On Mar 14, 4:15 pm, Keith Thompson <> wrote:
    > LL <> writes:
    > > /* matrix_mult.h */

    > [30 lines deleted]
    >
    > > /* sample_mm.c */

    > [10 lines deleted]
    >
    > > // Output

    >
    > [3 lines deleted]
    >
    > Yes, and ...?
    >
    > Did you have a question about the code?  Why did you post it?  I don't
    > object to your posting it, I just don't know what you expect us to do
    > with it.
    >
    > One comment: putting function definitions in a header file is a bad
    > idea.  You probably want to have the function *declarations* in
    > matrix_mult.h, and the *definitions* in matrix_mult.c, which itself
    > should have a #include "matrix_mult.h".  And matrix_mult.h should have
    > include guards, so the compiler won't choke if it's included more than
    > once.


    Also, the 50x50 hardwire matrix size is rather bogus.
    Finally, there is this:
    http://math-atlas.sourceforge.net/
    If you want to multiply matrices, and do other fun things with them
    using C.

    Reinvention of the wheel is fine, but when you can get a free one that
    it attached to a free Rolls-Royce it's good to keep that in mind as
    well.
    user923005, Mar 15, 2009
    #3
  4. LL

    Kojak Guest

    Le Sat, 14 Mar 2009 16:15:28 -0700,
    Keith Thompson a écrit :

    > LL <> writes:
    > > /* matrix_mult.h */

    > [All lines deleted]
    >
    > Yes, and ...?
    >
    > Did you have a question about the code? Why did you post it? I don't
    > object to your posting it, I just don't know what you expect us to do
    > with it.


    I guess he use this group as a notepad or a to-do list.

    --
    Jacques.
    Kojak, Mar 15, 2009
    #4
  5. LL

    user923005 Guest

    On Mar 14, 6:14 pm, Kojak <> wrote:
    > Le Sat, 14 Mar 2009 16:15:28 -0700,
    > Keith Thompson a écrit :
    >
    > > LL <> writes:
    > > > /* matrix_mult.h */

    > > [All lines deleted]

    >
    > > Yes, and ...?

    >
    > > Did you have a question about the code?  Why did you post it?  I don't
    > > object to your posting it, I just don't know what you expect us to do
    > > with it.

    >
    > I guess he use this group as a notepad or a to-do list.


    I suspect that he is simply overjoyed at getting the code to work and
    like a proud papa wanted to show everyone.

    He posted here earilier with non-working code.
    user923005, Mar 15, 2009
    #5
  6. On Mar 14, 8:52 pm, user923005 <> wrote:
    > On Mar 14, 6:14 pm, Kojak <> wrote:
    >
    > > Le Sat, 14 Mar 2009 16:15:28 -0700,
    > > Keith Thompson a écrit :

    >
    > > > LL <> writes:
    > > > > /* matrix_mult.h */
    > > > [All lines deleted]

    >
    > > > Yes, and ...?

    >
    > > > Did you have a question about the code?  Why did you post it?  I don't
    > > > object to your posting it, I just don't know what you expect us to do
    > > > with it.

    >
    > > I guess he use this group as a notepad or a to-do list.

    >
    > I suspect that he is simply overjoyed at getting the code to work and
    > like a proud papa wanted to show everyone.
    >
    > He posted here earilier with non-working code.


    Having recently been guilty of this very thing,
    I find your suspicions compelling.

    lxt
    luser-ex-troll, Mar 15, 2009
    #6
  7. LL

    Kojak Guest

    Le Sat, 14 Mar 2009 18:52:41 -0700 (PDT),
    user923005 a écrit :

    > On Mar 14, 6:14 pm, Kojak <> wrote:
    > > I guess he use this group as a notepad or a to-do list.

    >
    > I suspect that he is simply overjoyed at getting the code to work and
    > like a proud papa wanted to show everyone.
    >
    > He posted here earilier with non-working code.


    My first guess was related to some thread. He posted and replied
    to himself without any reply to other's replies (hum, not sure my
    sentence is really fair). That said, indeed, it's another way of
    seeing things, and you're certainly right.

    --
    Jacques.
    Kojak, Mar 15, 2009
    #7
    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,312
    joris
    Mar 24, 2010
  2. Michael Bader
    Replies:
    11
    Views:
    12,045
    Michael Bader
    Mar 3, 2004
  3. robix

    matrix multiplication

    robix, Nov 12, 2003, in forum: C Programming
    Replies:
    3
    Views:
    505
    Rouben Rostamian
    Nov 13, 2003
  4. Sssasss

    matrix Multiplication

    Sssasss, Oct 18, 2006, in forum: Python
    Replies:
    7
    Views:
    366
    Sssasss
    Oct 18, 2006
  5. William Hughes
    Replies:
    13
    Views:
    1,215
    Ben Bacarisse
    Mar 15, 2010
Loading...

Share This Page