Continuing f3:
f5=: [EMAIL PROTECTED] - (# +/\@(|.!.0))@(#;.1)@(1,2~:/\])

Another idea is

f7=: ;@:(<@([EMAIL PROTECTED]);.1) @ (1,2~:/\])

which beats f2 but not f3.  If the interpreter had the
same optimization for ;@:<@([EMAIL PROTECTED]);.n   that it has for
#;.n   , maybe f7 would be a contendah.

Students should note that ;@:(<@([EMAIL PROTECTED]);.1) is a lot
faster than ;@:(<@[EMAIL PROTECTED];.1) .

Henry Rich

> -----Original Message-----
> From: [EMAIL PROTECTED] 
> [mailto:[EMAIL PROTECTED] On Behalf Of Roger Hui
> Sent: Tuesday, July 31, 2007 11:28 AM
> To: Programming forum
> Subject: Re: RE: [Jprogramming] simple verb
> 
> Another alternative.  Actually an alternative for i.~ for this data
> whose speed relative to i.~ depends on the kind of data.
> 
> f2=: [EMAIL PROTECTED] - i.~
> f3=: [EMAIL PROTECTED] - (# +/[EMAIL PROTECTED]:@(0&,))@(#;.1)@(1,2~:/\])
> 
>    c=: 1e5 [EMAIL PROTECTED] 10
>    x=: c#i.#c
>    y=: 0.5 * x
> 
>    (f2 -: f3) x
> 1
>    (f2 -: f3) y
> 1
> 
>    ts=: 6!:2 , 7!:[EMAIL PROTECTED]
> 
>    ts 'f2 x'
> 0.0192337 6.29222e6
>    ts 'f3 x'
> 0.0444705 7.86509e6
> 
>    ts 'f2 y'
> 0.150214 6.29222e6
>    ts 'f3 y'
> 0.0593807 7.86509e6
> 
> #;.1@(1,2~:/\]) is an alternative to #/.~ which may be
> faster or slower depending on the kind of data.
> (Slower on "easy" data; faster on "hard" data.)
> 
>    (#/.~ -: #;.1@(1,2~:/\])) x
> 1
>    (#/.~ -: #;.1@(1,2~:/\])) y
> 1
>    ts '#/.~ x'
> 0.00797168 1.04941e6
>    ts '#;.1@(1,2~:/\]) x'
> 0.0135623 1.05043e6
>    ts '#/.~ y'
> 0.144353 6.29229e6
>    ts '#;.1@(1,2~:/\]) y'
> 0.0258773 1.05043e6
> 
> 
> 
> ----- Original Message -----
> From: Henry Rich <[EMAIL PROTECTED]>
> Date: Tuesday, July 31, 2007 6:24
> Subject: RE: [Jprogramming] simple verb
> To: 'Programming forum' <[email protected]>
> 
> > Another solution is
> > 
> >    ([EMAIL PROTECTED] - i.~) 4 4 4 2 2 1 3 3 3 3
> > 0 1 2 0 1 0 0 1 2 3 
> > 
> > JfC shows Roger's solution for scrambled inputs.
> > 
> > Henry Rich
> > 
> > > -----Original Message-----
> > > From: [EMAIL PROTECTED] 
> > > [mailto:[EMAIL PROTECTED] On Behalf Of R.E. Boss
> > > Sent: Tuesday, July 31, 2007 8:59 AM
> > > To: 'Programming forum'
> > > Subject: RE: [Jprogramming] simple verb
> > > 
> > > Alternative:
> > > 
> > >    (0:`+ @.[)/\.&.|. 0,2=/\4 4 4 2 2 1 3 3 3 3
> > > 0 1 2 0 1 0 0 1 2 3
> > > 
> > > Works alsof or scrambled:
> > > 
> > >    (0:`+ @.[)/\.&.|. 0,2=/\3 1 4 3 3 4 3 2 4 2
> > > 0 0 0 0 1 0 0 0 0 0
> > > 
> > > 
> > > R.E. Boss
> > > 
> > > 
> > > 
> > > > -----Oorspronkelijk bericht-----
> > > > Van: [EMAIL PROTECTED] [mailto:programming-
> > > > [EMAIL PROTECTED] Namens Mike Day
> > > > Verzonden: dinsdag 31 juli 2007 12:27
> > > > Aan: Programming forum
> > > > CC: [EMAIL PROTECTED]
> > > > Onderwerp: Re: [Jprogramming] simple verb
> > > > 
> > > > Damien
> > > > 
> > > > This works, but only if the data is in the form
> > > >    [x =: 3 2 1 4 # 4 2 1 3
> > > > 4 4 4 2 2 1 3 3 3 3
> > > >    NB. and not scrambled, eg
> > > >    y =: 3 1 4 3 3 4 3 2 4 2
> > > > 
> > > >    ;@((<@[EMAIL PROTECTED])/.~)x  NB. developed 
> > from your solution
> > > > 0 1 2 0 1 0 0 1 2 3
> > > > 
> > > > Is that what you need or do you also need a
> > > > result for scrambled x?
> > > > 
> > > > Mike
> > > > 
> > > > Damien wrote:
> > > > > Hi,
> > > > >
> > > > > I have a problem to create a verb. I think it is quite 
> > easy, but
> > > > > unfortunately I can not.
> > > > >
> > > > > My Input (example) :     4 4 4 2 2 1 3 
> > 3 3 3
> > > > > As output, I would :       0 
> > 1 2 0 1 0 0 1 2 3
> > > > >
> > > > > I would like a verb which counts the iteration number for 
> > > same element.
> > > > > It is equivalent to : (i. 3) , ( i. 2) , ( i. 1) , ( i. 4)
> > > > > I can do the line above because of the parenthesis.
> > > > > I can only get the vector : 3 2 1 4 easily with #/.~, but 
> > > that is all.
> 
> ----------------------------------------------------------------------
> 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