Java implements operations such as matrix addition, subtraction, multiplication, division, and transformation.

Source: Internet
Author: User

Java implements operations such as matrix addition, subtraction, multiplication, division, and transformation.

This example describes how to implement operations such as matrix addition, subtraction, multiplication, division, and conversion in Java. We will share this with you for your reference. The details are as follows:

For beginners in Java, compile a matrix budget program and use it as a tool for later algorithm writing.

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; I Nt [] [] 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 ("the result of the help house test is as follows:"); 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. T = "); print (f); int [] [] g = mul (a, B); System. out. println ("matrix a * B ="); print (g );}}

Running result:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.