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

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

removed useless (double) iterator initialization

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