• Eigen库使用(C++例程)


    查看Eigen版本 

    $ head -n 20 /usr/include/eigen3/Eigen/src/Core/util/Macros.h
    1 #define EIGEN_WORLD_VERSION 3
    2 #define EIGEN_MAJOR_VERSION 2
    3 #define EIGEN_MINOR_VERSION 92

    版本就是3.2.92


    搞清旋转关系

     eigen_test.cc: 

     1 #include <cmath>
     2 #include <iostream>
     3 #include <Eigen/Eigen>
     4  
     5 // wrap the angle within [-PI, PI)
     6 double WrapToPI(double ang_in_rad) {
     7   int c = ang_in_rad / (2.0 * M_PI);
     8   ang_in_rad -= c * (2.0 * M_PI);
     9   if (ang_in_rad < -M_PI) {
    10     ang_in_rad += 2.0 * M_PI;
    11   }
    12   if (ang_in_rad >= M_PI) {
    13     ang_in_rad -= 2.0 * M_PI;
    14   }
    15   return ang_in_rad;
    16 }
    17  
    18 int main(int argc, char *argv[]) {
    19   double roll = 30;
    20   double pitch = 45;
    21   double yaw = 90;
    22   std::cout << "\n指定欧拉角(roll pitch yaw): " << roll << " " << pitch << " " << yaw << std::endl;
    23  
    24   Eigen::Vector3d p1(0, 1, 0); // 点p在o1参考系下的坐标为(0, 1, 0)
    25   std::cout << "\n点p在o1参考系下的坐标p1: " << p1.transpose() << std::endl;
    26  
    27   Eigen::AngleAxisd v_21(yaw * M_PI / 180, Eigen::Vector3d::UnitZ()); // o1参考系绕其z轴(转yaw)顺时针旋转90度得到o2参考系
    28   Eigen::Matrix3d R_21 = v_21.matrix();
    29   Eigen::Vector3d p2 = R_21 * p1; // 点p在o2参考系下的坐标为(-1, 0, 0)
    30   std::cout << "\n点p在o2参考系下的坐标p2: " << p2.transpose() << std::endl;
    31  
    32   Eigen::AngleAxisd v_32(pitch * M_PI / 180, Eigen::Vector3d::UnitY()); // o2参考系绕其y轴(pitch)顺时针旋转45度得到o3参考系
    33   Eigen::Matrix3d R_32 = v_32.matrix();
    34   Eigen::Vector3d p3 = R_32 * p2; // 点p在o3参考系下的坐标为(-0.707, 0, 0.707)
    35   std::cout << "\n点p在o3参考系下的坐标p3: " << p3.transpose() << std::endl;
    36  
    37   Eigen::AngleAxisd v_43(roll * M_PI / 180, Eigen::Vector3d::UnitX()); // o3参考系绕其x轴(roll)顺时针旋转30度得到o4参考系
    38   Eigen::Matrix3d R_43 = v_43.matrix();
    39   Eigen::Vector3d p4 = R_43 * p3; // 点p在o4参考系下的坐标为(-0.707, -0.35, 0.61)
    40   std::cout << "\n点p在o4参考系下的坐标p4: " << p4.transpose() << std::endl;
    41  
    42   Eigen::Matrix3d R_41 = R_43 * R_32 * R_21; // 先绕z轴顺时针旋转90度,再绕y轴顺时针旋转45度,最后绕x轴顺时针旋转30度
    43   // Eigen::Matrix3d R_41 = v_43 * v_32 * v_21;
    44   p4 = R_41 * p1; // 点p在o4参考系下的坐标为(-0.707, -0.353, 0.612)
    45   std::cout << "\n点p在o4参考系下的坐标p4: " << p4.transpose() << std::endl;
    46  
    47   // 旋转矩阵->欧拉角
    48   Eigen::Vector3d euler_angles = R_41.eulerAngles(0, 1, 2); // (0,1,2) 表示分别绕XYZ轴顺序(与上面旋转顺序相反),即按roll,pitch,yaw顺序,顺时针为正
    49   // Euler's angles are not unique.
    50   // eigen has two sets of euler angles: (a, b, c) or (pi+a, pi-b, pi+c)
    51   // In your XYZ convention, both (0, pi, pi) and (pi, 0, 0) represents the same rotation, and both are correct.
    52   // The Eigen::eulerAngles method consistently chooses to minimize first angles.
    53   if (std::fabs(euler_angles(1)) > M_PI / 2) {
    54     euler_angles(0) = WrapToPI(M_PI + euler_angles(0));
    55     euler_angles(1) = WrapToPI(M_PI - euler_angles(1));
    56     euler_angles(2) = WrapToPI(M_PI + euler_angles(2));
    57   }
    58   std::cout << "\n旋转矩阵->欧拉角(roll pitch yaw): " << euler_angles.transpose() * 180 / M_PI << std::endl; // 30 45 90
    59  
    60   return 0;
    61 }

    输出:


    不同旋转表示及相互转换

    eigen_test.cc:  

     1 #include <cmath>
     2 #include <iostream>
     3 #include <Eigen/Eigen>
     4  
     5 int main(int argc, char *argv[]) {
     6   // 单位四元素
     7   Eigen::Quaterniond q = Eigen::Quaterniond(1, 0, 0, 0); // (w,x,y,z)
     8   // Eigen::Quaterniond q(1, 0, 0, 0); // (w,x,y,z)
     9   // Eigen::Quaterniond q(Eigen::Vector4d(0, 0, 0, 1)); // (x,y,z,w)
    10   std::cout << "\n单位四元素:\n" << q.coeffs() << std::endl; // (x,y,z,w)
    11  
    12   // 单位旋转矩阵
    13   Eigen::Matrix3d rotation_matrix3d = Eigen::Matrix3d::Identity();
    14   std::cout << "\n单位旋转矩阵:\n" << rotation_matrix3d << std::endl;
    15  
    16   // 旋转向量(轴角)
    17   Eigen::AngleAxisd angle_axis(M_PI / 4, Eigen::Vector3d(0, 0, 1)); // 绕z轴顺时针旋转45°(yaw)
    18   std::cout << "\n旋转向量:\naxi: " << angle_axis.axis().transpose() << ", angle: " << angle_axis.angle() * 180 / M_PI << std::endl;
    19  
    20   // 欧拉角
    21   Eigen::Vector3d euler_angles(0, 0, 45); // roll pitch yaw(自定义)
    22   std::cout << "\n欧拉角:\n(roll pitch yaw) = " << euler_angles.transpose() << std::endl;
    23  
    24   // 旋转向量->旋转矩阵
    25   rotation_matrix3d = angle_axis.matrix();
    26   // rotation_matrix3d = angle_axis.toRotationMatrix();
    27   std::cout << "\n旋转向量->旋转矩阵:\n" << rotation_matrix3d << std::endl;
    28  
    29   // 旋转矩阵->旋转向量(轴角)
    30   angle_axis.fromRotationMatrix(rotation_matrix3d);
    31   // angle_axis = rotation_matrix3d;
    32   std::cout << "\n旋转矩阵->旋转向量(轴角):\naxi: " << angle_axis.axis().transpose() << ", angle: " << angle_axis.angle() * 180 / M_PI << std::endl;
    33  
    34   // 旋转向量(轴角)->四元素
    35   q = Eigen::Quaterniond(angle_axis);
    36   std::cout << "\n旋转向量(轴角)->四元素:\n(w x y z) = " << q.w() << " " << q.x() << " " << q.y() << " " << q.z() << std::endl;
    37  
    38   // 四元素->旋转向量(轴角)
    39   angle_axis = q;
    40   std::cout << "\n四元素->旋转向量(轴角):\naxi: " << angle_axis.axis().transpose() << ", angle: " << angle_axis.angle() * 180 / M_PI << std::endl;
    41  
    42   // 旋转矩阵->四元素
    43   q = Eigen::Quaterniond(rotation_matrix3d);
    44   // q = rotation_matrix3d;
    45   std::cout << "\n旋转矩阵->四元素:\n(w x y z) = " << q.w() << " " << q.x() << " " << q.y() << " " << q.z() << std::endl;
    46  
    47   // 四元素->旋转矩阵
    48   rotation_matrix3d = q.matrix();
    49   // rotation_matrix3d = q.toRotationMatrix();
    50   std::cout << "\n四元素->旋转矩阵:\n" << rotation_matrix3d << std::endl;
    51  
    52   // 旋转矩阵->欧拉角
    53   euler_angles = rotation_matrix3d.eulerAngles(0, 1, 2);
    54   std::cout << "\n旋转矩阵->欧拉角:\n(roll pitch yaw) = " << euler_angles.transpose() * 180 / M_PI << std::endl;
    55  
    56   return 0;
    57 }

    输出: 


    基础用法

    eigen_test.cc:  

     1 #include <cmath>
     2 #include <iostream>
     3 #include <Eigen/Eigen>
     4  
     5 int main(int argc, char *argv[]) {
     6   // 向量(列向量)
     7   Eigen::Vector3d v1(0, 0, 0); // 声明并定义
     8   v1.y() = 1;
     9   v1[2] = 2;
    10   std::cout << "v1: " << v1.transpose() << std::endl;
    11  
    12   Eigen::Vector3d v2;
    13   v2 << 2, 2, 2; // 先声明后定义
    14   std::cout << "v2: " << v2.transpose() << std::endl;
    15  
    16   Eigen::Vector3d t;
    17   t.setZero(); // 各分量设为0
    18   // t = Eigen::Vector3d::Zero();
    19   std::cout << "t: " << t.transpose() << std::endl;
    20   t.setOnes(); // 各分量设为1
    21   // t = Eigen::Vector3d::Ones();
    22   std::cout << "t: " << t.transpose() << std::endl;
    23  
    24   // 矩阵
    25   Eigen::Matrix<double,3,4> M;
    26   M << 1,0,0,1,
    27        0,2,0,1,
    28        0,0,1,1;
    29   M(1,1) = 1;
    30   std::cout << "M:\n" << M << std::endl;
    31  
    32   Eigen::Matrix3d R = Eigen::Matrix3d::Identity();
    33   std::cout << "R:\n" << R << std::endl;
    34  
    35   // 变换矩阵(4x4)
    36   Eigen::Matrix4d T;
    37   T << R, t, 0, 0, 0, 1;
    38   std::cout << "T:\n" << T << std::endl;
    39  
    40   // 数学运算
    41   v2 = R.inverse()*v2 - t;
    42   std::cout << "v2: " << v2.transpose() << std::endl;
    43   std::cout << "v2模长: " << v2.norm() << std::endl;
    44   std::cout << "v2单位向量: " << v2.normalized().transpose() << std::endl;
    45   std::cout << "v1点乘v2: " << v1.dot(v2) << std::endl;
    46   std::cout << "v1叉乘v2: " << v1.cross(v2).transpose() << std::endl; // 叉乘只能用于长度为3的向量
    47  
    48   // 块操作
    49   R = T.block<3, 3>(0, 0);
    50   t = T.block<3, 1>(0, 3);
    51   std::cout << "旋转R:\n" << T.topLeftCorner(3, 3) << std::endl;
    52   std::cout << "平移t: " << T.topRightCorner(3, 1).transpose() << std::endl;
    53  
    54   // 欧式变换矩阵(Isometry)
    55   Eigen::Isometry3d T1 = Eigen::Isometry3d::Identity(); // 虽然称为3d,实质上是4x4的矩阵(旋转R+平移t)
    56  
    57   // 旋转部分赋值
    58   // T1.linear() = Eigen::Matrix3d::Identity();
    59   // T1.linear() << 1, 0, 0, 0, 1, 0, 0, 0, 1;
    60   // T1.rotate(Eigen::Matrix3d::Identity());
    61   T1.rotate(Eigen::AngleAxisd(M_PI/4, Eigen::Vector3d(0,0,1)));
    62  
    63   // 平移部分赋值
    64   // T1.pretranslate(Eigen::Vector3d(1, 1, 1));
    65   T1.translation() = Eigen::Vector3d(1, 1, 1);
    66  
    67   std::cout << "T1:\n" << T1.matrix() << std::endl; // 输出4x4变换矩阵
    68   std::cout << "R1:\n" << T1.linear().matrix() << std::endl; // 输出旋转部分
    69   std::cout << "t1:\n" << T1.translation().transpose() << std::endl; // 输出平移部分
    70     
    71   Eigen::Quaterniond q(T1.linear());
    72   std::cout << "q: " << q.w() << " " << q.x() << " " << q.y() << " " << q.z() << std::endl;
    73  
    74   Eigen::Isometry3d T2(q);
    75   T2(0,3) = 1;
    76   T2(1,3) = 2;
    77   T2(2,3) = 3;
    78   std::cout << "T2:\n" << T2.matrix() << std::endl;
    79  
    80   Eigen::Vector3d v3(1,1,0);
    81   v3 = T1 * v3; // 相当于R1*v1+t1,隐含齐次坐标(1,1,0,1)
    82   std::cout << "v3: " << v3.transpose() << std::endl;
    83  
    84   // 仿射变换矩阵(Affine3d)
    85   Eigen::Translation3d t;
    86   Eigen::Quaterniond q;
    87   Eigen::Affine3d T = t * q;
    88  
    89   return 0;
    90 }

    输出:

     1 Eigen::MatrixXd B = Eigen::MatrixXd::Identity(6, 5);
     2 Eigen::VectorXd b(5);
     3 b << 1, 4, 6, -2, 0.4;
     4 Eigen::VectorXd Bb = B * b;
     5 std::cout << "The multiplication of B * b is " << std::endl << Bb << std::endl;
     6  
     7 Eigen::MatrixXd A(3, 2);
     8 A << 1, 2,
     9 2, 3,
    10 3, 4;
    11 Eigen::MatrixXd B = A.transpose();// the transpose of A is a 2x3 matrix
    12 Eigen::MatrixXd C = (B * A).inverse();// computer the inverse of BA, which is a 2x2 matrix
    13  
    14 Eigen::MatrixXd A = Eigen::MatrixXd::Random(7, 9);
    15 Eigen::MatrixXd A = Eigen::MatrixXd::Random(7, 9);
    16 std::cout << "The element at fourth row and 7the column is " << A(3, 6) << std::endl;
    17 Eigen::MatrixXd B = A.block(1, 2, 3, 3);
    18 std::cout << "Take sub-matrix whose upper left corner is A(1, 2)" << std::endl << B << std::endl;
    19 Eigen::VectorXd a = A.col(1); // take the second column of A
    20 Eigen::VectorXd b = B.row(0); // take the first row of B
    21 Eigen::VectorXd c = a.head(3);// take the first three elements of a
    22 Eigen::VectorXd d = b.tail(2);// take the last two elements of b
    23  
    24 Eigen::Quaterniond q1(2, 0, 1, -3); 
    25 q1.normalize();
    26 std::cout << "To represent rotation, we need to normalize it such that its length is " << q1.norm() << std::endl;
    27  
    28 Eigen::Vector3d v(1, 2, -1);
    29 Eigen::Quaterniond q2;
    30 q2.w() = 0;
    31 q2.vec() = v;
    32 Eigen::Quaterniond q = q1 * q2 * q1.inverse(); 
    33  
    34 Eigen::Quaterniond a = Eigen::Quterniond::Identity();

    Eigen旋转内插值

    eigen_test.cc:

     1 #include <cmath>
     2 #include <iostream>
     3 #include <Eigen/Eigen>
     4  
     5 int main() {
     6   Eigen::AngleAxisd angle_axis1(M_PI / 6, Eigen::Vector3d(0, 0, 1)); // 沿z轴(yaw)顺时针旋转30°
     7   Eigen::Quaterniond q1 = Eigen::Quaterniond(angle_axis1);
     8   Eigen::Vector3d t1(3, 3, 3);
     9   Eigen::AngleAxisd angle_axis2(M_PI / 2, Eigen::Vector3d(0, 0, 1)); // 沿z轴(yaw)顺时针旋转90°
    10   Eigen::Quaterniond q2 = Eigen::Quaterniond(angle_axis2);
    11   Eigen::Vector3d t2(9, 9, 9);
    12   double ratio = 1.0 / 3;
    13  
    14   auto q = q1.slerp(ratio, q2);
    15   q.normalize();
    16   const auto &t = (1 - ratio) * t1 + ratio * t2;
    17   Eigen::Matrix4d T = Eigen::Matrix4d::Identity();
    18   // Eigen::Matrix4d T{Eigen::Matrix4d::Identity()};
    19   T.block<3, 3>(0, 0) = q.toRotationMatrix();
    20   T.block<3, 1>(0, 3) = t;
    21  
    22   Eigen::Vector3d euler_angles = q.toRotationMatrix().eulerAngles(2, 1, 0);
    23   std::cout << "yaw pitch roll: " << euler_angles.transpose() * 180 / M_PI << std::endl;
    24   std::cout << "t: " << t.transpose() << std::endl;
    25  
    26   return 0;
    27 }

    输出:


    解线性方程组

    Eigen提供了解线性方程的计算方法,包括LU分解法,QR分解法,SVD(奇异值分解)、特征值分解等。对于一般形如Ax=b的线性系统,解方程的方式一般是将矩阵A进行分解,当然最基本的方法是高斯消元法。

    Eigen内置的解线性方程组的算法如下表所示: 

      eigen_test.cc:  

     1 #include <iostream>
     2 #include <Eigen/Dense>
     3 #include "Eigen/Core"
     4 #include "Eigen/Eigenvalues"
     5  
     6 using namespace std;
     7 using namespace Eigen;
     8  
     9 int main() {
    10     // Basic linear solving
    11     Matrix3f A;
    12     Vector3f b;
    13     A << 1,2,3,  4,5,6,  7,8,10;
    14     b << 3, 3, 4;
    15     cout << "Here is the matrix A:\n" << A << endl;
    16     cout << "Here is the vector b:\n" << b << endl;
    17     Vector3f x = A.colPivHouseholderQr().solve(b);
    18     cout << "The solution is:\n" << x << endl;
    19  
    20     Matrix2f A, b;
    21     LLT<Matrix2f> llt;
    22     A << 2, -1, -1, 3;
    23     b << 1, 2, 3, 1;
    24     cout << "Here is the matrix A:\n" << A << endl;
    25     cout << "Here is the right hand side b:\n" << b << endl;
    26     cout << "Computing LLT decomposition..." << endl;
    27     llt.compute(A);
    28     cout << "The solution is:\n" << llt.solve(b) << endl;
    29     A(1,1)++;
    30     cout << "The matrix A is now:\n" << A << endl;
    31     cout << "Computing LLT decomposition..." << endl;
    32     llt.compute(A);
    33     cout << "The solution is now:\n" << llt.solve(b) << endl;
    34  
    35     Matrix2f A, b;
    36     A << 2, -1, -1, 3;
    37     b << 1, 2, 3, 1;
    38     cout << "Here is the matrix A:\n" << A << endl;
    39     cout << "Here is the right hand side b:\n" << b << endl;
    40     Matrix2f x = A.ldlt().solve(b);
    41     cout << "The solution is:\n" << x << endl;
    42  
    43     // 计算矩阵的特征值和特征向量
    44     Matrix2f A;
    45     A << 1, 2, 2, 3;
    46     cout << "Here is the matrix A:\n" << A << endl;
    47     SelfAdjointEigenSolver<Matrix2f> eigensolver(A);
    48     if (eigensolver.info() != Success) abort();
    49     cout << "The eigenvalues of A are:\n" << eigensolver.eigenvalues() << endl;
    50     cout << "Here's a matrix whose columns are eigenvectors of A \n"
    51          << "corresponding to these eigenvalues:\n"
    52          << eigensolver.eigenvectors() << endl;
    53  
    54     // 计算矩阵的逆和行列式
    55     Matrix3f A;
    56     A << 1, 2, 1,
    57          2, 1, 0,
    58          -1, 1, 2;
    59     cout << "Here is the matrix A:\n" << A << endl;
    60     cout << "The determinant of A is " << A.determinant() << endl;
    61     cout << "The inverse of A is:\n" << A.inverse() << endl;
    62  
    63     // BDCSVD解最小二乘(推荐)
    64     MatrixXf A = MatrixXf::Random(3, 2);
    65     cout << "Here is the matrix A:\n" << A << endl;
    66     VectorXf b = VectorXf::Random(3);
    67     cout << "Here is the right hand side b:\n" << b << endl;
    68     cout << "The least-squares solution is:\n"
    69          << A.bdcSvd(ComputeThinU | ComputeThinV).solve(b) << endl;
    70  
    71     // JacobiSVD解最小二乘
    72     Eigen::Matrix3f H;
    73     Eigen::JacobiSVD<Eigen::Matrix3f> svd(H, Eigen::ComputeFullU | 
    74     Eigen::ComputeFullV);
    75     Eigen::Matrix3f U = svd.matrixU();
    76     Eigen::Matrix3f V = svd.matrixV();
    77  
    78     // AX = 0
    79     // (AX)`(AX)
    80     // X`(A`A)X
    81     // 求特征值和特征向量
    82     Eigen::SelfAdjointEigenSolver<Eigen::Matrix3d> self_adjoint_solver;
    83     self_adjoint_solver.compute(ATA);
    84     Eigen::Matrix3d eigen_values = self_adjoint_solver.eigenvalues().asDiagonal(); // The eigenvalues are sorted in increasing order.
    85     Eigen::Matrix3d eigen_vectors = self_adjoint_solver.eigenvectors();
    86     Eigen::Vector3d eigen_vector = eigen_vectors.col(0);
    87     eigen_values(0, 0) = 0;
    88     ATA = eigen_vectors * eigen_values * eigen_vectors.inverse();
    89  
    90     Eigen::EigenSolver<Eigen::Matrix4d> general_solver;
    91     general_solver.compute(ATA);
    92     cout << "eigenvalues:\n" << general_solver.eigenvalues();
    93     cout << "eigenvectors:\n" << general_solver.eigenvectors();
    94     //wxyz = general_solver.eigenvectors().col(0);
    95  
    96     return 0;
    97 }

    CMakeLists.txt

     1 cmake_minimum_required(VERSION 2.8.3)
     2 project(test)
     3  
     4 set(CMAKE_CXX_STANDARD 11)
     5 set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
     6  
     7 find_package(Eigen3)
     8 INCLUDE_DIRECTORIES(${EIGEN3_INCLUDE_DIR})
     9  
    10 add_executable(eigen_test eigen_test.cc)
    11 target_link_libraries(eigen_test ${Eigen_LIBS})

    强制类型转换

    1 Eigen::Matrix4f v1;
    2 const Eigen::Matrix4d v2 = v1.cast<double>();
    3  
    4 Eigen::Matrix4d v1;
    5 Eigen::Matrix4f v2 = v1.template cast<float>();

     Eigen::Matrix和cv::Mat相互转换

    1 Eigen::Matrix3d eigen_R;
    2 cv::Mat cv_R;
    3 cv::cv2eigen(cv_R, eigen_R);
    4 cv::eigen2cv(eigen_R, cv_R);

    Eigen的SSE兼容,内存分配,和std容器的兼容理解

    SSE支持128bit的多指令并行,但是有个要求是处理的对象必须要在内存地址以16byte整数倍的地方开始。不过这些细节Eigen在做并行化的时候会自己处理。但是,如果把一些Eigen的结构放到std的容器里面,比如vector,map。这些容器会把一个一个的Eigen结构在内存里面连续排放。

    Eigen提供了两种方法来解决:

    1、使用特别的内存分配对象。

    std::map<int, Eigen::Vector4f, std::less<int>, Eigen::aligned_allocator<std::pair<const int, Eigen::Vector4f> > >

    std::vector<Eigen::Affine3d, Eigen::aligned_allocator<Eigen::Affine3d>>

    2、在对象定义的时候,使用特殊的宏,注意必须在所有Eigen对象出现前使用这个宏。

    EIGEN_DEFINE_STL_VECTOR_SPECIALIZATION

    有这个问题的Eigen结构包括:

     1 Eigen::Vector2d
     2 Eigen::Vector4d
     3 Eigen::Vector4f
     4 Eigen::Matrix2d
     5 Eigen::Matrix2f
     6 Eigen::Matrix4d
     7 Eigen::Matrix4f
     8 Eigen::Affine3d
     9 Eigen::Affine3f
    10 Eigen::Quaterniond
    11 Eigen::Quaternionf

    另外如果上面提到的这些结构作为一个对象的成员,这个时候需要在类定义里面使用另外一个宏

    EIGEN_MAKE_ALIGNED_OPERATOR_NEW

    Eigen库中的Map类

    Map类用于通过C++中普通的连续指针或者数组 (raw C/C++ arrays)来构造Eigen里的Matrix类,这就好比Eigen里的Matrix类的数据和raw C++array 共享了一片地址,也就是引用。

    1. 比如有个API只接受普通的C++数组,但又要对普通数组进行线性代数操作,那么用它构造为Map类,直接操作Map就等于操作了原始普通数组,省时省力。

    2. 再比如有个庞大的Matrix类,在一个大循环中要不断读取Matrix中的一段连续数据,如果你每次都用block operation 去引用数据,太累(虽然block operation 也是引用类型)。于是就事先将这些数据构造成若干Map,那么以后循环中就直接操作Map就行了。

    实际上Map类并没有自己申请一片空内存,只是一个引用,所以需要构造时初始化,或者使用Map的指针。

    引申一下,Eigen里 ref 类也是引用类型,Armadillo 里 subview 都是引用类型,

    Eigen开发人说的

    The use 'sub' as a Matrix or Map. Actually Map, Ref, and Block inherit from the same base class. You can also use Block.

    所以说了这么多,就一句话 Map 就是个引用。

  • 相关阅读:
    转载 linux笔记
    ibatis3.06 + spring 3.06+velocity 整合的VSSI框架测试
    MongoDB的主键类型修改,记录下
    WPF ItemsControl 的 ItemsSource 绑定的一个bug
    Android + HTML开发手机应用 demo 代码
    数据库资深工程师 期待加盟
    Mapbar POI 转 经纬度坐标的各个版本
    MongoDB的GeoSpatial索引 之 GeoNear命令,取得距离
    建立可靠性 基于Sql server 的网站服务器群的设想
    css3替代图片的尖角圆角效果
  • 原文地址:https://www.cnblogs.com/ybqjymy/p/16309633.html
Copyright © 2020-2023  润新知