8000 BUG: Reascertain that linspace respects ndarray subclasses in start, stop. by mhvk · Pull Request #7156 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

BUG: Reascertain that linspace respects ndarray subclasses in start, stop. #7156

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 1 commit into from
Feb 1, 2016
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
11 changes: 8 additions & 3 deletions numpy/core/function_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,18 +96,23 @@ def linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None):

y = _nx.arange(0, num, dtype=dt)

delta = stop - start
if num > 1:
delta = stop - start
step = delta / div
if step == 0:
# Special handling for denormal numbers, gh-5437
y /= div
y *= delta
y = y * delta
else:
y *= step
# One might be tempted to use faster, in-place multiplication here,
# but this prevents step from overriding what class is produced,
# and thus prevents, e.g., use of Quantities; see gh-7142.
y = y * step
else:
# 0 and 1 item long sequences have an undefined step
step = NaN
# Multiply with delta to allow possible override of output class.
y = y * delta

y += start

Expand Down
15 changes: 14 additions & 1 deletion numpy/core/tests/test_function_base.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import division, absolute_import, print_function

from numpy import (logspace, linspace, dtype, array, finfo, typecodes, arange,
isnan)
isnan, ndarray)
from numpy.testing import (
TestCase, run_module_suite, assert_, assert_equal, assert_raises,
assert_array_equal
Expand Down Expand Up @@ -115,6 +115,19 @@ def __rdiv__(self, x):
b = PhysicalQuantity(1.0)
assert_equal(linspace(a, b), linspace(0.0, 1.0))

def test_subclass(self):
class PhysicalQuantity2(ndarray):
__array_priority__ = 10

a = array(0).view(PhysicalQuantity2)
b = array(1).view(PhysicalQuantity2)
ls = linspace(a, b)
assert type(ls) is PhysicalQuantity2
assert_equal(ls, linspace(0.0, 1.0))
ls = linspace(a, b, 1)
assert type(ls) is PhysicalQuantity2
assert_equal(ls, linspace(0.0, 1.0, 1))

def test_denormal_numbers(self):
# Regression test for gh-5437. Will probably fail when compiled
# with ICC, which flushes denormals to zero
Expand Down
0