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 | |
---|
16 | #include <itpp/itbase.h> |
---|
17 | #include "libDC.h" |
---|
18 | |
---|
19 | using namespace itpp; |
---|
20 | |
---|
21 | /*! \brief Symmetric matrix stored in square root decomposition using upper cholesky |
---|
22 | |
---|
23 | This matrix represent \f$A=Ch' Ch\f$ where only the upper triangle \f$Ch\f$ is stored; |
---|
24 | |
---|
25 | */ |
---|
26 | class chmat : public sqmat { |
---|
27 | protected: |
---|
28 | //! Upper triangle of the cholesky matrix |
---|
29 | mat Ch; |
---|
30 | public: |
---|
31 | |
---|
32 | void opupdt ( const vec &v, double w ); |
---|
33 | mat to_mat(); |
---|
34 | void mult_sym ( const mat &C ); |
---|
35 | void mult_sym ( const mat &C , chmat &U ) const; |
---|
36 | void mult_sym_t ( const mat &C ); |
---|
37 | void mult_sym_t ( const mat &C, chmat &U ) const; |
---|
38 | double logdet() const; |
---|
39 | vec sqrt_mult ( const vec &v ) const; |
---|
40 | double qform ( const vec &v ) const; |
---|
41 | double invqform ( const vec &v ) const; |
---|
42 | void clear(); |
---|
43 | void add ( const chmat &A2, double w=1.0 ) {}; |
---|
44 | //!Inversion in the same form, i.e. cholesky |
---|
45 | void inv ( chmat &Inv ) const { ( Inv.Ch ) = itpp::inv ( Ch ).T();}; //Fixme: can be more efficient |
---|
46 | ; |
---|
47 | // void inv ( mat &Inv ); |
---|
48 | |
---|
49 | //! Destructor for future use; |
---|
50 | virtual ~chmat() {}; |
---|
51 | //! Default constructor |
---|
52 | chmat ( const int dim0 ) : sqmat ( dim0 ),Ch ( dim0,dim0 ) {}; |
---|
53 | //! Default constructor |
---|
54 | chmat ( const vec &v) : sqmat ( v.length() ),Ch ( diag(sqrt(v)) ) {}; |
---|
55 | //! Copy constructor |
---|
56 | chmat ( const chmat &Ch0) : sqmat ( Ch0.dim),Ch ( Ch0.dim,Ch0.dim ) {Ch=Ch0.Ch;}; |
---|
57 | //! Default constructor (m3k:cholform) |
---|
58 | chmat ( const mat &M ) : sqmat ( M.rows() ),Ch ( M.rows(),M.cols() ) { |
---|
59 | mat Q; |
---|
60 | it_assert_debug ( M.rows() ==M.cols(),"chmat:: input matrix must be square!" ); |
---|
61 | Ch=chol ( M ); |
---|
62 | }; |
---|
63 | //! Access function |
---|
64 | mat & _Ch() {return Ch;} |
---|
65 | //! Access functions |
---|
66 | void setD ( const vec &nD ) {Ch=diag ( sqrt(nD) );} |
---|
67 | //! Access functions |
---|
68 | void setD ( const vec &nD, int i ) {for ( int j=i;j<nD.length();j++ ) {Ch( j,j ) =sqrt(nD ( j-i ));}} //Fixme can be more general |
---|
69 | |
---|
70 | //! Operators |
---|
71 | chmat& operator += ( const chmat &A2 ); |
---|
72 | chmat& operator -= ( const chmat &A2 ) ; |
---|
73 | }; |
---|
74 | |
---|
75 | |
---|
76 | //////// Operations: |
---|
77 | //!mapping of add operation to operators |
---|
78 | inline chmat& chmat::operator += ( const chmat &A2 ) {this->add ( A2 );return *this;} |
---|
79 | //!mapping of negative add operation to operators |
---|
80 | inline chmat& chmat::operator -= ( const chmat &A2 ) {this->add ( A2,-1.0 );return *this;} |
---|
81 | #endif // CHMAT_H |
---|