【问题标题】:Is it possible to realize column-wise operation and row-wise operation by one template function?是否可以通过一个模板函数实现按列操作和按行操作?
【发布时间】:2016-12-31 21:55:12
【问题描述】:

这是我的问题,我正在处理一个 n 维数据。为简单起见,假设 n=2。我也有一个一维数据的算法。为了将此算法扩展到二维问题,我可以这样做

for each row
    apply algorithm

但是,如果我想对每一列应用这个算法,我需要编写一个新函数

for each column
    apply algorithm

例如,假设我有一个函数:

void func(vector<T> &vec);

然后将这个函数应用到一个向量上,我可以简单地调用这个函数:

vector<T> vec;
func(vec);

对于 3D 数据:

T multiArray[l][m][n];

据我所知,如果我想将上述函数应用于第一维的所有向量,我会这样做:

for(int j=0;j<m;j++){
    for(int k=0;k<n;k++){
        vector<T> singleArray;
        for(int i=0;i<l;i++){
            singleArray.push_back(multiArray[i][j][k]);
        }
        func(singleArray);
    }
}

但是,对于同样的数据,如果我想对所有的三维向量都应用上面的函数,我需要重写为:

for(int i=0;i<l;i++){
    for(int j=0;j<m;j++){
        vector<T> singleArray;
        for(int k=0;k<n;k++){
            singleArray.push_back(multiArray[i][j][k]);
        }
        func(singleArray);
    }
}

基本上,除了每个 for 循环中的迭代器之外,一切都相同。我希望有一些方法可以用一个函数来实现这两种计算。

谢谢

【问题讨论】:

    标签: c++ algorithm math vector n-dimensional


    【解决方案1】:

    我不知道通用解决方案,但您可以使用参考解决您的具体问题(使用第一个、第二个或第三个索引或...)。

    在 3D 情况下,首先可以声明循环变量(ijk

    std::size_t i, j, k;
    

    接下来,您可以根据模板值 I“链接”另一个变量 (ijk)

    std::size_t & r = (I == 0U ? i : (I == 1U ? j : k));
    

    下面是一个可编译的例子

    #include <vector>
    #include <iostream>
    
    template <std::size_t I>
    void func (std::vector<std::vector<std::vector<double> > > & d)
     {
       std::size_t i, j, k;
    
       std::size_t & r = (I == 0U ? i : (I == 1U ? j : k));
    
       for ( i = 0U ; i < d.size() ; ++i )
         for ( j = 0U ; j < d[i].size() ; ++j )
           for ( k = 0U ; k < d[i][j].size() ; ++k )
              d[i][j][k] += r+1;
     }
    
    int main()
     {
       std::vector<std::vector<std::vector<double> > > data;
    
       // some data in data
    
       func<0>(data); // r is i
       func<1>(data); // r is j
       func<2>(data); // r is k
     }
    

    --- 编辑---

    OP 询问

    这个函数是否可以用于任意维度?

    没有。

    不是这个函数。

    但我提出了一个完全不同(而且更复杂)的解决方案。我写了它,但不要让我检查它是否真的有效。

    这个想法不再基于参考,而是基于模板专业化。

    这次模板索引是从 1 开始的:如果你想截取第一个索引(例如x),使用模板值1,如果你想截取第二个索引(例如y),则使用2,等等

    所以你打电话

    foo<1U>(data1);    // r is the first index
    

    对于一维向量,

    foo<1U>(data2);     // r is the first index
    foo<2U>(data2);     // r is the second index
    

    对于二维向量等

    如果你打电话

    foo<I>(data)
    

    如果I 大于data 的维度,则会出现编译错误。

    如果你打电话

    foo<0>(data)
    

    只有在编译 C++11 或更高版本时才会出现编译错误(C++98 r 变为零;但您可以添加 assert() 以获取运行时错误)。

    例子

    #include <vector>
    #include <iostream>
    
    template <std::size_t I>
    struct bar
     {
       template <typename T>
       static void baz (std::vector<T> & v, std::size_t)
        {
          for ( std::size_t i = 0U ; i < v.size() ; ++i )
             bar<I-1U>::baz(v[i], i);
        }
     };
    
    template <>
    struct bar<0U>
     {
       template <typename T>
       static void baz (std::vector<T> & v, std::size_t r)
        {
          for ( std::size_t i = 0U ; i < v.size() ; ++i )
             baz(v[i], r);
        }
    
       static void baz (double & d, std::size_t r)
        { d += r + 1U; }
     };
    
    
    template <std::size_t I, typename V>
    void foo (V & v)
     {
    #if __cplusplus >= 201103L   
       static_assert(I > 0U, "!"); // c++11 or newer
    #endif
    
       bar<I>::baz(v, 0U);
     }
    
    int main()
     {
       std::vector<double >                            data1;
       std::vector<std::vector<double> >               data2;
       std::vector<std::vector<std::vector<double> > > data3;
    
       // some data in data1, data2 and data3
    
       // foo<0U>(data1);  // compilation error in C++11 or newer
       foo<1U>(data1);     // r is the first index
       // foo<2U>(data1);  // compilation error
    
       // foo<0U>(data2);  // compilation error in C++11 or newer
       foo<1U>(data2);     // r is the first index
       foo<2U>(data2);     // r is the second index
       // foo<3U>(data2);  // compilation error
    
       // foo<0U>(data3);  // compilation error in C++11 or newer
       foo<1U>(data3);     // r is the first index
       foo<2U>(data3);     // r is the second index
       foo<3U>(data3);     // r is the third index
       // foo<4U>(data3);  // compilation error
     }
    

    【讨论】:

    • 这个函数是否可以用于任意维度?
    • @DiMiao - 答案改进;希望这会有所帮助。
    • Thx,你的方法为我的问题提供了一个很好的方向。真的很有帮助。
    猜你喜欢
    • 2019-05-06
    • 1970-01-01
    • 1970-01-01
    • 2017-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多