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

Revision 660, 3.8 kB (checked in by smidl, 15 years ago)

doc - doxygen warnings

  • 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        }; //Fixme: can be more efficient
60        ;
61//      void inv ( mat &Inv );
62
63        //! Destructor for future use;
64        virtual ~chmat() {};
65        //!
66        chmat ( ) : sqmat (), Ch ( ) {};
67        //! Default constructor
68        chmat ( const int dim0 ) : sqmat ( dim0 ), Ch ( dim0, dim0 ) {};
69        //! Default constructor
70        chmat ( const vec &v ) : sqmat ( v.length() ), Ch ( diag ( sqrt ( v ) ) ) {};
71        //! Copy constructor
72        chmat ( const chmat &Ch0 ) : sqmat ( Ch0.dim ), Ch ( Ch0.dim, Ch0.dim ) {
73                Ch = Ch0.Ch;
74        }
75
76        //! Default constructor (m3k:cholform)
77        chmat ( const mat &M ) : sqmat ( M.rows() ), Ch ( M.rows(), M.cols() ) {
78                mat Q;
79                bdm_assert_debug ( M.rows() == M.cols(), "chmat:: input matrix must be square!" );
80                Ch = chol ( M );
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
91        //! Access function
92        mat & _Ch() {
93                return Ch;
94        }
95        //! Access function
96        const mat & _Ch() const {
97                return Ch;
98        }
99        //! Access functions
100        void setD ( const vec &nD ) {
101                Ch = diag ( sqrt ( nD ) );
102        }
103        //! Access functions
104        void setCh ( const vec &chQ ) {
105                bdm_assert_debug ( chQ.length() == dim * dim, "wrong length" );
106                copy_vector ( dim*dim, chQ._data(), Ch._data() );
107        }
108
109        //! Access functions
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        }
115
116        //! Operator
117        chmat& operator += ( const chmat &A2 );
118        //! Operator
119        chmat& operator -= ( const chmat &A2 );
120        //! Operator
121        chmat& operator * ( const double &d ) {
122                Ch*sqrt ( d );
123                return *this;
124        };
125        //! Operator
126        chmat& operator = ( const chmat &A2 ) {
127                Ch = A2.Ch;
128                dim = A2.dim;
129                return *this;
130        }
131        //! Operator
132        chmat& operator *= ( double x ) {
133                Ch *= sqrt ( x );
134                return *this;
135        };
136};
137
138
139//////// Operations:
140//!mapping of add operation to operators
141inline chmat& chmat::operator += ( const chmat & A2 )  {
142        this->add ( A2 );
143        return *this;
144}
145//!mapping of negative add operation to operators
146inline chmat& chmat::operator -= ( const chmat & A2 )  {
147        this->add ( A2, -1.0 );
148        return *this;
149}
150
151}
152
153#endif // CHMAT_H
Note: See TracBrowser for help on using the browser.