February 22nd, 2007, 7:25 pm
My main difficulty is that I can't find a good way to "structure" the software.The software consists in a library for the simulation of stochastic differential equations according to some numerical schemes.The implementation has to be efficient (we are going do do benchmark comparisons on them) so the use of templates to avoid function pointers.Don't be too harsh on me, as it's my first "real life" problem that I'm trying to solve using C++. We want to be able to write at the console:./nsde.exe -m ou -mp 2 -s euler -sp 0 10 0.1 -f maximum_bb -n 1000here:-m model (ou: dXt = -cXtdt + dBt)-mp model parameters (c = 2)-s scheme: here euler-sp scheme parameters (here starting point x0=0, ending time T, delta of discretization 0.1)-f functional used (here I calculate the maximum of the path using Brownian Bridges interpolation)-n number of simulations (here 1000)The program should consider the ou model, then for 1000 times simulate the path with the euler scheme, compute the value of the functional of the path and store the result on a fileMy idea is to define a class FOR EACH (parametric) diffusion model, like:class ou { double c;public: double drift(double x) {return -c*x}; double diffu(double x) {return 1}; //diffusion coefficient .....};(not writing everything, like constructors for simplicity.....)and a class for the path of the process:class path {public: velarray<double> values(maxsize); velarray<double> times(maxsize); static long maxsize; long size; //actual lenght of the path double x0, delta, T; //starting point , delta of discretization, ending time} ;and call the numerical scheme withtypedef double (D::*pmfD)(double)template<class D, pmfD drift, pmfD diffu>void euler(path& thepath, const D& thed, double x0, double delta, double T>I have to decide if it is a good idea to use this class path, which restrict the reusability of the schemes...The point is that some functional of the path requires a path with constant spacing (come schemes returns values in this constant spacing augmented with other values betweenthis constant spacing). So I could define another class pathconst and define conversion via copy assignment.a functional is for example:double maximum_bb(path thepath) ;I will also include a class named custom with pointer to functions, and a template specialization for it, to allow for run-time defined processes to be taken in consideration.There are some "problems" with this approach:A) as you add more schemes you should add more function and precomputed coefficients in each diffusion process class (like ou). A scheme for example uses the function drift' + drift^2 (and for efficency reasons I will compute this function and plug in directly the simplified form, with also precomputed coefficients). I'm just searching for a more elegant approach (I fear the class to become a burden....).Should I add friends classes objects (with the precomputed coefficients as private data) to better organize the class?(for precomputed coefficients I mean for example something of the form c1 = exp(c^2 + c/3); )For example I could define a classclass ou_milstein { double func1(double x) {return ...} ; .....}and make it friend of class ou.A pro is that for some numerical scheme we need to perform some intialization / optimization procedure, that could be performed at the moment of creation of the obj of type ou_milstein. The cons are that:1. I have to put all the needed function for a given scheme in its own class (friend of ou), like ou_milestein for milstein. That would result in a lot of functions (like the drift) being duplicated....2. OR I could just create "some" classes for groups of numerical schemes needing the same functions. But then I would need to pass more than one class to each numerical scheme, so the calling syntax of the scheme risks becoming quite complicated.....OR I just avoid all this and put everything together in the class ou.....B) There are schemes that apply only to a particular diffusion process (like ou).In this situation my idea is to just define this schemes as friends of the class (for the other schemes this is not needed, they just have to access the functions (defined for them, see point A) in the public part of the ou class) to avoid putting the required functions in the public part of the class ou (again, to avoid burden).C) How to easily write the interface part of the test-program for the library that read:-m ou -mp 2 -s euler -sp 0 10 0.1 -f maximum_bb -n 1000and performs the simulation avoiding hundred of switch-cases ?At the end of MarkJoshi I see there is "the factory pattern" for similar situations, but I have not studied it (yet). Do you think it is appropriate?Do you have better ideas on how could I implement this library?Do you already see problems I can't see?I would like to have something that is efficient, but reusable and extendable if needed (not easy problem to solve, I know....).Someone I saw mentioned "the visitor pattern", could it help me out?Again, thank you VERY much in advance for your help!Best RegardsStephQ