Changeset 738

Show
Ignore:
Timestamp:
11/25/09 12:46:08 (14 years ago)
Author:
mido
Message:

a few moves of code from h to cpp, however, only part of the whole library is done

Location:
library
Files:
11 modified

Legend:

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

    r737 r738  
    251251} 
    252252 
     253RV RV::expand_delayes() const { 
     254        RV rvt = this->remove_time(); //rv at t=0 
     255        RV tmp = rvt; 
     256        int td = mint(); 
     257        for ( int i = -1; i >= td; i-- ) { 
     258                rvt.t_plus ( -1 ); 
     259                tmp.add ( rvt ); //shift u1 
     260        } 
     261        return tmp; 
     262} 
     263 
    253264str RV::tostr() const { 
    254265        ivec idlist ( dsize ); 
     
    323334} 
    324335 
     336std::string RV::scalarname ( int scalat ) const { 
     337        bdm_assert ( scalat < dsize, "Wrong input index" ); 
     338        int id = 0; 
     339        int scalid = 0; 
     340        while ( scalid + SIZES ( ids ( id ) ) <= scalat )  { 
     341                scalid += SIZES ( ids ( id ) ); 
     342                id++; 
     343        }; 
     344        //now id is the id of variable of interest 
     345        if ( size ( id ) == 1 ) 
     346                return  NAMES ( ids ( id ) ); 
     347        else 
     348                return  NAMES ( ids ( id ) ) + "_" + num2str ( scalat - scalid ); 
     349 
     350} 
     351 
    325352ivec RV::findself ( const RV &rv2 ) const { 
    326353        int i, j; 
     
    398425} 
    399426 
     427int logger::add_vector ( const RV &rv, string prefix ) { 
     428        int id; 
     429        if ( rv._dsize() > 0 ) { 
     430                id = entries.length(); 
     431                names = concat ( names, prefix ); // diff 
     432                entries.set_length ( id + 1, true ); 
     433                entries ( id ) = rv; 
     434        } else { 
     435                id = -1; 
     436        } 
     437        return id; // identifier of the last entry 
     438} 
     439 
     440int logger::add_setting ( const string &prefix ) { 
     441        Setting &root = setting_conf.getRoot(); 
     442        int id = root.getLength(); //root must be group!! 
     443        if ( prefix.length() > 0 ) { 
     444                settings.set_length ( id + 1, true ); 
     445                settings ( id ) = &root.add ( prefix, Setting::TypeList ); 
     446        } else { 
     447                id = -1; 
     448        } 
     449        return id; 
     450} 
     451 
     452void epdf::log_register ( logger &L, const string &prefix ) { 
     453        RV r; 
     454        if ( isnamed() ) { 
     455                r = _rv(); 
     456        } else { 
     457                r = RV ( "", dimension() ); 
     458        }; 
     459        root::log_register ( L, prefix ); 
     460 
     461        // log full data 
     462        if ( log_level == 10 ) { 
     463                logrec->ids.set_size ( 1 ); 
     464                logrec->ids ( 0 ) = logrec->L.add_setting ( prefix ); 
     465        } else { 
     466                // log only 
     467                logrec->ids.set_size ( 3 ); 
     468                if ( log_level > 0 ) { 
     469                        logrec->ids ( 0 ) = logrec->L.add_vector ( r, prefix + logrec->L.prefix_sep() + "mean" ); 
     470                } 
     471                if ( log_level > 1 ) { 
     472                        logrec->ids ( 1 ) = logrec->L.add_vector ( r, prefix + logrec->L.prefix_sep() + "lb" ); 
     473                        logrec->ids ( 2 ) = logrec->L.add_vector ( r, prefix + logrec->L.prefix_sep() + "ub" ); 
     474                } 
     475        } 
     476} 
     477 
     478void epdf::log_write() const { 
     479        if ( log_level == 10 ) { 
     480                to_setting ( logrec->L.log_to_setting ( logrec->ids ( 0 ) ) ); 
     481        } else { 
     482                if ( log_level > 0 ) { 
     483                        logrec->L.log_vector ( logrec->ids ( 0 ), mean() ); 
     484                } 
     485                if ( log_level > 1 ) { 
     486                        vec lb; 
     487                        vec ub; 
     488                        qbounds ( lb, ub ); 
     489                        logrec->L.log_vector ( logrec->ids ( 1 ), lb ); 
     490                        logrec->L.log_vector ( logrec->ids ( 2 ), ub ); 
     491                } 
     492        } 
     493} 
     494 
     495void datalink_buffered::set_connection ( const RV &rv, const RV &rv_up ) { 
     496        // create link between up and down 
     497        datalink_part::set_connection ( rv, rv_up.remove_time() ); // only non-delayed version 
     498 
     499        // create rvs of history 
     500        // we can store only what we get in rv_up - everything else is removed 
     501        ivec valid_ids = rv.findself_ids ( rv_up ); 
     502        RV rv_hist = rv.subselect ( find ( valid_ids >= 0 ) ); // select only rvs that are in rv_up 
     503        RV rv_hist0 = rv_hist.remove_time(); // these RVs will form history at time =0 
     504        // now we need to know what is needed from Up 
     505        rv_hist = rv_hist.expand_delayes(); // full regressor - including time 0 
     506        Hrv = rv_hist.subt ( rv_hist0 );   // remove time 0 
     507        history = zeros ( Hrv._dsize() ); 
     508 
     509        // decide if we need to copy val to history 
     510        if ( Hrv._dsize() > 0 ) { 
     511                v2h_up = rv_hist0.dataind ( rv_up ); // indeces of elements of rv_up to be copied 
     512        } // else v2h_up is empty 
     513 
     514        Hrv.dataind ( rv, h2v_hist, h2v_down ); 
     515 
     516        downsize = v2v_down.length() + h2v_down.length(); 
     517        upsize = v2v_up.length(); 
     518} 
     519 
     520void datalink_buffered::set_history ( const RV& rv1, const vec &hist0 ) { 
     521        bdm_assert ( rv1._dsize() == hist0.length(), "hist is not compatible with given rv1" ); 
     522        ivec ind_H; 
     523        ivec ind_h0; 
     524        Hrv.dataind ( rv1, ind_H, ind_h0 ); // find indeces of rv in 
     525        set_subvector ( history, ind_H, hist0 ( ind_h0 ) ); // copy given hist to appropriate places 
     526} 
     527 
     528void DS::log_register ( logger &L,  const string &prefix ) { 
     529        bdm_assert ( ytsize == Yrv._dsize(), "invalid DS: ytsize (" + num2str ( ytsize ) + ") different from Drv " + num2str ( Yrv._dsize() ) ); 
     530        bdm_assert ( utsize == Urv._dsize(), "invalid DS: utsize (" + num2str ( utsize ) + ") different from Urv " + num2str ( Urv._dsize() ) ); 
     531 
     532        root::log_register ( L, prefix ); 
     533        //we know that 
     534        if ( log_level > 0 ) { 
     535                logrec->ids.set_size ( 2 ); 
     536                logrec->ids ( 0 ) = logrec->L.add_vector ( Yrv, prefix ); 
     537                logrec->ids ( 1 ) = logrec->L.add_vector ( Urv, prefix ); 
     538        } 
     539} 
     540 
     541void DS::log_write ( ) const { 
     542        if ( log_level > 0 ) { 
     543                vec tmp ( Yrv._dsize() + Urv._dsize() ); 
     544                getdata ( tmp ); 
     545                // d is first in getdata 
     546                logrec->L.log_vector ( logrec->ids ( 0 ), tmp.left ( Yrv._dsize() ) ); 
     547                // u follows after d in getdata 
     548                logrec->L.log_vector ( logrec->ids ( 1 ), tmp.mid ( Yrv._dsize(), Urv._dsize() ) ); 
     549        } 
     550} 
     551 
     552void BM::set_options ( const string &opt ) { 
     553        if ( opt.find ( "logfull" ) != string::npos ) { 
     554                const_cast<epdf&> ( posterior() ).set_log_level ( 10 ) ; 
     555        } else { 
     556                if ( opt.find ( "logbounds" ) != string::npos ) { 
     557                        const_cast<epdf&> ( posterior() ).set_log_level ( 2 ) ; 
     558                } else { 
     559                        const_cast<epdf&> ( posterior() ).set_log_level ( 1 ) ; 
     560                } 
     561                if ( opt.find ( "logll" ) != string::npos ) { 
     562                        log_level = 1; 
     563                } 
     564        } 
     565} 
     566 
     567void BM::log_register ( logger &L, const string &prefix ) { 
     568        root::log_register ( L, prefix ); 
     569 
     570        const_cast<epdf&> ( posterior() ).log_register ( L, prefix + L.prefix_sep() + "apost" ); 
     571 
     572        if ( ( log_level ) > 0 ) { 
     573                logrec->ids.set_size ( 1 ); 
     574                logrec->ids ( 0 ) = L.add_vector ( RV ( "", 1 ), prefix + L.prefix_sep() + "ll" ); 
     575        } 
     576} 
     577 
     578void BM::log_write ( ) const { 
     579        posterior().log_write(); 
     580        if ( log_level > 0 ) { 
     581                logrec->L.logit ( logrec->ids ( 0 ), ll ); 
     582        } 
     583} 
     584 
    400585void BM::bayes_batch ( const mat &Data, const vec &cond ) { 
    401586        for ( int t = 0; t < Data.cols(); t++ ) { 
     
    403588        } 
    404589} 
     590 
    405591void BM::bayes_batch ( const mat &Data, const mat &Cond ) { 
    406592        for ( int t = 0; t < Data.cols(); t++ ) { 
     
    408594        } 
    409595} 
    410 } 
     596 
     597} 
  • library/bdm/base/bdmbase.h

    r737 r738  
    186186        } 
    187187        //! returns name of a scalar at position scalat, i.e. it can be in the middle of vector name, in that case it adds "_%d" to it 
    188         std::string scalarname ( int scalat ) const { 
    189                 bdm_assert ( scalat < dsize, "Wrong input index" ); 
    190                 int id = 0; 
    191                 int scalid = 0; 
    192                 while ( scalid + SIZES ( ids ( id ) ) <= scalat )  { 
    193                         scalid += SIZES ( ids ( id ) ); 
    194                         id++; 
    195                 }; 
    196                 //now id is the id of variable of interest 
    197                 if ( size ( id ) == 1 ) 
    198                         return  NAMES ( ids ( id ) ); 
    199                 else 
    200                         return  NAMES ( ids ( id ) ) + "_" + num2str ( scalat - scalid ); 
    201  
    202         } 
     188        std::string scalarname ( int scalat ) const; 
     189 
    203190        void set_time ( int at, int time0 ) { 
    204191                times ( at ) = time0; 
     
    246233        } 
    247234        //! return rvs with expanded delayes and sorted in the order of: \f$ [ rv_{0}, rv_{-1},\ldots  rv_{max_delay}]\f$ 
    248         RV expand_delayes() const { 
    249                 RV rvt = this->remove_time(); //rv at t=0 
    250                 RV tmp = rvt; 
    251                 int td = mint(); 
    252                 for ( int i = -1; i >= td; i-- ) { 
    253                         rvt.t_plus ( -1 ); 
    254                         tmp.add ( rvt ); //shift u1 
    255                 } 
    256                 return tmp; 
    257         } 
     235        RV expand_delayes() const; 
    258236        //!@} 
    259237 
     
    336314        //! returns an identifier which will be later needed for calling the \c logit() function 
    337315        //! For empty RV it returns -1, this entry will be ignored by \c logit(). 
    338         virtual int add_vector ( const RV &rv, string prefix = "" ) { 
    339                 int id; 
    340                 if ( rv._dsize() > 0 ) { 
    341                         id = entries.length(); 
    342                         names = concat ( names, prefix ); // diff 
    343                         entries.set_length ( id + 1, true ); 
    344                         entries ( id ) = rv; 
    345                 } else { 
    346                         id = -1; 
    347                 } 
    348                 return id; // identifier of the last entry 
    349         } 
    350         virtual int add_setting ( const string &prefix ) { 
    351                 Setting &root = setting_conf.getRoot(); 
    352                 int id = root.getLength(); //root must be group!! 
    353                 if ( prefix.length() > 0 ) { 
    354                         settings.set_length ( id + 1, true ); 
    355                         settings ( id ) = &root.add ( prefix, Setting::TypeList ); 
    356                 } else { 
    357                         id = -1; 
    358                 } 
    359                 return id; 
    360         } 
     316        virtual int add_vector ( const RV &rv, string prefix = "" ); 
     317 
     318        virtual int add_setting ( const string &prefix ); 
    361319 
    362320        //! log this vector 
     
    622580        //!  #1 mean 
    623581        //!  #2 mean + lower & upper bound 
    624         void log_register ( logger &L, const string &prefix ) { 
    625                 RV r; 
    626                 if ( isnamed() ) { 
    627                         r = _rv(); 
    628                 } else { 
    629                         r = RV ( "", dimension() ); 
    630                 }; 
    631                 root::log_register ( L, prefix ); 
    632  
    633                 // log full data 
    634                 if ( log_level == 10 ) { 
    635                         logrec->ids.set_size ( 1 ); 
    636                         logrec->ids ( 0 ) = logrec->L.add_setting ( prefix ); 
    637                 } else { 
    638                         // log only 
    639                         logrec->ids.set_size ( 3 ); 
    640                         if ( log_level > 0 ) { 
    641                                 logrec->ids ( 0 ) = logrec->L.add_vector ( r, prefix + logrec->L.prefix_sep() + "mean" ); 
    642                         } 
    643                         if ( log_level > 1 ) { 
    644                                 logrec->ids ( 1 ) = logrec->L.add_vector ( r, prefix + logrec->L.prefix_sep() + "lb" ); 
    645                                 logrec->ids ( 2 ) = logrec->L.add_vector ( r, prefix + logrec->L.prefix_sep() + "ub" ); 
    646                         } 
    647                 } 
    648         } 
    649         void log_write() const { 
    650                 if ( log_level == 10 ) { 
    651                         to_setting ( logrec->L.log_to_setting ( logrec->ids ( 0 ) ) ); 
    652                 } else { 
    653                         if ( log_level > 0 ) { 
    654                                 logrec->L.log_vector ( logrec->ids ( 0 ), mean() ); 
    655                         } 
    656                         if ( log_level > 1 ) { 
    657                                 vec lb; 
    658                                 vec ub; 
    659                                 qbounds ( lb, ub ); 
    660                                 logrec->L.log_vector ( logrec->ids ( 1 ), lb ); 
    661                                 logrec->L.log_vector ( logrec->ids ( 2 ), ub ); 
    662                         } 
    663                 } 
    664         } 
     582        void log_register ( logger &L, const string &prefix ); 
     583 
     584        void log_write() const; 
    665585        //!@} 
    666586 
     
    868788        } 
    869789 
    870         void set_connection ( const RV &rv, const RV &rv_up ) { 
    871                 // create link between up and down 
    872                 datalink_part::set_connection ( rv, rv_up.remove_time() ); // only non-delayed version 
    873  
    874                 // create rvs of history 
    875                 // we can store only what we get in rv_up - everything else is removed 
    876                 ivec valid_ids = rv.findself_ids ( rv_up ); 
    877                 RV rv_hist = rv.subselect ( find ( valid_ids >= 0 ) ); // select only rvs that are in rv_up 
    878                 RV rv_hist0 = rv_hist.remove_time(); // these RVs will form history at time =0 
    879                 // now we need to know what is needed from Up 
    880                 rv_hist = rv_hist.expand_delayes(); // full regressor - including time 0 
    881                 Hrv = rv_hist.subt ( rv_hist0 );   // remove time 0 
    882                 history = zeros ( Hrv._dsize() ); 
    883  
    884                 // decide if we need to copy val to history 
    885                 if ( Hrv._dsize() > 0 ) { 
    886                         v2h_up = rv_hist0.dataind ( rv_up ); // indeces of elements of rv_up to be copied 
    887                 } // else v2h_up is empty 
    888  
    889                 Hrv.dataind ( rv, h2v_hist, h2v_down ); 
    890  
    891                 downsize = v2v_down.length() + h2v_down.length(); 
    892                 upsize = v2v_up.length(); 
    893         } 
     790        void set_connection ( const RV &rv, const RV &rv_up ); 
     791         
    894792        //! set history of variable given by \c rv1 to values of \c hist. 
    895         void set_history ( const RV& rv1, const vec &hist0 ) { 
    896                 bdm_assert ( rv1._dsize() == hist0.length(), "hist is not compatible with given rv1" ); 
    897                 ivec ind_H; 
    898                 ivec ind_h0; 
    899                 Hrv.dataind ( rv1, ind_H, ind_h0 ); // find indeces of rv in 
    900                 set_subvector ( history, ind_H, hist0 ( ind_h0 ) ); // copy given hist to appropriate places 
    901         } 
     793        void set_history ( const RV& rv1, const vec &hist0 ); 
    902794}; 
    903795 
     
    1060952 
    1061953        //! Register DS for logging into logger L 
    1062         virtual void log_register ( logger &L,  const string &prefix ) { 
    1063                 bdm_assert ( ytsize == Yrv._dsize(), "invalid DS: ytsize (" + num2str ( ytsize ) + ") different from Drv " + num2str ( Yrv._dsize() ) ); 
    1064                 bdm_assert ( utsize == Urv._dsize(), "invalid DS: utsize (" + num2str ( utsize ) + ") different from Urv " + num2str ( Urv._dsize() ) ); 
    1065  
    1066                 root::log_register ( L, prefix ); 
    1067                 //we know that 
    1068                 if ( log_level > 0 ) { 
    1069                         logrec->ids.set_size ( 2 ); 
    1070                         logrec->ids ( 0 ) = logrec->L.add_vector ( Yrv, prefix ); 
    1071                         logrec->ids ( 1 ) = logrec->L.add_vector ( Urv, prefix ); 
    1072                 } 
    1073         } 
     954        virtual void log_register ( logger &L,  const string &prefix ); 
    1074955        //! Register DS for logging into logger L 
    1075         virtual void log_write ( ) const { 
    1076                 if ( log_level > 0 ) { 
    1077                         vec tmp ( Yrv._dsize() + Urv._dsize() ); 
    1078                         getdata ( tmp ); 
    1079                         // d is first in getdata 
    1080                         logrec->L.log_vector ( logrec->ids ( 0 ), tmp.left ( Yrv._dsize() ) ); 
    1081                         // u follows after d in getdata 
    1082                         logrec->L.log_vector ( logrec->ids ( 1 ), tmp.mid ( Yrv._dsize(), Urv._dsize() ) ); 
    1083                 } 
    1084         } 
     956        virtual void log_write ( ) const; 
    1085957        //!access function 
    1086958        virtual const RV& _drv() const { 
     
    12451117 
    12461118        //! Set boolean options from a string, recognized are: "logbounds,logll" 
    1247         virtual void set_options ( const string &opt ) { 
    1248                 if ( opt.find ( "logfull" ) != string::npos ) { 
    1249                         const_cast<epdf&> ( posterior() ).set_log_level ( 10 ) ; 
    1250                 } else { 
    1251                         if ( opt.find ( "logbounds" ) != string::npos ) { 
    1252                                 const_cast<epdf&> ( posterior() ).set_log_level ( 2 ) ; 
    1253                         } else { 
    1254                                 const_cast<epdf&> ( posterior() ).set_log_level ( 1 ) ; 
    1255                         } 
    1256                         if ( opt.find ( "logll" ) != string::npos ) { 
    1257                                 log_level = 1; 
    1258                         } 
    1259                 } 
    1260         } 
     1119        virtual void set_options ( const string &opt ); 
    12611120 
    12621121        //! Add all logged variables to a logger 
     
    12641123        //!  * y = 0/1 log-likelihood is to be logged 
    12651124        //!  * x = level of the posterior (typically 0/1/2 for nothing/mean/bounds) 
    1266         virtual void log_register ( logger &L, const string &prefix = "" ) { 
    1267                 root::log_register ( L, prefix ); 
    1268  
    1269                 const_cast<epdf&> ( posterior() ).log_register ( L, prefix + L.prefix_sep() + "apost" ); 
    1270  
    1271                 if ( ( log_level ) > 0 ) { 
    1272                         logrec->ids.set_size ( 1 ); 
    1273                         logrec->ids ( 0 ) = L.add_vector ( RV ( "", 1 ), prefix + L.prefix_sep() + "ll" ); 
    1274                 } 
    1275         } 
     1125        virtual void log_register ( logger &L, const string &prefix = "" ); 
     1126 
    12761127        //! Save results to the given logger, details of what is stored is configured by \c LIDs and \c options 
    1277         virtual void log_write ( ) const { 
    1278                 posterior().log_write(); 
    1279                 if ( log_level > 0 ) { 
    1280                         logrec->L.logit ( logrec->ids ( 0 ), ll ); 
    1281                 } 
    1282         } 
     1128        virtual void log_write ( ) const; 
     1129 
    12831130        //!@} 
    12841131        void from_setting ( const Setting &set ) { 
  • library/bdm/base/datasources.cpp

    r737 r738  
    4040        time = 0; 
    4141        Data = Dat; 
     42} 
     43 
     44void PdfDS::step() { 
     45        yt2rgr.store_data ( yt ); // y is now history 
     46        ut2rgr.filldown ( ut, rgr ); 
     47        yt2rgr.filldown ( yt, rgr ); 
     48        yt = ipdf->samplecond ( rgr ); 
     49        ut2rgr.store_data ( ut ); //u is now history 
     50} 
     51 
     52void PdfDS::getdata ( vec &dt_out ) const { 
     53        bdm_assert_debug ( dt_out.length() >= utsize + ytsize, "Short output vector" ); 
     54        dt_out.set_subvector ( 0, yt ); 
     55        dt_out.set_subvector ( ytsize, ut ); 
     56} 
     57 
     58void StateDS::step() { 
     59        vec imc ( IM->dimensionc() ); 
     60        imc.set_subvector ( 0, xt ); 
     61        u2imc.filldown ( ut, imc ); 
     62        xt = IM->samplecond ( imc ); 
     63 
     64        vec omc ( OM->dimensionc() ); 
     65        omc.set_subvector ( 0, xt ); 
     66        u2omc.filldown ( ut, omc ); 
     67        vec yt; 
     68        yt = OM->samplecond ( omc ); 
     69        //fill all data 
     70        dt.set_subvector ( 0, yt ); 
     71        dt.set_subvector ( yt.length(), xt ); 
     72        dt.set_subvector ( ytsize, ut ); 
    4273} 
    4374 
  • library/bdm/base/datasources.h

    r737 r738  
    171171 
    172172public: 
    173         void step() { 
    174                 yt2rgr.store_data ( yt ); // y is now history 
    175                 ut2rgr.filldown ( ut, rgr ); 
    176                 yt2rgr.filldown ( yt, rgr ); 
    177                 yt = ipdf->samplecond ( rgr ); 
    178                 ut2rgr.store_data ( ut ); //u is now history 
    179         } 
    180         void getdata ( vec &dt_out ) const { 
    181                 bdm_assert_debug ( dt_out.length() >= utsize + ytsize, "Short output vector" ); 
    182                 dt_out.set_subvector ( 0, yt ); 
    183                 dt_out.set_subvector ( ytsize, ut ); 
    184         } 
     173        void step(); 
     174 
     175        void getdata ( vec &dt_out ) const; 
     176 
    185177        void write ( const vec &ut0 ) { 
    186178                ut = ut0; 
     
    350342        } 
    351343 
    352         virtual void step() { 
    353                 vec imc ( IM->dimensionc() ); 
    354                 imc.set_subvector ( 0, xt ); 
    355                 u2imc.filldown ( ut, imc ); 
    356                 xt = IM->samplecond ( imc ); 
    357  
    358                 vec omc ( OM->dimensionc() ); 
    359                 omc.set_subvector ( 0, xt ); 
    360                 u2omc.filldown ( ut, omc ); 
    361                 vec yt; 
    362                 yt = OM->samplecond ( omc ); 
    363                 //fill all data 
    364                 dt.set_subvector ( 0, yt ); 
    365                 dt.set_subvector ( yt.length(), xt ); 
    366                 dt.set_subvector ( ytsize, ut ); 
    367         } 
     344        virtual void step(); 
    368345 
    369346        //! set parameters 
  • library/bdm/design/ctrlbase.cpp

    r737 r738  
    77} 
    88 
     9void LQG::update_system() { 
     10        pr.set_submatrix ( 0, 0, S->_B() ); 
     11        pr.set_submatrix ( 0, dimu, S->_A() ); 
     12 
     13        //penalization 
     14        qux.set_submatrix ( 0, 0, Qu._Ch() ); 
     15        qux.set_submatrix ( 0, dimx + dimu + dimy, Qu._Ch() ); 
     16 
     17        qyx.set_submatrix ( 0, 0, S->_C() ); 
     18        qyx.set_submatrix ( 0, dimx, -eye ( dimy ) ); 
     19 
     20        // parts of QR 
     21        hqy = Qy.to_mat() * qyx * pr; 
     22 
     23        // pre_qr 
     24        pre_qr = concat_vertical ( s * pr, concat_vertical ( hqy, qux ) ); 
     25} 
     26         
    927void LQG::set_control_parameters ( const mat &Qy0, const mat &Qu0, const vec &y_req0, int horizon0 ) { 
    1028        Qy = Qy0; 
     
    3856} 
    3957 
     58void LQG::ricatti_step() { 
     59        pre_qr.set_submatrix ( 0, 0, s*pr ); 
     60        pre_qr.set_submatrix ( dimx + dimu + dimy, dimu + dimx, -Qy.to_mat() *y_req ); 
     61        if ( !qr ( pre_qr, post_qr ) ) { 
     62                bdm_warning ( "QR in LQG unstable" ); 
     63        } 
     64        triu ( post_qr ); 
     65        // hn(m+1:2*m+n+r,m+1:2*m+n+r); 
     66        s = post_qr.get ( dimu, 2 * dimu + dimx + dimy - 1, dimu, 2 * dimu + dimx + dimy - 1 ); 
     67}; 
     68 
     69void LQG::redesign() { 
     70        for ( td = horizon; td > 0; td-- ) { 
     71                update_system(); 
     72                ricatti_step(); 
     73        } 
     74        /*                      ws=hn(1:m,m+1:2*m+n+r); 
     75                                wsd=hn(1:m,1:m); 
     76                                Lklq=-inv(wsd)*ws;*/ 
     77        L = -inv ( post_qr.get ( 0, dimu - 1, 0, dimu - 1 ) ) * post_qr.get ( 0, dimu - 1, dimu, 2 * dimu + dimx + dimy - 1 ); 
     78} 
     79 
    4080 
    4181} 
  • library/bdm/design/ctrlbase.h

    r737 r738  
    106106        void set_system ( shared_ptr<StateSpace<chmat> > S0 ); 
    107107        //! update internal whan system has changed 
    108         void update_system() { 
    109                 pr.set_submatrix ( 0, 0, S->_B() ); 
    110                 pr.set_submatrix ( 0, dimu, S->_A() ); 
    111  
    112                 //penalization 
    113                 qux.set_submatrix ( 0, 0, Qu._Ch() ); 
    114                 qux.set_submatrix ( 0, dimx + dimu + dimy, Qu._Ch() ); 
    115  
    116                 qyx.set_submatrix ( 0, 0, S->_C() ); 
    117                 qyx.set_submatrix ( 0, dimx, -eye ( dimy ) ); 
    118  
    119                 // parts of QR 
    120                 hqy = Qy.to_mat() * qyx * pr; 
    121  
    122                 // pre_qr 
    123                 pre_qr = concat_vertical ( s * pr, concat_vertical ( hqy, qux ) ); 
    124         } 
     108        void update_system(); 
    125109        //! set penalization matrices and control horizon 
    126110        void set_control_parameters ( const mat &Qy0, const mat &Qu0, const vec &y_req0, int horizon0 ); 
     
    137121        //! function for future use which is called at each time td; Should call initialize()! 
    138122        //! redesign one step of the 
    139         void ricatti_step() { 
    140                 pre_qr.set_submatrix ( 0, 0, s*pr ); 
    141                 pre_qr.set_submatrix ( dimx + dimu + dimy, dimu + dimx, -Qy.to_mat() *y_req ); 
    142                 if ( !qr ( pre_qr, post_qr ) ) { 
    143                         bdm_warning ( "QR in LQG unstable" ); 
    144                 } 
    145                 triu ( post_qr ); 
    146                 // hn(m+1:2*m+n+r,m+1:2*m+n+r); 
    147                 s = post_qr.get ( dimu, 2 * dimu + dimx + dimy - 1, dimu, 2 * dimu + dimx + dimy - 1 ); 
    148         }; 
    149         void redesign() { 
    150                 for ( td = horizon; td > 0; td-- ) { 
    151                         update_system(); 
    152                         ricatti_step(); 
    153                 } 
    154                 /*                      ws=hn(1:m,m+1:2*m+n+r); 
    155                                         wsd=hn(1:m,1:m); 
    156                                         Lklq=-inv(wsd)*ws;*/ 
    157                 L = -inv ( post_qr.get ( 0, dimu - 1, 0, dimu - 1 ) ) * post_qr.get ( 0, dimu - 1, dimu, 2 * dimu + dimx + dimy - 1 ); 
    158         } 
     123        void ricatti_step(); 
     124         
     125        void redesign(); 
     126 
    159127        //! compute control action 
    160128        vec ctrlaction ( const vec &state, const vec &ukm ) const { 
  • library/bdm/estim/arx.cpp

    r737 r738  
    6868} 
    6969 
     70void ARX::flatten ( const BMEF* B ) { 
     71        const ARX* A = dynamic_cast<const ARX*> ( B ); 
     72        // nu should be equal to B.nu 
     73        est.pow ( A->posterior()._nu() / posterior()._nu() ); 
     74        if ( evalll ) { 
     75                last_lognc = est.lognc(); 
     76        } 
     77} 
     78 
    7079ARX* ARX::_copy_ ( ) const { 
    7180        ARX* Tmp = new ARX ( *this ); 
     
    98107} 
    99108 
     109enorm<ldmat>* ARX::epredictor() const { 
     110        bdm_assert_debug ( dimy == posterior()._V().rows() - 1, "Regressor is not only 1" ); 
     111        return epredictor ( vec_1 ( 1.0 ) ); 
     112} 
    100113 
    101114mlstudent* ARX::predictor_student ( ) const { 
  • library/bdm/estim/arx.h

    r737 r738  
    8282        }; 
    8383        double logpred ( const vec &yt ) const; 
    84         void flatten ( const BMEF* B ) { 
    85                 const ARX* A = dynamic_cast<const ARX*> ( B ); 
    86                 // nu should be equal to B.nu 
    87                 est.pow ( A->posterior()._nu() / posterior()._nu() ); 
    88                 if ( evalll ) { 
    89                         last_lognc = est.lognc(); 
    90                 } 
    91         } 
     84        void flatten ( const BMEF* B ); 
    9285        //! Conditioned version of the predictor 
    9386        enorm<ldmat>* epredictor ( const vec &rgr ) const; 
    9487        //! Predictor for empty regressor 
    95         enorm<ldmat>* epredictor() const { 
    96                 bdm_assert_debug ( dimy == posterior()._V().rows() - 1, "Regressor is not only 1" ); 
    97                 return epredictor ( vec_1 ( 1.0 ) ); 
    98         } 
     88        enorm<ldmat>* epredictor() const; 
    9989        //! conditional version of the predictor 
    10090        template<class sq_T> 
  • library/bdm/math/chmat.cpp

    r737 r738  
    55 
    66namespace bdm { 
     7 
     8void chmat::add ( const chmat &A2, double w ) { 
     9        bdm_assert_debug ( dim == A2.dim, "Matrices of unequal dimension" ); 
     10        mat pre = concat_vertical ( Ch, sqrt ( w ) * A2.Ch ); 
     11        mat post = zeros ( pre.rows(), pre.cols() ); 
     12        if ( !qr ( pre, post ) ) { 
     13                bdm_warning ( "Unstable QR in chmat add" ); 
     14        } 
     15        Ch = post ( 0, dim - 1, 0, dim - 1 ); 
     16}; 
    717 
    818void chmat::opupdt ( const vec &v, double w ) { 
  • library/bdm/math/chmat.h

    r737 r738  
    4444        void clear(); 
    4545        //! add another chmat \c A2 with weight \c w. 
    46         void add ( const chmat &A2, double w = 1.0 ) { 
    47                 bdm_assert_debug ( dim == A2.dim, "Matrices of unequal dimension" ); 
    48                 mat pre = concat_vertical ( Ch, sqrt ( w ) * A2.Ch ); 
    49                 mat post = zeros ( pre.rows(), pre.cols() ); 
    50                 if ( !qr ( pre, post ) ) { 
    51                         bdm_warning ( "Unstable QR in chmat add" ); 
    52                 } 
    53                 Ch = post ( 0, dim - 1, 0, dim - 1 ); 
    54         }; 
     46        void add ( const chmat &A2, double w = 1.0 ); 
    5547        //!Inversion in the same form, i.e. cholesky 
    5648        void inv ( chmat &Inv ) const   { 
  • library/tests/testsuite/randun_test.cpp

    r722 r738  
    55using namespace itpp; 
    66 
    7 TEST ( test_randun ) { 
     7TEST ( randun_test ) { 
    88        // matlab output obtained by 
    99        // >>clear all