java實現矩陣基本運算
阿新 • • 發佈:2017-10-04
+= asi return sta pack res ret matrix n)
1 package Math; 2 3 /** 4 * 矩陣基本運算 5 * @author woyouyihujiu 6 * 7 */ 8 public class BasicMatrixMath { 9 public static final int OPEROPERATION_ADD = 1; 10 public static final int OPEROPERATION_SUB = 2; 11 public static final int OPEROPERATION_MUL = 3; 12 /** 13* 矩陣加法運算 14 * @param matrix_a 矩陣a 15 * @param matrix_b 矩陣b 16 * @return result1 運算合法,返回結果 17 */ 18 public int[][] additive(int[][] matrix_a, int[][] matrix_b){ 19 int[][] result1 = new int[matrix_a.length][matrix_a[0].length]; 20 if(legalOperation(matrix_a, matrix_b, OPEROPERATION_ADD)){21 for(int i = 0; i < matrix_a.length; i++){ 22 for(int j = 0; j < matrix_a[0].length; j++){ 23 result1[i][j] = matrix_a[i][j] + matrix_b[i][j]; 24 } 25 } 26 } 27 return result1; 28 } 29 /**30 * 矩陣減法運算 31 * @param matrix_a 矩陣a 32 * @param matrix_b 矩陣b 33 * @return result2 運算合法,返回結果 34 */ 35 public int[][] subtraction(int[][] matrix_a, int[][] matrix_b){ 36 int[][] result2 = new int[matrix_a.length][matrix_a[0].length]; 37 if(legalOperation(matrix_a, matrix_b, OPEROPERATION_SUB)){ 38 for(int i = 0; i < matrix_a.length; i++){ 39 for(int j = 0; j < matrix_a[0].length; j++){ 40 result2[i][j] = matrix_a[i][j] - matrix_b[i][j]; 41 } 42 } 43 } 44 return result2; 45 } 46 /** 47 * 矩陣乘法運算a 矩陣與矩陣相乘 48 * @param matrix_a 矩陣a 49 * @param matrix_b 矩陣b 50 * @return result3 運算合法,返回結果; null 運算不合法 51 */ 52 public int[][] multiplication_a(int[][] matrix_a, int[][] matrix_b){ 53 int[][] result3 = new int[matrix_a.length][matrix_b[0].length]; 54 if(legalOperation(matrix_a, matrix_b, OPEROPERATION_MUL)){ 55 for(int i = 0; i < matrix_a.length; i++){ 56 for(int j = 0;j < matrix_a[0].length; j++){ 57 result3[i][j] = calculateSingleResult(matrix_a, matrix_b, i, j); 58 } 59 } 60 return result3; 61 }else{ 62 return null; 63 } 64 } 65 /** 66 * 矩陣乘法運算b 矩陣的數乘 67 * @param matrix_a 矩陣a 68 * @param n 數n 69 * @return result4 運算合法,返回結果 70 */ 71 public int[][] multiplication_b(int[][] matrix_a, int n){ 72 int[][] result4 = new int[matrix_a.length][matrix_a[0].length]; 73 for(int i = 0; i < matrix_a.length; i++){ 74 for(int j = 0; j < matrix_a[0].length; j++){ 75 result4[i][j] = n * matrix_a[i][j]; 76 } 77 } 78 return result4; 79 } 80 /** 81 * 矩陣乘法a中result每個元素的單一運算 82 * @param matrix_a 矩陣a 83 * @param matrix_b 矩陣b 84 * @param row 參與單一運算的行標 85 * @param col 參與單一運算的列標 86 * @return result 運算結果 87 */ 88 public int calculateSingleResult(int[][] matrix_a, int[][] matrix_b, int row, int col){ 89 int result = 0; 90 for(int i = 0; i < matrix_a[0].length; i++){ 91 result += matrix_a[row][i] * matrix_b[i][col]; 92 } 93 return result; 94 } 95 /** 96 * 判斷矩陣是否可以進行合法運算 97 * @param matrix_a 矩陣a 98 * @param matrix_b 矩陣b 99 * @param type 判斷運算類型,是加法,減法,還是乘法運算 100 * @return legal true 運算合法; false 運算不合法 101 */ 102 private boolean legalOperation(int[][] matrix_a, int[][] matrix_b, int type){ 103 boolean legal = true; 104 if(type == OPEROPERATION_ADD || type == OPEROPERATION_SUB){ 105 if(matrix_a.length != matrix_b.length || matrix_a[0].length != matrix_b[0].length){ 106 legal = false; 107 } 108 }else if(type == OPEROPERATION_MUL){ 109 if(matrix_a.length != matrix_b[0].length){ 110 legal = false; 111 } 112 } 113 return legal; 114 } 115 public static void main(String[] args) { 116 int a[][] = new int[][]{{1,2},{3,4}}; 117 int b[][] = new int[][]{{5,6},{7,8}}; 118 BasicMatrixMath bmm = new BasicMatrixMath(); 119 System.out.println("addition two matrix"); 120 int[][] result = bmm.additive(a, b); 121 for (int i = 0; i < result.length; i++) { 122 for (int j = 0; j < result[0].length; j++) { 123 System.out.print("\t" + result[i][j]); 124 } 125 System.out.println(); 126 } 127 System.out.println("substract two matrix"); 128 int[][] result1 = bmm.subtraction(a, b); 129 for (int i = 0; i < result1.length; i++) { 130 for (int j = 0; j < result1[0].length; j++) { 131 System.out.print("\t" + result1[i][j]); 132 } 133 System.out.println(); 134 } 135 System.out.println("multiplex one matrix"); 136 result = bmm.multiplication_b(a, 3); 137 for (int i = 0; i < result.length; i++) { 138 for (int j = 0; j < result[0].length; j++) { 139 System.out.print("\t" + result[i][j]); 140 } 141 System.out.println(); 142 } 143 System.out.println("multiplex two matrix"); 144 result = bmm.multiplication_a(a, b); 145 for (int i = 0; i < result.length; i++) { 146 for (int j = 0; j < result[0].length; j++) { 147 System.out.print("\t" + result[i][j]); 148 } 149 System.out.println(); 150 } 151 152 } 153 }
java實現矩陣基本運算