/* @copyright Louis Dionne 2014 Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt) */ #ifndef BOOST_HANA_EXAMPLE_CPPCON_2014_MATRIX_MATRIX_HPP #define BOOST_HANA_EXAMPLE_CPPCON_2014_MATRIX_MATRIX_HPP #include #include #include #include #include #include #include #include #include #include namespace cppcon { template struct Matrix { struct hana { struct enabled_operators : boost::hana::Comparable , boost::hana::Monoid , boost::hana::Group , boost::hana::Ring { }; }; }; template struct matrix_type { struct hana { using datatype = Matrix; }; Storage rows_; constexpr auto ncolumns() const { return boost::hana::length(boost::hana::head(rows_)); } constexpr auto nrows() const { return boost::hana::length(rows_); } constexpr auto size() const { return nrows() * ncolumns(); } template constexpr decltype(auto) at(I i, J j) const { return boost::hana::at(j, boost::hana::at(i, rows_)); } }; auto row = boost::hana::tuple; auto matrix = [](auto&& ...rows) -> decltype(auto) { using namespace boost::hana; auto storage = tuple(std::forward(rows)...); auto ncolumns = length(head(storage)); BOOST_HANA_CONSTANT_CHECK( all(tail(storage), [&](auto const& row) { return length(row) == ncolumns; }) ); return matrix_type< sizeof...(rows), value(ncolumns), decltype(storage) >{std::move(storage)}; }; auto vector = boost::hana::on(matrix, row); // More operations auto rows = [](auto&& m) -> decltype(auto) { return std::forward(m).rows_; }; auto transpose = [](auto&& m) -> decltype(auto) { return boost::hana::unpack( boost::hana::unzip(rows(std::forward(m))), matrix ); }; auto columns = [](auto&& m) -> decltype(auto) { return rows(transpose(std::forward(m))); }; auto element_wise = [](auto&& f) -> decltype(auto) { using namespace boost::hana; return [f(std::forward(f))](auto&& ...m) -> decltype(auto) { return unpack( zip_with(partial(zip_with, f), rows(std::forward(m))... ), matrix ); }; }; namespace detail { auto tuple_scalar_product = [](auto&& u, auto&& v) -> decltype(auto) { using namespace boost::hana; return sum(zip_with(mult, std::forward(u), std::forward(v) )); }; } } // end namespace cppcon #endif // !BOOST_HANA_EXAMPLE_CPPCON_2014_MATRIX_MATRIX_HPP