原博文:minfanphd
有关矩阵在1到10天的时候就已经接触过了,本次的工作量主要是将矩阵的一些运算给加进去了。
在java中this()代表的是调用无参构造函数,若括号里有值则调用的是相应的构造函数。
package matrix; import java.util.Arrays; /** * @description: * @author: Qing Zhang * @time: 2021/6/11 */ public class IntMatrix { //数据 int[][] data; /** * @Description: 构造函数 * @Param: [paraRows, paraColumns] * @return: */ public IntMatrix(int paraRows, int paraColumns) { data = new int[paraRows][paraColumns]; } /** * @Description: 第二个构造函数 * @Param: [paraMatrix] * @return: */ public IntMatrix(int[][] paraMatrix) { data = new int[paraMatrix.length][paraMatrix[0].length]; for (int i = 0; i < data.length; i++) { for (int j = 0; j < data[0].length; j++) { data[i][j] = paraMatrix[i][j]; } } } /** * @Description: 第三个构造函数 * @Param: [paraMatrix] * @return: */ public IntMatrix(IntMatrix paraMatrix) { this(paraMatrix.getData()); } /** * @Description: 获取单位矩阵 * @Param: [paraRows] * @return: matrix.IntMatrix */ public static IntMatrix getIdentityMatrix(int paraRows) { IntMatrix resultMatrix = new IntMatrix(paraRows, paraRows); for (int i = 0; i < paraRows; i++) { resultMatrix.data[i][i] = 1; } return resultMatrix; } @Override public String toString() { return Arrays.deepToString(data); } /** * @Description: 获取自己的数据 * @Param: [] * @return: int[][] */ public int[][] getData() { return data; } /** * @Description: 获取矩阵行 * @Param: [] * @return: int */ public int getRows() { return data.length; } /** * @Description: 获取矩阵列 * @Param: [] * @return: int */ public int getColumns() { return data[0].length; } /** * @Description: 设置某个位置的值 * @Param: [paraRow, paraColumn, paraValue] * @return: void */ public void setValue(int paraRow, int paraColumn, int paraValue) { data[paraRow][paraColumn] = paraValue; } /** * @Description: 获取某个位置的值 * @Param: [paraRow, paraColumn] * @return: int */ public int getValue(int paraRow, int paraColumn) { return data[paraRow][paraColumn]; } /** * @Description: 矩阵相加 * @Param: [paraMatrix] * @return: void */ public void add(IntMatrix paraMatrix) throws Exception { //第一步是将目标矩阵的数据获取出来 int[][] tempData = paraMatrix.getData(); //第二部看两个矩阵大小是否匹配 if (data.length != tempData.length) { throw new Exception("Cannot add matrices. Rows not match: " + data.length + " vs. " + tempData.length + "."); } if (data[0].length != tempData[0].length) { throw new Exception("Cannot add matrices. Rows not match: " + data[0].length + " vs. " + tempData[0].length + "."); } //第三步将两个矩阵的数据相加 for (int i = 0; i < data.length; i++) { for (int j = 0; j < data[0].length; j++) { data[i][j] += tempData[i][j]; } } } /** * @Description: 两个给定的矩阵相加 * @Param: [paraMatrix1, paraMatrix2] * @return: matrix.IntMatrix */ public static IntMatrix add(IntMatrix paraMatrix1, IntMatrix paraMatrix2) throws Exception { //将第一个矩阵复制出来 IntMatrix resultMatrix = new IntMatrix(paraMatrix1); //加上第二个矩阵 resultMatrix.add(paraMatrix2); return resultMatrix; } /** * @Description: 矩阵相乘 * @Param: [paraMatrix1, paraMatrix2] * @return: matrix.IntMatrix */ public static IntMatrix multiply(IntMatrix paraMatrix1, IntMatrix paraMatrix2) throws Exception { //第一步匹配矩阵大小 int[][] tempData1 = paraMatrix1.getData(); int[][] tempData2 = paraMatrix2.getData(); if (tempData1[0].length != tempData2.length) { throw new Exception("Cannot multiply matrices: " + tempData1[0].length + " vs. " + tempData2.length + "."); } //第二步分配空间. int[][] resultData = new int[tempData1.length][tempData2[0].length]; //第三步矩阵相乘 for (int i = 0; i < tempData1.length; i++) { for (int j = 0; j < tempData2[0].length; j++) { for (int k = 0; k < tempData1[0].length; k++) { resultData[i][j] += tempData1[i][k] * tempData2[k][j]; } } } //构造目标矩阵 IntMatrix resultMatrix = new IntMatrix(resultData); return resultMatrix; } public static void main(String[] args) { IntMatrix tempMatrix1 = new IntMatrix(3, 3); tempMatrix1.setValue(0, 1, 1); tempMatrix1.setValue(1, 0, 1); tempMatrix1.setValue(1, 2, 1); tempMatrix1.setValue(2, 1, 1); System.out.println("The original matrix is: " + tempMatrix1); IntMatrix tempMatrix2 = null; try { tempMatrix2 = new IntMatrix(IntMatrix.multiply(tempMatrix1, tempMatrix1)); } catch (Exception e) { System.out.println(e); } System.out.println("The square matrix is: " + tempMatrix2); IntMatrix tempMatrix3 = new IntMatrix(tempMatrix2); try { tempMatrix3.add(tempMatrix1); } catch (Exception e) { System.out.println(e); } System.out.println("The connectivity matrix is: " + tempMatrix3); } }