鱼C论坛

 找回密码
 立即注册
查看: 1933|回复: 5

关于逆矩阵求解的算法

[复制链接]
发表于 2020-2-12 13:02:10 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
被人问了如何用c求解方阵的逆矩阵, 思考了好久没有思考出来;

因为问的是5*5以下的,之前考虑了求行列式的死算方法,但代码工程量很大。现在在思考高斯消元或者LU分解的可行性,求问dalao们有没有什么好的思路啊。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-2-12 14:31:04 | 显示全部楼层
可以举个例子吗
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-2-13 21:02:27 | 显示全部楼层
有现成的c代码实现,需要吗?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-2-13 23:14:04 | 显示全部楼层
alexw_h264 发表于 2020-2-13 21:02
有现成的c代码实现,需要吗?

需要~~谢谢dalao;
email 1002644731@qq.com
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-2-15 08:13:36 | 显示全部楼层
https://blog.csdn.net/zjxxyz123/article/details/79049579

/* 矩阵的求逆(借鉴他人) */
/* Uses Gauss-Jordan elimination.

   The elimination procedure works by applying elementary row
   operations to our input matrix until the input matrix is reduced to
   the identity matrix.
   Simultaneously, we apply the same elementary row operations to a
   separate identity matrix to produce the inverse matrix.
   If this makes no sense, read wikipedia on Gauss-Jordan elimination.

   This is not the fastest way to invert matrices, so this is quite
   possibly the bottleneck. */
int destructive_invert_matrix(Matrix input, Matrix output) {
    int i;
    int j;
    int r;
    double scalar;
    double shear_needed;
  assert(input.rows == input.cols);
  assert(input.rows == output.rows);
  assert(input.rows == output.cols);

  set_identity_matrix(output);

  /* Convert input to the identity matrix via elementary row operations.
     The ith pass through this loop turns the element at i,i to a 1
     and turns all other elements in column i to a 0. */

  for ( i = 0; i < input.rows; ++i) {

    if (input.data[i][i] == 0.0) {
      /* We must swap rows to get a nonzero diagonal element. */

      for (r = i + 1; r < input.rows; ++r) {
    if (input.data[r][i] != 0.0) {
      break;
    }
      }
      if (r == input.rows) {
    /* Every remaining element in this column is zero, so this
       matrix cannot be inverted. */
    return 0;
      }
      swap_rows(input, i, r);
      swap_rows(output, i, r);
    }

    /* Scale this row to ensure a 1 along the diagonal.
       We might need to worry about overflow from a huge scalar here. */
     scalar = 1.0 / input.data[i][i];
    scale_row(input, i, scalar);
    scale_row(output, i, scalar);

    /* Zero out the other elements in this column. */
    for ( j = 0; j < input.rows; ++j) {
      if (i == j) {
    continue;
      }
       shear_needed = -input.data[j][i];
      shear_row(input, j, i, shear_needed);
      shear_row(output, j, i, shear_needed);
    }
  }

  return 1;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-2-15 08:14:22 | 显示全部楼层
https://blog.csdn.net/zjxxyz123/article/details/79049579

/* 矩阵的求逆(借鉴他人) */
/* Uses Gauss-Jordan elimination.

   The elimination procedure works by applying elementary row
   operations to our input matrix until the input matrix is reduced to
   the identity matrix.
   Simultaneously, we apply the same elementary row operations to a
   separate identity matrix to produce the inverse matrix.
   If this makes no sense, read wikipedia on Gauss-Jordan elimination.

   This is not the fastest way to invert matrices, so this is quite
   possibly the bottleneck. */
int destructive_invert_matrix(Matrix input, Matrix output) {
    int i;
    int j;
    int r;
    double scalar;
    double shear_needed;
  assert(input.rows == input.cols);
  assert(input.rows == output.rows);
  assert(input.rows == output.cols);

  set_identity_matrix(output);

  /* Convert input to the identity matrix via elementary row operations.
     The ith pass through this loop turns the element at i,i to a 1
     and turns all other elements in column i to a 0. */

  for ( i = 0; i < input.rows; ++i) {

    if (input.data[i][i] == 0.0) {
      /* We must swap rows to get a nonzero diagonal element. */

      for (r = i + 1; r < input.rows; ++r) {
    if (input.data[r][i] != 0.0) {
      break;
    }
      }
      if (r == input.rows) {
    /* Every remaining element in this column is zero, so this
       matrix cannot be inverted. */
    return 0;
      }
      swap_rows(input, i, r);
      swap_rows(output, i, r);
    }

    /* Scale this row to ensure a 1 along the diagonal.
       We might need to worry about overflow from a huge scalar here. */
     scalar = 1.0 / input.data[i][i];
    scale_row(input, i, scalar);
    scale_row(output, i, scalar);

    /* Zero out the other elements in this column. */
    for ( j = 0; j < input.rows; ++j) {
      if (i == j) {
    continue;
      }
       shear_needed = -input.data[j][i];
      shear_row(input, j, i, shear_needed);
      shear_row(output, j, i, shear_needed);
    }
  }

  return 1;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-1-16 03:31

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表