From 47ce1062ec972beb34069dd12e5b96824ad70b94 Mon Sep 17 00:00:00 2001 From: Nao Pross Date: Sat, 23 Feb 2019 13:50:31 +0100 Subject: Start specializations of basic_matrix --- include/mmmatrix.hpp | 101 ++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 95 insertions(+), 6 deletions(-) diff --git a/include/mmmatrix.hpp b/include/mmmatrix.hpp index 7cb9359..d18f3f1 100644 --- a/include/mmmatrix.hpp +++ b/include/mmmatrix.hpp @@ -12,16 +12,27 @@ #pragma once #include +#include namespace mm { template class basic_matrix; + + template + class matrix; + + template + class square_matrix; + + // template + // class diag_matrix; + template - using row_vec = basic_matrix; + class row_vec; template - using col_vec = basic_matrix; + class col_vec; } template @@ -38,22 +49,45 @@ public: template basic_matrix(const basic_matrix& other); + basic_matrix(const T (& values)[Rows][Cols]); + basic_matrix(T (&& values)[Rows][Cols]); + // access data T& at(std::size_t row, std::size_t col); + auto&& operator[](std::size_t index); void swap_rows(std::size_t x, std::size_t y); void swap_cols(std::size_t x, std::size_t y); // mathematical operations basic_matrix transposed(); - inline basic_matrix t() { return transposed(); } + inline basic_matrix trd() { return transposed(); } // bool is_invertible(); // bool invert(); // basic_matrix inverse(); - inline constexpr bool is_square() { - return (Rows == Cols); + + /// downcast to square matrix + inline constexpr bool is_square() { return (Rows == Cols); } + inline constexpr square_matrix to_square() { + static_assert(is_square()); + return static_cast>(*this); + } + + + /// downcast to row_vector + inline constexpr bool is_row_vec() { return (Cols == 1); } + inline constexpr row_vec to_row_vec() { + static_assert(is_row_vec()); + return static_cast>(*this); + } + + /// downcast to col_vector + inline constexpr bool is_col_vec() { return (Rows == 1); } + inline constexpr col_vec to_col_vec() { + static_assert(is_col_vec()); + return static_cast>(*this); } private: @@ -89,6 +123,16 @@ mm::basic_matrix::basic_matrix(const mm::basic_matrix +mm::basic_matrix::basic_matrix(const T (& values)[Rows][Cols]) { + std::memcpy(&data, &values, sizeof(data)); +} + +template +mm::basic_matrix::basic_matrix(T (&& values)[Rows][Cols]) { + data = values; +} + /* member functions */ @@ -100,6 +144,16 @@ T& mm::basic_matrix::at(std::size_t row, std::size_t col) { return data[row][col]; } +template +auto&& mm::basic_matrix::operator[](std::size_t index) { + if constexpr (is_row_vec()) + return data[0][index]; + else if constexpr (is_col_vec()) + return data[index][0]; + + return row_vec(std::move(data[index])); +} + template void mm::basic_matrix::swap_rows(std::size_t x, std::size_t y) { if (x == y) @@ -124,7 +178,7 @@ mm::basic_matrix mm::basic_matrix::transposed() { for (int row = 0; row < M; row++) for (int col = 0; col < N; col++) - result.at(row, col) = at(col, row); + result[row][col] = this[col][row]; return result; } @@ -203,3 +257,38 @@ std::ostream& operator<<(std::ostream& os, const mm::basic_matrix return os; } + + +/* square matrix specializaiton */ + +template +class mm::square_matrix : public mm::basic_matrix { +public: + /// in place transpose + void transpose(); + inline void tr() { transpose(); } + + /// in place inverse + void invert(); +}; + + +template +void mm::square_matrix::transpose() { + for (int row = 0; row < N; row++) + for (int col = 0; col < row; col++) + std::swap(this->at(row, col), this->at(col, row)); +} + + +/* row vector specialization */ +template +class mm::row_vec : public mm::basic_matrix { +public: +}; + +/* column vector specialization */ +template +class mm::col_vec : public mm::basic_matrix { +public: +}; -- cgit v1.2.1