Changeset 270 for bdm/stat/libBM.cpp
- Timestamp:
- 02/16/09 10:02:08 (15 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
bdm/stat/libBM.cpp
r265 r270 5 5 //! Space of basic BDM structures 6 6 namespace bdm { 7 8 using std::cout; 9 10 static int RVcounter=0; 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); 11 12 12 13 RV RV0=RV(); 13 14 14 void RV::init ( ivec in_ids, Array<std::string> in_names, ivec in_sizes, ivec in_times) {15 int RV::init ( const string &name, int size ) { 15 16 //Refer 16 int i; 17 len = in_ids.length(); 18 //PRUDENT_MODE 19 // All vetors should be of same length 20 if ( ( len != in_names.length() ) || \ 21 ( len != in_sizes.length() ) || \ 22 ( len != in_times.length() ) ) { 23 it_error ( "RV::RV inconsistent length of input vectors." ); 24 } 25 26 ids = in_ids; 27 names = in_names; 28 sizes = in_sizes; 17 int id; 18 RVmap::const_iterator iter = RV_MAP.find ( name ); 19 if ( iter == RV_MAP.end() ) { 20 id=RV_MAP.size()+1; 21 RV_MAP.insert ( make_pair ( name,id ) ); //add new rv 22 if (id>=RV_NAMES.length()){ 23 RV_NAMES.set_length(id+RV_BUFFER_STEP,true); 24 RV_SIZES.set_length(id+RV_BUFFER_STEP,true); 25 } 26 RV_NAMES(id)=name; 27 RV_SIZES(id)=size; 28 } 29 else { 30 id = iter->second; 31 it_warning ("RV of name " + name + "already exists" ); 32 } 33 return id; 34 }; 35 36 int RV::countsize() const{ int tmp=0; for(int i=0;i<len;i++){tmp+=RV_SIZES(ids(i));} return tmp;} 37 38 void RV::init ( Array<std::string> in_names, ivec in_sizes,ivec in_times ) { 39 len = in_names.length(); 40 it_assert_debug ( in_names.length() ==in_times.length(), "check \"times\" " ); 41 it_assert_debug ( in_names.length() ==in_sizes.length(), "check \"sizes\" " ); 42 43 times.set_length ( len ); 44 ids.set_length ( len ); 45 int id; 46 for ( int i=0; i<len; i++ ) { 47 id = init ( in_names ( i ), in_sizes ( i ) ); 48 ids ( i ) = id; 49 } 29 50 times = in_times; 30 tsize = 0; 31 for ( i = 0;i < len;i++ ) {tsize += sizes ( i );} 32 }; 33 34 RV::RV ( Array<std::string> in_names, ivec in_sizes, ivec in_times ) { 35 int len = in_names.length(); 36 init ( linspace ( RVcounter+1, RVcounter+len ), in_names, in_sizes, in_times ); 37 RVcounter+=len; 38 } 39 40 RV::RV ( Array<std::string> in_names, ivec in_sizes ) { 41 int len = in_names.length(); 42 init ( linspace ( RVcounter+1, RVcounter+len ), in_names, in_sizes, zeros_i ( in_names.length() ) ); 43 RVcounter+=len; 44 } 45 46 RV::RV ( Array<std::string> in_names ) { 47 int len = in_names.length(); 48 init ( linspace ( RVcounter+1, RVcounter+len ), in_names, ones_i ( in_names.length() ), zeros_i ( in_names.length() ) ); 49 RVcounter+=len; 50 } 51 52 RV::RV() : tsize ( 0 ), len ( 0 ), ids ( 0 ), sizes ( 0 ), times ( 0 ),names ( 0 ) {}; 53 54 RV::RV(string name, int id, int sz, int tm){ 55 if (id>RVcounter) {RVcounter=id;}; 56 Array<string> A(1); A(0)=name; 57 init(vec_1(id),A,vec_1(sz),vec_1(tm)); 51 dsize = countsize(); 52 } 53 54 RV::RV ( string name, int sz, int tm ) { 55 Array<string> A ( 1 ); A ( 0 ) =name; 56 init (A,vec_1 ( sz ),vec_1 ( tm ) ); 58 57 } 59 58 … … 64 63 ivec index = itpp::find ( ind==-1 ); 65 64 66 67 65 if ( index.length() < rv2.len ) { //conflict 68 66 ids = concat ( ids, rv2.ids ( index ) ); 69 sizes = concat ( sizes, rv2.sizes ( index ) );70 67 times = concat ( times, rv2.times ( index ) ); 71 names = concat ( names, rv2.names ( to_Arr ( index ) ) );72 68 } 73 69 else { 74 70 ids = concat ( ids, rv2.ids ); 75 sizes = concat ( sizes, rv2.sizes );76 71 times = concat ( times, rv2.times ); 77 names = concat ( names, rv2.names ); 78 } 79 tsize = sum ( sizes ); 72 } 80 73 len = ids.length(); 81 return ( index.length() ==rv2.len ); 74 dsize = countsize(); 75 return ( index.length() ==rv2.len ); //conflict or not 82 76 } 83 77 else { //rv2 is empty 84 return true; 85 } 86 // return *this; 78 return true; // no conflict 79 } 87 80 }; 88 81 89 // RV::RV ( ivec in_ids ) { 90 // 91 // len = in_ids.length(); 92 // Array<std::string> A ( len ); 93 // std::string rvstr = "rv"; 94 // 95 // for ( int i = 0; i < len;i++ ) { 96 // A ( i ) = rvstr + to_str ( i ); 97 // } 98 // 99 // init ( in_ids, A, ones_i ( len ), zeros_i ( len ) ); 100 // } 101 102 RV RV::subselect (const ivec &ind ) const { 82 RV RV::subselect ( const ivec &ind ) const { 103 83 RV ret; 104 ret.init ( ids ( ind ), names ( to_Arr ( ind ) ), sizes ( ind ), times ( ind ) ); 84 ret.ids = ids(ind); 85 ret.times= times(ind); 86 ret.len = ind.length(); 87 ret.dsize=ret.countsize(); 105 88 return ret; 106 89 } … … 108 91 void RV::t ( int delta ) { times += delta;} 109 92 110 RV RV::operator() (const ivec &ind ) const {111 RV ret;112 if ( ind.length() >0 ) {113 ret.init ( ids ( ind ), names ( to_Arr ( ind ) ), sizes ( ind ), times ( ind ) );114 }115 return ret;116 }117 118 93 bool RV::equal ( const RV &rv2 ) const { 119 return ( ids == rv2.ids ) && ( times == rv2.times ) && ( sizes == rv2.sizes );94 return ( ids == rv2.ids ) && ( times == rv2.times ); 120 95 } 121 96 122 97 mat epdf::sample_m ( int N ) const { 123 mat X = zeros ( rv.count(), N );98 mat X = zeros ( dim, N ); 124 99 for ( int i = 0;i < N;i++ ) X.set_col ( i, this->sample() ); 125 100 return X; … … 128 103 129 104 std::ostream &operator<< ( std::ostream &os, const RV &rv ) { 130 105 int id; 131 106 for ( int i = 0; i < rv.len ;i++ ) { 132 os << rv.ids ( i ) << "(" << rv.sizes ( i ) << ")" << // id(size)= 133 "=" << rv.names ( i ) << "_{" << rv.times ( i ) << "}; "; //name_{time} 107 id=rv.ids ( i ); 108 os << id << "(" << RV_SIZES ( id ) << ")" << // id(size)= 109 "=" << RV_NAMES ( id ) << "_{" << rv.times ( i ) << "}; "; //name_{time} 134 110 } 135 111 return os; … … 137 113 138 114 str RV::tostr() const { 139 ivec idlist ( tsize );140 ivec tmlist ( tsize );115 ivec idlist ( dsize ); 116 ivec tmlist ( dsize ); 141 117 int i; 142 118 int pos = 0; 143 119 for ( i = 0;i < len;i++ ) { 144 idlist.set_subvector ( pos, pos + size s ( i) - 1, ids ( i ) );145 tmlist.set_subvector ( pos, pos + size s ( i) - 1, times ( i ) );146 pos += size s ( i);120 idlist.set_subvector ( pos, pos + size ( ids(i) ) - 1, ids ( i ) ); 121 tmlist.set_subvector ( pos, pos + size ( ids(i) ) - 1, times ( i ) ); 122 pos += size ( ids(i) ); 147 123 } 148 124 return str ( idlist, tmlist ); 149 125 } 150 126 151 ivec RV::dataind ( const RV &rv2 ) const {127 ivec RV::dataind ( const RV &rv2 ) const { 152 128 ivec res ( 0 ); 153 if ( rv2. count()>0 ) {129 if ( rv2._dsize() >0 ) { 154 130 str str2 = rv2.tostr(); 155 131 ivec part; … … 160 136 } 161 137 } 162 it_assert_debug (res.length()==tsize,"this rv is not fully present in crv!");138 it_assert_debug ( res.length() ==dsize,"this rv is not fully present in crv!" ); 163 139 return res; 164 140 165 141 } 166 142 167 void RV::dataind ( const RV &rv2, ivec &selfi, ivec &rv2i) const {143 void RV::dataind ( const RV &rv2, ivec &selfi, ivec &rv2i ) const { 168 144 //clean results 169 selfi.set_size (0);170 rv2i.set_size (0);171 145 selfi.set_size ( 0 ); 146 rv2i.set_size ( 0 ); 147 172 148 // just in case any rv is empty 173 if ( (len==0)||(rv2.length()==0)){return;}174 149 if ( ( len==0 ) || ( rv2.length() ==0 ) ) {return;} 150 175 151 //find comon rv 176 ivec cids=itpp::find (this->findself(rv2)>=0);177 178 // index of 179 if ( cids.length() >0 ) {152 ivec cids=itpp::find ( this->findself ( rv2 ) >=0 ); 153 154 // index of 155 if ( cids.length() >0 ) { 180 156 str str1 = tostr(); 181 str str2 = rv2.tostr(); 182 157 str str2 = rv2.tostr(); 158 183 159 ivec part1; 184 160 ivec part2; … … 186 162 // find common rv in strs 187 163 for ( j=0; j < cids.length();j++ ) { 188 i = cids (j);164 i = cids ( j ); 189 165 part1 = itpp::find ( ( str1.ids == ids ( i ) ) & ( str1.times == times ( i ) ) ); 190 166 part2 = itpp::find ( ( str2.ids == ids ( i ) ) & ( str2.times == times ( i ) ) ); … … 193 169 } 194 170 } 195 it_assert_debug (selfi.length() == rv2i.length(),"this should not happen!");171 it_assert_debug ( selfi.length() == rv2i.length(),"this should not happen!" ); 196 172 } 197 173 … … 199 175 ivec res = this->findself ( rv2 ); // nonzeros 200 176 ivec valid; 201 if ( tsize>0) {valid= itpp::find ( res == -1 );} //-1 => value not found => it remains177 if ( dsize>0 ) {valid= itpp::find ( res == -1 );} //-1 => value not found => it remains 202 178 return ( *this ) ( valid ); //keep those that were not found in rv2 203 179 } … … 223 199 } 224 200 225 void RV::newids(){ids=linspace ( RVcounter+1, RVcounter+len ),RVcounter+=len;} 226 227 RV compositepdf::getrv(bool checkoverlap){ 201 RV compositepdf::getrv ( bool checkoverlap ) { 228 202 RV rv; //empty rv 229 203 bool rvaddok; 230 for ( int i = 0;i < n;i++ ) {204 for ( int i = 0;i < n;i++ ) { 231 205 rvaddok=rv.add ( mpdfs ( i )->_rv() ); //add rv to common rvs. 232 233 it_assert_debug (rvaddok||(!checkoverlap),"mprod::mprod() input mpdfs overlap in rv!");206 // If rvaddok==false, mpdfs overlap => assert error. 207 it_assert_debug ( rvaddok|| ( !checkoverlap ),"mprod::mprod() input mpdfs overlap in rv!" ); 234 208 }; 235 209 return rv; 236 210 } 237 211 238 void compositepdf::setrvc (const RV &rv, RV &rvc){239 for ( int i = 0;i < n;i++ ) {212 void compositepdf::setrvc ( const RV &rv, RV &rvc ) { 213 for ( int i = 0;i < n;i++ ) { 240 214 RV rvx = mpdfs ( i )->_rvc().subt ( rv ); 241 215 rvc.add ( rvx ); //add rv to common rvc … … 243 217 } 244 218 245 void BM::bayesB (const mat &Data){246 for (int t=0;t<Data.cols();t++){bayes(Data.get_col(t));}247 } 248 } 219 void BM::bayesB ( const mat &Data ) { 220 for ( int t=0;t<Data.cols();t++ ) {bayes ( Data.get_col ( t ) );} 221 } 222 }