?之前的文章中,簡單的介紹了一些基本的操作,回歸之前的內(nèi)容可以參考一下鏈接:
zEigen庫的基本使用說明_每日億學(xué)的博客-CSDN博客_eigen庫
?本章內(nèi)容主要就是繼續(xù)延伸Eigen庫的使用內(nèi)容也會(huì)實(shí)時(shí)進(jìn)行更新,Eigen庫在SLAM中使用廣泛,需要對(duì)這個(gè)庫有一定的熟悉。
一、賦值操作
首先最簡單的如何給申請(qǐng)好的矩陣對(duì)象進(jìn)行賦值?
1、定義對(duì)象時(shí)
//這里以動(dòng)態(tài)的矩陣模板為例子
MatrixXi a { // 創(chuàng)建 2x2 matrix
{1, 2}, // first row
{3, 4} // second row
};
Matrix<int, 1, 5> c = {1, 2, 3, 4, 5};
2、<<操作
類似輸入流的方式
Matrix3f m;
m << 1, 2, 3,
4, 5, 6,
7, 8, 9;
3、(,)賦值
注意這里和數(shù)組不一樣它使用的是[]索引
Eigen::MatrixXd m(2,2);
m(0,0) = 3;
m(1,0) = 2.5;
m(0,1) = -1;
m(1,1) = m(1,0) + m(0,1);
這里主要使用的多維2和3這兩種。
二、矩陣和向量運(yùn)算
???????Eigen通過重載常見的 C++ 算術(shù)運(yùn)算符(如 +、-、*)或通過特殊方法(如 dot()、cross() 等)提供矩陣/向量算術(shù)運(yùn)算。對(duì)于???????Matrix類(矩陣和向量),運(yùn)算符僅重載以支持線性代數(shù)運(yùn)算。例如,matrix1
?*
?matrix2
意味著矩陣矩陣乘積,并且vector
?+
?scalar(一個(gè)數(shù))
是不允許的。
1、加減
要進(jìn)行加減操作,首先矩陣類型相同,矩陣大小也得相同才能進(jìn)行操作。
- 二元運(yùn)算符 + 如
a+b
- 二元運(yùn)算符 - 如
a-b
- 一元運(yùn)算符 - 如
-a
- 復(fù)合運(yùn)算符 += 如
a+=b
- 復(fù)合運(yùn)算符 -= 如
a-=b
#include <iostream>
#include <Eigen/Dense>
int main()
{
Eigen::Matrix2d a;
a << 1, 2,
3, 4;
Eigen::MatrixXd b(2,2);
b << 2, 3,
1, 4;
std::cout << "a + b =\n" << a + b << std::endl;
std::cout << "a - b =\n" << a - b << std::endl;
std::cout << "Doing a += b;" << std::endl;
a += b;
std::cout << "Now a =\n" << a << std::endl;
Eigen::Vector3d v(1,2,3);
Eigen::Vector3d w(1,0,0);
std::cout << "-v + w - v =\n" << -v + w - v << std::endl;
}
?輸出:
a + b = 3 5 4 8 a - b = -1 -1 2 0 Doing a += b; Now a = 3 5 4 8 -v + w - v = -1 -4 -6
2、標(biāo)量的乘法和除法
- 二元運(yùn)算符 * 如
matrix*scalar
- 二元運(yùn)算符 * 如
scalar*matrix
- 二元運(yùn)算符 / 如
matrix/scalar
- 復(fù)合運(yùn)算符 *= 如
matrix*=scalar
- 復(fù)合運(yùn)算符 /= 如
matrix/=scalar
#include <iostream>
#include <Eigen/Dense>
int main()
{
Eigen::Matrix2d a;
a << 1, 2,
3, 4;
Eigen::Vector3d v(1,2,3);
std::cout << "a * 2.5 =\n" << a * 2.5 << std::endl;
std::cout << "0.1 * v =\n" << 0.1 * v << std::endl;
std::cout << "Doing v *= 2;" << std::endl;
v *= 2;
std::cout << "Now v =\n" << v << std::endl;
}
?輸出:
a * 2.5 = 2.5 5 7.5 10 0.1 * v = 0.1 0.2 0.3 Doing v *= 2; Now v = 2 4 6
?3、轉(zhuǎn)置和共軛
? ? ? ? 矩陣a的轉(zhuǎn)置、共軛和伴隨(即共軛的轉(zhuǎn)置),分別成員函數(shù)為:transpose(),conjugate()和adjoint()。對(duì)于實(shí)數(shù)矩陣共軛共軛操作實(shí)際是空操作伴隨的其實(shí)就是轉(zhuǎn)置。
MatrixXcf a = MatrixXcf::Random(2,2);
cout << "Here is the matrix a\n" << a << endl;
cout << "Here is the matrix a^T\n" << a.transpose() << endl;
cout << "Here is the conjugate of a\n" << a.conjugate() << endl;
cout << "Here is the matrix a^*\n" << a.adjoint() << endl;
//輸出
ere is the matrix a
(-0.211,0.68) (-0.605,0.823)
(0.597,0.566) (0.536,-0.33)
Here is the matrix a^T
(-0.211,0.68) (0.597,0.566)
(-0.605,0.823) (0.536,-0.33)
Here is the conjugate of a
(-0.211,-0.68) (-0.605,-0.823)
(0.597,-0.566) (0.536,0.33)
Here is the matrix a^*
(-0.211,-0.68) (0.597,-0.566)
(-0.605,-0.823) (0.536,0.33)
這個(gè)官方提升禁止使用a=a.transpose()會(huì)出現(xiàn)一個(gè)別名問題!問題描述如下:
Matrix2i a; a << 1, 2, 3, 4;
cout << "Here is the matrix a:\n" << a << endl;
a = a.transpose(); // !!! do NOT do this !!!
cout << "and the result of the aliasing effect:\n" << a << endl;
//輸出
Here is the matrix a:
1 2
3 4
and the result of the aliasing effect:
1 2
2 4
在實(shí)際的編寫代碼時(shí)不小心就會(huì)這樣編寫,Eigen給出了一個(gè)原地轉(zhuǎn)置的函數(shù):transposePlace()
?這樣我們就不需要為了轉(zhuǎn)置再賦值給原變量了。
MatrixXf a(2,3); a << 1, 2, 3, 4, 5, 6;
cout << "Here is the initial matrix a:\n" << a << endl;
a.transposeInPlace();
cout << "and after being transposed:\n" << a << endl;
//輸出
Here is the initial matrix a:
1 2 3
4 5 6
and after being transposed:
1 4
2 5
3 6
4、矩陣-矩陣和矩陣*向量乘法
矩陣和矩陣之間可以使用*符號(hào)完成,向量其實(shí)也是矩陣屬性所以也是相對(duì)于矩陣隱式處理。一般有以下兩個(gè)情況:
- 二元運(yùn)算符 * 如
a*b
- 復(fù)合運(yùn)算符 *= 如
a*=b
(這在右邊相乘:a*=b
等同于a = a*b
)
?
#include <iostream>
#include <Eigen/Dense>
int main()
{
Eigen::Matrix2d mat;
mat << 1, 2,
3, 4;
Eigen::Vector2d u(-1,1), v(2,0);
std::cout << "Here is mat*mat:\n" << mat*mat << std::endl;
std::cout << "Here is mat*u:\n" << mat*u << std::endl;
std::cout << "Here is u^T*mat:\n" << u.transpose()*mat << std::endl;
std::cout << "Here is u^T*v:\n" << u.transpose()*v << std::endl;
std::cout << "Here is u*v^T:\n" << u*v.transpose() << std::endl;
std::cout << "Let's multiply mat by itself" << std::endl;
mat = mat*mat;
std::cout << "Now mat is mat:\n" << mat << std::endl;
//輸出
Here is mat*mat:
7 10
15 22
Here is mat*u:
1
1
Here is u^T*mat:
2 2
Here is u^T*v:
-2
Here is u*v^T:
-2 -0
2 0
Let's multiply mat by itself
Now mat is mat:
7 10
15 22
注意官網(wǎng)提示我們使用m=m*m運(yùn)算,不會(huì)出現(xiàn)別名問題Eigen幫我們修改成:
tmp=m*m;
m=tmp;
5、點(diǎn)積和叉積
對(duì)于點(diǎn)積和叉積,您需要dot()和cross()方法。當(dāng)然,點(diǎn)積也可以像u.adjoint()*v一樣得到一個(gè)1x1的矩陣。
#include <iostream>
#include <Eigen/Dense>
int main()
{
Eigen::Vector3d v(1,2,3);
Eigen::Vector3d w(0,1,2);
std::cout << "Dot product: " << v.dot(w) << std::endl;
double dp = v.adjoint()*w; // automatic conversion of the inner product to a scalar
std::cout << "Dot product via a matrix product: " << dp << std::endl;
std::cout << "Cross product:\n" << v.cross(w) << std::endl;
}
//輸出
Dot product: 8
Dot product via a matrix product: 8
Cross product:
1
-2
1
?官網(wǎng)提示叉積只能試用大小為3的向量。
6、基本算術(shù)歸納運(yùn)算
Eigen還提供了求總和(sum())、求總積(prod()),最大值(maxCoeff())和最小值(minCoeff())文章來源:http://www.zghlxwxcb.cn/news/detail-421439.html
#include <iostream>
#include <Eigen/Dense>
using namespace std;
int main()
{
Eigen::Matrix2d mat;
mat << 1, 2,
3, 4;
cout << "Here is mat.sum(): " << mat.sum() << endl;
cout << "Here is mat.prod(): " << mat.prod() << endl;
cout << "Here is mat.mean(): " << mat.mean() << endl;
cout << "Here is mat.minCoeff(): " << mat.minCoeff() << endl;
cout << "Here is mat.maxCoeff(): " << mat.maxCoeff() << endl;
cout << "Here is mat.trace(): " << mat.trace() << endl;
}
//輸出
Here is mat.sum(): 10
Here is mat.prod(): 24
Here is mat.mean(): 2.5
Here is mat.minCoeff(): 1
Here is mat.maxCoeff(): 4
Here is mat.trace(): 5
我們求取最大值和最小值還可以求取它的坐標(biāo):文章來源地址http://www.zghlxwxcb.cn/news/detail-421439.html
std::ptrdiff_t i, j;
float minOfM = m.minCoeff(&i,&j);
RowVector4i v = RowVector4i::Random();
int maxOfV = v.maxCoeff(&i);
到了這里,關(guān)于Eigen庫的基本使用說明(二)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!