Here's a less trivial convolution that shows some J idioms.

This numerically solves the heat equation on a line with the boundaries
fixed.  While this example is from physics, the same techniques can be
used to solve the Black-Scholes equation, which is really the heat
equation in disguise.

dot=:+/@:*           NB. dot product: could use /+ . *
c=:% 4 2 4           NB. convolution kernel
conv=:3: c˙._3 ] NB. convolution on interior
f=:0:,conv,1:        NB. full convolution
init=:0,(?9#0),1     NB. random initial condition
steady=:(i.11)%10    NB. known steady state
   init
0 0.148516 0.775182 0.93709 0.9775 0.00742525 0.345244 0.491862 0.166017
0.466072 1
   steady
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
   f^:_ init NB. iterate until fixed point
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
   0j3": f^:(<5) init NB. first 5 iterations, 3 decimal places
0.000 0.149 0.775 0.937 0.977 0.007 0.345 0.492 0.166 0.466 1.000
0.000 0.268 0.659 0.907 0.725 0.334 0.297 0.374 0.322 0.525 1.000
0.000 0.299 0.623 0.799 0.673 0.423 0.326 0.342 0.386 0.593 1.000
0.000 0.305 0.586 0.724 0.642 0.461 0.354 0.349 0.427 0.643 1.000
0.000 0.299 0.550 0.669 0.617 0.479 0.379 0.370 0.461 0.678 1.000
  0j3": f^:a: init  NB. all iterations, output suppressed

Best wishes,

John

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

Reply via email to