previous up next print clean
Next: BUILDING BLOCKS Up: C++ in physical science: Previous: SOLVERS


To write an operator, one must write the constructors, which initialize the operator, the destructor (which only needs to be written if something is dynamically allocated) and a ``${\tt ::dodata()}$'' member function to do the actual calculation.

Here is the header file for the convolution operator class, which define the data members and member functions of the operator:

class floatConvolve : public floatopone {
    // Construct a floatConvolve Operator given a floatArray
    // filter, and an Axis to operate on
  floatConvolve(floatArray &,Axis &);
     void dodata(floatArray &,floatArray &,int);
     floatArray filter;

Note that the only new public function is the constructor, which takes an array of filter coefficients and an axis specification as parameters.

Here is a constructor for the convolution operator:

floatConvolve::floatConvolve(floatArray & f,Axis & a) {
	    inaxis = (Axislist) a;
	    outaxis = inaxis;
	    outaxis.list[0].length += (::rows(f) - 1);
	    filter = f;

Note that the output axis length is set to be shorter than the input axis length. This convolution operator truncates its output.

No destructor is necessary, because nothing is dynamically allocated, so everything is deallocated automatically.

Finally, the dodata routine, to do the calculation for the operator:

void floatConvolve::dodata(floatArray & x,floatArray & y,int conj) {
   for (int ib=0; ib<(::rows(filter)); ib++) {
      for (int ix=0; ix<inaxis.list[0].length; ix++) {
         if (conj) {
            x(ix) += y(ib+ix) * filter(ib);
         } else {
            y(ib+ix) += filter(ib) * x(ix);

The base class for operators is written so that the dodata function of a new operator is passed two arrays. One matches the axes specified by the ${\tt Axislist}$ object inaxis and the other matches the the axes specified by outaxis. The routine is also passed a flag that specifies whether the operator is to be applied in the forward or conjugate sense.

If there are more axes present in the input space the dodata routine will be called repeatedly, once for each subspace that matches the operator's axes. This means that a 1-D operator only requires code dealing with 1-D arrays, all the other dimensions are handled by the base operator class.

previous up next print clean
Next: BUILDING BLOCKS Up: C++ in physical science: Previous: SOLVERS
Stanford Exploration Project