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

Revision 600, 9.2 kB (checked in by mido, 15 years ago)

presun globalnich veci v RV dovnitr tridy kde byly zadefinovany jako staticke promenne
(plus drobne zacisteni dokumentace a adresarove struktury)

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