diff options
| author | scoder <stefan_ml@behnel.de> | 2023-05-04 09:29:53 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-05-04 09:29:53 +0200 |
| commit | 442c8f48d3146ec32c7d5387310e171276cf10ac (patch) | |
| tree | d8911d1a64e384b7955d3fc09a07edd218a9f1ee /numpy/core/function_base.py | |
| parent | 3e4a6cba2da27bbe2a6e12c163238e503c9f6a07 (diff) | |
| parent | 9163e933df91b516b6f0c7a9ba8ad1750e642f37 (diff) | |
| download | numpy-442c8f48d3146ec32c7d5387310e171276cf10ac.tar.gz | |
Merge branch 'main' into cython3_noexcept
Diffstat (limited to 'numpy/core/function_base.py')
| -rw-r--r-- | numpy/core/function_base.py | 50 |
1 files changed, 40 insertions, 10 deletions
diff --git a/numpy/core/function_base.py b/numpy/core/function_base.py index f57e95742..00e4e6b0e 100644 --- a/numpy/core/function_base.py +++ b/numpy/core/function_base.py @@ -3,6 +3,7 @@ import warnings import operator import types +import numpy as np from . import numeric as _nx from .numeric import result_type, NaN, asanyarray, ndim from numpy.core.multiarray import add_docstring @@ -34,6 +35,11 @@ def linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, .. versionchanged:: 1.16.0 Non-scalar `start` and `stop` are now supported. + .. versionchanged:: 1.20.0 + Values are rounded towards ``-inf`` instead of ``0`` when an + integer ``dtype`` is specified. The old behavior can + still be obtained with ``np.linspace(start, stop, num).astype(int)`` + Parameters ---------- start : array_like @@ -86,6 +92,7 @@ def linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, scale (a geometric progression). logspace : Similar to `geomspace`, but with the end points specified as logarithms. + :ref:`how-to-partition` Examples -------- @@ -125,16 +132,21 @@ def linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, dt = result_type(start, stop, float(num)) if dtype is None: dtype = dt + integer_dtype = False + else: + integer_dtype = _nx.issubdtype(dtype, _nx.integer) delta = stop - start y = _nx.arange(0, num, dtype=dt).reshape((-1,) + (1,) * ndim(delta)) # 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 div > 0: + _mult_inplace = _nx.isscalar(delta) step = delta / div - if _nx.any(step == 0): + any_step_zero = ( + step == 0 if _mult_inplace else _nx.asanyarray(step == 0).any()) + if any_step_zero: # Special handling for denormal numbers, gh-5437 y /= div if _mult_inplace: @@ -156,11 +168,14 @@ def linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, y += start if endpoint and num > 1: - y[-1] = stop + y[-1, ...] = stop if axis != 0: y = _nx.moveaxis(y, 0, axis) + if integer_dtype: + _nx.floor(y, out=y) + if retstep: return y.astype(dtype, copy=False), step else: @@ -169,7 +184,7 @@ def linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, def _logspace_dispatcher(start, stop, num=None, endpoint=None, base=None, dtype=None, axis=None): - return (start, stop) + return (start, stop, base) @array_function_dispatch(_logspace_dispatcher) @@ -185,6 +200,9 @@ def logspace(start, stop, num=50, endpoint=True, base=10.0, dtype=None, .. versionchanged:: 1.16.0 Non-scalar `start` and `stop` are now supported. + .. versionchanged:: 1.25.0 + Non-scalar 'base` is now supported + Parameters ---------- start : array_like @@ -199,7 +217,7 @@ def logspace(start, stop, num=50, endpoint=True, base=10.0, dtype=None, endpoint : boolean, optional If true, `stop` is the last sample. Otherwise, it is not included. Default is True. - base : float, optional + base : array_like, optional The base of the log space. The step size between the elements in ``ln(samples) / ln(base)`` (or ``log_base(samples)``) is uniform. Default is 10.0. @@ -209,9 +227,10 @@ def logspace(start, stop, num=50, endpoint=True, base=10.0, dtype=None, an integer; `float` is chosen even if the arguments would produce an array of integers. axis : int, optional - The axis in the result to store the samples. Relevant only if start - or stop are array-like. By default (0), the samples will be along a - new axis inserted at the beginning. Use -1 to get an axis at the end. + The axis in the result to store the samples. Relevant only if start, + stop, or base are array-like. By default (0), the samples will be + along a new axis inserted at the beginning. Use -1 to get an axis at + the end. .. versionadded:: 1.16.0 @@ -229,10 +248,11 @@ def logspace(start, stop, num=50, endpoint=True, base=10.0, dtype=None, linspace : Similar to logspace, but with the samples uniformly distributed in linear space, instead of log space. geomspace : Similar to logspace, but with endpoints specified directly. + :ref:`how-to-partition` Notes ----- - Logspace is equivalent to the code + If base is a scalar, logspace is equivalent to the code >>> y = np.linspace(start, stop, num=num, endpoint=endpoint) ... # doctest: +SKIP @@ -247,6 +267,9 @@ def logspace(start, stop, num=50, endpoint=True, base=10.0, dtype=None, array([100. , 177.827941 , 316.22776602, 562.34132519]) >>> np.logspace(2.0, 3.0, num=4, base=2.0) array([4. , 5.0396842 , 6.34960421, 8. ]) + >>> np.logspace(2.0, 3.0, num=4, base=[2.0, 3.0], axis=-1) + array([[ 4. , 5.0396842 , 6.34960421, 8. ], + [ 9. , 12.98024613, 18.72075441, 27. ]]) Graphical illustration: @@ -264,7 +287,13 @@ def logspace(start, stop, num=50, endpoint=True, base=10.0, dtype=None, >>> plt.show() """ + ndmax = np.broadcast(start, stop, base).ndim + start, stop, base = ( + np.array(a, copy=False, subok=True, ndmin=ndmax) + for a in (start, stop, base) + ) y = linspace(start, stop, num=num, endpoint=endpoint, axis=axis) + base = np.expand_dims(base, axis=axis) if dtype is None: return _nx.power(base, y) return _nx.power(base, y).astype(dtype, copy=False) @@ -325,6 +354,7 @@ def geomspace(start, stop, num=50, endpoint=True, dtype=None, axis=0): progression. arange : Similar to linspace, with the step size specified instead of the number of samples. + :ref:`how-to-partition` Notes ----- @@ -363,7 +393,7 @@ def geomspace(start, stop, num=50, endpoint=True, dtype=None, axis=0): 6.12323400e-17+1.00000000e+00j, 7.07106781e-01+7.07106781e-01j, 1.00000000e+00+0.00000000e+00j]) - Graphical illustration of ``endpoint`` parameter: + Graphical illustration of `endpoint` parameter: >>> import matplotlib.pyplot as plt >>> N = 10 |
