[643] | 1 | #include "discrete.h" |
---|
[723] | 2 | |
---|
| 3 | namespace bdm { |
---|
| 4 | |
---|
| 5 | void rectangular_support::set_parameters ( const Array<vec> &ranges0, const ivec &gridsize0 ) { |
---|
| 6 | ranges = ranges0; |
---|
| 7 | gridsizes = gridsize0; |
---|
| 8 | initialize(); |
---|
| 9 | } |
---|
| 10 | |
---|
| 11 | |
---|
| 12 | void 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 | |
---|
| 27 | vec 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 | |
---|
| 36 | long 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 |
---|
[896] | 43 | dim_skips *= gridsizes ( j ); // indices in the next dimension are repeated with period gridsizes(j) times greater that in this dimesion |
---|
[723] | 44 | } |
---|
| 45 | return ind; |
---|
| 46 | } |
---|
| 47 | const 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 | return actvec; |
---|
| 53 | } |
---|
| 54 | const vec& rectangular_support::next_vec() { |
---|
| 55 | // go through all dimensions |
---|
| 56 | int j = 0; |
---|
| 57 | while ( j < dim ) { |
---|
| 58 | if ( actvec_ind ( j ) == gridsizes ( j ) - 1 ) { //j-th index is full |
---|
| 59 | actvec_ind ( j ) = 0; //shift back |
---|
| 60 | actvec ( j ) = ranges ( j ) ( 0 ) + 0.5 * steps ( j ); |
---|
| 61 | j++; |
---|
| 62 | } else { |
---|
| 63 | actvec_ind ( j ) ++; |
---|
| 64 | actvec ( j ) += steps ( j ); |
---|
| 65 | break; |
---|
| 66 | } |
---|
| 67 | } |
---|
| 68 | return actvec; |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | |
---|
| 72 | ivec rectangular_support::nearest_point ( const vec &val ) { |
---|
| 73 | ivec inds; |
---|
| 74 | inds.set_size ( dim ); |
---|
| 75 | for ( int j = 0; j < dim; j++ ) { |
---|
| 76 | if ( val ( j ) < ranges ( j ) ( 0 ) ) |
---|
| 77 | inds ( j ) = 0; |
---|
| 78 | else { |
---|
| 79 | if ( val ( j ) > ranges ( j ) ( 1 ) ) |
---|
| 80 | //! \todo GRIDSIZES prejmenovat.. |
---|
| 81 | inds ( j ) = gridsizes ( j ) - 1; |
---|
| 82 | else { |
---|
[766] | 83 | inds ( j ) = (int) ::round ( val ( j ) - ranges ( j ) ( 0 ) / steps ( j ) ); |
---|
[723] | 84 | } |
---|
| 85 | } |
---|
| 86 | } |
---|
| 87 | return inds; |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | void rectangular_support::from_setting ( const Setting &set ) { |
---|
| 91 | UI::get ( ranges , set, "ranges", UI::compulsory ); |
---|
| 92 | UI::get ( gridsizes, set, "gridsizes", UI::compulsory ); |
---|
| 93 | initialize(); |
---|
| 94 | } |
---|
| 95 | |
---|
| 96 | |
---|
| 97 | |
---|
| 98 | void discrete_support::from_setting ( const Setting &set ) { |
---|
[760] | 99 | UI::get ( Spoints, set, "points", UI::optional ); |
---|
[723] | 100 | if ( points() < 1 ) { |
---|
| 101 | int npoints; |
---|
| 102 | shared_ptr<epdf> ep = UI::build<epdf> ( set, "epdf", UI::compulsory ); |
---|
| 103 | if ( !UI::get ( npoints, set, "npoints", UI::optional ) ) { |
---|
| 104 | npoints = 100; |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | //sample |
---|
| 108 | Spoints.set_size ( npoints ); |
---|
| 109 | for ( int i = 0; i < points(); i++ ) { |
---|
| 110 | Spoints ( i ) = ep->sample(); |
---|
| 111 | } |
---|
| 112 | } |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | void grid_fnc::set_values ( double ( *evalptr ) ( const vec& ) ) { |
---|
| 116 | if ( sup.points() > 0 ) { |
---|
| 117 | values ( 0 ) = ( *evalptr ) ( sup.first_vec() ); |
---|
| 118 | for ( int j = 1; j < sup.points(); j++ ) { |
---|
| 119 | values ( j ) = ( *evalptr ) ( sup.next_vec() ); |
---|
| 120 | } |
---|
| 121 | } |
---|
| 122 | } |
---|
| 123 | |
---|
[737] | 124 | void grid_fnc::set_values ( const epdf &ep ) { |
---|
[730] | 125 | if ( sup.points() > 0 ) { |
---|
[737] | 126 | values ( 0 ) = exp ( ep.evallog ( sup.first_vec() ) ); |
---|
[730] | 127 | for ( int j = 1; j < sup.points(); j++ ) { |
---|
[737] | 128 | values ( j ) = exp ( ep.evallog ( sup.next_vec() ) ); |
---|
[730] | 129 | } |
---|
| 130 | } |
---|
[723] | 131 | } |
---|
[730] | 132 | |
---|
| 133 | } |
---|