root/library/bdm/stat/discrete.cpp

Revision 1188, 4.6 kB (checked in by smidl, 14 years ago)

support has abstract base

Line 
1#include "discrete.h"
2
3namespace bdm {
4
5void rectangular_support::set_parameters ( const Array<vec> &ranges0, const ivec &gridsize0 ) {
6    ranges = ranges0;
7    gridsizes = gridsize0;
8    initialize();
9}
10
11
12void rectangular_support::initialize() {
13    dim = ranges.length();
14    bdm_assert ( gridsizes.length() == dim, "Incompatible dimensions of input" );
15    Npoints = prod ( gridsizes );
16    bdm_assert ( Npoints > 0, "Wrong input parameters" );
17
18    //precompute steps
19    steps.set_size ( dim );
20    for ( int j = 0; j < dim; j++ ) {
21        steps ( j ) = ( ranges ( j ) ( 1 ) - ranges ( j ) ( 0 ) ) / gridsizes ( j );
22    }
23    actvec.set_size ( dim );
24    actvec_ind.set_size ( dim );
25}
26
27vec rectangular_support::get_vec ( const ivec &inds ) {
28    vec v ( dim );
29    for ( int j = 0; j < dim; j++ ) {
30        bdm_assert_debug ( inds ( j ) < gridsizes ( j ), "Index out of bounds" );
31        v ( j ) = ranges ( j ) ( 0 ) + ( 0.5 + inds ( j ) ) * steps ( j );
32    }
33    return v;
34}
35
36long rectangular_support::linear_index ( const ivec inds ) {
37    long ind = 0;
38    bdm_assert_debug ( inds.length() == dim, "Improper indices inds" );
39    int dim_skips = 1; // skips in active dimension, in the first dimension, the skips are 1 index per value
40
41    for ( int j = 0; j < dim; j++ ) {
42        ind += dim_skips * ( inds ( j ) ); // add shift in linear index caused by this dimension
43        dim_skips *= gridsizes ( j );  // indices in the next dimension are repeated with period gridsizes(j) times greater that in this dimesion
44    }
45    return ind;
46}
47const vec& rectangular_support::first_vec() {
48    for ( int j = 0; j < dim; j++ ) {
49        actvec ( j ) = ranges ( j ) ( 0 ) + 0.5 * steps ( j );
50        actvec_ind ( j ) = 0;
51    }
52    actvolume = prod(steps);
53    return actvec;
54}
55const vec& rectangular_support::next_vec() {
56    // go through all dimensions
57    int j = 0;
58    while ( j < dim ) {
59        if ( actvec_ind ( j ) == gridsizes ( j ) - 1 ) { //j-th index is full
60            actvec_ind ( j ) = 0; //shift back
61            actvec ( j ) = ranges ( j ) ( 0 ) + 0.5 * steps ( j );
62            j++;
63        } else {
64            actvec_ind ( j ) ++;
65            actvec ( j ) += steps ( j );
66            break;
67        }
68    }
69    // no need to set active volume
70    return actvec;
71}
72
73
74ivec rectangular_support::nearest_point ( const vec &val ) {
75    ivec inds;
76    inds.set_size ( dim );
77    for ( int j = 0; j < dim; j++ ) {
78        if ( val ( j ) < ranges ( j ) ( 0 ) )
79            inds ( j ) = 0;
80        else {
81            if ( val ( j ) > ranges ( j ) ( 1 ) )
82                //! \todo GRIDSIZES prejmenovat..
83                inds ( j ) = gridsizes ( j ) - 1;
84            else {
85                inds ( j ) = (int) ::round ( val ( j ) - ranges ( j ) ( 0 ) / steps ( j ) );
86            }
87        }
88    }
89    return inds;
90}
91
92void rectangular_support::from_setting ( const Setting &set ) {
93    UI::get ( ranges , set, "ranges", UI::compulsory );
94    UI::get ( gridsizes, set, "gridsizes", UI::compulsory );
95    initialize();
96}
97
98
99
100void discrete_support::from_setting ( const Setting &set ) {
101    UI::get ( Spoints, set, "points", UI::optional );
102    if ( points() < 1 ) {
103        int npoints;
104        shared_ptr<epdf> ep = UI::build<epdf> ( set, "epdf", UI::compulsory );
105        if ( !UI::get ( npoints, set, "npoints", UI::optional ) ) {
106            npoints = 100;
107        }
108
109        //sample
110        Spoints.set_size ( npoints );
111        for ( int i = 0; i < points(); i++ ) {
112            Spoints ( i ) = ep->sample();
113        }
114    }
115   
116    UI::get (volumes, set, "volumes",UI::optional);
117        if (volumes.length()<1){
118                volumes = 1.0/points()*ones(points());
119        } 
120       
121}
122
123void grid_fnc::set_values ( double ( *evalptr ) ( const vec& ) ) {
124    if ( sup->points() > 0 ) {
125        values ( 0 ) = ( *evalptr ) ( sup->first_vec() );
126        for ( int j = 1; j < sup->points(); j++ ) {
127            values ( j ) = ( *evalptr ) ( sup->next_vec() );
128        }
129    }
130}
131
132void grid_fnc::set_values ( const epdf &ep ) {
133    if ( sup->points() > 0 ) {
134        values ( 0 ) =  exp ( ep.evallog ( sup->first_vec() ) );
135        for ( int j = 1; j < sup->points(); j++ ) {
136            values ( j ) = exp ( ep.evallog ( sup->next_vec() ) );
137        }
138    }
139}
140
141double grid_fnc::integrate ( ) {
142        double intgr=0.0;
143        if ( sup->points() > 0 ) {
144                sup->first_vec(); // also first volume
145                intgr+= values(0) * sup->act_volume();
146                for ( int j = 1; j < sup->points(); j++ ) {
147                        sup->next_vec();
148                        intgr+= values(j)*sup->act_volume();
149                }
150        }
151        return intgr;
152}
153
154}
Note: See TracBrowser for help on using the browser.