uniform.cpp
Go to the documentation of this file.
1 // $Id: uniform.cpp tdelaet$
2 // Copyright (C) 2007 Tinne De Laet <first dot last at gmail dot com>
3 //
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU Lesser General Public License as published by
6 // the Free Software Foundation; either version 2.1 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 //
18 #include "uniform.h"
19 
20 #include "../wrappers/rng/rng.h" // Wrapper around several rng libraries
21 
22 #include <cmath>
23 #include <cassert>
24 
25 namespace BFL
26 {
27  using namespace MatrixWrapper;
28 
29  Uniform::Uniform (const ColumnVector& center, const ColumnVector& width)
30  : Pdf<ColumnVector> ( center.rows() )
31  , _samples(DimensionGet())
32  {
33  // check if inputs are consistent
34  assert (center.rows() == width.rows());
35  _Lower = center - width/2.0;
36  _Higher = center + width/2.0;
37  _Height = 1;
38  for (int i=1 ; i < width.rows()+1 ; i++ )
39  {
40  _Height = _Height / width(i);
41  }
42  }
43 
44  Uniform::Uniform (int dimension)
45  : Pdf<ColumnVector>(dimension)
46  , _samples(dimension)
47  {
48  _Lower.resize(dimension);
49  _Higher.resize(dimension);
50  }
51 
53 
54  std::ostream& operator<< (std::ostream& os, const Uniform& u)
55  {
56  os << "\nCenter: \n" << u.CenterGet()
57  << "\nWidth: \n" << u.WidthGet() << endl;
58  return os;
59  }
60 
61  //Clone function
63  {
64  return new Uniform(*this);
65  }
66 
67  Probability Uniform::ProbabilityGet(const ColumnVector& input) const
68  {
69  // test if input is located in area of Uniform distribution
70  for (int i=1; i<input.rows()+1; i++)
71  {
72  if ( ( input(i)>_Higher(i) ) || ( input(i) < _Lower(i) ) ) return 0;
73  }
74  return _Height;
75  }
76 
77  bool
78  Uniform::SampleFrom (vector<Sample<ColumnVector> >& list_samples, const int num_samples, int method, void * args) const
79  {
80  // Perform memory allocation
81  list_samples.resize(num_samples); // will break real-timeness if list_samples.size()!=num_samples
82  vector<Sample<ColumnVector> >::iterator rit = list_samples.begin();
83  switch(method)
84  {
85  case DEFAULT:
86  {
87  while (rit != list_samples.end())
88  {
89  for (unsigned int j=1; j < DimensionGet()+1; j++) _samples(j) = runif(_Lower(j) , _Higher(j) );
90  rit->ValueSet(_samples);
91  rit++;
92  }
93  return true;
94  }
95  default:
96  return false;
97  }
98  }
99 
100  bool
101  Uniform::SampleFrom (Sample<ColumnVector>& one_sample, int method, void * args) const
102  {
103  switch(method)
104  {
105  case DEFAULT:
106  {
107  for (unsigned int j=1; j < DimensionGet()+1; j++) _samples(j) = runif(_Lower(j) , _Higher(j) );
108  one_sample.ValueSet(_samples);
109  return true;
110  }
111  default:
112  return false;
113  }
114  }
115 
116  ColumnVector
118  {
119  return (_Higher+_Lower)/2.0;
120  }
121 
122  ColumnVector
124  {
125  return (_Higher-_Lower);
126  }
127 
128  void
129  Uniform::UniformSet (const ColumnVector& center,const ColumnVector& width)
130  {
131  assert(center.rows() == width.rows());
132  _Lower = center - width/2.0;
133  _Higher = center + width/2.0;
134  _Height = 1;
135  for (int i=1 ; i < width.rows()+1 ; i++ )
136  {
137  _Height = _Height / width(i);
138  }
139  if (this->DimensionGet() == 0) this->DimensionSet(center.rows());
140  assert(this->DimensionGet() == center.rows());
141  }
142 
143 
144 } // End namespace BFL
virtual Uniform * Clone() const
Clone function.
Definition: uniform.cpp:62
Class PDF: Virtual Base class representing Probability Density Functions.
Definition: pdf.h:53
#define DEFAULT
Class representing uniform density.
Definition: uniform.h:26
virtual ~Uniform()
Default Copy Constructor will do.
Definition: uniform.cpp:52
bool SampleFrom(vector< Sample< MatrixWrapper::ColumnVector > > &list_samples, const int num_samples, int method=DEFAULT, void *args=NULL) const
void ValueSet(const T &value)
Set the value of the Sample.
ColumnVector _samples
Definition: uniform.h:37
Uniform(const MatrixWrapper::ColumnVector &Center, const MatrixWrapper::ColumnVector &Width)
Constructor.
friend std::ostream & operator<<(std::ostream &os, const Uniform &u)
output stream for Uniform distribution
Definition: uniform.cpp:54
double _Height
Height of the uniform distribution.
Definition: uniform.h:34
void UniformSet(const MatrixWrapper::ColumnVector &center, const MatrixWrapper::ColumnVector &width)
Set the center and width of the uniform.
Definition: uniform.cpp:129
unsigned int DimensionGet() const
Get the dimension of the argument.
virtual MatrixWrapper::ColumnVector CenterGet() const
Get the center of the uniform.
Definition: uniform.cpp:117
virtual MatrixWrapper::ColumnVector WidthGet() const
Get the Width of the uniform distribution.
Definition: uniform.cpp:123
double runif()
virtual Probability ProbabilityGet(const MatrixWrapper::ColumnVector &input) const
Get the probability of a certain argument.
Definition: uniform.cpp:67
Class representing a probability (a double between 0 and 1)
Definition: bfl_constants.h:39
virtual void DimensionSet(unsigned int dim)
Set the dimension of the argument.
MatrixWrapper::ColumnVector _Higher
Upper border of the uniform distribution.
Definition: uniform.h:32
MatrixWrapper::ColumnVector _Lower
Lower border of the uniform distribution.
Definition: uniform.h:30


bfl
Author(s): Klaas Gadeyne, Wim Meeussen, Tinne Delaet and many others. See web page for a full contributor list. ROS package maintained by Wim Meeussen.
autogenerated on Mon Feb 28 2022 21:56:33