uniform.cpp
Go to the documentation of this file.
00001 // $Id: uniform.cpp tdelaet$
00002 // Copyright (C) 2007 Tinne De Laet <first dot last at gmail dot com>
00003 //
00004 // This program is free software; you can redistribute it and/or modify
00005 // it under the terms of the GNU Lesser General Public License as published by
00006 // the Free Software Foundation; either version 2.1 of the License, or
00007 // (at your option) any later version.
00008 //
00009 // This program is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 // GNU Lesser General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU Lesser General Public License
00015 // along with this program; if not, write to the Free Software
00016 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00017 //
00018 #include "uniform.h"
00019 
00020 #include "../wrappers/rng/rng.h" // Wrapper around several rng libraries
00021 
00022 #include <cmath>
00023 #include <cassert>
00024 
00025 namespace BFL
00026 {
00027   using namespace MatrixWrapper;
00028 
00029   Uniform::Uniform (const ColumnVector& center, const ColumnVector& width)
00030     : Pdf<ColumnVector> ( center.rows() )
00031     , _samples(DimensionGet())
00032   {
00033     // check if inputs are consistent
00034     assert (center.rows() == width.rows());
00035     _Lower = center - width/2.0;
00036     _Higher = center + width/2.0;
00037     _Height = 1;
00038     for (int i=1 ; i < width.rows()+1 ; i++ )
00039     {
00040         _Height = _Height / width(i);
00041     }
00042   }
00043 
00044   Uniform::Uniform (int dimension)
00045     : Pdf<ColumnVector>(dimension)
00046     , _samples(dimension)
00047   {
00048     _Lower.resize(dimension);
00049     _Higher.resize(dimension);
00050   }
00051 
00052   Uniform::~Uniform(){}
00053 
00054   std::ostream& operator<< (std::ostream& os, const Uniform& u)
00055   {
00056     os << "\nCenter: \n"    << u.CenterGet()
00057        << "\nWidth: \n" << u.WidthGet() << endl;
00058     return os;
00059   }
00060 
00061   //Clone function
00062   Uniform* Uniform::Clone() const
00063   {
00064       return new Uniform(*this);
00065   }
00066 
00067   Probability Uniform::ProbabilityGet(const ColumnVector& input) const
00068   {
00069     // test if input is located in area of Uniform distribution
00070     for (int i=1; i<input.rows()+1; i++)
00071     {
00072         if ( ( input(i)>_Higher(i) ) || ( input(i) < _Lower(i) ) ) return 0;
00073     }
00074     return _Height;
00075   }
00076 
00077   bool
00078   Uniform::SampleFrom (vector<Sample<ColumnVector> >& list_samples, const int num_samples, int method, void * args) const
00079   {
00080     // Perform memory allocation
00081     list_samples.resize(num_samples); // will break real-timeness if list_samples.size()!=num_samples
00082     vector<Sample<ColumnVector> >::iterator rit = list_samples.begin();
00083     switch(method)
00084       {
00085          case DEFAULT:
00086          {
00087                   while (rit != list_samples.end())
00088                    {
00089                  for (unsigned int j=1; j < DimensionGet()+1; j++) _samples(j) = runif(_Lower(j) , _Higher(j) );
00090                      rit->ValueSet(_samples);
00091                      rit++;
00092                    }
00093                   return true;
00094           }
00095           default:
00096              return false;
00097        }
00098   }
00099 
00100   bool
00101   Uniform::SampleFrom (Sample<ColumnVector>& one_sample, int method, void * args) const
00102   {
00103     switch(method)
00104     {
00105       case DEFAULT:
00106       {
00107          for (unsigned int j=1; j < DimensionGet()+1; j++) _samples(j) = runif(_Lower(j) , _Higher(j) );
00108              one_sample.ValueSet(_samples);
00109              return true;
00110       }
00111       default:
00112         return false;
00113       }
00114   }
00115 
00116   ColumnVector
00117   Uniform::CenterGet (  ) const
00118   {
00119     return (_Higher+_Lower)/2.0;
00120   }
00121 
00122   ColumnVector
00123   Uniform::WidthGet () const
00124   {
00125     return (_Higher-_Lower);
00126   }
00127 
00128   void
00129   Uniform::UniformSet (const ColumnVector& center,const ColumnVector& width)
00130   {
00131     assert(center.rows() == width.rows());
00132     _Lower = center - width/2.0;
00133     _Higher = center + width/2.0;
00134     _Height = 1;
00135     for (int i=1 ; i < width.rows()+1 ; i++ )
00136     {
00137         _Height = _Height / width(i);
00138     }
00139     if (this->DimensionGet() == 0) this->DimensionSet(center.rows());
00140     assert(this->DimensionGet() == center.rows());
00141   }
00142 
00143 
00144 } // End namespace BFL


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 Fri Aug 28 2015 10:10:21