root/bdm/stat/libBM.cpp @ 278

Revision 278, 5.7 kB (checked in by smidl, 15 years ago)

props

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