UJMP 矩陣庫的基本用法

例子來自於官方文檔,自己做了一點註釋和輸出

    @Test
    public void testUJMP() {

        //初始化一個4X4的矩陣
        Matrix dense = DenseMatrix.Factory.zeros(4, 4);
        Matrix dense2 = DenseMatrix.Factory.zeros(4, 4);
        //輸出矩陣的行和列的長度
        System.out.println("dense rowcount colcount " + dense.getRowCount() + "  " + dense.getColumnCount());;
        //利用行和列進行矩陣的賦值
        for (int i = 0; i < dense.getRowCount(); ++i){
            for (int j = 0 ; j < dense.getColumnCount(); ++j){
                //可以使用setXXX來進行矩陣的賦值,其中第一個參數是值,第二個參數是行,第三個參數是列
                dense.setAsInt((i*j + (int)(Math.pow(i + 1, j))), i, j);
                dense2.setAsInt(i + j, i , j);
            }
        }
        Math.pow(1,2);
        //輸出矩陣
        System.out.println(dense);
        System.out.println("dense2 \n" + dense2);

        //初始化一個稀疏矩陣
        Matrix spares = SparseMatrix.Factory.zeros(400,500);
        //這裏使用另一種方法獲取其行和列
        // long[] getSize()  是一個維度爲2的矩陣,第一個是行,第二個數是列
        for (int i = 0; i < spares.getSize()[0]; ++i){
            for (int j = 0; j< spares.getSize()[1]; ++j){
                spares.setAsBigDecimal(BigDecimal.valueOf(i *j), i, j);
            }
        }
        System.out.println(spares.getSize()[0] + "   " + spares.getSize()[1]);
        //System.out.println("spares Matrix : \n" + spares);

        /*****************************************
         *      矩陣的運算
         *****************************************/

        //轉置
        Matrix transpose = dense.transpose();
        System.out.println(transpose);
        //兩個矩陣求和

        Matrix sum = dense.plus(dense2);
        System.out.println("sum \n" + sum);

        //兩個矩陣相減
        Matrix difference = dense.minus(dense2);
        System.out.println("difference \n" + difference);

        //矩陣相乘
        Matrix matrixProduct = dense.mtimes(dense2);
        System.out.println("matrixProduct\n" + matrixProduct);

        //矩陣 k*M (K 爲常數, M爲矩陣)
        Matrix scaled = dense.times(2.0);
        System.out.println("scaled \n" + scaled);

        //矩陣的逆
        Matrix inverse = dense.inv();
        System.out.println(inverse);

        //僞逆矩陣 廣義逆矩陣
        Matrix pesudoInv = dense.pinv();
        System.out.println(pesudoInv);

        //求矩陣的行列式
        double determiant = dense.det();
        System.out.println("determiant = " + determiant);

        //矩陣的奇異值分解
        Matrix[] sigularValueDecompostion = dense.svd();
        for (int i = 0; i < sigularValueDecompostion.length; ++i){
            System.out.println("sigularValueDecompostion " + i + "= \n" + sigularValueDecompostion[i]);
        }

        //求矩陣的特徵值
        Matrix[] eigenValueDecompostion = dense.eig();
        for (int i = 0; i < eigenValueDecompostion.length; ++i){
            System.out.println("eigenValueDecompostion " + i + "= \n" + eigenValueDecompostion[i]);
        }

        //矩陣的LU分解,將矩陣分解成一個上三角矩陣和下三角矩陣的乘積
        Matrix[] luValueDecompostion = dense.lu();
        for (int i = 0; i < luValueDecompostion.length; ++i){
            System.out.println("luValueDecompostion " + i + "= \n" + luValueDecompostion[i]);
        }

        //qr分解  半正交矩陣與一個上三角矩陣的積,常用來求解線性最小二乘問題
        Matrix[] qrDecomposition = dense.qr();
        for (int i = 0; i < qrDecomposition.length; ++i){
            System.out.println("qrDecomposition " + i + "= \n" + qrDecomposition[i]);
        }


        //Cholesky分解 對於每一個正定矩陣 Cholesky分解都存在
        Matrix choleskyDecomposition = dense.chol();
        System.out.println("choleskyDecomposition \n" + choleskyDecomposition);

    }
發佈了57 篇原創文章 · 獲贊 108 · 訪問量 19萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章