【Eigen 1】Eigen中的norm、normalize、normalized三者对比

一、norm()

1. 对于Vector,norm返回的是向量的二范数

即:
∣ ∣ x ∣ ∣ 2 = ∑ i = 1 N x i 2 ||x||_2= \sqrt{\sum_{i=1}^{N} {x}^{2}_{i} } ∣∣x2=i=1Nxi2

Vector2d vec(3.0,4.0);
cout << vec.norm() << endl;	
/输出5

2. 对于Matrix,norm返回的是矩阵的弗罗贝尼乌斯范数(Frobenius Norm)

即:
∣ ∣ A ∣ ∣ F = ∑ i = 1 m ∑ j = 1 n ∣ x i j ∣ 2 ||A||_F= \sqrt{\sum_{i=1}^{m}\sum_{j=1}^{n} |x_{ij}|^{2} } ∣∣AF=i=1mj=1nxij2

Matrix2d mat;
mat << 1,2
    3,4;
cout << mat.norm() << endl;    //输出sqrt(1*1+2*2+3*3+4*4),即sqrt(30) = 5.47723

二、normalize()

清楚了norm()的定义后,normalize()其实就是把自身的各元素除以它的范数,返回值为void。

例如:

vec.normalize();
cout << vec << endl;    //输出:      0.6
                       //            0.8
mat.normalize();        //mat各元素除以mat.norm()
cout << mat << endl;    

三、normalized()

而normalized()与normalize()类似,只不过normalize()是在自身上做修改,而normalized()返回的是一个新的Vector/Matrix,并不改变原有的矩阵。

四、测试案例

基本代码

 // testing vector
    Vector3d vec(3, 4, 5);
    cout << "norm_using is:\n" << vec.norm() << endl;
    vec.normalize();
    cout << "normalize_using is:\n" << vec << endl;
    cout << "normalized_using is:\n" << vec.normalized() << endl;

    // testing matrix
    Matrix3d mat;
    mat << 1, 2, 3, 4, 5, 6, 7, 8, 9;
    cout << "norm_using is:\n" << mat.norm() << endl;
    mat.normalize();
    cout << "normalize_using is:\n" << mat << endl;
    cout << "normalized_using is:\n" << mat.normalized() << endl;

测试结果

norm_using is:
7.07107
normalize_using is:
0.424264
0.565685
0.707107
normalized_using is:
0.424264
0.565685
0.707107
norm_using is:
16.8819
normalize_using is:
0.0592349   0.11847  0.177705
  0.23694  0.296174  0.355409
 0.414644  0.473879  0.533114
normalized_using is:
0.0592349   0.11847  0.177705
  0.23694  0.296174  0.355409
 0.414644  0.473879  0.533114
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值