I have a python function which returns the nth-element in the cartesian product of a number of input arrays
def prod(n, arrs):
out = []
for i,arr in enumerate(arrs):
denom = numpy.prod([ len(p) for p in arrs[i+1:] ], dtype=int)
idx = n // denom % len(arr)
out.append( arr[idx] )
return out
This works great:
a = [ 1000, 1100, 1200, 1300, 1400 ]
b = [ 1.0, 1.5, 2.0, 2.5, 3.0, 3.5 ]
c = [ -2, -1, 0, 1, 2 ]
for n in range(20, 30):
i = prod(n, [a, b, c])
print(n, i)
[1000, 3.0, -2] [1000, 3.0, -1] [1000, 3.0, 0] [1000, 3.0, 1] [1000, 3.0, 2] [1000, 3.5, -2] [1000, 3.5, -1] [1000, 3.5, 0] [1000, 3.5, 1] [1000, 3.5, 2]
Now I would like to translate this to C++ (max standard C++-17)
template<typename... Ts>
auto prod(std::size_t n, const std::vector<Ts>&... vs)
-> std::tuple<const std::decay_t<typename std::vector<Ts>::value_type>&...>
{
// template magic here
}
Can someone help me with the template magic required to construct the tuple using the above formula?