From 034a5fddfb292f22659f293d72ceb576f5c61d82 Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Thu, 3 Aug 2023 16:18:00 +0200 Subject: A5: code complete --- a5/imatrix_nm.h | 80 ++++++++++++++++++++++++++++----------------------------- 1 file changed, 40 insertions(+), 40 deletions(-) (limited to 'a5/imatrix_nm.h') 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 #include -template +template class ImatrixNM { public: ImatrixNM() - : data(0, W * H) + : data(0, N * M) { } - ImatrixNM(ImatrixNM&& other) + ImatrixNM(ImatrixNM&& other) { *this = std::move(other); } - ImatrixNM(const ImatrixNM& other) + ImatrixNM(const ImatrixNM& other) { *this = other; } - ImatrixNM& operator=(ImatrixNM&& other) + ImatrixNM& operator=(ImatrixNM&& other) { data = std::move(other.data); return *this; } - ImatrixNM& operator=(const ImatrixNM& other) + ImatrixNM& operator=(const ImatrixNM& 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 operator+(const ImatrixNM& other) const + ImatrixNM operator+(const ImatrixNM& other) const { ImatrixNM m(*this); m.data += other.data; return m; } - ImatrixNM operator+(int val) + ImatrixNM operator+(int val) { - ImatrixNM m(*this); + ImatrixNM m(*this); m.data += val; return m; } - ImatrixNM operator-(const ImatrixNM& other) const + ImatrixNM operator-(const ImatrixNM& other) const { - ImatrixNM m(*this); + ImatrixNM m(*this); m.data -= other.data; return m; } - ImatrixNM operator-(int val) + ImatrixNM operator-(int val) { - ImatrixNM m(*this); + ImatrixNM m(*this); m.data -= val; return m; } - ImatrixNM operator%(const ImatrixNM& other) const + ImatrixNM operator%(const ImatrixNM& other) const { - ImatrixNM m(*this); + ImatrixNM m(*this); m.data %= other.data; return m; } - ImatrixNM operator%(int val) + ImatrixNM operator%(int val) { ImatrixNM m(*this); m.data %= val; return m; } - template - ImatrixNM operator*(const ImatrixNM& other) const + template + ImatrixNM operator*(const ImatrixNM& other) const { - ImatrixNM out; - for(std::size_t i = 0; i < W; ++i) + ImatrixNM 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 operator*(int val) const + ImatrixNM operator*(int val) const { - ImatrixNM m(*this); + ImatrixNM m(*this); m.data *= val; return m; } - template - ImatrixNM operator/(const ImatrixNM& other) const + template + ImatrixNM operator/(const ImatrixNM& other) const { - ImatrixNM out; - for(std::size_t i = 0; i < W; ++i) + ImatrixNM 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 operator/(int val) const + ImatrixNM operator/(int val) const { - ImatrixNM m(*this); + ImatrixNM m(*this); m.data /= val; return m; } @@ -156,9 +156,9 @@ public: std::vector 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 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 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 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)); } -- cgit v1.2.3