【问题标题】:Issue with bringing a 2d matrix into row echelon form in C using Gaussian Elimination使用高斯消除将 2d 矩阵转换为 C 中的行梯形形式的问题
【发布时间】:2019-05-21 03:45:17
【问题描述】:

我正在尝试使用 C 将矩阵变为行梯形。我遇到的问题是,有时我的代码将允许比率以 0 作为分母。代码sn-p如下。

double **identityMatrix = allocateMatrix(size, size);
double **modifiedMatrix = allocateMatrix(size,size);
identityMatrix = createIdentityMatrix(size);
modifiedMatrix = matrix;
double ratio = 0;
for(int curCol = 0; curCol < size; curCol++) //Bring to row echelon form
{
  for(int curRow = curCol +1; curRow < size; curRow++)
  {
    if(modifiedMatrix[curRow][curCol] != 0)
    {
     int tmp = curRow - 1;
      while(modifiedMatrix[tmp][curCol] == 0)
      {
         tmp--;
      }
      ratio = modifiedMatrix[curRow][curCol]/modifiedMatrix[tmp][curCol];
      modifiedMatrix = subtractRow(modifiedMatrix,curRow, tmp, size, ratio);
      identityMatrix = subtractRow(identityMatrix,curRow, tmp, size, ratio);
   }
  }
}

subtractRow 和 allocateMatrix 一样正确地实现了。矩阵总是保证是可逆的和正方形的。矩阵作为参数传入。问题显然出在我的 0 支票上,但我不确定它有什么问题。我将不胜感激任何帮助。

【问题讨论】:

  • double **identityMatrix = allocateMatrix(size, size); .... identityMatrix = createIdentityMatrix(size); 覆盖了identityMatrix 的初始化。
  • 分配矩阵只是为二维矩阵分配空间,因为我使用双指针来表示二维矩阵。
  • 为什么要用identityMatrix = createIdentityMatrix(size);覆盖它?
  • @chux 这实际上是一个好点,谢谢。但是,我不相信这会导致我除以 0 的问题。
  • 有趣。谷歌搜索“C 第二版中的数字食谱”。您会找到一个可以下载的 PDF,其中包含可靠且稳定的源代码。

标签: c matrix


【解决方案1】:

我最近一直在研究矩阵,将矩阵转换为行梯队的算法很有趣。

你可以查看我的代码here

编码愉快。

【讨论】:

  • 嗨。仅链接的答案在这里不受欢迎。请将代码的重要部分包含在代码块中,并添加一个突出显示的摘要。作为参考,您可以链接外部代码,但如果它变得不可用,您的答案对任何人都没有用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-27
  • 2017-06-14
  • 1970-01-01
相关资源
最近更新 更多