博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C#矩阵求逆
阅读量:5172 次
发布时间:2019-06-13

本文共 3798 字,大约阅读时间需要 12 分钟。

来源:

private double[,] ReverseMatrix( double[,] dMatrix, int Level ){    double dMatrixValue = MatrixValue( dMatrix, Level );    if( dMatrixValue == 0 ) return null;               double[,] dReverseMatrix = new double[Level,2*Level];    double x, c;    // Init Reverse matrix    for( int i = 0; i < Level; i++ )    {        for( int j = 0; j < 2 * Level; j++ )        {            if( j < Level )                dReverseMatrix[i,j] = dMatrix[i,j];            else                dReverseMatrix[i,j] = 0;        }         dReverseMatrix[i,Level + i ] = 1;    }     for( int i = 0, j = 0; i < Level && j < Level; i++, j++ )    {        if( dReverseMatrix[i,j] == 0 )        {            int m = i;            for( ; dMatrix[m,j] == 0; m++ );            if( m == Level )                return null;            else            {                // Add i-row with m-row                for( int n = j; n < 2 * Level; n++ )                    dReverseMatrix[i,n] += dReverseMatrix[m,n];            }        }         // Format the i-row with "1" start        x = dReverseMatrix[i,j];        if( x != 1 )        {            for( int n = j; n < 2 * Level; n++ )                if( dReverseMatrix[i,n] != 0 )                    dReverseMatrix[i,n] /= x;        }         // Set 0 to the current column in the rows after current row        for( int s = Level - 1; s > i;s-- )        {            x = dReverseMatrix[s,j];            for( int t = j; t < 2 * Level; t++ )                dReverseMatrix[s,t] -= ( dReverseMatrix[i,t]* x );        }    }     // Format the first matrix into unit-matrix    for( int i = Level - 2; i >= 0; i-- )    {        for( int j = i + 1 ; j < Level; j++ )            if( dReverseMatrix[i,j] != 0 )            {                c = dReverseMatrix[i,j];                for( int n = j; n < 2*Level; n++ )                    dReverseMatrix[i,n] -= ( c * dReverseMatrix[j,n] );            }    }     double[,] dReturn = new double[Level, Level];    for( int i = 0; i < Level; i++ )        for( int j = 0; j < Level; j++ )            dReturn[i,j] = dReverseMatrix[i,j+Level];    return dReturn;} private double MatrixValue( double[,] MatrixList, int Level ){    double[,] dMatrix = new double[Level, Level];    for( int i = 0; i < Level; i++ )        for( int j = 0; j < Level; j++ )            dMatrix[i,j] = MatrixList[i,j];    double c, x;    int k = 1;    for( int i = 0, j = 0; i < Level && j < Level; i++, j++ )    {        if( dMatrix[i,j] == 0 )        {            int m = i;            for( ; dMatrix[m,j] == 0; m++ );            if( m == Level )                return 0;            else            {                // Row change between i-row and m-row                for( int n = j; n < Level; n++ )                {                    c = dMatrix[i,n];                    dMatrix[i,n] = dMatrix[m,n];                    dMatrix[m,n] = c;                }                 // Change value pre-value                k *= (-1);            }        }         // Set 0 to the current column in the rows after current row        for( int s = Level - 1; s > i;s-- )        {            x = dMatrix[s,j];            for( int t = j; t < Level; t++ )                dMatrix[s,t] -= dMatrix[i,t]* ( x/dMatrix[i,j] );        }    }     double sn = 1;    for( int i = 0; i < Level; i++ )    {        if( dMatrix[i,i] != 0 )            sn *= dMatrix[i,i];        else            return 0;    }    return k*sn;}
调用如下:
double[,] dMatrix = new double[3,3]{
{
0,1,2},{
1,0,1},{
4,2,1}}; double[,] dReturn = ReverseMatrix( dMatrix, 3 ); if( dReturn != null ) { for( int i=0; i < 3; i++ ) Debug.WriteLine( string.Format( "{0} {1} {2}", dReturn[i,0], dReturn[i,1],dReturn[i,2] ) ); }

 

转载于:https://www.cnblogs.com/144823836yj/p/5519540.html

你可能感兴趣的文章
2016集训测试赛(二十)Problem B: 字典树
查看>>
中文保存在properties乱码的解决
查看>>
poj题目分类
查看>>
idea 配置mybatis Generator 不显示的解决方案 和 配置MBG
查看>>
英语生疏了,每日至少一句吧
查看>>
创建打不开文件夹
查看>>
完成登录功能,用session记住用户名
查看>>
12 for循环
查看>>
redis(hash篇)
查看>>
Scala实战高手****第12课:Scala函数式编程进阶(匿名函数、高阶函数、函数类型推断、Currying)与Spark源码鉴赏...
查看>>
Hibernate一对多关联
查看>>
python 把函数作为参数 ---高阶函数
查看>>
jQuery + ashx 实现图片按比例预览、异步上传及显示
查看>>
android 代码中使用textAppearance
查看>>
【iOS】UITableViewDelegate 方法没有调用
查看>>
解决code::blocks 17.12不能debug的方法
查看>>
bzoj2961&&bzoj4140 共点圆
查看>>
96:经典实例,判断那一条是闰年:
查看>>
upsource初探
查看>>
让SVN自动更新代码注释中的版本号
查看>>