I really appreciate this illustration of how to reformulate an accumulating loop using a running suffix. I think having both versions on RC is a great way of helping those interested understand how the J approach differs.
I've had another look at the problem and this is how I'd suggest formating the solution for the audience at Rosetta Code. nextY=: adverb define : tableau=. 1 0.5 0.5, x * u y ks=. (x * [: u y + (* x&,))/\. tableau ({:y) + 6 %~ +/ 1 2 2 1 * ks ) rk4M2=: adverb define 'Y0 a b h'=. 4{. y T=. a + i.@>:&.(%&h) b-a (,. [: h&(u nextY)@,/\. Y0 ,~ }.)&.|. T ) All versions produce the same result: (fyp rk4 -: fyp rk42) 1 0 10 0.001 1 (fyp rk4 -: fyp rk4M) 1 0 10 0.001 1 (fyp rk4 -: fyp rk4M2) 1 0 10 0.001 1 In my opinion there aren't any worthwhile differences in performance for any of the versions and I think that clarity is more important for RC anyway, however for the purposes of completeness ... (Note that the verb timespacex is defined in stdlib.ijs in J7) 10 timespacex 'fyp rk4 1 0 10 0.001' 0.1677 531584 10 timespacex 'fyp rk42 1 0 10 0.001' 0.13001 531584 10 timespacex 'fyp rk4M 1 0 10 0.001' 0.131611 798336 10 timespacex 'fyp rk4M2 1 0 10 0.001' 0.146232 665344 On Sat, May 12, 2012 at 5:32 AM, Aai <agroeneveld...@gmail.com> wrote: > Observation: the following variant of Ric's contribution gains in speed > by using a one-line 'pseudo fold' for the loop calculations. > > Here are the timings: > > ts@(,&' args')&> 'fyp rk4';'fyp rk42' [args=. 1 0 10 0.001 > 0.551397 527936 > 0.449719 527936 > > rk42 is almost as fast (?) as my rk4M. > > mind line wrapping > > rk42=: 1 :0 > Y=.Yt=.{. 'Y0 a b h'=. 4{. y > T=. a + i.@>:&.(%&h) b - a > for_t. }: T do. > Y=.Y,Yt=.Yt+6%~+/1 2 2 1*k1,k2,k3,k4=.h*u ty+h,k3=.h*u ty+-:h,k2=.h*u > ty+-:h,k1=.h*u ty=.t,Yt > end. > T ,. Y > ) > > > > On 10-05-12 22:52, Ric Sherlock wrote: >> Yes thanks for this. I wasn't especially happy with the existing >> solution. I agree with Raul that it would be nicer to be consistent >> with the definition style. >> I'll try to have a go over the weekend but if it is possible to break >> either of those long lines up into more recognizable (to non tacit J >> users) chunks. I think that would be worthwhile. >> >> On Fri, May 11, 2012 at 8:12 AM, Raul Miller<rauldmil...@gmail.com> wrote: >>> On Thu, May 10, 2012 at 1:46 PM, Aai<agroeneveld...@gmail.com> wrote: >>>> The J contribution on Rosetta Code >>>> http://rosettacode.org/wiki/Runge-Kutta_method#J for subject is a loopy >>>> one. My goal is to change that into a more functional and J-ish one. >>>> >>>> Here's my attempt for that particular task: >>>> >>>> p4=: 1 :(':';'({:y)+6%~+/1 2 2 1*(x*[: u y+(*x&,@{:))/\.1 0.5 0.5,x*u y') >>>> >>>> rk4M=: 1 :0 >>>> 'Y0 a b h'=. 4{. y >>>> (,.[:h&(u p4)@(,{:)/\.Y0,~}.)&.|. a+i.@>:&.(%&h) b-a >>>> ) >>>> >>>> (fyp rk4 -: fyp rk4M) 1 0 10 0.001 >>>> 1 >>> As a minor point, I think I would change the definition of p4 >>> to use the same definition format as rk4M >>> >>> p4=: 1 :0 >>> : >>> ({:y)+6%~+/1 2 2 1*(x*[: u y+(*x&,@{:))/\.1 0.5 0.5,x*u y >>> ) >>> >>> But I do not have the time to focus on this one. >>> >>> I hope I'm not overlooking anything important. >>> (I wonder if the parenthesized expressions deserve names?) >>> >>> Thanks for tackling this! >>> >>> -- >>> Raul >>> ---------------------------------------------------------------------- >>> For information about J forums seehttp://www.jsoftware.com/forums.htm >> ---------------------------------------------------------------------- >> For information about J forums seehttp://www.jsoftware.com/forums.htm > > -- > Met vriendelijke groet, > @@i = Arie Groeneveld > > ---------------------------------------------------------------------- > For information about J forums see http://www.jsoftware.com/forums.htm ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm