Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is a good tacit form of sum(1/(1+x)^y) in J

As a beginner exercise I tried to calculate the following sum in J, sum(1/(1+0.03)^n for n = 1 to 30 using +/%(1 + 0.03)^ >:i.30. How can I write this into a simple tacit form? all I tried are significantly uglier than the explicit form above like >:@[ (+/&:%)@:^ >:&i.@]

like image 800
ahala Avatar asked Dec 10 '25 00:12

ahala


2 Answers

You could start with

+/@:%@((1 + 0.03) ^ >:@i.) 30

You can make the 0.03 a left argument using a fork, but using a hook can be cleaner

(1 + 0.03) +/@:%@([ ^ >:@i.@]) 30   NB. use fork
(1 + 0.03) +/@:%@(^ >:@i.) 30       NB. use hook

The same operation (increment) is being performed on both the left and right arguments to ^. That is a hint that & (Compose) may be useful.

0.03 +/@:%@(^&>: i.) 30         NB. apply increment to both left & right arg
like image 181
Tikkanz Avatar answered Dec 12 '25 10:12

Tikkanz


When I want a tacit function I often let 13 : bang it out for me. In this case, some variations:

   13 : '+/ %((1+0.03)^1+i.y)'
[: +/ [: % 1.03 ^ 1 + i.

   13 : '+/ %((1+0.03)^>:i.y)'
[: +/ [: % 1.03 ^ [: >: i.

And with 1+0.03 or whatever as a leftargument:

   13 : '+/ %(x^1+i.y)'
[: +/ [: % [ ^ 1 + [: i. ]

   13 : '+/ %(x^>:i.y)'
[: +/ [: % [ ^ [: >: [: i. ]

There are way too many caps ([:) to call it less ugly, though, but that's a start.

like image 41
MPelletier Avatar answered Dec 12 '25 10:12

MPelletier



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!