Kip wrote:
> interested in tacitly terminating a series when
> its partial sums stop changing.
Below is one approach to mechanically transliterate your explicit exp to a
tacit version, expT.
Short story: we use ^:_ to calculate values of the series until it
converges. And, in place of temporary variables, we use an array to store,
retrieve, and update their values at each step. Other than that, the verb is
the same.
We can define convenience functions like k <==> 2 { ] and k eq (1+k) <==>
(1+2{[)`(2"_)`]} so that the code reads similarly to the imperative
original, but these are incidental. The final verb, expT, could be
expressed in Jose's preferred fully-fixed functional form (F^4), in a
straightforward and non-tricky way, be it a bit wordy (though actually
trying to use f. to get this form is frustrated by the heavy use of gerunds
for tacit amend).
The long, readable version is below. The F^4 version is:
(* *!.1e_14@|)"0&.+.@(0 { ])
([ ((0 { ]) + 1 { ])`(0"_)`]}
[ ((1 { ]) * [ % 2 { ])`(1"_)`]}
[ (1 + 2 { ])`(2"_)`]}
(0 { ])`(3"_)`]})^:
((0 { ]) ~: 3 { ])^:_:&1 1 0 0
That's one long line, so watch the wrap (in the code below, too). This is
just a silly proof-of-concept; please don't use this approach in any real J
code. Either stick with the original explicit code, or, if you can, reduce
it to a more elegant approach along the lines that Raul, Roger, and other
posters have been sketching.
-Dan
VARS =. 's t k os'
INIT =. 1 1 0 0 NB. Initial value of each 'variable'
NB. Convenience conjunction. Not strictly needed, just makes
NB. 'partSum' code more readable.
eq =. VARS 1 : '2 : ( ''v`(({.u`($0) i.~ ;:'' , (5!:5 <''m'') , ''
)"_)`]}'' )'
expT =: clean@s partSum^:(s~: os)^:_:&INIT
clean =: (* *!.1e_14@|)"0&.+.
partSum =: [ s eq (s+t) [ t eq (t * Y % k) [ k eq (1+k) os eq s
s =: 0 { ]
t =: 1 { ]
k =: 2 { ]
os =: 3 { ]
Y =: [ NB. Bad idea to name things lowercase 'y'
NB. For you metaprogramming addicts, we could say:
NB. (VARS)=:(i.#;:VARS) [^:(_-:]) L:0"0 {.(_ { ])`''
expT 1
2.71828
^1
2.71828
expT 0j1p1
_1
^ 0j1p1
_1j1.22465e_16
(^0j1p1) = expT 0j1p1
1
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm