矩阵运算
思路: 其实就是m1[]的行×m2[]的每一列,将每一列进行相加即可。
/*
* 矩阵乘法
* 矩阵1为n*m矩阵,矩阵2为m*p矩阵
* 结果为n*p矩阵
*
* */
public static long[][] matrixMultiply(long[][] m1, long[][] m2){
final int n = m1.length;
final int m = m1[0].length;
if (m != m2.length) throw new IllegalArgumentException();
final int p = m2[0].length;
long[][] result = new long[n][p]; //新矩阵的行数为m1的行数,列数为m2的列数
for (int i = 0; i < n; i++){//m1的每一行
for (int j = 0; j < p; j++){//m2的每一列
for (int k = 0; k < m; k++){
result[i][j] += m1[i][k] * m2[k][j];
}
}
}
return result;
}