Blitz logo

Blitz Support :

From: Derrick Bass (derrick_at_[hidden])
Date: 2003-05-31 08:15:06


Markus,
A few things. First of all "dot" is not a function that blitz
generalizes to work on array expressions, like it does for "sin" so on.
It probably should, but at the moment it does not. If you are only
going to use Vec's, then you can generalize it using the supplied
BZ_DECLARE_FUNCTION family of macros; they are supposed to be used for
user defined functions, but they work just fine on dot functions also.
If you will be working with multiple types, you'll need to define a
function for each type:
template<int N>
inline dot_float(TinyVector<float, N> a, TinyVector<float, N> b) {
        return dot(a,b);
}
and use a separate BZ_DECLARE_FUNCTION for each type. Or, you can delve
into Blitz's machinery and create a scary template function that covers
all possible cases and then submit it for inclusion in Blitz ;-)

As for getting the sum to work, make sure to include
<blitz/tinyvec-et.h>. Regarding the calculation of the sum twice, you
could hope that the compiler realizes there is a common subexpression
and optimizes properly. Or you can define a function that computes the
squared magnitude of a vector and then use the BZ_DECLARE_FUNCTION
machinery.

The code below compiles for me.

Derrick

#include <blitz/array.h>
#include <blitz/tinyvec-et.h>

typedef blitz::TinyVector<double, 3> Vec;
typedef blitz::Array<Vec, 1> VArray;
typedef blitz::Array<double, 1> SArray;

using namespace blitz;

BZ_DECLARE_FUNCTION2_RET(dot,double);

template<class T, int N>
inline T sumsqr(TinyVector<T,N> a) {
   return sum(sqr(a));
}

BZ_DECLARE_FUNCTION_RET(sumsqr, double);

int main() {
   VArray v1(10), v2(10);
   SArray s(10);

   s = dot(v1, v2);

   Range r(0,5);
   double q = sum( s(r) * dot(v1(r)+v2(r), v1(r)+v2(r)) );
   double q2 = sum( s(r) * sumsqr( v1(r) + v2(r) ) );
}