public JZ Multiply(JZ other) //矩阵乘法 { // 检查行列数是否符合要求 if (numColumns != other.GetNumRows()) { throw new Exception("矩阵的行/列数不匹配。"); } JZ result = new JZ(numRows, other.GetNumColumns()); double value; for (int i = 0; i < result.GetNumRows(); ++i) { for (int j = 0; j < other.GetNumColumns(); ++j) { value = 0.0; for (int k = 0; k < numColumns; ++k) { value += GetElement(i, k) * other.GetElement(k, j); } result.SetElement(i, j, value); } } return(result); }
public JZ Subtract(JZ other)//矩阵减法 { if (numColumns != other.GetNumColumns() || numRows != other.GetNumRows()) { throw new Exception("矩阵的行/列数不匹配。"); } // 构造结果矩阵 JZ result = new JZ(this); // 进行减法 for (int i = 0; i < numRows; ++i) { for (int j = 0; j < numColumns; ++j) { result.SetElement(i, j, GetElement(i, j) - other.GetElement(i, j)); } } return(result); }
public JZ Add(JZ other) //矩阵加法 { // 检查行列数是否相等 if (numColumns != other.GetNumColumns() || numRows != other.GetNumRows()) { throw new Exception("矩阵的行/列数不匹配。"); } // 构造结果矩阵 JZ result = new JZ(this); // 进行加法 for (int i = 0; i < numRows; ++i) { for (int j = 0; j < numColumns; ++j) { result.SetElement(i, j, GetElement(i, j) + other.GetElement(i, j)); } } return(result); }
public JZ(JZ other) { numColumns = other.GetNumColumns(); numRows = other.GetNumRows(); Init(numRows, numColumns); }