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

Revision 495, 3.5 kB (checked in by vbarta, 15 years ago)

moved square matrices to namespace bdm

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