矩阵计算是一项重要的数学运算,在数据科学、机器学习等领域具有广泛的应用。
首先,我们需要定义一个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;
}
}