[SciPy-User] Gradient inputs to SciPy optimize routines
Mon Jul 23 20:21:31 CDT 2012
I would speculate that the reason the optimise routines don't accept arbitrary sequence types for the Jacobian is one of performance. The gradient functions get called many times during the optimisation and pythons sequence types are slow enough that converting the arbitrary sequence into an array would probably be slower than computing the Jacobian using finite differences within the algorithm. Although calling np.ascontiguous has relatively low overhead if the data is already an array, it is another function call (expensive in python) and could be up to ~30% of total running time for a simple model function. The same applies to too many checks on data type. Within the optimising loop, they will be costly.
I like to think of all scipy functions as being designed to work on numpy arrays, but some of them will gracefully do the conversion for you if you give them the wrong data type.
From: The Helmbolds <firstname.lastname@example.org>
To: Sturla Molden <email@example.com>; SciPy Users List <firstname.lastname@example.org>
Sent: Tuesday, 24 July 2012 3:12 AM
Subject: Re: [SciPy-User] Gradient inputs to SciPy optimize routines
Thanks. That's good to know. So if all the optimize routines simply inserted
fprime = np.ascontinuaousarray(fprime)
before calling _minimize_(whatever), the user's `fprime` function could return any "array like" object and all would be well?
2645 E Southern Ave A241
Tempe AZ 85282
CELL Igor: 480-438-3918
CELL Alf: 602-568-6948 (but not often turned on)
From: Sturla Molden <email@example.com>
>To: The Helmbolds <firstname.lastname@example.org>; SciPy Users List <email@example.com>
>Cc: Warren Weckesser <firstname.lastname@example.org>; SciPy Users List <email@example.com>
>Sent: Monday, July 23, 2012 7:36 AM
>Subject: Re: [SciPy-User] Gradient inputs to SciPy optimize routines
>The NumPy functions np.ascontiguousarray and np.asfortranarray are often used internally in SciPy for this purpose; that is, they will only make a copy if the argument is not a NumPy ndarray that can be passed safely to C or Fortran. Any Python sequence which is not a C or Fortran contiguous NumPy array will be converted in that process.
SciPy-User mailing list
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the SciPy-User