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. Advertisements

  2. [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, Mar 14, 2009
    #2
    1. Advertisements

  3. LL

    user923005 Guest

    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 :
    I guess he use this group as a notepad or a to-do list.
     
    Kojak, Mar 15, 2009
    #4
  5. LL

    user923005 Guest

    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. 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 :
    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.
     
    Kojak, Mar 15, 2009
    #7
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.