Change Dir

先知cd——热爱生活是一切艺术的开始

统计

留言簿(18)

积分与排名

“牛”们的博客

各个公司技术

我的链接

淘宝技术

阅读排行榜

评论排行榜

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 阅读(4035) 评论(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
  回复  更多评论   


只有注册用户登录后才能发表评论。


网站导航: