diff options
Diffstat (limited to 'a5/imatrix_nm.h')
-rw-r--r-- | a5/imatrix_nm.h | 80 |
1 files changed, 40 insertions, 40 deletions
diff --git a/a5/imatrix_nm.h b/a5/imatrix_nm.h index 970031a..f69a099 100644 --- a/a5/imatrix_nm.h +++ b/a5/imatrix_nm.h @@ -5,32 +5,32 @@ #include <valarray> #include <stdexcept> -template<std::size_t W, std::size_t H> +template<std::size_t N, std::size_t M> class ImatrixNM { public: ImatrixNM() - : data(0, W * H) + : data(0, N * M) { } - ImatrixNM(ImatrixNM<W, H>&& other) + ImatrixNM(ImatrixNM<N, M>&& other) { *this = std::move(other); } - ImatrixNM(const ImatrixNM<W, H>& other) + ImatrixNM(const ImatrixNM<N, M>& other) { *this = other; } - ImatrixNM& operator=(ImatrixNM<W, H>&& other) + ImatrixNM& operator=(ImatrixNM<N, M>&& other) { data = std::move(other.data); return *this; } - ImatrixNM& operator=(const ImatrixNM<W, H>& other) + ImatrixNM& operator=(const ImatrixNM<N, M>& other) { data = other.data; return *this; @@ -38,70 +38,70 @@ public: int& m(std::size_t x, std::size_t y) { - if(x >= W || y >= H) throw std::out_of_range("Subscript out of range."); - return data[x + y * W]; + if(x >= N || y >= M) throw std::out_of_range("Subscript out of range."); + return data[x + y * N]; } const int& m(std::size_t x, std::size_t y) const { - if(x >= W || y >= H) throw std::out_of_range("Subscript out of range."); - return data[x + y * W]; + if(x >= N || y >= M) throw std::out_of_range("Subscript out of range."); + return data[x + y * N]; } - ImatrixNM<W, H> operator+(const ImatrixNM<W, H>& other) const + ImatrixNM<N, M> operator+(const ImatrixNM<N, M>& other) const { ImatrixNM m(*this); m.data += other.data; return m; } - ImatrixNM<W, H> operator+(int val) + ImatrixNM<N, M> operator+(int val) { - ImatrixNM<W, H> m(*this); + ImatrixNM<N, M> m(*this); m.data += val; return m; } - ImatrixNM<W, H> operator-(const ImatrixNM<W, H>& other) const + ImatrixNM<N, M> operator-(const ImatrixNM<N, M>& other) const { - ImatrixNM<W, H> m(*this); + ImatrixNM<N, M> m(*this); m.data -= other.data; return m; } - ImatrixNM <W, H>operator-(int val) + ImatrixNM <N, M>operator-(int val) { - ImatrixNM<W, H> m(*this); + ImatrixNM<N, M> m(*this); m.data -= val; return m; } - ImatrixNM<W, H> operator%(const ImatrixNM<W, H>& other) const + ImatrixNM<N, M> operator%(const ImatrixNM<N, M>& other) const { - ImatrixNM<W, H> m(*this); + ImatrixNM<N, M> m(*this); m.data %= other.data; return m; } - ImatrixNM<W, H> operator%(int val) + ImatrixNM<N, M> operator%(int val) { ImatrixNM m(*this); m.data %= val; return m; } - template<std::size_t N> - ImatrixNM<W, N> operator*(const ImatrixNM<H, N>& other) const + template<std::size_t R> + ImatrixNM<N, R> operator*(const ImatrixNM<M, R>& other) const { - ImatrixNM<W, N> out; - for(std::size_t i = 0; i < W; ++i) + ImatrixNM<N, R> out; + for(std::size_t i = 0; i < N; ++i) { for(std::size_t j = 0; j < N; ++j) { out.m(i,j) = 0; - for(std::size_t k = 0; k < H; ++k) + for(std::size_t k = 0; k < M; ++k) { - out.m(i,j) += m(i, j) * other.m(k, j); + out.m(i,j) += m(i,k) * other.m(k,j); } } } @@ -109,25 +109,25 @@ public: return out; } - ImatrixNM<W, H> operator*(int val) const + ImatrixNM<N, M> operator*(int val) const { - ImatrixNM<W, H> m(*this); + ImatrixNM<N, M> m(*this); m.data *= val; return m; } - template<std::size_t N> - ImatrixNM<W, N> operator/(const ImatrixNM<H, N>& other) const + template<std::size_t R> + ImatrixNM<N, R> operator/(const ImatrixNM<M, R>& other) const { - ImatrixNM<W, N> out; - for(std::size_t i = 0; i < W; ++i) + ImatrixNM<N, R> out; + for(std::size_t i = 0; i < N; ++i) { for(std::size_t j = 0; j < N; ++j) { out.m(i,j) = 0; - for(std::size_t k = 0; k < H; ++k) + for(std::size_t k = 0; k < M; ++k) { - out.m(i,j) += m(i, j) / other.m(k, j); + out.m(i,j) += m(i,k) / other.m(k,j); } } } @@ -135,9 +135,9 @@ public: return out; } - ImatrixNM<W, H> operator/(int val) const + ImatrixNM<N, M> operator/(int val) const { - ImatrixNM<W, H> m(*this); + ImatrixNM<N, M> m(*this); m.data /= val; return m; } @@ -156,9 +156,9 @@ public: std::vector<int> Row(std::size_t n) const { - if(n >= H) throw std::out_of_range("Subscript out of range."); + if(n >= M) throw std::out_of_range("Subscript out of range."); std::vector<int> out; - for(std::size_t x = 0; x < W; ++x) + for(std::size_t x = 0; x < N; ++x) { out.push_back(m(x, n)); } @@ -167,9 +167,9 @@ public: std::vector<int> Column(std::size_t n) const { - if(n >= W) throw std::out_of_range("Subscript out of range."); + if(n >= N) throw std::out_of_range("Subscript out of range."); std::vector<int> out; - for(std::size_t y = 0; y < H; ++y) + for(std::size_t y = 0; y < M; ++y) { out.push_back(m(n, y)); } |