| 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 multiplication by a matrix $C$ from both sides, 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_qform( const mat &C, bool trans=false ) =0; |
|---|
| 45 | |
|---|
| 46 | /*! |
|---|
| 47 | \brief Logarithm of a determinant. |
|---|
| 48 | |
|---|
| 49 | */ |
|---|
| 50 | virtual double logdet() =0; |
|---|
| 51 | |
|---|
| 52 | /*! |
|---|
| 53 | \brief Evaluates quadratic form $x= v'*V*v$; |
|---|
| 54 | |
|---|
| 55 | */ |
|---|
| 56 | virtual double qform(vec &v) =0; |
|---|
| 57 | |
|---|
| 58 | // //! easy version of the |
|---|
| 59 | // sqmat inv(); |
|---|
| 60 | |
|---|
| 61 | friend std::ostream &operator<< ( std::ostream &os, sqmat &sq ); |
|---|
| 62 | |
|---|
| 63 | //! Clearing matrix so that it corresponds to zeros. |
|---|
| 64 | virtual void clear() =0; |
|---|
| 65 | |
|---|
| 66 | //! Reimplementing common functions of mat: cols(). |
|---|
| 67 | virtual int cols() =0; |
|---|
| 68 | |
|---|
| 69 | //! Reimplementing common functions of mat: cols(). |
|---|
| 70 | virtual int rows() =0; |
|---|
| 71 | |
|---|
| 72 | }; |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | /*! \brief Fake sqmat. This class maps sqmat operations to operations on full matrix. |
|---|
| 76 | |
|---|
| 77 | This class can be used to compare performance of algorithms using decomposed matrices with perormance of the same algorithms using full matrices; |
|---|
| 78 | */ |
|---|
| 79 | class fsqmat: sqmat { |
|---|
| 80 | void opupdt( const vec &v, double w ); |
|---|
| 81 | mat to_mat(); |
|---|
| 82 | void mult_qform( const mat &C, bool trans=false ); |
|---|
| 83 | void inv(fsqmat &Inv); |
|---|
| 84 | void clear(); |
|---|
| 85 | |
|---|
| 86 | //! Constructor |
|---|
| 87 | fsqmat(const mat &M); |
|---|
| 88 | |
|---|
| 89 | /*! \brief Matrix inversion preserving the chosen form. |
|---|
| 90 | |
|---|
| 91 | @param Inv a space where the inverse is stored. |
|---|
| 92 | |
|---|
| 93 | */ |
|---|
| 94 | virtual void inv(fsqmat* Inv); |
|---|
| 95 | }; |
|---|
| 96 | |
|---|
| 97 | class ldmat: sqmat { |
|---|
| 98 | public: |
|---|
| 99 | |
|---|
| 100 | //! Construct by copy of L and D. |
|---|
| 101 | ldmat( const mat &L, const vec &D ); |
|---|
| 102 | //! Construct by decomposition of full matrix V. |
|---|
| 103 | ldmat( mat V ); |
|---|
| 104 | ldmat (); |
|---|
| 105 | |
|---|
| 106 | // Reimplementation of compulsory operatios |
|---|
| 107 | |
|---|
| 108 | void opupdt( const vec &v, double w ); |
|---|
| 109 | mat to_mat(); |
|---|
| 110 | void mult_qform( const mat &C, bool trans=false ); |
|---|
| 111 | void inv(sqmat* Inv); |
|---|
| 112 | void add ( const ldmat &ld2, double w=1.0 ); |
|---|
| 113 | double logdet(); |
|---|
| 114 | double qform(vec &v); |
|---|
| 115 | // sqmat& operator -= ( const sqmat & ld2 ); |
|---|
| 116 | void clear(); |
|---|
| 117 | int cols(); |
|---|
| 118 | int rows(); |
|---|
| 119 | |
|---|
| 120 | /*! \brief Matrix inversion preserving the chosen form. |
|---|
| 121 | |
|---|
| 122 | @param Inv a space where the inverse is stored. |
|---|
| 123 | |
|---|
| 124 | */ |
|---|
| 125 | virtual void inv(ldmat &Inv); |
|---|
| 126 | |
|---|
| 127 | ldmat& operator += (const ldmat &ldA); |
|---|
| 128 | ldmat& operator *= (double x); |
|---|
| 129 | |
|---|
| 130 | protected: |
|---|
| 131 | vec D; |
|---|
| 132 | mat L; |
|---|
| 133 | |
|---|
| 134 | }; |
|---|
| 135 | |
|---|
| 136 | //////// Operations: |
|---|
| 137 | |
|---|
| 138 | inline ldmat& ldmat::operator += (const ldmat &ldA) {this->add(ldA);return *this;} |
|---|
| 139 | inline int ldmat::cols(){return L.cols();} |
|---|
| 140 | inline int ldmat::rows(){return L.rows();} |
|---|
| 141 | |
|---|
| 142 | #endif // DC_H |
|---|