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
RevLine 
[39]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
[565]16#include "../bdmerror.h"
[384]17#include "square_mat.h"
[39]18
[495]19namespace bdm
20{
21
[39]22/*! \brief Symmetric matrix stored in square root decomposition using upper cholesky
23
[76]24This matrix represent \f$A=Ch' Ch\f$ where only the upper triangle \f$Ch\f$ is stored;
[39]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 );
[168]34        mat to_mat() const;
[39]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;
[108]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();
[98]44        //! add another chmat \c A2 with weight \c w.
[477]45        void add ( const chmat &A2, double w = 1.0 ) {
[565]46                bdm_assert_debug ( dim == A2.dim, "Matrices of unequal dimension" );
[477]47                mat pre = concat_vertical ( Ch, sqrt ( w ) * A2.Ch );
48                mat post = zeros ( pre.rows(), pre.cols() );
49                if ( !qr ( pre, post ) ) {
[565]50                        bdm_warning ( "Unstable QR in chmat add" );
[477]51                }
52                Ch = post ( 0, dim - 1, 0, dim - 1 );
[437]53        };
[39]54        //!Inversion in the same form, i.e. cholesky
[477]55        void inv ( chmat &Inv ) const   {
56                ( Inv.Ch ) = itpp::inv ( Ch ).T();
57        }; //Fixme: can be more efficient
[39]58        ;
59//      void inv ( mat &Inv );
60
61        //! Destructor for future use;
62        virtual ~chmat() {};
[270]63        //!
[477]64        chmat ( ) : sqmat (), Ch ( ) {};
[39]65        //! Default constructor
[477]66        chmat ( const int dim0 ) : sqmat ( dim0 ), Ch ( dim0, dim0 ) {};
[39]67        //! Default constructor
[477]68        chmat ( const vec &v ) : sqmat ( v.length() ), Ch ( diag ( sqrt ( v ) ) ) {};
[76]69        //! Copy constructor
[477]70        chmat ( const chmat &Ch0 ) : sqmat ( Ch0.dim ), Ch ( Ch0.dim, Ch0.dim ) {
71                Ch = Ch0.Ch;
[565]72        }
73
[39]74        //! Default constructor (m3k:cholform)
[477]75        chmat ( const mat &M ) : sqmat ( M.rows() ), Ch ( M.rows(), M.cols() ) {
[39]76                mat Q;
[565]77                bdm_assert_debug ( M.rows() == M.cols(), "chmat:: input matrix must be square!" );
[477]78                Ch = chol ( M );
[565]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
[39]89        //! Access function
[477]90        mat & _Ch() {
91                return Ch;
92        }
[294]93        //! Access function
[477]94        const mat & _Ch() const {
95                return Ch;
96        }
[39]97        //! Access functions
[477]98        void setD ( const vec &nD ) {
99                Ch = diag ( sqrt ( nD ) );
100        }
[39]101        //! Access functions
[294]102        void setCh ( const vec &chQ ) {
[565]103                bdm_assert_debug ( chQ.length() == dim * dim, "wrong length" );
[477]104                copy_vector ( dim*dim, chQ._data(), Ch._data() );
[294]105        }
[565]106
[294]107        //! Access functions
[477]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        }
[39]113
114        //! Operators
115        chmat& operator += ( const chmat &A2 );
[437]116        chmat& operator -= ( const chmat &A2 );
[477]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        };
[39]130};
131
132
133//////// Operations:
134//!mapping of add operation to operators
[477]135inline chmat& chmat::operator += ( const chmat & A2 )  {
136        this->add ( A2 );
137        return *this;
138}
[39]139//!mapping of negative add operation to operators
[477]140inline chmat& chmat::operator -= ( const chmat & A2 )  {
141        this->add ( A2, -1.0 );
142        return *this;
143}
[254]144
[495]145}
146
[39]147#endif // CHMAT_H
Note: See TracBrowser for help on using the browser.