inline PIBinaryStream & operator <<(PIBinaryStream
& s, const PIMathMatrix & v) {
s << (const PIVector2D &) v;
return s;
}
/**
* \brief Inline operator to deserialize matrix from PIByteArray
*
* @param s PIByteArray type
* @param v PIMathMatrix type
* @return PIMathMatrix deserialized from PIByteArray
*/
template
inline PIBinaryStream & operator >>(PIBinaryStream
& s, PIMathMatrix & v) {
s >> (PIVector2D &) v;
return s;
}
/**
* \brief Multiplying matrices by each other. If you enter an index out of the border of the matrix there will be "undefined behavior"
*
* @param fm first matrix multiplier
* @param sm second matrix multiplier
* @return matrix that is the result of multiplication
*/
template
inline PIMathMatrix operator*(const PIMathMatrix &fm,
const PIMathMatrix &sm) {
assert(fm.cols() == sm.rows());
PIMathMatrix tm(sm.cols(), fm.rows());
Type t;
for (uint j = 0; j < fm.rows(); ++j) {
for (uint i = 0; i < sm.cols(); ++i) {
t = Type(0);
for (uint k = 0; k < fm.cols(); ++k)
t += fm.element(j, k) * sm.element(k, i);
tm.element(j, i) = t;
}
}
return tm;
}
/**
* \brief Multiplying matrix and vector. If you enter an index out of the border of the matrix there will be "undefined behavior"
*
* @param fm first matrix multiplier
* @param sv second vector multiplier
* @return vector that is the result of multiplication
*/
template
inline PIMathVector operator*(const PIMathMatrix &fm,
const PIMathVector &sv) {
assert(fm.cols() == sv.size());
PIMathVector tv(fm.rows());
Type t;
for (uint j = 0; j < fm.rows(); ++j) {
t = Type(0);
for (uint i = 0; i < fm.cols(); ++i)
t += fm.element(j, i) * sv[i];
tv[j] = t;
}
return tv;
}
/**
* \brief Multiplying vector and matrix. If you enter an index out of the border of the matrix there will be "undefined behavior"
*
* @param sv first vector multiplier
* @param fm second matrix multiplier
* @return vector that is the result of multiplication
*/
template
inline PIMathVector operator*(const PIMathVector &sv,
const PIMathMatrix &fm) {
PIMathVector tv(fm.cols());
assert(fm.rows() == sv.size());
Type t;
for (uint j = 0; j < fm.cols(); ++j) {
t = Type(0);
for (uint i = 0; i < fm.rows(); ++i)
t += fm.element(i, j) * sv[i];
tv[j] = t;
}
return tv;
}
/**
* \brief Multiplying value of type Type and matrix
*
* @param x first multiplier of type Type
* @param v second matrix multiplier
* @return matrix that is the result of multiplication
*/
template
inline PIMathMatrix operator*(const Type &x, const PIMathMatrix &v) {
return v * x;
}
typedef PIMathMatrix PIMathMatrixi;
typedef PIMathMatrix PIMathMatrixd;
/**
* \brief Searching hermitian matrix
*
* @param m conjugate transpose matrix
* @return result of the hermitian
*/
template
PIMathMatrix > hermitian(const PIMathMatrix > &m) {
PIMathMatrix > ret(m);
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();
}
#undef PIMM_FOR
#undef PIMM_FOR_A
#undef PIMM_FOR_C
#undef PIMM_FOR_R
#endif // PIMATHMATRIX_H