ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Day8写点代码 矩阵乘法

2022-04-23 19:01:32  阅读:170  来源: 互联网

标签:tempFirstMatrix Day8 int 写点 ++ length tempSecondMatrix println 乘法


1、矩阵相乘的条件就是A矩阵的列等于B矩阵的行,且最后求得的C矩阵的行和列分别为A矩阵的行、B矩阵的列;

2、二阶数组的话, a.length 表示行,a[0].length 表示列

 1 import java.util.Arrays;
 2 public class MatrixMultiplication {
 3     /**
 4      * The entrance of the program
 5      * @param args
 6      */
 7     public static void main(String args[]) {
 8         matrixMultiplicationTest();
 9     }//Of main
10 
11     /**
12      * Matrix multiplication.
13      * The columns of the first matrix should be equal to the rows of the second one.
14      * 
15      * @param paraFirstMatrix
16      * @param paraSecondMatrix
17      * @return
18      */
19     public static int[][] multiplication(int[][] paraFirstMatrix, int[][] paraSecondMatrix) {
20         int m = paraFirstMatrix.length;//Ai
21         int n = paraFirstMatrix[0].length;//Aj
22         int p = paraSecondMatrix[0].length;//Bj
23         
24         //Step1. Dimension check
25         if (paraSecondMatrix.length != n) {//Bi != Aj
26             System.out.println("The two matrices cannot be multiplied.");
27             return null;
28         }
29 
30         //Step2. The loop.
31         int[][] resultMatrix = new int[m][p];
32         for (int i = 0; i < m; i++) {
33             for (int j = 0; j < p; j++) {
34                 for (int k = 0; k < n; k++) {
35                     resultMatrix[i][j] += paraFirstMatrix[i][k] * paraSecondMatrix[k][j];
36                 }// Of for k 
37             }// Of for j
38         }// Of for i
39         return resultMatrix;
40     }// Of multiplication
41     
42     /**
43      * Unit test for respective method
44      */
45     public static void matrixMultiplicationTest() {
46         int[][]tempFirstMatrix = new int[2][3];
47         for (int i = 0; i < tempFirstMatrix.length; i++) {
48             for (int j = 0; j < tempFirstMatrix[0].length; j++) {
49                 tempFirstMatrix[i][j] = i + j;
50             }// Of for j
51         }// Of for i
52         System.out.println("The first matrix is: \r\n" + Arrays.deepToString(tempFirstMatrix));
53         
54         int[][]tempSecondMatrix = new int[3][2];
55         for (int i = 0; i < tempSecondMatrix.length; i++) {
56             for (int j = 0; j < tempSecondMatrix[0].length; j++) {
57                 tempSecondMatrix[i][j] = i*10 + j;
58             }// Of for j
59         }// Of for i
60         System.out.println("The second matrix is: \r\n" + Arrays.deepToString(tempSecondMatrix));
61         
62         int[][] tempThirdMatrix = multiplication(tempFirstMatrix, tempSecondMatrix);
63         System.out.println("The Third matrix is: \r\n" + Arrays.deepToString(tempThirdMatrix));
64 
65         System.out.println("Try to multiply the first matrix with itself.\r\n");
66         tempThirdMatrix = multiplication(tempFirstMatrix, tempFirstMatrix);
67         System.out.println("The result matrix is: \r\n" + Arrays.deepToString(tempThirdMatrix));
68 
69     }// Of matrixMultiplicationTest
70     
71 }// Of class MatrixMultiplication

 

标签:tempFirstMatrix,Day8,int,写点,++,length,tempSecondMatrix,println,乘法
来源: https://www.cnblogs.com/f1uency/p/16183261.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有