Changeset 477 for library/bdm/base/bdmbase.cpp
- Timestamp:
- 08/05/09 14:40:03 (15 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
library/bdm/base/bdmbase.cpp
r471 r477 5 5 namespace bdm { 6 6 7 const int RV_BUFFER_STEP =1;7 const int RV_BUFFER_STEP = 1; 8 8 RVmap RV_MAP; 9 9 Array<string> RV_NAMES ( RV_BUFFER_STEP ); 10 10 ivec RV_SIZES ( RV_BUFFER_STEP ); 11 11 12 RV RV0=RV(); 13 14 void RV::clear_all() 15 { 16 RV_MAP.clear(); 17 RV_SIZES.clear(); 18 RV_NAMES = Array<string>(RV_BUFFER_STEP); 19 } 20 12 RV RV0 = RV(); 13 14 void RV::clear_all() { 15 RV_MAP.clear(); 16 RV_SIZES.clear(); 17 RV_NAMES = Array<string> ( RV_BUFFER_STEP ); 18 } 19 21 20 int RV::init ( const string &name, int size ) { 22 21 //Refer … … 24 23 RVmap::const_iterator iter = RV_MAP.find ( name ); 25 24 if ( iter == RV_MAP.end() ) { 26 id =RV_MAP.size() +1;25 id = RV_MAP.size() + 1; 27 26 //debug 28 /* { 29 cout << endl; 30 RVmap::const_iterator iter = RV_MAP.begin(); 31 for(RVmap::const_iterator iter=RV_MAP.begin(); iter!=RV_MAP.end(); iter++){ 32 cout << "key: " << iter->first << " val: " << iter->second <<endl; 33 } 34 }*/ 35 36 RV_MAP.insert ( make_pair ( name,id ) ); //add new rv 37 if ( id>=RV_NAMES.length() ) { 38 RV_NAMES.set_length ( id+RV_BUFFER_STEP,true ); 39 RV_SIZES.set_length ( id+RV_BUFFER_STEP,true ); 40 } 41 RV_NAMES ( id ) =name; 42 RV_SIZES ( id ) =size; 43 } 44 else { 27 /* { 28 cout << endl; 29 RVmap::const_iterator iter = RV_MAP.begin(); 30 for(RVmap::const_iterator iter=RV_MAP.begin(); iter!=RV_MAP.end(); iter++){ 31 cout << "key: " << iter->first << " val: " << iter->second <<endl; 32 } 33 }*/ 34 35 RV_MAP.insert ( make_pair ( name, id ) ); //add new rv 36 if ( id >= RV_NAMES.length() ) { 37 RV_NAMES.set_length ( id + RV_BUFFER_STEP, true ); 38 RV_SIZES.set_length ( id + RV_BUFFER_STEP, true ); 39 } 40 RV_NAMES ( id ) = name; 41 RV_SIZES ( id ) = size; 42 } else { 45 43 id = iter->second; 46 it_assert (RV_SIZES(id)==size,"RV "+name+" of different size already exists");44 it_assert ( RV_SIZES ( id ) == size, "RV " + name + " of different size already exists" ); 47 45 } 48 46 return id; 49 47 }; 50 48 51 int RV::countsize() const{ int tmp=0; for ( int i=0;i<len;i++ ) {tmp+=RV_SIZES ( ids ( i ) );} return tmp;} 49 int RV::countsize() const { 50 int tmp = 0; 51 for ( int i = 0; i < len; i++ ) { 52 tmp += RV_SIZES ( ids ( i ) ); 53 } 54 return tmp; 55 } 52 56 53 57 ivec RV::cumsizes() const { 54 58 ivec szs ( len ); 55 int tmp =0;56 for ( int i =0;i<len;i++ ) {57 tmp +=RV_SIZES ( ids ( i ) );59 int tmp = 0; 60 for ( int i = 0; i < len; i++ ) { 61 tmp += RV_SIZES ( ids ( i ) ); 58 62 szs ( i ) = tmp; 59 63 } … … 61 65 } 62 66 63 void RV::init(const Array<std::string> &in_names, const ivec &in_sizes, const ivec &in_times) 64 { 67 void RV::init ( const Array<std::string> &in_names, const ivec &in_sizes, const ivec &in_times ) { 65 68 len = in_names.length(); 66 it_assert_debug ( in_names.length() == in_times.length(), "check \"times\" " );67 it_assert_debug ( in_names.length() == in_sizes.length(), "check \"sizes\" " );69 it_assert_debug ( in_names.length() == in_times.length(), "check \"times\" " ); 70 it_assert_debug ( in_names.length() == in_sizes.length(), "check \"sizes\" " ); 68 71 69 72 times.set_length ( len ); 70 73 ids.set_length ( len ); 71 74 int id; 72 for ( int i =0; i<len; i++ ) {75 for ( int i = 0; i < len; i++ ) { 73 76 id = init ( in_names ( i ), in_sizes ( i ) ); 74 77 ids ( i ) = id; … … 79 82 80 83 RV::RV ( string name, int sz, int tm ) { 81 Array<string> A ( 1 ); A ( 0 ) =name; 82 init ( A,vec_1 ( sz ),vec_1 ( tm ) ); 84 Array<string> A ( 1 ); 85 A ( 0 ) = name; 86 init ( A, vec_1 ( sz ), vec_1 ( tm ) ); 83 87 } 84 88 85 89 bool RV::add ( const RV &rv2 ) { 86 90 // TODO 87 if ( rv2.len >0 ) { //rv2 is nonempty91 if ( rv2.len > 0 ) { //rv2 is nonempty 88 92 ivec ind = rv2.findself ( *this ); //should be -1 all the time 89 ivec index = itpp::find ( ind ==-1 );93 ivec index = itpp::find ( ind == -1 ); 90 94 91 95 if ( index.length() < rv2.len ) { //conflict 92 96 ids = concat ( ids, rv2.ids ( index ) ); 93 97 times = concat ( times, rv2.times ( index ) ); 94 } 95 else { 98 } else { 96 99 ids = concat ( ids, rv2.ids ); 97 100 times = concat ( times, rv2.times ); … … 99 102 len = ids.length(); 100 103 dsize = countsize(); 101 return ( index.length() ==rv2.len ); //conflict or not 102 } 103 else { //rv2 is empty 104 return ( index.length() == rv2.len ); //conflict or not 105 } else { //rv2 is empty 104 106 return true; // no conflict 105 107 } … … 109 111 RV ret; 110 112 ret.ids = ids ( ind ); 111 ret.times = times ( ind );113 ret.times = times ( ind ); 112 114 ret.len = ind.length(); 113 ret.dsize =ret.countsize();115 ret.dsize = ret.countsize(); 114 116 return ret; 115 117 } 116 118 117 RV RV::operator()(int di1, int di2) const 118 { 119 ivec sz = cumsizes(); 120 int i1 = 0; 121 while (sz(i1) < di1) i1++; 122 int i2 = i1; 123 while (sz(i2) < di2) i2++; 124 return subselect(linspace(i1, i2)); 125 } 126 127 void RV::t ( int delta ) { times += delta;} 119 RV RV::operator() ( int di1, int di2 ) const { 120 ivec sz = cumsizes(); 121 int i1 = 0; 122 while ( sz ( i1 ) < di1 ) i1++; 123 int i2 = i1; 124 while ( sz ( i2 ) < di2 ) i2++; 125 return subselect ( linspace ( i1, i2 ) ); 126 } 127 128 void RV::t ( int delta ) { 129 times += delta; 130 } 128 131 129 132 bool RV::equal ( const RV &rv2 ) const { … … 133 136 mat epdf::sample_m ( int N ) const { 134 137 mat X = zeros ( dim, N ); 135 for ( int i = 0; i < N;i++ ) X.set_col ( i, this->sample() );138 for ( int i = 0; i < N; i++ ) X.set_col ( i, this->sample() ); 136 139 return X; 137 140 } 138 141 139 vec mpdf::samplecond (const vec &cond) {140 condition(cond);141 142 143 } 144 145 mat mpdf::samplecond_m (const vec &cond, int N) {146 condition(cond);147 mat temp(shep->dimension(), N);148 vec smp(shep->dimension());149 for (int i = 0; i < N; i++) {150 151 temp.set_col(i, smp);152 153 154 155 } 156 157 double mpdf::evallogcond (const vec &dt, const vec &cond) {158 159 condition(cond);160 tmp = shep->evallog(dt);161 162 163 } 164 165 vec mpdf::evallogcond_m (const mat &Dt, const vec &cond) {166 condition(cond);167 return shep->evallog_m(Dt);168 } 169 170 vec mpdf::evallogcond_m (const Array<vec> &Dt, const vec &cond) {171 condition(cond);172 return shep->evallog_m(Dt);173 } 174 175 void mpdf::from_setting (const Setting &set){176 RV *r = UI::build<RV> (set, "rv");177 if (r) {178 set_rv (*r);142 vec mpdf::samplecond ( const vec &cond ) { 143 condition ( cond ); 144 vec temp = shep->sample(); 145 return temp; 146 } 147 148 mat mpdf::samplecond_m ( const vec &cond, int N ) { 149 condition ( cond ); 150 mat temp ( shep->dimension(), N ); 151 vec smp ( shep->dimension() ); 152 for ( int i = 0; i < N; i++ ) { 153 smp = shep->sample(); 154 temp.set_col ( i, smp ); 155 } 156 157 return temp; 158 } 159 160 double mpdf::evallogcond ( const vec &dt, const vec &cond ) { 161 double tmp; 162 condition ( cond ); 163 tmp = shep->evallog ( dt ); 164 // it_assert_debug(std::isfinite(tmp), "Infinite value"); 165 return tmp; 166 } 167 168 vec mpdf::evallogcond_m ( const mat &Dt, const vec &cond ) { 169 condition ( cond ); 170 return shep->evallog_m ( Dt ); 171 } 172 173 vec mpdf::evallogcond_m ( const Array<vec> &Dt, const vec &cond ) { 174 condition ( cond ); 175 return shep->evallog_m ( Dt ); 176 } 177 178 void mpdf::from_setting ( const Setting &set ) { 179 RV *r = UI::build<RV> ( set, "rv" ); 180 if ( r ) { 181 set_rv ( *r ); 179 182 delete r; 180 181 182 r = UI::build<RV> (set, "rvc");183 if (r) {184 set_rvc (*r);183 } 184 185 r = UI::build<RV> ( set, "rvc" ); 186 if ( r ) { 187 set_rvc ( *r ); 185 188 delete r; 186 189 } 187 190 } 188 191 189 192 std::ostream &operator<< ( std::ostream &os, const RV &rv ) { 190 193 int id; 191 for ( int i = 0; i < rv.len ; i++ ) {192 id =rv.ids ( i );194 for ( int i = 0; i < rv.len ; i++ ) { 195 id = rv.ids ( i ); 193 196 os << id << "(" << RV_SIZES ( id ) << ")" << // id(size)= 194 197 "=" << RV_NAMES ( id ) << "_{" << rv.times ( i ) << "}; "; //name_{time} … … 202 205 int i; 203 206 int pos = 0; 204 for ( i = 0; i < len;i++ ) {207 for ( i = 0; i < len; i++ ) { 205 208 idlist.set_subvector ( pos, pos + size ( i ) - 1, ids ( i ) ); 206 209 tmlist.set_subvector ( pos, pos + size ( i ) - 1, times ( i ) ); … … 212 215 ivec RV::dataind ( const RV &rv2 ) const { 213 216 ivec res ( 0 ); 214 if ( rv2._dsize() > 0 ) {217 if ( rv2._dsize() > 0 ) { 215 218 str str2 = rv2.tostr(); 216 219 ivec part; 217 220 int i; 218 for ( i = 0; i < len;i++ ) {221 for ( i = 0; i < len; i++ ) { 219 222 part = itpp::find ( ( str2.ids == ids ( i ) ) & ( str2.times == times ( i ) ) ); 220 223 res = concat ( res, part ); 221 224 } 222 225 } 223 it_assert_debug ( res.length() == dsize,"this rv is not fully present in crv!" );226 it_assert_debug ( res.length() == dsize, "this rv is not fully present in crv!" ); 224 227 return res; 225 228 … … 232 235 233 236 // just in case any rv is empty 234 if ( ( len==0 ) || ( rv2.length() ==0 ) ) {return;} 237 if ( ( len == 0 ) || ( rv2.length() == 0 ) ) { 238 return; 239 } 235 240 236 241 //find comon rv 237 ivec cids =itpp::find ( this->findself ( rv2 ) >=0 );242 ivec cids = itpp::find ( this->findself ( rv2 ) >= 0 ); 238 243 239 244 // index of 240 if ( cids.length() > 0 ) {245 if ( cids.length() > 0 ) { 241 246 str str1 = tostr(); 242 247 str str2 = rv2.tostr(); … … 244 249 ivec part1; 245 250 ivec part2; 246 int i, j;251 int i, j; 247 252 // find common rv in strs 248 for ( j =0; j < cids.length();j++ ) {253 for ( j = 0; j < cids.length(); j++ ) { 249 254 i = cids ( j ); 250 255 part1 = itpp::find ( ( str1.ids == ids ( i ) ) & ( str1.times == times ( i ) ) ); … … 254 259 } 255 260 } 256 it_assert_debug ( selfi.length() == rv2i.length(), "this should not happen!" );261 it_assert_debug ( selfi.length() == rv2i.length(), "this should not happen!" ); 257 262 } 258 263 … … 260 265 ivec res = this->findself ( rv2 ); // nonzeros 261 266 ivec valid; 262 if ( dsize>0 ) {valid= itpp::find ( res == -1 );} //-1 => value not found => it remains 267 if ( dsize > 0 ) { 268 valid = itpp::find ( res == -1 ); //-1 => value not found => it remains 269 } 263 270 return ( *this ) ( valid ); //keep those that were not found in rv2 264 271 } … … 267 274 int i, j; 268 275 ivec tmp = -ones_i ( len ); 269 for ( i = 0; i < len;i++ ) {270 for ( j = 0; j < rv2.length();j++ ) {276 for ( i = 0; i < len; i++ ) { 277 for ( j = 0; j < rv2.length(); j++ ) { 271 278 if ( ( ids ( i ) == rv2.ids ( j ) ) & ( times ( i ) == rv2.times ( j ) ) ) { 272 279 tmp ( i ) = j; … … 278 285 } 279 286 280 void RV::from_setting( const Setting &set ) 281 { 287 void RV::from_setting ( const Setting &set ) { 282 288 Array<string> A; 283 if ( !UI::get( A, set, "names" ) )284 A.set_length (0);285 289 if ( !UI::get ( A, set, "names" ) ) 290 A.set_length ( 0 ); 291 286 292 ivec szs; 287 if ( !UI::get(szs,set,"sizes") )288 szs = ones_i (A.length());289 293 if ( !UI::get ( szs, set, "sizes" ) ) 294 szs = ones_i ( A.length() ); 295 290 296 ivec tms; 291 if ( !UI::get(tms,set,"times") )292 tms = zeros_i (A.length());293 297 if ( !UI::get ( tms, set, "times" ) ) 298 tms = zeros_i ( A.length() ); 299 294 300 // TODO tady se bude plnit primo do jeho promennych, a pak se zavola validacnni metoda, takze cele prepsat, ano? 295 init ( A, szs, tms );301 init ( A, szs, tms ); 296 302 } 297 303 … … 302 308 } 303 309 304 void mepdf::condition (const vec &cond) {305 } 306 307 void mepdf::from_setting (const Setting &set) {308 shared_ptr<epdf> e (UI::build<epdf>(set, "epdf", UI::compulsory));309 set_ep(e);310 void mepdf::condition ( const vec &cond ) { 311 } 312 313 void mepdf::from_setting ( const Setting &set ) { 314 shared_ptr<epdf> e ( UI::build<epdf> ( set, "epdf", UI::compulsory ) ); 315 set_ep ( e ); 310 316 } 311 317 … … 313 319 RV rv; //empty rv 314 320 bool rvaddok; 315 for ( int i = 0; i < mpdfs.length();i++ ) {316 rvaddok =rv.add ( mpdfs ( i )->_rv() ); //add rv to common rvs.321 for ( int i = 0; i < mpdfs.length(); i++ ) { 322 rvaddok = rv.add ( mpdfs ( i )->_rv() ); //add rv to common rvs. 317 323 // If rvaddok==false, mpdfs overlap => assert error. 318 it_assert_debug ( rvaddok || ( !checkoverlap ),"mprod::mprod() input mpdfs overlap in rv!" );324 it_assert_debug ( rvaddok || ( !checkoverlap ), "mprod::mprod() input mpdfs overlap in rv!" ); 319 325 }; 320 326 return rv; … … 322 328 323 329 void compositepdf::setrvc ( const RV &rv, RV &rvc ) { 324 for ( int i = 0; i < mpdfs.length();i++ ) {330 for ( int i = 0; i < mpdfs.length(); i++ ) { 325 331 RV rvx = mpdfs ( i )->_rvc().subt ( rv ); 326 332 rvc.add ( rvx ); //add rv to common rvc … … 329 335 330 336 void BM::bayesB ( const mat &Data ) { 331 for ( int t=0;t<Data.cols();t++ ) {bayes ( Data.get_col ( t ) );} 332 } 333 } 337 for ( int t = 0; t < Data.cols(); t++ ) { 338 bayes ( Data.get_col ( t ) ); 339 } 340 } 341 }