Changeset 1188
- Timestamp:
- 09/16/10 13:21:59 (14 years ago)
- Location:
- library/bdm/stat
- Files:
-
- 2 modified
Legend:
- Unmodified
- Added
- Removed
-
library/bdm/stat/discrete.cpp
r1064 r1188 50 50 actvec_ind ( j ) = 0; 51 51 } 52 actvolume = prod(steps); 52 53 return actvec; 53 54 } … … 66 67 } 67 68 } 69 // no need to set active volume 68 70 return actvec; 69 71 } … … 111 113 } 112 114 } 115 116 UI::get (volumes, set, "volumes",UI::optional); 117 if (volumes.length()<1){ 118 volumes = 1.0/points()*ones(points()); 119 } 120 113 121 } 114 122 115 123 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() );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() ); 120 128 } 121 129 } … … 123 131 124 132 void 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() ) ); 129 137 } 130 138 } 131 139 } 132 140 141 double 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; 133 152 } 153 154 } -
library/bdm/stat/discrete.h
r1066 r1188 21 21 namespace bdm { 22 22 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 23 61 //! Rectangular support 24 62 //! Support points are located inbetween ranges! For example: 25 63 //! For ranges=[0,1] and gridsizes=[1] the support point is 0.5 26 class rectangular_support: public root{64 class rectangular_support: public support_base { 27 65 protected: 28 66 //! Array of boundaries (2D vectors: [begining,end]) for each dimension … … 30 68 //! Number of support points in each dimension 31 69 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; 42 74 public: 43 75 //! default constructor 44 rectangular_support() : dim ( 0 ), Npoints ( 0) {76 rectangular_support() : support_base() { 45 77 } 46 78 … … 87 119 88 120 //! Discrete support with stored support points 89 class discrete_support: public root{121 class discrete_support: public support_base { 90 122 protected: 91 123 //! storage of support points … … 93 125 //! index in iterators 94 126 int idx; 127 //! volumes for each point 128 vec volumes; 95 129 public: 96 130 //! Default constructor 97 discrete_support() : Spoints ( 0 ), idx ( 0 ) {}131 discrete_support() : Spoints ( 0 ), idx ( 0 ), volumes(0) {} 98 132 //! Access function 99 133 int points() const { … … 102 136 //! set the first vector to corner and store result in actvec 103 137 const vec& first_vec() { 104 bdm_assert_debug ( Spoints.length() > 0, "Empty support" ); 138 139 bdm_assert_debug ( Spoints.length() > 0, "Empty support" ); 105 140 idx = 0; 141 actvolume=volumes(0); 106 142 return Spoints ( idx ); 107 143 } … … 109 145 const vec& next_vec() { 110 146 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; 112 151 } 113 152 … … 123 162 void from_setting ( const Setting &set ); 124 163 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 } 125 170 //! access function 126 171 Array<vec> & _Spoints() { … … 134 179 protected: 135 180 //! grid - function support 136 rectangular_supportsup;181 shared_ptr<support_base> sup; 137 182 //! function values on the grid 138 183 vec values; 139 184 public: 140 185 //! constructor function 141 void set_support ( rectangular_support&sup0 ) {186 void set_support ( shared_ptr<support_base> &sup0 ) { 142 187 sup = sup0; 143 values = zeros ( sup .points() );188 values = zeros ( sup->points() ); 144 189 } 145 190 //! constructor function fills values by calling function \c f , double f(vec&), given by a pointer … … 151 196 //! get value nearest to the given point 152 197 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() ; 155 203 156 204 vec eval ( const vec &val ) {