OON: Re: integration, finding roots - design question

From: Evgenii Rudnyi (rudnyi@td.chem.msu.su)
Date: Thu Mar 25 1999 - 09:20:14 EST


> I have a question about the way, how to use e.g. integration algorithms
>in C++. In C it was very easy. You could define global function, which
>took as an argument pointer to the functio and some additional stuff. In
>C++ it cannot be done this way (apart from simple cases, when the function
>you want to integrate or which roots you wanna find is global function).
>Your functions are usually methods, co you have to do it another way. I
>found 3 different solutions, and would like to know your opinions and
>suggestions about them

Why don't you want to employ callback library? To this end, I use

C++ Template Callback Library
Copyright 1994 Rich Hickey

It is icluded in my TDLIB-library (http://www.chem.msu.su/~rudnyi/tdlib/)
and for your convience I put it alone at our server
(http://td.chem.msu.su/tmp/callback.zip about 10 Kb). Or you can try to find
it somewhere else in the Internet (I do not remember where I have found it).

The resultant code looks like as follows

typedef CBFunctor1wRet<double, double> D2D;

double f(double x)
    {return exp(-x*x);}

class tt
{
  double c;
public:
  tt(double c_) : c(c_) {}

double f(double x)
    {return exp(-x*x*c);}
};

int main()
{
  double a = 0.;
  double b = 1.;
  double abserr, resabs, resasc;
  double pi = 4.*atan(1.);
  cout << qk15(makeFunctor((D2D*)0, f), a, b, abserr, resabs, resasc) <<
endl;
  tt test(2.);
  cout << qk15(makeFunctor((D2D*)0, test, &tt::f), a, b, abserr, resabs,
resasc) << endl;
}

Evgenii Rudnyi

--
Chemistry Department     rudnyi@comp.chem.msu.su
Moscow State University  http://www.chem.msu.su/~rudnyi/
119899 Moscow            +7(095)939 5452, fax+7(095)932 8846,+7(095)939 1205
Russia
***************************************************************************
>From Kurt Vonnegut "Address to the Americal Physical Society"

Back in my days as a chemistry student I used to be quite a technocrat. ... I told them [my fraternity brothers at Cornell] that all power in the future would rest properly in the hands of chemists and physicists and engineers. The fraternity brothers knew more about the future and about the uses of power than I did. They are rich and they are powerful now. They all became lawyers.



This archive was generated by hypermail 2b29 : Wed Feb 20 2002 - 03:20:08 EST