日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Eigen入门之密集矩阵 4 - 块操作

發布時間:2023/12/15 编程问答 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Eigen入门之密集矩阵 4 - 块操作 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

簡介

Eigen 中Matrix/Array提供了.block()來進行block區塊操作,這是面向系數提供的操作功能。

語法

Eigen中提供了2種語法,針對產生的結果是一致的。但存在性能上的不同,任何時候,使用fixed-size模式都會得到更好地性能優化。

分類語法說明
dynamic-sizematrix.block(i,j,p,q);執行時才知道維度大小(p,q)。
fixed-sizematrix.block<p,q>(i,j);編譯時指定維度大小(p,q)。

解釋: 區塊的維度大小尺寸:(p,q), 起始的位置:(i,j)。同樣的,位置索引從0開始。

查看一個簡單的示例:

//matrix_block1.cpp#include <Eigen/Dense> #include <iostream>using namespace Eigen; using 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 << "matrix m:" << endl << m <<endl<<endl;cout<<"-----------------"<<endl;cout << "Block in the middle -- m.block<2,2>(1,1)" << endl;cout << m.block<2,2>(1,1) << endl << endl;cout<<"-----------------"<<endl;for (int i = 1; i <= 3; ++i){cout << "Get from (0,0), Block of size " << i << "x" << i << endl;cout << m.block(0,0,i,i) << endl << endl;}cout<<"-----------------"<<endl;for (int i = 1; i <= 3; ++i){cout << "Get from (1,1), Block of size " << i << "x" << i << endl;cout << m.block(1,1,i,i) << endl << endl;} }

執行的結果:

$ g++ -I /usr/local/include/eigen3 matrix_block1.cpp -o matrix_block1 $ $ ./matrix_block1 matrix m:1 2 3 45 6 7 89 10 11 12 13 14 15 16----------------- Block in the middle -- m.block<2,2>(1,1)6 7 10 11----------------- Get from (0,0), Block of size 1x1 1Get from (0,0), Block of size 2x2 1 2 5 6Get from (0,0), Block of size 3x31 2 35 6 79 10 11----------------- Get from (1,1), Block of size 1x1 6Get from (1,1), Block of size 2x26 7 10 11Get from (1,1), Block of size 3x36 7 8 10 11 12 14 15 16

block()不僅可以用于右值,也可以用于左值。下面來個Array的簡單示例。

//matrix_block2.cpp#include <Eigen/Dense> #include <iostream>using namespace std; using namespace Eigen;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; }

此示例中,在一個4X4的常量二維數組中,修改(2,2,1,1)區塊的系數,此次修改操作對應大小指定為1X1了,取值來源區塊為2X2數組。

執行結果如下,最后劃線的部分是最后一次block區塊替換的部分:

$ g++ -I /usr/local/include/eigen3 matrix_block2.cpp -o matrix_block2 $ $ ./matrix_block2 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 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

行和列的操作

單個的行或者列操作是block的一種特殊形式,Eigen中提供了更好地API方法col(), row()。

  • matrix.row(i) : 第 i 行;
  • matrix.col(j) : 第 j 列;

示例:

//matrix_block3.cpp #include <Eigen/Dense> #include <iostream>using 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 m.col(2) += 3 * m.col(0), the matrix m is:\n";cout << m << endl; }

執行:

$ g++ -I /usr/local/include/eigen3 matrix_block3.cpp -o matrix_block3 promote:eigen david$ ./matrix_block3 Here is the matrix m: 1 2 3 4 5 6 7 8 9 2nd Row: 4 5 6 After m.col(2) += 3 * m.col(0), the matrix m is:1 2 64 5 187 8 30

邊角操作

有時候,需要對矩陣的邊角進行操作。Matrix提供了對應的方法函數,可以很變量地進行操作。

target動態固定尺寸
左上matrix.topLeftCorner(p,q);matrix.topLeftCorner<p,q> ();
左下matrix.bottomLeftCorner(p,q);matrix.bottomLeftCorner<p,q> ();
右上matrix.topRightCorner(p,q);matrix.topRightCorner<p,q> ();
右上matrix.bottomRightCorner(p,q);matrix.bottomRightCorner<p,q> ();
頂部q行matrix.topRows(q);matrix.topRows ();
底部q行matrix.bottomRows(q);matrix.bottomRows ();
左邊p列matrix.leftCols§;matrix.leftCols

();

右邊p列matrix.rightCols(q);matrix.rightCols ();

向量的block塊操作

準對向量、一維數組,Eigen還提供了一組特別的塊操作。

操作Dynamic 模式fixed_size 模式
頭部n個vector.head(n);vector.head();
尾部n個vector.tail(n);vector.tail();
自i起始的n個vector.segment(i,n);vector.segment(i);

示例

//matrix_block4.cpp#include <Eigen/Dense> #include <iostream>using namespace std;int main() {Eigen::ArrayXf v(6);v << 1, 2, 3, 4, 5, 6;Eigen::ArrayXf vv = v.head(3) ;cout << "v.head(3) =" << endl << vv << endl << endl;Eigen::ArrayXf vvv = v.tail<3>() ;cout << "v.tail<3>() = " << endl << vvv << endl << endl;v.segment(1,4) *= 2;cout << "after 'v.segment(1,4) *= 2', v =" << endl << v << endl; }

執行:

$ g++ -I /usr/local/include/eigen3 matrix_block4.cpp -o matrix_block4 promote:eigen david$ ./matrix_block4 v.head(3) = 1 2 3v.tail<3>() = 4 5 6after 'v.segment(1,4) *= 2', v =1468 106

總結

以上是生活随笔為你收集整理的Eigen入门之密集矩阵 4 - 块操作的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。