Java实现矩阵加减乘除转制等运算

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/miangangzhen/article/details/52275480


Java初学,编写矩阵预算程序,当做工具,以便以后写算法时使用。




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("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);
    }
}



展开阅读全文

没有更多推荐了,返回首页