医疗行业网站建设方案,网络建设是什么意思,wordpress更新以后进不去,网站如何搭建Block块操作 一、使用块操作二、列和行三、Corner-related操作四、向量的块运算 块是矩阵或数组的矩形部分。块表达式既可以用作右值#xff0c;也可以用作左值。与通常的Eigen表达式一样#xff0c;只要让编译器进行优化#xff0c;这种抽象的运行时成本为零。优化都是自动… Block块操作 一、使用块操作二、列和行三、Corner-related操作四、向量的块运算 块是矩阵或数组的矩形部分。块表达式既可以用作右值也可以用作左值。与通常的Eigen表达式一样只要让编译器进行优化这种抽象的运行时成本为零。优化都是自动的无需我们考虑太多。
一、使用块操作
Eigen中最通用的块操作称为.block()。有两个版本语法如下:
块操作动态大小的块表达式固定大小块表达式大小为(p,q)的块从(i,j)开始matrix.block (i, j, p, q);matrix.block p, q (i, j);
在Eigen中索引从0开始。
这两个版本都可以用于固定大小和动态大小的矩阵和数组。这两个表达式在语义上是等价的。唯一的区别是如果块大小很小固定大小的版本通常会提供更快的代码但需要在编译时知道这个大小。
下面的程序使用动态大小和固定大小的版本打印矩阵中几个块的值。
#include Eigen/Dense
#include iostreamusing namespace std;int main()
{Eigen::MatrixXf m(4,4);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.block2,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 middle6 7
10 11Block of size 1x1
1Block of size 2x2
1 2
5 6Block of size 3x31 2 35 6 79 10 11在上面的例子中.block()函数被用作右值也就是说它只能作为只读。但是块也可以用作左值这意味着您可以对块进行赋值。
下面的示例说明了这一点。这个示例还演示了数组中的块其工作原理与上面演示的矩阵中的块完全相同。
#include Eigen/Dense
#include iostreamint main()
{Eigen::Array22f m;m 1,2,3,4;Eigen::Array44f a Eigen::Array44f::Constant(0.6);std::cout Here is the array a:\n a \n\n;a.block2,2(1,1) m;std::cout Here is now a with m copied into its central 2x2 block:\n a \n\n;a.block(0,0,2,3) a.block(2,1,2,3);std::cout Here is now a with bottom-right 2x3 block copied into top-left 2x3 block:\n a \n\n;
}输出
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.6Here 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.6Here is now a with bottom-right 2x3 block copied into top-left 2x3 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虽然 .block()方法可用于任何块操作但还有其他方法用于特殊情况提供更专门的API和/或更好的性能。关于性能的话题最重要的是在编译时向Eigen提供尽可能多的信息。例如如果您的块是矩阵中的单个完整列那么使用下面描述的专门的 .col() 函数可以让Eigen知道这一点这可以为其提供优化机会。
本页的其余部分将描述这些专门的方法。
二、列和行
单独的列和行是块的特殊情况。Eigen提供了方便地解决这些问题的方法:.col()和.row()。
块操作方法第 i 行*matrix.row( i );第 j 列*matrix.col ( j );
col()和row()的参数是要访问的列或行的索引。在Eigen中索引从0开始。
#include Eigen/Dense
#include iostreamusing namespace std;int main()
{Eigen::MatrixXf m(3,3);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:\n;cout m endl;
}输出
Here is the matrix m:
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 64 5 187 8 30该示例还演示了块表达式(这里是列)可以像任何其他表达式一样用于算术。
三、Corner-related操作
Eigen还为在矩阵或数组的一个角或边上刷新的块提供了特殊方法。例如. topleftcorner()可用于引用矩阵左上角的块。
不同的可能性总结如下表:
块操作动态大小的块表达式固定大小块表达式Top-left p by q block *matrix.topLeftCorner(p,q);matrix.topLeftCornerp,q();Bottom-left p by q block *matrix.bottomLeftCorner(p,q);matrix.bottomLeftCornerp,q();Top-right p by q block *matrix.topRightCorner(p,q);matrix.topRightCornerp,q();Bottom-right p by q block *matrix.bottomRightCorner(p,q);matrix.bottomRightCornerp,q();Block containing the first q rows *matrix.topRows(q);matrix.topRowsq();Block containing the last q rows *matrix.bottomRows( q );matrix.bottomRowsq();Block containing the first p columns *matrix.leftCols( p );matrix.leftColsp();Block containing the last q columns *matrix.rightCols(q);matrix.rightColsq();Block containing the q columns starting from i *matrix.middleCols(i,q);matrix.middleColsq(i);Block containing the q rows starting from i *matrix.middleRows(i,q);matrix.middleRowsq(i);
下面是一个简单的例子说明上述操作的用法:
#include Eigen/Dense
#include iostreamusing namespace std;int main()
{Eigen::Matrix4f m;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.bottomRows2() endl m.bottomRows2() endl endl;m.topLeftCorner(1,3) m.bottomRightCorner(3,1).transpose();cout After assignment, m endl m endl;输出
m.leftCols(2) 1 25 69 10
13 14m.bottomRows2() 9 10 11 12
13 14 15 16After assignment, m 8 12 16 45 6 7 89 10 11 12
13 14 15 16四、向量的块运算
Eigen还提供了一组专门为向量和一维数组的特殊情况设计的块操作:
块操作动态大小的块表达式固定大小块表达式包含前n个元素的块*vector.head (n);vector.head n ();包含最后n个元素的块*vector.tail (n);vector.tail n ();包含n个元素的块从位置i *开始vector.segment (n);vector.segment n (i);
下面是一个例子:
#include Eigen/Dense
#include iostreamusing namespace std;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.tail3() endl v.tail3() endl endl;v.segment(1,4) * 2;cout after v.segment(1,4) * 2, v endl v endl;
}输出
v.head(3)
1
2
3v.tail3()
4
5
6after v.segment(1,4) * 2, v 1468
106