That took a bit to digest.

I think you are asking about how to deal with callbacks to J.

The issue here is that there's four J verbs, which all need to be
handled through a callback into J.

There's also potentially a callback from J back into gsl to get a
uniformly distributed random number. But the mechanism there is
slightly awful (the example uses gsl's default behavior for uniform
random numbers which has a 32 bit legacy), so I think J's built in
random number generator should be preferred for this example.

With that in mind, the J side of things would look something like this:

p=:''
p=:p, N_TRIES=: 200
p=:p, ITERS_FIXED_T=: 1000
p=:p, STEP_SIZE=: 1.0
p=:p, K=: 1.0
p=:p, T_INITIAL=: 0.008
p=:p, MU_T=: 1.003
p=:p, T_MIN=: 2e_6
params=: p

E1=: {{ (^-(y-1)^2)*1 o.8*y }}
M1=: |@-
S1=: {{ (?0)*2*y-y*x }}
P1=: {{ 0 [9!:11 p [echo y [9!:11]12 [p=. 9!:10'' }}

The next part involves setting up the callback (15!:13 and
https://github.com/jsoftware/jsource/blob/master/jsrc/x15.c#L1425),
the J side of the callback would probably look something like this:

cdcallback=: {{
  'fn yv xv'=. 3{. y
  select. fn.
    case. 0 do. cdresult=: E1 yv
    case. 1 do. cdresult=: xv M1 yv
    case. 2 do. cdresult=: xv S1 yv
    case. 3 do. cdresult=: P1 y
  end.
}}

The final step would be to wrap all of this in a C implementation with
a main(), which plugs itself into gsl_siman_solve

But it would probably be more space efficient and time efficient to
implement a workalike for gsl_siman_solve natively in J, perhaps based
on the writeup at https://en.wikipedia.org/wiki/Simulated_annealing#,
because the interface friction here has a rather high cost.
(Specifically, gsl_siman_solve expects that the most efficient way of
working with data structures is at the machine operation level. It
expects to be blind to array structure. Meanwhile, J expects that the
most efficient way of working with data structures is to require cache
coherence at the low level. The combination, from a programmer's
perspective, is sort of like a colony of ants trying to carry a sports
car.)

I hope this helps,

--
Raul


On Sat, Feb 27, 2021 at 7:08 AM Emir U <e...@usgroupltd.uk> wrote:
>
> Hey Chris, thank you, those examples were very helpful. I haven't touched C 
> in ages and the data structures in GSL are pretty hairy at times. It'd be 
> very helpful to know how you'd approach something like the "Trivial Example" 
> from https://www.gnu.org/software/gsl/doc/html/siman.html but with the 1D 
> function specified from J. Emir
> ----------------------------------------------------------------------
> 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