您好,登錄后才能下訂單哦!
本文章向大家介紹使用Java怎么實(shí)現(xiàn)一個(gè)矩陣加減乘除運(yùn)算功能的基本知識點(diǎn)總結(jié)和需要注意事項(xiàng),具有一定的參考價(jià)值,需要的朋友可以參考一下。
Java的特點(diǎn)有哪些 1.Java語言作為靜態(tài)面向?qū)ο缶幊陶Z言的代表,實(shí)現(xiàn)了面向?qū)ο罄碚?,允許程序員以優(yōu)雅的思維方式進(jìn)行復(fù)雜的編程。 2.Java具有簡單性、面向?qū)ο?、分布式、安全性、平臺(tái)獨(dú)立與可移植性、動(dòng)態(tài)性等特點(diǎn)。 3.使用Java可以編寫桌面應(yīng)用程序、Web應(yīng)用程序、分布式系統(tǒng)和嵌入式系統(tǒng)應(yīng)用程序等。
public class MatrixOperation { public static int[][] add(int[][] matrix_a, int[][] matrix_b) { int row = matrix_a.length; int col = matrix_a[0].length; int[][] result = new int[row][col]; if (row != matrix_b.length || col != matrix_b[0].length) { System.out.println("Fault"); } else { for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { result[i][j] = matrix_a[i][j] + matrix_b[i][j]; } } } return result; } public static int[][] sub(int[][] matrix_a, int[][] matrix_b) { int row = matrix_a.length; int col = matrix_a[0].length; int[][] result = new int[row][col]; if (row != matrix_b.length || col != matrix_b[0].length) { System.out.println("Fault"); } else { for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { result[i][j] = matrix_a[i][j] - matrix_b[i][j]; } } } return result; } public static int[][] dot(int[][] matrix_a, int[][] matrix_b) { /* * matrix_a's dimention m*p matrix_b's dimention p*n. return dimention * m*n */ int row = matrix_a.length; int col = matrix_a[0].length; int[][] result = new int[row][col]; if (col != matrix_b.length) { System.out.println("Fault"); } else { for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { result[i][j] = 0; for (int k = 0; k < col; k++) { result[i][j] += matrix_a[i][k] * matrix_b[k][j]; } } } } return result; } public static int[][] dot(int[][] matrix_a, int b) { int row = matrix_a.length; int col = matrix_a[0].length; int[][] result = new int[row][col]; for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { result[i][j] = matrix_a[i][j] * b; } } return result; } public static int[][] mul(int[][] matrix_a, int[][] matrix_b) { /* * matrix_a's dimention m*n matrix_b's dimention m*n. return dimention * m*n */ int row = matrix_a.length; int col = matrix_a[0].length; int[][] result = new int[row][col]; if (row != matrix_b.length || col != matrix_b[0].length) { System.out.println("Fault"); } else { for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { result[i][j] = matrix_a[i][j] * matrix_b[i][j]; } } } return result; } public static int[][] transport(int[][] matrix_a) { int row = matrix_a.length; int col = matrix_a[0].length; int[][] result = new int[row][col]; for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { result[j][i] = matrix_a[i][j]; } } return result; } public static void print(int[][] matrix) { int row = matrix.length; int col = matrix[0].length; for (int i = 0; i < row; i++) { System.out.print("["); for (int j = 0; j < col; j++) { System.out.print(matrix[i][j]); if (j != col - 1) { System.out.print(", "); } } System.out.print("]\n"); } } public static void main(String[] args) { int[][] a = { { 1, 2 }, { 3, 4 } }; int[][] b = { { 7, 8 }, { 6, 5 } }; int[][] c = add(a, b); System.out.println("億速云測試結(jié)果如下:"); System.out.println("matrix a = "); print(a); System.out.println("matrix b = "); print(b); System.out.println("matrix a + b = "); print(c); c = sub(a, b); System.out.println("matrix a - b = "); print(c); int[][] d = dot(a, b); System.out.println("matrix a dot b = "); print(d); int[][] e = dot(a, 3); System.out.println("matrix a * 3 = "); print(e); int[][] f = transport(a); System.out.println("matrix a.T = "); print(f); int[][] g = mul(a, b); System.out.println("matrix a * b = "); print(g); } }
運(yùn)行結(jié)果:
以上就是小編為大家?guī)淼氖褂肑ava怎么實(shí)現(xiàn)一個(gè)矩陣加減乘除運(yùn)算功能的全部內(nèi)容了,希望大家多多支持億速云!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。