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