矩阵 c#中的类的问题

来源:百度知道 编辑:UC知道 时间:2024/05/14 16:27:10
用c#里的类表示矩阵,并重载矩阵的加法

以下是行主映射,一位数组描述的矩阵类,加、减、乘法等运算的效率 都比二位数组描述的矩阵高。

class Matrix
{
private int row = 0, col = 0, size = 0;
private int[] element = null;

public int this[int r, int c]
{
get
{
return element[(r - 1) * r + c - 1];
}

set
{
element[(r - 1) * r + c - 1] = value;
}
}

public Matrix(int r, int c)
{
row = r;
col = c;
size = r * c;
element = new int[size];
}

public static Matrix operator +(Matrix m1, Matrix m2)
{
if (m1.row != m2.row || m1.col != m2.col)
{
throw new Exception("矩阵维度不匹配");
}
else
{