Off by 1 error.
The inner expression should be:
r[j+n*k]= j*n+(n-1)-k;
The parenthesis can be omitted.
--
Raul
On Mon, Jan 28, 2013 at 1:40 PM, Raul Miller <[email protected]> wrote:
> P.S. here's an implementation of the index generating scheme you described:
>
> #include <stdlib.h>
> int * reverseOfTranspose(int n) {
> int * r= malloc(n*n*sizeof n);
> if (!r) return r;
> for (int j= 0; j < n; j++)
> for (int k= 0; k < n; k++)
> r[j+n*k]= j*n+n-k;
> return r;
> }
>
> I used index arithmetic instead of multi-dimensional indices because
> that was easier.
>
> I hope that mail does not mess up my indentation.
>
> It's not tested, so I might have made a mistake, but I think this
> should at least be close to what you want.
>
> Nothing here is J programming, so I've sent this to chat.
>
> FYI,
>
> --
> Raul
>
> On Mon, Jan 28, 2013 at 1:30 PM, Raul Miller <[email protected]> wrote:
>> The C approach to doing transpose typically involves swapping the
>> indices when they are used, and not bothering to make a fresh copy (or
>> updated copy) of the data.
>>
>> This of course yields a fair bit of complexity which gets pushed into
>> the code (or into the objects) but that's how it's usually done.
>>
>> That said, you can achieve the same thing by explicitly copying the
>> matrix using swapped indices. In a loop; b[j][k]= a[k][j]
>>
>> Or am I overlooking something important?
>>
>> (Also, perhaps this thread should be in chat?)
>>
>> Thanks,
>>
>> --
>> Raul
>>
>> On Mon, Jan 28, 2013 at 12:58 PM, Brian Schott <[email protected]>
>> wrote:
>>> I am trying to use objective C and am using j to develop some
>>> pieces. I have the verb `transposeSquare` which transposes a square matrix's
>>> indices using indices and reproduces j's Transpose. In C code the indices
>>> are named ii and are being generated in a for loop as follows, where width
>>> and height are actually both 13 but my j examples use 3 instead of 13.
>>>
>>> for (int ii = 0 ; ii < width * height ; ++ii)
>>>
>>> transposeSquare =: (] <.@% %:@#)+#| %:@# *]
>>> transposeSquare i. 9
>>> 0 3 6 1 4 7 2 5 8
>>> ,|:i. 3 3 NB. this is the "J way"
>>> 0 3 6 1 4 7 2 5 8
>>>
>>> Now with C code for loop in mind I need a verb which first does what
>>> transposeSquare does and then reverses the indices using the operators
>>> available to C so that the result of the verb would be as follows.
>>>
>>> ,|.|:i. 3 3 NB. this is the "J way"
>>> 2 5 8 1 4 7 0 3 6
>>>
>>> That is, I need a verb (say, reverseOfTranspose, rOT), which gives
>>> the same result as ,|.|:i. 3 3 but with the scalar argument 3 and is limited
>>> to C operators.
>>>
>>> (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