Changeset 685 for library/bdm

Show
Ignore:
Timestamp:
10/29/09 23:23:56 (15 years ago)
Author:
smidl
Message:

mex tutorial cleanup + working mexDS

Location:
library/bdm
Files:
5 modified

Legend:

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

    r682 r685  
    594594                } 
    595595        } 
     596        void log_write() const { 
     597                if (log_level>0) { 
     598                        logrec->L.logit( logrec->ids(0), mean() ); 
     599                } 
     600                if (log_level>2) { 
     601                        vec lb; vec ub; 
     602                        qbounds(lb,ub); 
     603                        logrec->L.logit( logrec->ids(1), lb ); 
     604                        logrec->L.logit( logrec->ids(2), ub ); 
     605                } 
     606        } 
    596607        //!@} 
    597608 
     
    951962public: 
    952963        //! default constructors 
    953                 DS() : Drv(), Urv(),Yrv() {log_level=1;}; 
     964        DS() : dtsize(0),utsize(0),ytsize(0),Drv(), Urv(),Yrv() {log_level=1;}; 
    954965 
    955966        //! Returns maximum number of provided data, by default it is set to maximum allowed length, shorter DS should overload this method! See, MemDS.max_length(). 
     
    9891000                if (log_level >0){ 
    9901001                        logrec->ids.set_size(2); 
    991                         logrec->ids(0) = logrec->L.add ( Yrv, "" ); 
    992                         logrec->ids(1) = logrec->L.add ( Urv, "" ); 
     1002                        logrec->ids(0) = logrec->L.add ( Yrv, prefix ); 
     1003                        logrec->ids(1) = logrec->L.add ( Urv, prefix ); 
    9931004                } 
    9941005        } 
     
    11941205        //!@} 
    11951206        void from_setting ( const Setting &set ) { 
    1196                 shared_ptr<RV> r = UI::build<RV> ( set, "drv", UI::optional ); 
     1207                shared_ptr<RV> r = UI::build<RV> ( set, "yrv", UI::optional ); 
    11971208                if ( r ) { 
    11981209                        set_yrv ( *r ); 
    11991210                } 
     1211                shared_ptr<RV> r2 = UI::build<RV> ( set, "rvc", UI::optional ); 
     1212                if ( r2 ) { 
     1213                        rvc =   *r2; 
     1214                } 
     1215                shared_ptr<RV> r3 = UI::build<RV> ( set, "rv", UI::optional ); 
     1216                if ( r3 ) { 
     1217                        set_rv(*r3); 
     1218                } 
     1219                 
    12001220                string opt; 
    12011221                if ( UI::get ( opt, set, "options", UI::optional ) ) { 
  • library/bdm/base/loggers.h

    r676 r685  
    3636public: 
    3737        //! convenience constructor 
    38         memlog ( int maxlen0, string itf = "" ) :logger("."), maxlen ( maxlen0 ), ind ( 0 ), vectors ( 0 ), itfilename ( itf ) {} 
     38        memlog ( int maxlen0, string itf = "" ) :logger("_"), maxlen ( maxlen0 ), ind ( 0 ), vectors ( 0 ), itfilename ( itf ) {} 
    3939 
    4040        //!Default constructor 
    41         memlog() : logger("."), maxlen ( 0 ), ind ( 0 ), vectors ( 0 ) {} 
     41        memlog() : logger("_"), maxlen ( 0 ), ind ( 0 ), vectors ( 0 ) {} 
    4242 
    4343        //! Initialize storage 
  • library/bdm/mex/mex_BM.h

    r679 r685  
    8080                        mxArray *in[2]; 
    8181                        in[0] = data; 
    82                         in[1] = mxCreateDoubleMatrix(dt.size(), 1, mxREAL); 
    83                         vec2mxArray(dt, in[1]); 
     82                        in[1] = mxCreateDoubleMatrix(yt.size(), 1, mxREAL); 
     83                        vec2mxArray(yt, in[1]); 
    8484                        mexCallMATLAB(1, &tmp, 2, in, (name+"_bayes").c_str()); 
    8585                        old = data; 
  • library/bdm/mex/mex_datasource.h

    r676 r685  
    9191        \code 
    9292        system={ 
    93                 type="mexDS"; 
     93                class="mexDS"; 
    9494                step_name="";              // name of function to call 
    9595                input_name="";             // name of workspace variable where inputs are written 
    96                 rv_out = {class='RV',...}  // identification of outputs 
    97                 rv_in = {class='RV',...}   // identification of inputs 
     96                yrv = {class='RV',...}  // identification of outputs 
     97                urv = {class='RV',...}   // identification of inputs 
    9898                }; 
    9999        \endcode 
     
    106106                UI::get(input_name, set, "input_name", UI::compulsory); 
    107107                 
    108                 shared_ptr<RV> ry = UI::build<RV> ( set, "rv_out", UI::compulsory ); 
    109                 shared_ptr<RV> ru = UI::build<RV> ( set, "rv_in", UI::compulsory); 
     108                shared_ptr<RV> ry = UI::build<RV> ( set, "yrv", UI::compulsory ); 
     109                shared_ptr<RV> ru = UI::build<RV> ( set, "urv", UI::compulsory); 
    110110 
    111                 dtsize=ry->_dsize(); 
     111                ytsize=ry->_dsize(); 
    112112                utsize=ru->_dsize(); 
    113  
     113                dtsize = ytsize+utsize; 
     114                 
    114115                set_drv(*ry, *ru); 
     116                validate(); 
    115117        } 
    116118         
    117119        void step() { 
    118120                mxArray* tmp; 
    119                 mxArray* tmp2; 
     121                mxArray* dummy=NULL; 
    120122                // write inputs to variable input_name 
    121123                mxArray* mxinp= mexGetVariable ( "global", input_name.c_str()) ; 
     124                if (mxinp){ 
     125                        if((int)mxGetM(mxinp)!=utsize || (int)mxGetN(mxinp)!=utsize) { 
     126                                // mxinp is invalid - create new one 
     127                                mxDestroyArray(mxinp); 
     128                                mxinp=mxCreateDoubleMatrix(utsize,1,mxREAL); 
     129                        } 
     130                         
     131                } else { 
     132                        mxinp=mxCreateDoubleMatrix(utsize,1,mxREAL); 
     133                } 
    122134                vec2mxArray(ut, mxinp); 
     135                mexPutVariable("global",input_name.c_str(),mxinp); 
    123136                // call function step_name 
    124                 mexCallMATLAB(1, &tmp, 0, (mxArray **) &tmp2, step_name.c_str()); 
     137                mexCallMATLAB(1, &tmp, 0, (mxArray **) &dummy, step_name.c_str()); 
    125138                // save its results 
    126                 dt=mxArray2vec(tmp); 
     139                bdm_assert_debug((int)mxGetM(tmp)==ytsize || (int)mxGetN(tmp)==ytsize,"mexDS.step() expected return vector of length " + num2str(dtsize) + 
     140                "got vector " + num2str((int)mxGetM(tmp)) + "x" + num2str((int)mxGetN(tmp))); 
     141                //write  y 
     142                dt.set_subvector(0,mxArray2vec(tmp)); 
     143                //write u 
     144                dt.set_subvector(ytsize,ut); 
    127145        } 
    128         void write(const vec &ut0){ ut=ut0;} 
    129         void getdata(vec &dt_out) const {dt_out = dt;   } 
     146        void write(const vec &ut0){  
     147                bdm_assert_debug(ut0.length()==ut.length(),"mexDS: Incompatible input vector"); 
     148                ut=ut0; 
     149        } 
     150        void getdata(vec &dt_out) const { 
     151                bdm_assert_debug(dt_out.length()==dt.length(),"mexDS: Incompatible output vector"); 
     152                dt_out = dt;     
     153        } 
    130154 
     155        void validate() { 
     156                dt=zeros(dtsize); 
     157                ut=zeros(utsize); 
     158        } 
    131159 
    132160        // TODO dodelat void to_setting( Setting &set ) const; 
  • library/bdm/mex/mex_logger.h

    r665 r685  
    6262                        istart = 0; 
    6363                        for ( int j = 0; j < entries ( i ).length(); j++ ) { // same for as in add!!! 
    64                                 vec_name = names ( i ) + entries ( i ).name ( j ); 
     64                                vec_name = names ( i ) +prefix_sep()+ entries ( i ).name ( j ); 
    6565                                iend = istart + entries ( i ).size ( j ) - 1; 
    6666                                M = vectors ( i ).get_cols ( istart, iend );