基本矩阵运算的Java实现

基本矩阵运算的Java实现

分类: 图像处理 2537人阅读 评论(3) 收藏 举报

一: 矩阵的加法与减法

规则:矩阵的加法与减法要求两个矩阵的行列完全相等,方可以完成两个矩阵的之间的运算。

举例说明如下


二:矩阵的乘法

规则:矩阵的乘法要求两个矩阵符合A(mx k),  B( k x n)即矩阵A的列数与矩阵B的行数相等,否

则无法完成矩阵运算。举例说明如下:


Java代码如下:

[java] view plaincopy
  1. package pet.shop;  
  2.   
  3. public class BasicMatrixMath {  
  4.     public final static int OPERATION_ADD = 1;  
  5.     public final static int OPERATION_SUB = 2;  
  6.     public final static int OPERATION_MUL = 4;  
  7.       
  8.     /** 
  9.      * To be able to add two matrices, they must be of the same size 
  10.      * @param matrixa 
  11.      * @param matrixb 
  12.      */  
  13.     public int[][] add(int[][] matrixa, int[][] matrixb) {  
  14.         if(legalOperation(matrixa, matrixb, OPERATION_ADD)) {  
  15.             for(int i=0; i<matrixa.length; i++) {  
  16.                 for(int j=0; j<matrixa[0].length; j++) {  
  17.                     matrixa[i][j] = matrixa[i][j] + matrixb[i][j];  
  18.                 }  
  19.             }  
  20.         }  
  21.         return matrixa;  
  22.     }  
  23.       
  24.     /** 
  25.      * To be able to substract two matrices, they must be of the same size 
  26.      *  
  27.      * @param matrixa 
  28.      * @param matrixb 
  29.      */  
  30.     public int[][] substract(int[][] matrixa, int[][] matrixb) {  
  31.         if(legalOperation(matrixa, matrixb, OPERATION_SUB)) {  
  32.             for(int i=0; i<matrixa.length; i++) {  
  33.                 for(int j=0; j<matrixa[0].length; j++) {  
  34.                     matrixa[i][j] = matrixa[i][j] - matrixb[i][j];  
  35.                 }  
  36.             }  
  37.         }  
  38.         return matrixa;  
  39.     }  
  40.       
  41.     /** 
  42.      *  
  43.      * @param matrixa 
  44.      * @param matrixb 
  45.      */  
  46.     public int[][] multiplication(int[][] matrixa, int[][] matrixb) {  
  47.         if(legalOperation(matrixa, matrixb, OPERATION_SUB)) {  
  48.             int[][] result = new int[matrixa.length][matrixb[0].length];  
  49.             for(int i=0; i<matrixa.length; i++) {  
  50.                 for(int j=0; j<matrixb[0].length; j++) {  
  51.                     // i will complete this tomorrow @2012/09/17  
  52.                     result[i][j] = calculateSingleResult(matrixa, matrixb, i, j);   
  53.                 }  
  54.             }  
  55.             return result;  
  56.         }   
  57.         else  
  58.         {  
  59.             return null;  
  60.         }  
  61.     }  
  62.       
  63.     private int calculateSingleResult(int[][] matrixa, int[][] matrixb, int row, int col) {  
  64.         int result = 0;  
  65.         for(int k=0; k<matrixa[0].length; k++) {  
  66.             result += matrixa[row][k] * matrixb[k][col];  
  67.         }  
  68.         return result;  
  69.     }  
  70.   
  71.     /** 
  72.      *  
  73.      * @param matrixa 
  74.      * @param b 
  75.      */  
  76.     public int[][] multiplication(int[][] matrixa, int b) {  
  77.         for(int i=0; i<matrixa.length; i++) {  
  78.             for(int j=0; j<matrixa[0].length; j++) {  
  79.                 matrixa[i][j] = matrixa[i][j] * b;  
  80.             }  
  81.         }  
  82.         return matrixa;  
  83.     }  
  84.       
  85.     /** 
  86.      * validate whether the parameters is valid parameters. 
  87.      *  
  88.      * @param a 
  89.      * @param b 
  90.      * @param type 
  91.      * @return 
  92.      */  
  93.     private boolean legalOperation(int[][] a, int[][] b, int type) {  
  94.         boolean legal = true;  
  95.         if(type == OPERATION_ADD || type == OPERATION_SUB)  
  96.         {  
  97.             if(a.length != b.length || a[0].length != b[0].length) {  
  98.                 legal = false;  
  99.             }  
  100.         }   
  101.         else if(type == OPERATION_MUL)  
  102.         {  
  103.             if(a[0].length != b.length) {  
  104.                 legal = false;  
  105.             }  
  106.         }  
  107.         return legal;  
  108.     }  
  109.       
  110.     /** 
  111.      *  test code here !!!! 
  112.      * @param args 
  113.      */  
  114.     public static void main(String[] args) {  
  115.         int[][] a = new int[][]{{1,2},{3,4}};  
  116.         int[][] b = new int[][]{{78}, {65}};  
  117.         BasicMatrixMath bmm = new BasicMatrixMath();  
  118.           
  119.         System.out.println("addition two matrix");  
  120.         int[][] result = bmm.add(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(" " + result[i][j]);  
  124.             }  
  125.             System.out.println();  
  126.         }  
  127.           
  128.         System.out.println("substract two matrix");  
  129.         result = bmm.substract(a, b);  
  130.         for(int i=0; i<result.length; i++) {  
  131.             for(int j=0; j<result[0].length; j++) {  
  132.                 System.out.print(" " + result[i][j]);  
  133.             }  
  134.             System.out.println();  
  135.         }  
  136.           
  137.         System.out.println("multiplex one matrix");  
  138.         result = bmm.multiplication(a, 3);  
  139.         for(int i=0; i<result.length; i++) {  
  140.             for(int j=0; j<result[0].length; j++) {  
  141.                 System.out.print(" " + result[i][j]);  
  142.             }  
  143.             System.out.println();  
  144.         }  
  145.           
  146.         System.out.println("multiplex two matrix");  
  147.         result = bmm.multiplication(a, b);  
  148.         for(int i=0; i<result.length; i++) {  
  149.             for(int j=0; j<result[0].length; j++) {  
  150.                 System.out.print(" " + result[i][j]);  
  151.             }  
  152.             System.out.println();  
  153.         }     
  154.     }  
  155. }  
原文地址:https://www.cnblogs.com/jamesf/p/4751601.html