日撸代码300行学习笔记 Day 8

本文介绍了矩阵相乘的原理及Java实现。通过代码演示、解析,详细解释了矩阵相乘的计算方法和嵌套循环的逻辑,帮助读者理解两个矩阵相乘的条件及其实际运算过程。
摘要由CSDN通过智能技术生成

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循环,但是对于一开始不好理解的嵌套循环就采用那种带值的方法来进行分析,这样理解起来不仅轻松而且还透彻。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值