如何用C语言求一个三阶矩阵的逆矩阵
1个回答
2015-12-17
展开全部
C语言求任意阶矩阵的逆矩阵程序
#include <malloc.h>
#include <stdio.h>
/// <summary>
/// 求行列式绝对值
/// </summary>
/// <param name="src">输入的矩阵</param>
/// <param name="n">矩阵的阶数</param>
/// <returns>矩阵对应行列式的值</returns>
double mat_det(double *src[], const unsigned n){
if (2 > n) return src[0][0];
int subsize = n - 1;
double **subvet = (double **) malloc(sizeof(double *)*subsize);
for (int i = 0; i < subsize; ++i)
subvet[i] = src[i+1];
double value = mat_det(subvet, subsize) * src[0][subsize] * ((subsize & 1) ? -1 : 1);
for (int i = 0; i < subsize; ++i){
subvet[i] = src[i];
value += mat_det(subvet, subsize)*src[i + 1][subsize] * ((n + i & 1) ? -1 : 1);
}
free(subvet);
return value;
}
/// <summary>
/// 求矩阵指定位置元素代数余子式的值
/// </summary>
/// <param name="src">输入的矩阵</param>
/// <param name="n">矩阵的阶数</param>
/// <param name="x">矩阵指定元素坐标的x值</param>
/// <param name="y">矩阵指定元素坐标的y值</param>
/// <returns>矩阵指定元素对应代数余子式的值</returns>
double mat_minor(double *src[], const unsigned n, const unsigned x, const unsigned y){
double **minmat = (double **) malloc(sizeof(double *)*(n - 1));
for (unsigned i = 0; i<n - 1; ++i){
minmat[i] = (double *) malloc(sizeof(double)*(n - 1));
for (unsigned j = 0; j<n - 1; ++j)
minmat[i][j] = src[i + (i >= x)][j + (j >= y)];
}
double value = mat_det(minmat, n - 1) * (x + y & 1 ? -1 : 1);
for (unsigned i = 0; i<n - 1; ++i)
free(minmat[i]);
free(minmat);
return value;
}
/// <summary>
/// 求矩阵的逆矩阵
/// </summary>
/// <param name="src">输入的矩阵</param>
/// <param name="des">输入的矩阵的逆矩阵</param>
/// <param name="n">矩阵的阶数</param>
void mat_inv(double *src [],double *des[],const unsigned n){
double det = mat_det(src, n); //求矩阵的行列式的值
for (unsigned i = 0; i < n; ++i){
for (unsigned j = 0; j < n; ++j)
des[i][j] = mat_minor(src, n, j, i) / det;
}
}
int main(){
double input[][3] = { { 8, 4, 9 }, { 2, 3, 5 }, { 7, 6, 1 } };
double output[][3] = { { 0 }, { 0 }, { 0 } };
double *src[3],*des[3];
//矩阵须转换为指针数组形式
for (int i = 0; i < 3; ++i){
src[i] = input[i];
des[i] = output[i];
}
mat_inv(src,des,3);
for (int i = 0; i < 3; ++i){
for (int j = 0; j < 3; ++j)
printf("%lf\t", des[i][j]);
printf("\n");
}
return 0;
}
#include <malloc.h>
#include <stdio.h>
/// <summary>
/// 求行列式绝对值
/// </summary>
/// <param name="src">输入的矩阵</param>
/// <param name="n">矩阵的阶数</param>
/// <returns>矩阵对应行列式的值</returns>
double mat_det(double *src[], const unsigned n){
if (2 > n) return src[0][0];
int subsize = n - 1;
double **subvet = (double **) malloc(sizeof(double *)*subsize);
for (int i = 0; i < subsize; ++i)
subvet[i] = src[i+1];
double value = mat_det(subvet, subsize) * src[0][subsize] * ((subsize & 1) ? -1 : 1);
for (int i = 0; i < subsize; ++i){
subvet[i] = src[i];
value += mat_det(subvet, subsize)*src[i + 1][subsize] * ((n + i & 1) ? -1 : 1);
}
free(subvet);
return value;
}
/// <summary>
/// 求矩阵指定位置元素代数余子式的值
/// </summary>
/// <param name="src">输入的矩阵</param>
/// <param name="n">矩阵的阶数</param>
/// <param name="x">矩阵指定元素坐标的x值</param>
/// <param name="y">矩阵指定元素坐标的y值</param>
/// <returns>矩阵指定元素对应代数余子式的值</returns>
double mat_minor(double *src[], const unsigned n, const unsigned x, const unsigned y){
double **minmat = (double **) malloc(sizeof(double *)*(n - 1));
for (unsigned i = 0; i<n - 1; ++i){
minmat[i] = (double *) malloc(sizeof(double)*(n - 1));
for (unsigned j = 0; j<n - 1; ++j)
minmat[i][j] = src[i + (i >= x)][j + (j >= y)];
}
double value = mat_det(minmat, n - 1) * (x + y & 1 ? -1 : 1);
for (unsigned i = 0; i<n - 1; ++i)
free(minmat[i]);
free(minmat);
return value;
}
/// <summary>
/// 求矩阵的逆矩阵
/// </summary>
/// <param name="src">输入的矩阵</param>
/// <param name="des">输入的矩阵的逆矩阵</param>
/// <param name="n">矩阵的阶数</param>
void mat_inv(double *src [],double *des[],const unsigned n){
double det = mat_det(src, n); //求矩阵的行列式的值
for (unsigned i = 0; i < n; ++i){
for (unsigned j = 0; j < n; ++j)
des[i][j] = mat_minor(src, n, j, i) / det;
}
}
int main(){
double input[][3] = { { 8, 4, 9 }, { 2, 3, 5 }, { 7, 6, 1 } };
double output[][3] = { { 0 }, { 0 }, { 0 } };
double *src[3],*des[3];
//矩阵须转换为指针数组形式
for (int i = 0; i < 3; ++i){
src[i] = input[i];
des[i] = output[i];
}
mat_inv(src,des,3);
for (int i = 0; i < 3; ++i){
for (int j = 0; j < 3; ++j)
printf("%lf\t", des[i][j]);
printf("\n");
}
return 0;
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
东莞大凡
2024-08-07 广告
2024-08-07 广告
作为东莞市大凡光学科技有限公司的一员,我们深知Matlab圆点标定板在相机标定中的重要性。该标定板通过均匀分布的圆点,帮助精确计算相机参数,优化成像效果。Matlab强大的编程功能,使得我们能够灵活设计标定板,调整圆点大小、数量和分布,以满...
点击进入详情页
本回答由东莞大凡提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询