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

Revision 565, 3.6 kB (checked in by vbarta, 15 years ago)

using own error macros (basically copied from IT++, but never aborting)

  • 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        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                bdm_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                        bdm_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
74        //! Default constructor (m3k:cholform)
75        chmat ( const mat &M ) : sqmat ( M.rows() ), Ch ( M.rows(), M.cols() ) {
76                mat Q;
77                bdm_assert_debug ( M.rows() == M.cols(), "chmat:: input matrix must be square!" );
78                Ch = chol ( M );
79        }
80
81        /*!
82          Some templates require this constructor to compile, but
83          it shouldn't actually be called.
84        */
85        chmat ( const chmat &M, const ivec &perm ) {
86                bdm_error ( "not implemented" );
87        }
88
89        //! Access function
90        mat & _Ch() {
91                return Ch;
92        }
93        //! Access function
94        const mat & _Ch() const {
95                return Ch;
96        }
97        //! Access functions
98        void setD ( const vec &nD ) {
99                Ch = diag ( sqrt ( nD ) );
100        }
101        //! Access functions
102        void setCh ( const vec &chQ ) {
103                bdm_assert_debug ( chQ.length() == dim * dim, "wrong length" );
104                copy_vector ( dim*dim, chQ._data(), Ch._data() );
105        }
106
107        //! Access functions
108        void setD ( const vec &nD, int i ) {
109                for ( int j = i; j < nD.length(); j++ ) {
110                        Ch ( j, j ) = sqrt ( nD ( j - i ) );    //Fixme can be more general
111                }
112        }
113
114        //! Operators
115        chmat& operator += ( const chmat &A2 );
116        chmat& operator -= ( const chmat &A2 );
117        chmat& operator * ( const double &d ) {
118                Ch*sqrt ( d );
119                return *this;
120        };
121        chmat& operator = ( const chmat &A2 ) {
122                Ch = A2.Ch;
123                dim = A2.dim;
124                return *this;
125        }
126        chmat& operator *= ( double x ) {
127                Ch *= sqrt ( x );
128                return *this;
129        };
130};
131
132
133//////// Operations:
134//!mapping of add operation to operators
135inline chmat& chmat::operator += ( const chmat & A2 )  {
136        this->add ( A2 );
137        return *this;
138}
139//!mapping of negative add operation to operators
140inline chmat& chmat::operator -= ( const chmat & A2 )  {
141        this->add ( A2, -1.0 );
142        return *this;
143}
144
145}
146
147#endif // CHMAT_H
Note: See TracBrowser for help on using the browser.