/** * This is the seventh code. Names and comments should follow my style strictly. * * @author ShiHuai Wen shihuaiwen@outlook.com. */ publicclassMatrixAddition {
/** ********************* * The entrance of the program. * * @param args Not used now. ********************* */ publicstaticvoidmain(String args[]) { matrixElementSumTest();
matrixAdditionTest(); }// Of main
/** ********************* * Sum the elements of a matrix. * * @param paraMatrix The given matrix. * @return The sum of all its elements. ********************* */ publicstaticintmatrixElementSum(int[][] paraMatrix) { intresultSum=0; for (inti=0; i < paraMatrix.length; i++) { for (intj=0; j < paraMatrix[0].length; j++) { resultSum += paraMatrix[i][j]; } // Of for j } // Of for i
return resultSum; }// Of matrixElementSum
/** ********************* * Unit test for respective method. ********************* */ publicstaticvoidmatrixElementSumTest() { int[][] tempMatrix = newint[3][4]; for (inti=0; i < tempMatrix.length; i++) { for (intj=0; j < tempMatrix[0].length; j++) { tempMatrix[i][j] = i * 10 + j; } // Of for j } // Of for i
System.out.println("The matrix is: \r\n" + Arrays.deepToString(tempMatrix)); System.out.println("The matrix element sum is: " + matrixElementSum(tempMatrix) + "\r\n"); }// Of matrixElementSumTest
/** ********************* * Add two matrices. Attention: NO error check is provided at this moment. * * @param paraMatrix1 The first matrix. * @param paraMatrix2 The second matrix. It should have the same size as * the first one's. * @return The addition of these matrices. ********************* */ publicstaticint[][] matrixAddition(int[][] paraMatrix1, int[][] paraMatrix2) { int[][] resultMatrix = newint[paraMatrix1.length][paraMatrix1[0].length];
for (inti=0; i < paraMatrix1.length; i++) { for (intj=0; j < paraMatrix1[0].length; j++) { resultMatrix[i][j] = paraMatrix1[i][j] + paraMatrix2[i][j]; } // Of for j } // Of for i
return resultMatrix; }// Of matrixAddition
/** ********************* * Unit test for respective method. ********************* */ publicstaticvoidmatrixAdditionTest() { int[][] tempMatrix = newint[3][4]; for (inti=0; i < tempMatrix.length; i++) { for (intj=0; j < tempMatrix[0].length; j++) { tempMatrix[i][j] = i * 10 + j; } // Of for j } // Of for i
System.out.println("The matrix is: \r\n" + Arrays.deepToString(tempMatrix)); int[][] tempNewMatrix = matrixAddition(tempMatrix, tempMatrix); System.out.println("The new matrix is: \r\n" + Arrays.deepToString(tempNewMatrix)); }// Of matrixAdditionTest
/** * This is the eighth code. Names and comments should follow my style strictly. * * @author ShiHuai Wen shihuaiwen@outlook.com. */ publicclassMatrixMultiplication {
/** ********************* * The entrance of the program. * * @param args Not used now. ********************* */ publicstaticvoidmain(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. ********************* */ publicstaticint[][] multiplication(int[][] paraFirstMatrix, int[][] paraSecondMatrix) { intm= paraFirstMatrix.length; intn= paraFirstMatrix[0].length; intp= paraSecondMatrix[0].length;
// Step 1. Dimension check. if (paraSecondMatrix.length != n) { System.out.println("The two matrices cannot be multiplied."); returnnull; } // Of if
// Step 2. The loop. int[][] resultMatrix = newint[m][p]; for (inti=0; i < m; i++) { for (intj=0; j < p; j++) { for (intk=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. ********************* */ publicstaticvoidmatrixMultiplicationTest() { int[][] tempFirstMatrix = newint[2][3]; for (inti=0; i < tempFirstMatrix.length; i++) { for (intj=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 = newint[3][2]; for (inti=0; i < tempSecondMatrix.length; i++) { for (intj=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