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

Revision 436, 6.9 kB (checked in by vbarta, 15 years ago)

moved egiw_test to testsuite (partially converted to a configurable test); added public method clearing RVs

  • 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
139
140std::ostream &operator<< ( std::ostream &os, const RV &rv ) {
141        int id;
142        for ( int i = 0; i < rv.len ;i++ ) {
143                id=rv.ids ( i );
144                os << id << "(" << RV_SIZES ( id ) << ")" <<  // id(size)=
145                "=" << RV_NAMES ( id )  << "_{"  << rv.times ( i ) << "}; "; //name_{time}
146        }
147        return os;
148}
149
150str RV::tostr() const {
151        ivec idlist ( dsize );
152        ivec tmlist ( dsize );
153        int i;
154        int pos = 0;
155        for ( i = 0;i < len;i++ ) {
156                idlist.set_subvector ( pos, pos + size ( i ) - 1, ids ( i ) );
157                tmlist.set_subvector ( pos, pos + size ( i ) - 1, times ( i ) );
158                pos += size ( i );
159        }
160        return str ( idlist, tmlist );
161}
162
163ivec RV::dataind ( const RV &rv2 ) const {
164        ivec res ( 0 );
165        if ( rv2._dsize() >0 ) {
166                str str2 = rv2.tostr();
167                ivec part;
168                int i;
169                for ( i = 0;i < len;i++ ) {
170                        part = itpp::find ( ( str2.ids == ids ( i ) ) & ( str2.times == times ( i ) ) );
171                        res = concat ( res, part );
172                }
173        }
174        it_assert_debug ( res.length() ==dsize,"this rv is not fully present in crv!" );
175        return res;
176
177}
178
179void RV::dataind ( const RV &rv2, ivec &selfi, ivec &rv2i ) const {
180        //clean results
181        selfi.set_size ( 0 );
182        rv2i.set_size ( 0 );
183
184        // just in case any rv is empty
185        if ( ( len==0 ) || ( rv2.length() ==0 ) ) {return;}
186
187        //find comon rv
188        ivec cids=itpp::find ( this->findself ( rv2 ) >=0 );
189
190        // index of
191        if ( cids.length() >0 ) {
192                str str1 = tostr();
193                str str2 = rv2.tostr();
194
195                ivec part1;
196                ivec part2;
197                int i,j;
198                // find common rv in strs
199                for ( j=0; j < cids.length();j++ ) {
200                        i = cids ( j );
201                        part1 = itpp::find ( ( str1.ids == ids ( i ) ) & ( str1.times == times ( i ) ) );
202                        part2 = itpp::find ( ( str2.ids == ids ( i ) ) & ( str2.times == times ( i ) ) );
203                        selfi = concat ( selfi, part1 );
204                        rv2i = concat ( rv2i, part2 );
205                }
206        }
207        it_assert_debug ( selfi.length() == rv2i.length(),"this should not happen!" );
208}
209
210RV RV::subt ( const RV &rv2 ) const {
211        ivec res = this->findself ( rv2 ); // nonzeros
212        ivec valid;
213        if ( dsize>0 ) {valid= itpp::find ( res == -1 );} //-1 => value not found => it remains
214        return ( *this ) ( valid ); //keep those that were not found in rv2
215}
216
217ivec RV::findself ( const RV &rv2 ) const {
218        int i, j;
219        ivec tmp = -ones_i ( len );
220        for ( i = 0;i < len;i++ ) {
221                for ( j = 0;j < rv2.length();j++ ) {
222                        if ( ( ids ( i ) == rv2.ids ( j ) ) & ( times ( i ) == rv2.times ( j ) ) ) {
223                                tmp ( i ) = j;
224                                break;
225                        }
226                }
227        }
228        return tmp;
229}
230
231void RV::from_setting( const Setting &set ) 
232{       
233        Array<string> A;
234        if( set.exists("names"))
235                UI::get( A, set, "names" );
236        else
237                A.set_length(0);
238       
239        ivec szs;
240        if( set.exists("sizes"))
241                UI::get(szs,set,"sizes");
242        else
243                szs = ones_i(A.length());
244       
245        ivec tms;
246        if( set.exists( "times") )
247                UI::get(tms,set,"times");
248        else
249                tms = zeros_i(A.length());
250       
251        // TODO tady se bude plnit primo do jeho promennych, a pak se zavola validacnni metoda, takze cele prepsat, ano?
252        init( A, szs, tms );
253}
254
255/*void RV::to_setting( Setting &set ) const
256{       
257        Transport::to_setting( set );
258
259        Setting &kilometers_setting = set.add("kilometers", Setting::TypeInt );
260        kilometers_setting = kilometers;
261
262        UI::save( passengers, set, "passengers" );
263}*/
264
265RV concat ( const RV &rv1, const RV &rv2 ) {
266        RV pom = rv1;
267        pom.add ( rv2 );
268        return pom;
269}
270
271RV compositepdf::getrv ( bool checkoverlap ) {
272        RV rv; //empty rv
273        bool rvaddok;
274        for ( int i = 0;i < mpdfs.length();i++ ) {
275                rvaddok=rv.add ( mpdfs ( i )->_rv() ); //add rv to common rvs.
276                // If rvaddok==false, mpdfs overlap => assert error.
277                it_assert_debug ( rvaddok|| ( !checkoverlap ),"mprod::mprod() input mpdfs overlap in rv!" );
278        };
279        return rv;
280}
281
282void compositepdf::setrvc ( const RV &rv, RV &rvc ) {
283        for ( int i = 0;i < mpdfs.length();i++ ) {
284                RV rvx = mpdfs ( i )->_rvc().subt ( rv );
285                rvc.add ( rvx ); //add rv to common rvc
286        };
287}
288
289void BM::bayesB ( const mat &Data ) {
290        for ( int t=0;t<Data.cols();t++ ) {bayes ( Data.get_col ( t ) );}
291}
292}
Note: See TracBrowser for help on using the browser.