Change Dir

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

统计

留言簿(18)

积分与排名

“牛”们的博客

各个公司技术

我的链接

淘宝技术

阅读排行榜

评论排行榜

Commons Math学习笔记——向量

 

看其他篇章到目录选择。

首先从我们计算机人最熟悉的线性代数开始。

今天先写第一篇:向量——vector。另外补充一句,每一个具体类或者包的研究都是示例性质的,具体要用到什么类型的类或者接口,大家应该自己动手去翻阅api doc

Vector是一个普通的向量。在mathorg.apache.commons.math.linear中有RealVectorFieldVector这两种向量,均是接口类型。前者是实数类型的向量,后者是场向量。以实数向量为例,RealVectorAbstractRealVector, ArrayRealVector。具体的结构类图见下:


这个结构可能会有些变化,因为在doc中,可以看到ArrayRealVector是继承AbstractRealVector的,而2.0的代码中ArrayRealVector还是直接实现了RealVector,且包中也没有AbstractRealVector。可见代码的变化和doc中的描述也有不同,虽然doc中说明了AbstractRealVectorsince 2.0的,而且ArrayRealVector也是since 2.0的。呵呵,一个不一致。其实是update date的不同了。

向量这个概念是线性代数的基础。RealVector作为基本的接口,已经定义了基本所有的向量操作。比如向量的加减乘除运算、向量外积、向量内积、向量范数等等,当然vector的实现时基于数组类型的。RealVector的内部实现是double []data;

需要重点解释的是一个操作map***:就像原来的api解释的“The various mapXxx and mapXxxToSelf methods operate on vectors element-wise, i.e. they perform the same operation (adding a scalar, applying a function ...) on each element in turn. The mapXxx versions create a new vector to hold the result and do not change the instance. The mapXxxToSelf versions use the instance itself to store the results, so the instance is changed by these methods. In both cases, the result vector is returned by the methods”。

具体是什么呢?很多的map***操作和map***toself操作就是对向量的每一个元素做固定操作的意思。而map***是返回新的实例的,而map***toself则返回自己。这个从源码可以看出,比如mapAdd()的实现:


1public RealVector mapAdd(double d) {
2        double[] out = new double[data.length];
3        for (int i = 0; i < data.length; i++{
4            out[i] = data[i] + d;
5        }

6        return new ArrayRealVector(out);
7}

8


mapAddToSelf()的实现:

1public RealVector mapAddToSelf(double d) {
2        for (int i = 0; i < data.length; i++{
3            data[i] = data[i] + d;
4        }

5        return this;
6}

7

区别显然了,一个返回new ArrayRealVector,一个返回this

具体见代码:


 1/**
 2 * 
 3 */

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

13public class VectorTest {
14
15    /**
16     * @param args
17     */

18    public static void main(String[] args) {
19        // TODO Auto-generated method stub
20        vector();
21    }

22
23    private static void vector() {
24        // TODO Auto-generated method stub
25        double[] vec1 = { 1d, 2d, 3d };
26        double[] vec2 = { 4d, 5d, 6d };
27        ArrayRealVector v1 = new ArrayRealVector(vec1);
28        ArrayRealVector v2 = new ArrayRealVector(vec2);
29
30        // output directly
31        System.out.println("v1 is " + v1);
32        // dimension : size of vector
33        System.out.println("size is " + v1.getDimension());
34        // vector add
35        System.out.println("v1 + v2 = " + v1.add(v2));
36        System.out.println("v1 + v2 = " + v1.add(vec2));
37        // vector substract
38        System.out.println("v1 - v2 = " + v1.subtract(v2));
39        // vector element by element multiply
40        System.out.println("v1 * v2 = " + v1.ebeMultiply(v2));
41        // vector element by element divide
42        System.out.println("v1 / v2 = " + v1.ebeDivide(v2));
43        // get index at 1
44        System.out.println("v1[1] = " + v1.getEntry(1));
45        // vector append
46        RealVector t_vec = v1.append(v2);
47        System.out.println("v1 append v2 is " + t_vec);
48        // vector distance
49        System.out.println("distance between v1 and v2 is "
50                + v1.getDistance(v2));
51        System.out.println("L1 distance between v1 and v2 is "
52                + v1.getL1Distance(v2));
53        // vector norm
54        System.out.println("norm of v1 is " + v1.getNorm());
55        // vector dot product
56        System.out.println("dot product of v1 and v2 is " + v1.dotProduct(v2));
57        // vector outer product
58        System.out.println("outer product of v1 and v2 is "
59                + v1.outerProduct(v2));
60        // vector orthogonal projection
61        System.out.println("hogonal projection of v1 and v2 is "
62                + v1.projection(v2));
63        // vector map operations
64        System.out.println("Map the Math.abs(double) function to v1 is "
65                + v1.mapAbs());
66        v1.mapInvToSelf();
67        System.out.println("Map the 1/x function to v1 itself is " + v1);
68        // vector get sub vector
69        System.out.println("sub vector of v1 is " + v1.getSubVector(02));
70    }

71
72}

73

对应的输出:

v1 is {1; 2; 3}
size is 3
v1 + v2 = {5; 7; 9}
v1 + v2 = {5; 7; 9}
v1 - v2 = {-3; -3; -3}
v1 * v2 = {4; 10; 18}
v1 / v2 = {0.25; 0.4; 0.5}
v1[1] = 2.0
v1 append v2 is {1; 2; 3; 4; 5; 6}
distance between v1 and v2 is 5.196152422706632
L1 distance between v1 and v2 is 9.0
norm of v1 is 3.7416573867739413
dot product of v1 and v2 is 32.0
outer product of v1 and v2 is Array2DRowRealMatrix{{4.0,5.0,6.0},{8.0,10.0,12.0},{12.0,15.0,18.0}}
hogonal projection of v1 and v2 is {1.66; 2.08; 2.49}
Map the Math.abs(double) function to v1 is {1; 2; 3}
Map the 1/x function to v1 itself is {1; 0.5; 0.33}
sub vector of v1 is {1; 0.5}

向量是一个基础数学结构,以后还会大量的提到。
Commons Math库为我们提供了这样方便的向量表示,在用Java写起程序来也是得心应手。

当然所有的研究要以文档为主,参看文档写代码这是必须做到的事情。所以,不要嫌麻烦,赶紧把api doc放到桌面上,开始coding吧。

 

相关资料:

向量定义:http://zh.wikipedia.org/zh/%E7%9F%A2%E9%87%8F

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

posted on 2010-12-10 17:46 changedi 阅读(2672) 评论(3)  编辑  收藏 所属分类: 数学

评论

# re: Commons Math学习笔记——向量 [未登录] 2014-04-07 21:24 JAVA

学习了,博主很用功,我也想把MATH好好掌握,模拟和线性规划都需要  回复  更多评论   

# re: Commons Math学习笔记——向量 2014-04-08 21:25 hjswust

向量外积,怎么算出来是一个矩阵,不是一个向量呢?
(ax,by,cz)×(a2x,b2y,c2z)=
|x ,y ,z |
|a ,b ,c |
|a2,b2,c2|
=((ab2-b2c)x,(a2c-ac2)y,(ab2-a2b)z)  回复  更多评论   

# re: Commons Math学习笔记——向量 2014-04-10 14:50 changedi

@hjswust
http://en.wikipedia.org/wiki/Outer_product 这里的解释不知道是否能回答你的问题,当然我同意你向量外积的说法,commons库要是把这个方法命名为张量积tensorProduct可能更合适吧  回复  更多评论   


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


网站导航: