diff options
Diffstat (limited to 'numpy/core/function_base.py')
-rw-r--r-- | numpy/core/function_base.py | 17 |
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 |