求行列式值、求逆矩阵C#源代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class inversematrix
    {
        public static float DetValue(float[,] matrix)//求某矩阵的行列式
        {
            float sum = 0;
            int sign = 1;
            if (matrix.GetLength(0) == 2)//递归出口
            {
                return matrix[0, 0] * matrix[1, 1] - matrix[0, 1] * matrix[1, 0]; 
            }
            for (int i = 0; i < matrix.GetLength(0); i++)
            {
                float[,] tempmatrix = new float[matrix.GetLength(0) - 1, matrix.GetLength(1) - 1];
                for (int j = 0; j < matrix.GetLength(0) - 1; j++)
                {
                    for (int k = 0; k < matrix.GetLength(1) - 1; k++)

                    {

                        tempmatrix[j, k] = matrix[j + 1, k >= i ? k + 1 : k];
                    }
                }
                sum += sign * matrix[0, i] * DetValue(tempmatrix);//递归,调用函数自身
                sign = sign * (-1);
            }
            return sum;
        }
        public static float[,] BSmatrix(float[,] matrix)//求某矩阵的伴随矩阵
        {
            int sign = 1;
            float[,] bsmatrix = new float[matrix.GetLength(0), matrix.GetLength(1)];
            for (int i = 0; i < matrix.GetLength(0); i++)
            {
                for (int j = 0; j < matrix.GetLength(1); j++)
                {
                    float[,] tempmatrix = new float[matrix.GetLength(0) - 1, matrix.GetLength(1) - 1];
                    for (int k = 0; k < matrix.GetLength(0) - 1; k++)
                    {
                        for (int m = 0; m < matrix.GetLength(1) - 1; m++)
                        {
                            tempmatrix[k, m] = matrix[k >= i ? k + 1 : k, m >= j ? m + 1 : m];
                        }
                    }
                    bsmatrix[j, i] = sign * DetValue(tempmatrix);
                    sign = sign * -1;
                }
            }
            return bsmatrix;
        }
        public static void DisplayMatrix(float[,] matrix)//显示矩阵
        {
            for (int i = 0; i < matrix.GetLength(0); i++)
            {
                for (int j = 0; j < matrix.GetLength(1); j++)
                {
                    Console.Write("{0,10:f2}", matrix[i, j]); 
                }
                Console.WriteLine();
            }
        }
        public static float[,] KNmatrix(float[,] matrix)//求某矩阵的逆矩阵
        {
            float[,] tempmatrixout = new float[matrix.GetLength(0), matrix.GetLength(1)];
            float[,] tempmatrixin=new float[matrix.GetLength(0),matrix.GetLength(1)];
            tempmatrixin=BSmatrix(matrix);
            float valuein=DetValue(matrix);
            for (int i = 0; i < matrix.GetLength(0); i++)
            {
                for (int j = 0; j < matrix.GetLength(1); j++)
                {
                    tempmatrixout[i, j] = tempmatrixin[i, j] / valuein;
                }
            }
            return tempmatrixout;
        }
        static void Main(string[] args)
        {
            float[,] test = new float[,] { { 4, 2, -6 }, { 7, 5, 4 }, { 3, 4, 9 } };
            float[,] resultmatrix = new float[test.GetLength(0), test.GetLength(1)];
            resultmatrix = KNmatrix(test);
            Console.WriteLine("求得逆矩阵为:");
            DisplayMatrix(resultmatrix);
            Console.ReadLine();
            
        }
    }
}
 

猜你喜欢

转载自blog.csdn.net/qq_35154529/article/details/81086937