日撸Java三百行 day08(矩阵相乘)

1.矩阵相乘

矩阵相乘需满足前一个矩阵的列数等于后一个矩阵的行数,所以在做矩阵运算前需判断该条件。

计算方法:

c[m][p] = a[m][n] * b[n][p]

c[i][j] = \sum_{k=0}^{n-1} a[i][k]+a[k][j] (0<=i<m, 0<=j<p)

这里使用了i,j,k三个递增变量,则需用到三次循环

2.矩阵相乘的简单测试

package basic;

import java.util.Arrays;

/**
 * The usage of the sth.
 * 
 * @author Yunhua Hu yunhuahu0528@163.com.
 */
public class MatrixMultiplication {

	/**
	 *********************
	 * 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

输出:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值