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