Velocity Reviews > Matrix multiplication code

# Matrix multiplication code

LL
Guest
Posts: n/a

 03-14-2009
/* 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)[i][k]=0;
for (j=0; j<r; j++) {
(*c)[i][k]+=a[i][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)[i][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

Keith Thompson
Guest
Posts: n/a

 03-14-2009
LL <(E-Mail Removed)> 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.velocityreviews.com/forums/(E-Mail Removed) <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"

user923005
Guest
Posts: n/a

 03-15-2009
On Mar 14, 4:15*pm, Keith Thompson <(E-Mail Removed)> wrote:
> LL <(E-Mail Removed)> 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.

Kojak
Guest
Posts: n/a

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

> LL <(E-Mail Removed)> 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.

user923005
Guest
Posts: n/a

 03-15-2009
On Mar 14, 6:14*pm, Kojak <(E-Mail Removed)> wrote:
> Le Sat, 14 Mar 2009 16:15:28 -0700,
> Keith Thompson a écrit :
>
> > LL <(E-Mail Removed)> 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.

luser-ex-troll
Guest
Posts: n/a

 03-15-2009
On Mar 14, 8:52*pm, user923005 <(E-Mail Removed)> wrote:
> On Mar 14, 6:14*pm, Kojak <(E-Mail Removed)> wrote:
>
> > Le Sat, 14 Mar 2009 16:15:28 -0700,
> > Keith Thompson a écrit :

>
> > > LL <(E-Mail Removed)> 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

Kojak
Guest
Posts: n/a

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

> On Mar 14, 6:14Â*pm, Kojak <(E-Mail Removed)> 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.