| 78 | //! convert dimension indeces into linear index, the indexing is in the same way as in \c next_vec() |
| 79 | long linear_index (const ivec inds){ |
| 80 | long ind=0; |
| 81 | it_assert_debug(inds.length()==dim,"Improper indeces inds"); |
| 82 | int dim_skips=1; // skips in active dimension, in the first dimension, the skips are 1 index per value |
| 83 | |
| 84 | for (int j=0; j<dim; j++){ |
| 85 | ind += dim_skips*(inds(j)); // add shift in linear index caused by this dimension |
| 86 | dim_skips*=gridsizes(j); // indeces in the next dimension are repeated with period gridsizes(j) times greater that in this dimesion |
| 87 | } |
| 88 | return ind; |
| 89 | } |
| 146 | |
| 147 | class grid_fnc: public fnc{ |
| 148 | protected: |
| 149 | rectangular_support sup; |
| 150 | vec values; |
| 151 | public: |
| 152 | //! constructor function |
| 153 | void set_support(rectangular_support &sup0){sup=sup0; values=zeros(sup.points());} |
| 154 | //! constructor function fills values by calling function \c f , double f(vec&), given by a pointer |
| 155 | void set_values(double (*evalptr)(const vec&)){ |
| 156 | if (sup.points()>0){ |
| 157 | values(0) = (*evalptr)(sup.first_vec()); |
| 158 | for (int j=1; j<sup.points(); j++){ |
| 159 | values(j)=(*evalptr)(sup.next_vec()); |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | //! get value nearest to the given point |
| 164 | double nearest_val(const vec &val){return values( sup.linear_index(sup.nearest_point(val)));} |
| 165 | |
| 166 | vec eval(const vec &val){return vec_1(nearest_val(val));} |
| 167 | }; |
| 168 | UIREGISTER(grid_fnc); |
| 169 | |
| 170 | //! Piecewise constant pdf on rectangular support |
| 171 | //! Each point on the grid represents a centroid around which the density is constant. |
| 172 | //! This is a trivial point-mass density where all points have the same mass. |
| 173 | class egrid: public epdf{ |
| 174 | protected: |
| 175 | rectangular_support sup; |
| 176 | vec values; |
| 177 | public: |
| 178 | //! we assume that evallog is not called too often otherwise we should cache log(values) |
| 179 | double evallog(const vec &val){return log(values( sup.linear_index(sup.nearest_point(val))));} |
| 180 | |
| 181 | }; |