基于apache-commons-math3实现矩阵运算
今天是:
我需要三件东西:爱情友谊和图书。然而这三者之间何其相通!炽热的爱情可以充实图书的内容,图书又是人们最忠实的朋友。
User Image 基于apache-commons-math3实现矩阵运算 2020-02-29

矩阵在计算机科学,应用数学,统计等学科有着非常重要的意义  。 现在用在java中实现矩阵的运算。重要基于math3 官网传送门https://commons.apache.org/proper/commons-math/

public class MatrixOperate {
    public void operate() {
        double[][] dataA = getMatrixData(3);
        RealMatrix matrixA = createMatrix(dataA);
        System.out.println("新创建的创建的矩阵A:===========>>");
        printMatrix(matrixA);

        double[][] dataB = getMatrixData(3);
        RealMatrix matrixB = createMatrix(dataB);
        System.out.println("新创建的创建的矩阵B:===========>>");
        printMatrix(matrixB);

        //矩阵乘法;
        RealMatrix mulMatrix = mulMatrix(matrixA, matrixB);
        //矩阵加法;
        RealMatrix addMatrix = addMatrix(matrixA, matrixB);
        //矩阵减法
        RealMatrix subMatrix = subMatrix(matrixA, matrixB);
        //矩阵转置
        RealMatrix transMatrix = matrixA.transpose();
        System.out.println("矩阵A的转置矩阵:===========>>");
        printMatrix(transMatrix);

        //逆矩阵
        RealMatrix inverseMatrix = inverseMatrix(matrixA);
        System.out.println("矩阵A的逆矩阵:===========>>");
        printMatrix(inverseMatrix);

        //乘以或除以常数
        RealMatrix scalarMatrix = matrixA.scalarMultiply(2);
        System.out.println("矩阵A乘以常数2:===========>>");
        printMatrix(scalarMatrix);

        RealMatrix matrixARow1 = matrixA.getRowMatrix(1);
        System.out.println("矩阵A第二行:===========>>");
        printMatrix(matrixARow1);

        RealMatrix matrixAColumn1 = matrixA.getColumnMatrix(1);
        System.out.println("矩阵A第二列:===========>>");
        printMatrix(matrixAColumn1);

    }

    private RealMatrix inverseMatrix(RealMatrix matrixA) {
        RealMatrix inverse = new LUDecomposition(matrixA).getSolver().getInverse();
        RealMatrix inverseb = MatrixUtils.inverse(matrixA);//默认使用QR-decomposition计算
        //printMatrix(inverseb);
        return inverse;
    }

    private RealMatrix subMatrix(RealMatrix matrixA, RealMatrix matrixB) {
        return matrixA.subtract(matrixB);
    }

    private RealMatrix addMatrix(RealMatrix matrixA, RealMatrix matrixB) {
        return matrixA.add(matrixB);
    }

    private RealMatrix mulMatrix(RealMatrix matrixA, RealMatrix matrixB) {
        return matrixA.multiply(matrixB);
    }

    private RealMatrix createMatrix(double[][] data) {
        RealMatrix matrix = new Array2DRowRealMatrix(data);
        //RealMatrix matrix = MatrixUtils.createRealMatrix(data);
        return matrix;
    }

    public double[][] getMatrixData(int n) {
        double[][] rmdata = new double[n][n];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                rmdata[i][j] = RandomUtils.nextInt(1, 10 * n);
            }
        }
        return rmdata;
    }

    public void printMatrix(RealMatrix matrix) {
        double[][] matrixData = matrix.getData();
        for (double[] d : matrixData) {
            System.out.println(Arrays.toString(d));
        }
    }
}

//输出

新创建的创建的矩阵A:===========>>
[7.0, 27.0, 17.0]
[28.0, 10.0, 16.0]
[26.0, 13.0, 7.0]
新创建的创建的矩阵B:===========>>
[15.0, 15.0, 8.0]
[12.0, 27.0, 14.0]
[17.0, 10.0, 12.0]
矩阵A的转置矩阵:===========>>
[7.0, 28.0, 26.0]
[27.0, 10.0, 13.0]
[17.0, 16.0, 7.0]
矩阵A的逆矩阵:===========>>
[-0.02046870364876891, 0.004746366063482642, 0.03886087214476417]
[0.03263126668644319, -0.05829130821714626, 0.0539899139721151]
[0.015425689706318598, 0.09062592702462179, -0.10175022248590923]
矩阵A乘以常数2:===========>>
[14.0, 54.0, 34.0]
[56.0, 20.0, 32.0]
[52.0, 26.0, 14.0]
矩阵A第二行:===========>>
[28.0, 10.0, 16.0]
矩阵A第二列:===========>>
[27.0]
[10.0]
[13.0]
 

总结:要对矩阵的定义有比较清晰的理解,才能验证矩阵操作后是否正确。利用矩阵可解线性方程,一些算法,统计问题。

分享到:

我的

类型标签

外部链接

网站访问总量