简体   繁体   English

C ++中的Eigen库是否具有动态矢量或矩阵

[英]Does Eigen Library in C++ have a dynamic vector or matrix

Is there a way to set a dynamic vector or matrix in Eigen library? 有没有办法在Eigen库中设置动态矢量或矩阵? If not, is there a way to still use the Eigen library in conjunction with another class like vector ? 如果不是,是否有办法仍然将Eigen库与另一个类(例如vector结合使用?

For example let's say I have n*1 matrix called MatrixXd S(n,1); 例如,假设我有n*1矩阵MatrixXd S(n,1); Now for simplicity let n=3 and S = 4 2 6 . 现在为简单起见,让n=3S = 4 2 6 Pretend that the elements in S are future stock prices and let K = 2 which will be the strike price. 假设S中的元素是未来股票价格,并令K = 2这将是行使价)。 Don't worry you won't need to understand the terminology of an option. 不用担心,您将不需要了解选项的术语。 Now say I want to know at what positions of S will we have S - K > 0 and say I want to store these positions in a vector call b . 现在说我想知道在S哪个位置上S S - K > 0并想将这些位置存储在向量调用b

Clearly, depending on the elements of S the vector b will be of a different size. 显然,根据S的元素,向量b大小将不同。 Thus, I need to have b being of a dynamic variable. 因此,我需要让b动态变量。 The only class I am familiar with that allows this is the vector class ie, #include <vector> . 我所熟悉的唯一允许该类的是矢量类,即#include <vector>

My question is as follows: Is it okay to use the Eigen library and the #include <vector> class together? 我的问题如下:可以同时使用Eigen库和#include <vector>类吗? Note that I will be performing operations of b with the Eigen library vectors and matrices I have created. 请注意,我将使用创建的特征库向量和矩阵执行b运算。

If I am not making sense, or if my question is unclear please let me know and I will clarify as much as possible. 如果我没有任何道理,或者我的问题不清楚,请让我知道,我将尽可能多地澄清。

Yes, it does. 是的,它确实。 It's presented in the " A simple first program " of Getting started : 它在入门指南的“ 一个简单的第一个程序 ”中介绍

#include <iostream>
#include <Eigen/Dense>
using Eigen::MatrixXd;
int main()
{
  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);
  std::cout << m << std::endl;
}

You do need to pass the size to the constructor, but it's works like a vector . 您确实需要将大小传递给构造函数,但它的工作方式类似于vector You can resize it later on too. 您也可以稍后resize

MatrixXd is a convenient typedef to a Matrix template which uses Dynamic as a template value for Rows , and Cols . MatrixXdMatrix模板的方便类型定义,该模板使用Dynamic作为RowsCols的模板值。 It's basically Matrix<double, Dynamic, Dynamic> . 它基本上是Matrix<double, Dynamic, Dynamic>

So you can have not only dynamic sized vectors and matrices, but also arbitrarily large fixed-size ones. 因此,您不仅可以拥有动态大小的向量和矩阵,还可以拥有任意大的固定大小的向量和矩阵。 Eigen does pretty nifty optimizations for small matrices, so using a fixed size there might be beneficial. Eigen对小型矩阵进行了非常漂亮的优化,因此使用固定大小可能会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM