C++_eigen function Library Usage Note--advanced initialization

Source: Internet
Author: User

    • The comma initializer
      • a simple example  
      • Join and Block initialize
        • Join, 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 matrix3f m;m << 1, 2, 3,4, 5, 6,7, 8, 9;std::cout << m;
    2. Join and Block initialize
      • Join, Row vectors togetherrowvectorxd 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 STRUCTUREMATRIXXF 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 expressionmatrix3f m;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"; std::cout << "A one-dimensional dynamic-size ar Ray:\n "; ARRAYXF A2 = Arrayxf::zero (3) std::cout << A2 << "\ n"; std::cout << "A two-dimensional dynamic-size arr Ay:\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. linspacedARRAYXXF table (4), Table.col (0) = arrayxf::linspaced (0,), 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
      1.57 1-4.37e-08
    5. 3 ways to construct the matrixconst 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 00 0 0 0 1 00 0 0 0 0 11 0 0 0 0 00 1 0 0 0 00 0 1 0 0 00 0 0 1 0 00 0 0 0 1 00 0 0 0 0 11 0 0 0 0 00 0 1 0 0 00 0 0 1 0 00 0 0 0 1 00 0 0 0 0 11 0 0 0 0 00 1 0 0 0 00 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) &L t;< 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 this actual matrix object once the comma initialization of our temporary Submatrix is done.

C++_eigen function Library Usage Note--advanced initialization

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.