ring.hpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // Copyright Louis Dionne 2013-2017
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
  4. #ifndef BOOST_HANA_EXAMPLE_CPPCON_2014_MATRIX_RING_HPP
  5. #define BOOST_HANA_EXAMPLE_CPPCON_2014_MATRIX_RING_HPP
  6. #include "matrix.hpp"
  7. #include <boost/hana/equal.hpp>
  8. #include <boost/hana/fwd/mult.hpp>
  9. #include <boost/hana/fwd/one.hpp>
  10. #include <boost/hana/if.hpp>
  11. #include <boost/hana/integral_constant.hpp>
  12. #include <boost/hana/range.hpp>
  13. #include <boost/hana/replicate.hpp>
  14. #include <boost/hana/transform.hpp>
  15. #include <boost/hana/tuple.hpp>
  16. #include <boost/hana/unpack.hpp>
  17. #include <boost/hana/zip_with.hpp>
  18. #include <utility>
  19. namespace boost { namespace hana {
  20. template <unsigned R1, unsigned C1, unsigned R2, unsigned C2>
  21. struct mult_impl<cppcon::Matrix<R1, C1>, cppcon::Matrix<R2, C2>> {
  22. template <typename M1, typename M2>
  23. static constexpr decltype(auto) apply(M1&& m1, M2&& m2) {
  24. static_assert(C1 == R2,
  25. "wrong dimensions for matrix multiplication");
  26. auto cols = cppcon::columns(std::forward<M2>(m2));
  27. return unpack(
  28. transform(cppcon::rows(std::forward<M1>(m1)),
  29. [&](auto&& row) -> decltype(auto) {
  30. return zip_with(cppcon::detail::tuple_scalar_product,
  31. replicate<tuple_tag>(std::forward<decltype(row)>(row), uint_c<R1>),
  32. cols
  33. );
  34. }
  35. ),
  36. cppcon::matrix
  37. );
  38. }
  39. };
  40. template <unsigned R, unsigned C>
  41. struct one_impl<cppcon::Matrix<R, C>> {
  42. static constexpr decltype(auto) apply() {
  43. return unpack(range_c<unsigned, 0, R>, [](auto ...n) {
  44. return unpack(range_c<unsigned, 0, C>, [=](auto ...m) {
  45. auto row = [=](auto n) {
  46. return cppcon::row(if_(n == m, int_c<1>, int_c<0>)...);
  47. };
  48. return cppcon::matrix(row(n)...);
  49. });
  50. });
  51. }
  52. };
  53. }}
  54. #endif // !BOOST_HANA_EXAMPLE_CPPCON_2014_MATRIX_RING_HPP