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.
  • 相关阅读:
    IsPostBack
    多次点击Button后DropDownList选中的值变为默认值?
    数据表数据的复制
    使用DriverManager获取数据库连接
    通过Driver获取数据库连接
    URL学习笔记
    使用UDP进行数据发送的实例一
    利用Socket 客户端---->服务端 传送文件到指定路径,并返回一个友好的回馈
    关于TCP的两个小练习_第一个博客~
    oracle-19c-单实例安装-一键式脚本
  • 原文地址:https://www.cnblogs.com/ymxiansen/p/5259547.html
Copyright © 2011-2022 走看看