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\)进 ...
随机推荐
- Unity打包PC端各种屏幕适配,无边框,最小化,显示可拖拽部分
using UnityEngine; using System.Collections; using UnityEngine.EventSystems; //using UnityEngine.Sce ...
- windows网络相关的命令
一.netstat命令 显示协议统计信息和当前 TCP/IP 网络连接. NETSTAT [-a] [-b] [-e] [-f] [-n] [-o] [-p proto] [-r] [-s] [-x] ...
- js垃圾回收(转
和C#.Java一样JavaScript有自动垃圾回收机制,也就是说执行环境会负责管理代码执行过程中使用的内存,在开发过程中就无需考虑内存分配及无用内存的回收问题了.JavaScript垃圾回收的机制 ...
- Mybatis--02
主要内容: 1 输入映射和输出映射 输入参数映射 返回值映射 2 动态sql if where foreach sql片段 3 关联查询 一对一关联 一对多关联 4 整合Spring #{}代表一个占 ...
- $Django importlib与dir知识,手写配置文件, 配置查找顺序 drf分页器&drf版本控制
1 importlib与dir知识 # importlib简介动态导入字符串模块 # 常规导入 from ss.aa import b from ss import a print(b,type(b ...
- 前端 ---client、offset、scroll系列
client.offset.scroll系列 1.client系列 代码如下: <!DOCTYPE html> <html> <head> <meta c ...
- Elasticsearch索引别名、Filtered索引别名、Template
在使用elasticsearch的时候,经常会遇到需要淘汰掉历史数据的场景. 为了方便数据淘汰,并使得数据管理更加灵活,我们经常会以时间为粒度建立索引,例如: 每个月建立一个索引:monthly-20 ...
- Ex 4_10 给定一个有向图G=(V,E),其中边...(bellman-ford算法的应用).._第十二次作业
在bellman-ford算法中,循环n-1(n为顶点个数)次可以找出从源点到其他顶点的最多n-1条边的最短路径,若循环k次则可以找出从源点到其他顶点的最多k条边的最短路径. package org. ...
- 【原创】大叔经验分享(31)CM金丝雀Canary报错
CM金丝雀Canary报错 1 HDFS 金丝雀Canary 测试无法为 /tmp/.cloudera_health_monitoring_canary_files 创建父目录. 2 Hive Met ...
- Python-视图 触发器 事务 存储过程
1.视图2.触发器*** 在某个时间发生了某个事件时 会自动触发一段sql语句3.事务*****4.存储过程***** 5.函数6.备份与恢复*** mysqldump -u -p (库名 [表名] ...