使用std :: tuple使用模板的多项式乘法

时间:2018-12-16 13:18:43

标签: c++ c++11 templates variadic-templates template-meta-programming

假设我有两个表示两个多项式系数的元组,那么我该如何计算乘法系数。 我想使用模板完成此操作。

mul_scalar()将元组的值乘以标量。 我正在尝试使用此函数扩展到多项式情况。通过遍历一个元组并使用mul_scalar()

#include <tuple>
#include <utility> 
#include <iostream>

template<std::size_t I = 0, typename T, typename... Tp>
inline typename std::enable_if<I == sizeof...(Tp), void>::type
mul_scalar(const T& lhs, const std::tuple<Tp...> rhs ) // Unused arguments are given no names.
{ }

template<std::size_t I = 0, typename T, typename... Tp>
inline typename std::enable_if<I < sizeof...(Tp), void>::type
mul_scalar(const T& lhs, const std::tuple<Tp...> rhs)
{
    std::cout << (std::get<I>(rhs))*lhs << " ";
    mul_scalar<I + 1, T, Tp...>(lhs, rhs);
}

template <std::size_t I = 0, template <typename ...> class Tup1, template <typename ...> class Tup2, typename ...A, typename ...B>
inline typename std::enable_if<I < sizeof...(A), void>::type
mul_poly(const Tup1<A...>& lhs, const Tup2<B...> rhs)
{
    mul_scalar(std::get<I>(lhs), rhs);
    //mul_poly(lhs, rhs); with I = I + 1
    // However I can't figure how to give other template args
}

int main(){
    auto poly_1 = std::make_tuple(2,1);
    auto poly_2 = std::make_tuple(3,4,5);
    mul_scalar(3,poly_1);
    std::cout << "\n";

    // Expected output 6 8 10 3 4 5
    mul_poly(poly_1, poly_2);
}

1 个答案:

答案 0 :(得分:4)

递归调用很简单

mul_poly<I+1u>(lhs, rhs);

但您必须添加基本案例

template <std::size_t I = 0, template <typename ...> class Tup1,
          template <typename ...> class Tup2, typename ...A, typename ...B>
inline typename std::enable_if<I == sizeof...(A), void>::type
    mul_poly (const Tup1<A...> &, const Tup2<B...> &)
 { }
  

如果您还可以告诉我其他方法,那将是很好的

奖金建议:如果可以使用C ++ 17,则可以使用mul_scalar() / std::index_sequence(或std::make_index_sequence)为std::index_sequence_for避免SFINAE和递归模板折叠如下

template <typename T, typename ... Tp, std::size_t ... Is>
void mul_scalar_helper (T const & lhs, std::tuple<Tp...> const & rhs,
                        std::index_sequence<Is...> const &)
 { ((std::cout << (std::get<Is>(rhs))*lhs << ' '), ...); }

template <typename T, typename ... Tp>
void mul_scalar (T const & lhs, std::tuple<Tp...> const & rhs)
 { mul_scalar_helper(lhs, rhs, std::index_sequence_for<Tp...>{}); }

对于mul_poly()来说并没有很大的区别

template <template <typename ...> class Tup1,
          template <typename ...> class Tup2,
          typename ... A, typename ... B, std::size_t ... Is>
void mul_poly_helper (Tup1<A...> const & lhs, Tup2<B...> const & rhs,
                      std::index_sequence<Is...> const &)
 { (mul_scalar(std::get<Is>(lhs), rhs), ...); }

template <template <typename ...> class Tup1,
          template <typename ...> class Tup2,
          typename ...A, typename ...B>
void mul_poly (Tup1<A...> const & lhs, Tup2<B...> const & rhs)
 { mul_poly_helper(lhs, rhs, std::index_sequence_for<A...>{}); }

无论如何...您确定std::tuple是正确的容器吗?您是否考虑过std::array

---编辑---

遵循liliscent的建议(谢谢!),您可以避免使用简单编写的std::index_sequencestd::index_sequence_for / std::apply()来使用辅助功能,

template <typename T, typename ... Tp>
void mul_scalar (T const & lhs, std::tuple<Tp...> const & rhs)
 { std::apply([&](auto const & ... x){ ((std::cout << x*lhs << ' '), ...); }, rhs); }

template <template <typename ...> class Tup1,
          template <typename ...> class Tup2,
          typename ...A, typename ...B>
void mul_poly (Tup1<A...> const & lhs, Tup2<B...> const & rhs)
 { std::apply([&](auto const & ... x){ (mul_scalar(x, rhs), ...); }, lhs); }

您也可以用两个mul_poly()来写std::apply(),而无需调用mul_scalar()

template <template <typename ...> class Tup1,
          template <typename ...> class Tup2,
          typename ...A, typename ...B>
void mul_poly (Tup1<A...> const & lhs, Tup2<B...> const & rhs)
 { std::apply([&](auto const & ... x){
      (std::apply([x](auto const & ... y){ ((std::cout << x*y << ' '), ...); }, 
                  rhs), ...); }, lhs); }