8000 ENH: add identity kwarg to frompyfunc by mattharrigan · Pull Request #8255 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

ENH: add identity kwarg to frompyfunc #8255

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions doc/release/upcoming_changes/8255.new_feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
`numpy.frompyfunc` now accepts an identity argument
---------------------------------------------------
This allows the :attr:`numpy.ufunc.identity` attribute to be set on the
resulting ufunc, meaning it can be used for empty and multi-dimensional
calls to :meth:`numpy.ufunc.reduce`.
9 changes: 8 additions & 1 deletion numpy/core/_add_newdocs.py
Original file line number Diff line number Diff line change
Expand Up @@ -4230,7 +4230,7 @@

add_newdoc('numpy.core.umath', 'frompyfunc',
"""
frompyfunc(func, nin, nout)
frompyfunc(func, nin, nout, *[, identity])

Takes an arbitrary Python function and returns a NumPy ufunc.

Expand All @@ -4245,6 +4245,13 @@
The number of input arguments.
nout : int
The number of objects returned by `func`.
identity : object, optional
The value to use for the `~numpy.ufunc.identity` attribute of the resulting
object. If specified, this is equivalent to setting the underlying
C ``identity`` field to ``PyUFunc_IdentityValue``.
If omitted, the identity is set to ``PyUFunc_None``. Note that this is
_not_ equivalent to setting the identity to ``None``, which implies the
operation is reorderable.

Returns
-------
Expand Down
16 changes: 9 additions & 7 deletions numpy/core/src/umath/umathmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,7 @@ object_ufunc_loop_selector(PyUFuncObject *ufunc,
}

PyObject *
ufunc_frompyfunc(PyObject *NPY_UNUSED(dummy), PyObject *args, PyObject *NPY_UNUSED(kwds)) {
/* Keywords are ignored for now */

ufunc_frompyfunc(PyObject *NPY_UNUSED(dummy), PyObject *args, PyObject *kwds) {
PyObject *function, *pyname = NULL;
int nin, nout, i, nargs;
PyUFunc_PyFuncData *fdata;
Expand All @@ -81,14 +79,18 @@ ufunc_frompyfunc(PyObject *NPY_UNUSED(dummy), PyObject *args, PyObject *NPY_UNUS
Py_ssize_t fname_len = -1;
void * ptr, **data;
int offset[2];
PyObject *identity = NULL; /* note: not the same semantics as Py_None */
static char *kwlist[] = {"", "nin", "nout", "identity", NULL};

if (!PyArg_ParseTuple(args, "Oii:frompyfunc", &function, &nin, &nout)) {
if (!PyArg_ParseTupleAndKeywords(args, kwds, "Oii|$O:frompyfunc", kwlist,
&function, &nin, &nout, &identity)) {
return NULL;
}
if (!PyCallable_Check(function)) {
PyErr_SetString(PyExc_TypeError, "function must be callable");
return NULL;
}

nargs = nin + nout;

pyname = PyObject_GetAttrString(function, "__name__");
Expand Down Expand Up @@ -146,10 +148,10 @@ ufunc_frompyfunc(PyObject *NPY_UNUSED(dummy), PyObject *args, PyObject *NPY_UNUS
/* Do a better job someday */
doc = "dynamic ufunc based on a python function";

self = (PyUFuncObject *)PyUFunc_FromFuncAndData(
self = (PyUFuncObject *)PyUFunc_FromFuncAndDataAndSignatureAndIdentity(
(PyUFuncGenericFunction *)pyfunc_functions, data,
types, /* ntypes */ 1, nin, nout, PyUFunc_None,
str, doc, /* unused */ 0);
types, /* ntypes */ 1, nin, nout, identity ? PyUFunc_IdentityValue : PyUFunc_None,
str, doc, /* unused */ 0, NULL, identity);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A future PR could add signature, which would help vectorize


if (self == NULL) {
PyArray_free(ptr);
Expand Down
26 changes: 26 additions & 0 deletions numpy/core/tests/test_umath.py
Original file line number Diff line number Diff line change
Expand Up @@ -2876,6 +2876,32 @@ def __new__(subtype, shape):
a = simple((3, 4))
assert_equal(a+a, a)


class TestFrompyfunc(object):

def test_identity(self):
def mul(a, b):
return a * b

# with identity=value
mul_ufunc = np.frompyfunc(mul, nin=2, nout=1, identity=1)
assert_equal(mul_ufunc.reduce([2, 3, 4]), 24)
assert_equal(mul_ufunc.reduce(np.ones((2, 2)), axis=(0, 1)), 1)
assert_equal(mul_ufunc.reduce([]), 1)

# with identity=None (reorderable)
mul_ufunc = np.frompyfunc(mul, nin=2, nout=1, identity=None)
assert_equal(mul_ufunc.reduce([2, 3, 4]), 24)
assert_equal(mul_ufunc.reduce(np.ones((2, 2)), axis=(0, 1)), 1)
assert_raises(ValueError, lambda: mul_ufunc.reduce([]))

# with no identity (not reorderable)
mul_ufunc = np.frompyfunc(mul, nin=2, nout=1)
assert_equal(mul_ufunc.reduce([2, 3, 4]), 24)
assert_raises(ValueError, lambda: mul_ufunc.reduce(np.ones((2, 2)), axis=(0, 1)))
assert_raises(ValueError, lambda: mul_ufunc.reduce([]))


def _check_branch_cut(f, x0, dx, re_sign=1, im_sign=-1, sig_zero_ok=False,
dtype=complex):
"""
Expand Down
0