Blitz logo

Blitz Support :

From: Frank Schimmel (f.schimmel_at_[hidden])
Date: 2005-05-31 05:59:10


>>>>> daniel egloff writes:

Hi Daniel!

> how can I make the following loop dimension independent, e.g. the
> dimension 4 should be variable, but compile time fixed.
> [...]

The number of dimensions of a blitz array must be a compile-time
constant. If that constant varies from compilation to compilation,
writing nested loops is not possible. (And besides: they don't perform
very well.) You could try to use an iterator instead (see below).

If that does not work, you can wrap you iteration in a function and
then you can have different versions of that function acting on you
arrays of different dimensionality using partial template
specialisation:

template<int dimension> class Helper;

template<>
class Helper<4> {
public:
  static const int dimension = 4;
  doStuff(blitz::Array<double,4> &array) {
    ...
  }
};

I'm not sure the syntax is all correct, though.
The kludge with the class is necessary, because partial template
specialisation exists for member functions only. :-/

You could also try to avoid the kludgy part using overloading to
emulate partial specialisation. That requires some deeper thinking,
tough. I'd reccomend the book "Modern C++ Design" by Andrei
Alexandrescu (http://www.moderncppdesign.com/book/main.html), which is
where I learned avout that trick. It's all implemented in the Loki C++
Library: http://sourceforge.net/projects/loki-lib/

> a related question: how can I make the following loop dimension
> independent:
> [code ...]
> // apply a function to every array element,
> [more code ...]

Have a look at the Array::iterator (typedef for ArrayIterator
instantiation).

for(blitz::Array::iterator i = array.begin(), e = array.end();
    i != e; ++i) {
  f(i.position(), *i);
}

The position() method gives the index of the element as a TinyVector.

> I think the second problem is related to functors. Whats about the
> first?
> Any help greatly appreciated.

Hope that does help
-Frank

PS: I hope your actual code is indented properly.
PPS: Please suggest to your legal department to shorten that disclaimer.