Next: 20.4 ns-random Up: 20. Mathematical Support Previous: 20.2 Random Variables

# 20.3 Integrals

The Integrator../ns-2/integrator.h supports the approximation of (continuous) integration by (discrete) sums; it is defined in integrator.h as

{\rm From integrator.h:}
class Integrator : public TclObject {
public:
Integrator();
void set(double x, double y);
void newPoint(double x, double y);
int command(int argc, const char*const* argv);
protected:
double lastx_;
double lasty_;
double sum_;
};

{\rm From integrator.cc:}
Integrator::Integrator() : lastx_(0.), lasty_(0.), sum_(0.)
{
bind("lastx_", &lastx_);
bind("lasty_", &lasty_);
bind("sum_", &sum_);
}

void Integrator::set(double x, double y)
{
lastx_ = x;
lasty_ = y;
sum_ = 0.;
}

void Integrator::newPoint(double x, double y)
{
sum_ += (x - lastx_) * lasty_;
lastx_ = x;
lasty_ = y;
}

int Integrator::command(int argc, const char*const* argv)
{
if (argc == 4) {
if (strcmp(argv[1], "newpoint") == 0) {
double x = atof(argv[2]);
double y = atof(argv[3]);
newPoint(x, y);
return (TCL_OK);
}
}
return (TclObject::command(argc, argv));
}

This class provides a base class used by other classes such as QueueMonitor that keep running sums. Each new element of the running sum is added by the [x, y]newPoint function. After the kth execution of newPoint, the running sum is equal to where x0 = y0 = 0 unless lastx
_
, lasty
_
, or sum
_
are reset via OTcl. Note that a new point in the sum can be added either by the C++ member newPoint or the OTcl member newpoint. The use of integrals to compute certain types of averages (e.g. mean queue lengths) is given in (pp. 429-430, [#!Jain91:Art!#]).

2000-08-24