zoukankan      html  css  js  c++  java
  • C++_Eigen函数库用法笔记——Block Operations

    • Using block operations
      • rvalue, i.e. it was only read from
      • lvalues, i.e. you can assign to a block
    • Columns and rows
    • Corner-related operations
    • Block operations for vectors

    • Using block operations
    Block operationVersion constructing a 
    dynamic-size block expression 
    Version constructing a 
    fixed-size block expression 
    Block of size (p,q), starting at (i,j)
    matrix.block(i,j,p,q);
    matrix.block<p,q>(i,j);
     
      • rvalue, i.e. it was only read from
    int main()
    {
    m << 1, 2, 3, 4,
    5, 6, 7, 8,
    9,10,11,12,
    13,14,15,16;
    cout << "Block in the middle" << endl;
    cout << m.block<2,2>(1,1) << endl << endl;
    for (int i = 1; i <= 3; ++i)
    {
    cout << "Block of size " << i << "x" << i << endl;
    cout << m.block(0,0,i,i) << endl << endl;
    }
    }
     
     
    Block in the middle
     6  7
    10 11

    Block of size 1x1
    1

    Block of size 2x2
    1 2
    5 6

    Block of size 3x3
     1  2  3
     5  6  7
     9 10 11
      • lvalues, i.e. you can assign to a block
    int main()
    {
    Array22f m;
    m << 1,2,3,4;
    Array44f a = Array44f::Constant(0.6);
    cout << "Here is the array a:" << endl << a << endl << endl;
    a.block<2,2>(1,1) = m;
    cout << "Here is now a with m copied into its central 2x2 block:" << endl << a << endl << endl;
    a.block(0,0,2,3) = a.block(2,1,2,3);
    cout << "Here is now a with bottom-right 2x3 block copied into top-left 2x2 block:" << endl << a << endl << endl;
         }
    Here is the array a:
    0.6 0.6 0.6 0.6
    0.6 0.6 0.6 0.6
    0.6 0.6 0.6 0.6
    0.6 0.6 0.6 0.6

    Here is now a with m copied into its central 2x2 block:
    0.6 0.6 0.6 0.6
    0.6   1   2 0.6
    0.6   3   4 0.6
    0.6 0.6 0.6 0.6

    Here is now a with bottom-right 2x3 block copied into top-left 2x2 block:
      3   4 0.6 0.6
    0.6 0.6 0.6 0.6
    0.6   3   4 0.6
    0.6 0.6 0.6 0.6
     
    • Columns and rows
    Block operation Method 
    ith row 
    matrix.row(i);
    jth column 
    matrix.col(j);
    int main()
    {
    m << 1,2,3,
    4,5,6,
    7,8,9;
    cout << "Here is the matrix m:" << endl << m << endl;
    cout << "2nd Row: " << m.row(1) << endl;
    m.col(2) += 3 * m.col(0);
    cout << "After adding 3 times the first column into the third column, the matrix m is: ";
    cout << m << endl;
    }
    1 2 3
    4 5 6
    7 8 9
    2nd Row: 4 5 6
    After adding 3 times the first column into the third column, the matrix m is:
     1  2  6
     4  5 18
     7  8 30
     
    • Corner-related operations
    Block operationVersion constructing a 
    dynamic-size block expression 
    Version constructing a 
    fixed-size block expression 
    Top-left p by q block 
    matrix.topLeftCorner(p,q);
    matrix.topLeftCorner<p,q>();
    Bottom-left p by q block 
    matrix.bottomLeftCorner(p,q);
    matrix.bottomLeftCorner<p,q>();
    Top-right p by q block 
    matrix.topRightCorner(p,q);
    matrix.topRightCorner<p,q>();
    Bottom-right p by q block 
    matrix.bottomRightCorner(p,q);
    matrix.bottomRightCorner<p,q>();
    Block containing the first q rows 
    matrix.topRows(q);
    matrix.topRows<q>();
    Block containing the last q rows 
    matrix.bottomRows(q);
    matrix.bottomRows<q>();
    Block containing the first p columns 
    matrix.leftCols(p);
    matrix.leftCols<p>();
    Block containing the last q columns 
    matrix.rightCols(q);
    matrix.rightCols<q>();
    int main()
    {
    m << 1, 2, 3, 4,
    5, 6, 7, 8,
    9, 10,11,12,
    13,14,15,16;
    cout << "m.leftCols(2) =" << endl << m.leftCols(2) << endl << endl;
    cout << "m.bottomRows<2>() =" << endl << m.bottomRows<2>() << endl << endl;
    m.topLeftCorner(1,3) = m.bottomRightCorner(3,1).transpose();
    cout << "After assignment, m = " << endl << m << endl;
    }
    m.leftCols(2) =
     1  2
     5  6
     9 10
    13 14

    m.bottomRows<2>() =
     9 10 11 12
    13 14 15 16

    After assignment, m = 
     8 12 16  4
     5  6  7  820:37:13
     9 10 11 12
    13 14 15 16
     
    • Block operations for vectors
    Block operation Version constructing a 
    dynamic-size block expression 
    Version constructing a 
    fixed-size block expression 
    Block containing the first n elements 
    vector.head(n);
    vector.head<n>();
    Block containing the last n elements 
    vector.tail(n);
    vector.tail<n>();
    Block containing n elements, starting at position i 
    vector.segment(i,n);
    vector.segment<n>(i);
    int main()
    {
    Eigen::ArrayXf v(6);
    v << 1, 2, 3, 4, 5, 6;
    cout << "v.head(3) =" << endl << v.head(3) << endl << endl;
    cout << "v.tail<3>() = " << endl << v.tail<3>() << endl << endl;
    v.segment(1,4) *= 2;
    cout << "after 'v.segment(1,4) *= 2', v =" << endl << v << endl;
         }
    v.head(3) =
    1
    2
    3

    v.tail<3>() = 
    4
    5
    6

    after 'v.segment(1,4) *= 2', v =
     1
     4
     6
     8
    10
     6
     
     
     
     
     
     
     
     
     
     
  • 相关阅读:
    格式化数字,将字符串格式的数字,如:1000000 改为 1 000 000 这种展示方式
    jquery图片裁剪插件
    前端开发采坑之安卓和ios的兼容问题
    页面消息提示,上下滚动
    可以使用css的方式让input不能输入文字吗?
    智慧农村“三网合一”云平台测绘 大数据 农业 信息平台 应急
    三维虚拟城市平台测绘 大数据 规划 三维 信息平台 智慧城市
    农业大数据“一张图”平台测绘 大数据 房产 国土 农业 信息平台
    应急管理管理局安全生产预警平台应急管理系统不动产登记 测绘 大数据 规划 科教 三维 信息平台
    地下综合管廊管理平台测绘 大数据 地下管线 三维 信息平台
  • 原文地址:https://www.cnblogs.com/ymxiansen/p/5259570.html
Copyright © 2011-2022 走看看