1.矩阵相乘
昨天讲解了两个矩阵相加,今天的主要内容是讲解两个矩阵的相乘。对于今天的内容我们首先需要做一些准备,我们首先要知道用数学的方法如何计算两个矩阵相乘,在这里我就简单的介绍一下矩阵相乘的计算方法。两矩阵相乘,左边矩阵第一行乘以右边矩阵第一列(分别相乘,第一个数乘第一个数),乘完之后相加,即为结果的第一行第一列的元素,以此类推往下依次计算后面的元素。
1.1代码演示
package basic;
import java.util.Arrays;
/**
* This is the eighth code. Names and comments should follow my style strictly.
*
* @author WU JUN 2298320301@qq.com.
*/
public class Day8 {
/**
*********************
* The entrance of the program.
*
* @param args
* Not used now.
*********************
*/
public static void main(String args[]) {
matrixMultiplicationTest();
}// Of main
/**
*********************
* Matrix multiplication. The columns of the first matrix should be equal to the
* rows of the second one.
*
* @param paraFirstMatrix
* The first matrix.
* @param paraSecondMatrix
* The second matrix.
* @return The result matrix.
*********************
*/
public static int[][] multiplication(int[][] paraFirstMatrix, int[][] paraSecondMatrix) {
int m = paraFirstMatrix.length;
int n = paraFirstMatrix[0].length;
int p = paraSecondMatrix[0].length;
// Step 1. Dimension check.
if (paraSecondMatrix.length != n) {
System.out.println("The two matrices cannot be multiplied.");
return null;
} // Of if
// Step 2. The loop.
int[][] resultMatrix = new int[m][p];
for (int i = 0; i < m; i++) {
for (int j = 0; j < p; j++) {
for (int k = 0; k < n; k++) {
resultMatrix[i][j] += paraFirstMatrix[i][k] * paraSecondMatrix[k][j];
} // Of for k
} // Of for j
} // Of for i
return resultMatrix;
}// Of multiplication
/**
*********************
* Unit test for respective method.
*********************
*/
public static void matrixMultiplicationTest() {
int[][] tempFirstMatrix = new int[2][3];
for (int i = 0; i < tempFirstMatrix.length; i++) {
for (int j = 0; j < tempFirstMatrix[0].length; j++) {
tempFirstMatrix[i][j] = i + j;
} // Of for j
} // Of for i
System.out.println("The first matrix is: \r\n" + Arrays.deepToString(tempFirstMatrix));
int[][] tempSecondMatrix = new int[3][2];
for (int i = 0; i < tempSecondMatrix.length; i++) {
for (int j = 0; j < tempSecondMatrix[0].length; j++) {
tempSecondMatrix[i][j] = i * 10 + j;
} // Of for j
} // Of for i
System.out.println("The second matrix is: \r\n" + Arrays.deepToString(tempSecondMatrix));
int[][] tempThirdMatrix = multiplication(tempFirstMatrix, tempSecondMatrix);
System.out.println("The third matrix is: \r\n" + Arrays.deepToString(tempThirdMatrix));
System.out.println("Trying to multiply the first matrix with itself.\r\n");
tempThirdMatrix = multiplication(tempFirstMatrix, tempFirstMatrix);
System.out.println("The result matrix is: \r\n" + Arrays.deepToString(tempThirdMatrix));
}// Of matrixMultiplicationTest
}// Of class MatrixMultiplication
1.2代码解析
1、我们还是按照昨天将的方法对代码进行分析,首先我们从入口函数下手。上述代码的主函数之中只要一个方法就是matrixMultiplicationTest,该方法不需要传入任何参数,所以我们直接分析方法的内部程序。
2、matrixMultiplicationTest函数中一开始就是先分别完成了对tempFirstMatrix和tempSecondMatrix两个二维数组的定义以及初始化操作,并且别将两个数组中的元素打印显示了一遍。如下图部分代码:
紧接着继续往下分析,我们定义了一个新的二维数组tempThirdMatrix来接收multiplication的返回值,并且再跳出multiplication函数之后将tempThirdMatrix数组的元素打印一遍。如下图部分代码:
3、接下来我们来对multiplication函数进行分析,该函数需要传入两个参数,且两个参数都是int类型的二维数组。函数一开始先是分别获取两个矩阵的行和列的长度,然后紧接着就是一个判断语句。这个判断语句我们需要先了解两个矩阵相乘需要注意的地方,两个矩阵相乘时第一个矩阵的列数需要和第二个矩阵的行数相等时才有意义,所以这个判断语句的作用就是用来判断两个矩阵相乘是否有意义,如果有意义则程序继续往下运行否则返回值为null。我们继续往下看紧接着就是三个for循环语句的嵌套运算,当遇到这种较为复杂的嵌套循环语句时不要慌张,我们可以采用最老实的方法那就是带数字进去套。我通过一个例子来说明这个嵌套循环,对于外层循环我先取i=0,然后进入第二层循环我们取j=0,最后进入第三层循环。这时候程序就应该执行resultMatrix[0][0] += paraFirstMatrix[0][k] * paraSecondMatrix[k][0],代码这样一简化就清晰了许多。随着第三层一步步循环,resultMatrix[0][0]元素就等于paraFirstMatrix的第0行元素与paraSecondMatrix第0列元素对应相乘的和。resultMatrix数组后面元素的值就可以通过这种方法一个个计算出来,这样也能够使得代码理解起来更加简单一些。最后就是将resultMatrix数组返回到matrixMultiplicationTest函数中并完成最终的打印输出。
1.3运行结果
2.总结
对于今天的内容我们学习到了两个矩阵相乘是如何进行计算的,并且两个矩阵要满足什么样的条件时相乘才有意义。其次今天代码有一点难度的就是那个三层的嵌套for循环,但是对于一开始不好理解的嵌套循环就采用那种带值的方法来进行分析,这样理解起来不仅轻松而且还透彻。