您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
本文章向大家介绍使用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怎么实现一个矩阵加减乘除运算功能的全部内容了,希望大家多多支持亿速云!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。