Changeset 737 for library/bdm/mex
- Timestamp:
- 11/25/09 12:14:38 (15 years ago)
- Location:
- library/bdm/mex
- Files:
-
- 5 modified
Legend:
- Unmodified
- Added
- Removed
-
library/bdm/mex/config2mxstruct.h
r728 r737 32 32 if ( !setting.isArray() ) mexErrMsgTxt ( "Given setting is not an array" ); 33 33 mxArray *result = mxCreateDoubleMatrix ( 1, setting.getLength(), mxREAL ); 34 double *elements = mxGetPr (result);34 double *elements = mxGetPr ( result ); 35 35 for ( int i = 0; i < setting.getLength(); i++ ) { 36 36 if ( setting.getType() == Setting::TypeInt ) { //TODO: tady je chyba -- zaporna cisla nejsou TypeInt 37 elements[i] = ( int) setting[i];37 elements[i] = ( int ) setting[i]; 38 38 } else { 39 39 elements[i] = setting[i]; … … 45 45 //! Convert libconfig's array to Matlab matrix 46 46 mxArray* list2mxmatrix ( const Setting &setting ) { 47 if ( !setting.isList() || ( strcmp ("matrix", setting[0])!=0 ) )47 if ( !setting.isList() || ( strcmp ( "matrix", setting[0] ) != 0 ) ) 48 48 mexErrMsgTxt ( "Given setting is not a matrix" ); 49 49 int rows = setting[1]; … … 62 62 return result; 63 63 } 64 64 65 65 //! Convert libconfig's gourp to Matlab structure 66 66 mxArray* group2mxstruct ( const Setting &setting ) { … … 114 114 return result; 115 115 } 116 117 if ( ( setting[0].getType() == Setting::TypeString ) ){118 string s =(setting[0]);119 if ( s=="matrix") {116 117 if ( ( setting[0].getType() == Setting::TypeString ) ) { 118 string s = ( setting[0] ); 119 if ( s == "matrix" ) { 120 120 return list2mxmatrix ( setting ); 121 121 } -
library/bdm/mex/mex_BM.h
r706 r737 6 6 namespace bdm { 7 7 /*! 8 * \brief Wrapper of BM mapping BM's methods to matlab functions 8 * \brief Wrapper of BM mapping BM's methods to matlab functions 9 9 10 10 The data are stored in an internal matrix \c Data . Each column of Data corresponds to one discrete time observation \f$t\f$. Access to this matrix is via indices \c rowid and \c delays. … … 16 16 17 17 //! epdf with functions implemented in matlab 18 class mexEpdf: public epdf {19 20 21 22 23 24 25 26 void from_setting(const Setting &S) {27 name = (const char *) S["name"];28 UImxConfig conf(S);29 data = mxDuplicateArray(conf.mxconfig);30 31 32 33 34 35 }36 37 38 string fname = name+"_mean";39 mexCallMATLAB(1, &tmp, 1, (mxArray **) &data, fname.c_str());40 return mxArray2vec(tmp);41 }18 class mexEpdf: public epdf { 19 protected: 20 //! prefix of matlab functions 21 string name; 22 //! pointer to storage structure 23 mxArray *data; 24 public: 25 mexEpdf() {}; 26 void from_setting ( const Setting &S ) { 27 name = ( const char * ) S["name"]; 28 UImxConfig conf ( S ); 29 data = mxDuplicateArray ( conf.mxconfig ); 30 //mexCallMATLAB(1, &data, 0, 0, (name+"_new").c_str()); 31 //TODO (future...): 32 //mxArray * init_data = setting2mxarray S["init_data"]; 33 //mexCallMATLAB(1, &data, 1, &init_data, name+"_from_setting"); 34 //delete init_data; 35 } 36 vec mean() const { 37 mxArray *tmp; 38 string fname = name + "_mean"; 39 mexCallMATLAB ( 1, &tmp, 1, ( mxArray ** ) &data, fname.c_str() ); 40 return mxArray2vec ( tmp ); 41 } 42 42 }; 43 UIREGISTER (mexEpdf);43 UIREGISTER ( mexEpdf ); 44 44 45 45 //! BM with functions implemented in matlab 46 class mexBM: public BM {47 48 49 50 51 52 53 54 55 46 class mexBM: public BM { 47 protected : 48 //! prefix of matlab functions 49 string name; 50 //! internal estimator 51 mexEpdf est; 52 //! mxArray with attributes of this object 53 mxArray *data; 54 public: 55 mexBM() {} 56 56 57 58 59 60 return mxDuplicateArray(data);61 57 //! duplicate internal data pointer? 58 mxArray *get_data() { 59 //mexCallMATLAB(0, NULL, 1, &data, "dump"); 60 return mxDuplicateArray ( data ); 61 } 62 62 63 void from_setting(const Setting &S) {64 BM::from_setting(S);65 name = (const char *) S["name"];66 UImxConfig conf(S);67 data = mxDuplicateArray(conf.mxconfig);68 69 70 71 //only member object there could be a structure of72 73 74 Setting &posterior = S["posterior"];75 est.from_setting(posterior);76 77 void bayes(const vec &yt, const vec &cond) {63 void from_setting ( const Setting &S ) { 64 BM::from_setting ( S ); 65 name = ( const char * ) S["name"]; 66 UImxConfig conf ( S ); 67 data = mxDuplicateArray ( conf.mxconfig ); 68 //string fname = name+"_new"; 69 //mexCallMATLAB(1, &data, 0, 0, (name+"_new").c_str()); 70 //the following works as long as the posterior is the 71 //only member object there could be a structure of 72 //member objects in the setting and a for cycle over 73 //all of them right here in the code 74 Setting &posterior = S["posterior"]; 75 est.from_setting ( posterior ); 76 } 77 void bayes ( const vec &yt, const vec &cond ) { 78 78 //void bayes() { 79 80 81 82 in[1] = mxCreateDoubleMatrix(yt.size(), 1, mxREAL);83 vec2mxArray(yt, in[1]);84 mexCallMATLAB(1, &tmp, 2, in, (name+"_bayes").c_str());85 86 data = mxDuplicateArray(tmp);87 if (old) mxDestroyArray(old);88 if (tmp) mxDestroyArray(tmp);89 90 }91 92 93 94 79 mxArray *tmp, *old; 80 mxArray *in[2]; 81 in[0] = data; 82 in[1] = mxCreateDoubleMatrix ( yt.size(), 1, mxREAL ); 83 vec2mxArray ( yt, in[1] ); 84 mexCallMATLAB ( 1, &tmp, 2, in, ( name + "_bayes" ).c_str() ); 85 old = data; 86 data = mxDuplicateArray ( tmp ); 87 if ( old ) mxDestroyArray ( old ); 88 if ( tmp ) mxDestroyArray ( tmp ); 89 //mexCallMATLAB(0, NULL, 1, &data, "dump"); 90 } 91 //! return correctly typed posterior (covariant return) 92 const mexEpdf& posterior() const { 93 return est; 94 } //tohle by melo zustat!! 95 95 }; 96 UIREGISTER (mexBM);96 UIREGISTER ( mexBM ); 97 97 98 98 } -
library/bdm/mex/mex_datasource.h
r706 r737 16 16 */ 17 17 class mxArrayDS : public MemDS { 18 18 public: 19 19 //!Default constructor 20 mxArrayDS () :MemDS() {};20 mxArrayDS () : MemDS() {}; 21 21 22 22 /*! \brief Create memory data source from mxArray … … 26 26 type="mexDS"; 27 27 varname=""; // name of workspace variable 28 row_rv = {class='RV',...} // definition of 28 row_rv = {class='RV',...} // definition of 29 29 }; 30 30 \endcode … … 35 35 void from_setting ( const Setting &set ) { 36 36 Data = mxArray2mat ( mexGetVariable ( "base", set["varname"] ) ); 37 /* UI::get ( rowid, set, "rids" , UI::compulsory );38 bdm_assert_debug ( max ( rowid ) <= Data.rows(), "MemDS rowid is too high for given Dat." );37 /* UI::get ( rowid, set, "rids" , UI::compulsory ); 38 bdm_assert_debug ( max ( rowid ) <= Data.rows(), "MemDS rowid is too high for given Dat." ); 39 39 40 UI::get ( delays, set, "tds", UI::compulsory );41 time = max ( delays );42 bdm_assert_debug ( time < Data.cols(), "MemDS delays are too high." );43 */40 UI::get ( delays, set, "tds", UI::compulsory ); 41 time = max ( delays ); 42 bdm_assert_debug ( time < Data.cols(), "MemDS delays are too high." ); 43 */ 44 44 //set MemDS 45 rowid = linspace (0,Data.rows()-1);46 dtsize =rowid.length();47 ytsize =rowid.length();48 utsize =0;49 45 rowid = linspace ( 0, Data.rows() - 1 ); 46 dtsize = rowid.length(); 47 ytsize = rowid.length(); 48 utsize = 0; 49 50 50 shared_ptr<RV> r = UI::build<RV> ( set, "rv", UI::optional ); 51 51 RV ru = RV(); 52 if ( r){52 if ( r ) { 53 53 set_drv ( *r, ru ); 54 54 } else { 55 RV def ((const char*)set["varname"],Data.rows());56 set_drv (def, ru);55 RV def ( ( const char* ) set["varname"], Data.rows() ); 56 set_drv ( def, ru ); 57 57 } 58 58 } … … 69 69 70 70 The identifier of matlab function is stored in attribute \c name. 71 This identifier defines: 71 This identifier defines: 72 72 \li function to call to do a step(): name_step.m 73 73 \li workspace variable to write input to: name_input 74 74 */ 75 75 class mexDS : public DS { 76 77 //! identifier of matlab function78 79 80 81 82 83 //! cache of inputs84 85 76 protected: 77 //! identifier of matlab function 78 string step_name; 79 //! identifier of matlab input variabel 80 string input_name; 81 //! cache of results from name_step 82 vec dt; 83 //! cache of inputs 84 vec ut; 85 public: 86 86 //!Default constructor 87 mexDS () :DS() {};87 mexDS () : DS() {}; 88 88 89 89 /*! \brief Create memory data source from mxArray … … 103 103 */ 104 104 void from_setting ( const Setting &set ) { 105 UI::get (step_name, set, "step_name", UI::compulsory);106 UI::get (input_name, set, "input_name", UI::compulsory);107 105 UI::get ( step_name, set, "step_name", UI::compulsory ); 106 UI::get ( input_name, set, "input_name", UI::compulsory ); 107 108 108 shared_ptr<RV> ry = UI::build<RV> ( set, "yrv", UI::compulsory ); 109 shared_ptr<RV> ru = UI::build<RV> ( set, "urv", UI::compulsory );109 shared_ptr<RV> ru = UI::build<RV> ( set, "urv", UI::compulsory ); 110 110 111 ytsize =ry->_dsize();112 utsize =ru->_dsize();113 dtsize = ytsize +utsize;114 115 set_drv (*ry, *ru);111 ytsize = ry->_dsize(); 112 utsize = ru->_dsize(); 113 dtsize = ytsize + utsize; 114 115 set_drv ( *ry, *ru ); 116 116 validate(); 117 117 } 118 118 119 119 void step() { 120 120 mxArray* tmp; 121 mxArray* dummy =NULL;121 mxArray* dummy = NULL; 122 122 // write inputs to variable input_name 123 mxArray* mxinp = mexGetVariable ( "global", input_name.c_str()) ;124 if ( mxinp){125 if ((int)mxGetM(mxinp)!=utsize || (int)mxGetN(mxinp)!=utsize) {123 mxArray* mxinp = mexGetVariable ( "global", input_name.c_str() ) ; 124 if ( mxinp ) { 125 if ( ( int ) mxGetM ( mxinp ) != utsize || ( int ) mxGetN ( mxinp ) != utsize ) { 126 126 // mxinp is invalid - create new one 127 mxDestroyArray (mxinp);128 mxinp =mxCreateDoubleMatrix(utsize,1,mxREAL);127 mxDestroyArray ( mxinp ); 128 mxinp = mxCreateDoubleMatrix ( utsize, 1, mxREAL ); 129 129 } 130 130 131 131 } else { 132 mxinp =mxCreateDoubleMatrix(utsize,1,mxREAL);132 mxinp = mxCreateDoubleMatrix ( utsize, 1, mxREAL ); 133 133 } 134 vec2mxArray (ut, mxinp);135 mexPutVariable ("global",input_name.c_str(),mxinp);134 vec2mxArray ( ut, mxinp ); 135 mexPutVariable ( "global", input_name.c_str(), mxinp ); 136 136 // call function step_name 137 mexCallMATLAB (1, &tmp, 0, (mxArray **) &dummy, step_name.c_str());137 mexCallMATLAB ( 1, &tmp, 0, ( mxArray ** ) &dummy, step_name.c_str() ); 138 138 // save its results 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)));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 141 //write y 142 dt.set_subvector (0,mxArray2vec(tmp));142 dt.set_subvector ( 0, mxArray2vec ( tmp ) ); 143 143 //write u 144 dt.set_subvector (ytsize,ut);144 dt.set_subvector ( ytsize, ut ); 145 145 } 146 void write (const vec &ut0){147 bdm_assert_debug (ut0.length()==ut.length(),"mexDS: Incompatible input vector");148 ut =ut0;146 void write ( const vec &ut0 ) { 147 bdm_assert_debug ( ut0.length() == ut.length(), "mexDS: Incompatible input vector" ); 148 ut = ut0; 149 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; 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 153 } 154 154 155 155 void validate() { 156 dt =zeros(dtsize);157 ut =zeros(utsize);156 dt = zeros ( dtsize ); 157 ut = zeros ( utsize ); 158 158 } 159 159 -
library/bdm/mex/mex_logger.h
r728 r737 36 36 for ( r = 0; r < rows; r++ ) { 37 37 *temp++ = in ( c ) ( r ); 38 38 39 39 } 40 40 } … … 48 48 mexlog() : memlog ( 0, "" ) {}; 49 49 //! constructor 50 mexlog (long maxlen0) : memlog ( maxlen0, "" ) {};50 mexlog ( long maxlen0 ) : memlog ( maxlen0, "" ) {}; 51 51 //! copy internal data to output mxArray 52 52 mxArray* toCell() { … … 62 62 istart = 0; 63 63 for ( int j = 0; j < entries ( i ).length(); j++ ) { // same for as in add!!! 64 vec_name = names ( i ) + prefix_sep()+ entries ( i ).name ( j );64 vec_name = names ( i ) + prefix_sep() + entries ( i ).name ( j ); 65 65 iend = istart + entries ( i ).size ( j ) - 1; 66 66 M = vectors ( i ).get_cols ( istart, iend ); … … 77 77 maxlen = root["maxlen"]; 78 78 } 79 Config& _setting_conf(){return setting_conf;} 79 Config& _setting_conf() { 80 return setting_conf; 81 } 80 82 }; 81 83 UIREGISTER ( mexlog ); -
library/bdm/mex/mex_parser.h
r706 r737 53 53 //treat empty matrices independently 54 54 mat val; 55 if ( mxGetM(value)>0) {55 if ( mxGetM ( value ) > 0 ) { 56 56 val = mxArray2mat ( value ); 57 57 } … … 112 112 mexErrMsgTxt ( "Given mxArray is not a cell." ); 113 113 }; 114 for ( unsigned int i = 0; i < ( unsigned int) mxGetNumberOfElements ( mxarray ); i++ ) {114 for ( unsigned int i = 0; i < ( unsigned int ) mxGetNumberOfElements ( mxarray ); i++ ) { 115 115 mxArray *value = mxGetCell ( mxarray, i ); 116 116 if ( mxIsChar ( value ) ) {