Blitz logo

Blitz Support :

From: Scott, Steven (sls_at_[hidden])
Date: 2004-07-14 17:23:54


I'd like to develop an interface class that inherits from blitz::Array,
but which provides methods that my existing code expects. For example,

        double ans=0;
        vector x(1, 10);
        for(i=x.lo(); i<=x.hi(); ++i) ans+= h(x[i]); // h(double)
defined elsewhere

I'd like to do something like:

class vector : public blitz::Array<double, 1>{
// some constructors
// operator=()
    int lo(){return lbound(firstDim);}
    int hi(){return ubound(firstDim);}
    //... only a few other methods need to be defined
};

In theory, my old code would still work, I could continue to write code
using familiar syntax (e.g. lo() and hi()), but I could have Blitz do
the heavy lifting on my future code:

        vector x(10), y(10), z(10);
        x = z*y + y/cos(exp(z)); // or some such nonsense

THE PROBLEM:

I can't seem to define the correct constructors or operator=(). I'd
like to just pass the RHS of operator= to the blitz operator=. I don't
know how to do this, I think because the RHS is an Array Expression,
which I don't know how to manipulate.

My current attempt at a class definition, a sample program that breaks
it, and the compiler errors, are provided below.

Thanks for the help!

Steve

-----------------------------------------
namespace lin_alg{
  class vector : public blitz::Array<double, 1>{
  public:
    typedef blitz::Array<double, 1> vec_t;
    typedef blitz::Range Range;

    // constructors: call blitz constructors
    // empty
    vector(){}
    // construct from blitz vector
    vector(vec_t &x) : vec_t::Array(x) {}
    // copy
    vector(vector &x) : vec_t::Array( static_cast<vec_t&>(x)){}
    // extent parameters
    vector(int dim) : vec_t::Array(dim) {}
    // Range argument
    vector(Range r) : vec_t::Array(r) {}
    // from double data
    vector(double *dat, int len) :
      vec_t::Array(dat, blitz::shape(len), blitz::neverDeleteData) {}
 
    vector & operator=(const double &x){
       return vector(vec_t::operator=(x));}

    int lo(){return vec_t::lbound(blitz::firstDim);}
    int hi(){return vec_t::ubound(blitz::firstDim);}
    //... only a few other methods need to be defined

  };
}
------------------------------------------------
int main(){
lin_alg::vector x(4);
x=10;
}
-------------------------------------------
/home/sls/cpplib/lin_alg/vector.h: In member function `lin_alg::vector&
   lin_alg::vector::operator=(const double&)':
/home/sls/cpplib/lin_alg/vector.h:35: error: no matching function for
call to `
 
lin_alg::vector::vector(blitz::ListInitializationSwitch<blitz::Array<dou
ble,
   1>, double*>)'
/home/sls/cpplib/lin_alg/vector.h:31: error: candidates are:
   lin_alg::vector::vector(double*, int)
/home/sls/cpplib/lin_alg/vector.h:29: error:
   lin_alg::vector::vector(blitz::Range)
/home/sls/cpplib/lin_alg/vector.h:27: error:
   lin_alg::vector::vector(int)
/home/sls/cpplib/lin_alg/vector.h:25: error:
   lin_alg::vector::vector(steve::vector&)
/home/sls/cpplib/lin_alg/vector.h:23: error:
   lin_alg::vector::vector(blitz::Array<double, 1>&)
/home/sls/cpplib/lin_alg/vector.h:21: error:
   lin_alg::vector::vector()