C中的矩阵乘法总是返回0?

时间:2013-05-01 14:51:46

标签: c matrix multiplication zero

我一直在尝试用C进行基本矩阵计算,但是将两个矩阵相乘总是返回0值。在阅读了其他有类似问题的人之后,我仍然不明白为什么会发生这种情况。这是乘法函数:

double** MatrixMultiplication(double** matrixA, double** matrixB,
                              int sizeXA, int sizeYA, int sizeXB, int sizeYB) 
{
    double** matrixC = MatrixAllocate(sizeXA, sizeYB);
    for (int i = 0; i < sizeXA; i++) {
        for (int j = 0; j < sizeYB; j++) {
            for (int k = 0; k < sizeXA; k++) {
                matrixC[i][j] += matrixA[i][k] * matrixB[k][j];
            }
        }
    }
    return matrixC;
}

这是MatrixAllocate函数的代码:

double** MatrixAllocate(int sizeX, int sizeY) {
    double **matrix;
    matrix = (double**)malloc(sizeX*sizeof(double*));
    for (int i = 0; i < sizeX; i++)
        matrix[i] = (double*)malloc(sizeY*sizeof(double));

    for (int i = 0; i < sizeX; i++) {
        for (int j = 0; j < sizeY; j++) {
            matrix[i][j] = 0;
        }
    }
    return matrix;
}

MatrixA和MatrixB都由用户填充,打印时它们具有非零数据。

3 个答案:

答案 0 :(得分:1)

还有其他问题,因为这对我来说很合适。请注意,我已经稍微修改了我的方法版本。 (更改了方法名称,因为只有类以Capitals开头,在矩阵创建中添加了控制初始值的能力,不能全部为种子进行测试。)

预期的输出是:

  


  行[0]:0 1 2
  行[1]:3 4 5
  行[2]:6 7 8
  酒吧
  行[0]:0 1 2
  行[1]:3 4 5
  行[2]:6 7 8
  RES
  Row [0]:15 19 23
  行[1]:45 58 71
  Row [2]:75 97 119

来源是:

#include <iostream>
#include <malloc.h>
#include <stdio.h>

/* Replace this method with a printf version for C compatibility*/
void print(double **mat, int x, int y)
{
  int r, c;
  for ( r = 0; r < x; r++ ) {
    std::cout << "Row [" << r << "]: ";
    for ( c = 0; c < y; c++ ) {
      std::cout << mat[r][c] << " ";
    }
    std::cout << std::endl;
  }
}

void del(double **mat, int x)
{
  int r = 0;
  for ( r = 0; r < x; r++ ) {
      free( mat[r] );
    }
  free( mat );
}

double** createMatrix(int sizeX, int sizeY, int val=0)
{

  double **matrix;
  matrix = (double **)malloc( sizeX * sizeof( double * ) );
  int i = 0;
  for (i = 0; i < sizeX; i++) {
    matrix[i] = (double *)malloc( sizeY * sizeof( double ) );
    int j = 0;
    for (j = 0; j < sizeY; j++) {
      if ( val == 0 ) {
        matrix[i][j] = 0;
      }
      else {
        matrix[i][j] = i * sizeY + j;
      }
    }
  }
  return matrix;
}

double **multiply(double **a, double **b, int xa, int ya, int xb, int yb)
{
  if ( ya != xb ) {
    printf( "Can't multiply incompatible matrices\n");
    return NULL;
  }

  double **c = createMatrix( xa, yb, 1 );
  int i,j,k;
  for ( i = 0; i < xa; i++ ) {
    for ( j = 0; j < yb; j++ ) {
      for ( k = 0; k < xb; k++ ) {
        c[i][j] += a[i][k] * b[k][j];
      }
    }
  }
  return c;
}

int main(int argc, char *argv[])
{
  double **foo = createMatrix( 3, 3 );
  double **bar = createMatrix( 3, 3 );
  double **res = NULL;

  printf( "Foo: \n" );
  print( foo, 3, 3 );
  printf( "Bar: \n" );
  print( bar, 3, 3 );

  res = multiply( foo, bar, 3, 3, 3, 3);
  printf( "Res: \n" );

  if ( res ) {
    print( res, 3, 3 );
    del( res, 3 );
  }
  else {
    printf("Couldn't multiply see earlier error message!\n");
  }

  del( foo, 3 );
  del( bar, 3 );

  return 0;
}

答案 1 :(得分:1)

检查矩阵A的行长度,矩阵B的列长度在矩阵A和B的乘法之前相等。

答案 2 :(得分:0)

MatrixAllocate返回单位矩阵而不是零矩阵可能会很方便。为此,请将循环有效内容更改为

            matrix[i][j] = i==j? 1: 0;