root/library/bdm/base/bdmbase.cpp @ 604

Revision 604, 9.4 kB (checked in by smidl, 15 years ago)

change of syntax of RV

  • Property svn:eol-style set to native
Line 
1
2#include "bdmbase.h"
3
4//! Space of basic BDM structures
5namespace bdm {
6
7const int RV::BUFFER_STEP = 1;
8
9Array<string> RV::NAMES ( RV::BUFFER_STEP );
10
11ivec RV::SIZES ( RV::BUFFER_STEP );
12
13RV::str2int_map RV::MAP;
14
15void RV::clear_all() {
16        MAP.clear();
17        SIZES.clear();
18        NAMES = Array<string> ( BUFFER_STEP );
19}
20
21string RV::show_all(){
22  ostringstream os;
23  for(str2int_map::const_iterator iter=MAP.begin(); iter!=MAP.end(); iter++){
24          os << "key: " << iter->first << " val: " << iter->second <<endl;
25  }
26  return os.str();
27};
28
29int RV::init ( const string &name, int size ) {
30        //Refer
31        int id;
32        str2int_map::const_iterator iter = MAP.find ( name );
33        if ( iter == MAP.end() ) {
34                id = MAP.size() + 1;
35                //debug
36                /*              {
37                                        cout << endl;
38                                        str2int_map::const_iterator iter = MAP.begin();
39                                        for(str2int_map::const_iterator iter=MAP.begin(); iter!=MAP.end(); iter++){
40                                                cout << "key: " << iter->first << " val: " << iter->second <<endl;
41                                        }
42                                }*/
43
44                MAP.insert ( make_pair ( name, id ) ); //add new rv
45                if ( id >= NAMES.length() ) {
46                        NAMES.set_length ( id + BUFFER_STEP, true );
47                        SIZES.set_length ( id + BUFFER_STEP, true );
48                }
49                NAMES ( id ) = name;
50                SIZES ( id ) = size;
51        } else {
52                id = iter->second;
53                bdm_assert_debug ( SIZES ( id ) == size, "RV " + name + " of different size already exists" );
54        }
55        return id;
56};
57
58int RV::countsize() const {
59        int tmp = 0;
60        for ( int i = 0; i < len; i++ ) {
61                tmp += SIZES ( ids ( i ) );
62        }
63        return tmp;
64}
65
66ivec RV::cumsizes() const {
67        ivec szs ( len );
68        int tmp = 0;
69        for ( int i = 0; i < len; i++ ) {
70                tmp += SIZES ( ids ( i ) );
71                szs ( i ) = tmp;
72        }
73        return szs;
74}
75
76void RV::init ( const Array<std::string> &in_names, const ivec &in_sizes, const ivec &in_times ) {
77        len = in_names.length();
78        bdm_assert_debug ( in_names.length() == in_times.length(), "check \"times\" " );
79        bdm_assert_debug ( in_names.length() == in_sizes.length(), "check \"sizes\" " );
80
81        times.set_length ( len );
82        ids.set_length ( len );
83        int id;
84        for ( int i = 0; i < len; i++ ) {
85                id = init ( in_names ( i ), in_sizes ( i ) );
86                ids ( i ) = id;
87        }
88        times = in_times;
89        dsize = countsize();
90}
91
92RV::RV ( string name, int sz, int tm ) {
93        Array<string> A ( 1 );
94        A ( 0 ) = name;
95        init ( A, vec_1 ( sz ), vec_1 ( tm ) );
96}
97
98bool RV::add ( const RV &rv2 ) {
99        // TODO
100        if ( rv2.len > 0 ) { //rv2 is nonempty
101                ivec ind = rv2.findself ( *this ); //should be -1 all the time
102                ivec index = itpp::find ( ind == -1 );
103
104                if ( index.length() < rv2.len ) { //conflict
105                        ids = concat ( ids, rv2.ids ( index ) );
106                        times = concat ( times, rv2.times ( index ) );
107                } else {
108                        ids = concat ( ids, rv2.ids );
109                        times = concat ( times, rv2.times );
110                }
111                len = ids.length();
112                dsize = countsize();
113                return ( index.length() == rv2.len ); //conflict or not
114        } else { //rv2 is empty
115                return true; // no conflict
116        }
117};
118
119RV RV::subselect ( const ivec &ind ) const {
120        RV ret;
121        ret.ids = ids ( ind );
122        ret.times = times ( ind );
123        ret.len = ind.length();
124        ret.dsize = ret.countsize();
125        return ret;
126}
127
128RV RV::operator() ( int di1, int di2 ) const {
129        ivec sz = cumsizes();
130        int i1 = 0;
131        while ( sz ( i1 ) < di1 ) i1++;
132        int i2 = i1;
133        while ( sz ( i2 ) < di2 ) i2++;
134        return subselect ( linspace ( i1, i2 ) );
135}
136
137void RV::t_plus ( int delta ) {
138        times += delta;
139}
140
141bool RV::equal ( const RV &rv2 ) const {
142        return ( ids == rv2.ids ) && ( times == rv2.times );
143}
144
145shared_ptr<mpdf> epdf::condition ( const RV &rv ) const {
146        bdm_warning ( "Not implemented" );
147        return shared_ptr<mpdf>();
148}
149
150shared_ptr<epdf> epdf::marginal ( const RV &rv ) const {
151        bdm_warning ( "Not implemented" );
152        return shared_ptr<epdf>();
153}
154
155mat epdf::sample_m ( int N ) const {
156        mat X = zeros ( dim, N );
157        for ( int i = 0; i < N; i++ ) X.set_col ( i, this->sample() );
158        return X;
159}
160
161vec epdf::evallog_m ( const mat &Val ) const {
162        vec x ( Val.cols() );
163        for ( int i = 0; i < Val.cols(); i++ ) {
164                x ( i ) = evallog ( Val.get_col ( i ) );
165        }
166
167        return x;
168}
169
170vec epdf::evallog_m ( const Array<vec> &Avec ) const {
171        vec x ( Avec.size() );
172        for ( int i = 0; i < Avec.size(); i++ ) {
173                x ( i ) = evallog ( Avec ( i ) );
174        }
175
176        return x;
177}
178
179mat mpdf::samplecond_m ( const vec &cond, int N ) {
180        mat M ( dimension(), N );
181        for ( int i = 0; i < N; i++ ) {
182                M.set_col ( i, samplecond ( cond ) );
183        }
184
185        return M;
186}
187
188void mpdf::from_setting ( const Setting &set ) {
189        shared_ptr<RV> r = UI::build<RV> ( set, "rv", UI::optional );
190        if ( r ) {
191                set_rv ( *r );
192        }
193
194        r = UI::build<RV> ( set, "rvc", UI::optional );
195        if ( r ) {
196                set_rvc ( *r );
197        }
198}
199
200void datalink::set_connection ( const RV &rv, const RV &rv_up ) {
201        downsize = rv._dsize();
202        upsize = rv_up._dsize();
203        v2v_up = rv.dataind ( rv_up );
204        bdm_assert_debug ( v2v_up.length() == downsize, "rv is not fully in rv_up" );
205}
206
207void datalink::set_connection ( int ds, int us, const ivec &upind ) {
208        downsize = ds;
209        upsize = us;
210        v2v_up = upind;
211        bdm_assert_debug ( v2v_up.length() == downsize, "rv is not fully in rv_up" );
212}
213
214void datalink_part::set_connection ( const RV &rv, const RV &rv_up ) {
215        rv.dataind ( rv_up, v2v_down, v2v_up );
216        downsize = v2v_down.length();
217        upsize = v2v_up.length();
218}
219
220void datalink_m2e::set_connection ( const RV &rv, const RV &rvc, const RV &rv_up ) {
221        datalink::set_connection ( rv, rv_up );
222        condsize = rvc._dsize();
223        //establish v2c connection
224        rvc.dataind ( rv_up, v2c_lo, v2c_up );
225}
226
227vec datalink_m2e::get_cond ( const vec &val_up ) {
228        vec tmp ( condsize );
229        set_subvector ( tmp, v2c_lo, val_up ( v2c_up ) );
230        return tmp;
231}
232
233void datalink_m2e::pushup_cond ( vec &val_up, const vec &val, const vec &cond ) {
234        bdm_assert_debug ( downsize == val.length(), "Wrong val" );
235        bdm_assert_debug ( upsize == val_up.length(), "Wrong val_up" );
236        set_subvector ( val_up, v2v_up, val );
237        set_subvector ( val_up, v2c_up, cond );
238}
239
240std::ostream &operator<< ( std::ostream &os, const RV &rv ) {
241        int id;
242        for ( int i = 0; i < rv.len ; i++ ) {
243                id = rv.ids ( i );
244                os << id << "(" << RV::SIZES ( id ) << ")" <<  // id(size)=
245                "=" << RV::NAMES ( id )  << "_{"  << rv.times ( i ) << "}; "; //name_{time}
246        }
247        return os;
248}
249
250str RV::tostr() const {
251        ivec idlist ( dsize );
252        ivec tmlist ( dsize );
253        int i;
254        int pos = 0;
255        for ( i = 0; i < len; i++ ) {
256                idlist.set_subvector ( pos, pos + size ( i ) - 1, ids ( i ) );
257                tmlist.set_subvector ( pos, pos + size ( i ) - 1, times ( i ) );
258                pos += size ( i );
259        }
260        return str ( idlist, tmlist );
261}
262
263ivec RV::dataind ( const RV &rv2 ) const {
264        ivec res ( 0 );
265        if ( rv2._dsize() > 0 ) {
266                str str2 = rv2.tostr();
267                ivec part;
268                int i;
269                for ( i = 0; i < len; i++ ) {
270                        part = itpp::find ( ( str2.ids == ids ( i ) ) & ( str2.times == times ( i ) ) );
271                        res = concat ( res, part );
272                }
273        }
274
275        bdm_assert_debug ( res.length() == dsize, "this rv is not fully present in crv!" );
276        return res;
277
278}
279
280void RV::dataind ( const RV &rv2, ivec &selfi, ivec &rv2i ) const {
281        //clean results
282        selfi.set_size ( 0 );
283        rv2i.set_size ( 0 );
284
285        // just in case any rv is empty
286        if ( ( len == 0 ) || ( rv2.length() == 0 ) ) {
287                return;
288        }
289
290        //find comon rv
291        ivec cids = itpp::find ( this->findself ( rv2 ) >= 0 );
292
293        // index of
294        if ( cids.length() > 0 ) {
295                str str1 = tostr();
296                str str2 = rv2.tostr();
297
298                ivec part1;
299                ivec part2;
300                int i, j;
301                // find common rv in strs
302                for ( j = 0; j < cids.length(); j++ ) {
303                        i = cids ( j );
304                        part1 = itpp::find ( ( str1.ids == ids ( i ) ) & ( str1.times == times ( i ) ) );
305                        part2 = itpp::find ( ( str2.ids == ids ( i ) ) & ( str2.times == times ( i ) ) );
306                        selfi = concat ( selfi, part1 );
307                        rv2i = concat ( rv2i, part2 );
308                }
309        }
310        bdm_assert_debug ( selfi.length() == rv2i.length(), "this should not happen!" );
311}
312
313RV RV::subt ( const RV &rv2 ) const {
314        ivec res = this->findself ( rv2 ); // nonzeros
315        ivec valid;
316        if ( dsize > 0 ) {
317                valid = itpp::find ( res == -1 );    //-1 => value not found => it remains
318        }
319        return ( *this ) ( valid ); //keep those that were not found in rv2
320}
321
322ivec RV::findself ( const RV &rv2 ) const {
323        int i, j;
324        ivec tmp = -ones_i ( len );
325        for ( i = 0; i < len; i++ ) {
326                for ( j = 0; j < rv2.length(); j++ ) {
327                        if ( ( ids ( i ) == rv2.ids ( j ) ) & ( times ( i ) == rv2.times ( j ) ) ) {
328                                tmp ( i ) = j;
329                                break;
330                        }
331                }
332        }
333        return tmp;
334}
335
336ivec RV::findself_ids ( const RV &rv2 ) const {
337        int i, j;
338        ivec tmp = -ones_i ( len );
339        for ( i = 0; i < len; i++ ) {
340                for ( j = 0; j < rv2.length(); j++ ) {
341                        if ( ( ids ( i ) == rv2.ids ( j ) ) ) {
342                                tmp ( i ) = j;
343                                break;
344                        }
345                }
346        }
347        return tmp;
348}
349
350void RV::from_setting ( const Setting &set ) {
351        Array<string> A;
352        UI::get ( A, set, "names" );
353
354        ivec szs;
355        if ( !UI::get ( szs, set, "sizes" ) )
356                szs = ones_i ( A.length() );
357
358        ivec tms;
359        if ( !UI::get ( tms, set, "times" ) )
360                tms = zeros_i ( A.length() );
361
362        // TODO tady se bude plnit primo do jeho promennych, a pak se zavola validacnni metoda, takze cele prepsat, ano?
363        init ( A, szs, tms );
364}
365
366RV concat ( const RV &rv1, const RV &rv2 ) {
367        RV pom = rv1;
368        pom.add ( rv2 );
369        return pom;
370}
371
372void mepdf::from_setting ( const Setting &set ) {
373        shared_ptr<epdf> e ( UI::build<epdf> ( set, "epdf", UI::compulsory ) );
374        iepdf = e;
375        set_ep ( iepdf.get() );
376}
377
378RV get_composite_rv ( const Array<shared_ptr<mpdf> > &mpdfs,
379                      bool checkoverlap ) {
380        RV rv; //empty rv
381        bool rvaddok;
382        for ( int i = 0; i < mpdfs.length(); i++ ) {
383                rvaddok = rv.add ( mpdfs ( i )->_rv() ); //add rv to common rvs.
384                // If rvaddok==false, mpdfs overlap => assert error.
385                bdm_assert_debug ( rvaddok || !checkoverlap, "mprod::mprod() input mpdfs overlap in rv!" );
386        }
387
388        return rv;
389}
390
391void BM::bayesB ( const mat &Data ) {
392        for ( int t = 0; t < Data.cols(); t++ ) {
393                bayes ( Data.get_col ( t ) );
394        }
395}
396}
Note: See TracBrowser for help on using the browser.