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