8000 BUG/DEP: Make ufunclike functions more ufunc-like by eric-wieser · Pull Request #8996 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

BUG/DEP: Make ufunclike functions more ufunc-like #8996

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 2 commits into from
Apr 28, 2017
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
BUG: Return scalars from ufunclike objects
No need to reinvent the wheel here - the ufunc machinery will handle the out arguments

Fixes #8993
  • Loading branch information
eric-wieser committed Apr 26, 2017
commit 05f422827b3317ad3774cbd1859edbf6bc196f75
25 changes: 25 additions & 0 deletions numpy/lib/tests/test_ufunclike.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import division, absolute_import, print_function

import numpy as np
import numpy.core as nx
import numpy.lib.ufunclike as ufl
from numpy.testing import (
Expand Down Expand Up @@ -62,11 +63,35 @@ def __array_wrap__(self, obj, context=None):
assert_(isinstance(f, MyArray))
assert_equal(f.metadata, 'foo')

# check 0d arrays don't decay to scalars
m0d = m[0,...]
m0d.metadata = 'bar'
f0d = ufl.fix(m0d)
assert_(isinstance(f0d, MyArray))
assert_equal(f0d.metadata, 'bar')

def test_deprecated(self):
# NumPy 1.13.0, 2017-04-26
assert_warns(DeprecationWarning, ufl.fix, [1, 2], y=nx.empty(2))
assert_warns(DeprecationWarning, ufl.isposinf, [1, 2], y=nx.empty(2))
assert_warns(DeprecationWarning, ufl.isneginf, [1, 2], y=nx.empty(2))

def test_scalar(self):
Copy link
Member
@charris charris Apr 26, 2017

Choose a reason for hiding this comment

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

@need a blank line between functions.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yep, bad rebase...

Copy link
Member Author

Choose a reason for hiding t 8000 his comment

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

(fixed)

x = np.inf
actual = np.isposinf(x)
expected = np.True_
assert_equal(actual, expected)
assert_equal(type(actual), type(expected))

x = -3.4
actual = np.fix(x)
expected = np.float64(-3.0)
assert_equal(actual, expected)
assert_equal(type(actual), type(expected))

out = np.array(0.0)
actual = np.fix(x, out=out)
assert_(actual is out)

if __name__ == "__main__":
run_module_suite()
27 changes: 10 additions & 17 deletions numpy/lib/ufunclike.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,15 @@ def fix(x, out=None):
array([ 2., 2., -2., -2.])

"""
x = nx.asanyarray(x)
y1 = nx.floor(x)
y2 = nx.ceil(x)
if out is None:
out = nx.asanyarray(y1)
out[...] = nx.where(x >= 0, y1, y2)
return out
# promote back to an array if flattened
res = nx.asanyarray(nx.ceil(x, out=out))
res = nx.floor(x, out=res, where=nx.greater_equal(x, 0))

# when no out argument is passed and no subclasses are involved, flatten
# scalars
if out is None and type(res) is nx.ndarray:
res = res[()]
return res

@_deprecate_out_named_y
def isposinf(x, out=None):
Expand Down Expand Up @@ -137,11 +138,7 @@ def isposinf(x, out=None):
array([0, 0, 1])

"""
if y is None:
x = nx.asarray(x)
out = nx.empty(x.shape, dtype=nx.bool_)
nx.logical_and(nx.isinf(x), ~nx.signbit(x), out)
return out
return nx.logical_and(nx.isinf(x), ~nx.signbit(x), out)


@_deprecate_out_named_y
Expand Down Expand Up @@ -202,8 +199,4 @@ def isneginf(x, out=None):
array([1, 0, 0])

"""
if out is None:
x = nx.asarray(x)
out = nx.empty(x.shape, dtype=nx.bool_)
nx.logical_and(nx.isinf(x), nx.signbit(x), out)
return out
return nx.logical_and(nx.isinf(x), nx.signbit(x), out)
Copy link
Member Author

Choose a reason for hiding this comment

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

For a little more efficiency, we could change this to:

nx.logical_and(nx.isinf(x, out), nx.signbit(x), out)

But that seemed out of scope for the bugfix/deprecation

0