@@ -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 < typename T >
inline bool _PIMathMatrixNullCompare ( const T v ) {
static_assert ( std : : is_floating_point < T > : : 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 < complexf > ( 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 < complexd > ( 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 < Type > & val ) {
resize ( Rows , Cols ) ;
int i = 0 ;
@@ -79,7 +107,7 @@ public:
/**
* @brief С reates a matrix whose main diagonal is filled with ones and the remaining elements are zeros
*
* @return identitied matrix of type PIMathMatrixT
* @return identity matrix of type PIMathMatrixT
*/
static _CMatrix identity ( ) {
_CMatrix tm = _CMatrix ( ) ;
@@ -177,7 +205,8 @@ public:
uint rows ( ) const { return Rows ; }
/**
* @brief Method which returns the selected 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
@@ -190,6 +219,7 @@ public:
/**
* @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
@@ -201,7 +231,8 @@ public:
}
/**
* @brief Set the selected column in matrix
* @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
@@ -214,6 +245,7 @@ public:
/**
* @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
@@ -225,7 +257,8 @@ public:
}
/**
* @brief Method which changes selected rows in a matrix
* @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
@@ -242,7 +275,8 @@ public:
}
/**
* @brief Method which changes selected columns in a matrix
* @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
@@ -297,7 +331,8 @@ public:
}
/**
* @brief Full access to elements reference 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
@@ -306,7 +341,8 @@ public:
Type & at ( uint row , uint col ) { return m [ row ] [ col ] ; }
/**
* @brief Full access to element 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
@@ -315,7 +351,7 @@ public:
Type at ( uint row , uint col ) const { return m [ row ] [ col ] ; }
/**
* @brief Full access to the 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
@@ -323,7 +359,7 @@ public:
Type * operator [ ] ( uint row ) { return m [ row ] ; }
/**
* @brief Read-only access to the 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
@@ -470,7 +506,7 @@ public:
/**
* @brief Transforming matrix to upper triangular
*
* @return transformed upper triangular matrix
* @return copy of transformed upper triangular matrix
*/
_CMatrix & toUpperTriangular ( bool * ok = 0 ) {
if ( Cols ! = Rows ) {
@@ -512,7 +548,7 @@ public:
/**
* @brief Matrix inversion operation
*
* @return inverted matrix
* @return copy of inverted matrix
*/
_CMatrix & invert ( bool * ok = 0 ) {
static_assert ( Cols = = Rows , " Only square matrix invertable " ) ;
@@ -687,6 +723,13 @@ template<uint Rows, uint Cols, typename Type>
inline std : : ostream & operator < < ( std : : ostream & s , const PIMathMatrixT < Rows , Cols , Type > & 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 < uint Rows , uint Cols , typename Type >
inline PICout operator < < ( PICout s , const PIMathMatrixT < Rows , Cols , Type > & m ) {
s < < " { " ;
@@ -698,6 +741,13 @@ inline PICout operator<<(PICout s, const PIMathMatrixT<Rows, Cols, Type> &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 < uint CR , uint Rows0 , uint Cols1 , typename Type >
inline PIMathMatrixT < Rows0 , Cols1 , Type > operator * ( const PIMathMatrixT < Rows0 , CR , Type > & fm ,
const PIMathMatrixT < CR , Cols1 , Type > & sm ) {
@@ -715,6 +765,13 @@ inline PIMathMatrixT<Rows0, Cols1, Type> operator*(const PIMathMatrixT<Rows0, CR
}
/// Multiply matrix {Rows x Cols} 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 < uint Cols , uint Rows , typename Type >
inline PIMathVectorT < Rows , Type > operator * ( const PIMathMatrixT < Rows , Cols , Type > & fm ,
const PIMathVectorT < Cols , Type > & sv ) {
@@ -730,6 +787,13 @@ inline PIMathVectorT<Rows, Type> operator*(const PIMathMatrixT<Rows, Cols, Type>
}
/// 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 < uint Cols , uint Rows , typename Type >
inline PIMathVectorT < Cols , Type > operator * ( const PIMathVectorT < Rows , Type > & sv ,
const PIMathMatrixT < Rows , Cols , Type > & fm ) {
@@ -745,6 +809,13 @@ inline PIMathVectorT<Cols, Type> operator*(const PIMathVectorT<Rows, Type> &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 < uint Cols , uint Rows , typename Type >
inline PIMathMatrixT < Rows , Cols , Type > operator * ( const Type & x , const PIMathMatrixT < Rows , Cols , Type > & v ) {
return v * x ;
@@ -788,14 +859,33 @@ class PIP_EXPORT PIMathMatrix : public PIVector2D<Type> {
typedef PIMathMatrix < Type > _CMatrix ;
typedef PIMathVector < Type > _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<Type> of matrix elements
*/
PIMathMatrix ( const uint cols , const uint rows , const PIVector < Type > & 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<Type> of PIVector, which creates matrix
*/
PIMathMatrix ( const PIVector < PIVector < Type > > & 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<Type>, which creates matrix
*/
PIMathMatrix ( const PIVector2D < Type > & val ) {
if ( ! val . isEmpty ( ) ) {
_V2D : : resize ( val . rows ( ) , val . cols ( ) ) ;
@@ -824,23 +919,24 @@ public:
}
/**
* @brief Creates a matrix whose row equal to 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 identity matrix by vector
* @return row matrix of every element that is equal to every element of the vector
*/
static _CMatrix matrixRow ( const PIMathVector < Type > & val ) { return _CMatrix ( val . size ( ) , 1 , val . toVector ( ) ) ; }
/**
* @brief Creates a matrix whose column equal to 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 identity matrix by vector
* @return column matrix of every element that is equal to every element of the vector
*/
static _CMatrix matrixCol ( const PIMathVector < Type > & val ) { return _CMatrix ( 1 , val . size ( ) , val . toVector ( ) ) ; }
/**
* @brief Set the selected column in matrix
* @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
@@ -852,8 +948,8 @@ public:
}
/**
* @brief Set the selected row in matrix
*
* @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
@@ -864,7 +960,8 @@ public:
}
/**
* @brief Method which changes selected row s in a matrix
* @brief Method which replace selected column s 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
@@ -876,10 +973,11 @@ public:
}
/**
* @brief Method which changes selected column s in a matrix
* @brief Method which replace selected row s 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 column
* @param c1 is the number of the second selected column
* @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 ) {
@@ -908,7 +1006,7 @@ public:
/**
* @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
* @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 ;
@@ -918,7 +1016,7 @@ public:
/**
* @brief Method which checks if every elements of matrix are zeros
*
* @return true if matrix is null , else false
* @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 ;
@@ -1050,7 +1148,7 @@ public:
}
/**
* @brief Determinant of the matrix is calculated
* @brief Determinant of the matrix is calculated. Works only with square matrix
*
* @return matrix determinant
*/
@@ -1070,7 +1168,7 @@ public:
}
/**
* @brief Trace of the matrix is calculated
* @brief Trace of the matrix is calculated. Works only with square matrix
*
* @return matrix trace
*/
@@ -1088,9 +1186,9 @@ public:
}
/**
* @brief Transforming matrix to upper triangular
* @brief Transforming matrix to upper triangular. Works only with square matrix
*
* @return transformed upper triangular matrix
* @return copy of transformed upper triangular matrix
*/
_CMatrix & toUpperTriangular ( bool * ok = 0 ) {
if ( ! isSquare ( ) ) {
@@ -1130,9 +1228,9 @@ public:
}
/**
* @brief Matrix inversion operation
* @brief Matrix inversion operation. Works only with square matrix
*
* @return inverted matrix
* @return copy of inverted matrix
*/
_CMatrix & invert ( bool * ok = 0 , _CMCol * sv = 0 ) {
if ( ! isSquare ( ) ) {
@@ -1218,6 +1316,13 @@ template<typename Type>
inline std : : ostream & operator < < ( std : : ostream & s , const PIMathMatrix < Type > & 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 < typename Type >
inline PICout operator < < ( PICout s , const PIMathMatrix < Type > & m ) {
s < < " Matrix{ " ;
@@ -1232,12 +1337,26 @@ inline PICout operator<<(PICout s, const PIMathMatrix<Type> &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 < typename Type >
inline PIByteArray & operator < < ( PIByteArray & s , const PIMathMatrix < Type > & v ) {
s < < ( const PIVector2D < Type > & ) 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 < typename Type >
inline PIByteArray & operator > > ( PIByteArray & s , PIMathMatrix < Type > & v ) {
s > > ( PIVector2D < Type > & ) v ;
@@ -1246,6 +1365,13 @@ inline PIByteArray &operator>>(PIByteArray &s, PIMathMatrix<Type> &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 < typename Type >
inline PIMathMatrix < Type > operator * ( const PIMathMatrix < Type > & fm ,
const PIMathMatrix < Type > & sm ) {
@@ -1265,6 +1391,13 @@ inline PIMathMatrix<Type> operator*(const PIMathMatrix<Type> &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 < typename Type >
inline PIMathVector < Type > operator * ( const PIMathMatrix < Type > & fm ,
const PIMathVector < Type > & sv ) {
@@ -1283,6 +1416,13 @@ inline PIMathVector<Type> operator*(const PIMathMatrix<Type> &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 < typename Type >
inline PIMathVector < Type > operator * ( const PIMathVector < Type > & sv ,
const PIMathMatrix < Type > & fm ) {
@@ -1299,6 +1439,13 @@ inline PIMathVector<Type> operator*(const PIMathVector<Type> &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 < typename Type >
inline PIMathMatrix < Type > operator * ( const Type & x , const PIMathMatrix < Type > & v ) {
return v * x ;
@@ -1307,6 +1454,12 @@ inline PIMathMatrix<Type> operator*(const Type &x, const PIMathMatrix<Type> &v)
typedef PIMathMatrix < int > PIMathMatrixi ;
typedef PIMathMatrix < double > PIMathMatrixd ;
/**
* @brief Searching hermitian matrix
*
* @param m conjugate transpose matrix
* @return result of the hermitian
*/
template < typename T >
PIMathMatrix < complex < T > > hermitian ( const PIMathMatrix < complex < T > > & m ) {
PIMathMatrix < complex < T > > ret ( m ) ;