From f5652efc32315eae8f8324f0d8246b3e1f8910d0 Mon Sep 17 00:00:00 2001 From: andrey Date: Thu, 22 Oct 2020 16:48:46 +0300 Subject: [PATCH] refactoring PIMathMatrix --- libs/main/math/pimathmatrix.h | 92 +++++++++++++++++------------------ 1 file changed, 45 insertions(+), 47 deletions(-) diff --git a/libs/main/math/pimathmatrix.h b/libs/main/math/pimathmatrix.h index 05f7a551..b495d90d 100644 --- a/libs/main/math/pimathmatrix.h +++ b/libs/main/math/pimathmatrix.h @@ -301,7 +301,9 @@ public: * * @param v value for the multiplication assigment */ - void operator*=(const Type &v) {PIMM_FOR m[r][c] *= v;} + void operator*=(const Type &v) { + PIMM_FOR m[r][c] *= v; + } /** * @brief Division assignment with value "v" @@ -654,11 +656,10 @@ class PIMathMatrix; /// Matrix -#define PIMM_FOR(c, r) for (uint c = 0; c < _V2D::cols_; ++c) for (uint r = 0; r < _V2D::rows_; ++r) -#define PIMM_FOR_I(c, r) for (uint r = 0; r < _V2D::rows_; ++r) for (uint c = 0; c < _V2D::cols_; ++c) -#define PIMM_FOR_A(v) for (uint v = 0; v < _V2D::mat.size(); ++v) -#define PIMM_FOR_C(v) for (uint v = 0; v < _V2D::cols_; ++v) -#define PIMM_FOR_R(v) for (uint v = 0; v < _V2D::rows_; ++v) +#define PIMM_FOR for (uint r = 0; r < _V2D::rows_; ++r) for (uint c = 0; c < _V2D::cols_; ++c) +#define PIMM_FOR_A for (uint i = 0; i < _V2D::mat.size(); ++i) +#define PIMM_FOR_C for (uint i = 0; i < _V2D::cols_; ++i) +#define PIMM_FOR_R for (uint i = 0; i < _V2D::rows_; ++i) //! \brief A class that works with matrix operations, the input data of which is the data type of the matrix //! @tparam There are can be basic C++ language data and different classes where the arithmetic operators(=, +=, -=, *=, /=, ==, !=, +, -, *, /) @@ -667,7 +668,6 @@ template class PIP_EXPORT PIMathMatrix : public PIVector2D { typedef PIVector2D _V2D; typedef PIMathMatrix _CMatrix; - typedef PIMathVector _CMCol; public: /** * @brief Constructor of class PIMathMatrix, which creates a matrix @@ -688,7 +688,7 @@ public: PIMathMatrix(const uint cols, const uint rows, const PIVector &val) { _V2D::resize(rows, cols); int i = 0; - PIMM_FOR_I(c, r) _V2D::element(r, c) = val[i++]; + PIMM_FOR _V2D::element(r, c) = val[i++]; } /** @@ -699,7 +699,11 @@ public: PIMathMatrix(const PIVector > &val) { if (!val.isEmpty()) { _V2D::resize(val.size(), val[0].size()); - PIMM_FOR_I(c, r) _V2D::element(r, c) = val[r][c]; + for (uint r = 0; r < _V2D::rows_; ++r) { + assert(val[r].size() == _V2D::cols_); + for (uint c = 0; c < _V2D::cols_; ++c) + _V2D::element(r, c) = val[r][c]; + } } } @@ -711,7 +715,7 @@ public: PIMathMatrix(const PIVector2D &val) { if (!val.isEmpty()) { _V2D::resize(val.rows(), val.cols()); - PIMM_FOR_I(c, r) _V2D::element(r, c) = val.element(r, c); + PIMM_FOR _V2D::element(r, c) = val.element(r, c); } } @@ -728,26 +732,13 @@ public: return tm; } - /** - * @brief Creates a matrix filled by zeros - * - * @param cols is number of matrix column uint type - * @param rows is number of matrix row uint type - * @return zero matrix(cols,rows) - */ - static _CMatrix zero(const uint cols, const uint rows) { - _CMatrix tm(cols, rows); - tm.fill(Type(0)); - return tm; - } - /** * @brief Creates a row matrix of every element that is equal to every element of the vector * * @param val is the vector type PIMathVector * @return row matrix of every element that is equal to every element of the vector */ - static _CMatrix matrixRow(const PIMathVector &val) { return _CMatrix(val.size(), 1, val.toVector()); } + static _CMatrix matrixRow(const PIMathVector &val) {return _CMatrix(val.size(), 1, val.toVector());} /** * @brief Creates a column matrix of every element that is equal to every element of the vector @@ -755,18 +746,19 @@ public: * @param val is the vector type PIMathVector * @return column matrix of every element that is equal to every element of the vector */ - static _CMatrix matrixCol(const PIMathVector &val) { return _CMatrix(1, val.size(), val.toVector()); } + static _CMatrix matrixCol(const PIMathVector &val) {return _CMatrix(1, val.size(), val.toVector());} /** * @brief Set the selected column in matrix. If there are more elements of the vector than elements in the column of the matrix - * or index larger than the number of columns otherwise there will be "undefined behavior" + * or index larger than the number of columns otherwise there will be "undefined behavior" * * @param index is the number of the selected column * @param v is a vector of the type _CMCol that needs to fill the column * @return matrix type _CMatrix */ - _CMatrix &setCol(uint index, const _CMCol &v) { - PIMM_FOR_R(i) _V2D::element(i, index) = v[i]; + _CMatrix &setCol(uint index, const PIMathVector &v) { + assert(_V2D::rows == v.size()); + PIMM_FOR_R _V2D::element(i, index) = v[i]; return *this; } @@ -777,8 +769,9 @@ public: * @param v is a vector of the type _CMCol that needs to fill the row * @return matrix type _CMatrix */ - _CMatrix &setRow(uint index, const _CMCol &v) { - PIMM_FOR_C(i) _V2D::element(index, i) = v[i]; + _CMatrix &setRow(uint index, const PIMathVector &v) { + assert(_V2D::cols == v.size()); + PIMM_FOR_C _V2D::element(index, i) = v[i]; return *this; } @@ -791,7 +784,7 @@ public: * @return matrix type _CMatrix */ _CMatrix &swapCols(uint r0, uint r1) { - PIMM_FOR_C(i) { piSwap(_V2D::element(i, r0), _V2D::element(i, r1)); } + PIMM_FOR_C piSwap(_V2D::element(i, r0), _V2D::element(i, r1)); return *this; } @@ -804,7 +797,7 @@ public: * @return matrix type _CMatrix */ _CMatrix &swapRows(uint c0, uint c1) { - PIMM_FOR_R(i) { piSwap(_V2D::element(c0, i), _V2D::element(c1, i)); } + PIMM_FOR_R piSwap(_V2D::element(c0, i), _V2D::element(c1, i)); return *this; } @@ -815,7 +808,7 @@ public: * @return filled matrix type _CMatrix */ _CMatrix &fill(const Type &v) { - PIMM_FOR_A(i) _V2D::mat[i] = v; + PIMM_FOR_A _V2D::mat[i] = v; return *this; } @@ -832,7 +825,7 @@ public: * @return true if matrix is identity, else false */ bool isIdentity() const { - PIMM_FOR(c, r) if ((c == r) ? _V2D::element(r, c) != Type(1) : _V2D::element(r, c) != Type(0))return false; + PIMM_FOR if ((c == r) ? _V2D::element(r, c) != Type(1) : _V2D::element(r, c) != Type(0))return false; return true; } @@ -842,7 +835,7 @@ public: * @return true if matrix elements equal to zero, else false */ bool isNull() const { - PIMM_FOR_A(i) if (_V2D::mat[i] != Type(0)) return false; + PIMM_FOR_A if (_V2D::mat[i] != Type(0)) return false; return true; } @@ -861,7 +854,7 @@ public: void operator+=(const _CMatrix &sm) { assert(_V2D::rows() == sm.rows()); assert(_V2D::cols() == sm.cols()); - PIMM_FOR_A(i) _V2D::mat[i] += sm.mat[i]; + PIMM_FOR_A _V2D::mat[i] += sm.mat[i]; } /** @@ -872,7 +865,7 @@ public: void operator-=(const _CMatrix &sm) { assert(_V2D::rows() == sm.rows()); assert(_V2D::cols() == sm.cols()); - PIMM_FOR_A(i) _V2D::mat[i] -= sm.mat[i]; + PIMM_FOR_A _V2D::mat[i] -= sm.mat[i]; } /** @@ -880,14 +873,19 @@ public: * * @param v value for the multiplication assigment */ - void operator*=(const Type &v) { PIMM_FOR_A(i) _V2D::mat[i] *= v; } + void operator*=(const Type &v) { + PIMM_FOR_A _V2D::mat[i] *= v; + } /** * @brief Division assignment with value "v" * * @param v value for the division assigment */ - void operator/=(const Type &v) { PIMM_FOR_A(i) _V2D::mat[i] /= v; } + void operator/=(const Type &v) { + assert(piAbs(v) > PIMATHVECTOR_ZERO_CMP); + PIMM_FOR_A _V2D::mat[i] /= v; + } /** * @brief Matrix substraction @@ -896,7 +894,7 @@ public: */ _CMatrix operator-() const { _CMatrix tm(*this); - PIMM_FOR_A(i) tm.mat[i] = -_V2D::mat[i]; + PIMM_FOR_A tm.mat[i] = -_V2D::mat[i]; return tm; } @@ -910,7 +908,7 @@ public: _CMatrix tm(*this); assert(tm.rows() == sm.rows()); assert(tm.cols() == sm.cols()); - PIMM_FOR_A(i) tm.mat[i] += sm.mat[i]; + PIMM_FOR_A tm.mat[i] += sm.mat[i]; return tm; } @@ -924,7 +922,7 @@ public: _CMatrix tm(*this); assert(tm.rows() == sm.rows()); assert(tm.cols() == sm.cols()); - PIMM_FOR_A(i) tm.mat[i] -= sm.mat[i]; + PIMM_FOR_A tm.mat[i] -= sm.mat[i]; return tm; } @@ -936,7 +934,7 @@ public: */ _CMatrix operator*(const Type &v) const { _CMatrix tm(*this); - PIMM_FOR_A(i) tm.mat[i] *= v; + PIMM_FOR_A tm.mat[i] *= v; return tm; } @@ -947,8 +945,9 @@ public: * @return the result of matrix division */ _CMatrix operator/(const Type &v) const { + assert(piAbs(v) > PIMATHVECTOR_ZERO_CMP); _CMatrix tm(*this); - PIMM_FOR_A(i) tm.mat[i] /= v; + PIMM_FOR_A tm.mat[i] /= v; return tm; } @@ -1042,7 +1041,7 @@ public: * @param sv is a vector multiplier * @return copy of inverted matrix */ - _CMatrix &invert(bool *ok = 0, _CMCol *sv = 0) { + _CMatrix &invert(bool *ok = 0, PIMathVector *sv = 0) { if (!isSquare()) { if (ok != 0) *ok = false; return *this; @@ -1116,7 +1115,7 @@ public: */ _CMatrix transposed() const { _CMatrix tm(_V2D::rows_, _V2D::cols_); - PIMM_FOR(c, r) tm.element(c, r) = _V2D::element(r, c); + PIMM_FOR tm.element(c, r) = _V2D::element(r, c); return tm; } }; @@ -1276,7 +1275,6 @@ PIMathMatrix > hermitian(const PIMathMatrix > &m) { } #undef PIMM_FOR -#undef PIMM_FOR_I #undef PIMM_FOR_A #undef PIMM_FOR_C #undef PIMM_FOR_R