使用math.net数字反转20X20矩阵

时间:2016-01-14 15:09:45

标签: matrix matrix-inverse mathnet

我需要反转20X20矩阵,但结果不正确。

我使用应用程序中的一些值创建了一个测试程序:

using System;
using System.Diagnostics;
using MathNet.Numerics.LinearAlgebra;
using MathNet.Numerics.LinearAlgebra.Double;

namespace MathNetTest
{
  class Program
  {
    static void Main(string[] args)
    {
      Matrix<double> A = new DenseMatrix(20,20) ;

      A[0,0] = 3.3556377856E-06 ;
      A[0,1] = 7.8402752E-05 ;
      A[0,2] = 0.00183184 ;
      A[0,3] = 0.0428 ;
      A[0,4] = 1 ;
      A[0,5] = 0 ;
      A[0,6] = 0 ;
      A[0,7] = 0 ;
      A[0,8] = 0 ;
      A[0,9] = 0 ;
      A[0,10] = 0 ;
      A[0,11] = 0 ;
      A[0,12] = 0 ;
      A[0,13] = 0 ;
      A[0,14] = 0 ;
      A[0,15] = 0 ;
      A[0,16] = 0 ;
      A[0,17] = 0 ;
      A[0,18] = 0 ;
      A[0,19] = 0 ;
      A[1,0] = 0.000313611008 ;
      A[1,1] = 0.00549552 ;
      A[1,2] = 0.0856 ;
      A[1,3] = 1 ;
      A[1,4] = 0 ;
      A[1,5] = 0 ;
      A[1,6] = 0 ;
      A[1,7] = 0 ;
      A[1,8] = 0 ;
      A[1,9] = 0 ;
      A[1,10] = 0 ;
      A[1,11] = 0 ;
      A[1,12] = 0 ;
      A[1,13] = 0 ;
      A[1,14] = 0 ;
      A[1,15] = 0 ;
      A[1,16] = 0 ;
      A[1,17] = 0 ;
      A[1,18] = 0 ;
      A[1,19] = 0 ;
      A[2,0] = 0.02198208 ;
      A[2,1] = 0.2568 ;
      A[2,2] = 2 ;
      A[2,3] = 0 ;
      A[2,4] = 0 ;
      A[2,5] = 0 ;
      A[2,6] = 0 ;
      A[2,7] = 0 ;
      A[2,8] = 0 ;
      A[2,9] = 0 ;
      A[2,10] = 0 ;
      A[2,11] = 0 ;
      A[2,12] = 0 ;
      A[2,13] = 0 ;
      A[2,14] = 0 ;
      A[2,15] = 0 ;
      A[2,16] = 0 ;
      A[2,17] = 0 ;
      A[2,18] = 0 ;
      A[2,19] = 0 ;
      A[3,0] = 1.0272 ;
      A[3,1] = 6 ;
      A[3,2] = 0 ;
      A[3,3] = 0 ;
      A[3,4] = 0 ;
      A[3,5] = 0 ;
      A[3,6] = 0 ;
      A[3,7] = 0 ;
      A[3,8] = 0 ;
      A[3,9] = 0 ;
      A[3,10] = 0 ;
      A[3,11] = 0 ;
      A[3,12] = 0 ;
      A[3,13] = 0 ;
      A[3,14] = 0 ;
      A[3,15] = 0 ;
      A[3,16] = 0 ;
      A[3,17] = 0 ;
      A[3,18] = 0 ;
      A[3,19] = 0 ;
      A[4,0] = -4.5954068161E-06 ;
      A[4,1] = -9.9252847E-05 ;
      A[4,2] = -0.00214369 ;
      A[4,3] = -0.0463 ;
      A[4,4] = -1 ;
      A[4,5] = 4.5954068161E-06 ;
      A[4,6] = 9.9252847E-05 ;
      A[4,7] = 0.00214369 ;
      A[4,8] = 0.0463 ;
      A[4,9] = 1 ;
      A[4,10] = 0 ;
      A[4,11] = 0 ;
      A[4,12] = 0 ;
      A[4,13] = 0 ;
      A[4,14] = 0 ;
      A[4,15] = 0 ;
      A[4,16] = 0 ;
      A[4,17] = 0 ;
      A[4,18] = 0 ;
      A[4,19] = 0 ;
      A[5,0] = -0.000397011388 ;
      A[5,1] = -0.00643107 ;
      A[5,2] = -0.0926 ;
      A[5,3] = -1 ;
      A[5,4] = 0 ;
      A[5,5] = 0.000397011388 ;
      A[5,6] = 0.00643107 ;
      A[5,7] = 0.0926 ;
      A[5,8] = 1 ;
      A[5,9] = 0 ;
      A[5,10] = 0 ;
      A[5,11] = 0 ;
      A[5,12] = 0 ;
      A[5,13] = 0 ;
      A[5,14] = 0 ;
      A[5,15] = 0 ;
      A[5,16] = 0 ;
      A[5,17] = 0 ;
      A[5,18] = 0 ;
      A[5,19] = 0 ;
      A[6,0] = -0.02572428 ;
      A[6,1] = -0.2778 ;
      A[6,2] = -2 ;
      A[6,3] = 0 ;
      A[6,4] = 0 ;
      A[6,5] = 0.02572428 ;
      A[6,6] = 0.2778 ;
      A[6,7] = 2 ;
      A[6,8] = 0 ;
      A[6,9] = 0 ;
      A[6,10] = 0 ;
      A[6,11] = 0 ;
      A[6,12] = 0 ;
      A[6,13] = 0 ;
      A[6,14] = 0 ;
      A[6,15] = 0 ;
      A[6,16] = 0 ;
      A[6,17] = 0 ;
      A[6,18] = 0 ;
      A[6,19] = 0 ;
      A[7,0] = -1.1112 ;
      A[7,1] = -6 ;
      A[7,2] = 0 ;
      A[7,3] = 0 ;
      A[7,4] = 0 ;
      A[7,5] = 1.1112 ;
      A[7,6] = 6 ;
      A[7,7] = 0 ;
      A[7,8] = 0 ;
      A[7,9] = 0 ;
      A[7,10] = 0 ;
      A[7,11] = 0 ;
      A[7,12] = 0 ;
      A[7,13] = 0 ;
      A[7,14] = 0 ;
      A[7,15] = 0 ;
      A[7,16] = 0 ;
      A[7,17] = 0 ;
      A[7,18] = 0 ;
      A[7,19] = 0 ;
      A[8,0] = 0 ;
      A[8,1] = 0 ;
      A[8,2] = 0 ;
      A[8,3] = 0 ;
      A[8,4] = 0 ;
      A[8,5] = -6.1505984016E-06 ;
      A[8,6] = -0.000123505992 ;
      A[8,7] = -0.00248004 ;
      A[8,8] = -0.0498 ;
      A[8,9] = -1 ;
      A[8,10] = 6.1505984016E-06 ;
      A[8,11] = 0.000123505992 ;
      A[8,12] = 0.00248004 ;
      A[8,13] = 0.0498 ;
      A[8,14] = 1 ;
      A[8,15] = 0 ;
      A[8,16] = 0 ;
      A[8,17] = 0 ;
      A[8,18] = 0 ;
      A[8,19] = 0 ;
      A[9,0] = 0 ;
      A[9,1] = 0 ;
      A[9,2] = 0 ;
      A[9,3] = 0 ;
      A[9,4] = 0 ;
      A[9,5] = -0.000494023968 ;
      A[9,6] = -0.00744012 ;
      A[9,7] = -0.0996 ;
      A[9,8] = -1 ;
      A[9,9] = 0 ;
      A[9,10] = 0.000494023968 ;
      A[9,11] = 0.00744012 ;
      A[9,12] = 0.0996 ;
      A[9,13] = 1 ;
      A[9,14] = 0 ;
      A[9,15] = 0 ;
      A[9,16] = 0 ;
      A[9,17] = 0 ;
      A[9,18] = 0 ;
      A[9,19] = 0 ;
      A[10,0] = 0 ;
      A[10,1] = 0 ;
      A[10,2] = 0 ;
      A[10,3] = 0 ;
      A[10,4] = 0 ;
      A[10,5] = -0.02976048 ;
      A[10,6] = -0.2988 ;
      A[10,7] = -2 ;
      A[10,8] = 0 ;
      A[10,9] = 0 ;
      A[10,10] = 0.02976048 ;
      A[10,11] = 0.2988 ;
      A[10,12] = 2 ;
      A[10,13] = 0 ;
      A[10,14] = 0 ;
      A[10,15] = 0 ;
      A[10,16] = 0 ;
      A[10,17] = 0 ;
      A[10,18] = 0 ;
      A[10,19] = 0 ;
      A[11,0] = 0 ;
      A[11,1] = 0 ;
      A[11,2] = 0 ;
      A[11,3] = 0 ;
      A[11,4] = 0 ;
      A[11,5] = -1.1952 ;
      A[11,6] = -6 ;
      A[11,7] = 0 ;
      A[11,8] = 0 ;
      A[11,9] = 0 ;
      A[11,10] = 1.1952 ;
      A[11,11] = 6 ;
      A[11,12] = 0 ;
      A[11,13] = 0 ;
      A[11,14] = 0 ;
      A[11,15] = 0 ;
      A[11,16] = 0 ;
      A[11,17] = 0 ;
      A[11,18] = 0 ;
      A[11,19] = 0 ;
      A[12,0] = 0 ;
      A[12,1] = 0 ;
      A[12,2] = 0 ;
      A[12,3] = 0 ;
      A[12,4] = 0 ;
      A[12,5] = 0 ;
      A[12,6] = 0 ;
      A[12,7] = 0 ;
      A[12,8] = 0 ;
      A[12,9] = 0 ;
      A[12,10] = -8.0706559921E-06 ;
      A[12,11] = -0.000151419437 ;
      A[12,12] = -0.00284089 ;
      A[12,13] = -0.0533 ;
      A[12,14] = -1 ;
      A[12,15] = 8.0706559921E-06 ;
      A[12,16] = 0.000151419437 ;
      A[12,17] = 0.00284089 ;
      A[12,18] = 0.0533 ;
      A[12,19] = 1 ;
      A[13,0] = 0 ;
      A[13,1] = 0 ;
      A[13,2] = 0 ;
      A[13,3] = 0 ;
      A[13,4] = 0 ;
      A[13,5] = 0 ;
      A[13,6] = 0 ;
      A[13,7] = 0 ;
      A[13,8] = 0 ;
      A[13,9] = 0 ;
      A[13,10] = -0.000605677748 ;
      A[13,11] = -0.00852267 ;
      A[13,12] = -0.1066 ;
      A[13,13] = -1 ;
      A[13,14] = 0 ;
      A[13,15] = 0.000605677748 ;
      A[13,16] = 0.00852267 ;
      A[13,17] = 0.1066 ;
      A[13,18] = 1 ;
      A[13,19] = 0 ;
      A[14,0] = 0 ;
      A[14,1] = 0 ;
      A[14,2] = 0 ;
      A[14,3] = 0 ;
      A[14,4] = 0 ;
      A[14,5] = 0 ;
      A[14,6] = 0 ;
      A[14,7] = 0 ;
      A[14,8] = 0 ;
      A[14,9] = 0 ;
      A[14,10] = -0.03409068 ;
      A[14,11] = -0.3198 ;
      A[14,12] = -2 ;
      A[14,13] = 0 ;
      A[14,14] = 0 ;
      A[14,15] = 0.03409068 ;
      A[14,16] = 0.3198 ;
      A[14,17] = 2 ;
      A[14,18] = 0 ;
      A[14,19] = 0 ;
      A[15,0] = 0 ;
      A[15,1] = 0 ;
      A[15,2] = 0 ;
      A[15,3] = 0 ;
      A[15,4] = 0 ;
      A[15,5] = 0 ;
      A[15,6] = 0 ;
      A[15,7] = 0 ;
      A[15,8] = 0 ;
      A[15,9] = 0 ;
      A[15,10] = -1.2792 ;
      A[15,11] = -6 ;
      A[15,12] = 0 ;
      A[15,13] = 0 ;
      A[15,14] = 0 ;
      A[15,15] = 1.2792 ;
      A[15,16] = 6 ;
      A[15,17] = 0 ;
      A[15,18] = 0 ;
      A[15,19] = 0 ;
      A[16,0] = 0 ;
      A[16,1] = 0 ;
      A[16,2] = 0 ;
      A[16,3] = 0 ;
      A[16,4] = 0 ;
      A[16,5] = 0 ;
      A[16,6] = 0 ;
      A[16,7] = 0 ;
      A[16,8] = 0 ;
      A[16,9] = 0 ;
      A[16,10] = 0 ;
      A[16,11] = 0 ;
      A[16,12] = 0 ;
      A[16,13] = 0 ;
      A[16,14] = 0 ;
      A[16,15] = 3.3556377856E-06 ;
      A[16,16] = 7.8402752E-05 ;
      A[16,17] = 0.00183184 ;
      A[16,18] = 0.0428 ;
      A[16,19] = 1 ;
      A[17,0] = 0 ;
      A[17,1] = 0 ;
      A[17,2] = 0 ;
      A[17,3] = 0 ;
      A[17,4] = 0 ;
      A[17,5] = 0 ;
      A[17,6] = 0 ;
      A[17,7] = 0 ;
      A[17,8] = 0 ;
      A[17,9] = 0 ;
      A[17,10] = 0 ;
      A[17,11] = 0 ;
      A[17,12] = 0 ;
      A[17,13] = 0 ;
      A[17,14] = 0 ;
      A[17,15] = 0.000313611008 ;
      A[17,16] = 0.00549552 ;
      A[17,17] = 0.0856 ;
      A[17,18] = 1 ;
      A[17,19] = 0 ;
      A[18,0] = 0 ;
      A[18,1] = 0 ;
      A[18,2] = 0 ;
      A[18,3] = 0 ;
      A[18,4] = 0 ;
      A[18,5] = 0 ;
      A[18,6] = 0 ;
      A[18,7] = 0 ;
      A[18,8] = 0 ;
      A[18,9] = 0 ;
      A[18,10] = 0 ;
      A[18,11] = 0 ;
      A[18,12] = 0 ;
      A[18,13] = 0 ;
      A[18,14] = 0 ;
      A[18,15] = 0.02198208 ;
      A[18,16] = 0.2568 ;
      A[18,17] = 2 ;
      A[18,18] = 0 ;
      A[18,19] = 0 ;
      A[19,0] = 0 ;
      A[19,1] = 0 ;
      A[19,2] = 0 ;
      A[19,3] = 0 ;
      A[19,4] = 0 ;
      A[19,5] = 0 ;
      A[19,6] = 0 ;
      A[19,7] = 0 ;
      A[19,8] = 0 ;
      A[19,9] = 0 ;
      A[19,10] = 0 ;
      A[19,11] = 0 ;
      A[19,12] = 0 ;
      A[19,13] = 0 ;
      A[19,14] = 0 ;
      A[19,15] = 1.0272 ;
      A[19,16] = 6 ;
      A[19,17] = 0 ;
      A[19,18] = 0 ;
      A[19,19] = 0 ;

      double  det = A.Determinant() ;
      Matrix<double> B = A.Inverse() ;

      Matrix<double> I1 = B.Multiply(A) ;

      for ( int i1 = 0 ; i1 < 20 ; i1++ )
      {
        for ( int i2 = 0 ; i2 < 20 ; i2++ )
        {
          Debug.Write ( String.Format("{0:f20} ", I1[i1,i2]) ) ;
        }
        Debug.WriteLine("") ;
      }
    }
  }
}

矩阵的行列式不为零。

当我将矩阵与其假设的逆相乘时,结果当然应该是一个单位矩阵,但事实并非如此。

我做错了吗?

这是将A * B乘以格式{0:f4}

的结果
3.0516 29.8308 403.7012 4502.0000 0.0000 0.3089 11.5556 237.5566 2906.0000 0.0000 1.5993 13.8137 -143.7188 -6196.0000 -185344.0000 -4.0366 -58.6136 -757.0625 -7344.0000 0.0000
-0.3510 -4.0969 -69.5088 -768.2500 0.0000 -0.0526 -1.9899 -40.7600 -502.5000 0.0000 -0.2736 -2.3473 23.8477 1050.0000 31616.0000 0.6906 10.0144 130.0352 1267.0000 0.0000
0.0228 0.3309 5.5107 50.7500 0.0000 0.0032 0.1254 2.5747 31.2500 0.0000 0.0175 0.1500 -1.5508 -68.0000 -2048.0000 -0.0442 -0.6423 -8.3008 -81.0000 0.0000
-0.0006 -0.0094 -0.1284 -0.4375 0.0000 -0.0001 -0.0037 -0.0748 -0.9063 0.0000 -0.0005 -0.0040 0.0499 2.0313 60.0000 0.0012 0.0182 0.2345 2.2813 0.0000
0.0000 0.0001 0.0011 0.0118 1.0000 0.0000 0.0000 0.0006 0.0082 0.0000 0.0000 0.0000 -0.0009 -0.0286 -0.7500 0.0000 -0.0002 -0.0020 -0.0192 0.0000
-2.6921 -51.6272 -833.1016 -9718.0000 0.0000 -3.0679 -54.7289 -654.8184 -6060.0000 0.0000 -2.0779 -18.6694 -73.3594 7480.0000 300032.0000 9.4324 138.8463 1813.2656 17688.0000 0.0000
0.5280 9.9864 160.6318 1869.0000 0.0000 0.7568 11.2792 124.2202 1164.2500 0.0000 0.4065 3.6434 11.8789 -1495.0000 -58240.0000 -1.8032 -26.5348 -346.0117 -3378.0000 0.0000
-0.0386 -0.7200 -11.5379 -133.6250 0.0000 -0.0529 -0.7244 -7.8596 -84.0938 0.0000 -0.0297 -0.2653 -0.6511 108.6875 4208.0000 0.1288 1.8943 24.7454 241.4375 0.0000
0.0012 0.0230 0.3657 4.2476 0.0000 0.0016 0.0227 0.2800 3.6421 0.0000 0.0010 0.0086 0.0187 -3.4785 -134.2500 -0.0041 -0.0600 -0.7838 -7.6426 0.0000
0.0000 -0.0003 -0.0046 -0.0540 0.0000 0.0000 -0.0003 -0.0035 -0.0333 1.0000 0.0000 -0.0001 -0.0006 0.0354 1.5000 0.0001 0.0007 0.0098 0.0963 0.0000
2.2159 32.5541 447.8652 4992.0000 0.0000 0.4233 13.3315 260.0723 3196.0000 0.0000 3.6278 30.1333 123.3438 -2092.0000 -123904.0000 -5.2220 -76.3832 -1004.1719 -9940.0000 0.0000
-0.4501 -6.7982 -95.6135 -1070.5000 0.0000 -0.1371 -3.2576 -57.6484 -670.7500 0.0000 -0.5277 -5.0465 -26.8984 438.0000 26368.0000 1.1128 16.3116 213.9023 2119.0000 0.0000
0.0345 0.5310 7.6026 85.4688 0.0000 0.0140 0.2881 4.7319 53.7969 0.0000 0.0401 0.4588 3.1641 -35.5625 -2112.0000 -0.0889 -1.3052 -17.1086 -169.0000 0.0000
-0.0012 -0.0186 -0.2685 -3.0322 0.0000 -0.0006 -0.0110 -0.1729 -1.9248 0.0000 -0.0014 -0.0156 -0.0786 2.2617 75.0000 0.0032 0.0464 0.6079 5.9980 0.0000
0.0000 0.0002 0.0036 0.0406 0.0000 0.0000 0.0002 0.0023 0.0255 0.0000 0.0000 0.0002 0.0011 -0.0169 0.0000 0.0000 -0.0006 -0.0081 -0.0800 0.0000
0.5480 3.9908 14.1191 -4.0000 0.0000 -1.1027 -9.8011 -53.9141 -24.0000 0.0000 0.7832 9.6061 -12.8750 24.0000 0.0000 0.9848 0.0848 1.1563 -8.0000 0.0000
-0.0930 -0.6594 -2.1245 9.7500 0.0000 0.1894 1.6800 8.6348 -0.5000 0.0000 -0.1339 -1.6457 2.9570 -1.0000 0.0000 0.0029 0.9727 -0.6367 -2.0000 0.0000
0.0060 0.0438 0.1286 -0.2344 0.0000 -0.0122 -0.1075 -0.5685 -0.2500 0.0000 0.0087 0.1047 -0.1809 0.6250 0.0000 -0.0002 0.0011 1.0481 -0.3750 0.0000
-0.0002 -0.0012 -0.0043 -0.0103 0.0000 0.0003 0.0031 0.0167 0.0059 0.0000 -0.0002 -0.0030 0.0046 0.0117 0.0000 0.0000 -0.0001 -0.0014 1.0078 0.0000
0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 -0.0002 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 -0.0001 1.0000

1 个答案:

答案 0 :(得分:1)

我用this C# Matrix Library尝试了你的号码。决定程序例程抛出一个异常并抱怨你的矩阵接近单数。

要检查矩阵反演能力,您可以对矩阵求平方并计算结果的倒数。