This comes up pretty often with Optim as well, making a closure works fine 
when you're only solving a single problem with a single set of parameters, 
but it's a little cumbersome when you have a small set of parameter values 
that you're trying to loop over many times.

David, can you give a sketch of the funky workaround you were using here? 
You wound up creating a one-element array that you were using to transmit 
the index into the parameter data vector into the inner-loop function, or 
something like that, right?


On Friday, May 8, 2015 at 11:49:48 AM UTC-7, Steven G. Johnson wrote:
>
> Here is the example from the SciPy manual, translated into working Julia 
> code:
>
> @pyimport scipy.optimize as so
>
> using PyCall
> args = (2, 3, 7, 8, 9, 10)
> function fun(x, args...)
>     u, v = x
>     a, b, c, d, e, f = args
>     return a*u^2 + b*u*v + c*v^2 + d*u + e*v + f
> end
> function gradf(x, args...)
>     u, v = x
>     a, b, c, d, e, f = args
>     gu = 2*a*u + b*v + d     # u-component of the gradient
>     gv = b*u + 2*c*v + e     # v-component of the gradient
>     return [gu, gv]
> end
> x0 = [0, 0]
> so.fmin_cg(fun, x0, fprime=gradf, args=args)
>
>
>
> I copied their style, but it would have been cleaner to write e.g.
>
> function fun(x, a, b, c, d, e, f)
>     u, v = x
>     return a*u^2 + b*u*v + c*v^2 + d*u + e*v + f
> end
>
>
> rather than using varargs.  Of course, it would be cleaner to omit the 
> whole "args" nonsense to begin with, and just write:
>
> so.fmin_cg(x -> fun2(x, a, b, c, d, e, f), x0, fprime= x -> gradf(x, a, b, 
> c, d, e, f))
>
>
> since, as I mentioned above, their "args" is just a clumsy workaround for 
> lexical scoping, which both Python and Julia already have.
>

Reply via email to