diff options
author | Bent Bisballe Nyeng <deva@aasimon.org> | 2023-08-03 16:18:00 +0200 |
---|---|---|
committer | Bent Bisballe Nyeng <deva@aasimon.org> | 2023-08-03 16:18:00 +0200 |
commit | 034a5fddfb292f22659f293d72ceb576f5c61d82 (patch) | |
tree | 80fb94e7f859ba81943132e2c02fa09946b9be0a /a5/matrix.cc | |
parent | d4d7a3a372fa64d84a48e8f0a472349cbb67b4fb (diff) |
A5: code complete
Diffstat (limited to 'a5/matrix.cc')
-rw-r--r-- | a5/matrix.cc | 222 |
1 files changed, 211 insertions, 11 deletions
diff --git a/a5/matrix.cc b/a5/matrix.cc index 01744fa..4a34fa0 100644 --- a/a5/matrix.cc +++ b/a5/matrix.cc @@ -2,26 +2,226 @@ #include "imatrix_nm.h" #include "matrix.h" +#include <iostream> + +template<typename T, std::size_t N, std::size_t M> +std::ostream& operator<<(std::ostream& s, const Matrix<T,N,M>& m) +{ + for(auto j = 0u; j < M; ++j) + { + for(auto i = 0u; i < N; ++i) + { + s << m.m(i,j) << ' '; + } + s << '\n'; + } + return s; +} + +template<std::size_t N, std::size_t M> +std::ostream& operator<<(std::ostream& s, const ImatrixNM<N,M>& m) +{ + for(auto j = 0u; j < M; ++j) + { + for(auto i = 0u; i < N; ++i) + { + s << m.m(i,j) << ' '; + } + s << '\n'; + } + return s; +} + +std::ostream& operator<<(std::ostream& s, const Imatrix& m) +{ + for(auto j = 0u; j < m.h; ++j) + { + for(auto i = 0u; i < m.w; ++i) + { + s << m.m(i,j) << ' '; + } + s << '\n'; + } + return s; +} + struct Chess_piece { + Chess_piece operator*(const Chess_piece&) const {return {};} + Chess_piece operator/(const Chess_piece&) const {return {};} + Chess_piece operator+(const Chess_piece&) const {return {};} + Chess_piece operator-(const Chess_piece&) const {return {};} + + Chess_piece& operator*=(const Chess_piece&){return *this;} + Chess_piece& operator/=(const Chess_piece&){return *this;} + Chess_piece& operator+=(const Chess_piece&){return *this;} + Chess_piece& operator-=(const Chess_piece&){return *this;} + + Chess_piece operator*(int) const {return {};} + Chess_piece operator/(int) const {return {};} + Chess_piece operator+(int) const {return {};} + Chess_piece operator-(int) const {return {};} + + Chess_piece& operator*=(int){return *this;} + Chess_piece& operator/=(int){return *this;} + Chess_piece& operator+=(int){return *this;} + Chess_piece& operator-=(int){return *this;} }; +std::ostream& operator<<(std::ostream& s, const Chess_piece&) +{ + s << "Chess_piece"; + return s; +} + +template<typename M1, typename M2> +void test(M1& m1, M2& m2) +{ + std::cout << "m1=\n" << m1 << '\n'; + std::cout << "m2=\n" << m2 << '\n'; + + { + auto m3 = m1 * m2; + std::cout << "m1*m2\n" << m3 << '\n'; + } + + { + auto m3 = m1 / m2; + std::cout << "m1/m2\n" << m3 << '\n'; + } + + { + auto m3 = m1 + m1; + std::cout << "m1+m1\n" << m3 << '\n'; + } + + { + auto m3 = m1 - m1; + std::cout << "m1-m1\n" << m3 << '\n'; + } + + if constexpr (supports_modulo<M1, M1>) + { + auto m3 = m1 % m1; + std::cout << "m1%m1\n" << m3 << '\n'; + } + + { + auto m3 = m1 * 2; + std::cout << "m1*2\n" << m3 << '\n'; + } + + { + auto m3 = m1 / 2; + std::cout << "m1/2\n" << m3 << '\n'; + } + + { + auto m3 = m1 + 2; + std::cout << "m1+2\n" << m3 << '\n'; + } + + { + auto m3 = m1 - 2; + std::cout << "m1-2\n" << m3 << '\n'; + } + + if constexpr (supports_modulo<M1, int>) + { + auto m3 = m1 % 2; + std::cout << "m1%2\n" << m3 << '\n'; + } + + { + auto m3 = m1; + m3.Move({0,0}, {1,1}); + std::cout << "m3.Move (copy assign)\n" << m3 << '\n'; + } + + { + auto m3(m1); + auto r = m3.Row(0); + std::cout << "m3.Row (copy ctor)\n"; + for(auto i : r) std::cout << i << ' '; + std::cout << "\n\n"; + } + + { + auto m3(std::move(m1)); + auto c = m3.Column(0); + std::cout << "m3.Column (move ctor)\n"; + for(auto i : c) std::cout << i << ' '; + std::cout << "\n\n"; + } + + { + auto m3 = std::move(m2); + std::cout << "m3 (move assign)\n" << m3 << '\n'; + } +} + int main() { - Matrix<double, 3, 5> m1; - m1 = m1 + 2; - m1.m(2,2) = 42; - try + std::cout << "Imatrix:\n"; { - m1.m(6,6) = 0; + Imatrix m1(3,2); + m1.m(0,0) = 1; m1.m(1,0) = 2; m1.m(2,0) = 3; + m1.m(0,1) = 4; m1.m(1,1) = 5; m1.m(2,1) = 6; + + Imatrix m2(2,3); + m2.m(0,0) = 1; m2.m(1,0) = 2; + m2.m(0,1) = 3; m2.m(1,1) = 4; + m2.m(0,2) = 5; m2.m(1,2) = 6; + + test(m1, m2); } - catch(...) + + std::cout << "ImatrixNM<..,..>:\n"; { - // expected + ImatrixNM<3, 2> m1; + m1.m(0,0) = 1; m1.m(1,0) = 2; m1.m(2,0) = 3; + m1.m(0,1) = 4; m1.m(1,1) = 5; m1.m(2,1) = 6; + + ImatrixNM<2, 3> m2; + m2.m(0,0) = 1; m2.m(1,0) = 2; + m2.m(0,1) = 3; m2.m(1,1) = 4; + m2.m(0,2) = 5; m2.m(1,2) = 6; + + test(m1, m2); } - Matrix<double, 5, 2> m2; - auto m3 = m1 * m2; - Matrix<Chess_piece, 2, 2> c1; - auto c2 = c1 + 42; + std::cout << "Matrix<int, .., ..>:\n"; + { + Matrix<int, 3, 2> m1; + m1.m(0,0) = 1; m1.m(1,0) = 2; m1.m(2,0) = 3; + m1.m(0,1) = 4; m1.m(1,1) = 5; m1.m(2,1) = 6; + + Matrix<int, 2, 3> m2; + m2.m(0,0) = 1; m2.m(1,0) = 2; + m2.m(0,1) = 3; m2.m(1,1) = 4; + m2.m(0,2) = 5; m2.m(1,2) = 6; + + test(m1, m2); + } + + std::cout << "Matrix<double, .., ..>:\n"; + { + Matrix<double, 3, 2> m1; + m1.m(0,0) = 1; m1.m(1,0) = 2; m1.m(2,0) = 3; + m1.m(0,1) = 4; m1.m(1,1) = 5; m1.m(2,1) = 6; + + Matrix<double, 2, 3> m2; + m2.m(0,0) = 1; m2.m(1,0) = 2; + m2.m(0,1) = 3; m2.m(1,1) = 4; + m2.m(0,2) = 5; m2.m(1,2) = 6; + + test(m1, m2); + } + + std::cout << "Matrix<Chess_piece, .., ..>:\n"; + { + Matrix<Chess_piece, 3, 2> m1; + Matrix<Chess_piece, 2, 3> m2; + test(m1, m2); + } } |