/*! * \file * \brief Matrices in decomposed forms (LDL', LU, UDU', etc). * \author Vaclav Smidl. * * ----------------------------------- * BDM++ - C++ library for Bayesian Decision Making under Uncertainty * * Using IT++ for numerical operations * ----------------------------------- */ #ifndef DC_H #define DC_H #include "../itpp_ext.h" /*! \defgroup math Auxiliary math functions @{ */ using namespace itpp; //! Auxiliary function dydr; dyadic reduction void dydr( double * r, double *f, double *Dr, double *Df, double *R, int jl, int jh, double *kr, int m, int mx ); //! Auxiliary function ltuinv; inversion of a triangular matrix; //TODO can be done via: dtrtri.f from lapack mat ltuinv( const mat &L ); /*! \brief Abstract class for representation of double symmetric matrices in square-root form. All operations defined on this class should be optimized for the chosen decomposition. */ class sqmat { public: /*! * Perfroms a rank-1 update by outer product of vectors: \f$V = V + w v v'\f$. * @param v Vector forming the outer product to be added * @param w weight of updating; can be negative BLAS-2b operation. */ virtual void opupdt ( const vec &v, double w ) =0; /*! \brief Conversion to full matrix. */ virtual mat to_mat() const =0; /*! \brief Inplace symmetric multiplication by a SQUARE matrix \f$C\f$, i.e. \f$V = C*V*C'\f$ @param C multiplying matrix, */ virtual void mult_sym ( const mat &C ) =0; /*! \brief Inplace symmetric multiplication by a SQUARE transpose of matrix \f$C\f$, i.e. \f$V = C'*V*C\f$ @param C multiplying matrix, */ virtual void mult_sym_t ( const mat &C ) =0; /*! \brief Logarithm of a determinant. */ virtual double logdet() const =0; /*! \brief Multiplies square root of \f$V\f$ by vector \f$x\f$. Used e.g. in generating normal samples. */ virtual vec sqrt_mult (const vec &v ) const =0; /*! \brief Evaluates quadratic form \f$x= v'*V*v\f$; */ virtual double qform (const vec &v ) const =0; /*! \brief Evaluates quadratic form \f$x= v'*inv(V)*v\f$; */ virtual double invqform (const vec &v ) const =0; // //! easy version of the // sqmat inv(); //! Clearing matrix so that it corresponds to zeros. virtual void clear() =0; //! Reimplementing common functions of mat: cols(). int cols() const {return dim;}; //! Reimplementing common functions of mat: rows(). int rows() const {return dim;}; //! Destructor for future use; virtual ~sqmat(){}; //! Default constructor sqmat(const int dim0): dim(dim0){}; //! Default constructor sqmat(): dim(0){}; protected: //! dimension of the square matrix int dim; }; /*! \brief Fake sqmat. This class maps sqmat operations to operations on full matrix. This class can be used to compare performance of algorithms using decomposed matrices with perormance of the same algorithms using full matrices; */ class fsqmat: public sqmat { protected: //! Full matrix on which the operations are performed mat M; public: void opupdt ( const vec &v, double w ); mat to_mat() const; void mult_sym ( const mat &C); void mult_sym_t ( const mat &C); //! store result of \c mult_sym in external matrix \f$U\f$ void mult_sym ( const mat &C, fsqmat &U) const; //! store result of \c mult_sym_t in external matrix \f$U\f$ void mult_sym_t ( const mat &C, fsqmat &U) const; void clear(); //! Default initialization fsqmat(){}; // mat will be initialized OK //! Default initialization with proper size fsqmat(const int dim0); // mat will be initialized OK //! Constructor fsqmat ( const mat &M ); //! Constructor fsqmat ( const fsqmat &M, const ivec &perm ):sqmat(M.rows()){it_error("not implemneted");}; //! Constructor fsqmat ( const vec &d ):sqmat(d.length()){M=diag(d);}; //! Destructor for future use; virtual ~fsqmat(){}; /*! \brief Matrix inversion preserving the chosen form. @param Inv a space where the inverse is stored. */ void inv ( fsqmat &Inv ) const; double logdet() const {return log ( det ( M ) );}; double qform (const vec &v ) const {return ( v* ( M*v ) );}; double invqform (const vec &v ) const {return ( v* ( itpp::inv(M)*v ) );}; vec sqrt_mult (const vec &v ) const {mat Ch=chol(M); return Ch*v;}; //! Add another matrix in fsq form with weight w void add ( const fsqmat &fsq2, double w=1.0 ){M+=fsq2.M;}; //! Access functions void setD (const vec &nD){M=diag(nD);} //! Access functions vec getD (){return diag(M);} //! Access functions void setD (const vec &nD, int i){for(int j=i;jadd ( ldA );return *this;} //!mapping of negative add operation to operators inline ldmat& ldmat::operator -= ( const ldmat &ldA ) {this->add ( ldA,-1.0 );return *this;} /*! @} */ #endif // DC_H