Files
cenet/include/mat.hpp
T

240 lines
6.0 KiB
C++

#ifndef MAT_HPP
#define MAT_HPP
#include <cstddef>
#include <cstring>
#include <random>
#include <type_traits>
template <std::size_t N, typename T = double, typename = std::enable_if_t<(N >= 1)>>
class mat_col {
template <std::size_t M, std::size_t N2, typename T2, typename>
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 <std::size_t M, std::size_t N, typename T = double, typename = std::enable_if_t<(M >= 1 && N >= 1)>>
class mat {
public:
using col_type = mat_col<N, T>;
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 <typename Dist>
static mat<M, N, T> random(Dist dist) {
static std::random_device s_rd;
static std::mt19937 s_mt(s_rd());
mat<M, N, T> 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<N, T> operator[](std::size_t col) { return { m_items + (col * N) }; }
mat_col<N, const T> operator[](std::size_t col) const { return { m_items + (col * N) }; }
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<N, M, T> transpose() const {
mat<N, M, T> 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 <std::size_t M, std::size_t N, typename T>
mat<M, N, T> operator+(T scalar, const mat<M, N, T>& rhs) {
mat<M, N, T> 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 <std::size_t M, std::size_t N, typename T>
mat<M, N, T> operator-(T scalar, const mat<M, N, T>& rhs) {
mat<M, N, T> 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 <std::size_t N, typename T>
mat<1, N, T> operator+(T scalar, const mat_col<N, T>& rhs) {
mat<1, N, T> res;
for (std::size_t j = 0; j < N; ++j) {
res[0][j] = scalar + rhs[j];
}
return res;
}
template <std::size_t N, typename T>
mat<1, N, T> operator-(T scalar, const mat_col<N, T>& rhs) {
mat<1, N, T> res;
for (std::size_t j = 0; j < N; ++j) {
res[0][j] = scalar - rhs[j];
}
return res;
}
template <std::size_t M, std::size_t N, typename T>
mat<M, N, T> operator*(const mat<M, N, T>& lhs, T rhs) {
mat<M, N, T> 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 <std::size_t M, std::size_t N, typename T>
mat<M, N, T> operator*(T lhs, const mat<M, N, T>& rhs) {
mat<M, N, T> 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 <std::size_t M, std::size_t N, std::size_t P, typename T>
mat<M, P, T> operator*(const mat<M, N, T>& lhs, const mat<N, P, T>& rhs) {
mat<M, P, T> 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 <std::size_t M, std::size_t P, typename T>
mat<M, P, T> operator*(const mat<M, 1, T>& lhs, const mat_col<P, T>& rhs) {
mat<M, P, T> 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