C++_Eigen函数库用法笔记——Advanced Initialization

本文深入探讨了矩阵操作和初始化方法,包括基本的矩阵创建、拼接、填充表达式等,同时展示了如何使用特殊矩阵如零矩阵、单位矩阵、线性分布矩阵,并通过实例演示了如何构建矩阵和使用临时对象。
  • The comma initializer
    • a simple example 
    • join and block initialize 
      • join two row vectors together
      • initialize metrics with block structure
      • fill block expression
  • Special metrics and arrays
    • Zero();
      • Array33f::Zero();
      • ArrayXf::Zero(3);
      • ArrayXXf::Zero(3,4);
    • Constant(rows,cols,value);
    • Identity();
    • LinSpaced
    • 3 ways to construct the matrix
  • Usage as temporary objects
 

 
  1. The comma initializer
    1. a simple example 
      m << 1, 2, 3,
      4, 5, 6,
      7, 8, 9;
      std::cout << m;
    2. join and block initialize 
      • join two row vectors together
        RowVectorXd vec1(3);
        vec1 << 1, 2, 3;
        std::cout << "vec1 = " << vec1 << std::endl;
        RowVectorXd vec2(4);
        vec2 << 1, 4, 9, 16;;
        std::cout << "vec2 = " << vec2 << std::endl;
        RowVectorXd joined(7);
        joined << vec1, vec2;
        std::cout << "joined = " << joined << std::endl;
        vec1 = 1 2 3
        vec2 =  1  4  9 16
        joined =  1  2  3  1  4  9 16
      • initialize metrics with block structure
        MatrixXf matA(2, 2);
        matA << 1, 2, 3, 4;
        MatrixXf matB(4, 4);
        matB << matA, matA/10, matA/10, matA;
        std::cout << matB << std::endl;
          1   2 0.1 0.2
          3   4 0.3 0.4
        0.1 0.2   1   2
        0.3 0.4   3   4
      • fill block expression
        m.row(0) << 1, 2, 3;
        m.block(1,0,2,2) << 4, 5, 7, 8;
        m.col(2).tail(2) << 6, 9;
        std::cout << m;
        1 2 3
        4 5 6
        7 8 9
  2. Special metrics and arrays
    1. Zero()
      • Array33f::Zero();
      • ArrayXf::Zero(3);
      • ArrayXXf::Zero(3,4);
        std::cout << "A fixed-size array:\n";
        Array33f a1 = Array33f::Zero();
        std::cout << a1 << "\n\n";
        std::cout << "A one-dimensional dynamic-size array:\n";
        ArrayXf a2 = ArrayXf::Zero(3);
        std::cout << a2 << "\n\n";
        std::cout << "A two-dimensional dynamic-size array:\n";
        ArrayXXf a3 = ArrayXXf::Zero(3, 4);
        std::cout << a3 << "\n";
        A fixed-size array:
        0 0 0
        0 0 0
        0 0 0

        A one-dimensional dynamic-size array:
        0
        0
        0

        A two-dimensional dynamic-size array:
        0 0 0 0
        0 0 0 0
        0 0 0 0
    2. Constant(rows,cols,value);
    3. Identity();
    4. LinSpaced
      ArrayXXf table(10, 4);
      table.col(0) = ArrayXf::LinSpaced(10, 0, 90);
      table.col(1) = M_PI / 180 * table.col(0);
      table.col(2) = table.col(1).sin();
      table.col(3) = table.col(1).cos();
      std::cout << " Degrees Radians Sine Cosine\n";
      std::cout << table << std::endl;
      Degrees   Radians      Sine    Cosine
              0         0         0         1
             10     0.175     0.174     0.985
             20     0.349     0.342      0.94
             30     0.524       0.5     0.866
             40     0.698     0.643     0.766
             50     0.873     0.766     0.643
             60      1.05     0.866       0.5
             70      1.22      0.94     0.342
             80       1.4     0.985     0.174
             90      1.57         1 -4.37e-08
    5. 3 ways to construct the matrix
      const int size = 6;
      MatrixXd mat1(size, size);
      mat1.topLeftCorner(size/2, size/2) = MatrixXd::Zero(size/2, size/2);
      mat1.topRightCorner(size/2, size/2) = MatrixXd::Identity(size/2, size/2);
      mat1.bottomLeftCorner(size/2, size/2) = MatrixXd::Identity(size/2, size/2);
      mat1.bottomRightCorner(size/2, size/2) = MatrixXd::Zero(size/2, size/2);
      std::cout << mat1 << std::endl << std::endl;
      MatrixXd mat2(size, size);
      mat2.topLeftCorner(size/2, size/2).setZero();
      mat2.topRightCorner(size/2, size/2).setIdentity();
      mat2.bottomLeftCorner(size/2, size/2).setIdentity();
      mat2.bottomRightCorner(size/2, size/2).setZero();
      std::cout << mat2 << std::endl << std::endl;
      MatrixXd mat3(size, size);
      mat3 << MatrixXd::Zero(size/2, size/2), MatrixXd::Identity(size/2, size/2),
      MatrixXd::Identity(size/2, size/2), MatrixXd::Zero(size/2, size/2);
      std::cout << mat3 << std::endl;
      0 0 0 1 0 0
      0 0 0 0 1 0
      0 0 0 0 0 1
      1 0 0 0 0 0
      0 1 0 0 0 0
      0 0 1 0 0 0
      
      0 0 0 1 0 0
      0 0 0 0 1 0
      0 0 0 0 0 1
      1 0 0 0 0 0
      0 1 0 0 0 0
      0 0 1 0 0 0
      
      0 0 0 1 0 0
      0 0 0 0 1 0
      0 0 0 0 0 1
      1 0 0 0 0 0
      0 1 0 0 0 0
      0 0 1 0 0 0
      
       
  3. Usage as temporary objects
    MatrixXf mat = MatrixXf::Random(2, 3);
    std::cout << mat << std::endl << std::endl;
    mat = ( MatrixXf(2,2) << 0, 1, 1, 0).finished() * mat;
    std::cout << mat << std::endl;
     0.68  0.566  0.823
    -0.211  0.597 -0.605

    -0.211  0.597 -0.605
      0.68  0.566  0.823
The finished() method is necessary here to get the actual matrix object once the comma initialization of our temporary submatrix is done.

转载于:https://www.cnblogs.com/ymxiansen/p/5259547.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值