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 1 commit
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
Prev Previous commit
Add test, docs, and release note for identity
  • Loading branch information
eric-wieser committed Dec 6, 2019
commit 2e43d9d6638401b3dd8bc40a63b08602271c772c
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
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