Java矩陣運算包ujmp的基本使用

本人最近在用ujmp包寫一些程序,ujmp包是針對於超大數據量計算的矩陣的運算包,並且有圖形顯示的功能且支持多種文件格式的讀取和輸出,還支持連接數據庫,matlab數據類型和weka數據類型,總體來說非常好用,但是有一個很大的缺陷就是基本沒有相關的示例和文檔,官網上的示例有基本全都過時不能用了,本人總結了一下相關用法,僅供大家參考,代碼並不能運行,知識給大家列出了相應的矩陣運算方式和構造方式,希望能對大家有所幫助。

基本用法如下:

@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);

    }

當然還有一些高級操作如下:

        long m = 5;
        long n = 5;
        /**
         * 製造一個空矩陣
         */
        Matrix emptyMatrix = MatrixFactory.emptyMatrix();
        /**
         * 製造一個m*n隨機矩陣
         */
        Matrix randMatrix = Matrix.factory.rand(m, n);
        /**
         * 製造一個m*n零矩陣
         */
        Matrix zeroMatrix = Matrix.factory.zeros(m, n);
        /**
         * 製造一個m*n對角線爲1其餘元素爲0的矩陣
         */
        Matrix eyeMatrix = Matrix.factory.eye(m, n);
        /**
         * 製造一個m*n全部元素爲1的矩陣
         */
        Matrix oneMatrix = Matrix.factory.ones(m, n);
        /**
         * 矩陣的相關操作
         */
        // 矩陣與數值的相關運算,意思大家根據英語的含義就能看出,這裏就不解釋了
        Matrix res_1 = oneMatrix.times(10);
        Matrix res_2 = oneMatrix.divide(10);
        Matrix res_3 = oneMatrix.plus(10);
        Matrix res_4 = oneMatrix.minus(10);
        /**
         * 矩陣與矩陣的相關運算 加和減函數都不用變,乘的話要加上m表示matrix間計算
         */
        Matrix res_5 = oneMatrix.mtimes(randMatrix);
        Matrix res_7 = oneMatrix.plus(randMatrix);
        Matrix res_8 = oneMatrix.minus(randMatrix);
        /**
         * 求轉置求逆,這裏有三種返回型,分別是link orig new 計算時間new > orig > link 無返回型和orig的時間類似
         */
        Matrix res_9 = oneMatrix.transpose(Ret.LINK);
        Matrix res_10 = oneMatrix.transpose(Ret.ORIG);
        Matrix res_11 = oneMatrix.transpose(Ret.NEW);
        Matrix res_12 = oneMatrix.inv();
        // 選取子矩陣
        Matrix res_13 = oneMatrix.subMatrix(Ret.NEW, startRow, startColumn,
                endRow, endColumn);
        // 選取行
        Matrix res_14 = oneMatrix.selectRows(returnType, rows);
        // 選取列
        Matrix res_15 = oneMatrix.selectColumns(returnType, columns);
        // 按第i列進行排序,reverse表示返回的排序矩陣是按正序還是逆序
        Matrix res_16 = oneMatrix.sortrows(returnType, column, reverse);
        // 將矩陣的所有數值相加得到的返回值
        Matrix res_17 = oneMatrix.getValueSum();
        // 選去矩陣的行和列
        Matrix res_18 = oneMatrix.getColumnCount();
        Matrix res_19 = oneMatrix.getRowCount();
        //判斷矩陣否和一個矩陣或一個值相等,相等的話在相應的位置設置爲爲true否則爲false,
        //如果要看相等的個數的總和則可再繼續用一個getvaluecount函數即可
        Matrix res_20 = oneMatrix.eq(returnType, matrix);
        matrix res_21 = oneMatrix.eq(returnType, value)
        當矩陣返回類型爲RET.ORIG的時候不能使用任何有可能改變矩陣大小的操作(除非自己知道確實不會改變),例如轉置、選取行列、子矩陣等~~~~~

最後做一點補充

package MatrixPFTest.yi.maytwenty;

import org.ujmp.core.Matrix;
import org.ujmp.core.MatrixFactory;
import org.ujmp.core.calculation.Calculation.Ret;

public class PerfomaceTest {
    public static void main(String[] args) {
        long begin, end;
        /**
         * test變test2才變 *********test2不能被改變
         */

        long m = 725, n = 20;
        // Matrix test_1 = Matrix.factory.rand(5, 5);
        // test_1.showGUI();
        // Matrix test_2 = test_1.transpose(Ret.ORIG);
        // test_2.showGUI();
        // Matrix test_3 = test_2.mtimes(Matrix.factory.ones(5, 5).times(2));
        // test_3.showGUI();
        begin = System.currentTimeMillis();
        Matrix res = Matrix.factory.rand(m, n);
        Matrix res0 = Matrix.factory.rand(m, n);
        end = System.currentTimeMillis();
        Constans.sop("構建矩陣耗時" + (end - begin) + "ms");
        // res.setLabel("res");
        // res.showGUI();

        begin = System.currentTimeMillis();
        Matrix res_1_trannull = res.transpose();
        end = System.currentTimeMillis();
        Constans.sop("res_1_trannull-耗時" + (end - begin) + "ms");

        begin = System.currentTimeMillis();
        Matrix res_2_tranlink = res.transpose(Ret.LINK);
        end = System.currentTimeMillis();
        Constans.sop("res_2_tranlink-耗時" + (end - begin) + "ms");
        // res_2_tranlink.setLabel("res_2_tranlink");
        // res_2_tranlink.setAsDouble(10, 0, 0);
        // res_2_tranlink.showGUI();

        /**
         * 進行矩陣賦值,兩個矩陣式同一個矩陣,除非用copy()
         */
        Matrix xxxMatrix = res_2_tranlink;
        xxxMatrix.setAsDouble(10, 0, 0);
        xxxMatrix.showGUI();
        /**
         * 對LINK的矩陣進行賦值
         */
        res_2_tranlink = MatrixFactory.ones(1, 1);
        res_2_tranlink.setAsDouble(110, 0, 0);
        res_2_tranlink.showGUI();

        /**
         * 選取特定行與列
         */
        begin = System.currentTimeMillis();
        Matrix res_3 = res_2_tranlink.selectColumns(Ret.NEW, 10);
        end = System.currentTimeMillis();
        res_3.showGUI();
        Constans.sop("選取列-NEW-耗時" + (end - begin) + "ms");

        begin = System.currentTimeMillis();
        Matrix res_4 = res_2_tranlink.selectColumns(Ret.LINK, 0);
        end = System.currentTimeMillis();
        res_4.setAsDouble(10, 0, 0);
        res_4.showGUI();
        Constans.sop("選取列-link-耗時" + (end - begin) + "ms");

        /**
         * 求逆耗時較長,但是inv和invSymm相差無幾
         */
        for (int i = 0; i < 1; ++i) {
            begin = System.currentTimeMillis();
            Matrix res_5 = res_2_tranlink.inv();
            end = System.currentTimeMillis();
            Constans.sop("inv-耗時" + (end - begin) + "ms");
        }

        /**
         * 獲取行數,列數
         */
        begin = System.currentTimeMillis();
        long res_rowcount = res_2_tranlink.getRowCount();
        end = System.currentTimeMillis();
        Constans.sop("getRowCount-耗時" + (end - begin) + "ms");

        /**
         * 矩陣相乘的檢測
         */

        begin = System.currentTimeMillis();
        Matrix res_muti_link = res_2_tranlink.mtimes(Ret.LINK, false, res0);
        end = System.currentTimeMillis();
        res_muti_link.setAsDouble(100, 0, 0);
        // res_muti_link.showGUI();
        Constans.sop("res_muti_link-耗時" + (end - begin) + "ms");

        // 這裏是LINK後和LINK後的矩陣相乘,但是返回的是NEW,所以可以改變值
        Matrix afterlinklink = res_muti_link.mtimes(res_2_tranlink);
        afterlinklink.setAsDouble(100, 0, 0);
        afterlinklink.showGUI();
        begin = System.currentTimeMillis();
        Matrix res_muti_new = res_2_tranlink.mtimes(Ret.NEW, false, res0);
        end = System.currentTimeMillis();
        res_muti_new.showGUI();
        Constans.sop("res_muti_new-耗時" + (end - begin) + "ms");

        /**
         * 對不是LINK的矩陣選取行或列再改變變量值,使用LINK的話都會受到影響
         */
        Matrix beforeMatrix = Matrix.factory.rand(5, 5);
        beforeMatrix.setLabel("beforeMatrix");
        beforeMatrix.showGUI();

        Matrix nowMatrix = beforeMatrix.selectRows(Ret.NEW, 0);
        nowMatrix.setAsDouble(10, 0, 0);
        nowMatrix.setLabel("nowMatrix");
        nowMatrix.showGUI();

        Matrix laterMatrix = beforeMatrix.transpose(Ret.LINK);
        laterMatrix.setLabel("laterMatrix");
        // laterMatrix.showGUI();
        Matrix xx = laterMatrix.minus(Ret.LINK, false, 10);
        double xxd = xx.getAsDouble(0, 0);
        Constans.sop(xxd);
        // xx.showGUI();

    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章