Compare commits

3 Commits

Author SHA1 Message Date
fbe850abf0 template math functions in pimathmatrix.h and pimathvector.h and pimathbase.h
add PIMathMatrixT::rotate for matrix 2x2
2020-10-22 17:29:58 +03:00
f5652efc32 refactoring PIMathMatrix 2020-10-22 16:48:46 +03:00
7413c7252b refactor PIMathMatrixT and fix pimathvector.h 2020-10-22 16:13:39 +03:00
4 changed files with 145 additions and 157 deletions

View File

@@ -98,9 +98,6 @@ const double rad2deg = M_180_PI;
inline int sign(const float & x) {return (x < 0.) ? -1 : (x > 0. ? 1 : 0);} inline int sign(const float & x) {return (x < 0.) ? -1 : (x > 0. ? 1 : 0);}
inline int sign(const double & x) {return (x < 0.) ? -1 : (x > 0. ? 1 : 0);} inline int sign(const double & x) {return (x < 0.) ? -1 : (x > 0. ? 1 : 0);}
inline int pow2(const int p) {return 1 << p;} inline int pow2(const int p) {return 1 << p;}
inline int sqr(const int v) {return v * v;}
inline float sqr(const float & v) {return v * v;}
inline double sqr(const double & v) {return v * v;}
inline double sinc(const double & v) {if (v == 0.) return 1.; double t = M_PI * v; return sin(t) / t;} inline double sinc(const double & v) {if (v == 0.) return 1.; double t = M_PI * v; return sin(t) / t;}
PIP_EXPORT double piJ0(const double & v); PIP_EXPORT double piJ0(const double & v);
@@ -109,22 +106,23 @@ PIP_EXPORT double piJn(int n, const double & v);
PIP_EXPORT double piY0(const double & v); PIP_EXPORT double piY0(const double & v);
PIP_EXPORT double piY1(const double & v); PIP_EXPORT double piY1(const double & v);
PIP_EXPORT double piYn(int n, const double & v); PIP_EXPORT double piYn(int n, const double & v);
inline double toDb(double val) {return 10. * log10(val);}
inline double fromDb(double val) {return pow(10., val / 10.);} template <typename T> inline constexpr T toDb(T val) {return T(10.) * std::log10(val);}
inline double toRad(double deg) {return deg * M_PI_180;} template <typename T> inline constexpr T fromDb(T val) {return std::pow(T(10.), val / T(10.));}
inline double toDeg(double rad) {return rad * M_180_PI;} template <typename T> inline constexpr T toRad(T deg) {return deg * T(M_PI_180);}
template <typename T> inline constexpr T toDeg(T rad) {return rad * T(M_180_PI);}
template <typename T> inline constexpr T sqr(const T & v) {return v * v;}
// [-1 ; 1] // [-1 ; 1]
PIP_EXPORT double randomd(); PIP_EXPORT double randomd();
// [-1 ; 1] normal // [-1 ; 1] normal
PIP_EXPORT double randomn(double dv = 0., double sv = 1.); PIP_EXPORT double randomn(double dv = 0., double sv = 1.);
template<typename T> inline PIVector<T> piAbs(const PIVector<T> & v) {
inline PIVector<double> abs(const PIVector<double> & v) { PIVector<T> result;
PIVector<double> result;
result.resize(v.size()); result.resize(v.size());
for (uint i = 0; i < v.size(); i++) for (uint i = 0; i < v.size(); i++)
result[i] = fabs(v[i]); result[i] = piAbs<T>(v[i]);
return result; return result;
} }

View File

@@ -53,20 +53,15 @@ class PIP_EXPORT PIMathMatrixT {
static_assert(Cols > 0, "Column count must be > 0"); static_assert(Cols > 0, "Column count must be > 0");
public: public:
/** /**
* @brief Constructor that calls the private resize method * @brief Constructs PIMathMatrixT that is filled by \a new_value
*
* @return identitied matrix of type PIMathMatrixT
*/ */
PIMathMatrixT() { resize(Rows, Cols); } PIMathMatrixT(const Type &new_value = Type()) {PIMM_FOR m[r][c] = new_value;}
/** /**
* @brief Constructor that calls the private resize method * @brief Contructs PIMathMatrixT from PIVector
*
* @param val is the PIVector with which the matrix is filled
* @return identitied matrix of type PIMathMatrixT
*/ */
PIMathMatrixT(const PIVector<Type> &val) { PIMathMatrixT(const PIVector<Type> &val) {
resize(Rows, Cols); assert(Rows*Cols == val.size());
int i = 0; int i = 0;
PIMM_FOR m[r][c] = val[i++]; PIMM_FOR m[r][c] = val[i++];
} }
@@ -91,31 +86,19 @@ public:
return tm; return tm;
} }
/**
* @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 tm.m[r][c] = v;
return tm;
}
/** /**
* @brief Method which returns number of columns in matrix * @brief Method which returns number of columns in matrix
* *
* @return type uint shows number of columns * @return type uint shows number of columns
*/ */
uint cols() const { return Cols; } constexpr uint cols() const {return Cols;}
/** /**
* @brief Method which returns number of rows in matrix * @brief Method which returns number of rows in matrix
* *
* @return type uint shows number of rows * @return type uint shows number of rows
*/ */
uint rows() const { return Rows; } constexpr 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.
@@ -177,13 +160,8 @@ public:
* @param r1 is the number of the second selected row * @param r1 is the number of the second selected row
* @return matrix type _CMatrix * @return matrix type _CMatrix
*/ */
_CMatrix &swapRows(uint r0, uint r1) { _CMatrix &swapRows(uint rf, uint rs) {
Type t; PIMM_FOR_C piSwap<Type>(m[rf][i], m[rs][i]);
PIMM_FOR_C {
t = m[r0][i];
m[r0][i] = m[r1][i];
m[r1][i] = t;
}
return *this; return *this;
} }
@@ -195,13 +173,8 @@ public:
* @param c1 is the number of the second selected column * @param c1 is the number of the second selected column
* @return matrix type _CMatrix * @return matrix type _CMatrix
*/ */
_CMatrix &swapCols(uint c0, uint c1) { _CMatrix &swapCols(uint cf, uint cs) {
Type t; PIMM_FOR_R piSwap<Type>(m[i][cf], m[i][cs]);
PIMM_FOR_R {
t = m[i][c0];
m[i][c0] = m[i][c1];
m[i][c1] = t;
}
return *this; return *this;
} }
@@ -221,7 +194,7 @@ public:
* *
* @return true if matrix is square, else false * @return true if matrix is square, else false
*/ */
bool isSquare() const { return cols() == rows(); } constexpr bool isSquare() const { return Rows == Cols; }
/** /**
* @brief Method which checks if main diagonal of matrix consists of ones and another elements are zeros * @brief Method which checks if main diagonal of matrix consists of ones and another elements are zeros
@@ -243,30 +216,41 @@ public:
return true; return true;
} }
/**
* @brief Full access to elements reference by row "row" and col "col".
* If you enter an index out of the border of the matrix there will be "undefined behavior"
*
* @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". * @brief Read-only access to element by \a row and \a col.
* If you enter an index out of the border of the matrix there will be "undefined behavior" * If you enter an index out of the border of the matrix there will be "undefined behavior"
* *
* @param row is a parameter that shows the row number of the matrix of the selected element * @param row of matrix
* @param col is a parameter that shows the column number of the matrix of the selected element * @param col of matrix
* @return element of matrix by row "row" and col "col" * @return copy of element of matrix
*/ */
Type at(uint row, uint col) const { return m[row][col]; } Type at(uint row, uint col) const { return m[row][col]; }
/**
* @brief Full access to element by \a row and \a col.
* If you enter an index out of the border of the matrix there will be "undefined behavior"
*
* @param row of matrix
* @param col of matrix
* @return element of matrix
*/
inline Type & element(uint row, uint col) {return m[row][col];}
/**
* @brief Read-only access to element by \a row and \a col.
* If you enter an index out of the border of the matrix there will be "undefined behavior"
*
* @param row of matrix
* @param col of matrix
* @return element of matrix
*/
inline const Type & element(uint row, uint col) const {return m[row][col];}
/** /**
* @brief Full access to the matrix row pointer. If you enter an index out of the border of the matrix there will be "undefined behavior" * @brief Full access to the matrix row pointer. If you enter an index out of the border of the matrix there will be "undefined behavior"
* *
* @param row is a row of necessary matrix * @param row of matrix
* @return matrix row pointer * @return matrix row pointer
*/ */
Type *operator[](uint row) { return m[row]; } Type *operator[](uint row) { return m[row]; }
@@ -274,26 +258,15 @@ public:
/** /**
* @brief Read-only access to the matrix row pointer. If you enter an index out of the border of the matrix there will be "undefined behavior" * @brief Read-only access to the matrix row pointer. If you enter an index out of the border of the matrix there will be "undefined behavior"
* *
* @param row is a row of necessary matrix * @param row of matrix
* @return matrix row pointer * @return matrix row pointer
*/ */
const Type *operator[](uint row) const {return m[row];} const Type *operator[](uint row) const {return m[row];}
/** /**
* @brief Matrix assignment to matrix "sm" * @brief Matrix compare
* *
* @param sm matrix for the assigment * @param sm matrix for compare
* @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 * @return if matrices are equal true, else false
*/ */
bool operator==(const _CMatrix &sm) const { bool operator==(const _CMatrix &sm) const {
@@ -302,9 +275,9 @@ public:
} }
/** /**
* @brief Compare with matrix "sm" * @brief Matrix negative compare
* *
* @param sm matrix for the compare * @param sm matrix for compare
* @return if matrices are not equal true, else false * @return if matrices are not equal true, else false
*/ */
bool operator!=(const _CMatrix &sm) const { return !(*this == sm); } bool operator!=(const _CMatrix &sm) const { return !(*this == sm); }
@@ -328,14 +301,19 @@ public:
* *
* @param v value for the multiplication assigment * @param v value for the multiplication assigment
*/ */
void operator*=(const Type &v) { PIMM_FOR m[r][c] *= v; } void operator*=(const Type &v) {
PIMM_FOR m[r][c] *= v;
}
/** /**
* @brief Division assignment with value "v" * @brief Division assignment with value "v"
* *
* @param v value for the division assigment * @param v value for the division assigment
*/ */
void operator/=(const Type &v) { PIMM_FOR m[r][c] /= v; } void operator/=(const Type &v) {
assert(piAbs<Type>(v) > PIMATHVECTOR_ZERO_CMP);
PIMM_FOR m[r][c] /= v;
}
/** /**
* @brief Matrix substraction * @brief Matrix substraction
@@ -391,6 +369,7 @@ public:
* @return the result of matrix division * @return the result of matrix division
*/ */
_CMatrix operator/(const Type &v) const { _CMatrix operator/(const Type &v) const {
assert(piAbs<Type>(v) > PIMATHVECTOR_ZERO_CMP);
_CMatrix tm = _CMatrix(*this); _CMatrix tm = _CMatrix(*this);
PIMM_FOR tm.m[r][c] /= v; PIMM_FOR tm.m[r][c] /= v;
return tm; return tm;
@@ -446,7 +425,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 (piAbs<Type>(smat.m[i + 1][i + 1]) < Type(1E-200)) {
if (ok != 0) *ok = false; if (ok != 0) *ok = false;
return *this; return *this;
} }
@@ -490,7 +469,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 (piAbs<Type>(smat.m[i + 1][i + 1]) < Type(1E-200)) {
if (ok != 0) *ok = false; if (ok != 0) *ok = false;
return *this; return *this;
} }
@@ -534,23 +513,25 @@ public:
return tm; return tm;
} }
private: _CMatrix rotate(Type angle) {
void resize(uint rows_, uint cols_, const Type &new_value = Type()) { static_assert(Rows == 2 && Cols == 2, "Works only with 2x2 matrix");
r_ = rows_; Type c = std::cos(angle);
c_ = cols_; Type s = std::sin(angle);
PIMM_FOR m[r][c] = new_value; PIMathMatrixT<2u, 2u> tm;
tm[0][0] = tm[1][1] = c;
tm[0][1] = -s;
tm[1][0] = s;
*this = *this * tm;
return *this;
} }
int c_, r_; private:
Type m[Rows][Cols]; Type m[Rows][Cols];
}; };
#pragma pack(pop) #pragma pack(pop)
#ifdef PIP_STD_IOSTREAM #ifdef PIP_STD_IOSTREAM
template<uint Rows, uint Cols, typename Type> template<uint Rows, uint Cols, typename Type>
inline std::ostream & operator <<(std::ostream & s, const PIMathMatrixT<Rows, Cols, Type> & m) { inline std::ostream & operator <<(std::ostream & s, const PIMathMatrixT<Rows, Cols, Type> & m) {
@@ -687,11 +668,10 @@ class PIMathMatrix;
/// Matrix /// Matrix
#define PIMM_FOR(c, r) for (uint c = 0; c < _V2D::cols_; ++c) for (uint r = 0; r < _V2D::rows_; ++r) #define PIMM_FOR for (uint r = 0; r < _V2D::rows_; ++r) for (uint c = 0; c < _V2D::cols_; ++c)
#define PIMM_FOR_I(c, r) for (uint r = 0; r < _V2D::rows_; ++r) for (uint c = 0; c < _V2D::cols_; ++c) #define PIMM_FOR_A for (uint i = 0; i < _V2D::mat.size(); ++i)
#define PIMM_FOR_A(v) for (uint v = 0; v < _V2D::mat.size(); ++v) #define PIMM_FOR_C for (uint i = 0; i < _V2D::cols_; ++i)
#define PIMM_FOR_C(v) for (uint v = 0; v < _V2D::cols_; ++v) #define PIMM_FOR_R for (uint i = 0; i < _V2D::rows_; ++i)
#define PIMM_FOR_R(v) for (uint v = 0; v < _V2D::rows_; ++v)
//! \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
//! @tparam There are can be basic C++ language data and different classes where the arithmetic operators(=, +=, -=, *=, /=, ==, !=, +, -, *, /) //! @tparam There are can be basic C++ language data and different classes where the arithmetic operators(=, +=, -=, *=, /=, ==, !=, +, -, *, /)
@@ -700,7 +680,6 @@ 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;
public: public:
/** /**
* @brief Constructor of class PIMathMatrix, which creates a matrix * @brief Constructor of class PIMathMatrix, which creates a matrix
@@ -721,7 +700,7 @@ public:
PIMathMatrix(const uint cols, const uint rows, const PIVector<Type> &val) { PIMathMatrix(const uint cols, const uint rows, const PIVector<Type> &val) {
_V2D::resize(rows, cols); _V2D::resize(rows, cols);
int i = 0; int i = 0;
PIMM_FOR_I(c, r) _V2D::element(r, c) = val[i++]; PIMM_FOR _V2D::element(r, c) = val[i++];
} }
/** /**
@@ -732,7 +711,11 @@ public:
PIMathMatrix(const PIVector<PIVector<Type> > &val) { PIMathMatrix(const PIVector<PIVector<Type> > &val) {
if (!val.isEmpty()) { if (!val.isEmpty()) {
_V2D::resize(val.size(), val[0].size()); _V2D::resize(val.size(), val[0].size());
PIMM_FOR_I(c, r) _V2D::element(r, c) = val[r][c]; for (uint r = 0; r < _V2D::rows_; ++r) {
assert(val[r].size() == _V2D::cols_);
for (uint c = 0; c < _V2D::cols_; ++c)
_V2D::element(r, c) = val[r][c];
}
} }
} }
@@ -744,7 +727,7 @@ public:
PIMathMatrix(const PIVector2D<Type> &val) { PIMathMatrix(const PIVector2D<Type> &val) {
if (!val.isEmpty()) { if (!val.isEmpty()) {
_V2D::resize(val.rows(), val.cols()); _V2D::resize(val.rows(), val.cols());
PIMM_FOR_I(c, r) _V2D::element(r, c) = val.element(r, c); PIMM_FOR _V2D::element(r, c) = val.element(r, c);
} }
} }
@@ -761,19 +744,6 @@ public:
return tm; return tm;
} }
/**
* @brief Creates a matrix filled by zeros
*
* @param cols is number of matrix column uint type
* @param rows is number of matrix row uint type
* @return zero matrix(cols,rows)
*/
static _CMatrix zero(const uint cols, const uint rows) {
_CMatrix tm(cols, rows);
tm.fill(Type(0));
return tm;
}
/** /**
* @brief Creates a row matrix of every element that is equal to every element of the vector * @brief Creates a row matrix of every element that is equal to every element of the vector
* *
@@ -798,8 +768,9 @@ public:
* @param v is a vector of the type _CMCol that needs to fill the 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 &setCol(uint index, const _CMCol &v) { _CMatrix &setCol(uint index, const PIMathVector<Type> &v) {
PIMM_FOR_R(i) _V2D::element(i, index) = v[i]; assert(_V2D::rows == v.size());
PIMM_FOR_R _V2D::element(i, index) = v[i];
return *this; return *this;
} }
@@ -810,8 +781,9 @@ public:
* @param v is a vector of the type _CMCol that needs to fill the row * @param v is a vector of the type _CMCol that needs to fill the row
* @return matrix type _CMatrix * @return matrix type _CMatrix
*/ */
_CMatrix &setRow(uint index, const _CMCol &v) { _CMatrix &setRow(uint index, const PIMathVector<Type> &v) {
PIMM_FOR_C(i) _V2D::element(index, i) = v[i]; assert(_V2D::cols == v.size());
PIMM_FOR_C _V2D::element(index, i) = v[i];
return *this; return *this;
} }
@@ -824,7 +796,7 @@ public:
* @return matrix type _CMatrix * @return matrix type _CMatrix
*/ */
_CMatrix &swapCols(uint r0, uint r1) { _CMatrix &swapCols(uint r0, uint r1) {
PIMM_FOR_C(i) { piSwap(_V2D::element(i, r0), _V2D::element(i, r1)); } PIMM_FOR_C piSwap<Type>(_V2D::element(i, r0), _V2D::element(i, r1));
return *this; return *this;
} }
@@ -837,7 +809,7 @@ public:
* @return matrix type _CMatrix * @return matrix type _CMatrix
*/ */
_CMatrix &swapRows(uint c0, uint c1) { _CMatrix &swapRows(uint c0, uint c1) {
PIMM_FOR_R(i) { piSwap(_V2D::element(c0, i), _V2D::element(c1, i)); } PIMM_FOR_R piSwap(_V2D::element(c0, i), _V2D::element(c1, i));
return *this; return *this;
} }
@@ -848,7 +820,7 @@ public:
* @return filled matrix type _CMatrix * @return filled matrix type _CMatrix
*/ */
_CMatrix &fill(const Type &v) { _CMatrix &fill(const Type &v) {
PIMM_FOR_A(i) _V2D::mat[i] = v; PIMM_FOR_A _V2D::mat[i] = v;
return *this; return *this;
} }
@@ -865,7 +837,7 @@ public:
* @return true if matrix is identity, else false * @return true if matrix is identity, else false
*/ */
bool isIdentity() const { bool isIdentity() const {
PIMM_FOR(c, r) if ((c == r) ? _V2D::element(r, c) != Type(1) : _V2D::element(r, c) != Type(0))return false; PIMM_FOR if ((c == r) ? _V2D::element(r, c) != Type(1) : _V2D::element(r, c) != Type(0))return false;
return true; return true;
} }
@@ -875,7 +847,7 @@ public:
* @return true if matrix elements equal to zero, else false * @return true if matrix elements equal to zero, else false
*/ */
bool isNull() const { bool isNull() const {
PIMM_FOR_A(i) if (_V2D::mat[i] != Type(0)) return false; PIMM_FOR_A if (_V2D::mat[i] != Type(0)) return false;
return true; return true;
} }
@@ -894,7 +866,7 @@ public:
void operator+=(const _CMatrix &sm) { void operator+=(const _CMatrix &sm) {
assert(_V2D::rows() == sm.rows()); assert(_V2D::rows() == sm.rows());
assert(_V2D::cols() == sm.cols()); assert(_V2D::cols() == sm.cols());
PIMM_FOR_A(i) _V2D::mat[i] += sm.mat[i]; PIMM_FOR_A _V2D::mat[i] += sm.mat[i];
} }
/** /**
@@ -905,7 +877,7 @@ public:
void operator-=(const _CMatrix &sm) { void operator-=(const _CMatrix &sm) {
assert(_V2D::rows() == sm.rows()); assert(_V2D::rows() == sm.rows());
assert(_V2D::cols() == sm.cols()); assert(_V2D::cols() == sm.cols());
PIMM_FOR_A(i) _V2D::mat[i] -= sm.mat[i]; PIMM_FOR_A _V2D::mat[i] -= sm.mat[i];
} }
/** /**
@@ -913,14 +885,19 @@ public:
* *
* @param v value for the multiplication assigment * @param v value for the multiplication assigment
*/ */
void operator*=(const Type &v) { PIMM_FOR_A(i) _V2D::mat[i] *= v; } void operator*=(const Type &v) {
PIMM_FOR_A _V2D::mat[i] *= v;
}
/** /**
* @brief Division assignment with value "v" * @brief Division assignment with value "v"
* *
* @param v value for the division assigment * @param v value for the division assigment
*/ */
void operator/=(const Type &v) { PIMM_FOR_A(i) _V2D::mat[i] /= v; } void operator/=(const Type &v) {
assert(piAbs<Type>(v) > PIMATHVECTOR_ZERO_CMP);
PIMM_FOR_A _V2D::mat[i] /= v;
}
/** /**
* @brief Matrix substraction * @brief Matrix substraction
@@ -929,7 +906,7 @@ public:
*/ */
_CMatrix operator-() const { _CMatrix operator-() const {
_CMatrix tm(*this); _CMatrix tm(*this);
PIMM_FOR_A(i) tm.mat[i] = -_V2D::mat[i]; PIMM_FOR_A tm.mat[i] = -_V2D::mat[i];
return tm; return tm;
} }
@@ -943,7 +920,7 @@ public:
_CMatrix tm(*this); _CMatrix tm(*this);
assert(tm.rows() == sm.rows()); assert(tm.rows() == sm.rows());
assert(tm.cols() == sm.cols()); assert(tm.cols() == sm.cols());
PIMM_FOR_A(i) tm.mat[i] += sm.mat[i]; PIMM_FOR_A tm.mat[i] += sm.mat[i];
return tm; return tm;
} }
@@ -957,7 +934,7 @@ public:
_CMatrix tm(*this); _CMatrix tm(*this);
assert(tm.rows() == sm.rows()); assert(tm.rows() == sm.rows());
assert(tm.cols() == sm.cols()); assert(tm.cols() == sm.cols());
PIMM_FOR_A(i) tm.mat[i] -= sm.mat[i]; PIMM_FOR_A tm.mat[i] -= sm.mat[i];
return tm; return tm;
} }
@@ -969,7 +946,7 @@ public:
*/ */
_CMatrix operator*(const Type &v) const { _CMatrix operator*(const Type &v) const {
_CMatrix tm(*this); _CMatrix tm(*this);
PIMM_FOR_A(i) tm.mat[i] *= v; PIMM_FOR_A tm.mat[i] *= v;
return tm; return tm;
} }
@@ -980,8 +957,9 @@ public:
* @return the result of matrix division * @return the result of matrix division
*/ */
_CMatrix operator/(const Type &v) const { _CMatrix operator/(const Type &v) const {
assert(piAbs<Type>(v) > PIMATHVECTOR_ZERO_CMP);
_CMatrix tm(*this); _CMatrix tm(*this);
PIMM_FOR_A(i) tm.mat[i] /= v; PIMM_FOR_A tm.mat[i] /= v;
return tm; return tm;
} }
@@ -1075,7 +1053,7 @@ public:
* @param sv is a vector multiplier * @param sv is a vector multiplier
* @return copy of inverted matrix * @return copy of inverted matrix
*/ */
_CMatrix &invert(bool *ok = 0, _CMCol *sv = 0) { _CMatrix &invert(bool *ok = 0, PIMathVector<Type> *sv = 0) {
if (!isSquare()) { if (!isSquare()) {
if (ok != 0) *ok = false; if (ok != 0) *ok = false;
return *this; return *this;
@@ -1149,7 +1127,7 @@ public:
*/ */
_CMatrix transposed() const { _CMatrix transposed() const {
_CMatrix tm(_V2D::rows_, _V2D::cols_); _CMatrix tm(_V2D::rows_, _V2D::cols_);
PIMM_FOR(c, r) tm.element(c, r) = _V2D::element(r, c); PIMM_FOR tm.element(c, r) = _V2D::element(r, c);
return tm; return tm;
} }
}; };
@@ -1309,7 +1287,6 @@ PIMathMatrix<complex<T> > hermitian(const PIMathMatrix<complex<T> > &m) {
} }
#undef PIMM_FOR #undef PIMM_FOR
#undef PIMM_FOR_I
#undef PIMM_FOR_A #undef PIMM_FOR_A
#undef PIMM_FOR_C #undef PIMM_FOR_C
#undef PIMM_FOR_R #undef PIMM_FOR_R

View File

@@ -56,16 +56,20 @@ public:
return tv; return tv;
} }
uint size() const {return Size;} constexpr uint size() const {return Size;}
_CVector & fill(const Type & v) {PIMV_FOR c[i] = v; return *this;} _CVector & fill(const Type & v) {PIMV_FOR c[i] = v; return *this;}
_CVector & move(const Type & v) {PIMV_FOR c[i] += v; return *this;} _CVector & move(const Type & v) {PIMV_FOR c[i] += v; return *this;}
_CVector & move(const _CVector & v) {PIMV_FOR c[i] += v[i]; return *this;} _CVector & move(const _CVector & v) {PIMV_FOR c[i] += v[i]; return *this;}
_CVector & swapElements(uint f, uint s) {
piSwap<Type>(c[f], c[s]);
return *this;
}
Type lengthSqr() const { Type lengthSqr() const {
Type tv(0); Type tv(0);
PIMV_FOR tv += c[i] * c[i]; PIMV_FOR tv += c[i] * c[i];
return tv; return tv;
} }
Type length() const {return sqrt(lengthSqr());} Type length() const {return std::sqrt(lengthSqr());}
Type manhattanLength() const { Type manhattanLength() const {
Type tv(0); Type tv(0);
PIMV_FOR tv += piAbs<Type>(c[i]); PIMV_FOR tv += piAbs<Type>(c[i]);
@@ -78,10 +82,10 @@ public:
} }
Type angleSin(const _CVector & v) const { Type angleSin(const _CVector & v) const {
Type tv = angleCos(v); Type tv = angleCos(v);
return sqrt(Type(1) - tv * tv); return std::sqrt(Type(1) - tv * tv);
} }
Type angleRad(const _CVector & v) const {return acos(angleCos(v));} Type angleRad(const _CVector & v) const {return std::acos(angleCos(v));}
Type angleDeg(const _CVector & v) const {return toDeg(angleRad(v));} Type angleDeg(const _CVector & v) const {return toDeg<Type>(angleRad(v));}
Type angleElevation(const _CVector & v) const {return 90.0 - angleDeg(v - *this);} Type angleElevation(const _CVector & v) const {return 90.0 - angleDeg(v - *this);}
_CVector projection(const _CVector & v) { _CVector projection(const _CVector & v) {
Type tv = v.length(); Type tv = v.length();
@@ -99,13 +103,15 @@ public:
bool isNull() const {PIMV_FOR if (c[i] != Type(0)) return false; return true;} bool isNull() const {PIMV_FOR if (c[i] != Type(0)) return false; return true;}
bool isOrtho(const _CVector & v) const {return ((*this) ^ v) == Type(0);} bool isOrtho(const _CVector & v) const {return ((*this) ^ v) == Type(0);}
Type & at(uint index) {return c[index];}
Type at(uint index) const {return c[index];}
Type & operator [](uint index) {return c[index];} Type & operator [](uint index) {return c[index];}
Type operator [](uint index) const {return c[index];} const Type & operator [](uint index) const {return c[index];}
Type at(uint index) const {return c[index];}
_CVector & operator =(const Type & v) {PIMV_FOR c[i] = v; return *this;} _CVector & operator =(const Type & v) {PIMV_FOR c[i] = v; return *this;}
bool operator ==(const _CVector & v) const {PIMV_FOR if (c[i] != v[i]) return false; return true;} bool operator ==(const _CVector & v) const {PIMV_FOR if (c[i] != v[i]) return false; return true;}
bool operator !=(const _CVector & v) const {return !(*this == c);} bool operator !=(const _CVector & v) const {return !(*this == c);}
void operator +=(const _CVector & v) {PIMV_FOR c[i] += v[i];} void operator +=(const _CVector & v) {PIMV_FOR c[i] += v[i];}
void operator -=(const _CVector & v) {PIMV_FOR c[i] -= v[i];} void operator -=(const _CVector & v) {PIMV_FOR c[i] -= v[i];}
void operator *=(const Type & v) {PIMV_FOR c[i] *= v;} void operator *=(const Type & v) {PIMV_FOR c[i] *= v;}
@@ -287,8 +293,8 @@ public:
PIMV_FOR c[i] += v[i]; PIMV_FOR c[i] += v[i];
return *this; return *this;
} }
_CVector & swapElements(uint fe, uint se) { _CVector & swapElements(uint f, uint s) {
piSwap<Type>(c[fe], c[se]); piSwap<Type>(c[f], c[s]);
return *this; return *this;
} }
Type lengthSqr() const { Type lengthSqr() const {
@@ -296,7 +302,7 @@ public:
PIMV_FOR tv += c[i] * c[i]; PIMV_FOR tv += c[i] * c[i];
return tv; return tv;
} }
Type length() const {return sqrt(lengthSqr());} Type length() const {return std::sqrt(lengthSqr());}
Type manhattanLength() const { Type manhattanLength() const {
Type tv(0); Type tv(0);
PIMV_FOR tv += piAbs<Type>(c[i]); PIMV_FOR tv += piAbs<Type>(c[i]);
@@ -311,9 +317,9 @@ public:
Type angleSin(const _CVector & v) const { Type angleSin(const _CVector & v) const {
assert(c.size() == v.size()); assert(c.size() == v.size());
Type tv = angleCos(v); Type tv = angleCos(v);
return sqrt(Type(1) - tv * tv); return std::sqrt(Type(1) - tv * tv);
} }
Type angleRad(const _CVector & v) const {return acos(angleCos(v));} Type angleRad(const _CVector & v) const {return std::acos(angleCos(v));}
Type angleDeg(const _CVector & v) const {return toDeg(angleRad(v));} Type angleDeg(const _CVector & v) const {return toDeg(angleRad(v));}
_CVector projection(const _CVector & v) { _CVector projection(const _CVector & v) {
assert(c.size() == v.size()); assert(c.size() == v.size());
@@ -340,12 +346,15 @@ public:
bool isValid() const {return !c.isEmpty();} bool isValid() const {return !c.isEmpty();}
bool isOrtho(const _CVector & v) const {return dot(v) == Type(0);} bool isOrtho(const _CVector & v) const {return dot(v) == Type(0);}
Type & at(uint index) {return c[index];}
Type at(uint index) const {return c[index];}
Type & operator [](uint index) {return c[index];} Type & operator [](uint index) {return c[index];}
Type operator [](uint index) const {return c[index];} const Type & operator [](uint index) const {return c[index];}
Type at(uint index) const {return c[index];}
_CVector & operator =(const Type & v) {PIMV_FOR c[i] = v; return *this;}
bool operator ==(const _CVector & v) const {return c == v.c;} bool operator ==(const _CVector & v) const {return c == v.c;}
bool operator !=(const _CVector & v) const {return c != v.c;} bool operator !=(const _CVector & v) const {return c != v.c;}
void operator +=(const _CVector & v) { void operator +=(const _CVector & v) {
assert(c.size() == v.size()); assert(c.size() == v.size());
PIMV_FOR c[i] += v[i]; PIMV_FOR c[i] += v[i];

View File

@@ -45,5 +45,9 @@ int main() {
PIMathVectord v2({3,2,1}); PIMathVectord v2({3,2,1});
piCout << v; piCout << v;
piCout << v2; piCout << v2;
PIMathMatrixT22d m = PIMathMatrixT22d::identity();
piCout << m;
m.rotate(toRad(90.));
piCout << m;
return 0; return 0;
} }