⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 matrixinverse.txt

📁 通过CSharp写的关于矩阵求逆的程序
💻 TXT
📖 第 1 页 / 共 2 页
字号:






using System;using System.Collections.Generic;using System.Text;
namespace ByteTest
{    /// <summary>
    /// 算法的大致思想是通过行列式初等变换来求。
    /// </summary>
    public class MatrixComputer
    {   /// <summary>
        /// 求逆矩阵
        /// </summary>
        /// <param name="dMatrix"></param>
        /// <returns></returns>
        public static double[,] ReverseMatrix(double[,] dMatrix)
        {    //获取矩阵的行数
            int Level = dMatrix.GetLength(1);
            ////求矩阵的行列式的值
            //double dMatrixValue = MatrixValue(dMatrix);
            //if (dMatrixValue == 0)
            //{
            //    return null;
            //}
            double[,] dReverseMatrix = new double[Level,2*Level];
            //初始化矩阵Level×(*Level)
            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
                    {
                        if (j - Level == i)
                        {   dReverseMatrix[i, j] = 1;    }
                        else
                        {   dReverseMatrix[i, j] = 0;     }
                    }
                }
            }
            for (int i = 0, j = 0; i < Level && j < Level; i++, j++)
            {
                if (dReverseMatrix[i, j] == 0)
                {
                    if (i == Level - 1)
                    {    return null;            }
                    int m = i + 1;
                    for (; dMatrix[m, j] == 0; m++)
                    {
                        if (m == Level - 1)
                        {  return null;  }
                    }
                    if (m == Level)
                    {   return null;  }
                    else
                    {    //把i行和m行相加
                        for (int n = j; n < 2*Level; n++)
                        {  dReverseMatrix[i, n] += dReverseMatrix[m, n]; }
                    }
                }
                double temp = dReverseMatrix[i, j];
                if (temp != 1)
                {    //把i行数据,变成以开始的一行数据
                    for (int n = j; n < 2*Level; n++)
                    {
                        if (dReverseMatrix[i, n] != 0)
                        {    dReverseMatrix[i, n] /= temp;    }
                    }
                }
                //把i行后的所有行的j列变成
                for (int s = Level - 1; s > i; s--)
                {
                    temp = dReverseMatrix[s, j];
                    for (int t = j; t < 2*Level; t++)
                    {     dReverseMatrix[s, t] -= (dReverseMatrix[i, t]*temp);   }
                }
            }
            //把矩阵Level×(*Level)前Level×Level转变为单位矩阵
            for (int i = Level - 2; i >= 0; i--)
            {
                for (int j = i + 1; j < Level; j++)
                {
                    if (dReverseMatrix[i, j] != 0)
                    {
                        double tmp = dReverseMatrix[i, j];
                        for (int n = j; n < 2*Level; n++)
                        { dReverseMatrix[i, n] -= (tmp*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;
        }
        /// <summary>
        /// 求矩阵对应行列式的值
        /// </summary>
        /// <param name="MatrixList"></param>
        /// <returns></returns>
        public static double MatrixValue(double[,] MatrixList)
        {
            int Level = MatrixList.GetLength(1);
            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];
                }
            }
            int sign = 1;
            for (int i = 0, j = 0; i < Level && j < Level; i++, j++)
            {
                //判断改行dMatrix[i, j]是否为,若是,则寻找i后的行(m,m>i,切dMatrix[m, j]!=0)进行交换
                if (dMatrix[i, j] == 0)
                {
                    if (i == Level - 1)
                    {
                        return 0;
                    }
                    int m = i + 1;
                    //获取一个dMatrix[m, j]不为为的行
                    for (; dMatrix[m, j] == 0; m++)
                    {
                        if (m == Level - 1)
                        {
                            return 0;
                        }
                    }
                    //判断是否达到矩阵的最大行,若是,则返回
                    //把i行和m行调换
                    double temp;
                    for (int n = j; n < Level; n++)
                    {
                        temp = dMatrix[i, n];
                        dMatrix[i, n] = dMatrix[m, n];
                        dMatrix[m, n] = temp;
                    }
                    sign *= (-1);
                }
                //把当前行以后的行所对应的列变成
                double tmp;
                for (int s = Level - 1; s > i; s--)
                {
                    tmp = dMatrix[s, j];
                    //j行后面的所有行
                    for (int t = j; t < Level; t++)
                    {
                        dMatrix[s, t] -= dMatrix[i, t]*(tmp/dMatrix[i, j]);
                    }
                }
            }
            double result = 1;
            for (int i = 0; i < Level; i++)
            {
                if (dMatrix[i, i] != 0)
                {
                    result *= dMatrix[i, i];
                }
                else
                {
                    return 0;
                }
            }
            return sign*result;
        }
        /// <summary>
        /// 矩阵乘法实现,即获取两个矩阵相乘的结果
        /// </summary>
        /// <param name="firstMatrix"></param>
        /// <param name="secondMatrix"></param>
        /// <returns></returns>
        public static double[,] MatrixMultiplication(double[,] firstMatrix, double[,] secondMatrix)
        {
            double[,] resultMatrix = new double[firstMatrix.GetLength(0),secondMatrix.GetLength(1)];
            //判断相乘矩阵是否合法,即第一个矩阵的列要等于第二个矩阵的行
            if (firstMatrix.GetLength(1) != secondMatrix.GetLength(0))
            {
                return null;
            }            
            //求结果矩阵
            for (int rowIndex = 0; rowIndex < firstMatrix.GetLength(0); rowIndex++)
            {
                for (int colIndex = 0; colIndex < secondMatrix.GetLength(1); colIndex++)
                {
                    //初始化结果矩阵的元素
                    resultMatrix[rowIndex, colIndex] = 0;
                    for (int i = 0; i < firstMatrix.GetLength(1); i++)
                    {
                        //求结果矩阵的元素值
                        resultMatrix[rowIndex, colIndex] += firstMatrix[rowIndex, i] * secondMatrix[i, colIndex];
                    }
                }
            }
            return resultMatrix;
        }
    }
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -