Eigen笔记——基本数据类型及初始化
Eigen常见的类型及初始化方法
测试例程如下:
// 2x1 列向量 Eigen::Vector2f vector_2f(1.0f, 2.0f); std::cout<<"vector_2f:\n"<<vector_2f<<std::endl<<std::endl; // 3x1 列向量 Eigen::Vector3f vector_3f(1.0f, 2.0f, 3.0f); std::cout<<"vector_3f:\n"<<vector_3f<<std::endl<<std::endl; // 4x1 列向量 Eigen::Vector4f vector_4f = Eigen::Vector4f::Random(); std::cout<<"vector_4f:\n"<<vector_4f<<std::endl<<std::endl; // 3x3 矩阵 Eigen::Matrix3f matrix_3f; // 初始化为单位阵 matrix_3f = Eigen::Matrix3f::Identity(); std::cout<<"matrix_3f Identity :\n"<<matrix_3f<<std::endl; // 数值填充初始化 matrix_3f << 1,2,3, 4,5,6, 7,8,9; std::cout<<"matrix_3f:\n"<<matrix_3f<<std::endl<<std::endl; //一矩阵初始化Ones Eigen::Matrix3d matrix_3d; matrix_3d.setOnes(); matrix_3d = Eigen::Matrix3d::Ones(); std::cout<<"matrix_3d Ones:\n"<<matrix_3d<<std::endl<<std::endl; //4x4 矩阵 Eigen::Matrix4f matrix_4f; //单位矩阵初始化Identity matrix_4f.setIdentity(); matrix_4f = Eigen::Matrix4f::Identity(); std::cout<<"matrix_4f identity :\n"<<matrix_4f<<std::endl<<std::endl; //零矩阵初始化Zero Eigen::Matrix4d matrix_4d = Eigen::Matrix4d::Zero(); matrix_4d.setZero(); std::cout<<"matrix_4d Zero:\n"<<matrix_4d<<std::endl<<std::endl;
//随机初始化Random Eigen::Matrix<float, 5, 2> matrix_5f; // 使用 [-1,1] 范围之间的随机数进行初始化填充; matrix_5f = Eigen::Matrix<float,5,2>::Random(); std::cout<<"matrix_5f random:\n"<<matrix_5f<<std::endl<<std::endl; //动态矩阵:常数初始化Constant Eigen::MatrixXf matrix_xf; const int iColNum = 3; const int iRowNum = 2; matrix_xf.resize(iRowNum,iColNum); //Constant和setConstant等价 matrix_xf = Eigen::MatrixXf::Constant(iRowNum,iColNum,255); matrix_xf.setConstant(iRowNum,iColNum,10); std::cout<<"matrix_xf size: "<<matrix_xf.rows() \ <<" rows, "<<matrix_xf.cols()<<" cols."<<std::endl; std::cout<<"matrix_xf constant init:\n"<<matrix_xf<<std::endl<<std::endl;
作者:水水滴答
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。