summaryrefslogtreecommitdiff
path: root/numpy/core
diff options
context:
space:
mode:
authorOleksandr Pavlyk <oleksandr.pavlyk@intel.com>2017-09-18 10:04:37 -0500
committerOleksandr Pavlyk <oleksandr.pavlyk@intel.com>2017-09-28 12:15:36 -0500
commit8acb0f7e73be9b8e797ffbe1bdce4d7076202183 (patch)
tree2e9224ad858a40212d10abed085be498cdc21b7f /numpy/core
parent1e45fd9ea1a72cb03a0bab79d30134ef9b7106be (diff)
downloadnumpy-8acb0f7e73be9b8e797ffbe1bdce4d7076202183.tar.gz
MAINT: When delta is a NumPy scalar, do multiplication in-place
This is faster and more memory efficient. Out-of-place operation was preventing me from using linspace to create over 2**31 equispaced doubles on my machine due to insufficient memory.
Diffstat (limited to 'numpy/core')
-rw-r--r--numpy/core/function_base.py17
1 files changed, 12 insertions, 5 deletions
diff --git a/numpy/core/function_base.py b/numpy/core/function_base.py
index 0415e16ac..82de1a36e 100644
--- a/numpy/core/function_base.py
+++ b/numpy/core/function_base.py
@@ -115,17 +115,24 @@ def linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None):
y = _nx.arange(0, num, dtype=dt)
delta = stop - start
+ # In-place multiplication y *= delta/div is faster, but prevents the multiplicant
+ # from overriding what class is produced, and thus prevents, e.g. use of Quantities,
+ # see gh-7142. Hence, we multiply in place only for standard scalar types.
+ _mult_inplace = _nx.isscalar(delta)
if num > 1:
step = delta / div
if step == 0:
# Special handling for denormal numbers, gh-5437
y /= div
- y = y * delta
+ if _mult_inplace:
+ y *= delta
+ else:
+ y = y * delta
else:
- # 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
+ if _mult_inplace:
+ y *= step
+ else:
+ y = y * step
else:
# 0 and 1 item long sequences have an undefined step
step = NaN