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\)进 ...
随机推荐
- Python运维开发基础09-函数基础【转】
上节作业回顾 #!/usr/bin/env python3 # -*- coding:utf-8 -*- # author:Mr.chen # 实现简单的shell命令sed的替换功能 import ...
- while(cin>>n1>>n2)
这里有2个点, 1. while(cin>>n)用到了强制类型转换 2. 强调输入遇到-1则退出,说明要一直看是否输入了-1,并记录下来 #include <iostream> ...
- ARX工程必须使用release模式编译
在添加如下代码保证debug版本的arx文件也是使用MFC的release库 // 'DEBUG workaround' below prevents the MFC or ATL #includ ...
- 028_shell脚本递归求值
一. #!/bin/sh factorial() { if [ "$1" -gt "1" ]; then i=`expr $1 - 1` j=`factoria ...
- tcpdump抓取mysql语句
抓包工具tcpdump的使用,抓取具体的sql语句 [root@test7_chat_api_im ~]# tcpdump -s -l - |strings tcpdump: listening on ...
- python-序列化模块
本节内容 前言 json模块 pickle模块 shelve模块 总结 一.前言 1. 现实需求 每种编程语言都有各自的数据类型,其中面向对象的编程语言还允许开发者自定义数据类型(如:自定义类),Py ...
- SQL语句的行列转换
[一]行转列 1,查询原始的数据 /***这次练习的主题,行转列,列转行***/select * from Scores 2,得到姓名,通过group by select Student as '姓名 ...
- oracle导出序列的几种办法
oracle导出序列的几种办法 注:本文来源于<oracle导出序列的几种办法> 方法一: select 'create sequence ' ||sequence_name|| ' mi ...
- Swift 中 insetBy(dx: CGFloat, dy: CGFloat) -> CGRect 用法详解
insetBy(dx: CGFloat, dy: CGFloat) -> CGRect 点击头文件进去 可以发现它是返回的一个CGRect insetBy方法是CGRect 的一个方法 dx后面 ...
- Java的家庭记账本程序(C)
日期:2019.2.4 博客期:029 星期一 今天初步修改了程序,实现了几个基本的功能: 个人信息管理.除查询以外的全部功能!