在 C++ 中枚举 k 维超立方体顶点的最有效方法是什么?

发布于 2024-10-06 20:08:11 字数 1326 浏览 4 评论 0原文

基本问题:我有 ak 维度盒子。我有一个上限和下限的向量。枚举顶点坐标的最有效方法是什么?

背景:举个例子,假设我有一个 3 维盒子。获得最有效的算法/代码是什么:

vertex[0] = ( 0, 0, 0 ) -> ( L_0, L_1, L_2 )
vertex[1] = ( 0, 0, 1 ) -> ( L_0, L_1, U_2 )
vertex[2] = ( 0, 1, 0 ) -> ( L_0, U_1, L_2 )
vertex[3] = ( 0, 1, 1 ) -> ( L_0, U_1, U_2 )

vertex[4] = ( 1, 0, 0 ) -> ( U_0, L_1, L_2 )
vertex[5] = ( 1, 0, 1 ) -> ( U_0, L_1, U_2 )
vertex[6] = ( 1, 1, 0 ) -> ( U_0, U_1, L_2 )
vertex[7] = ( 1, 1, 1 ) -> ( U_0, U_1, U_2 )

其中L_0对应于下界向量的第0个元素&同样,U_2 是上限向量的第二个元素。

我的代码:

const unsigned int nVertices = ((unsigned int)(floor(std::pow( 2.0, double(nDimensions)))));

for ( unsigned int idx=0; idx < nVertices; ++idx )
{
   for ( unsigned int k=0; k < nDimensions; ++k )
   {
      if ( 0x00000001 & (idx >> k) )
      {
         bound[idx][k] = upperBound[k];
      }
      else
      {
         bound[idx][k] = lowerBound[k];
      }
   }
}

其中变量bound声明为:

std::vector< std::vector<double> > bound(nVertices);

但我已经预先调整了它的大小,以免在分配内存的循环中浪费时间。每次运行算法时,我都需要调用上述过程大约 50,000,000 次——所以我需要它非常高效。

可能的子问题:移动 k 是否比总是移动 1 并存储中间结果更快? (我应该使用>>=??)

Basic Question: I have a k dimensional box. I have a vector of upper bounds and lower bounds. What is the most efficient way to enumerate the coordinates of the vertices?

Background: As an example, say I have a 3 dimensional box. What is the most efficient algorithm / code to obtain:

vertex[0] = ( 0, 0, 0 ) -> ( L_0, L_1, L_2 )
vertex[1] = ( 0, 0, 1 ) -> ( L_0, L_1, U_2 )
vertex[2] = ( 0, 1, 0 ) -> ( L_0, U_1, L_2 )
vertex[3] = ( 0, 1, 1 ) -> ( L_0, U_1, U_2 )

vertex[4] = ( 1, 0, 0 ) -> ( U_0, L_1, L_2 )
vertex[5] = ( 1, 0, 1 ) -> ( U_0, L_1, U_2 )
vertex[6] = ( 1, 1, 0 ) -> ( U_0, U_1, L_2 )
vertex[7] = ( 1, 1, 1 ) -> ( U_0, U_1, U_2 )

where L_0 corresponds to the 0'th element of the lower bound vector & likewise U_2 is the 2nd element of the upper bound vector.

My Code:

const unsigned int nVertices = ((unsigned int)(floor(std::pow( 2.0, double(nDimensions)))));

for ( unsigned int idx=0; idx < nVertices; ++idx )
{
   for ( unsigned int k=0; k < nDimensions; ++k )
   {
      if ( 0x00000001 & (idx >> k) )
      {
         bound[idx][k] = upperBound[k];
      }
      else
      {
         bound[idx][k] = lowerBound[k];
      }
   }
}

where the variable bound is declared as:

std::vector< std::vector<double> > bound(nVertices);

but I've pre-sized it so as not to waste time in the loop allocating memory. I need to call the above procedure about 50,000,000 times every time I run my algorithm -- so I need this to be really efficient.

Possible Sub-Questions: Does it tend to be faster to shift by k instead of always shifting by 1 and storing an intermediate result? (Should I be using >>= ??)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陌若浮生 2024-10-13 20:08:11

如果您可以减少条件分支,它可能会更快:

bound[idx][k] = upperLowerBounds[(idx >> k) & 1][k];

如果您可以在单个数组中交错上限和下限,您可能会进一步改进:

bound[idx][k] = upperLowerBounds[(k << 1) | (idx >> k)&1];

我不知道增量移动 idx 是否有帮助。它很容易实现,所以值得一试。

It will probably go faster if you can reduce conditional branching:

bound[idx][k] = upperLowerBounds[(idx >> k) & 1][k];

You might improve things even more if you can interleave the upper and lower bounds in a single array:

bound[idx][k] = upperLowerBounds[(k << 1) | (idx >> k)&1];

I don't know if shifting idx incrementally helps. It's simple enough to implement, so it's worth a try.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文