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
RevLine 
[262]1
[384]2#include "bdmbase.h"
[2]3
[262]4//! Space of basic BDM structures
[254]5namespace bdm {
[271]6
[270]7const int RV_BUFFER_STEP=1;
8RVmap RV_MAP;
[271]9Array<string> RV_NAMES ( RV_BUFFER_STEP );
10ivec RV_SIZES ( RV_BUFFER_STEP );
[2]11
[211]12RV RV0=RV();
13
[436]14void RV::clear_all()
15{
16    RV_MAP.clear();
17    RV_SIZES.clear();
18    RV_NAMES = Array<string>(RV_BUFFER_STEP);
19}
20 
[270]21int RV::init ( const string &name, int size ) {
[162]22        //Refer
[270]23        int id;
[430]24        RVmap::const_iterator iter = RV_MAP.find ( name );
[270]25        if ( iter == RV_MAP.end() ) {
[271]26                id=RV_MAP.size() +1;
[280]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               
[270]36                RV_MAP.insert ( make_pair ( name,id ) ); //add new rv
[271]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 );
[270]40                }
[271]41                RV_NAMES ( id ) =name;
42                RV_SIZES ( id ) =size;
[5]43        }
[270]44        else {
45                id = iter->second;
[271]46                it_assert(RV_SIZES(id)==size,"RV "+name+" of different size already exists");
[270]47        }
48        return id;
[2]49};
50
[271]51int RV::countsize() const{ int tmp=0; for ( int i=0;i<len;i++ ) {tmp+=RV_SIZES ( ids ( i ) );} return tmp;}
[2]52
[271]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
[422]63void RV::init(const Array<std::string> &in_names, const ivec &in_sizes, const ivec &in_times)
64{
[270]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\" " );
[271]68
[270]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();
[162]78}
[8]79
[270]80RV::RV ( string name, int sz, int tm ) {
81        Array<string> A ( 1 ); A ( 0 ) =name;
[271]82        init ( A,vec_1 ( sz ),vec_1 ( tm ) );
[162]83}
84
[145]85bool RV::add ( const RV &rv2 ) {
[32]86        // TODO
[162]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();
[270]100                dsize = countsize();
101                return ( index.length() ==rv2.len ); //conflict or not
[145]102        }
[162]103        else { //rv2 is empty
[270]104                return true; // no conflict
[145]105        }
[32]106};
107
[270]108RV RV::subselect ( const ivec &ind ) const {
[162]109        RV ret;
[271]110        ret.ids = ids ( ind );
111        ret.times= times ( ind );
[270]112        ret.len = ind.length();
113        ret.dsize=ret.countsize();
[162]114        return ret;
[5]115}
116
[422]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
[145]127void RV::t ( int delta ) { times += delta;}
[8]128
[145]129bool RV::equal ( const RV &rv2 ) const {
[270]130        return ( ids == rv2.ids ) && ( times == rv2.times );
[102]131}
[5]132
[201]133mat epdf::sample_m ( int N ) const {
[270]134        mat X = zeros ( dim, N );
[145]135        for ( int i = 0;i < N;i++ ) X.set_col ( i, this->sample() );
[102]136        return X;
[461]137}
[102]138
[461]139vec mpdf::samplecond(const vec &cond) {
140    condition(cond);
141    vec temp = shep->sample();
142    return temp;
143}
[102]144
[461]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
[102]189std::ostream &operator<< ( std::ostream &os, const RV &rv ) {
[270]190        int id;
[5]191        for ( int i = 0; i < rv.len ;i++ ) {
[270]192                id=rv.ids ( i );
193                os << id << "(" << RV_SIZES ( id ) << ")" <<  // id(size)=
194                "=" << RV_NAMES ( id )  << "_{"  << rv.times ( i ) << "}; "; //name_{time}
[5]195        }
196        return os;
197}
198
[145]199str RV::tostr() const {
[270]200        ivec idlist ( dsize );
201        ivec tmlist ( dsize );
[19]202        int i;
203        int pos = 0;
[145]204        for ( i = 0;i < len;i++ ) {
[280]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 );
[19]208        }
[145]209        return str ( idlist, tmlist );
[19]210}
[32]211
[270]212ivec RV::dataind ( const RV &rv2 ) const {
[145]213        ivec res ( 0 );
[270]214        if ( rv2._dsize() >0 ) {
[165]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                }
[145]222        }
[270]223        it_assert_debug ( res.length() ==dsize,"this rv is not fully present in crv!" );
[145]224        return res;
[165]225
[145]226}
227
[270]228void RV::dataind ( const RV &rv2, ivec &selfi, ivec &rv2i ) const {
[182]229        //clean results
[270]230        selfi.set_size ( 0 );
231        rv2i.set_size ( 0 );
232
[182]233        // just in case any rv is empty
[270]234        if ( ( len==0 ) || ( rv2.length() ==0 ) ) {return;}
235
[182]236        //find comon rv
[270]237        ivec cids=itpp::find ( this->findself ( rv2 ) >=0 );
238
239        // index of
240        if ( cids.length() >0 ) {
[182]241                str str1 = tostr();
[270]242                str str2 = rv2.tostr();
243
[182]244                ivec part1;
245                ivec part2;
246                int i,j;
247                // find common rv in strs
248                for ( j=0; j < cids.length();j++ ) {
[270]249                        i = cids ( j );
[182]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        }
[270]256        it_assert_debug ( selfi.length() == rv2i.length(),"this should not happen!" );
[182]257}
258
[176]259RV RV::subt ( const RV &rv2 ) const {
[145]260        ivec res = this->findself ( rv2 ); // nonzeros
[178]261        ivec valid;
[270]262        if ( dsize>0 ) {valid= itpp::find ( res == -1 );} //-1 => value not found => it remains
[145]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
[377]280void RV::from_setting( const Setting &set ) 
[357]281{       
282        Array<string> A;
[377]283        if( set.exists("names"))
284                UI::get( A, set, "names" );
[357]285        else
286                A.set_length(0);
287       
288        ivec szs;
[377]289        if( set.exists("sizes"))
290                UI::get(szs,set,"sizes");
[357]291        else
292                szs = ones_i(A.length());
293       
294        ivec tms;
[377]295        if( set.exists( "times") )
296                UI::get(tms,set,"times");
[357]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
[377]304/*void RV::to_setting( Setting &set ) const
[357]305{       
[377]306        Transport::to_setting( set );
[357]307
[377]308        Setting &kilometers_setting = set.add("kilometers", Setting::TypeInt );
[357]309        kilometers_setting = kilometers;
310
[377]311        UI::save( passengers, set, "passengers" );
[357]312}*/
313
[102]314RV concat ( const RV &rv1, const RV &rv2 ) {
[32]315        RV pom = rv1;
[102]316        pom.add ( rv2 );
[32]317        return pom;
318}
[170]319
[270]320RV compositepdf::getrv ( bool checkoverlap ) {
[175]321        RV rv; //empty rv
322        bool rvaddok;
[384]323        for ( int i = 0;i < mpdfs.length();i++ ) {
[175]324                rvaddok=rv.add ( mpdfs ( i )->_rv() ); //add rv to common rvs.
[270]325                // If rvaddok==false, mpdfs overlap => assert error.
326                it_assert_debug ( rvaddok|| ( !checkoverlap ),"mprod::mprod() input mpdfs overlap in rv!" );
[175]327        };
328        return rv;
329}
330
[270]331void compositepdf::setrvc ( const RV &rv, RV &rvc ) {
[384]332        for ( int i = 0;i < mpdfs.length();i++ ) {
[178]333                RV rvx = mpdfs ( i )->_rvc().subt ( rv );
334                rvc.add ( rvx ); //add rv to common rvc
[175]335        };
336}
337
[270]338void BM::bayesB ( const mat &Data ) {
339        for ( int t=0;t<Data.cols();t++ ) {bayes ( Data.get_col ( t ) );}
[182]340}
[262]341}
Note: See TracBrowser for help on using the browser.