View previous topic :: View next topic |
Author |
Message |
[n00b@localhost] Apprentice
Joined: 30 Aug 2004 Posts: 266 Location: London, UK
|
Posted: Tue May 21, 2013 3:09 pm Post subject: C++ templates |
|
|
I have been asked to convert some Matlab code into C++ to increase its performance but am having trouble redesigning it into some sort of object oriented system. I have a background in Java and C programming and object oriented system design but have never done any (serious) programming in C++.
The existing code performs an MCMC simulation with some data and parameters. Each parameter has a name, value and prior probability distribution. The value of each parameter is updated with a sample from the prior probability distribution and the combined likelihood of all parameters is calculated using the pdf of each distribution. The prior distribution for each parameter may have different parameters or be of a completely different type altogether. In order to save coding time I would like to reuse the probability distributions that are part of the C++11 standard. The problem I have is that the probability distributions do not belong to any common hierarchy and, as such, I cannot simply create an array or vector of different distributions.
My attempt at a solution was to create a parameter base class and have a templated derived class but this just shifts the problem elsewhere.
Code: |
template <typename real = double>
class parameter {
public:
typedef real value_type;
parameter(const std::string & name, real value) : _name(name), _value(value) {}
virtual ~parameter() {}
const std::string & name() const { return _name; }
real value() const { return _value; }
void setValue(real value) { _value = value; }
private:
std::string _name;
real _value;
};
template <class Prior, typename real = double>
class prior_parameter : public parameter<real> {
public:
typedef Prior prior_type;
prior_parameter(const std::string & name, real value, const Prior & prior) : parameter<real>(name, value), _prior(prior) {}
template <class RNG>
void update(RNG & r) { setValue(_prior(r)); }
real probability() { return pdf(_prior, parameter<real>::value()); }
private:
Prior _prior;
};
|
With this solution I can create a std::vector<parameter *> but when iterating through them the update function is unavailable as it is defined in the subclass. Moving the declaration to the superclass and marking it virtual is not an option as it is a templated function.
I'm running out of ideas here. Is there a solution to this? |
|
Back to top |
|
|
nw1 n00b
Joined: 06 Aug 2011 Posts: 12
|
Posted: Wed May 22, 2013 12:10 pm Post subject: |
|
|
How about an RNGBase class with virtual functions, then derive RNGWrapper template from that to hold the RNG?
That way, update can always take an RNGBase& argument, and so become a virtual function in the parameter class, and be overridden in the prior_parameter class. |
|
Back to top |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|