C#实现矩阵计算

矩阵计算是一项重要的数学运算,在数据科学、机器学习等领域具有广泛的应用。

首先,我们需要定义一个Matrix类来表示矩阵,并实现一些基本的矩阵操作。

 class Matrix
    {
        public Matrix(double[,] matrix)
        {
            int rows = matrix.GetLength(0);
            int lines = matrix.GetLength(1);
            Element = new double[rows, lines];
            for (int i = 0; i < rows; i++)
                for (int j = 0; j < lines; j++)
                    Element[i, j] = matrix[i, j];
        }
        public Matrix(double[][] matrix)
        {
            int rows = matrix.GetLength(0);
            int lines = matrix.GetLength(1);
            Element = new double[rows, lines];
            for (int i = 0; i < rows; i++)
                for (int j = 0; j < lines; j++)
                    Element[i, j] = matrix[i][j];
        }
        public double[,] Element { get; set; }

        public int Rows
        {
            get
            {
                return Element.GetLength(0);
            }
        }
        public int Columns
        {
            get
            {
                return Element.GetLength(1);
            }
        }

        public double this[int i, int j]
        {
            get
            {
                if (i < Rows && j < Columns)
                    return Element[i, j];
                else
                {
                    System.Exception ex = new Exception("索引超出界限!");
                    throw ex;
                }
            }
            set
            {
                Element[i, j] = value;
            }
        }
        public int GetRows()
        {
            return Element.GetLength(0);
        }
        public int GetColumns()
        {
            return Element.GetLength(1);
        }

上述代码中,我们定义了基本的矩阵类,其中包含了矩阵的数据(使用二维数组存储)、行数和列数等属性。并在接下来的代码实现中利用类里面的各种方法实现矩阵的加减乘除、转置、求逆等运算功能。

//矩阵加法运算
        public static Matrix operator+(Matrix matrix1, Matrix matrix2)
        {
            double[,] array = new double[matrix1.Rows, matrix1.Columns];
            Matrix result = new Matrix(array);
            if (matrix1.Rows == matrix2.Rows && matrix1.Columns == matrix2.Columns)

                for (int i = 0; i < matrix1.Rows; i++)
                    for (int j = 0; j < matrix1.Columns; j++)
                        result[i, j] = matrix1[i, j] + matrix2[i, j];

            else if (matrix1.Rows != matrix2.Rows)
            {
                MessageBox.Show("加法运算行数不匹配!");
            }
            else
            {
                MessageBox.Show("加法运算列数不匹配!");
            }
            return result;
        }

        //二维矩阵减法运算
        public static Matrix operator -(Matrix matrix1, Matrix matrix2)
        {
            double[,] array = new double[matrix1.Rows, matrix1.Columns];
            Matrix result = new Matrix(array);
            if (matrix1.Rows == matrix2.Rows && matrix2.Columns == matrix2.Columns)
                for (int i = 0; i < matrix1.Rows; i++)
                    for (int j = 0; j < matrix1.Columns; j++)
                        result[i, j] = matrix1[i, j] - matrix2[i, j];
            else if (matrix1.Rows != matrix2.Rows)
            {
                MessageBox.Show("加法运算行数不匹配!");
            }
            else
            {
                MessageBox.Show("加法运算列数不匹配!");
            }
            return result;
        }

        //矩阵乘法运算
        public static Matrix operator *(Matrix matrix1, Matrix matrix2)
        {
            double[,] array = new double[matrix1.Rows, matrix1.Columns];
            Matrix result = new Matrix(array);
            try
            {
                if (matrix1.Columns == matrix2.Rows)
                    for (int row1 = 0; row1 < matrix1.Rows; row1++)
                        for (int column2 = 0; column2 < matrix2.Columns; column2++)
                        {
                            double Sum = 0;
                            for (int column1 = 0; column1 < matrix1.Columns; column1++)
                            {
                                Sum += matrix1[row1, column1] * matrix1[column1, row1];
                            }
                            result[row1, column2] = Sum;
                        }
                else
                {
                    MessageBox.Show("矩阵乘法运算两个矩阵行列不匹配!");
                }
            }
            catch
            {
                MessageBox.Show("矩阵乘法运算错误!");
            }
            return result;
        }

上述是矩阵运算的部分实现。

实现效果如图。

又参考了部分重新写了一个矩阵类


    class Matrix
    {
        public int row;
        public int column;
        public double[,] arr;
        public Matrix()
        {
            row = 0; column = 0;
            arr = new double[row, column];
        }
        public Matrix(int row1, int column1)
        {
            row = row1;
            column = column1;
            arr = new double[row, column];
        }
        public Matrix(Matrix matrix)
        {
            row = matrix.row;
            column = matrix.column;
            for (int i = 0; i < row; i++)
                for (int j = 0; j < column; j++)
                    arr[i, j] = matrix.arr[i, j];
        }
        public Matrix(int row1, int column1, double[,] arr1)
        {
            row = row1;
            column = column1;
            arr = arr1;
        }
        public static Matrix operator +(Matrix A, Matrix B)
        {
            Matrix C = new Matrix(A.row, B.column);
            if (A.row == B.row && A.column == B.column)
            {
                for (int i = 0; i < A.row; i++)
                    for (int j = 0; j < A.column; j++)
                        C.arr[i, j] = A.arr[i, j] + B.arr[i, j];
            }
            return C;
        }
        public static Matrix operator -(Matrix A, Matrix B)
        {
            Matrix C = new Matrix(A.row, B.column);
            if (A.row == B.row && A.column == B.column)
            {
                for (int i = 0; i < A.row; i++)
                    for (int j = 0; j < A.column; j++)
                        C.arr[i, j] = A.arr[i, j] - B.arr[i, j];
            }
            return C;
        }
        public static Matrix operator *(Matrix A, Matrix B)
        {
            Matrix C = new Matrix(A.row, B.column);
            if (A.column == B.row)
            {
                for (int i = 0; i < A.row; i++)
                    for (int j = 0; j < B.column; j++)
                    {
                        double temp = 0;
                        for (int k = 0; k < A.column; k++)
                            temp += A.arr[i, k] * B.arr[k, j];
                        C.arr[i, j] = temp;
                    }
            }
            return C;
        }
        public Matrix transposs(Matrix A)
        {
            Matrix B = new Matrix(A.column, A.row);
            for (int i = 0; i < A.column; i++)
                for (int j = 0; j < A.row; j++)
                    B.arr[i, j] = A.arr[j, i];
            return B;
        }
        //逆矩阵
        public double[,] InverseMatrix(double[,] matrix)
        {
            int n = matrix.GetLength(0);
            double[,] result = new double[n, n];
            double[,] temp = new double[n, 2 * n];

            //将矩阵和单位矩阵拼接成一个2n* n的矩阵
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    temp[i, j] = matrix[i, j];
                    temp[i, j + n] = i == j ? 1 : 0;
                }
            }
            //高斯 - 约旦消元法
            for (int i = 0; i < n; i++)
            {
                double tempValue = temp[i, i];
                for (int j = i; j < 2 * n; j++)
                {
                    temp[i, j] /= tempValue;
                }
                for (int j = 0; j < n; j++)
                {
                    if (j != i)
                    {
                        tempValue = temp[j, i];
                        for (int k = i; k < 2 * n; k++)
                        {
                            temp[j, k] -= tempValue * temp[i, k];
                        }
                    }
                }
            }
            //取出逆矩阵
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    result[i, j] = temp[i, j + n];
                }
            }

            return result;
        }

        public Matrix Inverse(Matrix matrix)
        {
            matrix.arr = InverseMatrix(matrix.arr);
            return matrix;
        }
    }

  • 6
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

湫秋刀鱼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值