Changeset 162 for bdm/stat

Show
Ignore:
Timestamp:
09/04/08 20:27:01 (16 years ago)
Author:
smidl
Message:

opravy a dokumentace

Location:
bdm/stat
Files:
7 modified

Legend:

Unmodified
Added
Removed
  • bdm/stat/emix.h

    r148 r162  
    3131*/ 
    3232class emix : public epdf { 
    33         protected: 
    34                 //! weights of the components 
    35                 vec w; 
    36                 //! Component (epdfs) 
    37                 Array<epdf*> Coms; 
    38         public: 
    39                 //!Default constructor 
    40                 emix(RV &rv) : epdf(rv) {}; 
    41                 //! Set weights \c w and components \c R 
    42                 void set_parameters(const vec &w, const Array<epdf*> &Coms); 
     33protected: 
     34        //! weights of the components 
     35        vec w; 
     36        //! Component (epdfs) 
     37        Array<epdf*> Coms; 
     38public: 
     39        //!Default constructor 
     40        emix ( RV &rv ) : epdf ( rv ) {}; 
     41        //! Set weights \c w and components \c R 
     42        void set_parameters ( const vec &w, const Array<epdf*> &Coms ); 
    4343 
    44                 vec sample() const; 
    45                 vec mean() const { 
    46                         int i; vec mu = zeros(rv.count()); 
    47                         for (i = 0;i < w.length();i++) {mu += w(i) * Coms(i)->mean(); } 
    48                         return mu; 
    49                 } 
    50                 double evalpdflog(const vec &val) const { 
    51                         int i; 
    52                         double sum = 0.0; 
    53                         for (i = 0;i < w.length();i++) {sum += w(i) * Coms(i)->evalpdflog(val);} 
    54                         return log(sum); 
    55                 }; 
     44        vec sample() const; 
     45        vec mean() const { 
     46                int i; vec mu = zeros ( rv.count() ); 
     47                for ( i = 0;i < w.length();i++ ) {mu += w ( i ) * Coms ( i )->mean(); } 
     48                return mu; 
     49        } 
     50        double evalpdflog ( const vec &val ) const { 
     51                int i; 
     52                double sum = 0.0; 
     53                for ( i = 0;i < w.length();i++ ) {sum += w ( i ) * Coms ( i )->evalpdflog ( val );} 
     54                return log ( sum ); 
     55        }; 
    5656 
    5757//Access methods 
    58                 //! returns a pointer to the internal mean value. Use with Care! 
    59                 vec& _w() {return w;} 
     58        //! returns a pointer to the internal mean value. Use with Care! 
     59        vec& _w() {return w;} 
    6060}; 
    6161 
     
    6868Note that 
    6969*/ 
    70 class eprod: public epdf { 
    71         protected: 
    72                 // 
    73                 int n; 
    74                 // pointers to epdfs 
    75                 Array<epdf*> epdfs; 
    76                 Array<mpdf*> mpdfs; 
    77                 // 
    78                 Array<ivec> rvinds; 
    79                 Array<ivec> rvcinds; 
    80                 //! Indicate independence of its factors 
    81                 bool independent; 
    82                 //! Indicate internal creation of mpdfs which must be destroyed 
    83                 bool intermpdfs; 
    84         public: 
    85                 //!Constructor from list of eFacs or list of mFacs 
    86                 eprod(Array<mpdf*> mFacs): epdf(RV()), n(mFacs.length()), epdfs(n), mpdfs(mFacs), rvinds(n), rvcinds(n) { 
    87                         int i; 
    88                         intermpdfs = false; 
    89                         for (i = 0;i < n;i++) { 
    90                                 rv.add(mpdfs(i)->_rv()); //add rv to common rvs. 
    91                                 epdfs(i) = &(mpdfs(i)->_epdf()); // add pointer to epdf 
    92                         }; 
    93                         independent = true; 
    94                         //test rvc of mpdfs and fill rvinds 
    95                         for (i = 0;i < n;i++) { 
    96                                 // find ith rv in common rv 
    97                                 rvinds(i) = mpdfs(i)->_rv().dataind(rv); 
    98                                 // find ith rvc in common rv 
    99                                 rvcinds(i) = mpdfs(i)->_rvc().dataind(rv); 
    100                                 if (rvcinds(i).length()>0) {independent = false;} 
    101                         } 
    102  
     70class mprod: public mpdf { 
     71protected: 
     72        // 
     73        int n; 
     74        // pointers to epdfs 
     75        Array<epdf*> epdfs; 
     76        Array<mpdf*> mpdfs; 
     77        // 
     78        //! Indeces of rvs in common rv 
     79        Array<ivec> rvinds; 
     80        //! Indeces of rvc in common rv 
     81        Array<ivec> rvcinrv; 
     82        //! Indeces of rvc in common rvc 
     83        Array<ivec> rvcinds; 
     84        //! Indicate independence of its factors 
     85        bool independent; 
     86//      //! Indicate internal creation of mpdfs which must be destroyed 
     87//      bool intermpdfs; 
     88public: 
     89        //!Constructor from list of eFacs or list of mFacs 
     90        mprod ( Array<mpdf*> mFacs ) : mpdf ( RV(), RV() ), n ( mFacs.length() ), epdfs ( n ), mpdfs ( mFacs ), rvinds ( n ), rvcinrv ( n ), rvcinds ( n ) { 
     91                int i; 
     92                // Create rv 
     93                for ( i = 0;i < n;i++ ) { 
     94                        rv.add ( mpdfs ( i )->_rv() ); //add rv to common rvs. 
     95                        epdfs ( i ) = & ( mpdfs ( i )->_epdf() ); // add pointer to epdf 
    10396                }; 
    104                 eprod(Array<epdf*> eFacs): epdf(RV()), n(eFacs.length()), epdfs(eFacs), mpdfs(n), rvinds(n), rvcinds(n) { 
    105                         int i; 
    106                         intermpdfs = true; 
    107                         for (i = 0;i < n;i++) { 
    108                                 if (rv.add(eFacs(i)->_rv())) {it_error("Incompatible eFacs.rv!");} //add rv to common rvs. 
    109                                 mpdfs(i) = new mepdf(*(epdfs(i))); 
    110                                 epdfs(i) = &(mpdfs(i)->_epdf()); // add pointer to epdf 
    111                         }; 
    112                         independent = true; 
    113                         //test rvc of mpdfs and fill rvinds 
    114                         for (i = 0;i < n;i++) { 
    115                                 // find ith rv in common rv 
    116                                 rvinds(i) = mpdfs(i)->_rv().dataind(rv); 
    117                         } 
     97                // Create rvc 
     98                for ( i = 0;i < n;i++ ) { 
     99                        rvc.add ( mpdfs ( i )->_rv().subt ( rv ) ); //add rv to common rvs. 
    118100                }; 
    119101 
    120                 double evalpdflog(const vec &val) const { 
    121                         int i; 
    122                         double res = 0.0; 
    123                         for (i = n - 1;i > 0;i++) { 
    124                                 if (rvcinds(i).length() > 0) 
    125                                         {mpdfs(i)->condition(val(rvcinds(i)));} 
    126                                 // add logarithms 
    127                                 res += epdfs(i)->evalpdflog(val(rvinds(i))); 
     102                independent = true; 
     103                //test rvc of mpdfs and fill rvinds 
     104                for ( i = 0;i < n;i++ ) { 
     105                        // find ith rv in common rv 
     106                        rvinds ( i ) = mpdfs ( i )->_rv().dataind ( rv ); 
     107                        // find ith rvc in common rv 
     108                        rvcinrv ( i ) = mpdfs ( i )->_rvc().dataind ( rv ); 
     109                        // find ith rvc in common rv 
     110                        rvcinds ( i ) = mpdfs ( i )->_rvc().dataind ( rvc ); 
     111                        // 
     112                        if ( rvcinds ( i ).length() >0 ) {independent = false;} 
     113                        if ( rvcinds ( i ).length() >0 ) {independent = false;} 
     114                } 
     115        }; 
     116 
     117        double evalpdflog ( const vec &val ) const { 
     118                int i; 
     119                double res = 0.0; 
     120                for ( i = n - 1;i > 0;i++ ) { 
     121                        if ( rvcinds ( i ).length() > 0 ) 
     122                                {mpdfs ( i )->condition ( val ( rvcinds ( i ) ) );} 
     123                        // add logarithms 
     124                        res += epdfs ( i )->evalpdflog ( val ( rvinds ( i ) ) ); 
     125                } 
     126                return res; 
     127        } 
     128        vec samplecond ( const vec &cond, double &ll ) { 
     129                vec smp=zeros ( rv.count() ); 
     130                vec condi; 
     131                for ( int i = ( n - 1 );i >= 0;i-- ) { 
     132                        if ( rvcinds ( i ).length() > 0 ) { 
     133                                condi = zeros ( rvcinrv.length() + rvcinds.length() ); 
     134                                // copy data in condition 
     135                                set_subvector ( condi,rvcinds ( i ), cond ); 
     136                                // copy data in already generated sample 
     137                                set_subvector ( condi,rvcinrv ( i ), smp ); 
     138 
     139                                mpdfs ( i )->condition ( condi ); 
    128140                        } 
    129                         return res; 
     141                        // copy contribution of this pdf into smp 
     142                        set_subvector ( smp,rvinds ( i ), epdfs ( i )->sample() ); 
    130143                } 
    131                 vec sample () const{ 
    132                         vec smp=zeros(rv.count()); 
    133                         for (int i = (n - 1);i >= 0;i--) { 
    134                                 if (rvcinds(i).length() > 0) 
    135                                         {mpdfs(i)->condition(smp(rvcinds(i)));} 
    136                                 set_subvector(smp,rvinds(i), epdfs(i)->sample()); 
    137                         }                        
    138                         return smp; 
    139                 } 
    140                 vec mean() const{ 
    141                         vec tmp(rv.count()); 
    142                         if (independent) { 
    143                                 for (int i=0;i<n;i++) { 
    144                                         vec pom = epdfs(i)->mean(); 
    145                                         set_subvector(tmp,rvinds(i), pom); 
    146                                 } 
    147                         } 
    148                         else { 
    149                                 int N=50*rv.count(); 
    150                                 it_warning("eprod.mean() computed by sampling"); 
    151                                 tmp = zeros(rv.count()); 
    152                                 for (int i=0;i<N;i++){ tmp += sample();} 
    153                                 tmp /=N; 
    154                         } 
    155                         return tmp; 
    156                 } 
    157                 ~eprod(){if (intermpdfs) {for (int i=0;i<n;i++){delete mpdfs(i);}}}; 
     144                return smp; 
     145        } 
     146        mat samplecond ( const vec &cond, vec &ll, int N ) { 
     147                mat Smp(rv.count(),N); 
     148                for(int i=0;i<N;i++){Smp.set_col(i,samplecond(cond,ll(i)));} 
     149                return Smp; 
     150        } 
     151//      vec mean() const { 
     152//              vec tmp ( rv.count() ); 
     153//              if ( independent ) { 
     154//                      for ( int i=0;i<n;i++ ) { 
     155//                              vec pom = epdfs ( i )->mean(); 
     156//                              set_subvector ( tmp,rvinds ( i ), pom ); 
     157//                      } 
     158//              } 
     159//              else { 
     160//                      int N=50*rv.count(); 
     161//                      it_warning ( "eprod.mean() computed by sampling" ); 
     162//                      tmp = zeros ( rv.count() ); 
     163//                      for ( int i=0;i<N;i++ ) { tmp += sample();} 
     164//                      tmp /=N; 
     165//              } 
     166//              return tmp; 
     167//      } 
     168 
     169        ~mprod() {}; 
    158170}; 
    159171 
     
    162174*/ 
    163175class mmix : public mpdf { 
    164         protected: 
    165                 //! Component (epdfs) 
    166                 Array<mpdf*> Coms; 
    167                 //!Internal epdf 
    168                 emix Epdf; 
    169         public: 
    170                 //!Default constructor 
    171                 mmix(RV &rv, RV &rvc) : mpdf(rv, rvc), Epdf(rv) {ep = &Epdf;}; 
    172                 //! Set weights \c w and components \c R 
    173                 void set_parameters(const vec &w, const Array<mpdf*> &Coms) { 
    174                         Array<epdf*> Eps(Coms.length()); 
     176protected: 
     177        //! Component (epdfs) 
     178        Array<mpdf*> Coms; 
     179        //!Internal epdf 
     180        emix Epdf; 
     181public: 
     182        //!Default constructor 
     183        mmix ( RV &rv, RV &rvc ) : mpdf ( rv, rvc ), Epdf ( rv ) {ep = &Epdf;}; 
     184        //! Set weights \c w and components \c R 
     185        void set_parameters ( const vec &w, const Array<mpdf*> &Coms ) { 
     186                Array<epdf*> Eps ( Coms.length() ); 
    175187 
    176                         for (int i = 0;i < Coms.length();i++) { 
    177                                 Eps(i) = & (Coms(i)->_epdf()); 
    178                         } 
    179                         Epdf.set_parameters(w, Eps); 
    180                 }; 
     188                for ( int i = 0;i < Coms.length();i++ ) { 
     189                        Eps ( i ) = & ( Coms ( i )->_epdf() ); 
     190                } 
     191                Epdf.set_parameters ( w, Eps ); 
     192        }; 
    181193 
    182                 void condition(const vec &cond) { 
    183                         for (int i = 0;i < Coms.length();i++) {Coms(i)->condition(cond);} 
    184                 }; 
     194        void condition ( const vec &cond ) { 
     195                for ( int i = 0;i < Coms.length();i++ ) {Coms ( i )->condition ( cond );} 
     196        }; 
    185197}; 
    186198#endif //MX_H 
  • bdm/stat/libBM.cpp

    r145 r162  
    77using std::cout; 
    88 
     9static int RVcounter=0; 
     10 
    911void RV::init ( ivec in_ids, Array<std::string> in_names, ivec in_sizes, ivec in_times ) { 
    10         // 
     12        //Refer 
    1113        int i; 
    1214        len = in_ids.length(); 
    1315        //PRUDENT_MODE 
    1416        // All vetors should be of same length 
    15         if ( ( len != in_ids.length() ) || \ 
    16                 ( len != in_names.length() ) || \ 
     17        if ( ( len != in_names.length() ) || \ 
    1718                ( len != in_sizes.length() ) || \ 
    1819                ( len != in_times.length() ) ) { 
     
    2829}; 
    2930 
    30 RV::RV ( ivec in_ids, Array<std::string> in_names, ivec in_sizes, ivec in_times ) { 
    31         init ( in_ids, in_names, in_sizes, in_times ); 
     31RV::RV ( Array<std::string> in_names, ivec in_sizes, ivec in_times ) { 
     32        int len = in_names.length(); 
     33        init ( linspace ( RVcounter+1, RVcounter+len ), in_names, in_sizes, in_times ); 
     34        RVcounter+=len; 
    3235} 
    3336 
    34 RV::RV() : tsize ( 0 ), len ( 0 ) {}; 
     37RV::RV ( Array<std::string> in_names, ivec in_sizes ) { 
     38        int len = in_names.length(); 
     39        init ( linspace ( RVcounter+1, RVcounter+len ), in_names, in_sizes, zeros_i ( in_names.length() ) ); 
     40        RVcounter+=len; 
     41} 
     42 
     43RV::RV ( Array<std::string> in_names ) { 
     44        int len = in_names.length(); 
     45        init ( linspace ( RVcounter+1, RVcounter+len ), in_names, ones_i ( in_names.length() ), zeros_i ( in_names.length() ) ); 
     46        RVcounter+=len; 
     47} 
     48 
     49RV::RV() : tsize ( 0 ), len ( 0 ), ids ( 0 ), names ( 0 ), sizes ( 0 ), times ( 0 ) {}; 
    3550 
    3651bool RV::add ( const RV &rv2 ) { 
    3752        // TODO 
    38         ivec ind = rv2.findself ( *this ); //should be -1 all the time 
    39         ivec index = itpp::find(ind==-1);  
    40          
    41         if ( index.length() < rv2.len ) { //conflict 
    42                 ids = concat ( ids, rv2.ids(index) ); 
    43                 sizes = concat ( sizes, rv2.sizes(index) ); 
    44                 times = concat ( times, rv2.times(index) ); 
    45                 names = concat ( names, rv2.names(to_Arr(index)) ); 
     53        if ( rv2.len>0 ) { //rv2 is nonempty 
     54                ivec ind = rv2.findself ( *this ); //should be -1 all the time 
     55                ivec index = itpp::find ( ind==-1 ); 
     56 
     57 
     58                if ( index.length() < rv2.len ) { //conflict 
     59                        ids = concat ( ids, rv2.ids ( index ) ); 
     60                        sizes = concat ( sizes, rv2.sizes ( index ) ); 
     61                        times = concat ( times, rv2.times ( index ) ); 
     62                        names = concat ( names, rv2.names ( to_Arr ( index ) ) ); 
     63                } 
     64                else { 
     65                        ids = concat ( ids, rv2.ids ); 
     66                        sizes = concat ( sizes, rv2.sizes ); 
     67                        times = concat ( times, rv2.times ); 
     68                        names = concat ( names, rv2.names ); 
     69                } 
     70                tsize = sum ( sizes ); 
     71                len = ids.length(); 
     72                return ( index.length() <rv2.len ); 
    4673        } 
    47         else { 
    48                 ids = concat ( ids, rv2.ids ); 
    49                 sizes = concat ( sizes, rv2.sizes ); 
    50                 times = concat ( times, rv2.times ); 
    51                 names = concat ( names, rv2.names ); 
     74        else { //rv2 is empty 
     75                return true; 
    5276        } 
    53         tsize = sum(sizes); 
    54         len = ids.length(); 
    55         return (index.length()<rv2.len); 
    56  
    5777//      return *this; 
    5878}; 
    5979 
    60 RV::RV ( ivec in_ids ) { 
    61  
    62         len = in_ids.length(); 
    63         Array<std::string> A ( len ); 
    64         std::string rvstr = "rv"; 
    65  
    66         for ( int i = 0; i < len;i++ ) { 
    67                 A ( i ) = rvstr + to_str ( i ); 
    68         } 
    69  
    70         init ( in_ids, A, ones_i ( len ), zeros_i ( len ) ); 
    71 } 
     80// RV::RV ( ivec in_ids ) { 
     81// 
     82//      len = in_ids.length(); 
     83//      Array<std::string> A ( len ); 
     84//      std::string rvstr = "rv"; 
     85// 
     86//      for ( int i = 0; i < len;i++ ) { 
     87//              A ( i ) = rvstr + to_str ( i ); 
     88//      } 
     89// 
     90//      init ( in_ids, A, ones_i ( len ), zeros_i ( len ) ); 
     91// } 
    7292 
    7393RV RV::subselect ( ivec ind ) const { 
    74         return RV ( ids ( ind ), names ( to_Arr ( ind ) ), sizes ( ind ), times ( ind ) ); 
     94        RV ret; 
     95        ret.init ( ids ( ind ), names ( to_Arr ( ind ) ), sizes ( ind ), times ( ind ) ); 
     96        return ret; 
    7597} 
    7698 
     
    78100 
    79101RV RV::operator() ( ivec ind ) const { 
    80         return RV ( ids ( ind ), names ( to_Arr ( ind ) ), sizes ( ind ), times ( ind ) ); 
     102        RV ret; 
     103        if ( ind.length() >0 ) { 
     104                ret.init ( ids ( ind ), names ( to_Arr ( ind ) ), sizes ( ind ), times ( ind ) ); 
     105        } 
     106        return ret; 
    81107} 
    82108 
     
    127153 
    128154RV RV::subt ( const RV rv2 ) const { 
     155        cout << *this <<endl; 
     156        cout << rv2 <<endl; 
     157 
    129158        ivec res = this->findself ( rv2 ); // nonzeros 
    130159        ivec valid = itpp::find ( res == -1 ); //-1 => value not found => it remains 
  • bdm/stat/libBM.h

    r145 r162  
    5252private: 
    5353        //! auxiliary function used in constructor 
    54         void init ( ivec in_ids, Array<std::string> in_names, ivec in_sizes, ivec in_times ); 
    55 public: 
    56         //! Full constructor which is called by the others 
    57         RV ( ivec in_ids, Array<std::string> in_names, ivec in_sizes, ivec in_times ); 
    58         //! default constructor 
    59         RV ( ivec ids ); 
    60         //! Empty constructor will be set later 
     54        void init (ivec in_ids, Array<std::string> in_names, ivec in_sizes, ivec in_times ); 
     55public: 
     56        //! Full constructor  
     57        RV ( Array<std::string> in_names, ivec in_sizes, ivec in_times ); 
     58        //! Constructor with times=0 
     59        RV ( Array<std::string> in_names, ivec in_sizes ); 
     60        //! Constructor with sizes=1, times=0 
     61        RV ( Array<std::string> in_names ); 
     62        //! Constructor of empty RV 
    6163        RV (); 
    6264 
     
    137139public: 
    138140        //!default constructor 
    139         epdf() :rv ( ivec ( 0 ) ) {}; 
     141        epdf() :rv ( ) {}; 
    140142 
    141143        //!default constructor 
     
    180182//      virtual fnc moment ( const int order = 1 ); 
    181183        //! 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 \param ll is a return value of log-likelihood of the sample. 
    182         virtual vec samplecond ( vec &cond, double &ll ) {this->condition ( cond ); 
     184        virtual vec samplecond (const vec &cond, double &ll ) {this->condition ( cond ); 
    183185        vec temp= ep->sample(); 
    184186        ll=ep->evalpdflog ( temp );return temp;}; 
    185187        //! 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 \param ll is a return value of log-likelihood of the sample. 
    186         virtual mat samplecond ( vec &cond, vec &ll, int N ) { 
     188        virtual mat samplecond (const vec &cond, vec &ll, int N ) { 
    187189                this->condition ( cond ); 
    188190                mat temp ( rv.count(),N ); vec smp ( rv.count() );  
  • bdm/stat/libEF.cpp

    r129 r162  
    112112}; 
    113113 
    114 vec mgamma::samplecond ( vec &cond, double &ll ) { 
    115         this->condition(cond ); 
    116         vec smp = epdf.sample(); 
    117         ll = epdf.evalpdflog ( smp ); 
    118         return smp; 
    119 }; 
    120  
    121 //Fixme repetition of mlnorm.samplecond. 
    122 mat mgamma::samplecond ( vec &cond, vec &lik, int n ) { 
    123         int i; 
    124         int dim = rv.count(); 
    125         mat Smp ( dim,n ); 
    126         vec smp ( dim ); 
    127         this->condition ( cond ); 
    128  
    129         for ( i=0; i<n; i++ ) { 
    130                 smp = epdf.sample(); 
    131                 lik ( i ) = epdf.eval ( smp ); 
    132                 Smp.set_col ( i ,smp ); 
    133         } 
    134  
    135         return Smp; 
    136 }; 
    137  
    138114ivec eEmp::resample ( RESAMPLING_METHOD method ) { 
    139115        ivec ind=zeros_i ( n ); 
  • bdm/stat/libEF.h

    r129 r162  
    270270        //! Set value of \c k 
    271271        void set_parameters ( double k ); 
    272         //!Generate one sample of the posterior 
    273         vec samplecond ( vec &cond, double &lik ); 
    274         //!Generate matrix of samples of the posterior 
    275         mat samplecond ( vec &cond, vec &lik, int n ); 
    276272        void condition ( const vec &val ) {*_beta=k/val;}; 
    277273}; 
  • bdm/stat/loggers.cpp

    r102 r162  
    88#endif 
    99 
    10 void memlog::itsave(const char* fname){ 
    11         it_file itf(fname); 
     10void memlog::itsave ( const char* fname ) { 
     11        it_file itf ( fname ); 
    1212        int i; 
    13         for (i=0; i<entries.length();i++){ 
    14          itf << Name(names(i)) << vectors(i); 
     13        for ( i=0; i<entries.length();i++ ) { 
     14                if ( names ( i ).length() <1 ) { 
     15                        std::ostringstream o; 
     16                        o<<"Variable"<<i; 
     17                        itf << Name ( o.str() ) << vectors ( i ); 
     18                } 
     19                else { 
     20                        itf << Name ( names ( i ) ) << vectors ( i ); 
     21                } 
    1522        } 
    1623} 
     
    2633 
    2734// Initialize directory 
    28         makedir(dirname); //  
     35        makedir ( dirname ); // 
    2936 
    3037// directory OK, or it can be overwritten 
     
    4047                                scalarnames ( ii ) = names ( i ) + "_" + entries ( i ).name ( j ); 
    4148                                ii++; 
    42                         } else { 
     49                        } 
     50                        else { 
    4351                                for ( k=0;k<rvsize;k++ ) { //for all scalars in given RV 
    4452                                        sprintf ( num,"%d",k ); 
     
    6270        memlog::init(); 
    6371 
     72        //Delete old data 
    6473        write_buffers ( 0 ); 
    6574} 
     
    8190        for ( i=0;i<nen;i++ ) { //for entries 
    8291                Dt = vectors ( i )._data(); 
    83                 DtRows = vectors(i).rows(); 
    84                  
     92                DtRows = vectors ( i ).rows(); 
     93 
    8594                jj=0; 
    8695                for ( j=0;j<entries ( i ).length();j++ ) { //for RVs in entries 
     
    93102                                                fp = open ( filename, O_CREAT | O_WRONLY | O_APPEND, 00644 ); 
    94103 
    95                                         write ( fp,&Dt[jj*(DtRows)],(Len)*sizeof ( double ) ); 
     104                                        write ( fp,&Dt[jj* ( DtRows ) ], ( Len ) *sizeof ( double ) ); 
    96105                                        close ( fp ); 
    97106 
     
    106115} 
    107116 
    108 void dirfilelog::step ( bool final ) { 
     117void dirfilelog::step ( ) { 
    109118 
    110         if ( final ) { 
    111                 if ( ind>0 ) 
    112                         write_buffers ( ind ); //assuming here that i+1 was not filled 
    113         } else 
    114                 if ( ind== ( maxlen -1 ) ) { 
    115                         write_buffers ( ind+1 ); 
    116                         ind = 0; 
    117                 } else 
    118                         ind++; 
     119        if ( ind== ( maxlen -1 ) ) { 
     120                write_buffers ( ind+1 ); 
     121                ind = 0; 
     122        } 
     123        else 
     124                ind++; 
    119125 
    120126} 
     127 
     128void dirfilelog::finalize ( ) { 
     129        if ( ind>0 ) 
     130                write_buffers ( ind ); //assuming here that i+1 was not filled 
     131} 
  • bdm/stat/loggers.h

    r145 r162  
    3737 
    3838//! returns an identifier which will be later needed for calling the log() function 
    39         virtual int add ( RV rv, string name="" ) { 
     39        virtual int add (const RV &rv, string name="" ) { 
    4040                int id=entries.length(); 
    4141                names=concat ( names, name ); // diff 
     
    4848        virtual void logit ( int id, vec v ) =0; 
    4949 
    50 //! consider this record to be complete 
    51         virtual void step(bool final=false) =0; 
     50//! Shifts storage position for another time step. 
     51        virtual void step() =0; 
     52 
     53        //! Finalize storing information 
     54        virtual void finalize() {}; 
    5255 
    5356        //! for future use 
     
    8285                ; 
    8386        } 
    84         void step(bool final=false) {if ( ind<maxlen ) ind++; else it_error ( "memlog::ind is too high;" );} 
    85         void logit ( int id, vec v ) {vectors ( id ).set_row ( ind,v );} 
     87        void step() {if ( ind<maxlen ) ind++; else it_error ( "memlog::ind is too high;" );} 
     88        void logit ( int id, vec v ) { 
     89                it_assert_debug(id<vectors.length(),"Logger was not initialized, run init()."); 
     90                vectors ( id ).set_row ( ind,v );} 
    8691        //! Save values into an itfile named after \c fname. 
    8792        void itsave(const char* fname); 
     
    110115        //! Initialize storage 
    111116        void init(); 
    112         void step(bool final=false); 
     117        void step(); 
     118        void finalize(); 
    113119        /*! \brief Write memory storage to disk.  
    114120        @param Len length of buffer to be written, if 0 the file is truncated at 0.