首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >避免对角矩阵在特征中的分配

避免对角矩阵在特征中的分配
EN

Stack Overflow用户
提问于 2022-04-12 18:40:31
回答 1查看 33关注 0票数 1

问题

如何避免特征小对角矩阵的动态分配?

上下文

我用的是艾根3.4。我有N乘N对角矩阵W:

代码语言:javascript
复制
auto W = Eigen::DiagonalMatrix<double, Dynamic>(N);

如果N <= 512使用堆栈上的缓冲区,我想避免分配:

代码语言:javascript
复制
double W_buffer[512];

对于法线向量和矩阵,我知道我可以使用Map

代码语言:javascript
复制
double y_buff[512];
auto y = Eigen::Map<VectorXd>( y_buff, N );

然而,当我尝试使用对角矩阵时,它会给我一个错误,因为InnerStrideAtCompileTime不是Eigen::DiagonalMatrix的成员。

与DiagonalMatrix一起使用Map时的错误消息

代码语言:javascript
复制
In file included from eigen/Eigen/Core:311,
                 from eigen/Eigen/Dense:1,
                 from build/release/CMakeFiles/bench.dir/cmake_pch.hxx:5,
                 from <command-line>:
eigen/Eigen/src/Core/Map.h: In instantiation of ‘struct Eigen::internal::traits<Eigen::Map<Eigen::DiagonalMatrix<double, -1> > >’:
eigen/Eigen/src/Core/util/ForwardDeclarations.h:34:48:   required from ‘struct Eigen::internal::accessors_level<Eigen::Map<Eigen::DiagonalMatrix<double, -1> > >’
eigen/Eigen/src/Core/util/ForwardDeclarations.h:101:75:   required from ‘class Eigen::Map<Eigen::DiagonalMatrix<double, -1> >’
include/volar/estimators.hpp:203:18:   required from ‘static volar::R volar::PolyLE<Degree>::estimate(volar::R, volar::ViewR, volar::ViewR, const Kernel&) [with Kernel = volar::UniformK; int Degree = 1; volar::R = double; volar::ViewR = volar::View<double>]’
include/volar/kernel_smoothing.hpp:81:64:   required from ‘volar::R volar::LocalRFT<Estimator, Kernel>::operator()(volar::R) const [with Estimator = volar::EigenLinearLE; Kernel = volar::UniformK; volar::R = double]’
bench/core.cpp:43:23:   required from ‘void localRF(benchmark::State&) [with Method = volar::EigenLinearLE; Kernel = volar::UniformK]’
bench/core.cpp:96:1:   required from here
eigen/Eigen/src/Core/Map.h:30:53: error: ‘InnerStrideAtCompileTime’ is not a member of ‘Eigen::DiagonalMatrix<double, -1>’
   30 |                              ? int(PlainObjectType::InnerStrideAtCompileTime)
      |                                                     ^~~~~~~~~~~~~~~~~~~~~~~~
In file included from eigen/Eigen/Core:163,
                 from eigen/Eigen/Dense:1,
                 from build/release/CMakeFiles/bench.dir/cmake_pch.hxx:5,
                 from <command-line>:
EN

回答 1

Stack Overflow用户

发布于 2022-04-12 18:57:15

Eigen::DiagonalMatrix的第三个模板参数MaxSizeAtCompileTime允许您这样做。

当与Eigen::Dynamic相结合时,DiagonalMatrix将有一个足够大的内部缓冲区供MaxSizeAtCompileTime使用,但它仍然是动态大小的。

例如,以下内容相当于您试图使用外部缓冲区时所做的工作:

代码语言:javascript
复制
auto W = Eigen::DiagonalMatrix<double, Eigen::Dynamic, 512>(N)

显然,尝试使用大于MaxSizeAtCompileTime的大小初始化它将在运行时失败(使用断言),但这并不比使用Map时必须处理的问题更糟糕。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71847633

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档