From 6e100e19f556ccd15a933b48bc45bf9f88e74b95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A8=D0=B8=D1=88=D0=BE=D0=B2=20=D0=9C=D0=B0=D0=BA=D1=81?= =?UTF-8?q?=D0=B8=D0=BC=20=D0=94=D0=B5=D0=BD=D0=B8=D1=81=D0=BE=D0=B2=D0=B8?= =?UTF-8?q?=D1=87?= Date: Tue, 8 Sep 2020 16:05:22 +0300 Subject: [PATCH] doc correction --- libs/main/math/pimathmatrix.h | 853 ++++++++++++++++++------------- tests/math/testpimathmatrix.cpp | 30 +- tests/math/testpimathmatrixt.cpp | 35 +- 3 files changed, 530 insertions(+), 388 deletions(-) diff --git a/libs/main/math/pimathmatrix.h b/libs/main/math/pimathmatrix.h index 80f1eaab..9a767dd5 100644 --- a/libs/main/math/pimathmatrix.h +++ b/libs/main/math/pimathmatrix.h @@ -28,18 +28,35 @@ #include "pimathvector.h" #include "pimathcomplex.h" - +/** +* @brief Inline funtion of compare with zero different types +* +* @param v is input parameter of type T +* @return true if zero, false if not zero +*/ template inline bool _PIMathMatrixNullCompare(const T v) { static_assert(std::is_floating_point::value, "Type must be floating point"); return (piAbs(v) < T(1E-200)); } +/** +* @brief Inline funtion of compare with zero colmplexf type +* +* @param v is input parameter of type colmplexf +* @return true if zero, false if not zero +*/ template<> inline bool _PIMathMatrixNullCompare(const complexf v) { return (abs(v) < float(1E-200)); } +/** +* @brief Inline funtion of compare with zero complexd type +* +* @param v is input parameter of type colmplexd +* @return true if zero, false if not zero +*/ template<> inline bool _PIMathMatrixNullCompare(const complexd v) { return (abs(v) < double(1E-200)); @@ -68,8 +85,19 @@ class PIP_EXPORT PIMathMatrixT { static_assert(Rows > 0, "Row count must be > 0"); static_assert(Cols > 0, "Column count must be > 0"); public: + /** + * @brief Constructor that calls the private resize method + * + * @return identitied matrix of type PIMathMatrixT + */ PIMathMatrixT() { resize(Rows, Cols); } + /** + * @brief Constructor that calls the private resize method + * + * @param val is the PIVector with which the matrix is ​​filled + * @return identitied matrix of type PIMathMatrixT + */ PIMathMatrixT(const PIVector &val) { resize(Rows, Cols); int i = 0; @@ -77,10 +105,10 @@ public: } /** - * @brief Сreates a matrix whose main diagonal is filled with ones and the remaining elements are zeros - * - * @return identitied matrix of type PIMathMatrixT - */ + * @brief Сreates a matrix whose main diagonal is filled with ones and the remaining elements are zeros + * + * @return identity matrix of type PIMathMatrixT + */ static _CMatrix identity() { _CMatrix tm = _CMatrix(); PIMM_FOR_WB(r, c) tm.m[r][c] = (c == r ? Type(1) : Type(0)); @@ -88,11 +116,11 @@ public: } /** - * @brief Creates a matrix that is filled with elements - * - * @param v is a parameter the type and value of which is selected and later filled into the matrix - * @return filled matrix of type PIMathMatrixT - */ + * @brief Creates a matrix that is filled with elements + * + * @param v is a parameter the type and value of which is selected and later filled into the matrix + * @return filled matrix of type PIMathMatrixT + */ static _CMatrix filled(const Type &v) { _CMatrix tm; PIMM_FOR_WB(r, c) tm.m[r][c] = v; @@ -100,73 +128,73 @@ public: } /** - * @brief Rotation the matrix by an "angle". Works only with 2x2 matrix, - * else return default construction of PIMathMatrixT -* - * @param angle is the angle of rotation of the matrix - * @return rotated matrix - */ + * @brief Rotation the matrix by an "angle". Works only with 2x2 matrix, + * else return default construction of PIMathMatrixT + * + * @param angle is the angle of rotation of the matrix + * @return rotated matrix + */ static _CMatrix rotation(double angle) { return _CMatrix(); } /** - * @brief Rotation of the matrix by an "angle" along the X axis. Works only with 3x3 matrix, - * else return default construction of PIMathMatrixT - * - * @param angle is the angle of rotation of the matrix along the X axis - * @return rotated matrix - */ + * @brief Rotation of the matrix by an "angle" along the X axis. Works only with 3x3 matrix, + * else return default construction of PIMathMatrixT + * + * @param angle is the angle of rotation of the matrix along the X axis + * @return rotated matrix + */ static _CMatrix rotationX(double angle) { return _CMatrix(); } /** - * @brief Rotation of the matrix by an "angle" along the Y axis. Works only with 3x3 matrix, - * else return default construction of PIMathMatrixT - * - * @param angle is the angle of rotation of the matrix along the Y axis - * @return rotated matrix - */ + * @brief Rotation of the matrix by an "angle" along the Y axis. Works only with 3x3 matrix, + * else return default construction of PIMathMatrixT + * + * @param angle is the angle of rotation of the matrix along the Y axis + * @return rotated matrix + */ static _CMatrix rotationY(double angle) { return _CMatrix(); } /** - * @brief Rotation of the matrix by an "angle" along the Z axis. Works only with 3x3 matrix, - * else return default construction of PIMathMatrixT - * - * @param angle is the angle of rotation of the matrix along the Z axis - * @return rotated matrix - */ + * @brief Rotation of the matrix by an "angle" along the Z axis. Works only with 3x3 matrix, + * else return default construction of PIMathMatrixT + * + * @param angle is the angle of rotation of the matrix along the Z axis + * @return rotated matrix + */ static _CMatrix rotationZ(double angle) { return _CMatrix(); } /** - * @brief Scaling the matrix along the X axis by the value "factor". Works only with 3x3 and 2x2 matrix, - * else return default construction of PIMathMatrixT - * - * @param factor is the value of scaling by X axis - * @return rotated matrix - */ + * @brief Scaling the matrix along the X axis by the value "factor". Works only with 3x3 and 2x2 matrix, + * else return default construction of PIMathMatrixT + * + * @param factor is the value of scaling by X axis + * @return rotated matrix + */ static _CMatrix scaleX(double factor) { return _CMatrix(); } /** - * @brief Scaling the matrix along the Y axis by the value "factor". Works only with 3x3 and 2x2 matrix, - * else return default construction of PIMathMatrixT - * - * @param factor is the value of scaling by Y axis - * @return rotated matrix - */ + * @brief Scaling the matrix along the Y axis by the value "factor". Works only with 3x3 and 2x2 matrix, + * else return default construction of PIMathMatrixT + * + * @param factor is the value of scaling by Y axis + * @return rotated matrix + */ static _CMatrix scaleY(double factor) { return _CMatrix(); } /** - * @brief Scaling the matrix along the Z axis by the value "factor". Works only with 3x3 matrix, - * else return default construction of PIMathMatrixT - * - * @param factor is the value of scaling by Z axis - * @return rotated matrix - */ + * @brief Scaling the matrix along the Z axis by the value "factor". Works only with 3x3 matrix, + * else return default construction of PIMathMatrixT + * + * @param factor is the value of scaling by Z axis + * @return rotated matrix + */ static _CMatrix scaleZ(double factor) { return _CMatrix(); } /** - * @brief Method which returns number of columns in matrix - * - * @return type uint shows number of columns - */ + * @brief Method which returns number of columns in matrix + * + * @return type uint shows number of columns + */ uint cols() const { return Cols; } /** @@ -177,11 +205,12 @@ public: uint rows() const { return Rows; } /** - * @brief Method which returns the selected column in PIMathVectorT format - * - * @param index is the number of the selected column - * @return column in PIMathVectorT format - */ + * @brief Method which returns the selected column in PIMathVectorT format. + * If you enter an index out of the border of the matrix will be SEGFAULT + * + * @param index is the number of the selected column + * @return column in PIMathVectorT format + */ _CMCol col(uint index) { _CMCol tv; PIMM_FOR_R(i) tv[i] = m[i][index]; @@ -189,11 +218,12 @@ public: } /** - * @brief Method which returns the selected row in PIMathVectorT format - * - * @param index is the number of the selected row - * @return row in PIMathVectorT format - */ + * @brief Method which returns the selected row in PIMathVectorT format + * If you enter an index out of the border of the matrix will be SEGFAULT + * + * @param index is the number of the selected row + * @return row in PIMathVectorT format + */ _CMRow row(uint index) { _CMRow tv; PIMM_FOR_C(i) tv[i] = m[index][i]; @@ -201,36 +231,39 @@ public: } /** - * @brief Set the selected column in matrix - * - * @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 - */ + * @brief Set the selected column in matrix. + * If you enter an index out of the border of the matrix will be SEGFAULT + * + * @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) m[i][index] = v[i]; return *this; } /** - * @brief Set the selected row in matrix - * - * @param index is the number of the selected row - * @param v is a vector of the type _CMCol that needs to fill the row - * @return matrix type _CMatrix - */ + * @brief Set the selected row in matrix + * If you enter an index out of the border of the matrix will be SEGFAULT + * + * @param index is the number of the selected row + * @param v is a vector of the type _CMCol that needs to fill the row + * @return matrix type _CMatrix + */ _CMatrix &setRow(uint index, const _CMRow &v) { PIMM_FOR_C(i) m[index][i] = v[i]; return *this; } /** - * @brief Method which changes selected rows in a matrix - * - * @param r0 is the number of the first selected row - * @param r1 is the number of the second selected row - * @return matrix type _CMatrix - */ + * @brief Method which changes selected rows in a matrix. + * If you enter an index out of the border of the matrix will be SEGFAULT + * + * @param r0 is the number of the first selected row + * @param r1 is the number of the second selected row + * @return matrix type _CMatrix + */ _CMatrix &swapRows(uint r0, uint r1) { Type t; PIMM_FOR_C(i) { @@ -242,12 +275,13 @@ public: } /** - * @brief Method which changes selected columns in a matrix - * - * @param c0 is the number of the first selected column - * @param c1 is the number of the second selected column - * @return matrix type _CMatrix - */ + * @brief Method which changes selected columns in a matrix. + * If you enter an index out of the border of the matrix will be SEGFAULT + * + * @param c0 is the number of the first selected column + * @param c1 is the number of the second selected column + * @return matrix type _CMatrix + */ _CMatrix &swapCols(uint c0, uint c1) { Type t; PIMM_FOR_R(i) { @@ -259,140 +293,142 @@ public: } /** - * @brief Method which fills the matrix with selected value - * - * @param v is a parameter the type and value of which is selected and later filled into the matrix - * @return filled matrix type _CMatrix - */ + * @brief Method which fills the matrix with selected value + * + * @param v is a parameter the type and value of which is selected and later filled into the matrix + * @return filled matrix type _CMatrix + */ _CMatrix &fill(const Type &v) { PIMM_FOR_WB(r, c) m[r][c] = v; return *this; } /** - * @brief Method which checks if matrix is square - * - * @return true if matrix is square, else false - */ + * @brief Method which checks if matrix is square + * + * @return true if matrix is square, else false + */ bool isSquare() const { return cols() == rows(); } /** - * @brief Method which checks if main diagonal of matrix consists of ones and another elements are zeros - * - * @return true if matrix is identitied, else false - */ + * @brief Method which checks if main diagonal of matrix consists of ones and another elements are zeros + * + * @return true if matrix is identitied, else false + */ bool isIdentity() const { PIMM_FOR_WB(r, c) if ((c == r) ? m[r][c] != Type(1) : m[r][c] != Type(0)) return false; return true; } /** - * @brief Method which checks if every elements of matrix are zeros - * - * @return true if matrix is null, else false - */ + * @brief Method which checks if every elements of matrix are zeros + * + * @return true if matrix is null, else false + */ bool isNull() const { PIMM_FOR_WB(r, c) if (m[r][c] != Type(0)) return false; return true; } /** - * @brief Full access to elements reference by row "row" and col "col" - * - * @param row is a parameter that shows the row number of the matrix of the selected element - * @param col is a parameter that shows the column number of the matrix of the selected element - * @return reference to element of matrix by row "row" and col "col" - */ + * @brief Full access to elements reference by row "row" and col "col". + * If you enter an index out of the border of the matrix will be SEGFAULT + * + * @param row is a parameter that shows the row number of the matrix of the selected element + * @param col is a parameter that shows the column number of the matrix of the selected element + * @return reference to element of matrix by row "row" and col "col" + */ Type &at(uint row, uint col) { return m[row][col]; } /** - * @brief Full access to element by row "row" and col "col" - * - * @param row is a parameter that shows the row number of the matrix of the selected element - * @param col is a parameter that shows the column number of the matrix of the selected element - * @return element of matrix by row "row" and col "col" - */ + * @brief Full access to element by row "row" and col "col". + * If you enter an index out of the border of the matrix will be SEGFAULT + * + * @param row is a parameter that shows the row number of the matrix of the selected element + * @param col is a parameter that shows the column number of the matrix of the selected element + * @return element of matrix by row "row" and col "col" + */ Type at(uint row, uint col) const { return m[row][col]; } /** - * @brief Full access to the matrix row pointer - * - * @param row is a row of necessary matrix - * @return matrix row pointer - */ + * @brief Full access to the matrix row pointer. If you enter an index out of the border of the matrix will be SEGFAULT + * + * @param row is a row of necessary matrix + * @return matrix row pointer + */ Type *operator[](uint row) { return m[row]; } /** - * @brief Read-only access to the matrix row pointer - * - * @param row is a row of necessary matrix - * @return matrix row pointer - */ + * @brief Read-only access to the matrix row pointer. If you enter an index out of the border of the matrix will be SEGFAULT + * + * @param row is a row of necessary matrix + * @return matrix row pointer + */ const Type *operator[](uint row) const { return m[row]; } /** - * @brief Matrix assignment to matrix "sm" - * - * @param sm matrix for the assigment - * @return matrix equal with sm - */ + * @brief Matrix assignment to matrix "sm" + * + * @param sm matrix for the assigment + * @return matrix equal with sm + */ _CMatrix &operator=(const _CMatrix &sm) { memcpy(m, sm.m, sizeof(Type) * Cols * Rows); return *this; } /** - * @brief Compare with matrix "sm" - * - * @param sm matrix for the compare - * @return if matrices are equal true, else false - */ + * @brief Compare with matrix "sm" + * + * @param sm matrix for the compare + * @return if matrices are equal true, else false + */ bool operator==(const _CMatrix &sm) const { PIMM_FOR_WB(r, c) if (m[r][c] != sm.m[r][c]) return false; return true; } /** - * @brief Compare with matrix "sm" - * - * @param sm matrix for the compare - * @return if matrices are not equal true, else false - */ + * @brief Compare with matrix "sm" + * + * @param sm matrix for the compare + * @return if matrices are not equal true, else false + */ bool operator!=(const _CMatrix &sm) const { return !(*this == sm); } /** - * @brief Addition assignment with matrix "sm" - * - * @param sm matrix for the addition assigment - */ - void operator+=(const _CMatrix &sm) { PIMM_FOR_WB(r, c) m[r][c] += sm.m[r][c]; } + * @brief Addition assignment with matrix "sm" + * + * @param sm matrix for the addition assigment + */ + void operator+=(const _CMatrix &sm) { PIMM_FOR_WB(r, c) m[r][c] += sm.m[r][c]; } /** - * @brief Subtraction assignment with matrix "sm" - * - * @param sm matrix for the subtraction assigment - */ + * @brief Subtraction assignment with matrix "sm" + * + * @param sm matrix for the subtraction assigment + */ void operator-=(const _CMatrix &sm) { PIMM_FOR_WB(r, c) m[r][c] -= sm.m[r][c]; } /** - * @brief Multiplication assignment with value "v" - * - * @param v value for the multiplication assigment - */ + * @brief Multiplication assignment with value "v" + * + * @param v value for the multiplication assigment + */ void operator*=(const Type &v) { PIMM_FOR_WB(r, c) m[r][c] *= v; } /** - * @brief Division assignment with value "v" - * - * @param v value for the division assigment - */ + * @brief Division assignment with value "v" + * + * @param v value for the division assigment + */ void operator/=(const Type &v) { PIMM_FOR_WB(r, c) m[r][c] /= v; } /** - * @brief Matrix substraction - * - * @return the result of matrix substraction - */ + * @brief Matrix substraction + * + * @return the result of matrix substraction + */ _CMatrix operator-() const { _CMatrix tm; PIMM_FOR_WB(r, c) tm.m[r][c] = -m[r][c]; @@ -400,11 +436,11 @@ public: } /** - * @brief Matrix addition - * - * @param sm is matrix term - * @return the result of matrix addition - */ + * @brief Matrix addition + * + * @param sm is matrix term + * @return the result of matrix addition + */ _CMatrix operator+(const _CMatrix &sm) const { _CMatrix tm = _CMatrix(*this); PIMM_FOR_WB(r, c) tm.m[r][c] += sm.m[r][c]; @@ -412,11 +448,11 @@ public: } /** - * @brief Matrix substraction - * - * @param sm is matrix subtractor - * @return the result of matrix substraction - */ + * @brief Matrix substraction + * + * @param sm is matrix subtractor + * @return the result of matrix substraction + */ _CMatrix operator-(const _CMatrix &sm) const { _CMatrix tm = _CMatrix(*this); PIMM_FOR_WB(r, c) tm.m[r][c] -= sm.m[r][c]; @@ -424,11 +460,11 @@ public: } /** - * @brief Matrix multiplication - * - * @param v is value factor - * @return the result of matrix multiplication - */ + * @brief Matrix multiplication + * + * @param v is value factor + * @return the result of matrix multiplication + */ _CMatrix operator*(const Type &v) const { _CMatrix tm = _CMatrix(*this); PIMM_FOR_WB(r, c) tm.m[r][c] *= v; @@ -436,11 +472,11 @@ public: } /** - * @brief Matrix division - * - * @param v is value divider - * @return the result of matrix division - */ + * @brief Matrix division + * + * @param v is value divider + * @return the result of matrix division + */ _CMatrix operator/(const Type &v) const { _CMatrix tm = _CMatrix(*this); PIMM_FOR_WB(r, c) tm.m[r][c] /= v; @@ -448,10 +484,10 @@ public: } /** - * @brief Determinant of the matrix is ​​calculated - * - * @return matrix determinant - */ + * @brief Determinant of the matrix is ​​calculated + * + * @return matrix determinant + */ Type determinant(bool *ok = 0) const { _CMatrix m(*this); bool k; @@ -468,10 +504,10 @@ public: } /** - * @brief Transforming matrix to upper triangular - * - * @return transformed upper triangular matrix - */ + * @brief Transforming matrix to upper triangular + * + * @return copy of transformed upper triangular matrix + */ _CMatrix &toUpperTriangular(bool *ok = 0) { if (Cols != Rows) { if (ok != 0) *ok = false; @@ -510,10 +546,10 @@ public: } /** - * @brief Matrix inversion operation - * - * @return inverted matrix - */ + * @brief Matrix inversion operation + * + * @return copy of inverted matrix + */ _CMatrix &invert(bool *ok = 0) { static_assert(Cols == Rows, "Only square matrix invertable"); _CMatrix mtmp = _CMatrix::identity(), smat(*this); @@ -563,10 +599,10 @@ public: } /** - * @brief Matrix inversion operation - * - * @return inverted matrix - */ + * @brief Matrix inversion operation + * + * @return inverted matrix + */ _CMatrix inverted(bool *ok = 0) const { _CMatrix tm(*this); tm.invert(ok); @@ -574,10 +610,10 @@ public: } /** - * @brief Matrix transposition operation - * - * @return transposed matrix - */ + * @brief Matrix transposition operation + * + * @return transposed matrix + */ _CMatrixI transposed() const { _CMatrixI tm; PIMM_FOR_WB(r, c) tm[c][r] = m[r][c]; @@ -687,6 +723,13 @@ template inline std::ostream & operator <<(std::ostream & s, const PIMathMatrixT & m) {s << "{"; PIMM_FOR_I(r, c) s << m[r][c]; if (c < Cols - 1 || r < Rows - 1) s << ", ";} if (r < Rows - 1) s << std::endl << " ";} s << "}"; return s;} #endif +/** +* @brief Add matrix "m" at the end of matrix and return reference to matrix +* +* @param s PICout type +* @param m PIMathMatrixT type +* @return bitwise left PICout +*/ template inline PICout operator<<(PICout s, const PIMathMatrixT &m) { s << "{"; @@ -698,6 +741,13 @@ inline PICout operator<<(PICout s, const PIMathMatrixT &m) { } /// Multiply matrices {Rows0 x CR} on {CR x Cols1}, result is {Rows0 x Cols1} +/** +* @brief Multiplying matrices by each other. If you enter an index out of the border of the matrix will be SEGFAULT +* +* @param fm first matrix multiplier +* @param sm second matrix multiplier +* @return matrix that is the result of multiplication +*/ template inline PIMathMatrixT operator*(const PIMathMatrixT &fm, const PIMathMatrixT &sm) { @@ -715,6 +765,13 @@ inline PIMathMatrixT operator*(const PIMathMatrixT inline PIMathVectorT operator*(const PIMathMatrixT &fm, const PIMathVectorT &sv) { @@ -730,6 +787,13 @@ inline PIMathVectorT operator*(const PIMathMatrixT } /// Multiply vector {Rows} on matrix {Rows x Cols}, result is vector {Cols} +/** +* @brief Multiplying vector and matrix. If you enter an index out of the border of the matrix will be SEGFAULT +* +* @param sv first vector multiplier +* @param fm second matrix multiplier +* @return vector that is the result of multiplication +*/ template inline PIMathVectorT operator*(const PIMathVectorT &sv, const PIMathMatrixT &fm) { @@ -745,6 +809,13 @@ inline PIMathVectorT operator*(const PIMathVectorT &sv, } /// Multiply value(T) on matrix {Rows x Cols}, result is vector {Rows} +/** +* @brief Multiplying value of type Type and matrix +* +* @param x first multiplier of type Type +* @param fm second matrix multiplier +* @return matrix that is the result of multiplication +*/ template inline PIMathMatrixT operator*(const Type &x, const PIMathMatrixT &v) { return v * x; @@ -788,14 +859,33 @@ class PIP_EXPORT PIMathMatrix : public PIVector2D { typedef PIMathMatrix _CMatrix; typedef PIMathVector _CMCol; public: + /** + * @brief Constructor of class PIMathMatrix, which creates a matrix + * + * @param cols is number of matrix column uint type + * @param rows is number of matrix row uint type + * @param f is type of matrix elements + */ PIMathMatrix(const uint cols = 0, const uint rows = 0, const Type &f = Type()) { _V2D::resize(rows, cols, f); } + /** + * @brief Constructor of class PIMathMatrix, which creates a matrix + * + * @param cols is number of matrix column uint type + * @param rows is number of matrix row uint type + * @param val is PIVector of matrix elements + */ 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++]; } + /** + * @brief Constructor of class PIMathMatrix, which creates a matrix + * + * @param val is PIVector of PIVector, which creates matrix + */ PIMathMatrix(const PIVector > &val) { if (!val.isEmpty()) { _V2D::resize(val.size(), val[0].size()); @@ -803,6 +893,11 @@ public: } } + /** + * @brief Constructor of class PIMathMatrix, which creates a matrix + * + * @param val is PIVector2D, which creates matrix + */ PIMathMatrix(const PIVector2D &val) { if (!val.isEmpty()) { _V2D::resize(val.rows(), val.cols()); @@ -810,191 +905,194 @@ public: } } - /** - * @brief Creates a matrix whose main diagonal is filled with ones and the remaining elements are zeros - * - * @param cols is number of matrix column uint type - * @param rows is number of matrix row uint type - * @return identity matrix of type PIMathMatrix - */ + /** + * @brief Creates a matrix whose main diagonal is filled with ones and the remaining elements are zeros + * + * @param cols is number of matrix column uint type + * @param rows is number of matrix row uint type + * @return identity matrix of type PIMathMatrix + */ static _CMatrix identity(const uint cols, const uint rows) { _CMatrix tm(cols, rows); for (uint r = 0; r < rows; ++r) for (uint c = 0; c < cols; ++c) tm.element(r, c) = (c == r ? Type(1) : Type(0)); return tm; } - /** - * @brief Creates a matrix whose row equal to vector - * - * @param val is the vector type PIMathVector - * @return identity matrix by vector - */ + /** + * @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()); } /** - * @brief Creates a matrix whose column equal to vector - * - * @param val is the vector type PIMathVector - * @return identity matrix by vector - */ + * @brief Creates a column matrix of every element that is equal to every element of the vector + * + * @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()); } /** - * @brief Set the selected column in matrix - * - * @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 - */ + * @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 a SEGFAULT + * + * @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]; return *this; } /** - * @brief Set the selected row in matrix - * - * @param index is the number of the selected row - * @param v is a vector of the type _CMCol that needs to fill the row - * @return matrix type _CMatrix - */ + * @brief Set the selected row in matrix. If there are more elements of the vector than elements in the row of the matrix, + * or index larger than the number of rows otherwise there will be a SEGFAULT + * @param index is the number of the selected row + * @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]; return *this; } /** - * @brief Method which changes selected rows in a matrix - * - * @param r0 is the number of the first selected row - * @param r1 is the number of the second selected row - * @return matrix type _CMatrix - */ + * @brief Method which replace selected columns in a matrix. You cannot use an index larger than the number of columns, + * otherwise there will be a SEGFAULT + * + * @param r0 is the number of the first selected row + * @param r1 is the number of the second selected row + * @return matrix type _CMatrix + */ _CMatrix &swapCols(uint r0, uint r1) { PIMM_FOR_C(i) { piSwap(_V2D::element(i, r0), _V2D::element(i, r1)); } return *this; } - /** - * @brief Method which changes selected columns in a matrix - * - * @param c0 is the number of the first selected column - * @param c1 is the number of the second selected column - * @return matrix type _CMatrix - */ + /** + * @brief Method which replace selected rows in a matrix. You cannot use an index larger than the number of rows, + * otherwise there will be a SEGFAULT + * + * @param c0 is the number of the first selected row + * @param c1 is the number of the second selected row + * @return matrix type _CMatrix + */ _CMatrix &swapRows(uint c0, uint c1) { PIMM_FOR_R(i) { piSwap(_V2D::element(c0, i), _V2D::element(c1, i)); } return *this; } /** - * @brief Method which fills the matrix with selected value - * - * @param v is a parameter the type and value of which is selected and later filled into the matrix - * @return filled matrix type _CMatrix - */ + * @brief Method which fills the matrix with selected value + * + * @param v is a parameter the type and value of which is selected and later filled into the matrix + * @return filled matrix type _CMatrix + */ _CMatrix &fill(const Type &v) { PIMM_FOR_A(i) _V2D::mat[i] = v; return *this; } /** - * @brief Method which checks if matrix is square - * - * @return true if matrix is square, else false - */ + * @brief Method which checks if matrix is square + * + * @return true if matrix is square, else false + */ bool isSquare() const { return _V2D::cols_ == _V2D::rows_; } /** - * @brief Method which checks if main diagonal of matrix consists of ones and another elements are zeros - * - * @return true if matrix is identitied, else false - */ + * @brief Method which checks if main diagonal of matrix consists of ones and another elements are zeros + * + * @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; return true; } /** - * @brief Method which checks if every elements of matrix are zeros - * - * @return true if matrix is null, else false - */ + * @brief Method which checks if every elements of matrix are zeros + * + * @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; return true; } /** - * @brief Method which checks if matrix is empty - * - * @return true if matrix is valid, else false - */ + * @brief Method which checks if matrix is empty + * + * @return true if matrix is valid, else false + */ bool isValid() const { return !PIVector2D::isEmpty(); } /** - * @brief Matrix assignment to matrix "v" - * - * @param v matrix for the assigment - * @return matrix equal with v - */ + * @brief Matrix assignment to matrix "v" + * + * @param v matrix for the assigment + * @return matrix equal with v + */ _CMatrix &operator=(const PIVector > &v) { *this = _CMatrix(v); return *this; } /** - * @brief Compare with matrix "sm" - * - * @param sm matrix for the compare - * @return if matrices are equal true, else false - */ + * @brief Compare with matrix "sm" + * + * @param sm matrix for the compare + * @return if matrices are equal true, else false + */ bool operator==(const _CMatrix &sm) const { PIMM_FOR_A(i) if (_V2D::mat[i] != sm.mat[i]) return false; return true; } /** - * @brief Compare with matrix "sm" - * - * @param sm matrix for the compare - * @return if matrices are not equal true, else false - */ + * @brief Compare with matrix "sm" + * + * @param sm matrix for the compare + * @return if matrices are not equal true, else false + */ bool operator!=(const _CMatrix &sm) const { return !(*this == sm); } /** - * @brief Addition assignment with matrix "sm" - * - * @param sm matrix for the addition assigment - */ + * @brief Addition assignment with matrix "sm" + * + * @param sm matrix for the addition assigment + */ void operator+=(const _CMatrix &sm) { PIMM_FOR_A(i) _V2D::mat[i] += sm.mat[i]; } /** - * @brief Subtraction assignment with matrix "sm" - * - * @param sm matrix for the subtraction assigment - */ + * @brief Subtraction assignment with matrix "sm" + * + * @param sm matrix for the subtraction assigment + */ void operator-=(const _CMatrix &sm) { PIMM_FOR_A(i) _V2D::mat[i] -= sm.mat[i]; } - /** - * @brief Multiplication assignment with value "v" - * - * @param v value for the multiplication assigment - */ + /** + * @brief Multiplication assignment with value "v" + * + * @param v value for the multiplication assigment + */ void operator*=(const Type &v) { PIMM_FOR_A(i) _V2D::mat[i] *= v; } /** - * @brief Division assignment with value "v" - * - * @param v value for the division assigment - */ + * @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; } - /** - * @brief Matrix substraction - * - * @return the result of matrix substraction - */ + /** + * @brief Matrix substraction + * + * @return the result of matrix substraction + */ _CMatrix operator-() const { _CMatrix tm(*this); PIMM_FOR_A(i) tm.mat[i] = -_V2D::mat[i]; @@ -1002,11 +1100,11 @@ public: } /** - * @brief Matrix addition - * - * @param sm is matrix term - * @return the result of matrix addition - */ + * @brief Matrix addition + * + * @param sm is matrix term + * @return the result of matrix addition + */ _CMatrix operator+(const _CMatrix &sm) const { _CMatrix tm(*this); PIMM_FOR_A(i) tm.mat[i] += sm.mat[i]; @@ -1014,11 +1112,11 @@ public: } /** - * @brief Matrix subtraction - * - * @param sm is matrix subtractor - * @return the result of matrix subtraction - */ + * @brief Matrix subtraction + * + * @param sm is matrix subtractor + * @return the result of matrix subtraction + */ _CMatrix operator-(const _CMatrix &sm) const { _CMatrix tm(*this); PIMM_FOR_A(i) tm.mat[i] -= sm.mat[i]; @@ -1026,11 +1124,11 @@ public: } /** - * @brief Matrix multiplication - * - * @param v is value factor - * @return the result of matrix multiplication - */ + * @brief Matrix multiplication + * + * @param v is value factor + * @return the result of matrix multiplication + */ _CMatrix operator*(const Type &v) const { _CMatrix tm(*this); PIMM_FOR_A(i) tm.mat[i] *= v; @@ -1038,11 +1136,11 @@ public: } /** - * @brief Matrix division - * - * @param v is value divider - * @return the result of matrix division - */ + * @brief Matrix division + * + * @param v is value divider + * @return the result of matrix division + */ _CMatrix operator/(const Type &v) const { _CMatrix tm(*this); PIMM_FOR_A(i) tm.mat[i] /= v; @@ -1050,10 +1148,10 @@ public: } /** - * @brief Determinant of the matrix is ​​calculated - * - * @return matrix determinant - */ + * @brief Determinant of the matrix is ​​calculated. Works only with square matrix + * + * @return matrix determinant + */ Type determinant(bool *ok = 0) const { _CMatrix m(*this); bool k; @@ -1070,10 +1168,10 @@ public: } /** - * @brief Trace of the matrix is calculated - * - * @return matrix trace - */ + * @brief Trace of the matrix is calculated. Works only with square matrix + * + * @return matrix trace + */ Type trace(bool *ok = 0) const { Type ret = Type(0); if (!isSquare()) { @@ -1088,10 +1186,10 @@ public: } /** - * @brief Transforming matrix to upper triangular - * - * @return transformed upper triangular matrix - */ + * @brief Transforming matrix to upper triangular. Works only with square matrix + * + * @return copy of transformed upper triangular matrix + */ _CMatrix &toUpperTriangular(bool *ok = 0) { if (!isSquare()) { if (ok != 0) *ok = false; @@ -1130,10 +1228,10 @@ public: } /** - * @brief Matrix inversion operation - * - * @return inverted matrix - */ + * @brief Matrix inversion operation. Works only with square matrix + * + * @return copy of inverted matrix + */ _CMatrix &invert(bool *ok = 0, _CMCol *sv = 0) { if (!isSquare()) { if (ok != 0) *ok = false; @@ -1190,10 +1288,10 @@ public: } /** - * @brief Matrix inversion operation - * - * @return inverted matrix - */ + * @brief Matrix inversion operation + * + * @return inverted matrix + */ _CMatrix inverted(bool *ok = 0) const { _CMatrix tm(*this); tm.invert(ok); @@ -1201,10 +1299,10 @@ public: } /** - * @brief Matrix transposition operation - * - * @return transposed matrix - */ + * @brief Matrix transposition operation + * + * @return transposed matrix + */ _CMatrix transposed() const { _CMatrix tm(_V2D::rows_, _V2D::cols_); PIMM_FOR(c, r) tm.element(c, r) = _V2D::element(r, c); @@ -1218,6 +1316,13 @@ template inline std::ostream & operator <<(std::ostream & s, const PIMathMatrix & m) {s << "{"; for (uint r = 0; r < m.rows(); ++r) { for (uint c = 0; c < m.cols(); ++c) { s << m.element(r, c); if (c < m.cols() - 1 || r < m.rows() - 1) s << ", ";} if (r < m.rows() - 1) s << std::endl << " ";} s << "}"; return s;} #endif +/** +* @brief Add matrix "m" at the end of matrix and return reference to matrix +* +* @param s PICout type +* @param m PIMathMatrix type +* @return bitwise left PICout +*/ template inline PICout operator<<(PICout s, const PIMathMatrix &m) { s << "Matrix{"; @@ -1232,12 +1337,26 @@ inline PICout operator<<(PICout s, const PIMathMatrix &m) { return s; } +/** +* @brief Add matrix "m" at the end of matrix and return reference to matrix +* +* @param s PIByteArray type +* @param v PIMathMatrix type +* @return bitwise left PIByteArray +*/ template inline PIByteArray &operator<<(PIByteArray &s, const PIMathMatrix &v) { s << (const PIVector2D &) v; return s; } +/** +* @brief Add matrix "m" at the end of matrix and return reference to matrix +* +* @param s PIByteArray type +* @param v PIMathMatrix type +* @return bitwise right PIByteArray +*/ template inline PIByteArray &operator>>(PIByteArray &s, PIMathMatrix &v) { s >> (PIVector2D &) v; @@ -1246,6 +1365,13 @@ inline PIByteArray &operator>>(PIByteArray &s, PIMathMatrix &v) { /// Multiply matrices {CR x Rows0} on {Cols1 x CR}, result is {Cols1 x Rows0} +/** +* @brief Multiplying matrices by each other. If you enter an index out of the border of the matrix will be SEGFAULT +* +* @param fm first matrix multiplier +* @param sm second matrix multiplier +* @return matrix that is the result of multiplication +*/ template inline PIMathMatrix operator*(const PIMathMatrix &fm, const PIMathMatrix &sm) { @@ -1265,6 +1391,13 @@ inline PIMathMatrix operator*(const PIMathMatrix &fm, } /// Multiply matrix {Cols x Rows} on vector {Cols}, result is vector {Rows} +/** +* @brief Multiplying matrix and vector. If you enter an index out of the border of the matrix will be SEGFAULT +* +* @param fm first matrix multiplier +* @param sv second vector multiplier +* @return vector that is the result of multiplication +*/ template inline PIMathVector operator*(const PIMathMatrix &fm, const PIMathVector &sv) { @@ -1283,6 +1416,13 @@ inline PIMathVector operator*(const PIMathMatrix &fm, /// Multiply vector {Rows} on matrix {Rows x Cols}, result is vector {Cols} +/** +* @brief Multiplying vector and matrix. If you enter an index out of the border of the matrix will be SEGFAULT +* +* @param sv first vector multiplier +* @param fm second matrix multiplier +* @return vector that is the result of multiplication +*/ template inline PIMathVector operator*(const PIMathVector &sv, const PIMathMatrix &fm) { @@ -1299,6 +1439,13 @@ inline PIMathVector operator*(const PIMathVector &sv, } /// Multiply value(T) on matrix {Rows x Cols}, result is vector {Rows} +/** +* @brief Multiplying value of type Type and matrix +* +* @param x first multiplier of type Type +* @param fm second matrix multiplier +* @return matrix that is the result of multiplication +*/ template inline PIMathMatrix operator*(const Type &x, const PIMathMatrix &v) { return v * x; @@ -1307,6 +1454,12 @@ inline PIMathMatrix operator*(const Type &x, const PIMathMatrix &v) typedef PIMathMatrix PIMathMatrixi; typedef PIMathMatrix PIMathMatrixd; +/** +* @brief Searching hermitian matrix +* +* @param m conjugate transpose matrix +* @return result of the hermitian +*/ template PIMathMatrix > hermitian(const PIMathMatrix > &m) { PIMathMatrix > ret(m); diff --git a/tests/math/testpimathmatrix.cpp b/tests/math/testpimathmatrix.cpp index 4eeccd0c..09992b89 100644 --- a/tests/math/testpimathmatrix.cpp +++ b/tests/math/testpimathmatrix.cpp @@ -34,24 +34,22 @@ bool cmpMatrixWithValue(PIMathMatrix matrix, double val) TEST(PIMathMatrix_Test, identity) { - PIMathMatrix origMatr; - PIMathMatrix matrix; - int i; - bool b; - matrix = origMatr.identity(3, 3); - for(i = 0; i < 3; i++) - { - if(matrix[i][i] == 1.0) - { - b = true; - } - else - { - b = false; - break; + auto matrix = PIMathMatrix::identity(3, 3); + for(int i = 0; i < 3; i++){ + if(matrix[i][i] != 1.0){ + ASSERT_TRUE(false); } } - ASSERT_TRUE(b); + for(int i = 0; i < 3; i++){ + for(int j = 0; j < 3; j++){ + if(i != j){ + if(matrix[i][j] != 0.0){ + ASSERT_TRUE(false); + } + } + } + } + ASSERT_TRUE(true); } TEST(PIMathMatrix_Test, matrixRow) diff --git a/tests/math/testpimathmatrixt.cpp b/tests/math/testpimathmatrixt.cpp index 2d68d131..0542332a 100644 --- a/tests/math/testpimathmatrixt.cpp +++ b/tests/math/testpimathmatrixt.cpp @@ -38,31 +38,22 @@ bool cmpMatrixWithValue(PIMathMatrixT matrix, double val) TEST(PIMathMatrixT_Test, identity) { - PIMathMatrixT matr; - PIMathMatrixT matrix; - double d; - double i = 1.0; - bool a; - bool output; - matrix = matr.identity(); - d = matrix.determinant(); - uint j; - for(j = 0; j < cols; j++) - { - if(matrix.at(i, i) == 1.0) a = true; - else - { - a = false; - break; + auto matrix = PIMathMatrixT::identity(); + for(int i = 0; i < 3; i++){ + if(matrix[i][i] != 1.0){ + ASSERT_TRUE(false); } } - if((i == d) && (a == true)){ - output = true; + for(int i = 0; i < 3; i++){ + for(int j = 0; j < 3; j++){ + if(i != j){ + if(matrix[i][j] != 0.0){ + ASSERT_TRUE(false); + } + } + } } - else{ - output = false; - } - ASSERT_TRUE(output); + ASSERT_TRUE(true); } TEST(PIMathMatrixT_Test, at)