Matrix multiplication code

L

LL

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

Keith Thompson

LL said:
/* 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.
 
U

user923005

LL said:
/* 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.
 
K

Kojak

Le Sat, 14 Mar 2009 16:15:28 -0700,
Keith Thompson a écrit :
LL said:
/* 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.
 
U

user923005

Le Sat, 14 Mar 2009 16:15:28 -0700,
Keith Thompson a écrit :
LL said:
/* 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.
 
L

luser-ex-troll

Le Sat, 14 Mar 2009 16:15:28 -0700,
Keith Thompson a écrit :
/* 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
 
K

Kojak

Le Sat, 14 Mar 2009 18:52:41 -0700 (PDT),
user923005 a écrit :
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.
 

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

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top