/* * Advanced Simulation Library * * Copyright 2015 Avtech Scientific * * * This file is part of Advanced Simulation Library (ASL). * * ASL is free software: you can redistribute it and/or modify it * under the terms of the GNU Affero General Public License as * published by the Free Software Foundation, version 3 of the License. * * ASL is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with ASL. If not, see . * */ /// \file aslMatrices.h Matrices #ifndef ASLMATRICES #define ASLMATRICES #include "../aslUtilities.h" #include "aslVectors.h" namespace asl { /// class algebraic matrix. /// The class is an implementation of a dynamic matrix with defined algebraic operations template class AMatr { private: unsigned int nRow; unsigned int nCol; AVec internalVec; public: inline AMatr(); inline AMatr(unsigned int nR, unsigned int nC); inline AMatr(const AMatr &a); inline AMatr(unsigned int nR, unsigned int nC, AVec v); template AMatr(const AMatr &a); const AMatr & operator=(const AMatr & a); ///doesn't chek boundaries inline T& operator()(int i, int j) {return internalVec[i*nCol+j];} ///doesn't chek boundaries inline const T& operator()(int i, int j)const {return internalVec[i*nCol+j];} ///doesn't chek boundaries inline T& operator[](int i) {return internalVec[i];} ///doesn't chek boundaries inline const T& operator[](int i)const {return internalVec[i];} inline unsigned int getNRow() const; inline unsigned int getNCol() const; inline void resize(unsigned int nR, unsigned int nCol); inline const AVec & getInternalVec() const; inline AVec & getInternalVec(); void setRow(unsigned int r, const AVec & a); void setColumn(unsigned int c, const AVec & a); }; /// \relates AMatr template std::ostream& operator<<(std::ostream &f,const AMatr & a); /// \relates AMatr template inline const AMatr & operator+=(AMatr & a, const AMatr & b); /// \relates AMatr template inline const AMatr operator+ (const AMatr & a, const AMatr & b); /// \relates AMatr template inline const AMatr operator- (const AMatr & a, const AMatr & b); /// \relates AMatr template const AMatr operator* (const AMatr &a, const AMatr & b); /// \relates AMatr template const AVec operator* (const AMatr &a, const AVec & b); /// \relates AMatr template const AVec operator* (const AVec &a, const AMatr & b); /// \relates AMatr template const AMatr operator* (const AMatr &a, const T & b); /// \relates AMatr template const AMatr operator* (const T &a, const AMatr & b); ///Trace of a matrix \f$Tr(A)\equiv A_{ii}\f$ \relates AMatr template const T trace(const AMatr &a); ///Trace of a matrix product \f$Tr(A B)\equiv A_{ij}B_{ji}\f$ \relates AMatr template const T trace(const AMatr & a, const AMatr & b); /// \relates AMatr template inline const AMatr operator/ (const AMatr & b, const T & a); ///element product of two vectors /** \relates AMatr \f$ elementProduct\left( \left[\begin{array}{c} a_1\\ \vdots \\ a_n \end{array}\right], \left[\begin{array}{c} b_1\\ \vdots \\ b_n \end{array}\right] = \left[\begin{array}{ccc} a_1b_1 & \cdots & a_1b_n\\ \vdots & \ddots & \vdots\\ a_nb_1 & \cdots & a_nb_n\\ \end{array}\right] \right) \f$ */ template AMatr elementProduct(const AVec & a, const AVec & b); /// generates a matrix with a row \relates AMatr template AMatr makeAMatr(const AVec & a); /// generates a matrix with two rows \relates AMatr template AMatr makeAMatr(const AVec & a, const AVec & b); /// generates a matrix with three rows \relates AMatr template AMatr makeAMatr(const AVec & a, const AVec & b, const AVec & c); /// generates a matrix with \p n rows \relates AMatr template AMatr makeAMatr(AVec *a, unsigned int n); /// \relates AMatr template AMatr makeAMatrUnit(unsigned int n); /// returns AVec containing the diagonal elements /** \relates AMatr the finction is valid only for square matrices */ template AVec getDiagonal(const AMatr & a); /// returns AVec containing the uper off diagonal elements /** \relates AMatr the function is valid only for square matrices \todo implement */ template AVec getOffDiagonalUp(const AMatr & a); /// computes determinant expression fo cases 2x2 and 3x3 only /** \relates AMatr */ template T det(const AMatr & m); /// returns solution of a system of linear equations /** \ingroup ComplexDataTypes */ template AVec solveSystem(const AMatr & a, const AVec & b); /// generate matrix with content of the matrix \p a but with replaced row \p r by vector \p b \relates AMatr template AMatr replaceRow(const AMatr & a, const AVec & b, unsigned int r); /// generate matrix with content of the matrix \p a but with replaced column \p c by vector \p b \relates AMatr template AMatr replaceColumn(const AMatr & a, const AVec & b, unsigned int c); /// returns inverse matrix for cases 2x2 and 3x3 \relates AMatr template AMatr inverseMatrix(const AMatr & a); // ------------------------------ Implementation ----------------- template inline AMatr::AMatr(): nRow(0), nCol(0) { } template inline AMatr::AMatr(unsigned int nR, unsigned int nC): nRow(nR), nCol(nC), internalVec(nR*nC) { } template inline AMatr::AMatr(const AMatr &a): nRow(a.nRow), nCol(a.nCol), internalVec(a.internalVec) { } template inline AMatr::AMatr(unsigned int nR, unsigned int nC, AVec v): nRow(nR), nCol(nC), internalVec(v) { } template inline AVec & AMatr::getInternalVec() { return internalVec; } template inline const AVec & AMatr::getInternalVec() const { return internalVec; } template inline unsigned int AMatr::getNRow() const { return nRow; } template inline unsigned int AMatr::getNCol() const { return nCol; } template inline void AMatr::resize(unsigned int nr, unsigned int nc) { nRow=nr; nCol=nc; internalVec.resize(nr*nc); } template inline const AMatr & operator+=(AMatr & a, const AMatr & b) { a.getInternalVec += b.getInternalVec; return a; } template inline const AMatr operator+ (const AMatr & a, const AMatr & b) { return {a.getNRow(),a.getNCol(),a.getInternalVec() + b.getInternalVec()}; } template inline const AMatr operator- (const AMatr & a,const AMatr & b) { return {a.getNRow(),a.getNCol(),a.getInternalVec() - b.getInternalVec()}; } template inline const AMatr operator/ (const AMatr & b, const T & a) { return {b.getNRow(), b.getNCol(), b.getInternalVec() / a}; } /// Eigenvalues and eigenvectors calcutaion for symetric matrix 2x2 /** \param a matrix element \param b matrix element \param c matrix element \param l1 first eigenvalue \param l2 second eigenvalue \param v1x x component of first eigenvector \param v1y y component of first eigenvector \param v2x x component of second eigenvector \param v2y y component of second eigenvector \f[ A= \left| \begin{array}{cc} a & c \\ c & b \end{array}\right| \f] */ void getEValEVecMatSym2x2(double a, double b, double c, double & l1, double & l2, double & v1x, double & v1y, double & v2x, double & v2y); /// Eigenvalues and eigenvectors calcutaion for symetric matrix 2x2 /** \param a matrix element \param b matrix element \param c matrix element \param l1 first eigenvalue \param l2 second eigenvalue \param v1x x component of first eigenvector \param v1y y component of first eigenvector \param v2x x component of second eigenvector \param v2y y component of second eigenvector \f[ A= \left| \begin{array}{cc} a & c \\ c & b \end{array}\right| \f] */ void getEValEVecMatSym2x2(double a, double b, double c, double & l1, double & l2, double & v1x, double & v1y, double & v2x, double & v2y); /// Eigenvalues and eigenvectors calcutaion for symetric matrix 2x2 /** \param a matrix element \param b matrix element \param c matrix element \param d matrix element \param e matrix element \param f matrix element \param l1 first eigenvalue \param l2 second eigenvalue \param l3 second eigenvalue \param v1x x component of first eigenvector \param v1y y component of first eigenvector \param v1z z component of first eigenvector \param v2x x component of second eigenvector \param v2y y component of second eigenvector \param v2z z component of second eigenvector \param v3x x component of second eigenvector \param v3y y component of second eigenvector \param v3z z component of second eigenvector \f[ A= \left| \begin{array}{ccc} a & d & e\\ d & b & f \\ e & f & c \\ \end{array}\right| \f] */ void getEValEVecMatSym3x3(double a, double b, double c, double e, double f, double g, double & l1, double & l2, double & l3, double & v1x, double & v1y, double & v1z, double & v2x, double & v2y, double & v2z, double & v3x, double & v3y, double & v3z); } // asl #endif //ASLMATRICES