C# 矩阵求逆

一、矩阵求逆算法


本次实现矩阵求逆的算法为伴随矩阵法

代数余子式求逆矩阵:如果矩阵A可逆,则
A − 1 = A ∗ ∣ A ∣ A^{-1} = \frac{A^{*}}{|A|} A1=AA
(|A| != 0) ,|A|为该矩阵对应的行列式的值

设三阶矩阵A
A = [ a b c d e f g h i ] A=\left[ \begin{matrix} a & b & c \\ d & e & f \\ g & h & i \end{matrix} \right] A=adgbehcfi

1、首先求出伴随矩阵
A ∗ = [ e i − h f h c − b i b f − c e f g − i d i a − c g c d − a f d h − g e g b − a h a e − b d ] A^{*}=\left[ \begin{matrix} ei-hf & hc-bi & bf-ce \\ fg-id & ia-cg & cd-af \\ dh-ge & gb-ah & ae-bd \end{matrix} \right] A=eihffgiddhgehcbiiacggbahbfcecdafaebd
将矩阵 A 中的数值带入即可得出伴随矩阵

2、求出其行列式的值
∣ A ∣ = a e i + b f g + c d h − c e g − b d i − a f h |A| = aei + bfg+cdh-ceg-bdi-afh A=aei+bfg+cdhcegbdiafh
3、将结果带入式
A − 1 = A ∗ ∣ A ∣ A^{-1} = \frac{A^{*}}{|A|} A1=AA
中即可得出矩阵的逆


二、矩阵求逆的代码实现

1、按照上述方式,首先求出矩阵的伴随矩阵

首先定义一个3X3的二维数组表示一个矩阵

 double[,] input = new double[3, 3];   //初始矩阵
 double[,] output = new double[3, 3];  //矩阵的逆

———求出伴随矩阵——

 output[0, 0] = input[2, 2] * input[1, 1] - input[2, 1] * input[1, 2];
 output[0, 1] = input[2, 1] * input[0, 2] - input[0, 1] * input[2, 2];
 output[0, 2] = input[0, 1] * input[1, 2] - input[0, 2] * input[1, 1];

 output[1, 0] = input[1, 2] * input[2, 0] - input[2, 2] * input[1, 0];
 output[1, 1] = input[2, 2] * input[0, 0] - input[0, 2] * input[2, 0];
 output[1, 2] = input[0, 2] * input[1, 0] - input[0, 0] * input[1, 2];

 output[2, 0] = input[1, 0] * input[2, 1] - input[2, 0] * input[1, 1];
 output[2, 1] = input[2, 0] * input[0, 1] - input[0, 0] * input[2, 1];
 output[2, 2] = input[0, 0] * input[1, 1] - input[1, 0] * input[0, 1];

———————求出行列式的值———————

 double Avalue   = input[0, 0] * input[1, 1] * input[2, 2] 
 				+ input[0, 1] * input[1, 2] * input[2, 0] 
				+ input[0, 2] * input[1, 0] * input[2, 1]
				- input[0, 2] * input[1, 1] * input[2, 0] 
				- input[0, 1] * input[1, 0] * input[2, 2] 
			    - input[0, 0] * input[1, 2] * input[2, 1];

———————求出 逆矩阵 ———————

            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    output[i, j] = output[i, j] / Avalue;
                }
            }

得出 output[ i , j ] 即为**矩阵的逆**

以下完整代码

public static double[,] inv3(double[,] input)
        {

            double[,] output = new double[3, 3];

            output[0, 0] = input[2, 2] * input[1, 1] - input[2, 1] * input[1, 2];
            output[0, 1] = input[2, 1] * input[0, 2] - input[0, 1] * input[2, 2];
            output[0, 2] = input[0, 1] * input[1, 2] - input[0, 2] * input[1, 1];

            output[1, 0] = input[1, 2] * input[2, 0] - input[2, 2] * input[1, 0];
            output[1, 1] = input[2, 2] * input[0, 0] - input[0, 2] * input[2, 0];
            output[1, 2] = input[0, 2] * input[1, 0] - input[0, 0] * input[1, 2];

            output[2, 0] = input[1, 0] * input[2, 1] - input[2, 0] * input[1, 1];
            output[2, 1] = input[2, 0] * input[0, 1] - input[0, 0] * input[2, 1];
            output[2, 2] = input[0, 0] * input[1, 1] - input[1, 0] * input[0, 1];

            double Avalue = input[0, 0] * input[1, 1] * input[2, 2]
                + input[0, 1] * input[1, 2] * input[2, 0]
               + input[0, 2] * input[1, 0] * input[2, 1]
               - input[0, 2] * input[1, 1] * input[2, 0]
               - input[0, 1] * input[1, 0] * input[2, 2]
               - input[0, 0] * input[1, 2] * input[2, 1];

            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    output[i, j] = output[i, j] / Avalue;
                }
            }
            return output;

        }
  • 3
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

未来超低端科技研究所

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值