关于逆矩阵求解的算法
被人问了如何用c求解方阵的逆矩阵, 思考了好久没有思考出来;因为问的是5*5以下的,之前考虑了求行列式的死算方法,但代码工程量很大。现在在思考高斯消元或者LU分解的可行性,求问dalao们有没有什么好的思路啊。 可以举个例子吗 有现成的c代码实现,需要吗? alexw_h264 发表于 2020-2-13 21:02
有现成的c代码实现,需要吗?
需要~~谢谢dalao;
email 1002644731@qq.com 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 == 0.0) {
/* We must swap rows to get a nonzero diagonal element. */
for (r = i + 1; r < input.rows; ++r) {
if (input.data != 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;
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;
shear_row(input, j, i, shear_needed);
shear_row(output, j, i, shear_needed);
}
}
return 1;
}
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 == 0.0) {
/* We must swap rows to get a nonzero diagonal element. */
for (r = i + 1; r < input.rows; ++r) {
if (input.data != 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;
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;
shear_row(input, j, i, shear_needed);
shear_row(output, j, i, shear_needed);
}
}
return 1;
}
页:
[1]