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

Revision 422, 6.8 kB (checked in by vbarta, 15 years ago)

RV partial cleanup: more arguments passed by reference, less inline functions, tests integrated into the testsuite

  • 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
[270]14int RV::init ( const string &name, int size ) {
[162]15        //Refer
[270]16        int id;
[280]17        RVmap::const_iterator iter = RV_MAP.begin();
18        iter=RV_MAP.find ( name );
[270]19        if ( iter == RV_MAP.end() ) {
[271]20                id=RV_MAP.size() +1;
[280]21                //debug
22/*              {
23                        cout << endl;
24                        RVmap::const_iterator iter = RV_MAP.begin();
25                        for(RVmap::const_iterator iter=RV_MAP.begin(); iter!=RV_MAP.end(); iter++){
26                                cout << "key: " << iter->first << " val: " << iter->second <<endl;
27                        }
28                }*/
29               
[270]30                RV_MAP.insert ( make_pair ( name,id ) ); //add new rv
[271]31                if ( id>=RV_NAMES.length() ) {
32                        RV_NAMES.set_length ( id+RV_BUFFER_STEP,true );
33                        RV_SIZES.set_length ( id+RV_BUFFER_STEP,true );
[270]34                }
[271]35                RV_NAMES ( id ) =name;
36                RV_SIZES ( id ) =size;
[5]37        }
[270]38        else {
39                id = iter->second;
[271]40                it_assert(RV_SIZES(id)==size,"RV "+name+" of different size already exists");
[270]41        }
42        return id;
[2]43};
44
[271]45int RV::countsize() const{ int tmp=0; for ( int i=0;i<len;i++ ) {tmp+=RV_SIZES ( ids ( i ) );} return tmp;}
[2]46
[271]47ivec RV::cumsizes() const {
48        ivec szs ( len );
49        int tmp=0;
50        for ( int i=0;i<len;i++ ) {
51                tmp+=RV_SIZES ( ids ( i ) );
52                szs ( i ) = tmp;
53        }
54        return szs;
55}
56
[422]57void RV::init(const Array<std::string> &in_names, const ivec &in_sizes, const ivec &in_times)
58{
[270]59        len = in_names.length();
60        it_assert_debug ( in_names.length() ==in_times.length(), "check \"times\" " );
61        it_assert_debug ( in_names.length() ==in_sizes.length(), "check \"sizes\" " );
[271]62
[270]63        times.set_length ( len );
64        ids.set_length ( len );
65        int id;
66        for ( int i=0; i<len; i++ ) {
67                id = init ( in_names ( i ), in_sizes ( i ) );
68                ids ( i ) = id;
69        }
70        times = in_times;
71        dsize = countsize();
[162]72}
[8]73
[270]74RV::RV ( string name, int sz, int tm ) {
75        Array<string> A ( 1 ); A ( 0 ) =name;
[271]76        init ( A,vec_1 ( sz ),vec_1 ( tm ) );
[162]77}
78
[145]79bool RV::add ( const RV &rv2 ) {
[32]80        // TODO
[162]81        if ( rv2.len>0 ) { //rv2 is nonempty
82                ivec ind = rv2.findself ( *this ); //should be -1 all the time
83                ivec index = itpp::find ( ind==-1 );
84
85                if ( index.length() < rv2.len ) { //conflict
86                        ids = concat ( ids, rv2.ids ( index ) );
87                        times = concat ( times, rv2.times ( index ) );
88                }
89                else {
90                        ids = concat ( ids, rv2.ids );
91                        times = concat ( times, rv2.times );
92                }
93                len = ids.length();
[270]94                dsize = countsize();
95                return ( index.length() ==rv2.len ); //conflict or not
[145]96        }
[162]97        else { //rv2 is empty
[270]98                return true; // no conflict
[145]99        }
[32]100};
101
[270]102RV RV::subselect ( const ivec &ind ) const {
[162]103        RV ret;
[271]104        ret.ids = ids ( ind );
105        ret.times= times ( ind );
[270]106        ret.len = ind.length();
107        ret.dsize=ret.countsize();
[162]108        return ret;
[5]109}
110
[422]111RV RV::operator()(int di1, int di2) const
112{
113    ivec sz = cumsizes();
114    int i1 = 0;
115    while (sz(i1) < di1) i1++;
116    int i2 = i1;
117    while (sz(i2) < di2) i2++;
118    return subselect(linspace(i1, i2));
119}
120
[145]121void RV::t ( int delta ) { times += delta;}
[8]122
[145]123bool RV::equal ( const RV &rv2 ) const {
[270]124        return ( ids == rv2.ids ) && ( times == rv2.times );
[102]125}
[5]126
[201]127mat epdf::sample_m ( int N ) const {
[270]128        mat X = zeros ( dim, N );
[145]129        for ( int i = 0;i < N;i++ ) X.set_col ( i, this->sample() );
[102]130        return X;
131};
132
133
134std::ostream &operator<< ( std::ostream &os, const RV &rv ) {
[270]135        int id;
[5]136        for ( int i = 0; i < rv.len ;i++ ) {
[270]137                id=rv.ids ( i );
138                os << id << "(" << RV_SIZES ( id ) << ")" <<  // id(size)=
139                "=" << RV_NAMES ( id )  << "_{"  << rv.times ( i ) << "}; "; //name_{time}
[5]140        }
141        return os;
142}
143
[145]144str RV::tostr() const {
[270]145        ivec idlist ( dsize );
146        ivec tmlist ( dsize );
[19]147        int i;
148        int pos = 0;
[145]149        for ( i = 0;i < len;i++ ) {
[280]150                idlist.set_subvector ( pos, pos + size ( i ) - 1, ids ( i ) );
151                tmlist.set_subvector ( pos, pos + size ( i ) - 1, times ( i ) );
152                pos += size ( i );
[19]153        }
[145]154        return str ( idlist, tmlist );
[19]155}
[32]156
[270]157ivec RV::dataind ( const RV &rv2 ) const {
[145]158        ivec res ( 0 );
[270]159        if ( rv2._dsize() >0 ) {
[165]160                str str2 = rv2.tostr();
161                ivec part;
162                int i;
163                for ( i = 0;i < len;i++ ) {
164                        part = itpp::find ( ( str2.ids == ids ( i ) ) & ( str2.times == times ( i ) ) );
165                        res = concat ( res, part );
166                }
[145]167        }
[270]168        it_assert_debug ( res.length() ==dsize,"this rv is not fully present in crv!" );
[145]169        return res;
[165]170
[145]171}
172
[270]173void RV::dataind ( const RV &rv2, ivec &selfi, ivec &rv2i ) const {
[182]174        //clean results
[270]175        selfi.set_size ( 0 );
176        rv2i.set_size ( 0 );
177
[182]178        // just in case any rv is empty
[270]179        if ( ( len==0 ) || ( rv2.length() ==0 ) ) {return;}
180
[182]181        //find comon rv
[270]182        ivec cids=itpp::find ( this->findself ( rv2 ) >=0 );
183
184        // index of
185        if ( cids.length() >0 ) {
[182]186                str str1 = tostr();
[270]187                str str2 = rv2.tostr();
188
[182]189                ivec part1;
190                ivec part2;
191                int i,j;
192                // find common rv in strs
193                for ( j=0; j < cids.length();j++ ) {
[270]194                        i = cids ( j );
[182]195                        part1 = itpp::find ( ( str1.ids == ids ( i ) ) & ( str1.times == times ( i ) ) );
196                        part2 = itpp::find ( ( str2.ids == ids ( i ) ) & ( str2.times == times ( i ) ) );
197                        selfi = concat ( selfi, part1 );
198                        rv2i = concat ( rv2i, part2 );
199                }
200        }
[270]201        it_assert_debug ( selfi.length() == rv2i.length(),"this should not happen!" );
[182]202}
203
[176]204RV RV::subt ( const RV &rv2 ) const {
[145]205        ivec res = this->findself ( rv2 ); // nonzeros
[178]206        ivec valid;
[270]207        if ( dsize>0 ) {valid= itpp::find ( res == -1 );} //-1 => value not found => it remains
[145]208        return ( *this ) ( valid ); //keep those that were not found in rv2
209}
210
211ivec RV::findself ( const RV &rv2 ) const {
212        int i, j;
213        ivec tmp = -ones_i ( len );
214        for ( i = 0;i < len;i++ ) {
215                for ( j = 0;j < rv2.length();j++ ) {
216                        if ( ( ids ( i ) == rv2.ids ( j ) ) & ( times ( i ) == rv2.times ( j ) ) ) {
217                                tmp ( i ) = j;
218                                break;
219                        }
220                }
221        }
222        return tmp;
223}
224
[377]225void RV::from_setting( const Setting &set ) 
[357]226{       
227        Array<string> A;
[377]228        if( set.exists("names"))
229                UI::get( A, set, "names" );
[357]230        else
231                A.set_length(0);
232       
233        ivec szs;
[377]234        if( set.exists("sizes"))
235                UI::get(szs,set,"sizes");
[357]236        else
237                szs = ones_i(A.length());
238       
239        ivec tms;
[377]240        if( set.exists( "times") )
241                UI::get(tms,set,"times");
[357]242        else
243                tms = zeros_i(A.length());
244       
245        // TODO tady se bude plnit primo do jeho promennych, a pak se zavola validacnni metoda, takze cele prepsat, ano?
246        init( A, szs, tms );
247}
248
[377]249/*void RV::to_setting( Setting &set ) const
[357]250{       
[377]251        Transport::to_setting( set );
[357]252
[377]253        Setting &kilometers_setting = set.add("kilometers", Setting::TypeInt );
[357]254        kilometers_setting = kilometers;
255
[377]256        UI::save( passengers, set, "passengers" );
[357]257}*/
258
[102]259RV concat ( const RV &rv1, const RV &rv2 ) {
[32]260        RV pom = rv1;
[102]261        pom.add ( rv2 );
[32]262        return pom;
263}
[170]264
[270]265RV compositepdf::getrv ( bool checkoverlap ) {
[175]266        RV rv; //empty rv
267        bool rvaddok;
[384]268        for ( int i = 0;i < mpdfs.length();i++ ) {
[175]269                rvaddok=rv.add ( mpdfs ( i )->_rv() ); //add rv to common rvs.
[270]270                // If rvaddok==false, mpdfs overlap => assert error.
271                it_assert_debug ( rvaddok|| ( !checkoverlap ),"mprod::mprod() input mpdfs overlap in rv!" );
[175]272        };
273        return rv;
274}
275
[270]276void compositepdf::setrvc ( const RV &rv, RV &rvc ) {
[384]277        for ( int i = 0;i < mpdfs.length();i++ ) {
[178]278                RV rvx = mpdfs ( i )->_rvc().subt ( rv );
279                rvc.add ( rvx ); //add rv to common rvc
[175]280        };
281}
282
[270]283void BM::bayesB ( const mat &Data ) {
284        for ( int t=0;t<Data.cols();t++ ) {bayes ( Data.get_col ( t ) );}
[182]285}
[262]286}
Note: See TracBrowser for help on using the browser.