| 1 | /*! |
|---|
| 2 | * \file |
|---|
| 3 | * \brief Matrices in decomposed forms (LDL', LU, UDU', etc). |
|---|
| 4 | * \author Vaclav Smidl. |
|---|
| 5 | * |
|---|
| 6 | * ----------------------------------- |
|---|
| 7 | * BDM++ - C++ library for Bayesian Decision Making under Uncertainty |
|---|
| 8 | * |
|---|
| 9 | * Using IT++ for numerical operations |
|---|
| 10 | * ----------------------------------- |
|---|
| 11 | */ |
|---|
| 12 | |
|---|
| 13 | #ifndef DC_H |
|---|
| 14 | #define DC_H |
|---|
| 15 | |
|---|
| 16 | #include <itpp/itbase.h> |
|---|
| 17 | |
|---|
| 18 | using namespace itpp; |
|---|
| 19 | |
|---|
| 20 | /*! \brief Virtual class for representation of double symmetric matrices in square-root form. |
|---|
| 21 | |
|---|
| 22 | All operations defined on this class should be optimized for the chosed decomposition. |
|---|
| 23 | */ |
|---|
| 24 | class sqmat { |
|---|
| 25 | public: |
|---|
| 26 | /*! |
|---|
| 27 | * Perfroms a rank-1 update by outer product of vectors: $V = V + w v v'$. |
|---|
| 28 | * @param v Vector forming the outer product to be added |
|---|
| 29 | * @param w weight of updating; can be negative |
|---|
| 30 | |
|---|
| 31 | BLAS-2b operation. |
|---|
| 32 | */ |
|---|
| 33 | virtual void opupdt( const vec &v, double w ) =0; |
|---|
| 34 | |
|---|
| 35 | /*! \brief Conversion to full matrix. |
|---|
| 36 | */ |
|---|
| 37 | |
|---|
| 38 | virtual mat to_mat() =0; |
|---|
| 39 | |
|---|
| 40 | /*! \brief Inplace symmetric multiplication by a SQUARE matrix $C$, i.e. $V = C*V*C'$ |
|---|
| 41 | @param C multiplying matrix, |
|---|
| 42 | @param trans if true, product $V = C'*V*C$ will be computed instead; |
|---|
| 43 | */ |
|---|
| 44 | virtual void mult_sym( const mat &C, bool trans=true ) =0; |
|---|
| 45 | |
|---|
| 46 | |
|---|
| 47 | /*! |
|---|
| 48 | \brief Logarithm of a determinant. |
|---|
| 49 | |
|---|
| 50 | */ |
|---|
| 51 | virtual double logdet() =0; |
|---|
| 52 | |
|---|
| 53 | /*! |
|---|
| 54 | \brief Evaluates quadratic form $x= v'*V*v$; |
|---|
| 55 | |
|---|
| 56 | */ |
|---|
| 57 | virtual double qform(vec &v) =0; |
|---|
| 58 | |
|---|
| 59 | // //! easy version of the |
|---|
| 60 | // sqmat inv(); |
|---|
| 61 | |
|---|
| 62 | friend std::ostream &operator<< ( std::ostream &os, sqmat &sq ); |
|---|
| 63 | |
|---|
| 64 | //! Clearing matrix so that it corresponds to zeros. |
|---|
| 65 | virtual void clear() =0; |
|---|
| 66 | |
|---|
| 67 | //! Reimplementing common functions of mat: cols(). |
|---|
| 68 | virtual int cols() =0; |
|---|
| 69 | |
|---|
| 70 | //! Reimplementing common functions of mat: cols(). |
|---|
| 71 | virtual int rows() =0; |
|---|
| 72 | |
|---|
| 73 | }; |
|---|
| 74 | |
|---|
| 75 | |
|---|
| 76 | /*! \brief Fake sqmat. This class maps sqmat operations to operations on full matrix. |
|---|
| 77 | |
|---|
| 78 | This class can be used to compare performance of algorithms using decomposed matrices with perormance of the same algorithms using full matrices; |
|---|
| 79 | */ |
|---|
| 80 | class fsqmat: sqmat { |
|---|
| 81 | void opupdt( const vec &v, double w ); |
|---|
| 82 | mat to_mat(); |
|---|
| 83 | void mult_sym( const mat &C, bool trans=false ); |
|---|
| 84 | void mult_sym( const mat &C, fsqmat &U, bool trans=false ); |
|---|
| 85 | void inv(fsqmat &Inv); |
|---|
| 86 | void clear(); |
|---|
| 87 | |
|---|
| 88 | //! Constructor |
|---|
| 89 | fsqmat(const mat &M); |
|---|
| 90 | |
|---|
| 91 | /*! \brief Matrix inversion preserving the chosen form. |
|---|
| 92 | |
|---|
| 93 | @param Inv a space where the inverse is stored. |
|---|
| 94 | |
|---|
| 95 | */ |
|---|
| 96 | virtual void inv(fsqmat* Inv); |
|---|
| 97 | }; |
|---|
| 98 | |
|---|
| 99 | class ldmat: sqmat { |
|---|
| 100 | public: |
|---|
| 101 | |
|---|
| 102 | //! Construct by copy of L and D. |
|---|
| 103 | ldmat( const mat &L, const vec &D ); |
|---|
| 104 | //! Construct by decomposition of full matrix V. |
|---|
| 105 | ldmat( mat V ); |
|---|
| 106 | ldmat (); |
|---|
| 107 | |
|---|
| 108 | // Reimplementation of compulsory operatios |
|---|
| 109 | |
|---|
| 110 | void opupdt( const vec &v, double w ); |
|---|
| 111 | mat to_mat(); |
|---|
| 112 | void mult_sym( const mat &C, bool trans=false ); |
|---|
| 113 | void add ( const ldmat &ld2, double w=1.0 ); |
|---|
| 114 | double logdet(); |
|---|
| 115 | double qform(vec &v); |
|---|
| 116 | // sqmat& operator -= ( const sqmat & ld2 ); |
|---|
| 117 | void clear(); |
|---|
| 118 | int cols(); |
|---|
| 119 | int rows(); |
|---|
| 120 | |
|---|
| 121 | /*! \brief Matrix inversion preserving the chosen form. |
|---|
| 122 | |
|---|
| 123 | @param Inv a space where the inverse is stored. |
|---|
| 124 | |
|---|
| 125 | */ |
|---|
| 126 | virtual void inv(ldmat &Inv); |
|---|
| 127 | |
|---|
| 128 | /*! \brief Symmetric multiplication of $U$ by a general matrix $C$, result of which is stored in the current class. |
|---|
| 129 | |
|---|
| 130 | @param Inv a space where the inverse is stored. |
|---|
| 131 | |
|---|
| 132 | */ |
|---|
| 133 | void mult_sym( const mat &C, ldmat &U, bool trans=false ); |
|---|
| 134 | |
|---|
| 135 | ldmat& operator += (const ldmat &ldA); |
|---|
| 136 | ldmat& operator -= (const ldmat &ldA); |
|---|
| 137 | ldmat& operator *= (double x); |
|---|
| 138 | |
|---|
| 139 | protected: |
|---|
| 140 | vec D; |
|---|
| 141 | mat L; |
|---|
| 142 | |
|---|
| 143 | }; |
|---|
| 144 | |
|---|
| 145 | //////// Operations: |
|---|
| 146 | |
|---|
| 147 | inline ldmat& ldmat::operator += (const ldmat &ldA) {this->add(ldA);return *this;} |
|---|
| 148 | inline ldmat& ldmat::operator -= (const ldmat &ldA) {this->add(ldA,-1.0);return *this;} |
|---|
| 149 | inline int ldmat::cols(){return L.cols();} |
|---|
| 150 | inline int ldmat::rows(){return L.rows();} |
|---|
| 151 | |
|---|
| 152 | #endif // DC_H |
|---|