<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Change Dir

    先知cd——熱愛生活是一切藝術的開始

    統計

    留言簿(18)

    積分與排名

    “?!眰兊牟┛?/h3>

    各個公司技術

    我的鏈接

    淘寶技術

    閱讀排行榜

    評論排行榜

    Commons Math學習筆記——矩陣

     

    1.2 矩陣

    看其他篇章到目錄選擇。

    今天來第二篇:矩陣——Matrix

    Mathorg.apache.commons.math.linear里對矩陣的表示是有一個層次結構的。

    最頂層的AnyMatrix是一個基本的interface。下面有3sub interfaceBigMatrix, FieldMatrix<T>, RealMatrix。而每個sub interface分別被相應的矩陣類實現。整個矩陣的層次結構也就出來了。不過其中的BigMatrix已經不用了。被Array2DRowFieldMatrix替代了。


     

    具體拿RealMatrix來說。

    RealMatrix是一個可以表示實數類型數據的矩陣接口,實現RealMatrix接口的類有AbstractRealMatrix。它的子類有Array2DRowRealMatrix, BlockRealMatrix, OpenMapRealMatrix, RealMatrixImpl。

     

    今天以Array2DRowRealMatrix為例研究一下矩陣Matrix都有哪些操作。

    我認為通常最簡單的學習方式就是直接看代碼樣例,為此,我寫了使用Matrix的樣例代碼,通過代碼的演示來看看如何使用Array2DRowRealMatrix

    Array2DRowRealMatrix的內部實現是一個2double類型的數組double [][]data;。它的getData方法可以返回對應的數組表示。LU decomposition用來進行矩陣的分解及相關操作,Array2DRowRealMatrix中有幾個方法已經過時,均被LU decomposition取代。像矩陣的求逆運算、特征值以及奇異性等。矩陣的一些分解運算,我想還是放到后面再研究吧。代碼一次太多也不好,嘻嘻。

    具體的代碼實現里有注釋和輸出提示,我想這樣的代碼大家運行后基本就能完全理解Array2DRowRealMatrix的所有操作了。

     1/**
     2 * 
     3 */

     4package algorithm.math;
     5
     6import org.apache.commons.math.linear.Array2DRowRealMatrix;
     7
     8/**
     9 * @author Jia Yu
    10 * @date 2010-11-18
    11 */

    12public class MatrixTest {
    13
    14    public static void matrix() {
    15        double[][] data1 = { 1d, 2d, 3d }{ 2d, 5d, 3d }{ 1d, 0d, 8d } };
    16        double[][] t_data = -40d, 16d, 9d }{ 13d, -5d, -3d },
    17                { 5d, -2d, -1d } }
    ;
    18
    19        Array2DRowRealMatrix matrix1 = new Array2DRowRealMatrix(data1);
    20        Array2DRowRealMatrix t_mat = new Array2DRowRealMatrix(t_data);
    21        // output directly
    22        System.out.println("matrix is " + matrix1);
    23        // is square
    24        System.out.println("it is square matrix! : " + matrix1.isSquare());
    25        // dimension of row and column
    26        System.out.println("row dimension is " + matrix1.getRowDimension());
    27        System.out.println("column dimension is "
    28                + matrix1.getColumnDimension());
    29        // matrix add
    30        System.out.println("mat1 + mat1 = " + matrix1.add(matrix1));
    31        System.out.println("mat1 + 5 = " + matrix1.scalarAdd(5.0));
    32        // matrix sub
    33        System.out.println("mat1 - mat1 = " + matrix1.subtract(matrix1));
    34        // matrix norm
    35        System.out.println("the maximum absolute row sum norm is "
    36                + matrix1.getNorm());
    37        // matrix multiply
    38        System.out.println("mat1 * t_mat = " + matrix1.multiply(t_mat));
    39        System.out.println("mat1 * 5.0 = " + matrix1.scalarMultiply(5));
    40        System.out.println("t_mat * mat1 = " + matrix1.preMultiply(t_mat));
    41        // matrix trace
    42        System.out.println("the trace is " + matrix1.getTrace());
    43        // matrix transpose
    44        System.out.println("the transpose of mat1 is " + matrix1.transpose());
    45        // matrix to vector
    46        System.out
    47                .println("the first row vector is " + matrix1.getRowVector(0));
    48        // matrix get sub matrix of selected rows and columns
    49        System.out.println("sub matrix of mat1 is "
    50                + matrix1.getSubMatrix(new int[] 02 }new int[] 12 }));
    51    }

    52
    53    /**
    54     * @param args
    55     */

    56    public static void main(String[] args) {
    57        // TODO Auto-generated method stub
    58        matrix();
    59    }

    60}

    61

     運行結果:

    matrix is Array2DRowRealMatrix{{1.0,2.0,3.0},{2.0,5.0,3.0},{1.0,0.0,8.0}}
    it is square matrix! : true
    row dimension is 3
    column dimension is 3
    mat1 + mat1 = Array2DRowRealMatrix{{2.0,4.0,6.0},{4.0,10.0,6.0},{2.0,0.0,16.0}}
    mat1 + 5 = Array2DRowRealMatrix{{6.0,7.0,8.0},{7.0,10.0,8.0},{6.0,5.0,13.0}}
    mat1 - mat1 = Array2DRowRealMatrix{{0.0,0.0,0.0},{0.0,0.0,0.0},{0.0,0.0,0.0}}
    the maximum absolute row sum norm is 14.0
    mat1 * t_mat = Array2DRowRealMatrix{{1.0,0.0,0.0},{0.0,1.0,0.0},{0.0,0.0,1.0}}
    mat1 * 5.0 = Array2DRowRealMatrix{{5.0,10.0,15.0},{10.0,25.0,15.0},{5.0,0.0,40.0}}
    t_mat * mat1 = Array2DRowRealMatrix{{1.0,0.0,0.0},{0.0,1.0,0.0},{0.0,0.0,1.0}}
    the trace is 14.0
    the transpose of mat1 is Array2DRowRealMatrix{{1.0,2.0,1.0},{2.0,5.0,0.0},{3.0,3.0,8.0}}
    the first row vector is {1; 2; 3}
    sub matrix of mat1 is Array2DRowRealMatrix{{2.0,3.0},{0.0,8.0}}

    AnyMatrix接口只定義了最基本的操作,獲取維度和判斷方陣。

    RealMatrix接口擴展了AnyMatrix,定義了一些操作,比如加減乘等。

    AbstractRealMatrix抽象類實現了RealMatrix,定義了更多的get方法,可以獲得更多矩陣相關的參數,比如矩陣的秩、矩陣的跡、矩陣的特征值和矩陣的轉置等。

    Array2DRowRealMatrix繼承了AbstractRealMatrix,將里面的抽象方法全部實現。

    其中的multiply矩陣乘法運算,multiply返回的是this*m;而preMultiply返回的是m*this。

    另外值得一提的是,linear包里有MatrixUtils類提供了一系列靜態方法用來檢測矩陣,其中的方法主要以check***為主,檢測矩陣的合法性。

    相關資料:

    矩陣知識:http://zh.wikipedia.org/zh/%E7%9F%A9%E9%98%B5

    Commons math包:http://commons.apache.org/math/index.html

    posted on 2010-12-11 21:12 changedi 閱讀(4078) 評論(3)  編輯  收藏 所屬分類: 數學

    評論

    # re: Commons Math學習筆記——矩陣 2010-12-20 15:46 杜良永

    這個有求逆矩陣的功能嗎?  回復  更多評論   

    # re: Commons Math學習筆記——矩陣 2010-12-20 16:30 changedi

    @杜良永
    有的,詳見《矩陣分解》那一節的例子,代碼中的
    getInverse方法可以求得矩陣的逆,基于LU分解做的~~  回復  更多評論   

    # re: Commons Math學習筆記——矩陣 2011-04-07 10:35 xautchap

    有的,詳見《矩陣分解》那一節的例子,代碼中的
    getInverse方法可以求得矩陣的逆,基于LU分解做的~~

    對于速度來說,LU分解的速度沒有QR分解的效率好
    @changedi
      回復  更多評論   

    主站蜘蛛池模板: 亚洲乱亚洲乱妇24p| 亚洲欧洲视频在线观看| 七次郎成人免费线路视频| 免费在线观看毛片| 一级做a爰全过程免费视频毛片| 免费国产成人高清在线观看麻豆| 色偷偷噜噜噜亚洲男人| 成人伊人亚洲人综合网站222| 免费很黄无遮挡的视频毛片| 亚洲麻豆精品国偷自产在线91| A级毛片成人网站免费看| 久久精品国产亚洲AV麻豆不卡 | 深夜福利在线视频免费| 亚洲天堂免费在线视频| 在线免费观看h片| 337p日本欧洲亚洲大胆精品555588| 久久免费看黄a级毛片 | 国产精品二区三区免费播放心| 日韩亚洲综合精品国产| 爱情岛论坛网亚洲品质自拍| 免费成人在线电影| 亚洲国产夜色在线观看| 国产成人免费a在线视频app | 亚洲色偷偷综合亚洲AVYP| 国产一级淫片a免费播放口| 亚洲精品国产免费| 国产精品va无码免费麻豆| 精品一区二区三区免费视频| 亚洲高清资源在线观看| 成人免费视频国产| 免费a级毛片无码a∨免费软件 | 香港特级三A毛片免费观看| 国产V亚洲V天堂无码久久久| 桃子视频在线观看高清免费完整| 福利片免费一区二区三区| 亚洲VA中文字幕无码毛片| 免费羞羞视频网站| 国产99视频精品免费专区| 亚洲日韩精品无码专区加勒比| 中文字幕人成人乱码亚洲电影 | 精品日韩亚洲AV无码|