[39] | 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 CHMAT_H |
---|
| 14 | #define CHMAT_H |
---|
| 15 | |
---|
[565] | 16 | #include "../bdmerror.h" |
---|
[384] | 17 | #include "square_mat.h" |
---|
[39] | 18 | |
---|
[495] | 19 | namespace bdm |
---|
| 20 | { |
---|
| 21 | |
---|
[39] | 22 | /*! \brief Symmetric matrix stored in square root decomposition using upper cholesky |
---|
| 23 | |
---|
[76] | 24 | This matrix represent \f$A=Ch' Ch\f$ where only the upper triangle \f$Ch\f$ is stored; |
---|
[39] | 25 | |
---|
| 26 | */ |
---|
| 27 | class chmat : public sqmat { |
---|
| 28 | protected: |
---|
| 29 | //! Upper triangle of the cholesky matrix |
---|
| 30 | mat Ch; |
---|
| 31 | public: |
---|
| 32 | |
---|
| 33 | void opupdt ( const vec &v, double w ); |
---|
[168] | 34 | mat to_mat() const; |
---|
[39] | 35 | void mult_sym ( const mat &C ); |
---|
[660] | 36 | //! mult_sym with return value in parameter \c U |
---|
[39] | 37 | void mult_sym ( const mat &C , chmat &U ) const; |
---|
| 38 | void mult_sym_t ( const mat &C ); |
---|
[660] | 39 | //! mult_sym with return value in parameter \c U |
---|
[39] | 40 | void mult_sym_t ( const mat &C, chmat &U ) const; |
---|
| 41 | double logdet() const; |
---|
[108] | 42 | vec sqrt_mult ( const vec &v ) const; |
---|
| 43 | double qform ( const vec &v ) const; |
---|
| 44 | double invqform ( const vec &v ) const; |
---|
| 45 | void clear(); |
---|
[98] | 46 | //! add another chmat \c A2 with weight \c w. |
---|
[477] | 47 | void add ( const chmat &A2, double w = 1.0 ) { |
---|
[565] | 48 | bdm_assert_debug ( dim == A2.dim, "Matrices of unequal dimension" ); |
---|
[477] | 49 | mat pre = concat_vertical ( Ch, sqrt ( w ) * A2.Ch ); |
---|
| 50 | mat post = zeros ( pre.rows(), pre.cols() ); |
---|
| 51 | if ( !qr ( pre, post ) ) { |
---|
[565] | 52 | bdm_warning ( "Unstable QR in chmat add" ); |
---|
[477] | 53 | } |
---|
| 54 | Ch = post ( 0, dim - 1, 0, dim - 1 ); |
---|
[437] | 55 | }; |
---|
[39] | 56 | //!Inversion in the same form, i.e. cholesky |
---|
[477] | 57 | void inv ( chmat &Inv ) const { |
---|
| 58 | ( Inv.Ch ) = itpp::inv ( Ch ).T(); |
---|
| 59 | }; //Fixme: can be more efficient |
---|
[39] | 60 | ; |
---|
| 61 | // void inv ( mat &Inv ); |
---|
| 62 | |
---|
| 63 | //! Destructor for future use; |
---|
| 64 | virtual ~chmat() {}; |
---|
[270] | 65 | //! |
---|
[477] | 66 | chmat ( ) : sqmat (), Ch ( ) {}; |
---|
[39] | 67 | //! Default constructor |
---|
[477] | 68 | chmat ( const int dim0 ) : sqmat ( dim0 ), Ch ( dim0, dim0 ) {}; |
---|
[39] | 69 | //! Default constructor |
---|
[477] | 70 | chmat ( const vec &v ) : sqmat ( v.length() ), Ch ( diag ( sqrt ( v ) ) ) {}; |
---|
[76] | 71 | //! Copy constructor |
---|
[477] | 72 | chmat ( const chmat &Ch0 ) : sqmat ( Ch0.dim ), Ch ( Ch0.dim, Ch0.dim ) { |
---|
| 73 | Ch = Ch0.Ch; |
---|
[565] | 74 | } |
---|
| 75 | |
---|
[39] | 76 | //! Default constructor (m3k:cholform) |
---|
[477] | 77 | chmat ( const mat &M ) : sqmat ( M.rows() ), Ch ( M.rows(), M.cols() ) { |
---|
[39] | 78 | mat Q; |
---|
[565] | 79 | bdm_assert_debug ( M.rows() == M.cols(), "chmat:: input matrix must be square!" ); |
---|
[477] | 80 | Ch = chol ( M ); |
---|
[565] | 81 | } |
---|
| 82 | |
---|
| 83 | /*! |
---|
| 84 | Some templates require this constructor to compile, but |
---|
| 85 | it shouldn't actually be called. |
---|
| 86 | */ |
---|
| 87 | chmat ( const chmat &M, const ivec &perm ) { |
---|
| 88 | bdm_error ( "not implemented" ); |
---|
| 89 | } |
---|
| 90 | |
---|
[39] | 91 | //! Access function |
---|
[477] | 92 | mat & _Ch() { |
---|
| 93 | return Ch; |
---|
| 94 | } |
---|
[294] | 95 | //! Access function |
---|
[477] | 96 | const mat & _Ch() const { |
---|
| 97 | return Ch; |
---|
| 98 | } |
---|
[39] | 99 | //! Access functions |
---|
[477] | 100 | void setD ( const vec &nD ) { |
---|
| 101 | Ch = diag ( sqrt ( nD ) ); |
---|
| 102 | } |
---|
[39] | 103 | //! Access functions |
---|
[294] | 104 | void setCh ( const vec &chQ ) { |
---|
[565] | 105 | bdm_assert_debug ( chQ.length() == dim * dim, "wrong length" ); |
---|
[477] | 106 | copy_vector ( dim*dim, chQ._data(), Ch._data() ); |
---|
[294] | 107 | } |
---|
[565] | 108 | |
---|
[294] | 109 | //! Access functions |
---|
[477] | 110 | void setD ( const vec &nD, int i ) { |
---|
| 111 | for ( int j = i; j < nD.length(); j++ ) { |
---|
| 112 | Ch ( j, j ) = sqrt ( nD ( j - i ) ); //Fixme can be more general |
---|
| 113 | } |
---|
| 114 | } |
---|
[39] | 115 | |
---|
[660] | 116 | //! Operator |
---|
[39] | 117 | chmat& operator += ( const chmat &A2 ); |
---|
[660] | 118 | //! Operator |
---|
[437] | 119 | chmat& operator -= ( const chmat &A2 ); |
---|
[660] | 120 | //! Operator |
---|
[477] | 121 | chmat& operator * ( const double &d ) { |
---|
| 122 | Ch*sqrt ( d ); |
---|
| 123 | return *this; |
---|
| 124 | }; |
---|
[660] | 125 | //! Operator |
---|
[477] | 126 | chmat& operator = ( const chmat &A2 ) { |
---|
| 127 | Ch = A2.Ch; |
---|
| 128 | dim = A2.dim; |
---|
| 129 | return *this; |
---|
| 130 | } |
---|
[660] | 131 | //! Operator |
---|
[477] | 132 | chmat& operator *= ( double x ) { |
---|
| 133 | Ch *= sqrt ( x ); |
---|
| 134 | return *this; |
---|
| 135 | }; |
---|
[39] | 136 | }; |
---|
| 137 | |
---|
| 138 | |
---|
| 139 | //////// Operations: |
---|
| 140 | //!mapping of add operation to operators |
---|
[477] | 141 | inline chmat& chmat::operator += ( const chmat & A2 ) { |
---|
| 142 | this->add ( A2 ); |
---|
| 143 | return *this; |
---|
| 144 | } |
---|
[39] | 145 | //!mapping of negative add operation to operators |
---|
[477] | 146 | inline chmat& chmat::operator -= ( const chmat & A2 ) { |
---|
| 147 | this->add ( A2, -1.0 ); |
---|
| 148 | return *this; |
---|
| 149 | } |
---|
[254] | 150 | |
---|
[495] | 151 | } |
---|
| 152 | |
---|
[39] | 153 | #endif // CHMAT_H |
---|