1 | |
---|
2 | #include "libBM.h" |
---|
3 | #include "../itpp_ext.h" |
---|
4 | |
---|
5 | //! Space of basic BDM structures |
---|
6 | namespace bdm { |
---|
7 | |
---|
8 | const int RV_BUFFER_STEP=1; |
---|
9 | RVmap RV_MAP; |
---|
10 | Array<string> RV_NAMES ( RV_BUFFER_STEP ); |
---|
11 | ivec RV_SIZES ( RV_BUFFER_STEP ); |
---|
12 | |
---|
13 | RV RV0=RV(); |
---|
14 | |
---|
15 | int RV::init ( const string &name, int size ) { |
---|
16 | //Refer |
---|
17 | int id; |
---|
18 | RVmap::const_iterator iter = RV_MAP.begin(); |
---|
19 | iter=RV_MAP.find ( name ); |
---|
20 | if ( iter == RV_MAP.end() ) { |
---|
21 | id=RV_MAP.size() +1; |
---|
22 | //debug |
---|
23 | /* { |
---|
24 | cout << endl; |
---|
25 | RVmap::const_iterator iter = RV_MAP.begin(); |
---|
26 | for(RVmap::const_iterator iter=RV_MAP.begin(); iter!=RV_MAP.end(); iter++){ |
---|
27 | cout << "key: " << iter->first << " val: " << iter->second <<endl; |
---|
28 | } |
---|
29 | }*/ |
---|
30 | |
---|
31 | RV_MAP.insert ( make_pair ( name,id ) ); //add new rv |
---|
32 | if ( id>=RV_NAMES.length() ) { |
---|
33 | RV_NAMES.set_length ( id+RV_BUFFER_STEP,true ); |
---|
34 | RV_SIZES.set_length ( id+RV_BUFFER_STEP,true ); |
---|
35 | } |
---|
36 | RV_NAMES ( id ) =name; |
---|
37 | RV_SIZES ( id ) =size; |
---|
38 | } |
---|
39 | else { |
---|
40 | id = iter->second; |
---|
41 | it_assert(RV_SIZES(id)==size,"RV "+name+" of different size already exists"); |
---|
42 | } |
---|
43 | return id; |
---|
44 | }; |
---|
45 | |
---|
46 | int RV::countsize() const{ int tmp=0; for ( int i=0;i<len;i++ ) {tmp+=RV_SIZES ( ids ( i ) );} return tmp;} |
---|
47 | |
---|
48 | ivec RV::cumsizes() const { |
---|
49 | ivec szs ( len ); |
---|
50 | int tmp=0; |
---|
51 | for ( int i=0;i<len;i++ ) { |
---|
52 | tmp+=RV_SIZES ( ids ( i ) ); |
---|
53 | szs ( i ) = tmp; |
---|
54 | } |
---|
55 | return szs; |
---|
56 | } |
---|
57 | |
---|
58 | void RV::init ( Array<std::string> in_names, ivec in_sizes,ivec in_times ) { |
---|
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 | |
---|
74 | RV::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 | |
---|
79 | bool 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 | |
---|
102 | RV 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 | |
---|
111 | void RV::t ( int delta ) { times += delta;} |
---|
112 | |
---|
113 | bool RV::equal ( const RV &rv2 ) const { |
---|
114 | return ( ids == rv2.ids ) && ( times == rv2.times ); |
---|
115 | } |
---|
116 | |
---|
117 | mat epdf::sample_m ( int N ) const { |
---|
118 | mat X = zeros ( dim, N ); |
---|
119 | for ( int i = 0;i < N;i++ ) X.set_col ( i, this->sample() ); |
---|
120 | return X; |
---|
121 | }; |
---|
122 | |
---|
123 | |
---|
124 | std::ostream &operator<< ( std::ostream &os, const RV &rv ) { |
---|
125 | int id; |
---|
126 | for ( int i = 0; i < rv.len ;i++ ) { |
---|
127 | id=rv.ids ( i ); |
---|
128 | os << id << "(" << RV_SIZES ( id ) << ")" << // id(size)= |
---|
129 | "=" << RV_NAMES ( id ) << "_{" << rv.times ( i ) << "}; "; //name_{time} |
---|
130 | } |
---|
131 | return os; |
---|
132 | } |
---|
133 | |
---|
134 | str RV::tostr() const { |
---|
135 | ivec idlist ( dsize ); |
---|
136 | ivec tmlist ( dsize ); |
---|
137 | int i; |
---|
138 | int pos = 0; |
---|
139 | for ( i = 0;i < len;i++ ) { |
---|
140 | idlist.set_subvector ( pos, pos + size ( i ) - 1, ids ( i ) ); |
---|
141 | tmlist.set_subvector ( pos, pos + size ( i ) - 1, times ( i ) ); |
---|
142 | pos += size ( i ); |
---|
143 | } |
---|
144 | return str ( idlist, tmlist ); |
---|
145 | } |
---|
146 | |
---|
147 | ivec RV::dataind ( const RV &rv2 ) const { |
---|
148 | ivec res ( 0 ); |
---|
149 | if ( rv2._dsize() >0 ) { |
---|
150 | str str2 = rv2.tostr(); |
---|
151 | ivec part; |
---|
152 | int i; |
---|
153 | for ( i = 0;i < len;i++ ) { |
---|
154 | part = itpp::find ( ( str2.ids == ids ( i ) ) & ( str2.times == times ( i ) ) ); |
---|
155 | res = concat ( res, part ); |
---|
156 | } |
---|
157 | } |
---|
158 | it_assert_debug ( res.length() ==dsize,"this rv is not fully present in crv!" ); |
---|
159 | return res; |
---|
160 | |
---|
161 | } |
---|
162 | |
---|
163 | void RV::dataind ( const RV &rv2, ivec &selfi, ivec &rv2i ) const { |
---|
164 | //clean results |
---|
165 | selfi.set_size ( 0 ); |
---|
166 | rv2i.set_size ( 0 ); |
---|
167 | |
---|
168 | // just in case any rv is empty |
---|
169 | if ( ( len==0 ) || ( rv2.length() ==0 ) ) {return;} |
---|
170 | |
---|
171 | //find comon rv |
---|
172 | ivec cids=itpp::find ( this->findself ( rv2 ) >=0 ); |
---|
173 | |
---|
174 | // index of |
---|
175 | if ( cids.length() >0 ) { |
---|
176 | str str1 = tostr(); |
---|
177 | str str2 = rv2.tostr(); |
---|
178 | |
---|
179 | ivec part1; |
---|
180 | ivec part2; |
---|
181 | int i,j; |
---|
182 | // find common rv in strs |
---|
183 | for ( j=0; j < cids.length();j++ ) { |
---|
184 | i = cids ( j ); |
---|
185 | part1 = itpp::find ( ( str1.ids == ids ( i ) ) & ( str1.times == times ( i ) ) ); |
---|
186 | part2 = itpp::find ( ( str2.ids == ids ( i ) ) & ( str2.times == times ( i ) ) ); |
---|
187 | selfi = concat ( selfi, part1 ); |
---|
188 | rv2i = concat ( rv2i, part2 ); |
---|
189 | } |
---|
190 | } |
---|
191 | it_assert_debug ( selfi.length() == rv2i.length(),"this should not happen!" ); |
---|
192 | } |
---|
193 | |
---|
194 | RV RV::subt ( const RV &rv2 ) const { |
---|
195 | ivec res = this->findself ( rv2 ); // nonzeros |
---|
196 | ivec valid; |
---|
197 | if ( dsize>0 ) {valid= itpp::find ( res == -1 );} //-1 => value not found => it remains |
---|
198 | return ( *this ) ( valid ); //keep those that were not found in rv2 |
---|
199 | } |
---|
200 | |
---|
201 | ivec RV::findself ( const RV &rv2 ) const { |
---|
202 | int i, j; |
---|
203 | ivec tmp = -ones_i ( len ); |
---|
204 | for ( i = 0;i < len;i++ ) { |
---|
205 | for ( j = 0;j < rv2.length();j++ ) { |
---|
206 | if ( ( ids ( i ) == rv2.ids ( j ) ) & ( times ( i ) == rv2.times ( j ) ) ) { |
---|
207 | tmp ( i ) = j; |
---|
208 | break; |
---|
209 | } |
---|
210 | } |
---|
211 | } |
---|
212 | return tmp; |
---|
213 | } |
---|
214 | |
---|
215 | RV concat ( const RV &rv1, const RV &rv2 ) { |
---|
216 | RV pom = rv1; |
---|
217 | pom.add ( rv2 ); |
---|
218 | return pom; |
---|
219 | } |
---|
220 | |
---|
221 | RV compositepdf::getrv ( bool checkoverlap ) { |
---|
222 | RV rv; //empty rv |
---|
223 | bool rvaddok; |
---|
224 | for ( int i = 0;i < n;i++ ) { |
---|
225 | rvaddok=rv.add ( mpdfs ( i )->_rv() ); //add rv to common rvs. |
---|
226 | // If rvaddok==false, mpdfs overlap => assert error. |
---|
227 | it_assert_debug ( rvaddok|| ( !checkoverlap ),"mprod::mprod() input mpdfs overlap in rv!" ); |
---|
228 | }; |
---|
229 | return rv; |
---|
230 | } |
---|
231 | |
---|
232 | void compositepdf::setrvc ( const RV &rv, RV &rvc ) { |
---|
233 | for ( int i = 0;i < n;i++ ) { |
---|
234 | RV rvx = mpdfs ( i )->_rvc().subt ( rv ); |
---|
235 | rvc.add ( rvx ); //add rv to common rvc |
---|
236 | }; |
---|
237 | } |
---|
238 | |
---|
239 | void BM::bayesB ( const mat &Data ) { |
---|
240 | for ( int t=0;t<Data.cols();t++ ) {bayes ( Data.get_col ( t ) );} |
---|
241 | } |
---|
242 | } |
---|