[SciPy-User] scipy.optimize and vectorization (or not)

Matt Newville newville at cars.uchicago.edu
Wed May 3 13:41:14 EDT 2017


On Tue, May 2, 2017 at 9:22 PM, Gideon Simpson <gideon.simpson at gmail.com>
wrote:

> I have a scalar valued function that I’d like to minimize, but, by
> default, it’s not readily vectorized.  In particular, it depends
> parametrically on the solution of an ODE, and vaguely looks like:
>
> def f(x, ode_soln):
>         ...
>         val = spicy.integrate.quad(lambda t: sin(x * t) *
> ode_soln.sol(t),0,1)
>
>         return val
>
> By default, if I try to do
>
> spicy.optimize.minimize(lambda x: f(x,ode_soln))
>
> I get an error that indicates that minimize thinks f accepts arrays of
> data.


minimize() takes two arguments:  a callable function that takes an array of
values to be adjusted, and an array of starting values.  Presumably, `x` is
the array of values that you would like optimized, but you have to provide
starting values.

I don't think you need the lambda, but you might want something like:

     scipy.optimize.minimize(f, x0, args=(ode_soln,))

where x0 is an array of starting values.


> I can remedy this by writing a little looped version,
>
> def f_vec(xvec, ode_soln):
>         fvals = np.zeros_like(xvec)
>
>         for j in range(xvec.size):
>                 fvals[j] = f(xvec[j], ode_soln)
>
>         return fvals
>
> but I’m wondering if:
>
> 1.  Is there a smarter/more elegant solution to handling the vectorized
> input?
>

It does handle vectorized input, or perhaps I'm not understanding your
question.


>
> 2.  Is there a way to just tell minimize that it’s going to have to
> evaluate f one point at a time, rather than writing some other function?
>

No, or well it depends what you mean by "one point at a time".  The
objective function provided should take an array of candidate values for
the parameters and return either an array to be minimized in the
least-squares sense or the scalar cost value.

Hope that helps,

--Matt
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/scipy-user/attachments/20170503/06cc7704/attachment.html>


More information about the SciPy-User mailing list