Whoops, my bad with the copy/paste fingers. trimh was the 8th version (which
became trimalt!)
A bit more explanation.
trimalt=:{~ <@:(>:@:(3 * i.)@:<.@:(%&3)&.>)@:}:@:$ NB. original verb
Let's break it into parts
trimalta=:}:@:$
trimalta takes the first two dimensions of the array
trimalta i. 7 10 3
7 10
trimaltb=:(>:@:(3 * i.)@:<.@:(%&3)&.>)@:trimalta
trimaltb takes each of first two dimension, divides them by 3, rounds them down
to the nearest integer, generates the integers from 0 to that result,
multiplies by 3 and adds one, then each is boxed before they are combined
(avoiding the padding that would occur if the results were combined without
boxing)
trimaltb i. 7 10 3
┌───┬─────┐
│1 4│1 4 7│
└───┴─────┘
trimaltc=:<@: trimaltb
trimaltc just boxes the result from trimaltb so that it is ready to use as the
right argument of {~ (which is the same as the left argument of {)
trimaltc i. 7 10 3
┌───────────┐
│┌───┬─────┐│
││1 4│1 4 7││
│└───┴─────┘│
└───────────┘
trimaltd=:({~ trimaltc)
trimaltd is the hook (u v)x equivalent to x u v x but in this case (u~ v)x is
equivalent to (v x) u x where (v x) is trimaltc, x is the original matrix and u
is {
In other words ({~ trimaltc) i. 7 10 3 is the same as (trimaltc { ]) i.7 10 3
trimaltd i. 7 10 3
33 34 35
42 43 44
51 52 53
123 124 125
132 133 134
141 142 143
trimalt i. 7 10 3
33 34 35
42 43 44
51 52 53
123 124 125
132 133 134
141 142 143
100000 timespacex 'trimalt i. 7 10 3'
2.24551e_6 7040
trim1=: >@((2 2$3)&(cp;._3))@:(<"1)
100000 timespacex 'trim1 i. 7 10 3'
5.98121e_6 21632
(trim1 -: trimalt) i."1 _3[\ , >,&3 each {3&+@:i. each 15 ;15
1
I tried Henry's suggestion of cp;._3"2 (I think that you need to use _3 to
avoid shards) and I can get the right numbers (although in a different order
because of the change in the dimensions), but it does not seem quicker. I am
probably doing something wrong in my approach and look forward to having the
error in my approach discovered.
t2=:2 0 1 |: i. 7 10 3 NB. transpose to (3 r c)
0 3 6 9 12 15 18 21 24 27
tess cp ;._3"2 t2
33 42 51
123 132 141
34 43 52
124 133 142
35 44 53
125 134 143
1 2 0|: tess cp ;._3"2 t2 NB. transpose back to get equivalent result
33 34 35
42 43 44
51 52 53
123 124 125
132 133 134
141 142 143
100000 timespacex 'tess cp;._3"2 t2'
8.00083e_6 17024 NB. No transposing in or out
100000 timespacex 'trim1 t'
5.24875e_6 17152
100000 timespacex 'trimalt t'
1.64128e_6 2560
Cheers, bob
> On Jun 1, 2017, at 12:30 PM, Brian Schott <[email protected]> wrote:
>
> Rob,
>
> In my case your solution is even more efficient because my right argument
> is always the same shape, (well really there are 2 cases, and each case is
> internally shape invariant). So I can create a noun, say ndx, of that value
> and apply it as ndx&{ against the right argument. I had thought about such
> a solution, but could not come up with your right hand tine of the hook.
>
> Very cool.
>
>
> (I got a little lost in your answer though, because trimh was not defined
> and I couldn't tell where it fit. But I don't think that is relevant to
> your trimalt, which I was commenting on above.)
>
> To be honest I haven't completely grokked your right tine of trimalt, but
> it seems to work fine.
>
> Thanks,
>
> On Thu, Jun 1, 2017 at 2:30 PM, robert therriault <[email protected]>
> wrote:
>
>> Brian,
>>
>> I thought that as well, but I just finished a slightly different approach
>> where I went directly to the selection of the triples from the matrix. It
>> looks more complicated but is at least twice as fast and a third of the
>> space.
>>
>> trim1 =: >@(tess &(cp;._3))@:(<"1)
>> 100000 timespacex 'trim1 i. 7 10 3'
>> 5.78836e_6 21632
>>
>> trimalt=: {~ <@:(>:@:(3 * i.)@:<.@:(%&3)&.>)@:}:@:$ NB. Hook with
>> {~ the left tine
>> 100000 timespacex 'trimalt i. 7 10 3'
>> 2.41935e_6 7040
>>
>> 6.17228e_6 21632
>>
>> (trim1-:trimh) i."1 _3[\ , >,&3 each {3&+@:i. each 15 ;15 NB.
>> combinations from 3 3 3 to 15 15 3
>> 1
>>
>> For indices of less than 3 there is a difference in the shape produced, so
>> watch out for that boundary.
>>
>> $ trimalt i. 2 2 3
>> 0 0 3
>> $ trim1 i. 2 2 3
>> 0 0
>>
>> Cheers, bob
>>
>>
>>
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm