C#矩阵求逆

来源:http://zhidao.baidu.com/link?url=DiqAbq9YUYn3z7QjxGGoF0PLZwN-Y9ecqKB7Gy38JWRD1riMIYukVKXKq88pxtWLwIl6-iOUf2p3liE51phEe_

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] ) );
    }
时间: 2024-08-05 07:04:19

C#矩阵求逆的相关文章

由正交矩阵构建的仿射变换矩阵求逆的快速算法

原文地址http://blog.csdn.net/i_dovelemon/article/details/45827953 齐次坐标 我们都知道,在3D图形学中,所有的变换都可以划分为三种最基础的变换方式,分别为: 旋转变换 缩放变换 平移变换 通过对这三种变换进行组合,就能够实现任意的变换形式. 在3D坐标下,如果向量使用3D向量表示的话,对于这三种变换的处理方式如下: 旋转变换:乘法运算 缩放变换:乘法运算 平移变换:加法运算 也就是说,这三种变换的处理方式是不同的,旋转和缩放变换能够通过乘

矩阵求逆算法及程序实现(C++)

在做课题时,遇到了求多项式问题,利用了求逆方法.矩阵求逆一般使用简单的算法,还有快速算法 如全选主元高斯-约旦消元法,但本文程序主要写了简单的矩阵求逆算法定义法之伴随矩阵求逆公式如下,其中A可逆: ,其中是的伴随矩阵.. 1.给定一个方阵,非奇异(不是也可,程序有考虑): 2.由矩阵得到其行列式,求其值如|A|: 3.求其伴随矩阵: 4.得到其逆矩阵. 主要函数如下: 1 //得到给定矩阵src的逆矩阵保存到des中. 2 bool GetMatrixInverse(double src[N][

矩阵求逆c++实现

高斯消元法可以用来找出一个可逆矩阵的逆矩阵.设A 为一个N * N的矩阵,其逆矩阵可被两个分块矩阵表示出来.将一个N * N单位矩阵 放在A 的右手边,形成一个N * 2N的分块矩阵B = [A,I] .经过高斯消元法的计算程序后,矩阵B 的左手边会变成一个单位矩阵I ,而逆矩阵A ^(-1) 会出现在B 的右手边.假如高斯消元法不能将A 化为三角形的格式,那就代表A 是一个不可逆的矩阵.应用上,高斯消元法极少被用来求出逆矩阵.高斯消元法通常只为线性方程组求解. //**************

【题解】Matrix BZOJ 4128 矩阵求逆 离散对数 大步小步算法

传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=4128 大水题一道 使用大步小步算法,把数字的运算换成矩阵的运算就好了 矩阵求逆?这么基础的线代算法我也不想多说,还是自行百度吧 需要注意的是矩阵没有交换律,所以在计算$B\cdot A^{-m}$的时候不要把顺序搞混 代码: 1 #include <cstring> 2 #include <cstdio> 3 #include <algorithm> 4 #inc

自适应滤波:矩阵求逆

作者:桂. 时间:2017-04-02  10:36:09 链接:http://www.cnblogs.com/xingshansi/p/6658655.html 声明:欢迎被转载,不过记得注明出处哦~  [读书笔记09] 前言 西蒙.赫金的<自适应滤波器原理>第四版第八章:最小二乘法.因为最小二乘涉及到矩阵求逆,因为通常对于秩缺矩阵其逆是不可求的,这就需要借助广义逆矩阵.而广义逆矩阵可以借助奇异值分解(SVD,Singularly Valuable Decomposition)进行求解. 有

lapack笔记:矩阵求逆

在有限元等参单元计算中,大量用到Jacobi矩阵的求逆. 这里给出一个使用lapack库函数求2x2矩阵逆的例子: /********************************************************** File: test_lapack.c Author: Liang Zheng E-mail: [email protected] Date: 2014-6-19 **************************************************

矩阵求逆(二):逆矩阵

1.数学定义   定义:对于n阶矩阵A,如果有一个n阶矩阵B,使 AB=BA=E 则说矩阵A是可逆的,并把矩阵B称为A的逆矩阵. 定理:若矩阵A可逆,则|A|≠0. 定理:若|A|≠0,则矩阵A可逆,且 A-1=1/|A|*A* 2.算法实现 1)矩阵的行列式 ''' <summary> ''' 返回一个矩阵的行列式 ''' </summary> ''' <param name="dMatrix">原矩阵</param> ''' <

图像处理之基础---矩阵求逆实现

最近做一个加密算法遇到需要计算矩阵的逆,闲着无聊,记录一下,以后免得再麻烦. [cpp] view plaincopyprint? #include #include #include #define MAX 20 #define E 0.000000001 /** * 计算矩阵src的模 */ double calculate_A( double src[][MAX], int n ) { int i,j,k,x,y; double tmp[MAX][MAX], t; double resul

BZOJ 4128 Matrix BSGS+矩阵求逆

题意:链接 方法: BSGS+矩阵求逆 解析: 这题就是把Ax=B(mod C)的A和B换成了矩阵. 然而别的地方并没有改动. 所以就涉及到矩阵的逆元这个问题. 矩阵的逆元怎么求呢? 先在原矩阵后接一个单位矩阵,不妨设右对角线 先把原矩阵进行高斯消元 且消成严格右对角线的单位矩阵的形式. 然后在消元的同时把单位矩阵的部分一并计算,最后单位矩阵就变成了它的逆矩阵. 这道题保证矩阵有逆 然而没有逆矩阵的情况就是高斯消元搞不成. 所以判断应该也好判断. 另外,刚刚实测本题数据,关于将矩阵的hash,直

C++实现矩阵求逆

最近实现一个算法要用到求逆等矩阵运算,在网上搜到一个别人写的矩阵类,试了一下效果不错,贴在这里,做个保存. matrix.h文件: 1 #ifndef __MATRIX_H__ 2 #define __MATRIX_H__ 3 4 #pragma once 5 6 #include <iostream> 7 #include <fstream> 8 #include <sstream> 9 #include <vector> 10 #include <