From: Patrik J. <pa...@uc...> - 2007-10-17 22:26:39
|
Hi all, I need to take an array expression and shrink it by replacing every 2 (or 4, 8, etc) numbers with the mean of those numbers. Thus (1,2,3,4) -> (1.5, 3.5). This happens often and must be as efficient as possible, so I want to do it without temporaries, but I'm having some trouble figuring out how to do this. One way I thought was to make a function: template <typename T> <return type> shrink(const blitz::_bz_ArrayExpr<T> a) { return (a(2*blitz::tensor::i)+a(2*blitz::tensor::i+1)*0.5); }; so you can do shrunken_array =3D shrink(<expr>) but this doesn't work. It seems you can't use the tensor notation to index with stride? I thought it would work because the indices are evaluated on the left-hand side, so if that array is length N and the expression is length 2N, then it should work out fine. Another option would be to do something like: template <typename T> <return type> shrink(const blitz::_bz_ArrayExpr<T> a) { Range r(0, a.size()-2, 2); return (a(r)+a(r+1)*0.5); }; but that doesn't seem to work because _bz_ArrayExpr's don't have a size() member, and even if they did, you don't seem to be able to apply the Range to an expression. Then I thought I would try to use a StridedDomain<1> instead of the Range, but that fares no better. Can anyone offer a suggestion how to accomplish this? Thanks, /Patrik |