C#计算矩阵的逆矩阵方法实例分析
作者:北风其凉 时间:2021-11-06 18:24:29
本文实例讲述了C#计算矩阵的逆矩阵方法。分享给大家供大家参考。具体如下:
1.代码思路
1)对矩阵进行合法性检查:矩阵必须为方阵
2)计算矩阵行列式的值(Determinant函数)
3)只有满秩矩阵才有逆矩阵,因此如果行列式的值为0(在代码中以绝对值小于1E-6做判断),则终止函数,报出异常
4)求出伴随矩阵(AdjointMatrix函数)
5)逆矩阵各元素即其伴随矩阵各元素除以矩阵行列式的商
2.函数代码
(注:本段代码只实现了一个思路,可能并不是该问题的最优解)
/// <summary>
/// 求矩阵的逆矩阵
/// </summary>
/// <param name="matrix"></param>
/// <returns></returns>
public static double[][] InverseMatrix(double[][] matrix)
{
//matrix必须为非空
if (matrix == null || matrix.Length == 0)
{
return new double[][] { };
}
//matrix 必须为方阵
int len = matrix.Length;
for (int counter = 0; counter < matrix.Length; counter++)
{
if (matrix[counter].Length != len)
{
throw new Exception("matrix 必须为方阵");
}
}
//计算矩阵行列式的值
double dDeterminant = Determinant(matrix);
if (Math.Abs(dDeterminant) <= 1E-6)
{
throw new Exception("矩阵不可逆");
}
//制作一个伴随矩阵大小的矩阵
double[][] result = AdjointMatrix(matrix);
//矩阵的每项除以矩阵行列式的值,即为所求
for (int i = 0; i < matrix.Length; i++)
{
for (int j = 0; j < matrix.Length; j++)
{
result[i][j] = result[i][j] / dDeterminant;
}
}
return result;
}
/// <summary>
/// 递归计算行列式的值
/// </summary>
/// <param name="matrix">矩阵</param>
/// <returns></returns>
public static double Determinant(double[][] matrix)
{
//二阶及以下行列式直接计算
if (matrix.Length == 0) return 0;
else if (matrix.Length == 1) return matrix[0][0];
else if (matrix.Length == 2)
{
return matrix[0][0] * matrix[1][1] - matrix[0][1] * matrix[1][0];
}
//对第一行使用“加边法”递归计算行列式的值
double dSum = 0, dSign = 1;
for (int i = 0; i < matrix.Length; i++)
{
double[][] matrixTemp = new double[matrix.Length - 1][];
for (int count = 0; count < matrix.Length - 1; count++)
{
matrixTemp[count] = new double[matrix.Length - 1];
}
for (int j = 0; j < matrixTemp.Length; j++)
{
for (int k = 0; k < matrixTemp.Length; k++)
{
matrixTemp[j][k] = matrix[j + 1][k >= i ? k + 1 : k];
}
}
dSum += (matrix[0][i] * dSign * Determinant(matrixTemp));
dSign = dSign * -1;
}
return dSum;
}
/// <summary>
/// 计算方阵的伴随矩阵
/// </summary>
/// <param name="matrix">方阵</param>
/// <returns></returns>
public static double[][] AdjointMatrix(double [][] matrix)
{
//制作一个伴随矩阵大小的矩阵
double[][] result = new double[matrix.Length][];
for (int i = 0; i < result.Length; i++)
{
result[i] = new double[matrix[i].Length];
}
//生成伴随矩阵
for (int i = 0; i < result.Length; i++)
{
for (int j = 0; j < result.Length; j++)
{
//存储代数余子式的矩阵(行、列数都比原矩阵少1)
double[][] temp = new double[result.Length - 1][];
for (int k = 0; k < result.Length - 1; k++)
{
temp[k] = new double[result[k].Length - 1];
}
//生成代数余子式
for (int x = 0; x < temp.Length; x++)
{
for (int y = 0; y < temp.Length; y++)
{
temp[x][y] = matrix[x < i ? x : x + 1][y < j ? y : y + 1];
}
}
//Console.WriteLine("代数余子式:");
//PrintMatrix(temp);
result[j][i] = ((i + j) % 2 == 0 ? 1 : -1) * Determinant(temp);
}
}
//Console.WriteLine("伴随矩阵:");
//PrintMatrix(result);
return result;
}
/// <summary>
/// 打印矩阵
/// </summary>
/// <param name="matrix">待打印矩阵</param>
private static void PrintMatrix(double[][] matrix, string title = "")
{
//1.标题值为空则不显示标题
if (!String.IsNullOrWhiteSpace(title))
{
Console.WriteLine(title);
}
//2.打印矩阵
for (int i = 0; i < matrix.Length; i++)
{
for (int j = 0; j < matrix[i].Length; j++)
{
Console.Write(matrix[i][j] + "\t");
//注意不能写为:Console.Write(matrix[i][j] + '\t');
}
Console.WriteLine();
}
//3.空行
Console.WriteLine();
}
3.Main函数调用
static void Main(string[] args)
{
double[][] matrix = new double[][]
{
new double[] { 1, 2, 3 },
new double[] { 2, 2, 1 },
new double[] { 3, 4, 3 }
};
PrintMatrix(matrix, "原矩阵");
PrintMatrix(AdjointMatrix(matrix), "伴随矩阵");
Console.WriteLine("行列式的值为:" + Determinant(matrix) + '\n');
PrintMatrix(InverseMatrix(matrix), "逆矩阵");
Console.ReadLine();
}
4.执行结果
希望本文所述对大家的C#程序设计有所帮助。
标签:C#,矩阵
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Springboot项目全局异常统一处理案例代码
2021-08-26 10:51:19
ELK搭建线上日志收集系统
2021-11-01 17:34:41
![](https://img.aspxhome.com/file/2023/9/62419_0s.png)
JavaEE开发之SpringMVC中的自定义消息转换器与文件上传
2023-11-24 19:36:02
![](https://img.aspxhome.com/file/2023/4/59504_0s.jpg)
IDEA java出现无效的源发行版14解决方案
2021-06-25 08:50:33
![](https://img.aspxhome.com/file/2023/2/62172_0s.png)
Java实现的计算最大下标距离算法示例
2022-02-09 19:14:37
Java-String类最全汇总(下篇)
2023-11-09 14:45:26
![](https://img.aspxhome.com/file/2023/7/58857_0s.jpg)
Java并发包线程池ThreadPoolExecutor的实现
2022-11-10 09:52:41
![](https://img.aspxhome.com/file/2023/3/64383_0s.jpg)
详解Mybatis是如何解析配置文件的
2023-10-15 23:23:40
![](https://img.aspxhome.com/file/2023/0/61930_0s.png)
Java单元测试Powermockito和Mockito使用总结
2021-11-12 14:59:07
详解Spark Sql在UDF中如何引用外部数据
2021-08-17 14:51:17
![](https://img.aspxhome.com/file/2023/4/64124_0s.jpg)
Java中的BaseTypeHandler自定义类型转换器的使用
2022-03-09 00:34:16
spring boot的拦截器简单使用示例代码
2021-09-29 04:07:07
Java中使用StackWalker和Stream API进行堆栈遍历
2023-04-12 11:29:07
详解Java线程-守护线程与用户线程
2023-11-25 00:17:29
Java中lombok的@Builder注解的解析与简单使用详解
2022-12-29 06:44:55
java之Object类用法实例
2023-11-05 04:14:26
![](https://img.aspxhome.com/file/2023/8/58828_0s.png)
Java文件快速copy复制实例代码
2021-05-27 12:25:22
![](https://img.aspxhome.com/file/2023/7/66077_0s.png)
java图的深度优先遍历实现随机生成迷宫
2023-06-26 06:06:05
![](https://img.aspxhome.com/file/2023/2/62672_0s.jpg)
JSON复杂数据处理之Json树形结构数据转Java对象并存储到数据库的实现
2023-09-17 17:03:59
Android登录注册功能 数据库SQLite验证
2023-10-01 20:58:55
![](https://img.aspxhome.com/file/2023/6/86926_0s.gif)