Matrix multiplication algorithm not working correctly

L

LL

#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));

// This part: why doesn't it work properly?
int i,j,k;
for (i=0; i<m; i++) {
for (k=0; k<n; k++) {
for (j=0; j<r; j++) {
(*c)[k]=a[j]*b[j][k];
}
}
}
return c;
}

int main() {
// Sample matrices a,b
matrix a_={{1,0},{0,1}}; // Identity matrix
matrix b_={{1,2},{3,4}};
matrix *c_=(matrix*)malloc(sizeof(matrix));

// a_*b_=c_
c_=mm(a_,b_,2,2,2);
int i,j;
for (i=0; i<2; i++) {
for (j=0; j<2; j++) {
printf("%lf ", (*c_)[j]);
}
printf("\n");
}
}

/* Expected output:
* 1 2
* 3 4
*
* Output:
* 0 0
* 3 4
* Why!!??
*/
 
D

Dik T. Winter

> matrix* mm(matrix a, matrix b, int m, int r, int n) {
> matrix *c=(matrix*)malloc(sizeof(matrix));
>
> // This part: why doesn't it work properly?
> int i,j,k;
> for (i=0; i<m; i++) {
> for (k=0; k<n; k++) {
> for (j=0; j<r; j++) {
> (*c)[k]=a[j]*b[j][k];
> }
> }
> }
> return c;
> }


Perhaps because you should initialise the elements of c and add each product
to c[k]?
 
L

LL

#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));

// This part: why doesn't it work properly? int i,j,k;
for (i=0; i<m; i++) {
for (k=0; k<n; k++) {
Oh shit...
for (j=0; j<r; j++) {
(*c)[k]=a[j]*b[j][k];

Oh shit...
Made a mistake... if someone else does it for me that would be good too...

(*c)[k]+=a[j]*b[j][k];
}
}
}
return c;
}

int main() {
// Sample matrices a,b
matrix a_={{1,0},{0,1}}; // Identity matrix matrix b_={{1,2},{3,4}};
matrix *c_=(matrix*)malloc(sizeof(matrix));

// a_*b_=c_
c_=mm(a_,b_,2,2,2);
int i,j;
for (i=0; i<2; i++) {
for (j=0; j<2; j++) {
printf("%lf ", (*c_)[j]);
}
printf("\n");
}
}

/* Expected output:
* 1 2
* 3 4
*
* Output:
* 0 0
* 3 4
* Why!!??
*/
 
B

Ben Bacarisse

LL said:
#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));

// This part: why doesn't it work properly?
int i,j,k;
for (i=0; i<m; i++) {
for (k=0; k<n; k++) {
for (j=0; j<r; j++) {
(*c)[k]=a[j]*b[j][k];


That is not how matrix multiplication is done. You need to store in
(*c)[k] the sum of the products from this inner loop. Here you
repeatedly store different products there but only the last one
matters.
 
L

LL

#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));

// This part: why doesn't it work properly? int i,j,k; for (i=0; i<m;
i++) {
for (k=0; k<n; k++) {
Oh shit...
for (j=0; j<r; j++) {
(*c)[k]=a[j]*b[j][k];

Oh shit...
Made a mistake... if someone else does it for me that would be good
too...

(*c)[k]+=a[j]*b[j][k];

WTF... nm... plz someone else can do this for me(not homework)
if not then nm also...
}
}
}
return c;
}

int main() {
// Sample matrices a,b
matrix a_={{1,0},{0,1}}; // Identity matrix matrix b_={{1,2},{3,4}};
matrix *c_=(matrix*)malloc(sizeof(matrix));

// a_*b_=c_
c_=mm(a_,b_,2,2,2);
int i,j;
for (i=0; i<2; i++) {
for (j=0; j<2; j++) {
printf("%lf ", (*c_)[j]);
}
printf("\n");
}
}

/* Expected output:
* 1 2
* 3 4
*
* Output:
* 0 0
* 3 4
* Why!!??
*/
 
R

Richard Tobin

LL said:
for (i=0; i<m; i++) {
for (k=0; k<n; k++) {
for (j=0; j<r; j++) {
(*c)[k]=a[j]*b[j][k];


You're supposed to sum over all the values of j.

-- Richard
 
L

LL

#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));

// This part: why doesn't it work properly? int i,j,k; for (i=0;
i<m; i++) {
for (k=0; k<n; k++) {
Oh shit...
for (j=0; j<r; j++) {
(*c)[k]=a[j]*b[j][k];

Oh shit...
Made a mistake... if someone else does it for me that would be good
too...

(*c)[k]+=a[j]*b[j][k];

WTF... nm... plz someone else can do this for me(not homework) if not
then nm also...

Correction:
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]=(*c)[k]+a[j]*b[j][k];
}
}
}
This works for me... can someone verify?

}
}
}
return c;
}

int main() {
// Sample matrices a,b
matrix a_={{1,0},{0,1}}; // Identity matrix matrix b_={{1,2},{3,4}};
matrix *c_=(matrix*)malloc(sizeof(matrix));

// a_*b_=c_
c_=mm(a_,b_,2,2,2);
int i,j;
for (i=0; i<2; i++) {
for (j=0; j<2; j++) {
printf("%lf ", (*c_)[j]);
}
printf("\n");
}
}

/* Expected output:
* 1 2
* 3 4
*
* Output:
* 0 0
* 3 4
* Why!!??
*/
 
L

LL

On Thu, 12 Mar 2009 14:23:52 +0000, LL wrote:

#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));


// This part: why doesn't it work properly? int i,j,k; for (i=0;
i<m; i++) {
for (k=0; k<n; k++) {
Oh shit...

for (j=0; j<r; j++) {
(*c)[k]=a[j]*b[j][k];
Oh shit...
Made a mistake... if someone else does it for me that would be good
too...

(*c)[k]+=a[j]*b[j][k];

WTF... nm... plz someone else can do this for me(not homework) if not
then nm also...

Correction:
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]=(*c)[k]+a[j]*b[j][k];
}
}
}
This works for me... can someone verify?

Nope. On different examples it doesn't work.
}
}
}
return c;
}

int main() {
// Sample matrices a,b
matrix a_={{1,0},{0,1}}; // Identity matrix matrix
b_={{1,2},{3,4}}; matrix *c_=(matrix*)malloc(sizeof(matrix));

// a_*b_=c_
c_=mm(a_,b_,2,2,2);
int i,j;
for (i=0; i<2; i++) {
for (j=0; j<2; j++) {
printf("%lf ", (*c_)[j]);
}
printf("\n");
}
}

/* Expected output:
* 1 2
* 3 4
*
* Output:
* 0 0
* 3 4
* Why!!??
*/
 
L

LL

On Thu, 12 Mar 2009 14:39:02 +0000, LL wrote:

On Thu, 12 Mar 2009 14:23:52 +0000, LL wrote:

#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));



// This part: why doesn't it work properly? int i,j,k; for (i=0;
i<m; i++) {
for (k=0; k<n; k++) {
Oh shit...

for (j=0; j<r; j++) {
(*c)[k]=a[j]*b[j][k];
Oh shit...
Made a mistake... if someone else does it for me that would be good
too...

(*c)[k]+=a[j]*b[j][k];
WTF... nm... plz someone else can do this for me(not homework) if not
then nm also...

Correction:
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]=(*c)[k]+a[j]*b[j][k];
}
}
}
This works for me... can someone verify?

Nope. On different examples it doesn't work.

I think it might not work on non-square matrices. I'll think later.
}
}
}
return c;
}

int main() {
// Sample matrices a,b
matrix a_={{1,0},{0,1}}; // Identity matrix matrix
b_={{1,2},{3,4}}; matrix *c_=(matrix*)malloc(sizeof(matrix));

// a_*b_=c_
c_=mm(a_,b_,2,2,2);
int i,j;
for (i=0; i<2; i++) {
for (j=0; j<2; j++) {
printf("%lf ", (*c_)[j]);
}
printf("\n");
}
}

/* Expected output:
* 1 2
* 3 4
*
* Output:
* 0 0
* 3 4
* Why!!??
*/
 
L

LL

On Thu, 12 Mar 2009 14:39:02 +0000, LL wrote:

On Thu, 12 Mar 2009 14:23:52 +0000, LL wrote:

#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));



// This part: why doesn't it work properly? int i,j,k; for (i=0;
i<m; i++) {
for (k=0; k<n; k++) {
Oh shit...

for (j=0; j<r; j++) {
(*c)[k]=a[j]*b[j][k];
Oh shit...
Made a mistake... if someone else does it for me that would be good
too...

(*c)[k]+=a[j]*b[j][k];
WTF... nm... plz someone else can do this for me(not homework) if not
then nm also...

Correction:
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]=(*c)[k]+a[j]*b[j][k];
}
}
}
This works for me... can someone verify?

Nope. On different examples it doesn't work.

Actually I think the algorithm is correct now.
}
}
}
return c;
}

int main() {
// Sample matrices a,b
matrix a_={{1,0},{0,1}}; // Identity matrix matrix
b_={{1,2},{3,4}}; matrix *c_=(matrix*)malloc(sizeof(matrix));

// a_*b_=c_
c_=mm(a_,b_,2,2,2);
int i,j;
for (i=0; i<2; i++) {
for (j=0; j<2; j++) {
printf("%lf ", (*c_)[j]);
}
printf("\n");
}
}

/* Expected output:
* 1 2
* 3 4
*
* Output:
* 0 0
* 3 4
* Why!!??
*/
 
B

Ben Bacarisse

LL said:
Correction:
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]=(*c)[k]+a[j]*b[j][k];
}
}
}
This works for me... can someone verify?

Nope. On different examples it doesn't work.


You should give an example rather than just saying that "it does not
work" -- that makes it easier for others. We don't have to think of
all the test cases you might have tried. I say this because it looks
OK at first glance, and first glances are often all yo get from Usenet
posts.


You don't need to malloc c. The pointer is lost as soon as you do...

.... this. Also, there is not need to pass three numbers. The third
is dependent on the other two (and if you matrices are square you
could pass just one).

Please add a little white space to you lines!
 
B

Bartc

LL said:
#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));

// This part: why doesn't it work properly?
int i,j,k;
for (i=0; i<m; i++) {
for (k=0; k<n; k++) {
for (j=0; j<r; j++) {
(*c)[k]=a[j]*b[j][k];
}
}
}
return c;
}


I think maybe get the basic multiply working properly. If remember matrix
multiplication correctly, the following code multiplies 3x3 square matrices:

#include <stdio.h>

typedef double mat33[3][3];

/* c = a * b
matrices passed (effectively) by reference
*/
void mulmat33(mat33 a, mat33 b, mat33 c)
{
int i,j,k;

for (i=0;i<3;++i)
for (j=0; j<3; ++j) {
c[j]=0;
for (k=0; k<3; ++k)
c[j] += a[k]*b[k][j];
}
}

And some test code:

int main(void) {
mat33 a = {{1,2,3},{4,5,6},{7,8,9}};
mat33 b = {{12,13,14},{15,16,17},{18,19,20}};
mat33 c={0};
int i,j,k;

mulmat33(a, b, c);

for (i=0;i<3;++i) {
printf("(");

for (j=0; j<3; ++j)
printf("%f ",c[j]);
printf(")\n");
}
}

Then you can worry about functions to multiply N-sized matrices and dynamic
allocation.
 
L

LL

On Thu, 12 Mar 2009 14:39:57 +0000, LL wrote:

On Thu, 12 Mar 2009 14:39:02 +0000, LL wrote:

On Thu, 12 Mar 2009 14:23:52 +0000, LL wrote:

#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));



// This part: why doesn't it work properly? int i,j,k; for (i=0;
i<m; i++) {
for (k=0; k<n; k++) {
Oh shit...

for (j=0; j<r; j++) {
(*c)[k]=a[j]*b[j][k];
Oh shit...
Made a mistake... if someone else does it for me that would be good
too...

(*c)[k]+=a[j]*b[j][k];
WTF... nm... plz someone else can do this for me(not homework) if not
then nm also...
Correction:
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]=(*c)[k]+a[j]*b[j][k];
}
}
}
This works for me... can someone verify?

Nope. On different examples it doesn't work.

Actually I think the algorithm is correct now.

Why goddamn newsserver. Doesn't appear immediately. That's why my posting
is like this. I apologise if anyone wonder why.
}
}
}
return c;
}

int main() {
// Sample matrices a,b
matrix a_={{1,0},{0,1}}; // Identity matrix matrix
b_={{1,2},{3,4}}; matrix *c_=(matrix*)malloc(sizeof(matrix));

// a_*b_=c_
c_=mm(a_,b_,2,2,2);
int i,j;
for (i=0; i<2; i++) {
for (j=0; j<2; j++) {
printf("%lf ", (*c_)[j]);
}
printf("\n");
}
}

/* Expected output:
* 1 2
* 3 4
*
* Output:
* 0 0
* 3 4
* Why!!??
*/
 
K

Kojak

Le 12 Mar 2009 14:23:52 GMT,
LL a écrit :

[... sniped ...]

You seem soliloquize ! Do you know, Usenet is not write-only !
 
D

David Thompson

... matrix *c_=(matrix*)malloc(sizeof(matrix));

You don't need to malloc c. The pointer is lost as soon as you do...
Yes.


... this. Also, there is not need to pass three numbers. The third
is dependent on the other two (and if you matrices are square you
could pass just one).
Huh? Yes, square x same only needs 1, and rect x transposed 2, but
general matrix mult needs all 3.
 

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,769
Messages
2,569,582
Members
45,069
Latest member
SimplyleanKetoReviews

Latest Threads

Top