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
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
14int RV::init ( const string &name, int size ) {
15        //Refer
16        int id;
17        RVmap::const_iterator iter = RV_MAP.begin();
18        iter=RV_MAP.find ( name );
19        if ( iter == RV_MAP.end() ) {
20                id=RV_MAP.size() +1;
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               
30                RV_MAP.insert ( make_pair ( name,id ) ); //add new rv
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 );
34                }
35                RV_NAMES ( id ) =name;
36                RV_SIZES ( id ) =size;
37        }
38        else {
39                id = iter->second;
40                it_assert(RV_SIZES(id)==size,"RV "+name+" of different size already exists");
41        }
42        return id;
43};
44
45int RV::countsize() const{ int tmp=0; for ( int i=0;i<len;i++ ) {tmp+=RV_SIZES ( ids ( i ) );} return tmp;}
46
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
57void RV::init(const Array<std::string> &in_names, const ivec &in_sizes, const ivec &in_times)
58{
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\" " );
62
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();
72}
73
74RV::RV ( string name, int sz, int tm ) {
75        Array<string> A ( 1 ); A ( 0 ) =name;
76        init ( A,vec_1 ( sz ),vec_1 ( tm ) );
77}
78
79bool RV::add ( const RV &rv2 ) {
80        // TODO
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();
94                dsize = countsize();
95                return ( index.length() ==rv2.len ); //conflict or not
96        }
97        else { //rv2 is empty
98                return true; // no conflict
99        }
100};
101
102RV RV::subselect ( const ivec &ind ) const {
103        RV ret;
104        ret.ids = ids ( ind );
105        ret.times= times ( ind );
106        ret.len = ind.length();
107        ret.dsize=ret.countsize();
108        return ret;
109}
110
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
121void RV::t ( int delta ) { times += delta;}
122
123bool RV::equal ( const RV &rv2 ) const {
124        return ( ids == rv2.ids ) && ( times == rv2.times );
125}
126
127mat epdf::sample_m ( int N ) const {
128        mat X = zeros ( dim, N );
129        for ( int i = 0;i < N;i++ ) X.set_col ( i, this->sample() );
130        return X;
131};
132
133
134std::ostream &operator<< ( std::ostream &os, const RV &rv ) {
135        int id;
136        for ( int i = 0; i < rv.len ;i++ ) {
137                id=rv.ids ( i );
138                os << id << "(" << RV_SIZES ( id ) << ")" <<  // id(size)=
139                "=" << RV_NAMES ( id )  << "_{"  << rv.times ( i ) << "}; "; //name_{time}
140        }
141        return os;
142}
143
144str RV::tostr() const {
145        ivec idlist ( dsize );
146        ivec tmlist ( dsize );
147        int i;
148        int pos = 0;
149        for ( i = 0;i < len;i++ ) {
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 );
153        }
154        return str ( idlist, tmlist );
155}
156
157ivec RV::dataind ( const RV &rv2 ) const {
158        ivec res ( 0 );
159        if ( rv2._dsize() >0 ) {
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                }
167        }
168        it_assert_debug ( res.length() ==dsize,"this rv is not fully present in crv!" );
169        return res;
170
171}
172
173void RV::dataind ( const RV &rv2, ivec &selfi, ivec &rv2i ) const {
174        //clean results
175        selfi.set_size ( 0 );
176        rv2i.set_size ( 0 );
177
178        // just in case any rv is empty
179        if ( ( len==0 ) || ( rv2.length() ==0 ) ) {return;}
180
181        //find comon rv
182        ivec cids=itpp::find ( this->findself ( rv2 ) >=0 );
183
184        // index of
185        if ( cids.length() >0 ) {
186                str str1 = tostr();
187                str str2 = rv2.tostr();
188
189                ivec part1;
190                ivec part2;
191                int i,j;
192                // find common rv in strs
193                for ( j=0; j < cids.length();j++ ) {
194                        i = cids ( j );
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        }
201        it_assert_debug ( selfi.length() == rv2i.length(),"this should not happen!" );
202}
203
204RV RV::subt ( const RV &rv2 ) const {
205        ivec res = this->findself ( rv2 ); // nonzeros
206        ivec valid;
207        if ( dsize>0 ) {valid= itpp::find ( res == -1 );} //-1 => value not found => it remains
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
225void RV::from_setting( const Setting &set ) 
226{       
227        Array<string> A;
228        if( set.exists("names"))
229                UI::get( A, set, "names" );
230        else
231                A.set_length(0);
232       
233        ivec szs;
234        if( set.exists("sizes"))
235                UI::get(szs,set,"sizes");
236        else
237                szs = ones_i(A.length());
238       
239        ivec tms;
240        if( set.exists( "times") )
241                UI::get(tms,set,"times");
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
249/*void RV::to_setting( Setting &set ) const
250{       
251        Transport::to_setting( set );
252
253        Setting &kilometers_setting = set.add("kilometers", Setting::TypeInt );
254        kilometers_setting = kilometers;
255
256        UI::save( passengers, set, "passengers" );
257}*/
258
259RV concat ( const RV &rv1, const RV &rv2 ) {
260        RV pom = rv1;
261        pom.add ( rv2 );
262        return pom;
263}
264
265RV compositepdf::getrv ( bool checkoverlap ) {
266        RV rv; //empty rv
267        bool rvaddok;
268        for ( int i = 0;i < mpdfs.length();i++ ) {
269                rvaddok=rv.add ( mpdfs ( i )->_rv() ); //add rv to common rvs.
270                // If rvaddok==false, mpdfs overlap => assert error.
271                it_assert_debug ( rvaddok|| ( !checkoverlap ),"mprod::mprod() input mpdfs overlap in rv!" );
272        };
273        return rv;
274}
275
276void compositepdf::setrvc ( const RV &rv, RV &rvc ) {
277        for ( int i = 0;i < mpdfs.length();i++ ) {
278                RV rvx = mpdfs ( i )->_rvc().subt ( rv );
279                rvc.add ( rvx ); //add rv to common rvc
280        };
281}
282
283void BM::bayesB ( const mat &Data ) {
284        for ( int t=0;t<Data.cols();t++ ) {bayes ( Data.get_col ( t ) );}
285}
286}
Note: See TracBrowser for help on using the browser.