![]() |
Blitz Support : |
From: Julian C. Cummings (cummings_at_[hidden])
Date: 2004-08-24 03:33:44
Hello Marcel,
Here is one way to produce the circular shift:
vv = (tensor::i+2) % v.rows() + 1;
You cannot index an Array with an Array expression, which is why your
original attempt did not compile. If you really need to shift the values in
Array v, you could do it in two steps like this:
const int shift = 3;
const int N = v.rows();
vv(Range(0,shift-1)) = v(Range(N-shift,N-1));
vv(Range(shift,N-1)) = v(Range(0,N-1-shift));
It's a bit awkward because there is no modulo operator defined for the Range
object. Applying the modulo operation divides the Range into two valid
Ranges.
Hope this helps,
Julian C.
Dr. Julian C. Cummings
Staff Scientist, CACR/Caltech
(626) 395-2543
cummings_at_[hidden]
> -----Original Message-----
> From: blitz-support-bounces_at_[hidden]
> [mailto:blitz-support-bounces_at_[hidden]] On Behalf Of
> Marcel Loose
> Sent: Friday, August 13, 2004 3:38 AM
> To: blitz-support_at_[hidden]
> Subject: [Blitz-support] [Q] Circular shift of Array elements?
>
>
> Hi,
>
> I was wondering if there is a concise way to do a circular
> shift on the contents of an Array (1D, 2D, etc). For example,
> suppose I have the following 1D Array:
>
> Array<int, 1> v(5);
> v = tensor::i + 1;
>
> which looks like this (obviously):
>
> [ 1 2 3 4 5 ]
>
> Now, I would like to do a circular right shift of 3 on the
> elements of v and store the results in vv. Vector vv should
> then contain:
>
> [ 3 4 5 1 2 ]
>
> I tried to do it as follows:
>
> Array<int, 1> vv(v.copy());
> vv = v((tensor::i+3)%v.rows());
>
> However, this will not compile. The compiler (g++ 3.2.2)
> diagnostics start with:
>
> no match for call to `(blitz::Array<int, 1>) (
>
> blitz::_bz_ArrayExpr<blitz::_bz_ArrayExprOp<blitz::_bz_ArrayEx
> pr<blitz::_bz_A
> rrayExprOp<blitz::IndexPlaceholder<0>,
> blitz::_bz_ArrayExprConstant<int>, blitz::Add<int, int> > >,
> blitz::_bz_ArrayExprConstant<int>, blitz::Modulo<int, int> > >)'
>
> Is there a way to accomplish a circular shift (also in 2D),
> other than using a for-loop or swap ranges?? And if not, what
> is the best way to do this?
>
> Kind regards,
>
> Marcel Loose
>
> _______________________________________________
> Blitz-support mailing list
> Blitz-support_at_[hidden]
> http://www.oonumerics.org/mailman/listinfo.cgi/blitz-support
>