Matrix operation is needed in my job. I should translate matrix operation in math to java language.

So, I create a class "Matrix", it has some functions to realize some operation of matrix.

The code as follow: 

 
  
  1. import java.util.Arrays;  
  2.  
  3. public class Matrix {  
  4.  
  5.     private float[][] matrix;  
  6.     private int m;  
  7.     private int n;  
  8.  
  9.     public int getM() {  
  10.         return m;  
  11.     }  
  12.  
  13.     public int getN() {  
  14.         return n;  
  15.     }  
  16.  
  17.     public Matrix() {  
  18.         this(00);  
  19.     }  
  20.  
  21.     public Matrix(int m, int n) {  
  22.         this.m = m;  
  23.         this.n = n;  
  24.         this.matrix = new float[m][n];  
  25.     }  
  26.  
  27.     public Matrix(float[][] martix) {  
  28.         this.matrix = martix;  
  29.         calcDim();  
  30.     }  
  31.  
  32.     private void calcDim() {  
  33.         m = matrix.length;  
  34.         n = matrix[0].length;  
  35.     }  
  36.  
  37.     public void setMatrixElement(int i, int j, float value) {  
  38.  
  39.         try {  
  40.             if (i > m || j > n)  
  41.                 throw new Exception("索引越界");  
  42.         } catch (Exception e) {  
  43.             e.printStackTrace();  
  44.         }  
  45.         matrix[i][j] = value;  
  46.     }  
  47.  
  48.     public float getMartixElement(int i, int j) {  
  49.         return matrix[i][j];  
  50.     }  
  51.  
  52.     /**  
  53.      * 求矩阵M转置  
  54.      *   
  55.      * @return 转置矩阵  
  56.      */ 
  57.     public Matrix tranpose() {  
  58.         float[][] temp = new float[n][m];  
  59.         for (int i = 0; i < m; i++) {  
  60.             for (int j = 0; j < n; j++) {  
  61.                 temp[j][i] = matrix[i][j];  
  62.             }  
  63.         }  
  64.         return new Matrix(temp);  
  65.     }  
  66.  
  67.     /**  
  68.      * 矩阵相加  
  69.      *   
  70.      * @param matrix  
  71.      * @return  
  72.      */ 
  73.     public Matrix add(Matrix matrix) {  
  74.         try {  
  75.             if (matrix.getM() != m || matrix.getN() != n)  
  76.                 throw new Exception("矩阵维数不相等,不能相加");  
  77.         } catch (Exception e) {  
  78.             e.printStackTrace();  
  79.         }  
  80.  
  81.         float[][] temp = new float[m][n];  
  82.         for (int i = 0; i < m; i++) {  
  83.             for (int j = 0; j < n; j++) {  
  84.                 temp[i][j] = matrix.getMartixElement(i, j) + this.matrix[i][j];  
  85.             }  
  86.         }  
  87.  
  88.         return new Matrix(temp);  
  89.     }  
  90.  
  91.     /**  
  92.      * 矩阵相减  
  93.      *   
  94.      * @param matrix  
  95.      * @return  
  96.      */ 
  97.     public Matrix reduce(Matrix matrix) {  
  98.         try {  
  99.             if (matrix.getM() != m || matrix.getN() != n)  
  100.                 throw new Exception("矩阵维数不相等,不能相加");  
  101.         } catch (Exception e) {  
  102.             e.printStackTrace();  
  103.         }  
  104.  
  105.         float[][] temp = new float[m][n];  
  106.         for (int i = 0; i < m; i++) {  
  107.             for (int j = 0; j < n; j++) {  
  108.                 temp[i][j] = this.matrix[i][j] - matrix.getMartixElement(i, j);  
  109.             }  
  110.         }  
  111.  
  112.         return new Matrix(temp);  
  113.     }  
  114.  
  115.     /**  
  116.      * 矩阵相乘  
  117.      *   
  118.      * @param matrix  
  119.      * @return  
  120.      */ 
  121.     public Matrix multiply(Matrix matrix) {  
  122.  
  123.         try {  
  124.             if (matrix.getM() != n || matrix.getN() != m)  
  125.                 throw new Exception("矩阵维数不符合相乘要求");  
  126.         } catch (Exception e) {  
  127.             e.printStackTrace();  
  128.         }  
  129.  
  130.         float[][] temp = new float[m][m];  
  131.  
  132.         for (int i = 0; i < m; i++) {  
  133.             for (int k = 0; k < m; k++) {  
  134.                 float t = 0;  
  135.                 for (int j = 0; j < n; j++) {  
  136.                     t += this.matrix[i][j] * matrix.getMartixElement(j, k);  
  137.                 }  
  138.  
  139.                 temp[i][k] = t;  
  140.             }  
  141.         }  
  142.         return new Matrix(temp);  
  143.     }  
  144.  
  145.       
  146.     /**  
  147.      * 格式化输出  
  148.      *     
  149.      */ 
  150.     public String toFormatString() {  
  151.         StringBuffer buf = new StringBuffer();  
  152.         for (int i = 0; i < m; i++) {  
  153.             for (int j = 0; j < n; j++) {  
  154.                 buf.append(matrix[i][j]).append("\t");  
  155.             }  
  156.             buf.append("\n");  
  157.         }  
  158.           
  159.         return buf.toString();  
  160.     }  
  161.  
  162.     @Override 
  163.     public int hashCode() {  
  164.         final int prime = 31;  
  165.         int result = 1;  
  166.         result = prime * result + m;  
  167.         result = prime * result + Arrays.hashCode(matrix);  
  168.         result = prime * result + n;  
  169.         return result;  
  170.     }  
  171.  
  172.     @Override 
  173.     public boolean equals(Object obj) {  
  174.         if (this == obj)  
  175.             return true;  
  176.         if (obj == null)  
  177.             return false;  
  178.         if (getClass() != obj.getClass())  
  179.             return false;  
  180.         Matrix other = (Matrix) obj;  
  181.         if (m != other.m)  
  182.             return false;  
  183.         if (!Arrays.equals(matrix, other.matrix))  
  184.             return false;  
  185.         if (n != other.n)  
  186.             return false;  
  187.         return true;  
  188.     }  
  189.       
  190.