There are two problems here.

One is that you did not translate my code properly.  You should have used
i2=. (j*n)+n-(1+k)

The other was that my code can't work right.  To illustrate this:

   C.,|.|:i.3 3
+-+-------+-------+
|4|7 3 1 5|8 6 0 2|
+-+-------+-------+

Since we have length 4 cycles in this example, simple swaps can't do
the job properly.

To fix this, I think the easiest approach would be to use two loops,
one to do the transpose and the other to do the flip.

Do you feel comfortable doing that, or should I try my hand at this again?

Thanks,

-- 
Raul

On Mon, Jan 28, 2013 at 3:10 PM, Brian Schott <[email protected]> wrote:
> Raul,
>
> If I have translated your code correctly, then I don't think it
> produces the correct results.
>
>    rtf =: monad define  NB. Raul's transpose flip
> n=. %:#y
> r=. y
> for_j. i. n do.
> for_k. i. n do.
> i1=. j+n*k
> i2=. j*n-(1+k)
> if. i1 < i2 do.
>         t=.i1{r
>         r=.(i2{r) i1}r
>         r=.t i2}r
>         end.
> end.
> end.
> r
> )
>    rtf i. 9
> 0 2 4 3 1 5 6 7 8
>
> By efficiency I mean that minimizing swapping stuff. But that may not
> be a good efficiency, I suppose.
>
>
> On Mon, Jan 28, 2013 at 2:41 PM, Raul Miller <[email protected]> wrote:
>> On Mon, Jan 28, 2013 at 2:34 PM, Brian Schott <[email protected]> wrote:
>>> I realize there are approaches like Raul and Devon are proposing that
>>> use 2D array indexing and require two array resequencing, but I was
>>> hoping to do both steps at once arithmetically to resequence the
>>> single index list to achieve efficiency.
>>
>> What do you mean by efficiency?
>>
>> Time?
>>
>> Space?
>>
>> Code?
>>
>> Forum? (But I'll stop trying to move this to chat, I guess...)
>>
>> Anyways, here's an in-place implementation:
>>
>> int * whatever(int n, int * r) {
>>         for (int j= 0; j < n; j++)
>>                 for (int k= 0; k < n; k++) {
>>                         int i1= j+n*k;
>>                         int i2= j*n-(1+k);
>>                         if (i1 < i2) {
>>                                 int t= r[i1];
>>                                 r[i1]= r[i2];
>>                                 r[i2]= t;
>>                         }
>>                 }
>>         return r;
>> }
>>
>> --
>> Raul
>
> --
> (B=)
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to