Changeset 558

Show
Ignore:
Timestamp:
08/19/09 01:41:25 (15 years ago)
Author:
smidl
Message:

New stuff for discrete grid world - pdf and function

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • library/bdm/stat/discrete.h

    r557 r558  
    6666                                actvec_ind.set_size(dim); 
    6767                        } 
     68                        //! return vector at position given by vector of indeces 
     69                        vec get_vec(const ivec &inds){ 
     70                                vec v; 
     71                                for ( int j = 0; j < dim; j++ ) { 
     72                                        it_assert_debug(inds(j)<gridsizes(j), "Index out of bounds"); 
     73                                        v ( j ) = ranges(j)(0) + (0.5+inds(j))*steps(j); 
     74                                } 
     75                                return v; 
     76                        } 
    6877 
     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                        }  
    6990                        //! set the first corner to actvec 
    7091                        const vec& first_vec(){ 
     
    7899                        const vec& next_vec() { 
    79100                                // go through all dimensions 
    80                                 for ( int j = 0; j < dim; j++ ) { 
     101                                int j=0; 
     102                                while (j<dim) { 
    81103                                        if ( actvec_ind ( j ) == gridsizes ( j ) - 1 ) { //j-th index is full 
    82104                                                actvec_ind ( j ) = 0; //shift back 
    83105                                                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  
     106                                                j++; 
    93107                                        } else { 
    94108                                                actvec_ind ( j ) ++; 
     
    99113                                return actvec; 
    100114                        }  
     115                         
     116                        ivec nearest_point(const vec &val){ 
     117                                ivec inds; 
     118                                inds.set_size(dim); 
     119                                for(int j=0; j<dim; j++){ 
     120                                        if (val(j)<ranges(j)(0)) 
     121                                                inds(j) = 0; 
     122                                        else { 
     123                                                if (val(j)>ranges(j)(1)) 
     124                                                        inds(j) = gridsizes(j)-1; 
     125                                                else { 
     126                                                        inds(j) = ::round(val(j) - ranges(j)(0)/ steps(j)); 
     127                                                } 
     128                                        } 
     129                                } 
     130                                return inds; 
     131                        } 
     132 
    101133                        //! Access function 
    102134                        int points() const {return Npoints;} 
     
    112144        }; 
    113145        UIREGISTER(rectangular_support); 
     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        }; 
    114182} 
    115183#endif //DISCR_H