root/bdm/stat/libBM.cpp @ 254

Revision 254, 6.0 kB (checked in by smidl, 15 years ago)

create namespace bdm

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