root/library/bdm/math/chmat.h @ 725

Revision 725, 4.0 kB (checked in by smidl, 15 years ago)

Sampling from egiw

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