Boost logo

Boost Users :

From: David Abrahams (dave_at_[hidden])
Date: 2007-11-18 10:40:25


on Sun Nov 18 2007, StephQ <pelux-AT-ngi.it> wrote:

> I am currently working on a mathematical library for the simulation of
> stochastic processes. I am a researcher in Statistics, hence my limited skills
> in c++ programming :)
>
> Let me introduce the framework briefly.
> The part of the library I am interested with is very similar to the boost random
> library. I need to implement it because I need to extend some functionality and
> I am not confident in modifying the boost random library to extend its behaviour
> (too complicated for me :P).
>
> We have (lazy syntax):
>
> PseudoRng<TImpl> //A particular pseduo random number generator
> QuasiRng<TImpl> // A particualr quasi random number generator
> GaussianDistribution // A particular statistical distribution
>
> Sampler<GaussianDistribution> // The interface (for other components of the
> library) of a sampler of random numbers distributed as a GaussianDistribution
>
> Then I consider:
>
> template <
> class TGenerator,
> class TDistribution,
> typename TDistribution::variate_t (TDistribution::*algorithm)( const
> TGenerator& ) const = &TDistribution::sample_default >
>
> class VariateSampler : public Sampler<TDistribution>
> {
> ...
> }
>
> Here TGenerator can be PseudoRng<TImpl> or QuasiRng<TImpl>,
> TDistribution can be GaussianDistribution and the pointer to member
> function algorithm select the algorithm used to sample.
>
> While constructing VariateSampler I would like to:
> 1) Automatically deduce the type of TGenerator and TDistribution

>From what information and by what rules?

> 2) Automatically select a different default for algorithm, for particular
> TDistribution (like GaussianDistribution,PoissonDistribution....) depending on
> the fact that TGenerator is of type PseudoRng<TImpl> or QuasiRng<Timpl>. So for
> every TDistribution two different defaults for algorithm.
>
> I think that boost parameter should solve my problem, but I am quite confused
> about how to implement it, expecially the second point. Could you help me with
> the basic steps I need to consider?

Because Boost.Parameter is geared towards template type parameters, it
might be easier to approach this problem by starting with a different
interface for VariateSampler. Let's say:

  template <
    class TGenerator,
    class TDistribution,
    class Algorithm
>
  struct _VariateSampler : Sampler<TDistribution>
  {

  };

where Algorithm is required to be a function (object) callable with
TDistribution and TGenerator arguments. You can easily map it onto
the default you've specified using:

    template <class TDistribution, class TGenerator>
    struct default_algorithm
    {
       typename TDistribution::variate_t
       operator()(TDistribution const& d, TGenerator const& g) const
       {
           return d->sample_default(g);
       }
    };

Default selection might be done through a simple metafunction:

   template <class T> struct is_PseudoRng;

   template <class U> struct is_PseudoRng<PseudoRng<U> >
   : boost::mpl::true_ {};

   ...
   mpl::if_<is_PseudoRng<some_generator>, default1, default2>::type
   ...

HTH,

-- 
Dave Abrahams
Boost Consulting
http://www.boost-consulting.com

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net