numpy/numpy — pull request #290 by mforbes (2012-06-01 01:23:56+00:00)
Title: ENH: Add kwarg support for vectorize (tickets #2100, #1156, and #1487) (clean)

This branch represents a significant cleanup of vectorize.

This PR is a cleaned up version of https://github.com/numpy/numpy/pull/282 (all commits squashed and rebased on the master).

The new version removes much of the introspection previously used (which was a major source of issues: [1099], [1247], [1866], opting for determining the requisite information from the function call. This allows straightforward logic to be used to implement ENH ticket [2100] to allow support for kwargs.

API changes:
* Added `excluded` and `cache` keyword. The former allows arguments to be excluded from vectorization and the latter allows optional caching to prevent redundant function calls when determining the output.

Performance:
* If `otypes` is specified, then the base case (no kwargs, no excluded) performs comparably with the previous version (4% slowdown due to some extra testing).
* If `otypes` is not specified, then the lack of caching the `ufunc` (which led to some bugs/gotchas [1487]) causes about a 35% slowdown.
* Use of `excluded` and kwargs will be quite a bit slower, but is a convenience feature so this is probably okay.

This branch also addresses the following issues:

* [1487] - vectorize(): result depends on execution order; is this normal?: Resolved since we no longer use the same caching mechanism.
* [1488] - vectorize(): could the dtype of the output array be the "smallest possible dtype"?: Possibly resolved since caching is no-longer used unless `otypes` is specified. (This enhancement is not directly addressed, and seems like it was suggested to fix [1487]. The new behaviour may satisfy the request but this needs some testing.)
* [1866] - Cannot vectorize fortran function: Resolved because number of arguments now specified at call time. (We no-longer use vectorize to do the error-checking: the call to the function may fail if the user provides an incorrect number of arguments.)

Issue [1892] still remains and should be addressed separately.

[1099]: http://projects.scipy.org/numpy/ticket/1099
[1156]: http://projects.scipy.org/numpy/ticket/1156
[1247]: http://projects.scipy.org/numpy/ticket/1247
[1487]: http://projects.scipy.org/numpy/ticket/1487
[1488]: http://projects.scipy.org/numpy/ticket/1488
[1866]: http://projects.scipy.org/numpy/ticket/1866
[1892]: http://projects.scipy.org/numpy/ticket/1892
[2100]: http://projects.scipy.org/numpy/ticket/2100