Fix code formatting & grammar mistakes
This commit is contained in:
@@ -29,7 +29,6 @@
|
|||||||
#include "pimathcomplex.h"
|
#include "pimathcomplex.h"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
inline bool _PIMathMatrixNullCompare(const T v) {
|
inline bool _PIMathMatrixNullCompare(const T v) {
|
||||||
static_assert(std::is_floating_point<T>::value, "Type must be floating point");
|
static_assert(std::is_floating_point<T>::value, "Type must be floating point");
|
||||||
@@ -37,11 +36,12 @@ inline bool _PIMathMatrixNullCompare(const T v) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template<>
|
template<>
|
||||||
inline bool _PIMathMatrixNullCompare<complexf >(const complexf v) {
|
inline bool _PIMathMatrixNullCompare<complexf>(const complexf v) {
|
||||||
return (abs(v) < float(1E-200));
|
return (abs(v) < float(1E-200));
|
||||||
}
|
}
|
||||||
|
|
||||||
template<>
|
template<>
|
||||||
inline bool _PIMathMatrixNullCompare<complexd >(const complexd v) {
|
inline bool _PIMathMatrixNullCompare<complexd>(const complexd v) {
|
||||||
return (abs(v) < double(1E-200));
|
return (abs(v) < double(1E-200));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -56,6 +56,7 @@ inline bool _PIMathMatrixNullCompare<complexd >(const complexd v) {
|
|||||||
#define PIMM_FOR_R(v) for (uint v = 0; v < Rows; ++v)
|
#define PIMM_FOR_R(v) for (uint v = 0; v < Rows; ++v)
|
||||||
|
|
||||||
#pragma pack(push, 1)
|
#pragma pack(push, 1)
|
||||||
|
|
||||||
//! \brief A class that works with square matrix operations, the input data of which are columns, rows and the data type of the matrix
|
//! \brief A class that works with square matrix operations, the input data of which are columns, rows and the data type of the matrix
|
||||||
template<uint Rows, uint Cols = Rows, typename Type = double>
|
template<uint Rows, uint Cols = Rows, typename Type = double>
|
||||||
class PIP_EXPORT PIMathMatrixT {
|
class PIP_EXPORT PIMathMatrixT {
|
||||||
@@ -67,313 +68,391 @@ class PIP_EXPORT PIMathMatrixT {
|
|||||||
static_assert(Rows > 0, "Row count must be > 0");
|
static_assert(Rows > 0, "Row count must be > 0");
|
||||||
static_assert(Cols > 0, "Column count must be > 0");
|
static_assert(Cols > 0, "Column count must be > 0");
|
||||||
public:
|
public:
|
||||||
PIMathMatrixT() {resize(Rows, Cols);}
|
PIMathMatrixT() { resize(Rows, Cols); }
|
||||||
PIMathMatrixT(const PIVector<Type> & val) {resize(Rows, Cols); int i = 0; PIMM_FOR_I_WB(r, c) m[r][c] = val[i++];}
|
|
||||||
|
|
||||||
/**
|
PIMathMatrixT(const PIVector<Type> &val) {
|
||||||
* @brief Сreates a matrix whose main diagonal is filled with ones and the remaining elements are zeros
|
resize(Rows, Cols);
|
||||||
*
|
int i = 0;
|
||||||
* @return identitied matrix of type PIMathMatrixT
|
PIMM_FOR_I_WB(r, c) m[r][c] = val[i++];
|
||||||
*/
|
}
|
||||||
static _CMatrix identity() {_CMatrix tm = _CMatrix(); PIMM_FOR_WB(r, c) tm.m[r][c] = (c == r ? Type(1) : Type(0)); return tm;}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Creates a matrix that is filled with elements
|
* @brief Сreates a matrix whose main diagonal is filled with ones and the remaining elements are zeros
|
||||||
*
|
*
|
||||||
* @param v is a parameter the type and value of which is selected and later filled into the matrix
|
* @return identitied matrix of type PIMathMatrixT
|
||||||
* @return filled matrix of type PIMathMatrixT
|
*/
|
||||||
*/
|
static _CMatrix identity() {
|
||||||
static _CMatrix filled(const Type & v) {_CMatrix tm; PIMM_FOR_WB(r, c) tm.m[r][c] = v; return tm;}
|
_CMatrix tm = _CMatrix();
|
||||||
|
PIMM_FOR_WB(r, c) tm.m[r][c] = (c == r ? Type(1) : Type(0));
|
||||||
|
return tm;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Rotation the matrix by an "angle". Works only with 2x2 matrix,
|
* @brief Creates a matrix that is filled with elements
|
||||||
* else return default construction of PIMathMatrixT
|
*
|
||||||
|
* @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;
|
||||||
|
return tm;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @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
|
||||||
|
*/
|
||||||
|
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
|
||||||
|
*/
|
||||||
|
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
|
||||||
|
*/
|
||||||
|
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
|
||||||
|
*/
|
||||||
|
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
|
||||||
|
*/
|
||||||
|
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
|
||||||
|
*/
|
||||||
|
static _CMatrix scaleZ(double factor) { return _CMatrix(); }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Method which returns number of columns in matrix
|
||||||
|
*
|
||||||
|
* @return type uint shows number of columns
|
||||||
|
*/
|
||||||
|
uint cols() const { return Cols; }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Method which returns number of rows in matrix
|
||||||
*
|
*
|
||||||
* @param angle is the angle of rotation of the matrix
|
* @return type uint shows number of rows
|
||||||
* @return rotated matrix
|
*/
|
||||||
*/
|
uint rows() const { return Rows; }
|
||||||
static _CMatrix rotation(double angle) {return _CMatrix();}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Rotation of the matrix by an "angle" along the X axis. Works only with 3x3 matrix,
|
* @brief Method which returns the selected column in PIMathVectorT format
|
||||||
* else return default construction of PIMathMatrixT
|
*
|
||||||
*
|
* @param index is the number of the selected column
|
||||||
* @param angle is the angle of rotation of the matrix along the X axis
|
* @return column in PIMathVectorT format
|
||||||
* @return rotated matrix
|
*/
|
||||||
*/
|
_CMCol col(uint index) {
|
||||||
static _CMatrix rotationX(double angle) {return _CMatrix();}
|
_CMCol tv;
|
||||||
|
PIMM_FOR_R(i) tv[i] = m[i][index];
|
||||||
|
return tv;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Rotation of the matrix by an "angle" along the Y axis. Works only with 3x3 matrix,
|
* @brief Method which returns the selected row in PIMathVectorT format
|
||||||
* else return default construction of PIMathMatrixT
|
*
|
||||||
*
|
* @param index is the number of the selected row
|
||||||
* @param angle is the angle of rotation of the matrix along the Y axis
|
* @return row in PIMathVectorT format
|
||||||
* @return rotated matrix
|
*/
|
||||||
*/
|
_CMRow row(uint index) {
|
||||||
static _CMatrix rotationY(double angle) {return _CMatrix();}
|
_CMRow tv;
|
||||||
|
PIMM_FOR_C(i) tv[i] = m[index][i];
|
||||||
|
return tv;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Rotation of the matrix by an "angle" along the Z axis. Works only with 3x3 matrix,
|
* @brief Set the selected column in matrix
|
||||||
* else return default construction of PIMathMatrixT
|
*
|
||||||
*
|
* @param index is the number of the selected column
|
||||||
* @param angle is the angle of rotation of the matrix along the Z axis
|
* @param v is a vector of the type _CMCol that needs to fill the column
|
||||||
* @return rotated matrix
|
* @return matrix type _CMatrix
|
||||||
*/
|
*/
|
||||||
static _CMatrix rotationZ(double angle) {return _CMatrix();}
|
_CMatrix &setCol(uint index, const _CMCol &v) {
|
||||||
|
PIMM_FOR_R(i) m[i][index] = v[i];
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Scaling the matrix along the X axis by the value "factor". Works only with 3x3 and 2x2 matrix,
|
* @brief Set the selected row in matrix
|
||||||
* else return default construction of PIMathMatrixT
|
*
|
||||||
*
|
* @param index is the number of the selected row
|
||||||
* @param factor is the value of scaling by X axis
|
* @param v is a vector of the type _CMCol that needs to fill the row
|
||||||
* @return rotated matrix
|
* @return matrix type _CMatrix
|
||||||
*/
|
*/
|
||||||
static _CMatrix scaleX(double factor) {return _CMatrix();}
|
_CMatrix &setRow(uint index, const _CMRow &v) {
|
||||||
|
PIMM_FOR_C(i) m[index][i] = v[i];
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Scaling the matrix along the Y axis by the value "factor". Works only with 3x3 and 2x2 matrix,
|
* @brief Method which changes selected rows in a matrix
|
||||||
* else return default construction of PIMathMatrixT
|
*
|
||||||
*
|
* @param r0 is the number of the first selected row
|
||||||
* @param factor is the value of scaling by Y axis
|
* @param r1 is the number of the second selected row
|
||||||
* @return rotated matrix
|
* @return matrix type _CMatrix
|
||||||
*/
|
*/
|
||||||
static _CMatrix scaleY(double factor) {return _CMatrix();}
|
_CMatrix &swapRows(uint r0, uint r1) {
|
||||||
|
Type t;
|
||||||
|
PIMM_FOR_C(i) {
|
||||||
|
t = m[r0][i];
|
||||||
|
m[r0][i] = m[r1][i];
|
||||||
|
m[r1][i] = t;
|
||||||
|
}
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Scaling the matrix along the Z axis by the value "factor". Works only with 3x3 matrix,
|
* @brief Method which changes selected columns in a matrix
|
||||||
* else return default construction of PIMathMatrixT
|
*
|
||||||
*
|
* @param c0 is the number of the first selected column
|
||||||
* @param factor is the value of scaling by Z axis
|
* @param c1 is the number of the second selected column
|
||||||
* @return rotated matrix
|
* @return matrix type _CMatrix
|
||||||
*/
|
*/
|
||||||
static _CMatrix scaleZ(double factor) {return _CMatrix();}
|
_CMatrix &swapCols(uint c0, uint c1) {
|
||||||
|
Type t;
|
||||||
|
PIMM_FOR_R(i) {
|
||||||
|
t = m[i][c0];
|
||||||
|
m[i][c0] = m[i][c1];
|
||||||
|
m[i][c1] = t;
|
||||||
|
}
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Method which returns number of columns in matrix
|
* @brief Method which fills the matrix with selected value
|
||||||
*
|
*
|
||||||
* @return type uint shows number of columns
|
* @param v is a parameter the type and value of which is selected and later filled into the matrix
|
||||||
*/
|
* @return filled matrix type _CMatrix
|
||||||
uint cols() const {return Cols;}
|
*/
|
||||||
|
_CMatrix &fill(const Type &v) {
|
||||||
|
PIMM_FOR_WB(r, c) m[r][c] = v;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Method which returns number of rows in matrix
|
* @brief Method which checks if matrix is square
|
||||||
*
|
*
|
||||||
* @return type uint shows number of rows
|
* @return true if matrix is square, else false
|
||||||
*/
|
*/
|
||||||
uint rows() const {return Rows;}
|
bool isSquare() const { return cols() == rows(); }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Method which returns the selected column in PIMathVectorT format
|
* @brief Method which checks if main diagonal of matrix consists of ones and another elements are zeros
|
||||||
*
|
*
|
||||||
* @param index is the number of the selected column
|
* @return true if matrix is identitied, else false
|
||||||
* @return column in PIMathVectorT format
|
*/
|
||||||
*/
|
bool isIdentity() const {
|
||||||
_CMCol col(uint index) {_CMCol tv; PIMM_FOR_R(i) tv[i] = m[i][index]; return tv;}
|
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 returns the selected row in PIMathVectorT format
|
* @brief Method which checks if every elements of matrix are zeros
|
||||||
*
|
*
|
||||||
* @param index is the number of the selected row
|
* @return true if matrix is null, else false
|
||||||
* @return row in PIMathVectorT format
|
*/
|
||||||
*/
|
bool isNull() const {
|
||||||
_CMRow row(uint index) {_CMRow tv; PIMM_FOR_C(i) tv[i] = m[index][i]; return tv;}
|
PIMM_FOR_WB(r, c) if (m[r][c] != Type(0)) return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Set the selected column in matrix
|
* @brief Full access to elements reference by row "row" and col "col"
|
||||||
*
|
*
|
||||||
* @param index is the number of the selected column
|
* @param row is a parameter that shows the row number of the matrix of the selected element
|
||||||
* @param v is a vector of the type _CMCol that needs to fill the column
|
* @param col is a parameter that shows the column number of the matrix of the selected element
|
||||||
* @return matrix type _CMatrix
|
* @return reference to element of matrix by row "row" and col "col"
|
||||||
*/
|
*/
|
||||||
_CMatrix & setCol(uint index, const _CMCol & v) {PIMM_FOR_R(i) m[i][index] = v[i]; return *this;}
|
Type &at(uint row, uint col) { return m[row][col]; }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Set the selected row in matrix
|
* @brief Full access to element by row "row" and col "col"
|
||||||
*
|
*
|
||||||
* @param index is the number of the selected row
|
* @param row is a parameter that shows the row number of the matrix of the selected element
|
||||||
* @param v is a vector of the type _CMCol that needs to fill the row
|
* @param col is a parameter that shows the column number of the matrix of the selected element
|
||||||
* @return matrix type _CMatrix
|
* @return element of matrix by row "row" and col "col"
|
||||||
*/
|
*/
|
||||||
_CMatrix & setRow(uint index, const _CMRow & v) {PIMM_FOR_C(i) m[index][i] = v[i]; return *this;}
|
Type at(uint row, uint col) const { return m[row][col]; }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Method which changes selected rows in a matrix
|
* @brief Full access to the matrix row pointer
|
||||||
*
|
*
|
||||||
* @param r0 is the number of the first selected row
|
* @param row is a row of necessary matrix
|
||||||
* @param r1 is the number of the second selected row
|
* @return matrix row pointer
|
||||||
* @return matrix type _CMatrix
|
*/
|
||||||
*/
|
Type *operator[](uint row) { return m[row]; }
|
||||||
_CMatrix & swapRows(uint r0, uint r1) {Type t; PIMM_FOR_C(i) {t = m[r0][i]; m[r0][i] = m[r1][i]; m[r1][i] = t;} return *this;}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Method which changes selected columns in a matrix
|
* @brief Read-only access to the matrix row pointer
|
||||||
*
|
*
|
||||||
* @param c0 is the number of the first selected column
|
* @param row is a row of necessary matrix
|
||||||
* @param c1 is the number of the second selected column
|
* @return matrix row pointer
|
||||||
* @return matrix type _CMatrix
|
*/
|
||||||
*/
|
const Type *operator[](uint row) const { return m[row]; }
|
||||||
_CMatrix & swapCols(uint c0, uint c1) {Type t; PIMM_FOR_R(i) {t = m[i][c0]; m[i][c0] = m[i][c1]; m[i][c1] = t;} return *this;}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Method which fills the matrix with selected value
|
* @brief Matrix assignment to matrix "sm"
|
||||||
*
|
*
|
||||||
* @param v is a parameter the type and value of which is selected and later filled into the matrix
|
* @param sm matrix for the assigment
|
||||||
* @return filled matrix type _CMatrix
|
* @return matrix equal with sm
|
||||||
*/
|
*/
|
||||||
_CMatrix & fill(const Type & v) {PIMM_FOR_WB(r, c) m[r][c] = v; return *this;}
|
_CMatrix &operator=(const _CMatrix &sm) {
|
||||||
|
memcpy(m, sm.m, sizeof(Type) * Cols * Rows);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Method which checks if matrix is square
|
* @brief Compare with matrix "sm"
|
||||||
*
|
*
|
||||||
* @return true if matrix is square, else false
|
* @param sm matrix for the compare
|
||||||
*/
|
* @return if matrices are equal true, else false
|
||||||
bool isSquare() const {return cols() == rows();}
|
*/
|
||||||
|
bool operator==(const _CMatrix &sm) const {
|
||||||
|
PIMM_FOR_WB(r, c) if (m[r][c] != sm.m[r][c]) return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Method which checks if main diagonal of matrix consists of ones and another elements are zeros
|
* @brief Compare with matrix "sm"
|
||||||
*
|
*
|
||||||
* @return true if matrix is identitied, else false
|
* @param sm matrix for the compare
|
||||||
*/
|
* @return if matrices are not equal true, 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;}
|
*/
|
||||||
|
bool operator!=(const _CMatrix &sm) const { return !(*this == sm); }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Method which checks if every elements of matrix are zeros
|
* @brief Addition assignment with matrix "sm"
|
||||||
*
|
*
|
||||||
* @return true if matrix is null, else false
|
* @param sm matrix for the addition assigment
|
||||||
*/
|
*/
|
||||||
bool isNull() const {PIMM_FOR_WB(r, c) if (m[r][c] != Type(0)) return false; return true;}
|
void operator+=(const _CMatrix &sm) { PIMM_FOR_WB(r, c) m[r][c] += sm.m[r][c]; }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Full access to elements reference by row "row" and col "col"
|
* @brief Subtraction assignment with matrix "sm"
|
||||||
*
|
*
|
||||||
* @param row is a parameter that shows the row number of the matrix of the selected element
|
* @param sm matrix for the subtraction assigment
|
||||||
* @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"
|
void operator-=(const _CMatrix &sm) { PIMM_FOR_WB(r, c) m[r][c] -= sm.m[r][c]; }
|
||||||
*/
|
|
||||||
Type & at(uint row, uint col) {return m[row][col];}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Full access to element by row "row" and col "col"
|
* @brief Multiplication assignment with value "v"
|
||||||
*
|
*
|
||||||
* @param row is a parameter that shows the row number of the matrix of the selected element
|
* @param v value for the multiplication assigment
|
||||||
* @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"
|
void operator*=(const Type &v) { PIMM_FOR_WB(r, c) m[r][c] *= v; }
|
||||||
*/
|
|
||||||
Type at(uint row, uint col) const {return m[row][col];}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Full access to the matrix row pointer
|
* @brief Division assignment with value "v"
|
||||||
*
|
*
|
||||||
* @param row is a row of necessary matrix
|
* @param v value for the division assigment
|
||||||
* @return matrix row pointer
|
*/
|
||||||
*/
|
void operator/=(const Type &v) { PIMM_FOR_WB(r, c) m[r][c] /= v; }
|
||||||
Type * operator [](uint row) {return m[row];}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Read-only access to the matrix row pointer
|
* @brief Matrix substraction
|
||||||
*
|
*
|
||||||
* @param row is a row of necessary matrix
|
* @return the result of matrix substraction
|
||||||
* @return matrix row pointer
|
*/
|
||||||
*/
|
_CMatrix operator-() const {
|
||||||
const Type * operator [](uint row) const {return m[row];}
|
_CMatrix tm;
|
||||||
|
PIMM_FOR_WB(r, c) tm.m[r][c] = -m[r][c];
|
||||||
|
return tm;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Matrix assignment to matrix "sm"
|
* @brief Matrix addition
|
||||||
*
|
*
|
||||||
* @param sm matrix for the assigment
|
* @param sm is matrix term
|
||||||
* @return matrix equal with sm
|
* @return the result of matrix addition
|
||||||
*/
|
*/
|
||||||
_CMatrix & operator =(const _CMatrix & sm) {memcpy(m, sm.m, sizeof(Type) * Cols * Rows); return *this;}
|
_CMatrix operator+(const _CMatrix &sm) const {
|
||||||
|
_CMatrix tm = _CMatrix(*this);
|
||||||
|
PIMM_FOR_WB(r, c) tm.m[r][c] += sm.m[r][c];
|
||||||
|
return tm;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Compare with matrix "sm"
|
* @brief Matrix substraction
|
||||||
*
|
*
|
||||||
* @param sm matrix for the compare
|
* @param sm is matrix subtractor
|
||||||
* @return if matrices are equal true, else false
|
* @return the result of matrix substraction
|
||||||
*/
|
*/
|
||||||
bool operator ==(const _CMatrix & sm) const {PIMM_FOR_WB(r, c) if (m[r][c] != sm.m[r][c]) return false; return true;}
|
_CMatrix operator-(const _CMatrix &sm) const {
|
||||||
|
_CMatrix tm = _CMatrix(*this);
|
||||||
|
PIMM_FOR_WB(r, c) tm.m[r][c] -= sm.m[r][c];
|
||||||
|
return tm;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Compare with matrix "sm"
|
* @brief Matrix multiplication
|
||||||
*
|
*
|
||||||
* @param sm matrix for the compare
|
* @param v is value factor
|
||||||
* @return if matrices are not equal true, else false
|
* @return the result of matrix multiplication
|
||||||
*/
|
*/
|
||||||
bool operator !=(const _CMatrix & sm) const {return !(*this == sm);}
|
_CMatrix operator*(const Type &v) const {
|
||||||
|
_CMatrix tm = _CMatrix(*this);
|
||||||
|
PIMM_FOR_WB(r, c) tm.m[r][c] *= v;
|
||||||
|
return tm;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Addition assignment with matrix "sm"
|
* @brief Matrix division
|
||||||
*
|
*
|
||||||
* @param sm matrix for the addition assigment
|
* @param v is value divider
|
||||||
*/
|
* @return the result of matrix division
|
||||||
void operator +=(const _CMatrix & sm) {PIMM_FOR_WB(r, c) m[r][c] += sm.m[r][c];}
|
*/
|
||||||
|
_CMatrix operator/(const Type &v) const {
|
||||||
|
_CMatrix tm = _CMatrix(*this);
|
||||||
|
PIMM_FOR_WB(r, c) tm.m[r][c] /= v;
|
||||||
|
return tm;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Subtraction assignment with matrix "sm"
|
* @brief Determinant of the matrix is calculated
|
||||||
*
|
*
|
||||||
* @param sm matrix for the subtraction assigment
|
* @return matrix determinant
|
||||||
*/
|
*/
|
||||||
void operator -=(const _CMatrix & sm) {PIMM_FOR_WB(r, c) m[r][c] -= sm.m[r][c];}
|
Type determinant(bool *ok = 0) const {
|
||||||
|
|
||||||
/**
|
|
||||||
* @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
|
|
||||||
*/
|
|
||||||
void operator /=(const Type & v) {PIMM_FOR_WB(r, c) m[r][c] /= v;}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @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]; return tm;}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @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]; return tm;}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @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]; return tm;}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @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; return tm;}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @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; return tm;}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Determinant of the matrix is calculated
|
|
||||||
*
|
|
||||||
* @return matrix determinant
|
|
||||||
*/
|
|
||||||
Type determinant(bool * ok = 0) const {
|
|
||||||
_CMatrix m(*this);
|
_CMatrix m(*this);
|
||||||
bool k;
|
bool k;
|
||||||
Type ret = Type(0);
|
Type ret = Type(0);
|
||||||
@@ -388,12 +467,12 @@ public:
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Transforming matrix to upper triangular
|
* @brief Transforming matrix to upper triangular
|
||||||
*
|
*
|
||||||
* @return transformed upper triangular matrix
|
* @return transformed upper triangular matrix
|
||||||
*/
|
*/
|
||||||
_CMatrix & toUpperTriangular(bool * ok = 0) {
|
_CMatrix &toUpperTriangular(bool *ok = 0) {
|
||||||
if (Cols != Rows) {
|
if (Cols != Rows) {
|
||||||
if (ok != 0) *ok = false;
|
if (ok != 0) *ok = false;
|
||||||
return *this;
|
return *this;
|
||||||
@@ -419,7 +498,7 @@ public:
|
|||||||
for (uint k = i; k < Cols; ++k) smat.m[k][j] -= mul * smat.m[k][i];
|
for (uint k = i; k < Cols; ++k) smat.m[k][j] -= mul * smat.m[k][i];
|
||||||
}
|
}
|
||||||
if (i < Cols - 1) {
|
if (i < Cols - 1) {
|
||||||
if (fabs(smat.m[i+1][i+1]) < Type(1E-200)) {
|
if (fabs(smat.m[i + 1][i + 1]) < Type(1E-200)) {
|
||||||
if (ok != 0) *ok = false;
|
if (ok != 0) *ok = false;
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
@@ -430,12 +509,12 @@ public:
|
|||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Matrix inversion operation
|
* @brief Matrix inversion operation
|
||||||
*
|
*
|
||||||
* @return inverted matrix
|
* @return inverted matrix
|
||||||
*/
|
*/
|
||||||
_CMatrix & invert(bool * ok = 0) {
|
_CMatrix &invert(bool *ok = 0) {
|
||||||
static_assert(Cols == Rows, "Only square matrix invertable");
|
static_assert(Cols == Rows, "Only square matrix invertable");
|
||||||
_CMatrix mtmp = _CMatrix::identity(), smat(*this);
|
_CMatrix mtmp = _CMatrix::identity(), smat(*this);
|
||||||
bool ndet;
|
bool ndet;
|
||||||
@@ -462,7 +541,7 @@ public:
|
|||||||
for (uint k = 0; k < Cols; ++k) mtmp.m[k][j] -= mul * mtmp.m[k][i];
|
for (uint k = 0; k < Cols; ++k) mtmp.m[k][j] -= mul * mtmp.m[k][i];
|
||||||
}
|
}
|
||||||
if (i < Cols - 1) {
|
if (i < Cols - 1) {
|
||||||
if (fabs(smat.m[i+1][i+1]) < Type(1E-200)) {
|
if (fabs(smat.m[i + 1][i + 1]) < Type(1E-200)) {
|
||||||
if (ok != 0) *ok = false;
|
if (ok != 0) *ok = false;
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
@@ -483,39 +562,125 @@ public:
|
|||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Matrix inversion operation
|
* @brief Matrix inversion operation
|
||||||
*
|
*
|
||||||
* @return inverted matrix
|
* @return inverted matrix
|
||||||
*/
|
*/
|
||||||
_CMatrix inverted(bool * ok = 0) const {_CMatrix tm(*this); tm.invert(ok); return tm;}
|
_CMatrix inverted(bool *ok = 0) const {
|
||||||
|
_CMatrix tm(*this);
|
||||||
|
tm.invert(ok);
|
||||||
|
return tm;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Matrix transposition operation
|
* @brief Matrix transposition operation
|
||||||
*
|
*
|
||||||
* @return transposed matrix
|
* @return transposed matrix
|
||||||
*/
|
*/
|
||||||
_CMatrixI transposed() const {_CMatrixI tm; PIMM_FOR_WB(r, c) tm[c][r] = m[r][c]; return tm;}
|
_CMatrixI transposed() const {
|
||||||
|
_CMatrixI tm;
|
||||||
|
PIMM_FOR_WB(r, c) tm[c][r] = m[r][c];
|
||||||
|
return tm;
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void resize(uint rows_, uint cols_, const Type & new_value = Type()) {r_ = rows_; c_ = cols_; PIMM_FOR_WB(r, c) m[r][c] = new_value;}
|
void resize(uint rows_, uint cols_, const Type &new_value = Type()) {
|
||||||
|
r_ = rows_;
|
||||||
|
c_ = cols_;
|
||||||
|
PIMM_FOR_WB(r, c) m[r][c] = new_value;
|
||||||
|
}
|
||||||
|
|
||||||
int c_, r_;
|
int c_, r_;
|
||||||
Type m[Rows][Cols];
|
Type m[Rows][Cols];
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#pragma pack(pop)
|
#pragma pack(pop)
|
||||||
|
|
||||||
|
|
||||||
template<> inline PIMathMatrixT<2u, 2u> PIMathMatrixT<2u, 2u>::rotation(double angle) {double c = cos(angle), s = sin(angle); PIMathMatrixT<2u, 2u> tm; tm[0][0] = tm[1][1] = c; tm[0][1] = -s; tm[1][0] = s; return tm;}
|
template<>
|
||||||
template<> inline PIMathMatrixT<2u, 2u> PIMathMatrixT<2u, 2u>::scaleX(double factor) {PIMathMatrixT<2u, 2u> tm; tm[0][0] = factor; tm[1][1] = 1.; return tm;}
|
inline PIMathMatrixT<2u, 2u> PIMathMatrixT<2u, 2u>::rotation(double angle) {
|
||||||
template<> inline PIMathMatrixT<2u, 2u> PIMathMatrixT<2u, 2u>::scaleY(double factor) {PIMathMatrixT<2u, 2u> tm; tm[0][0] = 1.; tm[1][1] = factor; return tm;}
|
double c = cos(angle), s = sin(angle);
|
||||||
|
PIMathMatrixT<2u, 2u> tm;
|
||||||
|
tm[0][0] = tm[1][1] = c;
|
||||||
|
tm[0][1] = -s;
|
||||||
|
tm[1][0] = s;
|
||||||
|
return tm;
|
||||||
|
}
|
||||||
|
|
||||||
template<> inline PIMathMatrixT<3u, 3u> PIMathMatrixT<3u, 3u>::rotationX(double angle) {double c = cos(angle), s = sin(angle); PIMathMatrixT<3u, 3u> tm; tm[0][0] = 1.; tm[1][1] = tm[2][2] = c; tm[2][1] = s; tm[1][2] = -s; return tm;}
|
template<>
|
||||||
template<> inline PIMathMatrixT<3u, 3u> PIMathMatrixT<3u, 3u>::rotationY(double angle) {double c = cos(angle), s = sin(angle); PIMathMatrixT<3u, 3u> tm; tm[1][1] = 1.; tm[0][0] = tm[2][2] = c; tm[2][0] = -s; tm[0][2] = s; return tm;}
|
inline PIMathMatrixT<2u, 2u> PIMathMatrixT<2u, 2u>::scaleX(double factor) {
|
||||||
template<> inline PIMathMatrixT<3u, 3u> PIMathMatrixT<3u, 3u>::rotationZ(double angle) {double c = cos(angle), s = sin(angle); PIMathMatrixT<3u, 3u> tm; tm[2][2] = 1.; tm[0][0] = tm[1][1] = c; tm[1][0] = s; tm[0][1] = -s; return tm;}
|
PIMathMatrixT<2u, 2u> tm;
|
||||||
template<> inline PIMathMatrixT<3u, 3u> PIMathMatrixT<3u, 3u>::scaleX(double factor) {PIMathMatrixT<3u, 3u> tm; tm[1][1] = tm[2][2] = 1.; tm[0][0] = factor; return tm;}
|
tm[0][0] = factor;
|
||||||
template<> inline PIMathMatrixT<3u, 3u> PIMathMatrixT<3u, 3u>::scaleY(double factor) {PIMathMatrixT<3u, 3u> tm; tm[0][0] = tm[2][2] = 1.; tm[1][1] = factor; return tm;}
|
tm[1][1] = 1.;
|
||||||
template<> inline PIMathMatrixT<3u, 3u> PIMathMatrixT<3u, 3u>::scaleZ(double factor) {PIMathMatrixT<3u, 3u> tm; tm[0][0] = tm[1][1] = 1.; tm[2][2] = factor; return tm;}
|
return tm;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<>
|
||||||
|
inline PIMathMatrixT<2u, 2u> PIMathMatrixT<2u, 2u>::scaleY(double factor) {
|
||||||
|
PIMathMatrixT<2u, 2u> tm;
|
||||||
|
tm[0][0] = 1.;
|
||||||
|
tm[1][1] = factor;
|
||||||
|
return tm;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<>
|
||||||
|
inline PIMathMatrixT<3u, 3u> PIMathMatrixT<3u, 3u>::rotationX(double angle) {
|
||||||
|
double c = cos(angle), s = sin(angle);
|
||||||
|
PIMathMatrixT<3u, 3u> tm;
|
||||||
|
tm[0][0] = 1.;
|
||||||
|
tm[1][1] = tm[2][2] = c;
|
||||||
|
tm[2][1] = s;
|
||||||
|
tm[1][2] = -s;
|
||||||
|
return tm;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<>
|
||||||
|
inline PIMathMatrixT<3u, 3u> PIMathMatrixT<3u, 3u>::rotationY(double angle) {
|
||||||
|
double c = cos(angle), s = sin(angle);
|
||||||
|
PIMathMatrixT<3u, 3u> tm;
|
||||||
|
tm[1][1] = 1.;
|
||||||
|
tm[0][0] = tm[2][2] = c;
|
||||||
|
tm[2][0] = -s;
|
||||||
|
tm[0][2] = s;
|
||||||
|
return tm;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<>
|
||||||
|
inline PIMathMatrixT<3u, 3u> PIMathMatrixT<3u, 3u>::rotationZ(double angle) {
|
||||||
|
double c = cos(angle), s = sin(angle);
|
||||||
|
PIMathMatrixT<3u, 3u> tm;
|
||||||
|
tm[2][2] = 1.;
|
||||||
|
tm[0][0] = tm[1][1] = c;
|
||||||
|
tm[1][0] = s;
|
||||||
|
tm[0][1] = -s;
|
||||||
|
return tm;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<>
|
||||||
|
inline PIMathMatrixT<3u, 3u> PIMathMatrixT<3u, 3u>::scaleX(double factor) {
|
||||||
|
PIMathMatrixT<3u, 3u> tm;
|
||||||
|
tm[1][1] = tm[2][2] = 1.;
|
||||||
|
tm[0][0] = factor;
|
||||||
|
return tm;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<>
|
||||||
|
inline PIMathMatrixT<3u, 3u> PIMathMatrixT<3u, 3u>::scaleY(double factor) {
|
||||||
|
PIMathMatrixT<3u, 3u> tm;
|
||||||
|
tm[0][0] = tm[2][2] = 1.;
|
||||||
|
tm[1][1] = factor;
|
||||||
|
return tm;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<>
|
||||||
|
inline PIMathMatrixT<3u, 3u> PIMathMatrixT<3u, 3u>::scaleZ(double factor) {
|
||||||
|
PIMathMatrixT<3u, 3u> tm;
|
||||||
|
tm[0][0] = tm[1][1] = 1.;
|
||||||
|
tm[2][2] = factor;
|
||||||
|
return tm;
|
||||||
|
}
|
||||||
|
|
||||||
#ifdef PIP_STD_IOSTREAM
|
#ifdef PIP_STD_IOSTREAM
|
||||||
template<uint Rows, uint Cols, typename Type>
|
template<uint Rows, uint Cols, typename Type>
|
||||||
@@ -523,12 +688,19 @@ inline std::ostream & operator <<(std::ostream & s, const PIMathMatrixT<Rows, Co
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
template<uint Rows, uint Cols, typename Type>
|
template<uint Rows, uint Cols, typename Type>
|
||||||
inline PICout operator <<(PICout 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 << PICoutManipulators::NewLine << " ";} s << "}"; return s;}
|
inline PICout operator<<(PICout 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 << PICoutManipulators::NewLine << " "; }
|
||||||
|
s << "}";
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
/// Multiply matrices {Rows0 x CR} on {CR x Cols1}, result is {Rows0 x Cols1}
|
/// Multiply matrices {Rows0 x CR} on {CR x Cols1}, result is {Rows0 x Cols1}
|
||||||
template<uint CR, uint Rows0, uint Cols1, typename Type>
|
template<uint CR, uint Rows0, uint Cols1, typename Type>
|
||||||
inline PIMathMatrixT<Rows0, Cols1, Type> operator *(const PIMathMatrixT<Rows0, CR, Type> & fm,
|
inline PIMathMatrixT<Rows0, Cols1, Type> operator*(const PIMathMatrixT<Rows0, CR, Type> &fm,
|
||||||
const PIMathMatrixT<CR, Cols1, Type> & sm) {
|
const PIMathMatrixT<CR, Cols1, Type> &sm) {
|
||||||
PIMathMatrixT<Rows0, Cols1, Type> tm;
|
PIMathMatrixT<Rows0, Cols1, Type> tm;
|
||||||
Type t;
|
Type t;
|
||||||
for (uint j = 0; j < Rows0; ++j) {
|
for (uint j = 0; j < Rows0; ++j) {
|
||||||
@@ -544,8 +716,8 @@ inline PIMathMatrixT<Rows0, Cols1, Type> operator *(const PIMathMatrixT<Rows0, C
|
|||||||
|
|
||||||
/// Multiply matrix {Rows x Cols} on vector {Cols}, result is vector {Rows}
|
/// Multiply matrix {Rows x Cols} on vector {Cols}, result is vector {Rows}
|
||||||
template<uint Cols, uint Rows, typename Type>
|
template<uint Cols, uint Rows, typename Type>
|
||||||
inline PIMathVectorT<Rows, Type> operator *(const PIMathMatrixT<Rows, Cols, Type> & fm,
|
inline PIMathVectorT<Rows, Type> operator*(const PIMathMatrixT<Rows, Cols, Type> &fm,
|
||||||
const PIMathVectorT<Cols, Type> & sv) {
|
const PIMathVectorT<Cols, Type> &sv) {
|
||||||
PIMathVectorT<Rows, Type> tv;
|
PIMathVectorT<Rows, Type> tv;
|
||||||
Type t;
|
Type t;
|
||||||
for (uint j = 0; j < Rows; ++j) {
|
for (uint j = 0; j < Rows; ++j) {
|
||||||
@@ -559,8 +731,8 @@ inline PIMathVectorT<Rows, Type> operator *(const PIMathMatrixT<Rows, Cols, Type
|
|||||||
|
|
||||||
/// Multiply vector {Rows} on matrix {Rows x Cols}, result is vector {Cols}
|
/// Multiply vector {Rows} on matrix {Rows x Cols}, result is vector {Cols}
|
||||||
template<uint Cols, uint Rows, typename Type>
|
template<uint Cols, uint Rows, typename Type>
|
||||||
inline PIMathVectorT<Cols, Type> operator *(const PIMathVectorT<Rows, Type> & sv,
|
inline PIMathVectorT<Cols, Type> operator*(const PIMathVectorT<Rows, Type> &sv,
|
||||||
const PIMathMatrixT<Rows, Cols, Type> & fm) {
|
const PIMathMatrixT<Rows, Cols, Type> &fm) {
|
||||||
PIMathVectorT<Cols, Type> tv;
|
PIMathVectorT<Cols, Type> tv;
|
||||||
Type t;
|
Type t;
|
||||||
for (uint j = 0; j < Cols; ++j) {
|
for (uint j = 0; j < Cols; ++j) {
|
||||||
@@ -574,7 +746,7 @@ inline PIMathVectorT<Cols, Type> operator *(const PIMathVectorT<Rows, Type> & sv
|
|||||||
|
|
||||||
/// Multiply value(T) on matrix {Rows x Cols}, result is vector {Rows}
|
/// Multiply value(T) on matrix {Rows x Cols}, result is vector {Rows}
|
||||||
template<uint Cols, uint Rows, typename Type>
|
template<uint Cols, uint Rows, typename Type>
|
||||||
inline PIMathMatrixT<Rows, Cols, Type> operator *(const Type & x, const PIMathMatrixT<Rows, Cols, Type> & v) {
|
inline PIMathMatrixT<Rows, Cols, Type> operator*(const Type &x, const PIMathMatrixT<Rows, Cols, Type> &v) {
|
||||||
return v * x;
|
return v * x;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -612,209 +784,277 @@ class PIMathMatrix;
|
|||||||
//! \brief A class that works with matrix operations, the input data of which is the data type of the matrix
|
//! \brief A class that works with matrix operations, the input data of which is the data type of the matrix
|
||||||
template<typename Type>
|
template<typename Type>
|
||||||
class PIP_EXPORT PIMathMatrix : public PIVector2D<Type> {
|
class PIP_EXPORT PIMathMatrix : public PIVector2D<Type> {
|
||||||
typedef PIVector2D<Type> _V2D;
|
typedef PIVector2D<Type> _V2D;
|
||||||
typedef PIMathMatrix<Type> _CMatrix;
|
typedef PIMathMatrix<Type> _CMatrix;
|
||||||
typedef PIMathVector<Type> _CMCol;
|
typedef PIMathVector<Type> _CMCol;
|
||||||
public:
|
public:
|
||||||
PIMathMatrix(const uint cols = 0, const uint rows = 0, const Type & f = Type()) {_V2D::resize(rows, cols, f);}
|
PIMathMatrix(const uint cols = 0, const uint rows = 0, const Type &f = Type()) { _V2D::resize(rows, cols, f); }
|
||||||
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++];}
|
|
||||||
PIMathMatrix(const PIVector<PIVector<Type> > & val) {if(!val.isEmpty()) {_V2D::resize(val.size(), val[0].size()); PIMM_FOR_I(c, r) _V2D::element(r, c) = val[r][c];}}
|
|
||||||
PIMathMatrix(const PIVector2D<Type> & val) {if(!val.isEmpty()) {_V2D::resize(val.rows(), val.cols()); PIMM_FOR_I(c, r) _V2D::element(r, c) = val.element(r, c);}}
|
|
||||||
|
|
||||||
/**
|
PIMathMatrix(const uint cols, const uint rows, const PIVector<Type> &val) {
|
||||||
* @brief Сreates a matrix whose main diagonal is filled with ones and the remaining elements are zeros
|
_V2D::resize(rows, cols);
|
||||||
*
|
int i = 0;
|
||||||
* @param cols is number of matrix column uint type
|
PIMM_FOR_I(c, r) _V2D::element(r, c) = val[i++];
|
||||||
* @param rows is number of matrix row uint type
|
}
|
||||||
* @return identitied 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;}
|
|
||||||
|
|
||||||
/**
|
PIMathMatrix(const PIVector<PIVector<Type> > &val) {
|
||||||
* @brief Сreates a matrix whose row equal to vector
|
if (!val.isEmpty()) {
|
||||||
*
|
_V2D::resize(val.size(), val[0].size());
|
||||||
* @param val is the vector type PIMathVector
|
PIMM_FOR_I(c, r) _V2D::element(r, c) = val[r][c];
|
||||||
* @return matrix identitied by vector
|
}
|
||||||
*/
|
}
|
||||||
static _CMatrix matrixRow(const PIMathVector<Type> & val) {return _CMatrix(val.size(), 1, val.toVector());}
|
|
||||||
|
|
||||||
/**
|
PIMathMatrix(const PIVector2D<Type> &val) {
|
||||||
* @brief Сreates a matrix whose column equal to vector
|
if (!val.isEmpty()) {
|
||||||
*
|
_V2D::resize(val.rows(), val.cols());
|
||||||
* @param val is the vector type PIMathVector
|
PIMM_FOR_I(c, r) _V2D::element(r, c) = val.element(r, c);
|
||||||
* @return matrix identitied by vector
|
}
|
||||||
*/
|
}
|
||||||
static _CMatrix matrixCol(const PIMathVector<Type> & val) {return _CMatrix(1, val.size(), val.toVector());}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Set the selected column in matrix
|
* @brief Creates a matrix whose main diagonal is filled with ones and the remaining elements are zeros
|
||||||
*
|
*
|
||||||
* @param index is the number of the selected column
|
* @param cols is number of matrix column uint type
|
||||||
* @param v is a vector of the type _CMCol that needs to fill the column
|
* @param rows is number of matrix row uint type
|
||||||
* @return matrix type _CMatrix
|
* @return identity matrix of type PIMathMatrix
|
||||||
*/
|
*/
|
||||||
_CMatrix & setCol(uint index, const _CMCol & v) {PIMM_FOR_R(i) _V2D::element(i, index) = v[i]; return *this;}
|
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 Set the selected row in matrix
|
* @brief Creates a matrix whose row equal to vector
|
||||||
*
|
*
|
||||||
* @param index is the number of the selected row
|
* @param val is the vector type PIMathVector
|
||||||
* @param v is a vector of the type _CMCol that needs to fill the row
|
* @return identity matrix by vector
|
||||||
* @return matrix type _CMatrix
|
*/
|
||||||
*/
|
static _CMatrix matrixRow(const PIMathVector<Type> &val) { return _CMatrix(val.size(), 1, val.toVector()); }
|
||||||
_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
|
* @brief Creates a matrix whose column equal to vector
|
||||||
*
|
*
|
||||||
* @param r0 is the number of the first selected row
|
* @param val is the vector type PIMathVector
|
||||||
* @param r1 is the number of the second selected row
|
* @return identity matrix by vector
|
||||||
* @return matrix type _CMatrix
|
*/
|
||||||
*/
|
static _CMatrix matrixCol(const PIMathVector<Type> &val) { return _CMatrix(1, val.size(), val.toVector()); }
|
||||||
_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
|
* @brief Set the selected column in matrix
|
||||||
*
|
*
|
||||||
* @param c0 is the number of the first selected column
|
* @param index is the number of the selected column
|
||||||
* @param c1 is the number of the second selected column
|
* @param v is a vector of the type _CMCol that needs to fill the column
|
||||||
* @return matrix type _CMatrix
|
* @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;}
|
_CMatrix &setCol(uint index, const _CMCol &v) {
|
||||||
|
PIMM_FOR_R(i) _V2D::element(i, index) = v[i];
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Method which fills the matrix with selected value
|
* @brief Set the selected row in matrix
|
||||||
*
|
*
|
||||||
* @param v is a parameter the type and value of which is selected and later filled into the matrix
|
* @param index is the number of the selected row
|
||||||
* @return filled matrix type _CMatrix
|
* @param v is a vector of the type _CMCol that needs to fill the row
|
||||||
*/
|
* @return matrix type _CMatrix
|
||||||
_CMatrix & fill(const Type & v) {PIMM_FOR_A(i) _V2D::mat[i] = v; return *this;}
|
*/
|
||||||
|
_CMatrix &setRow(uint index, const _CMCol &v) {
|
||||||
|
PIMM_FOR_C(i) _V2D::element(index, i) = v[i];
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Method which checks if matrix is square
|
* @brief Method which changes selected rows in a matrix
|
||||||
*
|
*
|
||||||
* @return true if matrix is square, else false
|
* @param r0 is the number of the first selected row
|
||||||
*/
|
* @param r1 is the number of the second selected row
|
||||||
bool isSquare() const {return _V2D::cols_ == _V2D::rows_;}
|
* @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 checks if main diagonal of matrix consists of ones and another elements are zeros
|
* @brief Method which changes selected columns in a matrix
|
||||||
*
|
*
|
||||||
* @return true if matrix is identitied, else false
|
* @param c0 is the number of the first selected column
|
||||||
*/
|
* @param c1 is the number of the second selected column
|
||||||
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;}
|
* @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 checks if every elements of matrix are zeros
|
* @brief Method which fills the matrix with selected value
|
||||||
*
|
*
|
||||||
* @return true if matrix is null, else false
|
* @param v is a parameter the type and value of which is selected and later filled into the matrix
|
||||||
*/
|
* @return filled matrix type _CMatrix
|
||||||
bool isNull() const {PIMM_FOR_A(i) if (_V2D::mat[i] != Type(0)) return false; return true;}
|
*/
|
||||||
|
_CMatrix &fill(const Type &v) {
|
||||||
|
PIMM_FOR_A(i) _V2D::mat[i] = v;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Method which checks if matrix is empty
|
* @brief Method which checks if matrix is square
|
||||||
*
|
*
|
||||||
* @return true if matrix is valid, else false
|
* @return true if matrix is square, else false
|
||||||
*/
|
*/
|
||||||
bool isValid() const {return !PIVector2D<Type>::isEmpty();}
|
bool isSquare() const { return _V2D::cols_ == _V2D::rows_; }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Matrix assignment to matrix "v"
|
* @brief Method which checks if main diagonal of matrix consists of ones and another elements are zeros
|
||||||
*
|
*
|
||||||
* @param v matrix for the assigment
|
* @return true if matrix is identitied, else false
|
||||||
* @return matrix equal with v
|
*/
|
||||||
*/
|
bool isIdentity() const {
|
||||||
_CMatrix & operator =(const PIVector<PIVector<Type> > & v) {*this = _CMatrix(v); return *this;}
|
PIMM_FOR(c, r) if ((c == r) ? _V2D::element(r, c) != Type(1) : _V2D::element(r, c) != Type(0))return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Compare with matrix "sm"
|
* @brief Method which checks if every elements of matrix are zeros
|
||||||
*
|
*
|
||||||
* @param sm matrix for the compare
|
* @return true if matrix is null, else false
|
||||||
* @return if matrices are equal true, else false
|
*/
|
||||||
*/
|
bool isNull() const {
|
||||||
bool operator ==(const _CMatrix & sm) const {PIMM_FOR_A(i) if (_V2D::mat[i] != sm.mat[i]) return false; return true;}
|
PIMM_FOR_A(i) if (_V2D::mat[i] != Type(0)) return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Compare with matrix "sm"
|
* @brief Method which checks if matrix is empty
|
||||||
*
|
*
|
||||||
* @param sm matrix for the compare
|
* @return true if matrix is valid, else false
|
||||||
* @return if matrices are not equal true, else false
|
*/
|
||||||
*/
|
bool isValid() const { return !PIVector2D<Type>::isEmpty(); }
|
||||||
bool operator !=(const _CMatrix & sm) const {return !(*this == sm);}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Addition assignment with matrix "sm"
|
* @brief Matrix assignment to matrix "v"
|
||||||
*
|
*
|
||||||
* @param sm matrix for the addition assigment
|
* @param v matrix for the assigment
|
||||||
*/
|
* @return matrix equal with v
|
||||||
void operator +=(const _CMatrix & sm) {PIMM_FOR_A(i) _V2D::mat[i] += sm.mat[i];}
|
*/
|
||||||
|
_CMatrix &operator=(const PIVector<PIVector<Type> > &v) {
|
||||||
|
*this = _CMatrix(v);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Subtraction assignment with matrix "sm"
|
* @brief Compare with matrix "sm"
|
||||||
*
|
*
|
||||||
* @param sm matrix for the subtraction assigment
|
* @param sm matrix for the compare
|
||||||
*/
|
* @return if matrices are equal true, else false
|
||||||
void operator -=(const _CMatrix & sm) {PIMM_FOR_A(i) _V2D::mat[i] -= sm.mat[i];}
|
*/
|
||||||
|
bool operator==(const _CMatrix &sm) const {
|
||||||
|
PIMM_FOR_A(i) if (_V2D::mat[i] != sm.mat[i]) return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Multiplication assignment with value "v"
|
* @brief Compare with matrix "sm"
|
||||||
*
|
*
|
||||||
* @param v value for the multiplication assigment
|
* @param sm matrix for the compare
|
||||||
*/
|
* @return if matrices are not equal true, else false
|
||||||
void operator *=(const Type & v) {PIMM_FOR_A(i) _V2D::mat[i] *= v;}
|
*/
|
||||||
|
bool operator!=(const _CMatrix &sm) const { return !(*this == sm); }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Division assignment with value "v"
|
* @brief Addition assignment with matrix "sm"
|
||||||
*
|
*
|
||||||
* @param v value for the division assigment
|
* @param sm matrix for the addition assigment
|
||||||
*/
|
*/
|
||||||
void operator /=(const Type & v) {PIMM_FOR_A(i) _V2D::mat[i] /= v;}
|
void operator+=(const _CMatrix &sm) { PIMM_FOR_A(i) _V2D::mat[i] += sm.mat[i]; }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Matrix substraction
|
* @brief Subtraction assignment with matrix "sm"
|
||||||
*
|
*
|
||||||
* @return the result of matrix substraction
|
* @param sm matrix for the subtraction assigment
|
||||||
*/
|
*/
|
||||||
_CMatrix operator -() const {_CMatrix tm(*this); PIMM_FOR_A(i) tm.mat[i] = -_V2D::mat[i]; return tm;}
|
void operator-=(const _CMatrix &sm) { PIMM_FOR_A(i) _V2D::mat[i] -= sm.mat[i]; }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Matrix addition
|
* @brief Multiplication assignment with value "v"
|
||||||
*
|
*
|
||||||
* @param sm is matrix term
|
* @param v value for the multiplication assigment
|
||||||
* @return the result of matrix addition
|
*/
|
||||||
*/
|
void operator*=(const Type &v) { PIMM_FOR_A(i) _V2D::mat[i] *= v; }
|
||||||
_CMatrix operator +(const _CMatrix & sm) const {_CMatrix tm(*this); PIMM_FOR_A(i) tm.mat[i] += sm.mat[i]; return tm;}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Matrix substraction
|
* @brief Division assignment with value "v"
|
||||||
*
|
*
|
||||||
* @param sm is matrix subtractor
|
* @param v value for the division assigment
|
||||||
* @return the result of matrix substraction
|
*/
|
||||||
*/
|
void operator/=(const Type &v) { PIMM_FOR_A(i) _V2D::mat[i] /= v; }
|
||||||
_CMatrix operator -(const _CMatrix & sm) const {_CMatrix tm(*this); PIMM_FOR_A(i) tm.mat[i] -= sm.mat[i]; return tm;}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Matrix multiplication
|
* @brief Matrix substraction
|
||||||
*
|
*
|
||||||
* @param v is value factor
|
* @return the result of matrix substraction
|
||||||
* @return the result of matrix multiplication
|
*/
|
||||||
*/
|
_CMatrix operator-() const {
|
||||||
_CMatrix operator *(const Type & v) const {_CMatrix tm(*this); PIMM_FOR_A(i) tm.mat[i] *= v; return tm;}
|
_CMatrix tm(*this);
|
||||||
|
PIMM_FOR_A(i) tm.mat[i] = -_V2D::mat[i];
|
||||||
|
return tm;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Matrix division
|
* @brief Matrix addition
|
||||||
*
|
*
|
||||||
* @param v is value divider
|
* @param sm is matrix term
|
||||||
* @return the result of matrix division
|
* @return the result of matrix addition
|
||||||
*/
|
*/
|
||||||
_CMatrix operator /(const Type & v) const {_CMatrix tm(*this); PIMM_FOR_A(i) tm.mat[i] /= v; return tm;}
|
_CMatrix operator+(const _CMatrix &sm) const {
|
||||||
|
_CMatrix tm(*this);
|
||||||
|
PIMM_FOR_A(i) tm.mat[i] += sm.mat[i];
|
||||||
|
return tm;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Determinant of the matrix is calculated
|
* @brief Matrix subtraction
|
||||||
*
|
*
|
||||||
* @return matrix determinant
|
* @param sm is matrix subtractor
|
||||||
*/
|
* @return the result of matrix subtraction
|
||||||
Type determinant(bool * ok = 0) const {
|
*/
|
||||||
|
_CMatrix operator-(const _CMatrix &sm) const {
|
||||||
|
_CMatrix tm(*this);
|
||||||
|
PIMM_FOR_A(i) tm.mat[i] -= sm.mat[i];
|
||||||
|
return tm;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @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;
|
||||||
|
return tm;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @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;
|
||||||
|
return tm;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Determinant of the matrix is calculated
|
||||||
|
*
|
||||||
|
* @return matrix determinant
|
||||||
|
*/
|
||||||
|
Type determinant(bool *ok = 0) const {
|
||||||
_CMatrix m(*this);
|
_CMatrix m(*this);
|
||||||
bool k;
|
bool k;
|
||||||
Type ret = Type(0);
|
Type ret = Type(0);
|
||||||
@@ -829,12 +1069,12 @@ public:
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Trace of the matrix is calculated
|
* @brief Trace of the matrix is calculated
|
||||||
*
|
*
|
||||||
* @return matrix trace
|
* @return matrix trace
|
||||||
*/
|
*/
|
||||||
Type trace(bool * ok = 0) const {
|
Type trace(bool *ok = 0) const {
|
||||||
Type ret = Type(0);
|
Type ret = Type(0);
|
||||||
if (!isSquare()) {
|
if (!isSquare()) {
|
||||||
if (ok != 0) *ok = false;
|
if (ok != 0) *ok = false;
|
||||||
@@ -847,12 +1087,12 @@ public:
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Transforming matrix to upper triangular
|
* @brief Transforming matrix to upper triangular
|
||||||
*
|
*
|
||||||
* @return transformed upper triangular matrix
|
* @return transformed upper triangular matrix
|
||||||
*/
|
*/
|
||||||
_CMatrix & toUpperTriangular(bool * ok = 0) {
|
_CMatrix &toUpperTriangular(bool *ok = 0) {
|
||||||
if (!isSquare()) {
|
if (!isSquare()) {
|
||||||
if (ok != 0) *ok = false;
|
if (ok != 0) *ok = false;
|
||||||
return *this;
|
return *this;
|
||||||
@@ -878,7 +1118,7 @@ public:
|
|||||||
for (uint k = i; k < _V2D::cols_; ++k) smat.element(k, j) -= mul * smat.element(k, i);
|
for (uint k = i; k < _V2D::cols_; ++k) smat.element(k, j) -= mul * smat.element(k, i);
|
||||||
}
|
}
|
||||||
if (i < _V2D::cols_ - 1) {
|
if (i < _V2D::cols_ - 1) {
|
||||||
if (_PIMathMatrixNullCompare(smat.element(i+1, i+1))) {
|
if (_PIMathMatrixNullCompare(smat.element(i + 1, i + 1))) {
|
||||||
if (ok != 0) *ok = false;
|
if (ok != 0) *ok = false;
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
@@ -889,12 +1129,12 @@ public:
|
|||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Matrix inversion operation
|
* @brief Matrix inversion operation
|
||||||
*
|
*
|
||||||
* @return inverted matrix
|
* @return inverted matrix
|
||||||
*/
|
*/
|
||||||
_CMatrix & invert(bool * ok = 0, _CMCol * sv = 0) {
|
_CMatrix &invert(bool *ok = 0, _CMCol *sv = 0) {
|
||||||
if (!isSquare()) {
|
if (!isSquare()) {
|
||||||
if (ok != 0) *ok = false;
|
if (ok != 0) *ok = false;
|
||||||
return *this;
|
return *this;
|
||||||
@@ -926,7 +1166,7 @@ public:
|
|||||||
if (sv != 0) (*sv)[j] -= mul * (*sv)[i];
|
if (sv != 0) (*sv)[j] -= mul * (*sv)[i];
|
||||||
}
|
}
|
||||||
if (i < _V2D::cols_ - 1) {
|
if (i < _V2D::cols_ - 1) {
|
||||||
if (_PIMathMatrixNullCompare(smat.element(i+1, i+1))) {
|
if (_PIMathMatrixNullCompare(smat.element(i + 1, i + 1))) {
|
||||||
if (ok != 0) *ok = false;
|
if (ok != 0) *ok = false;
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
@@ -949,19 +1189,27 @@ public:
|
|||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Matrix inversion operation
|
* @brief Matrix inversion operation
|
||||||
*
|
*
|
||||||
* @return inverted matrix
|
* @return inverted matrix
|
||||||
*/
|
*/
|
||||||
_CMatrix inverted(bool * ok = 0) const {_CMatrix tm(*this); tm.invert(ok); return tm;}
|
_CMatrix inverted(bool *ok = 0) const {
|
||||||
|
_CMatrix tm(*this);
|
||||||
|
tm.invert(ok);
|
||||||
|
return tm;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Matrix transposition operation
|
* @brief Matrix transposition operation
|
||||||
*
|
*
|
||||||
* @return transposed matrix
|
* @return transposed matrix
|
||||||
*/
|
*/
|
||||||
_CMatrix transposed() const {_CMatrix tm(_V2D::rows_, _V2D::cols_); PIMM_FOR(c, r) tm.element(c, r) = _V2D::element(r, c); return tm;}
|
_CMatrix transposed() const {
|
||||||
|
_CMatrix tm(_V2D::rows_, _V2D::cols_);
|
||||||
|
PIMM_FOR(c, r) tm.element(c, r) = _V2D::element(r, c);
|
||||||
|
return tm;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
@@ -971,18 +1219,36 @@ inline std::ostream & operator <<(std::ostream & s, const PIMathMatrix<Type> & m
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
template<typename Type>
|
template<typename Type>
|
||||||
inline PICout operator <<(PICout s, const PIMathMatrix<Type> & m) {s << "Matrix{"; 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 << PICoutManipulators::NewLine << " ";} s << "}"; return s;}
|
inline PICout operator<<(PICout s, const PIMathMatrix<Type> &m) {
|
||||||
|
s << "Matrix{";
|
||||||
|
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 << PICoutManipulators::NewLine << " ";
|
||||||
|
}
|
||||||
|
s << "}";
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
template<typename Type>
|
template<typename Type>
|
||||||
inline PIByteArray & operator <<(PIByteArray & s, const PIMathMatrix<Type> & v) {s << (const PIVector2D<Type> &)v; return s;}
|
inline PIByteArray &operator<<(PIByteArray &s, const PIMathMatrix<Type> &v) {
|
||||||
|
s << (const PIVector2D<Type> &) v;
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
template<typename Type>
|
template<typename Type>
|
||||||
inline PIByteArray & operator >>(PIByteArray & s, PIMathMatrix<Type> & v) {s >> (PIVector2D<Type> &)v; return s;}
|
inline PIByteArray &operator>>(PIByteArray &s, PIMathMatrix<Type> &v) {
|
||||||
|
s >> (PIVector2D<Type> &) v;
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/// Multiply matrices {CR x Rows0} on {Cols1 x CR}, result is {Cols1 x Rows0}
|
/// Multiply matrices {CR x Rows0} on {Cols1 x CR}, result is {Cols1 x Rows0}
|
||||||
template<typename Type>
|
template<typename Type>
|
||||||
inline PIMathMatrix<Type> operator *(const PIMathMatrix<Type> & fm,
|
inline PIMathMatrix<Type> operator*(const PIMathMatrix<Type> &fm,
|
||||||
const PIMathMatrix<Type> & sm) {
|
const PIMathMatrix<Type> &sm) {
|
||||||
uint cr = fm.cols(), rows0 = fm.rows(), cols1 = sm.cols();
|
uint cr = fm.cols(), rows0 = fm.rows(), cols1 = sm.cols();
|
||||||
PIMathMatrix<Type> tm(cols1, rows0);
|
PIMathMatrix<Type> tm(cols1, rows0);
|
||||||
if (fm.cols() != sm.rows()) return tm;
|
if (fm.cols() != sm.rows()) return tm;
|
||||||
@@ -1000,8 +1266,8 @@ inline PIMathMatrix<Type> operator *(const PIMathMatrix<Type> & fm,
|
|||||||
|
|
||||||
/// Multiply matrix {Cols x Rows} on vector {Cols}, result is vector {Rows}
|
/// Multiply matrix {Cols x Rows} on vector {Cols}, result is vector {Rows}
|
||||||
template<typename Type>
|
template<typename Type>
|
||||||
inline PIMathVector<Type> operator *(const PIMathMatrix<Type> & fm,
|
inline PIMathVector<Type> operator*(const PIMathMatrix<Type> &fm,
|
||||||
const PIMathVector<Type> & sv) {
|
const PIMathVector<Type> &sv) {
|
||||||
uint c = fm.cols(), r = fm.rows();
|
uint c = fm.cols(), r = fm.rows();
|
||||||
PIMathVector<Type> tv(r);
|
PIMathVector<Type> tv(r);
|
||||||
if (c != sv.size()) return tv;
|
if (c != sv.size()) return tv;
|
||||||
@@ -1018,8 +1284,8 @@ inline PIMathVector<Type> operator *(const PIMathMatrix<Type> & fm,
|
|||||||
|
|
||||||
/// Multiply vector {Rows} on matrix {Rows x Cols}, result is vector {Cols}
|
/// Multiply vector {Rows} on matrix {Rows x Cols}, result is vector {Cols}
|
||||||
template<typename Type>
|
template<typename Type>
|
||||||
inline PIMathVector<Type> operator *(const PIMathVector<Type> & sv,
|
inline PIMathVector<Type> operator*(const PIMathVector<Type> &sv,
|
||||||
const PIMathMatrix<Type> & fm) {
|
const PIMathMatrix<Type> &fm) {
|
||||||
uint c = fm.cols(), r = fm.rows();
|
uint c = fm.cols(), r = fm.rows();
|
||||||
PIMathVector<Type> tv(c);
|
PIMathVector<Type> tv(c);
|
||||||
Type t;
|
Type t;
|
||||||
@@ -1034,7 +1300,7 @@ inline PIMathVector<Type> operator *(const PIMathVector<Type> & sv,
|
|||||||
|
|
||||||
/// Multiply value(T) on matrix {Rows x Cols}, result is vector {Rows}
|
/// Multiply value(T) on matrix {Rows x Cols}, result is vector {Rows}
|
||||||
template<typename Type>
|
template<typename Type>
|
||||||
inline PIMathMatrix<Type> operator *(const Type & x, const PIMathMatrix<Type> & v) {
|
inline PIMathMatrix<Type> operator*(const Type &x, const PIMathMatrix<Type> &v) {
|
||||||
return v * x;
|
return v * x;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1042,9 +1308,11 @@ typedef PIMathMatrix<int> PIMathMatrixi;
|
|||||||
typedef PIMathMatrix<double> PIMathMatrixd;
|
typedef PIMathMatrix<double> PIMathMatrixd;
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
PIMathMatrix<complex<T> > hermitian(const PIMathMatrix<complex<T> > & m) {
|
PIMathMatrix<complex<T> > hermitian(const PIMathMatrix<complex<T> > &m) {
|
||||||
PIMathMatrix<complex<T> > ret(m);
|
PIMathMatrix<complex<T> > ret(m);
|
||||||
for (uint r = 0; r < ret.rows(); ++r) for (uint c = 0; c < ret.cols(); ++c) ret.element(r, c).imag(-(ret.element(r, c).imag()));
|
for (uint r = 0; r < ret.rows(); ++r)
|
||||||
|
for (uint c = 0; c < ret.cols(); ++c)
|
||||||
|
ret.element(r, c).imag(-(ret.element(r, c).imag()));
|
||||||
return ret.transposed();
|
return ret.transposed();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user