gpt4 book ai didi

c++ - 使用 CWiseNullaryOp 扩展 Eigen : Cannot reproduce example

转载 作者:行者123 更新时间:2023-11-30 05:13:49 25 4
gpt4 key购买 nike

我正在尝试在 this page 上重现第二个示例Eigen 文档,但无法编译我的最小程序。我正在使用 Eigen 3.3~beta1-2。

使用以下参数运行 g++ 时 -std=c++11 -Wall -pipe -O3 -ffast-math -funsafe-math-optimizations -march=native -mtune=native -pedantic -Wno-unused -variable -I/usr/local/include/eigen3 -I/usr/include/eigen3 -c example.cpp,我得到这个错误:

example.cpp:8:41 ("Eigen::MatrixXi B = indexing(A, ri, ci)" in program below)  
/usr/include/eigen3/Eigen/src/Core/CoreEvaluators.h:348:27: error: no match for call to '(const indexing_functor<Eigen::Matrix<int, -1, -1>, Eigen::Array<int, 3, 1>, Eigen::Array<int, -1, 1> >) (Eigen::Index&)
return m_functor(index);

我的最小程序包含以下内容(基本上是从文档中的示例复制粘贴而来):

example.h

#include <iostream>
#include <stdio.h>
#include <Eigen/Core>

using namespace Eigen;

template<class ArgType, class RowIndexType, class ColIndexType>
class indexing_functor {
const ArgType &m_arg;
const RowIndexType &m_rowIndices;
const ColIndexType &m_colIndices;
public:
typedef Matrix<typename ArgType::Scalar,
RowIndexType::SizeAtCompileTime,
ColIndexType::SizeAtCompileTime,
ArgType::Flags&RowMajorBit?RowMajor:ColMajor,
RowIndexType::MaxSizeAtCompileTime,
ColIndexType::MaxSizeAtCompileTime> MatrixType;
indexing_functor(const ArgType& arg, const RowIndexType& row_indices, const ColIndexType& col_indices)
: m_arg(arg), m_rowIndices(row_indices), m_colIndices(col_indices)
{}
const typename ArgType::Scalar& operator() (Index row, Index col) const {
return m_arg(m_rowIndices[row], m_colIndices[col]);
}
};

template <class ArgType, class RowIndexType, class ColIndexType>
CwiseNullaryOp<indexing_functor<ArgType,RowIndexType,ColIndexType>, typename indexing_functor<ArgType,RowIndexType,ColIndexType>::MatrixType>
indexing(const Eigen::MatrixBase<ArgType>& arg, const RowIndexType& row_indices, const ColIndexType& col_indices)
{
typedef indexing_functor<ArgType,RowIndexType,ColIndexType> Func;
typedef typename Func::MatrixType MatrixType;
return MatrixType::NullaryExpr(row_indices.size(), col_indices.size(), Func(arg.derived(), row_indices, col_indices));
}

example.cpp

#include "example.h"

int main()
{
Eigen::MatrixXi A = Eigen::MatrixXi::Random(4,4);
Array3i ri(1,2,1);
ArrayXi ci(6); ci << 3,2,1,0,0,2;
Eigen::MatrixXi B = indexing(A, ri, ci);
std::cout << "A =" << std::endl;
std::cout << A << std::endl << std::endl;
std::cout << "A([" << ri.transpose() << "], [" << ci.transpose() << "]) =" << std::endl;
std::cout << B << std::endl;
}

我是否遗漏了有关模板在 C++ 中的工作原理的一些基本知识?

最佳答案

我确定 Ubuntu release对于 Eigen 3.3~beta1-2 必须有重大更改,或者以某种方式缺乏对此扩展的支持。该程序在使用最新 header 时有效。

关于c++ - 使用 CWiseNullaryOp 扩展 Eigen : Cannot reproduce example,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43749673/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com