00001 
00013 #ifndef CHMAT_H
00014 #define CHMAT_H
00015 
00016 #include "../bdmerror.h"
00017 #include "square_mat.h"
00018 
00019 namespace bdm
00020 {
00021 
00027 class chmat : public sqmat {
00028 protected:
00030         mat Ch;
00031 public:
00032 
00033         void opupdt ( const vec &v, double w );
00034         mat to_mat() const;
00035         void mult_sym ( const mat &C );
00036         void mult_sym ( const mat &C , chmat &U ) const;
00037         void mult_sym_t ( const mat &C );
00038         void mult_sym_t ( const mat &C, chmat &U ) const;
00039         double logdet() const;
00040         vec sqrt_mult ( const vec &v ) const;
00041         double qform ( const vec &v ) const;
00042         double invqform ( const vec &v ) const;
00043         void clear();
00045         void add ( const chmat &A2, double w = 1.0 ) {
00046                 bdm_assert_debug ( dim == A2.dim, "Matrices of unequal dimension" );
00047                 mat pre = concat_vertical ( Ch, sqrt ( w ) * A2.Ch );
00048                 mat post = zeros ( pre.rows(), pre.cols() );
00049                 if ( !qr ( pre, post ) ) {
00050                         bdm_warning ( "Unstable QR in chmat add" );
00051                 }
00052                 Ch = post ( 0, dim - 1, 0, dim - 1 );
00053         };
00055         void inv ( chmat &Inv ) const   {
00056                 ( Inv.Ch ) = itpp::inv ( Ch ).T();
00057         }; 
00058         ;
00059 
00060 
00062         virtual ~chmat() {};
00064         chmat ( ) : sqmat (), Ch ( ) {};
00066         chmat ( const int dim0 ) : sqmat ( dim0 ), Ch ( dim0, dim0 ) {};
00068         chmat ( const vec &v ) : sqmat ( v.length() ), Ch ( diag ( sqrt ( v ) ) ) {};
00070         chmat ( const chmat &Ch0 ) : sqmat ( Ch0.dim ), Ch ( Ch0.dim, Ch0.dim ) {
00071                 Ch = Ch0.Ch;
00072         }
00073 
00075         chmat ( const mat &M ) : sqmat ( M.rows() ), Ch ( M.rows(), M.cols() ) {
00076                 mat Q;
00077                 bdm_assert_debug ( M.rows() == M.cols(), "chmat:: input matrix must be square!" );
00078                 Ch = chol ( M );
00079         }
00080 
00085         chmat ( const chmat &M, const ivec &perm ) {
00086                 bdm_error ( "not implemented" );
00087         }
00088 
00090         mat & _Ch() {
00091                 return Ch;
00092         }
00094         const mat & _Ch() const {
00095                 return Ch;
00096         }
00098         void setD ( const vec &nD ) {
00099                 Ch = diag ( sqrt ( nD ) );
00100         }
00102         void setCh ( const vec &chQ ) {
00103                 bdm_assert_debug ( chQ.length() == dim * dim, "wrong length" );
00104                 copy_vector ( dim*dim, chQ._data(), Ch._data() );
00105         }
00106 
00108         void setD ( const vec &nD, int i ) {
00109                 for ( int j = i; j < nD.length(); j++ ) {
00110                         Ch ( j, j ) = sqrt ( nD ( j - i ) );    
00111                 }
00112         }
00113 
00115         chmat& operator += ( const chmat &A2 );
00116         chmat& operator -= ( const chmat &A2 );
00117         chmat& operator * ( const double &d ) {
00118                 Ch*sqrt ( d );
00119                 return *this;
00120         };
00121         chmat& operator = ( const chmat &A2 ) {
00122                 Ch = A2.Ch;
00123                 dim = A2.dim;
00124                 return *this;
00125         }
00126         chmat& operator *= ( double x ) {
00127                 Ch *= sqrt ( x );
00128                 return *this;
00129         };
00130 };
00131 
00132 
00135 inline chmat& chmat::operator += ( const chmat & A2 )  {
00136         this->add ( A2 );
00137         return *this;
00138 }
00140 inline chmat& chmat::operator -= ( const chmat & A2 )  {
00141         this->add ( A2, -1.0 );
00142         return *this;
00143 }
00144 
00145 }
00146 
00147 #endif // CHMAT_H