产生编译时间阵列结构(C ++ 17)

问题描述 投票:2回答:1

我有以下C ++ 17代码,以产生阵列,其中零个阵列是只为示例的目的而在我的实现他们将满(与-std = C ++编译1Z的编译时元组 - fconcepts)。

#include <array>
#include <tuple>
#include <cmath>

template <std::size_t nrA, std::size_t ncA, std::size_t nrB, std::size_t ncB,
          typename number=double>
constexpr auto operator *(const std::array<std::array<number,ncA>,nrA> & A,
                          const std::array<std::array<number,ncB>,nrB> & B)
{
  std::array<std::array<number,ncB>,nrA> res{} ;
  for (auto k=0u;k<ncB;++k)
    for (auto i=0u;i<nrA;++i)
      for (auto j=0u;j<nrB;++j)
        res[i][k] += A[i][j]*B[j][k];
  return res ;
}

constexpr auto logg2(const auto N)
{
  auto res = 0;
  auto n = N;
  while (n != 0) 
  {
    n /= 2;
    ++res;
  }
  return res;
}

template <std::size_t N,typename number=double>
constexpr auto create_R()
{
  return std::array<std::array<double,2*N>,N>{};
}

template <std::size_t N,typename number=double>
constexpr auto create_RT()
{
  return std::array<std::array<double,N>,2*N>{};
}

template <std::size_t N,std::size_t ...Is>
constexpr auto make_impl(const std::index_sequence<Is...>)
{
  return std::make_tuple(std::make_tuple(create_R<(N >> Is)>()...),
                         std::make_tuple(create_RT<(N >> Is)>()...));
}

template <std::size_t N,typename number=double>
constexpr auto make()
{
  return make_impl<N/2>(std::make_index_sequence<logg2(N/2) - 1>());
}

int main(int argc, char *argv[])
{
  const auto n = 4u;
  const auto A = std::array<std::array<double,2*n>,2*n>{};
  const auto [R,RT] = make<2*n>();
}

我想修改make<>()make<>(A),并返回一个结构化的结合[R,RT,As]其中As是包含在它下面的阵列元组

                              A,
               std::get<0>(R)*A*std::get<0>(RT),
std::get<1>(R)*std::get<0>(R)*A*std::get<0>(RT)*std::get<1>(RT)
                             ...

我一直在尝试了一段时间,发现没有解决方案。

有任何想法吗?

编辑1

按照要求通过@MaxLanghof,下面的打印矩阵:

template <std::size_t nr, std::size_t nc, typename number=double>
constexpr auto print(const std::array<std::array<number,nc>,nr> & A)
{
  for (auto i=0u;i<nr;++i)
    {
      for (auto j=0u;j<nc;++j)
        std::cout << std::right << std::setw(12) << A[i][j];
      std::cout << std::endl ;
    }
  std::cout << std::endl ;
}

并加入以下行main()

print(A);
print(std::get<0>(R)*A*std::get<0>(RT));
print(std::get<1>(R)*std::get<0>(R)*A*std::get<0>(RT)*std::get<1>(RT));

可以得到下面的输出

       0           0           0           0           0           0           0           0
       0           0           0           0           0           0           0           0
       0           0           0           0           0           0           0           0
       0           0           0           0           0           0           0           0
       0           0           0           0           0           0           0           0
       0           0           0           0           0           0           0           0
       0           0           0           0           0           0           0           0
       0           0           0           0           0           0           0           0

       0           0           0           0
       0           0           0           0
       0           0           0           0
       0           0           0           0

       0           0
       0           0
c++ arrays recursion c++17 compile-time
1个回答
3
投票

该解决方案包括创建用于每个As元组的条目的另一索引序列,然后用它来折叠表达乘法。我参加了一个类型的可读性包装std::array<std::array<T, Cols>, Rows>的自由(这也结束了必要的,见下文)。到makeMul每个呼叫产生了As元组元素(原A单独加入)中的一个。

template <std::size_t Rows, std::size_t Cols, typename T = double>
struct Mat2D : std::array<std::array<T, Cols>, Rows> {};

template <class T_Rs, std::size_t... Is>
constexpr auto mult_lhs(T_Rs Rs, std::index_sequence<Is...>) {
  return (std::get<sizeof...(Is) - Is - 1>(Rs) * ...);
}

template <class T_RTs, std::size_t... Is>
constexpr auto mult_rhs(T_RTs RTs, std::index_sequence<Is...>) {
  return (std::get<Is>(RTs) * ...);
}

template <class T_A, class T_Rs, class T_RTs, std::size_t... Is>
constexpr auto makeMul_impl(T_A A, T_Rs Rs, T_RTs RTs,
                            std::index_sequence<Is...> is) {
  return mult_lhs(Rs, is) * A * mult_rhs(RTs, is);
}

template <std::size_t Index, class T_A, class T_Rs, class T_RTs>
constexpr auto makeMul(T_A A, T_Rs Rs, T_RTs RTs) {
  return makeMul_impl(A, Rs, RTs, std::make_index_sequence<Index + 1>());
}

template <std::size_t N, std::size_t... Is, typename T = double>
constexpr auto make_impl(const Mat2D<2 * N, 2 * N, T>& A,
                         std::index_sequence<Is...>) {
  auto Rs = std::make_tuple(create_R<(N >> Is)>()...);
  auto RTs = std::make_tuple(create_RT<(N >> Is)>()...);
  auto As = std::make_tuple(A, makeMul<Is>(A, Rs, RTs)...);
  return std::make_tuple(Rs, RTs, As);
}

template <std::size_t N, typename T = double>
constexpr auto make(const Mat2D<N, N, T>& A) {
  return make_impl<N / 2>(A, std::make_index_sequence<logg2(N / 2) - 1>());
}

int main(int argc, char* argv[]) {
  const auto n = 4u;
  const auto A = Mat2D<2 * n, 2 * n, double>{};
  const auto [Rs, RTs, As] = make(A);
}

Demo

一旦你尝试使用重载std在模板中,它不会因为长相ADL发现:需要注意的是重载运营商clang类型是这里有问题,至少operator*(下面的标准更严格的)是非常重要的它在namespace std(我最初有Mat2D作为别名,而不是一个struct继承的东西) - 你是不允许添加的东西namespace std(除了少数特别定制点)。至少,这就是我所理解this错误。

std类型的继承是非常可怕的整体,但我会假设你的矩阵实际上是在实践中,用户定义类型,以便所有这将此事。

最后,我会认真地推荐给你的元组类型给予实际名称。当你有(数组的数组)的元组的元组,每个读者都会有投入的时间显著量甚至抓码。它将如果你已经帮例如分组在一个结构的每个RRT

template<std::size_t N, typename T = double>
struct R_RT {
  Mat2D<N, 2 * N, T> R;
  Mat2D<2 * N, N, T> RT;
};

和/或具有例如一种

template<class TupleOfR, class TupleOfRT, class TupleOfAs>
struct Rs_RTs_As {
  TupleOfR Rs;
  TupleOfRT RTs;
  TupleOfAs As;
};

即使在技术上允许任何三种类型,它仍然记录了你应该会发现那里的东西,并与概念,实际上你可以适当地限制的一切(包括作为“As将比RsRTs多了一个元素”这样的乐趣,这可能会采取多数读者而从纯粹的元组代码来实现)。

© www.soinside.com 2019 - 2024. All rights reserved.