jpasini
06-21-2008, 06:15 AM
Hi,
The possibility of using functors with many of the NR3 routines is one of the features I love of this new edition. However, I ran into trouble in a particular case, when using the Stiel class to obtain abscissas and weights for Gaussian quadrature. The compiler cannot find an appropriate constructor that takes a functor instead of a function. Is there a way of doing this without changing the Stiel class?
Below you'll find the full error message followed by the full test code. If I replace the functor with a normal function it compiles and works fine.
Thanks in advance,
Jose Miguel Pasini
Error message:
stiel_test.cpp: In function ‘void gauss_hermite(Int, Doub, Int)’:
stiel_test.cpp:32: error: no matching function for call to ‘Stiel::Stiel(const Int&, Doub&, Doub&, ShiftedGaussian&, Doub (&)(Doub), Doub (&)(Doub))’
../NR_C301/code/stiel.h:69: note: candidates are: Stiel::Stiel(Int, Doub, Doub, Doub (*)(Doub), Doub (*)(Doub), Doub (*)(Doub))
../NR_C301/code/stiel.h:61: note: Stiel::Stiel(Int, Doub, Doub, Doub, Doub (*)(Doub, Doub))
../NR_C301/code/stiel.h:1: note: Stiel::Stiel(const Stiel&)
make: *** [stiel_test] Error 1
Full test code:
#include "nr3.h"
#include "sort.h"
#include "quadrature.h"
#include "derule.h"
#include "eigen_sym.h"
#include "gauss_wgts2.h"
#include "stiel.h"
Doub map_semiinf_x(const Doub t)
{
return exp(t-exp(-t));
}
Doub map_semiinf_dxdt(const Doub t)
{
return exp(t-exp(-t))*(1.0 + exp(-t));
}
struct ShiftedGaussian {
Doub x0; // position of peak
ShiftedGaussian(const Doub xx0) : x0(xx0) { }
Doub operator()(const Doub x)
{
return exp(-(x-x0)*(x-x0));
}
};
void gauss_hermite(const Int npoints, const Doub x0, const Int prec)
{
Doub a = -6.0, b = 6.0;
ShiftedGaussian sg(x0);
Stiel gauss(npoints,a,b,sg,map_semiinf_x,map_semiinf_dxd t);
VecDoub x(npoints), w(npoints);
gauss.get_weights(x,w);
}
int main(int argc, char *argv[])
{
gauss_hermite(4,2.0,5);
return 0;
}
The possibility of using functors with many of the NR3 routines is one of the features I love of this new edition. However, I ran into trouble in a particular case, when using the Stiel class to obtain abscissas and weights for Gaussian quadrature. The compiler cannot find an appropriate constructor that takes a functor instead of a function. Is there a way of doing this without changing the Stiel class?
Below you'll find the full error message followed by the full test code. If I replace the functor with a normal function it compiles and works fine.
Thanks in advance,
Jose Miguel Pasini
Error message:
stiel_test.cpp: In function ‘void gauss_hermite(Int, Doub, Int)’:
stiel_test.cpp:32: error: no matching function for call to ‘Stiel::Stiel(const Int&, Doub&, Doub&, ShiftedGaussian&, Doub (&)(Doub), Doub (&)(Doub))’
../NR_C301/code/stiel.h:69: note: candidates are: Stiel::Stiel(Int, Doub, Doub, Doub (*)(Doub), Doub (*)(Doub), Doub (*)(Doub))
../NR_C301/code/stiel.h:61: note: Stiel::Stiel(Int, Doub, Doub, Doub, Doub (*)(Doub, Doub))
../NR_C301/code/stiel.h:1: note: Stiel::Stiel(const Stiel&)
make: *** [stiel_test] Error 1
Full test code:
#include "nr3.h"
#include "sort.h"
#include "quadrature.h"
#include "derule.h"
#include "eigen_sym.h"
#include "gauss_wgts2.h"
#include "stiel.h"
Doub map_semiinf_x(const Doub t)
{
return exp(t-exp(-t));
}
Doub map_semiinf_dxdt(const Doub t)
{
return exp(t-exp(-t))*(1.0 + exp(-t));
}
struct ShiftedGaussian {
Doub x0; // position of peak
ShiftedGaussian(const Doub xx0) : x0(xx0) { }
Doub operator()(const Doub x)
{
return exp(-(x-x0)*(x-x0));
}
};
void gauss_hermite(const Int npoints, const Doub x0, const Int prec)
{
Doub a = -6.0, b = 6.0;
ShiftedGaussian sg(x0);
Stiel gauss(npoints,a,b,sg,map_semiinf_x,map_semiinf_dxd t);
VecDoub x(npoints), w(npoints);
gauss.get_weights(x,w);
}
int main(int argc, char *argv[])
{
gauss_hermite(4,2.0,5);
return 0;
}