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

Revision 437, 3.3 kB (checked in by smidl, 15 years ago)

missing functions in chmat - square_matrix test now passes for chmat

  • 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
19/*! \brief Symmetric matrix stored in square root decomposition using upper cholesky
20
21This matrix represent \f$A=Ch' Ch\f$ where only the upper triangle \f$Ch\f$ is stored;
22
23*/
24class chmat : public sqmat {
25protected:
26//! Upper triangle of the cholesky matrix
27        mat Ch;
28public:
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)) {it_warning("Unstable QR in chmat add");}
47                Ch=post(0,dim-1,0,dim-1);
48        };
49        //!Inversion in the same form, i.e. cholesky
50        void inv ( chmat &Inv ) const   { ( Inv.Ch ) = itpp::inv ( Ch ).T();}; //Fixme: can be more efficient
51        ;
52//      void inv ( mat &Inv );
53
54        //! Destructor for future use;
55        virtual ~chmat() {};
56        //!
57        chmat ( ) : sqmat (),Ch ( ) {};
58        //! Default constructor
59        chmat ( const int dim0 ) : sqmat ( dim0 ),Ch ( dim0,dim0 ) {};
60        //! Default constructor
61        chmat ( const vec &v) : sqmat ( v.length() ),Ch ( diag(sqrt(v)) ) {};
62        //! Copy constructor
63        chmat ( const chmat &Ch0) : sqmat ( Ch0.dim),Ch ( Ch0.dim,Ch0.dim ) {Ch=Ch0.Ch;};
64        //! Default constructor (m3k:cholform)
65        chmat ( const mat &M ) : sqmat ( M.rows() ),Ch ( M.rows(),M.cols() ) {
66                mat Q;
67                it_assert_debug ( M.rows() ==M.cols(),"chmat:: input matrix must be square!" );
68                Ch=chol ( M );
69        };
70        //! Constructor
71        chmat ( const chmat &M, const ivec &perm ):sqmat(M.rows()){it_error("not implemneted");};
72        //! Access function
73        mat & _Ch() {return Ch;}
74        //! Access function
75        const mat & _Ch() const {return Ch;}
76        //! Access functions
77        void setD ( const vec &nD ) {Ch=diag ( sqrt(nD) );}
78        //! Access functions
79        void setCh ( const vec &chQ ) {
80                it_assert_debug(chQ.length()==dim*dim,""); 
81                copy_vector(dim*dim, chQ._data(), Ch._data()); 
82        }
83        //! Access functions
84        void setD ( const vec &nD, int i ) {for ( int j=i;j<nD.length();j++ ) {Ch( j,j ) =sqrt(nD ( j-i ));}} //Fixme can be more general
85
86        //! Operators
87        chmat& operator += ( const chmat &A2 );
88        chmat& operator -= ( const chmat &A2 );
89        chmat& operator * ( const double &d ){Ch*sqrt(d); return *this;};
90        chmat& operator = ( const chmat &A2 ){Ch=A2.Ch;dim=A2.dim;return *this;}
91        chmat& operator *= ( double x ){Ch*=sqrt(x); return *this;};
92};
93
94
95//////// Operations:
96//!mapping of add operation to operators
97inline chmat& chmat::operator += ( const chmat &A2 )  {this->add ( A2 );return *this;}
98//!mapping of negative add operation to operators
99inline chmat& chmat::operator -= ( const chmat &A2 )  {this->add ( A2,-1.0 );return *this;}
100
101#endif // CHMAT_H
Note: See TracBrowser for help on using the browser.