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

Revision 384, 3.0 kB (checked in by mido, 15 years ago)

possibly broken?

  • 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        //!Inversion in the same form, i.e. cholesky
44        void inv ( chmat &Inv ) const   { ( Inv.Ch ) = itpp::inv ( Ch ).T();}; //Fixme: can be more efficient
45        ;
46//      void inv ( mat &Inv );
47
48        //! Destructor for future use;
49        virtual ~chmat() {};
50        //!
51        chmat ( ) : sqmat (),Ch ( ) {};
52        //! Default constructor
53        chmat ( const int dim0 ) : sqmat ( dim0 ),Ch ( dim0,dim0 ) {};
54        //! Default constructor
55        chmat ( const vec &v) : sqmat ( v.length() ),Ch ( diag(sqrt(v)) ) {};
56        //! Copy constructor
57        chmat ( const chmat &Ch0) : sqmat ( Ch0.dim),Ch ( Ch0.dim,Ch0.dim ) {Ch=Ch0.Ch;};
58        //! Default constructor (m3k:cholform)
59        chmat ( const mat &M ) : sqmat ( M.rows() ),Ch ( M.rows(),M.cols() ) {
60                mat Q;
61                it_assert_debug ( M.rows() ==M.cols(),"chmat:: input matrix must be square!" );
62                Ch=chol ( M );
63        };
64        //! Constructor
65        chmat ( const chmat &M, const ivec &perm ):sqmat(M.rows()){it_error("not implemneted");};
66        //! Access function
67        mat & _Ch() {return Ch;}
68        //! Access function
69        const mat & _Ch() const {return Ch;}
70        //! Access functions
71        void setD ( const vec &nD ) {Ch=diag ( sqrt(nD) );}
72        //! Access functions
73        void setCh ( const vec &chQ ) {
74                it_assert_debug(chQ.length()==dim*dim,""); 
75                copy_vector(dim*dim, chQ._data(), Ch._data()); 
76        }
77        //! Access functions
78        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
79
80        //! Operators
81        chmat& operator += ( const chmat &A2 );
82        chmat& operator -= ( const chmat &A2 ) ;
83        chmat& operator * ( const chmat &A2 );
84        chmat& operator * ( const double &d ){Ch*d; return *this;};
85        chmat& operator = ( const chmat &A2 ){Ch=A2.Ch;dim=A2.dim;return *this;}
86};
87
88
89//////// Operations:
90//!mapping of add operation to operators
91inline chmat& chmat::operator += ( const chmat &A2 )  {this->add ( A2 );return *this;}
92//!mapping of negative add operation to operators
93inline chmat& chmat::operator -= ( const chmat &A2 )  {this->add ( A2,-1.0 );return *this;}
94
95#endif // CHMAT_H
Note: See TracBrowser for help on using the browser.