As you noted, John Randall's }: > }. is of interest. 2 >/\ ] is a natural J/APL2 way to do it, whereas }: > }. is the J form of an ancient APL1 idiom for first differences and the like, and seems faster for smallish d, but loses out for your required
size of vector for space requirement. However,  the performance
is pretty good with either idiom.

  ts =: 6!:2 , 7!:[EMAIL PROTECTED]  NB. time & space

  D=:10000$d     NB. "old" method faster
  ts'((1,2>/\]);@(<&(<./\);. 1)])D '
0.000238578 85248
  ts'((1,}:>}.);@(<&(<./\);. 1)])D '
0.000155048 150464

  D=:50000$d     NB. both similar for time
  ts'((1,2>/\]);@(<&(<./\);. 1)])D '
0.00151164 331008
  ts'((1,}:>}.);@(<&(<./\);. 1)])D '
0.00151807 592832

  D=:100000$d    NB. "new" method faster
  ts'((1,2>/\]);@(<&(<./\);. 1)])D NB. tacit form'
0.00302441 658688
  ts'((1,}:>}.);@(<&(<./\);. 1)])D NB. tacit form'
0.00445364 1.18266e6
Mike


Mikl Kurkov wrote:

On Fri, 24 Mar 2006 02:43:00 +0300, Mike Day <[EMAIL PROTECTED]> wrote:

I was drafting this before Oleg's reply arrived.  It's slightly
different from his, and shows a derivation of a tacit form.
==============================================================
Is this the sort of thing you need? Consider:

Yes, exactly.

   d
10 7 5 6 7 1 9 8 2 4
   (1 , 2 >/\ ])d NB. boolean for = left > right for each pair ?
1 1 1 0 0 1 0 1 1 0

   1 1 1 0 0 1 0 1 1 0 < ;. 1 d  NB. partition d by this boolean
+--+-+-----+---+-+---+
|10|7|5 6 7|1 9|8|2 4|
+--+-+-----+---+-+---+
   1 1 1 0 0 1 0 1 1 0(<&(<./\);. 1)d NB. required work in boxes
+--+-+-----+---+-+---+
|10|7|5 5 5|1 1|8|2 2|
+--+-+-----+---+-+---+

   1 1 1 0 0 1 0 1 1 0 ;@(<&(<./\);. 1)d NB. unbox
10 7 5 5 5 1 1 8 2 2

   ((1,2>/\]);@(<&(<./\);. 1)])d NB. tacit form
10 7 5 5 5 1 1 8 2 2

(;@(<&(<./\);. 1)~1,2>/\])d NB. tacit avoiding one "]" 10 7 5 5 5 1 1 8 2 2

Mike


Thanks for explanations. Your version seems to be most efficient.

----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to