ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

C++——Eigen库的学习(6)

2021-01-13 16:59:32  阅读:246  来源: 互联网

标签:Eigen mat1 矩阵 C++ 学习 table MatrixXd col size


七、特殊的矩阵和向量

1.零阵与零向量

在Eigen中,定义零阵的函数是zeros(),有三种定义方式,如下示例代码:

std::cout << "固定大小的数组:\n";
Array33f a1 = Array33f::Zero();
std::cout << a1 << "\n\n";

std::cout << "一维动态大小数组:\n";
ArrayXf a2 = ArrayXf::Zero(3);
std::cout << a2 << "\n\n";

std::cout << "二维动态大小数组:\n";
ArrayXXf a3 = ArrayXXf::Zero(3, 4);
std::cout << a3 << "\n";

代码输出如下:

A fixed-size array:
0 0 0
0 0 0
0 0 0

A one-dimensional dynamic-size array:
0
0
0

A two-dimensional dynamic-size array:
0 0 0 0
0 0 0 0
0 0 0 0

2.常量矩阵

Constant([rows],[cols],value)

3.随机矩阵

Random()

4.单位矩阵

Identity():只能使用与Matrix不使用Array,因为单位阵是个线性代数概念。

5.等间距序列

LinSpaced(size, low, high):从low到high等间距的size长度的序列,适用于vector和一维数组。

ArrayXXf table(10, 4);
table.col(0) = ArrayXf::LinSpaced(10, 0, 90);
table.col(1) = M_PI / 180 * table.col(0);
table.col(2) = table.col(1).sin();
table.col(3) = table.col(1).cos();
std::cout << "  Degrees   Radians      Sine    Cosine\n";
std::cout << table << std::endl;

输出为:

Degrees   Radians      Sine    Cosine
        0         0         0         1
       10     0.175     0.174     0.985
       20     0.349     0.342      0.94
       30     0.524       0.5     0.866
       40     0.698     0.643     0.766
       50     0.873     0.766     0.643
       60      1.05     0.866       0.5
       70      1.22      0.94     0.342
       80       1.4     0.985     0.174
       90      1.57         1 -4.37e-08

6.与特殊矩阵相关的功能函数

在Eigen中,还提供了特殊矩阵的设置函数:setZero(), MatrixBase::setIdentity()和 DenseBase::setLinSpaced()。

const int size = 6;
MatrixXd mat1(size, size);
mat1.topLeftCorner(size/2, size/2)     = MatrixXd::Zero(size/2, size/2);
mat1.topRightCorner(size/2, size/2)    = MatrixXd::Identity(size/2, size/2);
mat1.bottomLeftCorner(size/2, size/2)  = MatrixXd::Identity(size/2, size/2);
mat1.bottomRightCorner(size/2, size/2) = MatrixXd::Zero(size/2, size/2);
std::cout << mat1 << std::endl << std::endl;
MatrixXd mat2(size, size);
mat2.topLeftCorner(size/2, size/2).setZero();
mat2.topRightCorner(size/2, size/2).setIdentity();
mat2.bottomLeftCorner(size/2, size/2).setIdentity();
mat2.bottomRightCorner(size/2, size/2).setZero();
std::cout << mat2 << std::endl << std::endl;
MatrixXd mat3(size, size);
mat3 << MatrixXd::Zero(size/2, size/2), MatrixXd::Identity(size/2, size/2),
        MatrixXd::Identity(size/2, size/2), MatrixXd::Zero(size/2, size/2);
std::cout << mat3 << std::endl;

上述代码的输出都是同一个结果:

0 0 0 1 0 0
0 0 0 0 1 0
0 0 0 0 0 1
1 0 0 0 0 0
0 1 0 0 0 0
0 0 1 0 0 0

7.表达式变量

上面的静态方法如 Zero()、Constant()并不是直接返回一个矩阵或数组,实际上它们返回的是是‘expression object’,只是临时被使用/被用于优化。

MatrixXf::Constant(3,3,1.2)构建的是一个3*3的矩阵表达式(临时变量),为了获取真正的矩阵需要调用finished()函数:

MatrixXf mat = MatrixXf::Random(2, 3);
std::cout << mat << std::endl << std::endl;
mat = (MatrixXf(2,2) << 0, 1, 1, 0).finished() * mat;
std::cout << mat << std::endl;

输出结果为:

  0.68  0.566  0.823
-0.211  0.597 -0.605

-0.211  0.597 -0.605
  0.68  0.566  0.823

标签:Eigen,mat1,矩阵,C++,学习,table,MatrixXd,col,size
来源: https://blog.csdn.net/qq_33160678/article/details/112573537

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有