Show
Ignore:
Timestamp:
07/31/09 13:06:49 (15 years ago)
Author:
vbarta
Message:

mpdf (& its dependencies) reformat: now using shared_ptr, moved virtual method bodies to .cpp

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • library/bdm/base/bdmbase.h

    r436 r461  
    1818#include "../itpp_ext.h" 
    1919#include "../bdmroot.h" 
     20#include "../shared_ptr.h" 
    2021#include "user_info.h" 
    21  
    2222 
    2323using namespace libconfig; 
     
    350350  //! random variable in condition 
    351351  RV rvc; 
     352 
     353private: 
    352354  //! pointer to internal epdf 
    353   epdf* ep; 
     355  shared_ptr<epdf> shep; 
     356 
    354357public: 
    355358  //! \name Constructors 
    356359  //! @{ 
    357360 
    358   mpdf() : dimc(0), rvc() {}; 
    359   //! copy constructor does not set pointer \c ep - has to be done in offsprings! 
    360   mpdf(const mpdf &m) : dimc(m.dimc), rvc(m.rvc) {}; 
    361   //!@} 
    362  
    363   //! \name Matematical operations 
    364   //!@{ 
    365  
    366   //! Returns a sample from the density conditioned on \c cond, \f$x \sim epdf(rv|cond)\f$. \param cond is numeric value of \c rv 
    367   virtual vec samplecond(const vec &cond) { 
    368     this->condition(cond); 
    369     vec temp = ep->sample(); 
    370     return temp; 
    371   }; 
    372   //! Returns \param N samples from the density conditioned on \c cond, \f$x \sim epdf(rv|cond)\f$. \param cond is numeric value of \c rv 
    373   virtual mat samplecond_m(const vec &cond, int N) { 
    374     this->condition(cond); 
    375     mat temp(ep->dimension(), N); 
    376     vec smp(ep->dimension()); 
    377     for (int i = 0; i < N; i++) {smp = ep->sample() ; temp.set_col(i, smp);} 
    378     return temp; 
    379   }; 
    380   //! Update \c ep so that it represents this mpdf conditioned on \c rvc = cond 
    381   virtual void condition(const vec &cond) {it_error("Not implemented");}; 
     361    mpdf():dimc(0), rvc() { } 
     362 
     363    mpdf(const mpdf &m):dimc(m.dimc), rvc(m.rvc), shep(m.shep) { } 
     364    //!@} 
     365 
     366    //! \name Matematical operations 
     367    //!@{ 
     368 
     369    //! Returns a sample from the density conditioned on \c cond, \f$x \sim epdf(rv|cond)\f$. \param cond is numeric value of \c rv 
     370    virtual vec samplecond(const vec &cond); 
     371 
     372    //! Returns \param N samples from the density conditioned on \c cond, \f$x \sim epdf(rv|cond)\f$. \param cond is numeric value of \c rv 
     373    virtual mat samplecond_m(const vec &cond, int N); 
     374 
     375    //! Update \c ep so that it represents this mpdf conditioned on \c rvc = cond 
     376    virtual void condition(const vec &cond) {it_error("Not implemented");}; 
    382377 
    383378  //! Shortcut for conditioning and evaluation of the internal epdf. In some cases,  this operation can be implemented efficiently. 
    384   virtual double evallogcond(const vec &dt, const vec &cond) { 
    385     double tmp; 
    386     this->condition(cond); 
    387     tmp = ep->evallog(dt); 
    388  //   it_assert_debug(std::isfinite(tmp), "Infinite value"); 
    389     return tmp; 
    390   }; 
    391  
    392   //! Matrix version of evallogcond 
    393   virtual vec evallogcond_m(const mat &Dt, const vec &cond) {this->condition(cond); return ep->evallog_m(Dt);}; 
    394   //! Array<vec> version of evallogcond 
    395   virtual vec evallogcond_m(const Array<vec> &Dt, const vec &cond) {this->condition(cond); return ep->evallog_m(Dt);}; 
    396  
    397   //! \name Access to attributes 
    398   //! @{ 
    399  
    400   RV _rv() {return ep->_rv();} 
    401   RV _rvc() {it_assert_debug(isnamed(), ""); return rvc;} 
    402   int dimension() {return ep->dimension();} 
    403   int dimensionc() {return dimc;} 
    404   epdf& _epdf() {return *ep;} 
    405   epdf* _e() {return ep;} 
    406   //! Load from structure with elements: 
    407   //!  \code 
    408   //! { rv = {class="RV", names=(...),}; // RV describing meaning of random variable 
    409   //!   rvc= {class="RV", names=(...),}; // RV describing meaning of random variable in condition 
    410   //!   // elements of offsprings 
    411   //! } 
    412   //! \endcode 
    413   //!@} 
    414   void from_setting(const Setting &set){ 
    415           if (set.exists("rv")){ 
    416                   RV* r = UI::build<RV>(set,"rv"); 
    417                   set_rv(*r);  
    418                   delete r; 
    419           } 
    420           if (set.exists("rvc")){ 
    421                   RV* r = UI::build<RV>(set,"rvc"); 
    422                   set_rvc(*r);  
    423                   delete r; 
    424           } 
    425   } 
    426   //!@} 
    427  
    428   //! \name Connection to other objects 
    429   //!@{ 
    430   void set_rvc(const RV &rvc0) {rvc = rvc0;} 
    431   void set_rv(const RV &rv0) {ep->set_rv(rv0);} 
    432   bool isnamed() {return (ep->isnamed()) && (dimc == rvc._dsize());} 
    433   //!@} 
     379    virtual double evallogcond(const vec &dt, const vec &cond); 
     380 
     381    //! Matrix version of evallogcond 
     382    virtual vec evallogcond_m(const mat &Dt, const vec &cond); 
     383 
     384    //! Array<vec> version of evallogcond 
     385    virtual vec evallogcond_m(const Array<vec> &Dt, const vec &cond); 
     386 
     387    //! \name Access to attributes 
     388    //! @{ 
     389 
     390    RV _rv() { return shep->_rv(); } 
     391    RV _rvc() { it_assert_debug(isnamed(), ""); return rvc; } 
     392    int dimension() { return shep->dimension(); } 
     393    int dimensionc() { return dimc; } 
     394 
     395    epdf *e() { return shep.get(); } 
     396 
     397    void set_ep(shared_ptr<epdf> ep) { shep = ep; } 
     398 
     399    //! Load from structure with elements: 
     400    //!  \code 
     401    //! { rv = {class="RV", names=(...),}; // RV describing meaning of random variable 
     402    //!   rvc= {class="RV", names=(...),}; // RV describing meaning of random variable in condition 
     403    //!   // elements of offsprings 
     404    //! } 
     405    //! \endcode 
     406    //!@} 
     407    void from_setting(const Setting &set); 
     408    //!@} 
     409 
     410    //! \name Connection to other objects 
     411    //!@{ 
     412    void set_rvc(const RV &rvc0) { rvc = rvc0; } 
     413    void set_rv(const RV &rv0) { shep->set_rv(rv0); } 
     414    bool isnamed() { return (shep->isnamed()) && (dimc == rvc._dsize()); } 
     415    //!@} 
    434416}; 
    435417 
     
    628610*/ 
    629611class mepdf : public mpdf { 
    630         bool owning_ep; // flag trigers deleting ep by destructor 
    631612public: 
    632613        //!Default constructor 
    633         mepdf(){}; 
    634         mepdf ( epdf* em, bool owning_ep0=false ) :mpdf ( ) {ep= em ;owning_ep=owning_ep0;dimc=0;}; 
    635         mepdf (const epdf* em ) :mpdf ( ) {ep=const_cast<epdf*>( em );}; 
     614        mepdf() { } 
     615 
     616        mepdf(shared_ptr<epdf> em) :mpdf ( ) { set_ep(em); dimc=0; } 
     617 
    636618        void condition ( const vec &cond ) {} 
    637         ~mepdf(){if (owning_ep) delete ep;} 
     619 
    638620  //! Load from structure with elements: 
    639621  //!  \code 
     
    644626  //!@} 
    645627        void from_setting(const Setting &set){ 
    646                 epdf* e = UI::build<epdf>(set,"epdf"); 
    647                 ep=     e;  
    648                 owning_ep=true; 
     628                epdf *e = UI::build<epdf>(set, "epdf"); 
     629                set_ep(e); 
    649630        } 
    650631};