Changeset 1188 for library

Show
Ignore:
Timestamp:
09/16/10 13:21:59 (14 years ago)
Author:
smidl
Message:

support has abstract base

Location:
library/bdm/stat
Files:
2 modified

Legend:

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

    r1064 r1188  
    5050        actvec_ind ( j ) = 0; 
    5151    } 
     52    actvolume = prod(steps); 
    5253    return actvec; 
    5354} 
     
    6667        } 
    6768    } 
     69    // no need to set active volume 
    6870    return actvec; 
    6971} 
     
    111113        } 
    112114    } 
     115     
     116    UI::get (volumes, set, "volumes",UI::optional); 
     117        if (volumes.length()<1){ 
     118                volumes = 1.0/points()*ones(points()); 
     119        }  
     120         
    113121} 
    114122 
    115123void 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() ); 
     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() ); 
    120128        } 
    121129    } 
     
    123131 
    124132void grid_fnc::set_values ( const epdf &ep ) { 
    125     if ( sup.points() > 0 ) { 
    126         values ( 0 ) =  exp ( ep.evallog ( sup.first_vec() ) ); 
    127         for ( int j = 1; j < sup.points(); j++ ) { 
    128             values ( j ) = exp ( ep.evallog ( sup.next_vec() ) ); 
     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() ) ); 
    129137        } 
    130138    } 
    131139} 
    132140 
     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; 
    133152} 
     153 
     154} 
  • library/bdm/stat/discrete.h

    r1066 r1188  
    2121namespace bdm { 
    2222 
     23        class support_base: public root { 
     24        protected: 
     25                //! dimension 
     26                int dim; 
     27                //! Number of data points 
     28                int Npoints; 
     29                //! active vector for first_vec and next_vec 
     30                vec actvec; 
     31                //! volume of the active point 
     32                double actvolume; 
     33        public: 
     34                //! default constructor 
     35                support_base() : dim ( 0 ), Npoints ( 0 ) { 
     36                } 
     37                                                 
     38                //! set the first vector to corner and store result in actvec 
     39                virtual const vec& first_vec() NOT_IMPLEMENTED(empty_vec); 
     40                 
     41                //! Get next active vector, call ONLY after first_vector()! 
     42                virtual const vec& next_vec() NOT_IMPLEMENTED(empty_vec);; 
     43                 
     44                //! return active vector, call ONLY after first_vector()! 
     45                virtual const vec& act_vec() { 
     46                        return actvec; 
     47                }; 
     48                //! return active vector, call ONLY after first_vector()! 
     49                virtual const double& act_volume() { 
     50                        return actvolume; 
     51                }; 
     52                 
     53                //! Access function 
     54                int points() const { 
     55                        return Npoints; 
     56                } 
     57                                 
     58        }; 
     59        UIREGISTER ( support_base ); 
     60         
    2361//! Rectangular support 
    2462//! Support points are located inbetween ranges! For example: 
    2563//! For ranges=[0,1] and gridsizes=[1] the support point is 0.5 
    26 class rectangular_support: public root { 
     64class rectangular_support: public support_base { 
    2765protected: 
    2866    //! Array of boundaries (2D vectors: [begining,end]) for each dimension 
     
    3068    //! Number of support points in each dimension 
    3169    ivec gridsizes; 
    32     //! dimension 
    33     int dim; 
    34     //! Number of data points 
    35     int Npoints; 
    36     //! active vector for first_vec and next_vec 
    37     vec actvec; 
    38     //! indices of active vector 
    39     vec actvec_ind; 
    40     //! length of steps in each dimension 
    41     vec steps; 
     70        //! indices of active vector 
     71        vec actvec_ind; 
     72        // 
     73        vec steps; 
    4274public: 
    4375    //! default constructor 
    44     rectangular_support() : dim ( 0 ), Npoints ( 0 ) { 
     76    rectangular_support() : support_base() { 
    4577    } 
    4678 
     
    87119 
    88120//! Discrete support with stored support points 
    89 class discrete_support: public root { 
     121class discrete_support: public support_base { 
    90122protected: 
    91123    //! storage of support points 
     
    93125    //! index in iterators 
    94126    int idx; 
     127        //! volumes for each point 
     128        vec volumes; 
    95129public: 
    96130    //! Default constructor 
    97     discrete_support() : Spoints ( 0 ), idx ( 0 ) {} 
     131    discrete_support() : Spoints ( 0 ), idx ( 0 ), volumes(0) {} 
    98132    //! Access function 
    99133    int points() const { 
     
    102136    //! set the first vector to corner and store result in actvec 
    103137    const vec& first_vec() { 
    104         bdm_assert_debug ( Spoints.length() > 0, "Empty support" ); 
     138 
     139                bdm_assert_debug ( Spoints.length() > 0, "Empty support" ); 
    105140        idx = 0; 
     141                actvolume=volumes(0); 
    106142        return Spoints ( idx ); 
    107143    } 
     
    109145    const vec& next_vec() { 
    110146        bdm_assert_debug ( Spoints.length() > idx - 1, "Out of support points" ); 
    111         return Spoints ( ++idx ); 
     147                idx++; 
     148                actvolume=volumes(idx); 
     149                actvec = Spoints ( idx ); 
     150        return actvec; 
    112151    } 
    113152 
     
    123162    void from_setting ( const Setting &set ); 
    124163 
     164        void validate(){ 
     165                bdm_assert(volumes.length()==Spoints.length(),"dimension mismatch"); 
     166                Npoints=Spoints.length(); 
     167                if (Npoints>0) 
     168                        dim = Spoints(0).length(); 
     169        } 
    125170    //! access function 
    126171    Array<vec> & _Spoints() { 
     
    134179protected: 
    135180    //! grid - function support 
    136     rectangular_support sup; 
     181    shared_ptr<support_base> sup; 
    137182    //! function values on the grid 
    138183    vec values; 
    139184public: 
    140185    //! constructor function 
    141     void set_support ( rectangular_support &sup0 ) { 
     186    void set_support ( shared_ptr<support_base> &sup0 ) { 
    142187        sup = sup0; 
    143         values = zeros ( sup.points() ); 
     188        values = zeros ( sup->points() ); 
    144189    } 
    145190    //! constructor function fills values by calling function \c f , double f(vec&), given by a pointer 
     
    151196    //! get value nearest to the given point 
    152197    double nearest_val ( const vec &val ) { 
    153         return values ( sup.linear_index ( sup.nearest_point ( val ) ) ); 
    154     } 
     198                NOT_IMPLEMENTED(0.0); 
     199//        return values ( sup.linear_index ( sup.nearest_point ( val ) ) ); 
     200    } 
     201 
     202        double integrate() ; 
    155203 
    156204    vec eval ( const vec &val ) {