我已将Eigen标头包含在 Eclipse 的 C++ 项目中。当我尝试构建时出现此错误:
Symbol 'Eigen' could not be resolved Semantic Error
Run Code Online (Sandbox Code Playgroud)
据我了解,这是一个链接器错误,但 Eigen 是一个仅包含头文件的项目,因此没有可链接的库。引发错误的代码遵循他们提供的基本教程,有问题的代码片段如下所示:
#include <Eigen/Dense>
using namespace Eigen;
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?如果有帮助的话,我会在 Linux 上运行它。谢谢。
我正在尝试创建boost::lockfree::queue以下示例: http://www.boost.org/doc/libs/1_54_0/doc/html/lockfree/examples.html
我可以成功地为 int (整数数据类型)创建它。但是,我很难为 Eigen::MatrixXf 创建队列。队列声明于SolveDGEM.h。
boost::lockfree::queue<Eigen::MatrixXf> q_cam0;\nRun Code Online (Sandbox Code Playgroud)\n\n我收到以下我无法理解的编译器错误
\n\nIn file included from /home/eeuser/ros_workspaces/HeloRosProject/src/visensor_dgem/include/SolveDGEM.h:38:0,\n from /home/eeuser/ros_workspaces/HeloRosProject/src/visensor_dgem/src/dgem.cpp:4:\n/usr/local/include/boost/lockfree/queue.hpp: In instantiation of \xe2\x80\x98boost::lockfree::queue<Eigen::Matrix<float, -0x00000000000000001, -0x00000000000000001> >\xe2\x80\x99:\n/home/eeuser/ros_workspaces/HeloRosProject/src/visensor_dgem/include/SolveDGEM.h:76:45: instantiated from here\n/usr/local/include/boost/lockfree/queue.hpp:86:1: error: invalid application of \xe2\x80\x98sizeof\xe2\x80\x99 to incomplete type \xe2\x80\x98boost::STATIC_ASSERTION_FAILURE<false>\xe2\x80\x99 \n/usr/local/include/boost/lockfree/queue.hpp: In instantiation of \xe2\x80\x98boost::lockfree::queue<Eigen::Matrix<float, -0x00000000000000001, -0x00000000000000001> >\xe2\x80\x99:\n/home/eeuser/ros_workspaces/HeloRosProject/src/visensor_dgem/include/SolveDGEM.h:76:45: instantiated from here\n/usr/local/include/boost/lockfree/queue.hpp:90:1: error: invalid application of \xe2\x80\x98sizeof\xe2\x80\x99 to incomplete type \xe2\x80\x98boost::STATIC_ASSERTION_FAILURE<false>\xe2\x80\x99 \nRun Code Online (Sandbox Code Playgroud)\n 假设我有一个Matrix <float, Dynamic, Dynamic, RowMajor> Ain Eigen. 当我编写以下代码时:
cout << "Number of Columns of A is: "<< A.cols() << endl;
cout << "Number of Rows of A is: "<< A.rows() << endl;
Run Code Online (Sandbox Code Playgroud)
我得到以下结果:
Number of Columns of A is: 129
Number of Rows of A is: 600
Run Code Online (Sandbox Code Playgroud)
根据上面的结果,我期望当我编写以下代码时,我得到了,Exception Error但是这不会发生,并且它会打印一个值!为什么??!!
cout << A(500,140);
Run Code Online (Sandbox Code Playgroud) 我通过编写以下代码测试了变量是否被复制。这段代码来自官方文档:https://eigen.tuxfamily.org/dox/classEigen_1_1Ref.html
void cov(const Ref<const MatrixXf> & x, const Ref<const MatrixXf> & y, Ref<MatrixXf> C)
{
cout << "address of x : " << &x << endl;
cout << "address of C : " << &C << endl;
}
int main(int argc, const char * argv[]) {
MatrixXf m1(3,3);
MatrixXf m2(3,3);
MatrixXf m3(3,3);
m1 << 1,2,3,4,5,6,7,8,9;
m2 << 1,2,3,4,5,6,7,8,9;
m3 << 1,2,3,4,5,6,7,8,9;
cout << "address of m1 : " << &m1 << endl;
cout << "address of m3 : " …Run Code Online (Sandbox Code Playgroud) 我有一些代码将一些值剪辑到以 0 为中心的范围之间,如下所示。
Eigen::VectorXd a;
Eigen::VecotrXd b;
a = a.cwiseMin(b).cwiseMax(-b); // no temporary created here?
Run Code Online (Sandbox Code Playgroud)
我想将逻辑分解为一个函数。一种解决方案:
Eigen::VectorXd Clip(const Eigen::VectorXd& a, const Eigen::VectorXd& b);
a = Clip(a, b);
Run Code Online (Sandbox Code Playgroud)
但我认为这是低效的,因为它创建了一个额外的临时?
另一种解决方案:
void Clip(Eigen::Ref<Eigen::VectorXd> a, const Eigen::VectorXd& b) {
a = a.cwiseMin(b).cwiseMax(-b);
}
Run Code Online (Sandbox Code Playgroud)
但这有时似乎不方便使用:
void SomeFunctionSignatureICannotChange(const Eigen::VectorXd& a, const Eigen::VectorXd& b) {
// Eigen::VectorXd a_clipped = Clip(a, b); would be cleaner.
Eigen::VectorXd a_clipped;
Clip(a_clipped, b);
}
Run Code Online (Sandbox Code Playgroud)
我能想到的最佳解决方案:
template <typename DerivedV, typename DerivedB>
auto Clip(const Eigen::ArrayBase<DerivedV>& v,
const Eigen::ArrayBase<DerivedB>& bound)
-> decltype(v.min(bound).max(-bound)) {
return …Run Code Online (Sandbox Code Playgroud) 我正在使用以下代码填充特征矩阵:
int M = 3;
int N = 4;
MatrixXd A(M, N);
double res = sin(4);
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++) {
A(i, j) = sin(i+j);
}
}
Run Code Online (Sandbox Code Playgroud)
在 Matlab 中,我只需要 1 个 for 循环就可以使用矢量化来做同样的事情:
M = 3;
N = 4;
N_Vec = 0:(N-1);
A = zeros(M,N);
for i=1:M
A(i,:) = sin((i-1)+N_Vec);
end
Run Code Online (Sandbox Code Playgroud)
是否可以在 C++/Eigen 中做类似的事情,以便我可以摆脱 for 循环之一?如果有可能以某种方式摆脱两个 for 循环,那就更好了。那可能吗?
我有一个名为neuronLayerswhich 类型的成员变量std::vector<Eigen::RowVectorXf*>和另一个名为weightstype的成员变量std::vector<Eigen::MatrixXf*>。
作为前向传播函数的一部分,我需要将这两个向量的值相乘,并保留一个指向该值的指针。我已经确保我要相乘的值的维度大小匹配,所以没有问题。
我想出了这个:
for (unsigned i = 1; i < this->topology.size(); i++) {
delete this->neuronLayers[i];
this->neuronLayers[i] = new Eigen::RowVectorXf((*this->neuronLayers[i - 1]) * (*this->weights[i - 1]));
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,内存管理是我的一个弱点,所以我可能会遗漏一些非常明显的东西。我只是意识到不小心创建new对象会导致内存泄漏。
当我问这是否安全时,我想知道这是否会导致内存泄漏,或者以某种方式导致其他一些内存错误?如果可以,我如何修改该代码片段以解决此问题?
编辑:
neuronLayers在其他地方与其他期望它是Eigen::RowVectorXf*指针向量而不是Eigen::RowVectorXf对象向量的代码一起使用。显示的片段没有显示这一点。
#include <iostream>
#include<eigen3/Eigen/src/Core/Matrix.h> // does not define Eigen::StorageOptions
// need something like this
#include<eigen3/Eigen/src/ everything_in_here >
int main()
{
Eigen::Matrix<double,2,2> mat;
std::cout << mat(0,0) <<std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在那里,我试图构建一个矩阵对象,它总是询问 6 个模板参数并带有错误消息:
模板参数数量错误(3,应该是 6)
所以我开始添加它们,第四个是Eigen::StorageOptions并且没有在Matrix.h标头中定义。另外,还有太多的标题需要搜索。那么,我可以将所有文件包含在一个文件中吗#include?
我需要计算D维函数的函数值和梯度并将这些值加在一起。D我认为简单地将梯度部分存储在第一个分量中并将函数值存储在D+1维向量的最后一个分量中将是有益于计算的。
但是,如何从该维向量中提取第一个D分量(即梯度部分) ?D+1我知道可以选择切片,但我不想在这里添加任何无意义的开销。所以,我尝试简单地reinterpret_cast<Eigen::Vector<T, D>&>在我的上做一个Eigen::Vector<T, D + 1>并且它起作用了;但这真的能保证有效(并且安全)吗?
为什么我不能抓住这个例外?
我的(客户端)代码:
Eigen::MatrixXd FFs ;
try
{
FFs.resize( NUMPATCHES, NUMPATCHES ) ;
}
catch( int e )
{
error( "Not enough memory :(" ) ;
return ;
}
Run Code Online (Sandbox Code Playgroud)
抛出异常的特征代码是向下几级.
EIGEN_STRONG_INLINE void resize(Index rows, Index cols)
{
internal::check_rows_cols_for_overflow(rows, cols);
m_storage.resize(rows*cols, rows, cols);
}
哪个电话
void resize(DenseIndex size, DenseIndex rows, DenseIndex cols)
{
if(size != m_rows*m_cols)
{
internal::conditional_aligned_delete_auto(m_data, m_rows*m_cols);
if (size)
m_data = internal::conditional_aligned_new_auto(size);
else
m_data = 0;
EIGEN_INTERNAL_DENSE_STORAGE_CTOR_PLUGIN
}
m_rows = rows;
m_cols = cols;
}
粗体线是在线前被击中的线:
throw std::bad_alloc();
Run Code Online (Sandbox Code Playgroud)
被击中,这发生在internal::conditional_aligned_delete_auto(m_data, …
我阅读了Eigen 安装的文档。根据文档,您的项目中只需要引用头文件。如下图所示,我将 Eigen 的路径包含在包含目录中,但程序无法识别 Eigen。
示例代码:
#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;
}
Run Code Online (Sandbox Code Playgroud)
如何正确地将 Eigen 包含到我的 C++ 项目中?