[262] | 1 | |
---|
[384] | 2 | #include "bdmbase.h" |
---|
[2] | 3 | |
---|
[262] | 4 | //! Space of basic BDM structures |
---|
[254] | 5 | namespace bdm { |
---|
[271] | 6 | |
---|
[477] | 7 | const int RV_BUFFER_STEP = 1; |
---|
[270] | 8 | RVmap RV_MAP; |
---|
[271] | 9 | Array<string> RV_NAMES ( RV_BUFFER_STEP ); |
---|
| 10 | ivec RV_SIZES ( RV_BUFFER_STEP ); |
---|
[2] | 11 | |
---|
[477] | 12 | RV RV0 = RV(); |
---|
[211] | 13 | |
---|
[477] | 14 | void RV::clear_all() { |
---|
| 15 | RV_MAP.clear(); |
---|
| 16 | RV_SIZES.clear(); |
---|
| 17 | RV_NAMES = Array<string> ( RV_BUFFER_STEP ); |
---|
[436] | 18 | } |
---|
[477] | 19 | |
---|
[270] | 20 | int RV::init ( const string &name, int size ) { |
---|
[162] | 21 | //Refer |
---|
[270] | 22 | int id; |
---|
[430] | 23 | RVmap::const_iterator iter = RV_MAP.find ( name ); |
---|
[270] | 24 | if ( iter == RV_MAP.end() ) { |
---|
[477] | 25 | id = RV_MAP.size() + 1; |
---|
[280] | 26 | //debug |
---|
[477] | 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 ); |
---|
[270] | 39 | } |
---|
[477] | 40 | RV_NAMES ( id ) = name; |
---|
| 41 | RV_SIZES ( id ) = size; |
---|
| 42 | } else { |
---|
[270] | 43 | id = iter->second; |
---|
[477] | 44 | it_assert ( RV_SIZES ( id ) == size, "RV " + name + " of different size already exists" ); |
---|
[270] | 45 | } |
---|
| 46 | return id; |
---|
[2] | 47 | }; |
---|
| 48 | |
---|
[477] | 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 | } |
---|
[2] | 56 | |
---|
[271] | 57 | ivec RV::cumsizes() const { |
---|
| 58 | ivec szs ( len ); |
---|
[477] | 59 | int tmp = 0; |
---|
| 60 | for ( int i = 0; i < len; i++ ) { |
---|
| 61 | tmp += RV_SIZES ( ids ( i ) ); |
---|
[271] | 62 | szs ( i ) = tmp; |
---|
| 63 | } |
---|
| 64 | return szs; |
---|
| 65 | } |
---|
| 66 | |
---|
[477] | 67 | void RV::init ( const Array<std::string> &in_names, const ivec &in_sizes, const ivec &in_times ) { |
---|
[270] | 68 | len = in_names.length(); |
---|
[477] | 69 | it_assert_debug ( in_names.length() == in_times.length(), "check \"times\" " ); |
---|
| 70 | it_assert_debug ( in_names.length() == in_sizes.length(), "check \"sizes\" " ); |
---|
[271] | 71 | |
---|
[270] | 72 | times.set_length ( len ); |
---|
| 73 | ids.set_length ( len ); |
---|
| 74 | int id; |
---|
[477] | 75 | for ( int i = 0; i < len; i++ ) { |
---|
[270] | 76 | id = init ( in_names ( i ), in_sizes ( i ) ); |
---|
| 77 | ids ( i ) = id; |
---|
| 78 | } |
---|
| 79 | times = in_times; |
---|
| 80 | dsize = countsize(); |
---|
[162] | 81 | } |
---|
[8] | 82 | |
---|
[270] | 83 | RV::RV ( string name, int sz, int tm ) { |
---|
[477] | 84 | Array<string> A ( 1 ); |
---|
| 85 | A ( 0 ) = name; |
---|
| 86 | init ( A, vec_1 ( sz ), vec_1 ( tm ) ); |
---|
[162] | 87 | } |
---|
| 88 | |
---|
[145] | 89 | bool RV::add ( const RV &rv2 ) { |
---|
[32] | 90 | // TODO |
---|
[477] | 91 | if ( rv2.len > 0 ) { //rv2 is nonempty |
---|
[162] | 92 | ivec ind = rv2.findself ( *this ); //should be -1 all the time |
---|
[477] | 93 | ivec index = itpp::find ( ind == -1 ); |
---|
[162] | 94 | |
---|
| 95 | if ( index.length() < rv2.len ) { //conflict |
---|
| 96 | ids = concat ( ids, rv2.ids ( index ) ); |
---|
| 97 | times = concat ( times, rv2.times ( index ) ); |
---|
[477] | 98 | } else { |
---|
[162] | 99 | ids = concat ( ids, rv2.ids ); |
---|
| 100 | times = concat ( times, rv2.times ); |
---|
| 101 | } |
---|
| 102 | len = ids.length(); |
---|
[270] | 103 | dsize = countsize(); |
---|
[477] | 104 | return ( index.length() == rv2.len ); //conflict or not |
---|
| 105 | } else { //rv2 is empty |
---|
[270] | 106 | return true; // no conflict |
---|
[145] | 107 | } |
---|
[32] | 108 | }; |
---|
| 109 | |
---|
[270] | 110 | RV RV::subselect ( const ivec &ind ) const { |
---|
[162] | 111 | RV ret; |
---|
[271] | 112 | ret.ids = ids ( ind ); |
---|
[477] | 113 | ret.times = times ( ind ); |
---|
[270] | 114 | ret.len = ind.length(); |
---|
[477] | 115 | ret.dsize = ret.countsize(); |
---|
[162] | 116 | return ret; |
---|
[5] | 117 | } |
---|
| 118 | |
---|
[477] | 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 ) ); |
---|
[422] | 126 | } |
---|
| 127 | |
---|
[477] | 128 | void RV::t ( int delta ) { |
---|
| 129 | times += delta; |
---|
| 130 | } |
---|
[8] | 131 | |
---|
[145] | 132 | bool RV::equal ( const RV &rv2 ) const { |
---|
[270] | 133 | return ( ids == rv2.ids ) && ( times == rv2.times ); |
---|
[102] | 134 | } |
---|
[5] | 135 | |
---|
[201] | 136 | mat epdf::sample_m ( int N ) const { |
---|
[270] | 137 | mat X = zeros ( dim, N ); |
---|
[477] | 138 | for ( int i = 0; i < N; i++ ) X.set_col ( i, this->sample() ); |
---|
[102] | 139 | return X; |
---|
[461] | 140 | } |
---|
[102] | 141 | |
---|
[502] | 142 | vec epdf::evallog_m (const mat &Val) const { |
---|
| 143 | vec x (Val.cols()); |
---|
| 144 | for (int i = 0; i < Val.cols(); i++) { |
---|
| 145 | x (i) = evallog ( Val.get_col (i) ); |
---|
| 146 | } |
---|
[102] | 147 | |
---|
[502] | 148 | return x; |
---|
| 149 | } |
---|
| 150 | |
---|
| 151 | vec epdf::evallog_m (const Array<vec> &Avec) const { |
---|
| 152 | vec x (Avec.size()); |
---|
| 153 | for (int i = 0; i < Avec.size(); i++) { |
---|
| 154 | x (i) = evallog ( Avec (i) ); |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | return x; |
---|
| 158 | } |
---|
| 159 | |
---|
[477] | 160 | void mpdf::from_setting ( const Setting &set ) { |
---|
| 161 | RV *r = UI::build<RV> ( set, "rv" ); |
---|
| 162 | if ( r ) { |
---|
| 163 | set_rv ( *r ); |
---|
[471] | 164 | delete r; |
---|
[477] | 165 | } |
---|
[461] | 166 | |
---|
[477] | 167 | r = UI::build<RV> ( set, "rvc" ); |
---|
| 168 | if ( r ) { |
---|
| 169 | set_rvc ( *r ); |
---|
[471] | 170 | delete r; |
---|
[477] | 171 | } |
---|
[461] | 172 | } |
---|
| 173 | |
---|
[102] | 174 | std::ostream &operator<< ( std::ostream &os, const RV &rv ) { |
---|
[270] | 175 | int id; |
---|
[477] | 176 | for ( int i = 0; i < rv.len ; i++ ) { |
---|
| 177 | id = rv.ids ( i ); |
---|
[270] | 178 | os << id << "(" << RV_SIZES ( id ) << ")" << // id(size)= |
---|
| 179 | "=" << RV_NAMES ( id ) << "_{" << rv.times ( i ) << "}; "; //name_{time} |
---|
[5] | 180 | } |
---|
| 181 | return os; |
---|
| 182 | } |
---|
| 183 | |
---|
[145] | 184 | str RV::tostr() const { |
---|
[270] | 185 | ivec idlist ( dsize ); |
---|
| 186 | ivec tmlist ( dsize ); |
---|
[19] | 187 | int i; |
---|
| 188 | int pos = 0; |
---|
[477] | 189 | for ( i = 0; i < len; i++ ) { |
---|
[280] | 190 | idlist.set_subvector ( pos, pos + size ( i ) - 1, ids ( i ) ); |
---|
| 191 | tmlist.set_subvector ( pos, pos + size ( i ) - 1, times ( i ) ); |
---|
| 192 | pos += size ( i ); |
---|
[19] | 193 | } |
---|
[145] | 194 | return str ( idlist, tmlist ); |
---|
[19] | 195 | } |
---|
[32] | 196 | |
---|
[270] | 197 | ivec RV::dataind ( const RV &rv2 ) const { |
---|
[145] | 198 | ivec res ( 0 ); |
---|
[477] | 199 | if ( rv2._dsize() > 0 ) { |
---|
[165] | 200 | str str2 = rv2.tostr(); |
---|
| 201 | ivec part; |
---|
| 202 | int i; |
---|
[477] | 203 | for ( i = 0; i < len; i++ ) { |
---|
[165] | 204 | part = itpp::find ( ( str2.ids == ids ( i ) ) & ( str2.times == times ( i ) ) ); |
---|
| 205 | res = concat ( res, part ); |
---|
| 206 | } |
---|
[145] | 207 | } |
---|
[477] | 208 | it_assert_debug ( res.length() == dsize, "this rv is not fully present in crv!" ); |
---|
[145] | 209 | return res; |
---|
[165] | 210 | |
---|
[145] | 211 | } |
---|
| 212 | |
---|
[270] | 213 | void RV::dataind ( const RV &rv2, ivec &selfi, ivec &rv2i ) const { |
---|
[182] | 214 | //clean results |
---|
[270] | 215 | selfi.set_size ( 0 ); |
---|
| 216 | rv2i.set_size ( 0 ); |
---|
| 217 | |
---|
[182] | 218 | // just in case any rv is empty |
---|
[477] | 219 | if ( ( len == 0 ) || ( rv2.length() == 0 ) ) { |
---|
| 220 | return; |
---|
| 221 | } |
---|
[270] | 222 | |
---|
[182] | 223 | //find comon rv |
---|
[477] | 224 | ivec cids = itpp::find ( this->findself ( rv2 ) >= 0 ); |
---|
[270] | 225 | |
---|
| 226 | // index of |
---|
[477] | 227 | if ( cids.length() > 0 ) { |
---|
[182] | 228 | str str1 = tostr(); |
---|
[270] | 229 | str str2 = rv2.tostr(); |
---|
| 230 | |
---|
[182] | 231 | ivec part1; |
---|
| 232 | ivec part2; |
---|
[477] | 233 | int i, j; |
---|
[182] | 234 | // find common rv in strs |
---|
[477] | 235 | for ( j = 0; j < cids.length(); j++ ) { |
---|
[270] | 236 | i = cids ( j ); |
---|
[182] | 237 | part1 = itpp::find ( ( str1.ids == ids ( i ) ) & ( str1.times == times ( i ) ) ); |
---|
| 238 | part2 = itpp::find ( ( str2.ids == ids ( i ) ) & ( str2.times == times ( i ) ) ); |
---|
| 239 | selfi = concat ( selfi, part1 ); |
---|
| 240 | rv2i = concat ( rv2i, part2 ); |
---|
| 241 | } |
---|
| 242 | } |
---|
[477] | 243 | it_assert_debug ( selfi.length() == rv2i.length(), "this should not happen!" ); |
---|
[182] | 244 | } |
---|
| 245 | |
---|
[176] | 246 | RV RV::subt ( const RV &rv2 ) const { |
---|
[145] | 247 | ivec res = this->findself ( rv2 ); // nonzeros |
---|
[178] | 248 | ivec valid; |
---|
[477] | 249 | if ( dsize > 0 ) { |
---|
| 250 | valid = itpp::find ( res == -1 ); //-1 => value not found => it remains |
---|
| 251 | } |
---|
[145] | 252 | return ( *this ) ( valid ); //keep those that were not found in rv2 |
---|
| 253 | } |
---|
| 254 | |
---|
| 255 | ivec RV::findself ( const RV &rv2 ) const { |
---|
| 256 | int i, j; |
---|
| 257 | ivec tmp = -ones_i ( len ); |
---|
[477] | 258 | for ( i = 0; i < len; i++ ) { |
---|
| 259 | for ( j = 0; j < rv2.length(); j++ ) { |
---|
[145] | 260 | if ( ( ids ( i ) == rv2.ids ( j ) ) & ( times ( i ) == rv2.times ( j ) ) ) { |
---|
| 261 | tmp ( i ) = j; |
---|
| 262 | break; |
---|
| 263 | } |
---|
| 264 | } |
---|
| 265 | } |
---|
| 266 | return tmp; |
---|
| 267 | } |
---|
| 268 | |
---|
[477] | 269 | void RV::from_setting ( const Setting &set ) { |
---|
[357] | 270 | Array<string> A; |
---|
[479] | 271 | it_assert ( !A.length(), "default array not empty" ); |
---|
| 272 | UI::get ( A, set, "names" ); |
---|
[477] | 273 | |
---|
[357] | 274 | ivec szs; |
---|
[477] | 275 | if ( !UI::get ( szs, set, "sizes" ) ) |
---|
| 276 | szs = ones_i ( A.length() ); |
---|
| 277 | |
---|
[357] | 278 | ivec tms; |
---|
[477] | 279 | if ( !UI::get ( tms, set, "times" ) ) |
---|
| 280 | tms = zeros_i ( A.length() ); |
---|
| 281 | |
---|
[357] | 282 | // TODO tady se bude plnit primo do jeho promennych, a pak se zavola validacnni metoda, takze cele prepsat, ano? |
---|
[477] | 283 | init ( A, szs, tms ); |
---|
[357] | 284 | } |
---|
| 285 | |
---|
[102] | 286 | RV concat ( const RV &rv1, const RV &rv2 ) { |
---|
[32] | 287 | RV pom = rv1; |
---|
[102] | 288 | pom.add ( rv2 ); |
---|
[32] | 289 | return pom; |
---|
| 290 | } |
---|
[170] | 291 | |
---|
[477] | 292 | void mepdf::from_setting ( const Setting &set ) { |
---|
| 293 | shared_ptr<epdf> e ( UI::build<epdf> ( set, "epdf", UI::compulsory ) ); |
---|
[489] | 294 | iepdf = e; |
---|
| 295 | set_ep(iepdf.get()); |
---|
[462] | 296 | } |
---|
| 297 | |
---|
[270] | 298 | RV compositepdf::getrv ( bool checkoverlap ) { |
---|
[175] | 299 | RV rv; //empty rv |
---|
| 300 | bool rvaddok; |
---|
[477] | 301 | for ( int i = 0; i < mpdfs.length(); i++ ) { |
---|
| 302 | rvaddok = rv.add ( mpdfs ( i )->_rv() ); //add rv to common rvs. |
---|
[270] | 303 | // If rvaddok==false, mpdfs overlap => assert error. |
---|
[477] | 304 | it_assert_debug ( rvaddok || ( !checkoverlap ), "mprod::mprod() input mpdfs overlap in rv!" ); |
---|
[175] | 305 | }; |
---|
| 306 | return rv; |
---|
| 307 | } |
---|
| 308 | |
---|
[270] | 309 | void compositepdf::setrvc ( const RV &rv, RV &rvc ) { |
---|
[477] | 310 | for ( int i = 0; i < mpdfs.length(); i++ ) { |
---|
[178] | 311 | RV rvx = mpdfs ( i )->_rvc().subt ( rv ); |
---|
| 312 | rvc.add ( rvx ); //add rv to common rvc |
---|
[175] | 313 | }; |
---|
| 314 | } |
---|
| 315 | |
---|
[270] | 316 | void BM::bayesB ( const mat &Data ) { |
---|
[477] | 317 | for ( int t = 0; t < Data.cols(); t++ ) { |
---|
| 318 | bayes ( Data.get_col ( t ) ); |
---|
| 319 | } |
---|
[182] | 320 | } |
---|
[262] | 321 | } |
---|