1. Matrix類
在Eigen,所有的矩陣和向量都是Matrix模板類的對象,Vector只是一種特殊的矩陣(一行或者一列)。
Matrix有6個模板參數(shù),主要使用前三個參數(shù),剩下的有默認值。
Matrix<typename Scalar, int RowsAtCompileTime, int ColsAtCompileTime>
Scalar是表示元素的類型,RowsAtCompileTime為矩陣的行,ColsAtCompileTime為矩陣的列。
庫中提供了一些類型便于使用,比如:
typedef Matrix<float, 4, 4> Matrix4f;
2. Vectors向量
列向量文章來源:http://www.zghlxwxcb.cn/news/detail-519562.html
typedef Matrix<float, 3, 1> Vector3f;
行向量文章來源地址http://www.zghlxwxcb.cn/news/detail-519562.html
typedef Matrix<int, 1, 2> RowVector2i;
一、矩陣的定義
Matrix<double, 3, 3> A; // A.定義3x3double 矩陣
Matrix<double, 3, Dynamic> A; //定義3xn double 矩陣,列為動態(tài)變化
Matrix<double, Dynamic, Dynamic> A; // 定義 double 矩陣,行、列為動態(tài)變化,由需要決定
MatrixXd A; // 定義 double 矩陣,行、列為動態(tài)變化,由需要決定
Matrix<double, 3, 3, RowMajor> A; // 定義3x3 double 矩陣,按行儲存,默認按列儲存效率較高。
Matrix3f A; // 定義3x3 float 矩陣A.
Vector3f A; // 定義3x1 float 列向量A.
VectorXd A; // 定義動態(tài)double列向量A
RowVector3f A; // 定義1x3 float 行向量A.
RowVectorXd A; // 定義動態(tài)double行向量A.
二、基礎(chǔ)用法
// Eigen // Matlab // comments
A.size() // length(x) // 元素個數(shù)
C.rows() // size(C,1) // 行個數(shù)
C.cols() // size(C,2) // 列個數(shù)
A(i) // x(i+1) // 默認情況下列優(yōu)先,訪問(0,i)的元素
C(i, j) // C(i+1,j+1) //訪問(i, j)的元素。
A.resize(4, 4); // 運行時,如果之前已經(jīng)定義過形狀則會報錯。
B.resize(4, 9); // 運行時,如果之前已經(jīng)定義過形狀則會報錯。
A.resize(3, 3); // Ok; size didn't change.
B.resize(3, 9); // Ok; only dynamic cols changed.
A << 1, 2, 3, // Initialize A. The elements can also be
4, 5, 6, // matrices, which are stacked along cols
7, 8, 9; // and then the rows are stacked.
B << A, A, A; // B is three horizontally stacked A's.
A.fill(10); // Fill A with all 10's.
三、特殊矩陣定義
// Eigen
//單位矩陣定義
MatrixXd::Identity(rows,cols)
C.setIdentity(rows,cols)
//零矩陣定義
MatrixXd::Zero(rows,cols)
C.setZero(rows,cols)
//全1矩陣定義
MatrixXd::Ones(rows,cols)
C.setOnes(rows,cols)
//隨即矩陣定義
MatrixXd::Random(rows,cols)
C.setRandom(rows,cols)
//線性陣定義
VectorXd::LinSpaced(size,low,high)
v.setLinSpaced(size,low,high)
四、矩陣分塊
// 下面x為列或行向量,P為矩陣
**************************只能對向量操作************************************
x.head(n) // 列向量的前n個元素
x.head<n>() // 行向量的前n個元素
x.tail(n) // 列向量的倒數(shù)n個元素
x.tail<n>() // 行向量的倒數(shù)n個元素
x.segment(i, n) // 行向量從i開始的n個元素
x.segment<n>(i) // 列向量從i開始的n個元素
**************************只能對矩陣操作******************************************
P.block(i, j, rows, cols) // 從i行j列開始的rows行cols列塊。
P.block<rows, cols>(i, j) // 從i行j列開始的rows行cols列塊
P.row(i) // 矩陣P的第i行元素
P.col(j) // 矩陣P的第j列元素
P.leftCols<cols>() // P矩陣左邊cols列元素
P.leftCols(cols) //P矩陣左邊cols列元素
P.middleCols<cols>(j) // P矩陣第j列開始的cols列元素
P.middleCols(j, cols) // P矩陣第j列開始的cols列元素
P.rightCols<cols>() // P矩陣右邊cols列元素
P.rightCols(cols) // P矩陣右邊cols列元素
P.topRows<rows>() // P矩陣前rows行元素
P.topRows(rows) // P矩陣前rows行元素
P.middleRows<rows>(i) // P矩陣第i行開始的row行元素
P.middleRows(i, rows) // P矩陣第i行開始的row行元素
P.bottomRows<rows>() // P矩陣倒數(shù)row行
P.bottomRows(rows) // P矩陣倒數(shù)row行
P.topLeftCorner(rows, cols) // P矩陣左上角rows行,cols列元素
P.topRightCorner(rows, cols) // P矩陣右上角rows行,cols列元素
P.bottomLeftCorner(rows, cols)
P.bottomRightCorner(rows, cols)
P.topLeftCorner<rows,cols>()
P.topRightCorner<rows,cols>()
P.bottomLeftCorner<rows,cols>()
P.bottomRightCorner<rows,cols>()
五、矩陣元素交換
R.row(i) = P.col(j); //可以將P的列元素去替換R的行元素
R.col(j1).swap(P.col(j2)); //將P的列元素和R的列元素進行互換
六、矩陣轉(zhuǎn)置
R.adjoint() // R矩陣的伴隨矩陣
R.transpose() // R矩陣的轉(zhuǎn)置
R.diagonal() // R矩陣的跡,用列表示
x.asDiagonal() // 對角矩陣
R.reverse() // R矩陣逆時針旋轉(zhuǎn)180度(反轉(zhuǎn))
R.colwise().reverse(); // R矩陣的列反轉(zhuǎn)
R.rowwise().reverse(); // R矩陣的行反轉(zhuǎn)
R.transpose().colwise().reverse(); // R矩陣逆時針旋轉(zhuǎn)90度
R.transpose().rowwise().reverse(); // R矩陣順時針旋轉(zhuǎn)90度
R.conjugate() // conj(R)共軛矩陣
七、矩陣乘積
// Matrix-vector. Matrix-matrix. Matrix-scalar.
y = M*x; R = P*Q; R = P*s;
a = b*M; R = P - Q; R = s*P;
a *= M; R = P + Q; R = P/s;
R *= Q; R = s*P;
R += Q; R *= s;
R -= Q; R /= s;
八、矩陣內(nèi)部元素操作
// Vectorized operations on each element independently
// Eigen // Matlab
R = P.cwiseProduct(Q); // R = P .* Q對應(yīng)點乘
R = P.array() * s.array();// R = P .* s對應(yīng)點乘
R = P.cwiseQuotient(Q); // R = P ./ Q對應(yīng)點除
R = P.array() / Q.array();// R = P ./ Q對應(yīng)點除
R = P.array() + s.array();// R = P + s 對應(yīng)點加
R = P.array() - s.array();// R = P – s 對應(yīng)點減
R.array() += s; // R = R + s
R.array() -= s; // R = R - s
R.array() < Q.array(); // R < Q //Q矩陣元素比較,會在相應(yīng)位置置0或1
R.array() <= Q.array(); // R <= Q //Q矩陣元素比較,會在相應(yīng)位置置0或1
R.cwiseInverse(); // 1 ./ P //1點除以P
R.array().inverse(); // 1 ./ P //1點除以P
R.array().sin() // sin(P)
R.array().cos() // cos(P)
R.array().pow(s) // P .^ s
R.array().square() // P .^ 2
R.array().cube() // P .^ 3
R.cwiseSqrt() // sqrt(P)
R.array().sqrt() // sqrt(P)
R.array().exp() // exp(P)
R.array().log() // log(P)
R.cwiseMax(P) // max(R, P) //max(R, P) 對應(yīng)取大
R.array().max(P.array()) // max(R, P) //min(R, P) 對應(yīng)取小
R.cwiseMin(P) // min(R, P)
R.array().min(P.array()) // min(R, P)
R.cwiseAbs() // abs(P)
R.array().abs() // abs(P)
R.cwiseAbs2() // abs(P.^2)
R.array().abs2() // abs(P.^2)
(R.array() < s).select(P,Q); // (R < s ? P : Q)
附:
要取得[a,b)的隨機整數(shù),使用(rand() % (b-a))+ a;
要取得[a,b]的隨機整數(shù),使用(rand() % (b-a+1))+ a;
要取得(a,b]的隨機整數(shù),使用(rand() % (b-a))+ a + 1;
通用公式:a + rand() % n;其中的a是起始值,n是整數(shù)的范圍。
要取得a到b之間的隨機整數(shù),另一種表示:a + (int)b * rand() / (RAND_MAX + 1)。
要取得0~1之間的浮點數(shù),可以使用rand() / double(RAND_MAX)。
到了這里,關(guān)于Eigen 矩陣Matrix及其簡單操作的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!