/*! \file \brief Probability distributions for discrete support densities \author Vaclav Smidl. ----------------------------------- BDM++ - C++ library for Bayesian Decision Making under Uncertainty Using IT++ for numerical operations ----------------------------------- */ #ifndef DISCR_H #define DISCR_H #include "../shared_ptr.h" #include "../base/bdmbase.h" #include "../math/chmat.h" namespace bdm { //! Rectangular support //! Support ponits are located inbetween ranges! For example: //! For ranges=[0,1] and gridsizes=[1] the support point is 0.5 class rectangular_support: public root { protected: //! Array of boundaries (2D vectors: [begining,end]) for each dimension Array ranges; //! Number of support points in each dimension ivec gridsizes; //! dimension int dim; //! Number of data points int Npoints; //! active vector for first_vec and next_vec vec actvec; //! indeces of active vector vec actvec_ind; //! length of steps in each dimension vec steps; public: //! default constructor rectangular_support(){}; //! set parameters void set_parameters(const Array &ranges0, const ivec &gridsize0){ ranges=ranges0; gridsizes=gridsize0; initialize(); } //! Internal functio to set temporaries correctly void initialize() { dim = ranges.length(); it_assert_debug(gridsizes.length()==dim,"Incompatible dimensions of input"); Npoints = prod(gridsizes); it_assert_debug(Npoints>0,"Wrong input parameters"); //precompute steps steps.set_size(dim); for ( int j = 0; j < dim; j++ ) { steps ( j ) = ( ranges ( j ) ( 1 ) - ranges(j)(0) ) / gridsizes ( j ); } actvec.set_size(dim); actvec_ind.set_size(dim); } //! return vector at position given by vector of indeces vec get_vec(const ivec &inds){ vec v; for ( int j = 0; j < dim; j++ ) { it_assert_debug(inds(j)ranges(j)(1)) inds(j) = gridsizes(j)-1; else { inds(j) = ::round(val(j) - ranges(j)(0)/ steps(j)); } } } return inds; } //! Access function int points() const {return Npoints;} //! access function const vec& _steps() const {return steps;} void from_setting (const Setting &set) { UI::get (ranges , set, "ranges", UI::compulsory); UI::get (gridsizes, set, "gridsizes", UI::compulsory); initialize(); } }; UIREGISTER(rectangular_support); class grid_fnc: public fnc{ protected: rectangular_support sup; vec values; public: //! constructor function void set_support(rectangular_support &sup0){sup=sup0; values=zeros(sup.points());} //! constructor function fills values by calling function \c f , double f(vec&), given by a pointer void set_values(double (*evalptr)(const vec&)){ if (sup.points()>0){ values(0) = (*evalptr)(sup.first_vec()); for (int j=1; j