views:

92

answers:

4

So I'm trying to multiply matrices in c. However when I try to multiply the numbers in the two arrays, and put them in an answer array its always zero. heres the code for the method, thanks.

My matrix struct:

typedef struct matrix {
 int r;
 int c;
 double **mat;
 } *matrix_t;

My matrix multiplying method:

matrix_t mat_mult(matrix_t a, matrix_t b)
{
 int i, j, k;
 double x, temp1, temp2;
 double tempsol = 0.0;
 x = temp1 = temp2 = 0;
 matrix_t answer;

 if(a -> c == b -> r)
 {
    answer = mat_new(a -> r, b -> c);

    for(i = 0; i < a -> r; i++) 
       for( j = 0; j < b -> c; j++)
       {

           for( k = 0; k < a -> c; k++)
           {
               tempsol += a->mat[i][k] * b->mat[k][j];
               answer-> mat[i][j] =  tempsol;
           }

       }

 return answer;
 }
 else if(a -> r == b -> c)
 {
  answer = mat_new(a -> c, b -> r); 
  return answer;
 }
 else
 {
  printf("Matrices could not be multiplied");
  exit(1);
  return;
 }
}

heres the code for my mat_new as well

matrix_t mat_new(int r,int c)
{
int i = 0;
double **a;
matrix_t matrix_a;  

a = (double**)malloc(r *sizeof(double *));
for(i = 0; i < r; i++)
{
    a[i] = (double*)malloc(c *sizeof(double));
}
matrix_a = (matrix_t) malloc ( sizeof(struct matrix));
matrix_a -> mat = a;
matrix_a -> r = r;
matrix_a -> c = c;

return matrix_a;
}
A: 

Your code doesn't contain any obvious errors. Perhaps the problem lies in your mat_new(). The way you defined mat in your matrix structure as double **mat;, which I wouldn't recommend, may be causing some problems.

To allocate a 2x2 matrix to mat, you would need to do:

mat = new (double*)[2];
mat[0] = new double[2];
mat[1] = new double[2];

or a n by m matrix:

mat = new (double*)[n];
for (int i=0;i<n;i++) {
  mat[i] = new double[m];
}

Is this what you are doing?

Alexander Rafferty
this is my mat_new method, I dont't use the method you used, but if yours is better, i obviously will, anyways, heres mat_new
jbernie2
how do you enter more code?
jbernie2
i included the mat_new in the above code.
jbernie2
The title was "C". Your solution is C++.
+1  A: 

You need to free your objects. You need to reset tempsol. But most importantly, you need to review your mat_mult().

matrix_t mat_mult(matrix_t a, matrix_t b)
{
 /* ... */
 if(a -> c == b -> r)
 {
  /* ... */
 }
 else if(a -> r == b -> c)
 {
                                    /* BZZZZT!                 */
  answer = mat_new(a -> c, b -> r); /* BZZZZT! mat_mult(b, a); */
                                    /* BZZZZT!                 */
  return answer;
 }
 else
 {
  /* ... */
 }
}
pmg
A: 

This should work for your example:

matrix_t mat_new(int r,int c)
{
  matrix_t new = malloc(sizeof*new);
  new->r   = r;
  new->c   = c;
  new->mat = malloc( r*c*sizeof(double) );
  return new;
}
A: 

Seems like all your issues stem from reading in matrix values as integers rather than doubles. Everything works fine if you change temp in read_mat() to an int, then cast it to a double when you're putting it in the matrix.

cliff259