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

Revision 461, 8.0 kB (checked in by vbarta, 15 years ago)

mpdf (& its dependencies) reformat: now using shared_ptr, moved virtual method bodies to .cpp

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