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

Revision 384, 6.6 kB (checked in by mido, 15 years ago)

possibly broken?

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