Changeset 270 for bdm/stat/libBM.cpp

Show
Ignore:
Timestamp:
02/16/09 10:02:08 (15 years ago)
Author:
smidl
Message:

Changes in the very root classes!
* rv and rvc are no longer compulsory,
* samplecond does not return ll
* BM has drv

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • bdm/stat/libBM.cpp

    r265 r270  
    55//! Space of basic BDM structures 
    66namespace bdm { 
    7  
    8 using std::cout; 
    9  
    10 static int RVcounter=0; 
     7         
     8const int RV_BUFFER_STEP=1; 
     9RVmap RV_MAP; 
     10Array<string> RV_NAMES(RV_BUFFER_STEP); 
     11ivec RV_SIZES(RV_BUFFER_STEP); 
    1112 
    1213RV RV0=RV(); 
    1314 
    14 void RV::init ( ivec in_ids, Array<std::string> in_names, ivec in_sizes, ivec in_times ) { 
     15int RV::init ( const string &name, int size ) { 
    1516        //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 
     36int RV::countsize() const{ int tmp=0; for(int i=0;i<len;i++){tmp+=RV_SIZES(ids(i));} return tmp;} 
     37 
     38void 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        } 
    2950        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 
     54RV::RV ( string name, int sz, int tm ) { 
     55        Array<string> A ( 1 ); A ( 0 ) =name; 
     56        init (A,vec_1 ( sz ),vec_1 ( tm ) ); 
    5857} 
    5958 
     
    6463                ivec index = itpp::find ( ind==-1 ); 
    6564 
    66  
    6765                if ( index.length() < rv2.len ) { //conflict 
    6866                        ids = concat ( ids, rv2.ids ( index ) ); 
    69                         sizes = concat ( sizes, rv2.sizes ( index ) ); 
    7067                        times = concat ( times, rv2.times ( index ) ); 
    71                         names = concat ( names, rv2.names ( to_Arr ( index ) ) ); 
    7268                } 
    7369                else { 
    7470                        ids = concat ( ids, rv2.ids ); 
    75                         sizes = concat ( sizes, rv2.sizes ); 
    7671                        times = concat ( times, rv2.times ); 
    77                         names = concat ( names, rv2.names ); 
    78                 } 
    79                 tsize = sum ( sizes ); 
     72                } 
    8073                len = ids.length(); 
    81                 return ( index.length() ==rv2.len ); 
     74                dsize = countsize(); 
     75                return ( index.length() ==rv2.len ); //conflict or not 
    8276        } 
    8377        else { //rv2 is empty 
    84                 return true; 
    85         } 
    86 //      return *this; 
     78                return true; // no conflict 
     79        } 
    8780}; 
    8881 
    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 { 
     82RV RV::subselect ( const ivec &ind ) const { 
    10383        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(); 
    10588        return ret; 
    10689} 
     
    10891void RV::t ( int delta ) { times += delta;} 
    10992 
    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  
    11893bool 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 ); 
    12095} 
    12196 
    12297mat epdf::sample_m ( int N ) const { 
    123         mat X = zeros ( rv.count(), N ); 
     98        mat X = zeros ( dim, N ); 
    12499        for ( int i = 0;i < N;i++ ) X.set_col ( i, this->sample() ); 
    125100        return X; 
     
    128103 
    129104std::ostream &operator<< ( std::ostream &os, const RV &rv ) { 
    130  
     105        int id; 
    131106        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} 
    134110        } 
    135111        return os; 
     
    137113 
    138114str RV::tostr() const { 
    139         ivec idlist ( tsize ); 
    140         ivec tmlist ( tsize ); 
     115        ivec idlist ( dsize ); 
     116        ivec tmlist ( dsize ); 
    141117        int i; 
    142118        int pos = 0; 
    143119        for ( i = 0;i < len;i++ ) { 
    144                 idlist.set_subvector ( pos, pos + sizes ( i ) - 1, ids ( i ) ); 
    145                 tmlist.set_subvector ( pos, pos + sizes ( i ) - 1, times ( i ) ); 
    146                 pos += sizes ( 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) ); 
    147123        } 
    148124        return str ( idlist, tmlist ); 
    149125} 
    150126 
    151 ivec RV::dataind (const RV &rv2 ) const { 
     127ivec RV::dataind ( const RV &rv2 ) const { 
    152128        ivec res ( 0 ); 
    153         if ( rv2.count()>0 ) { 
     129        if ( rv2._dsize() >0 ) { 
    154130                str str2 = rv2.tostr(); 
    155131                ivec part; 
     
    160136                } 
    161137        } 
    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!" ); 
    163139        return res; 
    164140 
    165141} 
    166142 
    167 void RV::dataind (const RV &rv2, ivec &selfi, ivec &rv2i) const { 
     143void RV::dataind ( const RV &rv2, ivec &selfi, ivec &rv2i ) const { 
    168144        //clean results 
    169         selfi.set_size(0); 
    170         rv2i.set_size(0); 
    171          
     145        selfi.set_size ( 0 ); 
     146        rv2i.set_size ( 0 ); 
     147 
    172148        // 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 
    175151        //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 ) { 
    180156                str str1 = tostr(); 
    181                 str str2 = rv2.tostr();  
    182                  
     157                str str2 = rv2.tostr(); 
     158 
    183159                ivec part1; 
    184160                ivec part2; 
     
    186162                // find common rv in strs 
    187163                for ( j=0; j < cids.length();j++ ) { 
    188                         i = cids(j); 
     164                        i = cids ( j ); 
    189165                        part1 = itpp::find ( ( str1.ids == ids ( i ) ) & ( str1.times == times ( i ) ) ); 
    190166                        part2 = itpp::find ( ( str2.ids == ids ( i ) ) & ( str2.times == times ( i ) ) ); 
     
    193169                } 
    194170        } 
    195         it_assert_debug(selfi.length() == rv2i.length(),"this should not happen!"); 
     171        it_assert_debug ( selfi.length() == rv2i.length(),"this should not happen!" ); 
    196172} 
    197173 
     
    199175        ivec res = this->findself ( rv2 ); // nonzeros 
    200176        ivec valid; 
    201         if (tsize>0) {valid= itpp::find ( res == -1 );} //-1 => value not found => it remains 
     177        if ( dsize>0 ) {valid= itpp::find ( res == -1 );} //-1 => value not found => it remains 
    202178        return ( *this ) ( valid ); //keep those that were not found in rv2 
    203179} 
     
    223199} 
    224200 
    225 void RV::newids(){ids=linspace ( RVcounter+1, RVcounter+len ),RVcounter+=len;} 
    226  
    227 RV compositepdf::getrv(bool checkoverlap){ 
     201RV compositepdf::getrv ( bool checkoverlap ) { 
    228202        RV rv; //empty rv 
    229203        bool rvaddok; 
    230         for (int i = 0;i < n;i++ ) { 
     204        for ( int i = 0;i < n;i++ ) { 
    231205                rvaddok=rv.add ( mpdfs ( i )->_rv() ); //add rv to common rvs. 
    232                         // If rvaddok==false, mpdfs overlap => assert error. 
    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!" ); 
    234208        }; 
    235209        return rv; 
    236210} 
    237211 
    238 void compositepdf::setrvc(const RV &rv, RV &rvc){ 
    239         for (int i = 0;i < n;i++ ) { 
     212void compositepdf::setrvc ( const RV &rv, RV &rvc ) { 
     213        for ( int i = 0;i < n;i++ ) { 
    240214                RV rvx = mpdfs ( i )->_rvc().subt ( rv ); 
    241215                rvc.add ( rvx ); //add rv to common rvc 
     
    243217} 
    244218 
    245 void BM::bayesB(const mat &Data){ 
    246         for(int t=0;t<Data.cols();t++){bayes(Data.get_col(t));} 
    247 } 
    248 } 
     219void BM::bayesB ( const mat &Data ) { 
     220        for ( int t=0;t<Data.cols();t++ ) {bayes ( Data.get_col ( t ) );} 
     221} 
     222}