# Matrix multiplication code

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

1. ### LLGuest

/* 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

2. ### Keith ThompsonGuest

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

3. ### user923005Guest

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
4. ### KojakGuest

Le Sat, 14 Mar 2009 16:15:28 -0700,

> 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
5. ### user923005Guest

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
6. ### luser-ex-trollGuest

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,

lxt

luser-ex-troll, Mar 15, 2009
7. ### KojakGuest

Le Sat, 14 Mar 2009 18:52:41 -0700 (PDT),

> 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