1 | /*! |
---|
2 | \file |
---|
3 | \brief Probability distributions for discrete support densities |
---|
4 | \author Vaclav Smidl. |
---|
5 | |
---|
6 | ----------------------------------- |
---|
7 | BDM++ - C++ library for Bayesian Decision Making under Uncertainty |
---|
8 | |
---|
9 | Using IT++ for numerical operations |
---|
10 | ----------------------------------- |
---|
11 | */ |
---|
12 | |
---|
13 | #ifndef DISCR_H |
---|
14 | #define DISCR_H |
---|
15 | |
---|
16 | |
---|
17 | #include "../shared_ptr.h" |
---|
18 | #include "../base/bdmbase.h" |
---|
19 | #include "../math/chmat.h" |
---|
20 | |
---|
21 | namespace bdm |
---|
22 | { |
---|
23 | |
---|
24 | //! Rectangular support |
---|
25 | //! Support ponits are located inbetween ranges! For example: |
---|
26 | //! For ranges=[0,1] and gridsizes=[1] the support point is 0.5 |
---|
27 | class rectangular_support: public root { |
---|
28 | protected: |
---|
29 | //! Array of boundaries (2D vectors: [begining,end]) for each dimension |
---|
30 | Array<vec> ranges; |
---|
31 | //! Number of support points in each dimension |
---|
32 | ivec gridsizes; |
---|
33 | //! dimension |
---|
34 | int dim; |
---|
35 | //! Number of data points |
---|
36 | int Npoints; |
---|
37 | //! active vector for first_vec and next_vec |
---|
38 | vec actvec; |
---|
39 | //! indeces of active vector |
---|
40 | vec actvec_ind; |
---|
41 | //! length of steps in each dimension |
---|
42 | vec steps; |
---|
43 | public: |
---|
44 | //! default constructor |
---|
45 | rectangular_support(){}; |
---|
46 | |
---|
47 | //! set parameters |
---|
48 | void set_parameters(const Array<vec> &ranges0, const ivec &gridsize0){ |
---|
49 | ranges=ranges0; |
---|
50 | gridsizes=gridsize0; |
---|
51 | initialize(); |
---|
52 | } |
---|
53 | //! Internal functio to set temporaries correctly |
---|
54 | void initialize() { |
---|
55 | dim = ranges.length(); |
---|
56 | it_assert_debug(gridsizes.length()==dim,"Incompatible dimensions of input"); |
---|
57 | Npoints = prod(gridsizes); |
---|
58 | it_assert_debug(Npoints>0,"Wrong input parameters"); |
---|
59 | |
---|
60 | //precompute steps |
---|
61 | steps.set_size(dim); |
---|
62 | for ( int j = 0; j < dim; j++ ) { |
---|
63 | steps ( j ) = ( ranges ( j ) ( 1 ) - ranges(j)(0) ) / gridsizes ( j ); |
---|
64 | } |
---|
65 | actvec.set_size(dim); |
---|
66 | actvec_ind.set_size(dim); |
---|
67 | } |
---|
68 | |
---|
69 | //! set the first corner to actvec |
---|
70 | const vec& first_vec(){ |
---|
71 | for ( int j = 0; j < dim; j++ ) { |
---|
72 | actvec ( j ) = ranges(j)(0) + 0.5*steps(j); |
---|
73 | actvec_ind(j) = 0; |
---|
74 | } |
---|
75 | return actvec; |
---|
76 | } |
---|
77 | //! Get next active vector, call ONLY after first_vector()! |
---|
78 | const vec& next_vec() { |
---|
79 | // go through all dimensions |
---|
80 | for ( int j = 0; j < dim; j++ ) { |
---|
81 | if ( actvec_ind ( j ) == gridsizes ( j ) - 1 ) { //j-th index is full |
---|
82 | actvec_ind ( j ) = 0; //shift back |
---|
83 | actvec ( j ) = ranges ( j ) ( 0 ) + 0.5*steps(j); |
---|
84 | |
---|
85 | //! |
---|
86 | if ( j+1<dim) |
---|
87 | if (actvec_ind ( j + 1 )<gridsizes(j+1)-1){ |
---|
88 | actvec_ind ( j + 1 ) ++; //increase the next dimension; |
---|
89 | actvec ( j + 1 ) += steps ( j + 1 ); |
---|
90 | break; |
---|
91 | } |
---|
92 | |
---|
93 | } else { |
---|
94 | actvec_ind ( j ) ++; |
---|
95 | actvec ( j ) += steps ( j ); |
---|
96 | break; |
---|
97 | } |
---|
98 | } |
---|
99 | return actvec; |
---|
100 | } |
---|
101 | //! Access function |
---|
102 | int points() const {return Npoints;} |
---|
103 | |
---|
104 | //! access function |
---|
105 | const vec& _steps() const {return steps;} |
---|
106 | |
---|
107 | void from_setting (const Setting &set) { |
---|
108 | UI::get (ranges , set, "ranges", UI::compulsory); |
---|
109 | UI::get (gridsizes, set, "gridsizes", UI::compulsory); |
---|
110 | initialize(); |
---|
111 | } |
---|
112 | }; |
---|
113 | UIREGISTER(rectangular_support); |
---|
114 | } |
---|
115 | #endif //DISCR_H |
---|