C#矩阵求逆
private double[,] ReverseMatrix( double[,] dMatrix, int Level )
{
double dMatrixValue = MatrixValue( dMatrix, Level );
if( dMatrixValue == ) return null; double[,] dReverseMatrix = new double[Level,*Level];
double x, c;
// Init Reverse matrix
for( int i = ; i < Level; i++ )
{
for( int j = ; j < * Level; j++ )
{
if( j < Level )
dReverseMatrix[i,j] = dMatrix[i,j];
else
dReverseMatrix[i,j] = ;
} dReverseMatrix[i,Level + i ] = ;
} for( int i = , j = ; i < Level && j < Level; i++, j++ )
{
if( dReverseMatrix[i,j] == )
{
int m = i;
for( ; dMatrix[m,j] == ; m++ );
if( m == Level )
return null;
else
{
// Add i-row with m-row
for( int n = j; n < * Level; n++ )
dReverseMatrix[i,n] += dReverseMatrix[m,n];
}
} // Format the i-row with "1" start
x = dReverseMatrix[i,j];
if( x != )
{
for( int n = j; n < * Level; n++ )
if( dReverseMatrix[i,n] != )
dReverseMatrix[i,n] /= x;
} // Set 0 to the current column in the rows after current row
for( int s = Level - ; s > i;s-- )
{
x = dReverseMatrix[s,j];
for( int t = j; t < * Level; t++ )
dReverseMatrix[s,t] -= ( dReverseMatrix[i,t]* x );
}
} // Format the first matrix into unit-matrix
for( int i = Level - ; i >= ; i-- )
{
for( int j = i + ; j < Level; j++ )
if( dReverseMatrix[i,j] != )
{
c = dReverseMatrix[i,j];
for( int n = j; n < *Level; n++ )
dReverseMatrix[i,n] -= ( c * dReverseMatrix[j,n] );
}
} double[,] dReturn = new double[Level, Level];
for( int i = ; i < Level; i++ )
for( int j = ; 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 = ; i < Level; i++ )
for( int j = ; j < Level; j++ )
dMatrix[i,j] = MatrixList[i,j];
double c, x;
int k = ;
for( int i = , j = ; i < Level && j < Level; i++, j++ )
{
if( dMatrix[i,j] == )
{
int m = i;
for( ; dMatrix[m,j] == ; m++ );
if( m == Level )
return ;
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 *= (-);
}
} // Set 0 to the current column in the rows after current row
for( int s = Level - ; 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 = ;
for( int i = ; i < Level; i++ )
{
if( dMatrix[i,i] != )
sn *= dMatrix[i,i];
else
return ;
}
return k*sn;
}
调用如下:
double[,] dMatrix = new double[,]{{,,},{,,},{,,}};
double[,] dReturn = ReverseMatrix( dMatrix, );
if( dReturn != null )
{
for( int i=; i < ; i++ )
Debug.WriteLine( string.Format( "{0} {1} {2}",
dReturn[i,], dReturn[i,],dReturn[i,] ) );
}
C#矩阵求逆的更多相关文章
- 矩阵求逆算法及程序实现(C++)
在做课题时,遇到了求多项式问题,利用了求逆方法.矩阵求逆一般使用简单的算法,还有快速算法 如全选主元高斯-约旦消元法,但本文程序主要写了简单的矩阵求逆算法定义法之伴随矩阵求逆公式如下,其中A可逆: , ...
- matrix矩阵求逆 与解方程模板 留做备用 (有bug,待补充)
// // main.cpp // 矩阵求逆 // // Created by 唐 锐 on 13-6-20. // Copyright (c) 2013年 唐 锐. All rights reser ...
- 矩阵求逆的几种方法总结(C++)
矩阵求逆运算有多种算法: 伴随矩阵的思想,分别算出其伴随矩阵和行列式,再算出逆矩阵: LU分解法(若选主元即为LUP分解法: Ax = b ==> PAx = Pb ==>LUx = Pb ...
- RLS自适应滤波器中用矩阵求逆引理来避免求逆运算
在RLS自适应滤波器的实现过程中,难免不涉及矩阵的求逆运算.而求逆操作双是非常耗时的,一个很自然的想法就是尽可能的避免直接对矩阵进行求逆运算.那么,在RLS自适应滤波器的实现中,有没有一种方法能避免直 ...
- 矩阵求逆·学习笔记 $\times$ [$LuoguP4783$]矩阵求逆
哦?今天在\(luogu\)上fa♂现了矩阵求逆的板子--于是就切了切. 那么我们考虑一个矩阵\(A\),它的逆矩阵记作\(A^{-1}\),其中对于矩阵这个群来讲,会有\(A \cdot A^{-1 ...
- 【题解】Matrix BZOJ 4128 矩阵求逆 离散对数 大步小步算法
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=4128 大水题一道 使用大步小步算法,把数字的运算换成矩阵的运算就好了 矩阵求逆?这么基础的线 ...
- BZOJ 4128 Matrix BSGS+矩阵求逆
题意:链接 方法: BSGS+矩阵求逆 解析: 这题就是把Ax=B(mod C)的A和B换成了矩阵. 然而别的地方并没有修改. 所以就涉及到矩阵的逆元这个问题. 矩阵的逆元怎么求呢? 先在原矩阵后接一 ...
- BZOJ 1444 [JSOI2009]有趣的游戏 (Trie图/AC自动机+矩阵求逆)
题目大意:给你$N$个长度相等且互不相同的模式串,现在有一个字符串生成器会不断生成字符,其中每个字符出现的概率是$p_{i}/q_{i}$,当生成器生成的字符串包含了某个模式串,则拥有该模式串的玩家胜 ...
- LG4783 【模板】矩阵求逆
P4783 [模板]矩阵求逆 题目描述 求一个$N\times N$的矩阵的逆矩阵.答案对$10^9+7$取模. 输入输出格式 输入格式: 第一行有一个整数$N$,代表矩阵的大小: 从第$2$行到第$ ...
- LUOGU P4783 【模板】矩阵求逆(高斯消元)
传送门 解题思路 用高斯消元对矩阵求逆,设\(A*B=C\),\(C\)为单位矩阵,则\(B\)为\(A\)的逆矩阵.做法是把\(B\)先设成单位矩阵,然后对\(A\)做高斯消元的过程,对\(B\)进 ...
随机推荐
- 【转】数据库介绍(MySQL安装 体系结构、基本管理)
[转]数据库介绍(MySQL安装 体系结构.基本管理) 第1章 数据库介绍及mysql安装 1.1 数据库简介 数据库,简而言之可视为电子化的文件柜——存储电子文件的处所,用户可以对文件中的数据运行新 ...
- Cascade R-CNN论文讲解(转载)
转载链接:https://blog.csdn.net/qq_21949357/article/details/80046867 论文思想:为了解决IOU设置带来的最终的AP值,作者引入了cascade ...
- TreeGrid 控件集 :delphi 学习群 ---- 166637277 (Delphi学习交流与分享)
delphi 学习群: 166637277 (Delphi学习交流与分享). 群主QQ: 1936431438 TreeGrid 控件集 收集: 1.https://www.lmd.de/produ ...
- 【转】浅析SkipList跳跃表原理及代码实现
SkipList在Leveldb以及lucence中都广为使用,是比较高效的数据结构.由于它的代码以及原理实现的简单性,更为人们所接受.首先看看SkipList的定义,为什么叫跳跃表? "S ...
- webstorm加载项目卡死在scanning files to index
今天用webstorm导入项目时,需要加载node-modules文件夹,导致webstorm非常卡,页面提示scanning files to index... 网上搜到办法,记录下: 说明: 在n ...
- HDU 1074 (DP + 状态压缩)
题意: 给你N个课程, 每个课程有结束的时间 , 和完成这门课程需要的时间 超过课程结束ed时间,每一天就要花费 1点绩点: 然后要求你安排如何做课程使得花费的绩点最少 (看了博客后才发现状态压缩很⑥ ...
- destoon使用
使用小计 1.判断是否是手机端 {$DT_TOUCH}模板中使用 2.判断句 {if} {/if} 3.表单管理 扩展功能-----表单管理:添加表单---->管理表单选项------> ...
- 在 Confluence 中启用 HTTP 响应压缩
Confluence 能够支持 HTTP 的 GZip 传输编码.这个意味着 Confluence 将可以把数据压缩后传输给用户,这种配置能够针对不稳定的互联网状态下的传输速度缓慢和不稳定并且能够降低 ...
- Confluence 6 查看空间活动
空间活动信息是默认禁用(disabled by default)的.活动(Activity)的标没有显示,如果你的 Confluence Usage Stats 插件没有启用的.请查看下面的说明: ...
- Confluence 6 附件存储选项
在早期的 Confluence 版本中,我们允许存储附件到 WebDav 或者 Confluence 数据库中.针对新的 Confluence 安装,我们不再支持这 2 种存储了. 本地文件系统 在默 ...