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

Revision 487, 8.1 kB (checked in by smidl, 15 years ago)

1st step of mpdf redesign - BROKEN compile

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