Show
Ignore:
Timestamp:
09/03/09 00:27:23 (15 years ago)
Author:
smidl
Message:

new buffered datalink ( #32 ) and new datasources - all with minor trivial tests

Files:
1 modified

Legend:

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

    r596 r598  
    9999 
    100100        private: 
     101          enum enum_dummy {dummy}; 
    101102                //! auxiliary function used in constructor 
    102103                void init (const Array<std::string> &in_names, const ivec &in_sizes, const ivec &in_times); 
    103104                int init (const string &name, int size); 
     105                //! Private constructor from IDs, potentially dangerous since all ids must be valid! 
     106                //! dummy is there to prevent confusion with RV(" string"); 
     107                explicit RV (const ivec &ids0, enum_dummy dum): dsize(0), len(ids0.length()), ids(ids0), times(zeros_i(ids0.length())) { 
     108                  dsize = countsize(); 
     109                } 
    104110        public: 
    105111                //! \name Constructors 
     
    171177                //! Find indices of self in another rv, \return ivec of the same size as self. 
    172178                ivec findself (const RV &rv2) const; 
     179                //! Find indices of self in another rv, ignore time, \return ivec of the same size as self. 
     180                ivec findself_ids (const RV &rv2) const; 
    173181                //! Compare if \c rv2 is identical to this \c RV 
    174182                bool equal (const RV &rv2) const; 
     
    192200                //!@} 
    193201 
     202                //! @{ \name Auxiliary functions 
     203                //! returns rvs with time set to 0 and removed duplicates 
     204                RV remove_time() const {  return RV(unique(ids), dummy);        } 
     205                //! return rvs with expanded delayes and sorted in the order of: \f$ [ rv_{0}, rv_{-1}, rv_{ 
     206                RV expand_delayes() const { 
     207                  RV rvt=this->remove_time(); //rv at t=0 
     208                  RV tmp = rvt; 
     209                  int td = mint(); 
     210                  for ( int i = -1; i >= td; i-- ) { 
     211                        rvt.t ( -1 ); 
     212                        tmp.add ( rvt ); //shift u1 
     213                  } 
     214                  return tmp; 
     215                } 
     216                //!@} 
     217                 
    194218                //!\name Relation to vectors 
    195219                //!@{ 
     
    200224                //! Then, data can be copied via: data_of_this = cdata(ind); 
    201225                ivec dataind (const RV &crv) const; 
     226                //! same as dataind but this time crv should not be complete supperset of rv. 
     227                ivec dataind_part (const RV &crv) const; 
    202228                //! generate mutual indices when copying data between self and crv. 
    203229                //! Data are copied via: data_of_this(selfi) = data_of_rv2(rv2i) 
     
    461487                //! @{ 
    462488 
    463                 RV _rv() const { 
     489                const RV& _rv() const { 
    464490                        return ep->_rv(); 
    465491                } 
    466                 RV _rvc() { 
     492                const RV& _rvc() { 
    467493                        return rvc; 
    468494                } 
     
    570596                //! val-to-val link, indices of the upper val 
    571597                ivec v2v_up; 
    572  
     598                 
    573599        public: 
    574600                //! Constructor 
     
    588614                //! Get val for myself from val of "Up" 
    589615                vec pushdown (const vec &val_up) { 
     616                  vec tmp(downsize); 
     617                  filldown(val_up, tmp); 
     618                        return tmp; 
     619                } 
     620                //! Get val for vector val_down from val of "Up" 
     621                void filldown (const vec &val_up, vec &val_down) { 
    590622                        bdm_assert_debug (upsize == val_up.length(), "Wrong val_up"); 
    591                         return get_vec (val_up, v2v_up); 
     623                        val_down=val_up(v2v_up); 
    592624                } 
    593625 
     
    598630                        set_subvector (val_up, v2v_up, val); 
    599631                } 
     632                //! access functions 
     633                int _upsize(){return upsize;} 
     634                //! access functions 
     635                int _downsize(){return downsize;} 
     636}; 
     637 
     638/*! Extension of datalink to fill only part of Down 
     639*/ 
     640class datalink_part : public datalink{ 
     641  protected: 
     642        //! indeces of values in vector downsize 
     643        ivec v2v_down; 
     644  public: 
     645        void set_connection(const RV &rv, const RV &rv_up); 
     646                //! Get val for vector val_down from val of "Up" 
     647                void filldown (const vec &val_up, vec &val_down) { 
     648                        set_subvector(val_down, v2v_down, val_up(v2v_up)); 
     649                } 
     650}; 
     651 
     652/*! \brief Datalink that buffers delayed values - do not forget to call step() 
     653 
     654Up is current data, Down is their subset with possibly delayed values 
     655*/ 
     656class datalink_buffered: public datalink_part{ 
     657  protected: 
     658        //! History, ordered as \f$[Up_{t-1},Up_{t-2}, \ldots]\f$ 
     659        vec history; 
     660        //! rv of the history 
     661        RV Hrv; 
     662        //! h2v : indeces in down  
     663        ivec h2v_down; 
     664        //! h2v : indeces in history 
     665        ivec h2v_hist; 
     666  public: 
     667         
     668    datalink_buffered():datalink_part(),history(0), h2v_down(0), h2v_hist(0){}; 
     669        //! push current data to history 
     670        void step(const vec &val_up){ 
     671                if (Hrv._dsize()>0){ 
     672                  history.shift_right ( 0, Hrv._dsize() ); 
     673                  history.set_subvector ( 0, val_up ); // write U after Drv 
     674                } 
     675        } 
     676                //! Get val for myself from val of "Up" 
     677                vec pushdown (const vec &val_up) { 
     678                  vec tmp(downsize); 
     679                  filldown(val_up, tmp); 
     680                        return tmp; 
     681                } 
     682         
     683        void filldown(const vec &val_up, vec &val_down){ 
     684          bdm_assert_debug(val_down.length()>=downsize, "short val_down"); 
     685         
     686          set_subvector(val_down, v2v_down, val_up(v2v_up)); // copy direct values 
     687          set_subvector(val_down, h2v_down, history(h2v_hist)); // copy delayed values 
     688        } 
     689         
     690        void set_connection(const RV &rv, const RV &rv_up){ 
     691          // create link between up and down 
     692          datalink_part::set_connection(rv, rv_up); 
     693           
     694          // create rvs of history  
     695          // we can store only what we get in rv_up - everything else is removed 
     696          ivec valid_ids=rv.findself_ids(rv_up); 
     697          RV rv_hist = rv.subselect( find(valid_ids>=0) ); // select only rvs that are in rv_up 
     698          rv_hist = rv_hist.expand_delayes(); // full regressor - including time 0 
     699          Hrv=rv_hist.subt(rv.remove_time());   // remove time 0 
     700          history = zeros(Hrv._dsize()); 
     701           
     702          Hrv.dataind(rv,h2v_hist,h2v_down); 
     703           
     704          downsize = v2v_down.length()+h2v_down.length(); 
     705          upsize = v2v_up.length(); 
     706        } 
     707}; 
     708 
     709//! buffered datalink from 2 vectors to 1 
     710class datalink_2to1_buffered{ 
     711  protected: 
     712        datalink_buffered dl1; 
     713        datalink_buffered dl2; 
     714  public: 
     715        //! set connection between RVs 
     716        void set_connection(const RV &rv, const RV &rv_up1, const RV &rv_up2){ 
     717          dl1.set_connection(rv,rv_up1); 
     718          dl2.set_connection(rv,rv_up2); 
     719        } 
     720        void filldown(const vec &val1, const vec &val2, vec &val_down){ 
     721          bdm_assert_debug(val_down.length()>=dl1._downsize()+dl2._downsize(), "short val_down"); 
     722          dl1.filldown(val1, val_down); 
     723          dl2.filldown(val2, val_down); 
     724        } 
     725        void step(const vec &dt, const vec &ut) {dl1.step(dt); dl2.step(ut);} 
    600726}; 
    601727 
     
    805931                } 
    806932                //!access function 
    807                 virtual RV _drv() const { 
     933                virtual const RV& _drv() const { 
    808934        //              return concat (Drv, Urv);// why!!! 
    809935                        return Drv;// why!!!