int main()
{
Eigen::Vector2f v;
v << 1, 2;
v << 3, 4;
std::cout << v << std::endl;
return 0;
}
3
4
int main()
{
Eigen::MatrixXf m(3,2);
m << 1, 2,
3, 4,
5, 6;
std::cout << m << std::endl;
return 0;
}
1 2
3 4
5 6
int main()
{
Eigen::Vector2f v{ 1,2 };
Eigen::MatrixXf m(2,2);
m << v, v;
std::cout << m << std::endl;
return 0;
}
1 1
2 2
矩阵组成矩阵,按先排满行,再排满列
int main()
{
Eigen::MatrixXf m1(2,2);
m1.fill(1);
auto m2 = m1 * 2;
auto m3 = m1 * 3;
auto m4 = m1 * 4;
Eigen::MatrixXf res(4, 4);
res << m1, m2, m3, m4;
std::cout << res << std::endl;
return 0;
}
1 1 2 2
1 1 2 2
3 3 4 4
3 3 4 4
int main()
{
Eigen::MatrixXf m1(4,3);
m1.fill(1);
auto m2 = m1.col(1)*2;
Eigen::MatrixXf res(4, 4);
res << m1, m2;
std::cout << res << std::endl;
return 0;
}
1 1 1 2
1 1 1 2
1 1 1 2
1 1 1 2
Eigen::VectorXf::LinSpaced可以同时赋值行或列,但是直接赋值给矩阵后,就是变成1列了。需要使用reshaped()
int main()
{
Eigen::MatrixXf res(4, 4);
res.fill(1);
res.row(0) = Eigen::VectorXf::LinSpaced(4, 2, 5); //正确赋值
res.col(0) = Eigen::VectorXf::LinSpaced(4, 2, 5); //正确赋值
//res = Eigen::VectorXf::LinSpaced(16, 2, 17); //这个会导致res变成1列的
res.reshaped() = Eigen::VectorXf::LinSpaced(16, 2, 17); //正确赋值
std::cout << res << std::endl;
return 0;
}
2 6 10 14
3 7 11 15
4 8 12 16
5 9 13 17
resize会在保持内存顺序不变的情况下,变换行列
int main()
{
Eigen::MatrixXf res(4, 4);
res.reshaped() = Eigen::VectorXf::LinSpaced(16, 1, 16);
res.resize(8, 2);
std::cout << res << std::endl;
return 0;
}
1 9
2 10
3 11
4 12
5 13
6 14
7 15
8 16
两个动态类型可以直接赋值
int main()
{
MatrixXf a(2,2);
std::cout << "a is of size " << a.rows() << "x" << a.cols() << std::endl;
MatrixXf b(3,3);
a = b;
std::cout << "a is now of size " << a.rows() << "x" << a.cols() << std::endl;
return 0;
}
a is of size 2x2
a is now of size 3x3
int main()
{
Eigen::Vector2f v1;
v1 << 1, 2;
auto v2 = v1;
v2.array() += 2;
std::cout << v2 << std::endl;
return 0;
}
3
4
A.colwise().reverseInPlace();
类型转换
Type conversion
// Eigen // Matlab
A.cast<double>(); // double(A)
A.cast<float>(); // single(A)
A.cast<int>(); // int32(A)
A.real(); // real(A)
A.imag(); // imag(A)
// if the original type equals destination type, no work is done
std::string sep = "\n----------------------------------------\n";
Matrix3d m1;
m1 << 1.111111, 2, 3.33333, 4, 5, 6, 7, 8.888888, 9;
IOFormat CommaInitFmt(StreamPrecision, DontAlignCols, ", ", ", ", "", "", " << ", ";");
IOFormat CleanFmt(4, 0, ", ", "\n", "[", "]");
IOFormat OctaveFmt(StreamPrecision, 0, ", ", ";\n", "", "", "[", "]");
IOFormat HeavyFmt(FullPrecision, 0, ", ", ";\n", "[", "]", "[", "]");
std::cout << m1 << sep;
std::cout << m1.format(CommaInitFmt) << sep;
std::cout << m1.format(CleanFmt) << sep;
std::cout << m1.format(OctaveFmt) << sep;
std::cout << m1.format(HeavyFmt) << sep;
int main()
{
Eigen::Matrix2f m;
m << 1, 2,
3, 4;
Eigen::Vector2f v;
v << 2, 3;
m.array().colwise() *= v.array();
std::cout << m << std::endl;
return 0;
}
2 4
9 12
using namespace Eigen;
Vector2f v;
v << 1.0, 2.0;
auto v1 = Eigen::Rotation2Df(M_PI/2) * v;
std::cout << v1 << std::endl;
using namespace Eigen;
Vector2f v;
v << 1.0, 2.0;
auto v1 = Eigen::Scaling(2.0) * v;
std::cout << v1 << std::endl;
using namespace Eigen;
Vector2f v;
v << 1.0, 2.0;
auto v1 = Eigen::Translation2f(1.0,3.0) * v;
std::cout << v1 << std::endl;
Eigen::Vector3d v1{ 0,0,1 };
Eigen::Vector3d v2{ 0,1,0 };
Eigen::Matrix3d R;
//R = Eigen::Quaterniond::FromTwoVectors(v1, v2).toRotationMatrix();
//std::cout << R << std::endl;
std::cout << Eigen::Quaterniond::FromTwoVectors(v1, v2) * v1 << std::endl;
逆时针旋转
Vector2f v(1,0);
auto v1 = Rotation2Df(M_PI / 2) * v;
std::cout << v1 << std::endl;