使用Java怎么实现一个矩阵加减乘除运算功能
本文章向大家介绍使用Java怎么实现一个矩阵加减乘除运算功能的基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
目前创新互联建站已为数千家的企业提供了网站建设、域名、网络空间、网站运营、企业网站设计、禄丰网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。
Java的特点有哪些
Java的特点有哪些 1.Java语言作为静态面向对象编程语言的代表,实现了面向对象理论,允许程序员以优雅的思维方式进行复杂的编程。 2.Java具有简单性、面向对象、分布式、安全性、平台独立与可移植性、动态性等特点。 3.使用Java可以编写桌面应用程序、Web应用程序、分布式系统和嵌入式系统应用程序等。
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("创新互联测试结果如下:"); 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); } }
运行结果:
以上就是小编为大家带来的使用Java怎么实现一个矩阵加减乘除运算功能的全部内容了,希望大家多多支持创新互联!
网页题目:使用Java怎么实现一个矩阵加减乘除运算功能
网页路径:http://hbruida.cn/article/ihcpjc.html