【发布时间】:2019-10-31 13:06:11
【问题描述】:
我必须在编译期间计算一维数组内n 维矩阵中元素的位置。
简而言之,array[i][j][k]....[n] -> array[...]的映射,其中i,j,k,...,n在{1,2,3}中,每个索引的维度为DIM = 3。也就是说,每一行每一列都有 3 个元素。
我的主要问题是编写n 索引(参数包)的总和,作为模板并使用constexpr 在编译时评估总和。
我在其他堆栈帖子中的研究得出了以下 3 维公式:
a[i][j][k] -> a[(i*DIM*DIM) + (j*DIM) + k]
如果我们将其扩展为n 维度,则会得到以下公式:
a[i][j][k]....[n] -> a[(n*DIM ^ (indexAmount-1)] +... + (i*DIM*DIM) + (j*DIM) + k].
此外,i 使用模板和constexpr 编写了代码来生成和的加数,如下面的代码所示。
/**
* calculates DIM3^(indexPos)
*/
template<auto count>
int constexpr multiple_dim(){
if constexpr (count == 0){
return 1;
}else{
return DIM3 * multiple_dim<count-1>();
}
}
/**
*
*calculates addends for the end summation
* e.g if we have 3 indices i,j,k. j would be at position 2
* and j = 1. The parameters would be IndexPos = 2, index = 1.
*/
template<auto indexPos, auto index>
int constexpr calculate_flattened_index(){
if constexpr (indexPos == 0){
return (index-1);
}else{
return (index-1) * multiple_dim<indexPos>();
}
}
/**
* calculates the position of an element inside a
* nD matrix and maps it to a position in 1D
* A[i][j]..[n] -> ???? not implemented yet
* @tparam Args
* @return
*/
template<auto ...Args>
[[maybe_unused]] auto constexpr pos_nd_to_1d(){
/* maybe iterate over all indices inside the parameter pack?
const int count = 1;
for(int x : {Args...}){
}
return count;
*/
}
3D 矩阵 A 内元素的示例输出。
A111、A121、A131。 3 个元素的总和将是 1D 中的位置。例如A121 -> 0 + 3 + 0 = 3。 A111 将被放置在array[3] 的一维数组中。
std::cout << "Matrix A111" << std::endl;
//A111
std::cout << calculate_flattened_index<0 , 1>() << std::endl;
std::cout << calculate_flattened_index<1 , 1>() << std::endl;
std::cout << calculate_flattened_index<2 , 1>() << std::endl;
std::cout << "Matrix A121" << std::endl;
//A121
std::cout << calculate_flattened_index<0 , 1>() << std::endl;
std::cout << calculate_flattened_index<1 , 2>() << std::endl;
std::cout << calculate_flattened_index<2 , 1>() << std::endl;
std::cout << "Matrix A131" << std::endl;
//A131
std::cout << calculate_flattened_index<0 , 1>() << std::endl;
std::cout << calculate_flattened_index<1 , 3>() << std::endl;
std::cout << calculate_flattened_index<2 , 1>() << std::endl;
Output:
Matrix A111
0
0
0
Matrix A121
0
3
0
Matrix A131
0
6
0
所需的输出可能类似于以下代码:
函数调用
pos_nd_to_1d<1,1,1>() //A111
pos_nd_to_1d<1,2,1>() //A121
pos_nd_to_1d<1,3,1>() //A131
输出:
0 //0+0+0
3 //0+3+0
6 //0+6+0
【问题讨论】:
标签: templates matrix c++17 variadic-templates template-meta-programming