#ifndef MAT_HPP #define MAT_HPP #include #include #include #include template = 1)>> class mat_col { template friend class mat; private: mat_col(T* data) : m_data(data) {} public: T& operator[](std::size_t idx) { return m_data[idx]; } const T& operator[](std::size_t idx) const { return m_data[idx]; } private: T* m_data; }; template = 1 && N >= 1)>> class mat { public: using col_type = mat_col; public: mat() { std::memset(m_items, 0, sizeof(m_items)); } mat(T init) { for (std::size_t i = 0; i < M; ++i) { for (std::size_t j = 0; j < N; ++j) { (*this)[i][j] = init; } } } mat(col_type col) { static_assert((M == 1), "Invalid"); std::memcpy(m_items, col.m_data, sizeof(m_items)); } ~mat() = default; mat(mat&& other) noexcept { std::memcpy(m_items, other.m_items, sizeof(m_items)); } mat& operator=(mat&& other) noexcept { if (this == &other) return *this; std::memcpy(m_items, other.m_items, sizeof(m_items)); return *this; } mat(const mat& other) { std::memcpy(m_items, other.m_items, sizeof(m_items)); } mat& operator=(const mat& other) { if (this == &other) return *this; std::memcpy(m_items, other.m_items, sizeof(m_items)); return *this; } public: template static mat random(Dist dist) { static std::random_device s_rd; static std::mt19937 s_mt(s_rd()); mat res; for (std::size_t i = 0; i < M; ++i) { for (std::size_t j = 0; j < N; ++j) { res[i][j] = dist(s_mt); } } return res; } public: mat_col operator[](std::size_t col) { return { m_items + (col * M) }; } mat_col operator[](std::size_t col) const { return { m_items + (col * M) }; } public: mat operator+(const mat& rhs) const { mat res; for (std::size_t i = 0; i < M; ++i) { for (std::size_t j = 0; j < N; ++j) { res[i][j] = (*this)[i][j] + rhs[i][j]; } } return res; } mat& operator+=(const mat& rhs) { for (std::size_t i = 0; i < M; ++i) { for (std::size_t j = 0; j < N; ++j) { (*this)[i][j] += rhs[i][j]; } } return *this; } mat operator-(const mat& rhs) const { mat res; for (std::size_t i = 0; i < M; ++i) { for (std::size_t j = 0; j < N; ++j) { res[i][j] = (*this)[i][j] - rhs[i][j]; } } return res; } mat& operator-=(const mat& rhs) { for (std::size_t i = 0; i < M; ++i) { for (std::size_t j = 0; j < N; ++j) { (*this)[i][j] -= rhs[i][j]; } } return *this; } public: mat hadamard(const mat& rhs) const { mat res = *this; for (std::size_t i = 0; i < M; ++i) { for (std::size_t j = 0; j < N; ++j) { res[i][j] *= rhs[i][j]; } } return res; } public: mat transpose() const { mat res; for (std::size_t i = 0; i < M; ++i) { for (std::size_t j = 0; j < N; ++j) { res[j][i] = (*this)[i][j]; } } return res; } private: T m_items[M * N]; }; template mat operator+(T scalar, const mat& rhs) { mat res; for (std::size_t i = 0; i < M; ++i) { for (std::size_t j = 0; j < N; ++j) { res[i][j] = scalar + rhs[i][j]; } } return res; } template mat operator-(T scalar, const mat& rhs) { mat res; for (std::size_t i = 0; i < M; ++i) { for (std::size_t j = 0; j < N; ++j) { res[i][j] = scalar - rhs[i][j]; } } return res; } template mat<1, N, T> operator+(T scalar, const mat_col& rhs) { mat<1, N, T> res; for (std::size_t j = 0; j < N; ++j) { res[0][j] = scalar + rhs[j]; } return res; } template mat<1, N, T> operator-(T scalar, const mat_col& rhs) { mat<1, N, T> res; for (std::size_t j = 0; j < N; ++j) { res[0][j] = scalar - rhs[j]; } return res; } template mat operator*(const mat& lhs, T rhs) { mat res; for (std::size_t i = 0; i < M; ++i) { for (std::size_t j = 0; j < N; ++j) { res[i][j] = lhs[i][j] * rhs; } } return res; } template mat operator*(T lhs, const mat& rhs) { mat res; for (std::size_t i = 0; i < M; ++i) { for (std::size_t j = 0; j < N; ++j) { res[i][j] = rhs[i][j] * lhs; } } return res; } template mat operator*(const mat& lhs, const mat& rhs) { mat res; for (std::size_t i = 0; i < M; ++i) { for (std::size_t j = 0; j < P; ++j) { for (std::size_t k = 0; k < N; ++k) { res[i][j] += lhs[i][k] * rhs[k][j]; } } } return res; } template mat operator*(const mat& lhs, const mat_col& rhs) { mat res; for (std::size_t i = 0; i < M; ++i) { for (std::size_t j = 0; j < P; ++j) { res[i][j] += lhs[i][0] * rhs[j]; } } return res; } #endif // MAT_HPP