zoukankan      html  css  js  c++  java
  • 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: ";
          Array33f a1 = Array33f::Zero();
          std::cout << a1 << " ";
          std::cout << "A one-dimensional dynamic-size array: ";
          ArrayXf a2 = ArrayXf::Zero(3);
          std::cout << a2 << " ";
          std::cout << "A two-dimensional dynamic-size array: ";
          ArrayXXf a3 = ArrayXXf::Zero(3, 4);
          std::cout << a3 << " ";
          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 ";
        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
      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.
  • 相关阅读:
    EasyUI 清空表格
    【21年01月DW打卡】Task02
    【BUG12】排查解决一个锁超时 "MySQLTransactionRollbackException: Lock wait timeout exceeded; try restarting transaction" 的SQL问题
    【20年12月DW打卡】joyful-pandas
    【21年01月DW打卡】Task01
    【12月DW打卡】joyful-pandas
    【12月DW打卡】joyful-pandas
    【Pandas】resample重采样中的周‘W’按周日开始为一周('W'的频率偏移默认为‘right’、使用label = 'left' 重设)+ 常用freq的别名/注释
    【12月DW打卡】joyful-pandas
    【12月DW打卡】joyful-pandas
  • 原文地址:https://www.cnblogs.com/ymxiansen/p/5259547.html
Copyright © 2011-2022 走看看