IntMatrix
public static IntMatrix add(IntMatrix m1, IntMatrix m2) {
int r1 = m1.matrix.length;
int c1 = m1.matrix[0].length;
int r2 = m2.matrix.length;
int c2 = m2.matrix[0].length;
if (r1 != r2) {
throw new RuntimeException("两个矩阵的行高都不相等!");
}
if (c1 != c2) {
throw new RuntimeException("两个矩阵的列宽都不相等!");
}
IntMatrix resultMatrix = new IntMatrix(r1, c1);
for (int i = 0; i < m1.matrix.length; i++) {
for (int j = 0; j < m2.matrix.length; j++) {
resultMatrix.matrix[i][j] = m1.matrix[i][j] + m2.matrix[i][j];
}
}
return resultMatrix;
}
public static IntMatrix multiply(IntMatrix m1, IntMatrix m2) {
if (m2.matrix[0].length != m1.matrix.length) {
throw new RuntimeException("第一个矩阵的列宽不等于第二个矩阵的行高!");
}
int r = m1.matrix.length;
int c = m2.matrix[0].length;
IntMatrix resultMatrix = new IntMatrix(r, c);
for (int i = 0; i < m1.matrix.length; i++) {
for (int j = 0; j < m2.matrix.length; j++) {
for (int k = 0; k < m1.matrix[0].length; k++) {
resultMatrix.matr