【C++】Eigen中norm、normalize、normalized的区别

参考:

  1. https://blog.csdn.net/m0_56348460/article/details/117386857
  2. https://blog.csdn.net/huangjunsheng123/article/details/119274776?spm=1001.2101.3001.6650.7&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7Edefault-7.no_search_link&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7Edefault-7.no_search_link

一、norm()

1. 对于Vectornorm返回的是向量的二范数

即:
在这里插入图片描述
例如:

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

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

即:
在这里插入图片描述
例如:

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,并不改变原有的矩阵

四、norm、normalize、normalized的区别

# include  
# include 

using namespace std;
using namespace Eigen;

int main(int argc, char *argv[])
{
    Eigen::Vector3d vec(3,4,5);
    cout << "data.norm() = " << vec.norm() << endl;
    vec.normalize();
    cout << "vec.normalize() = " << vec << endl;
    cout << "vec.normalized() = " << vec.normalized() << endl;

    Eigen::Matrix3d mat;
    mat << 1,2,3,
           4,5,6,
           7,8,9;
    cout << "mat.norm() = " << mat.norm() << endl;
    mat.normalize();
    cout << "mat.normalize() = " << mat << endl;
    cout << "mat.normalized()" << mat.normalized() << endl;

    return 0;
}

【C++】Eigen中norm、normalize、normalized的区别_第1张图片

你可能感兴趣的:(线性代数,c++,矩阵)