Skip to content

Commit f9cf430

Browse files
committed
Add complex number support to linspace
1 parent 31142bb commit f9cf430

File tree

1 file changed

+29
-8
lines changed

1 file changed

+29
-8
lines changed

spec/API_specification/array_api/creation_functions.py

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -213,24 +213,45 @@ def full_like(x: array, /, fill_value: Union[bool, int, float, complex], *, dtyp
213213
an array having the same shape as ``x`` and where every element is equal to ``fill_value``.
214214
"""
215215

216-
def linspace(start: Union[int, float], stop: Union[int, float], /, num: int, *, dtype: Optional[dtype] = None, device: Optional[device] = None, endpoint: bool = True) -> array:
217-
"""
216+
def linspace(start: Union[int, float, complex], stop: Union[int, float, complex], /, num: int, *, dtype: Optional[dtype] = None, device: Optional[device] = None, endpoint: bool = True) -> array:
217+
r"""
218218
Returns evenly spaced numbers over a specified interval.
219219
220+
Let :math:`N` be the number of generated values (which is either ``num`` or ``num+1`` depending on whether ``endpoint`` is ``False`` or ``True``, respectively). For real-valued output arrays, the spacing between values is given by
221+
222+
.. math::
223+
\delta_{\textrm{real}} = \frac{\textrm{stop} - \textrm{start}}{N - 1}
224+
225+
For complex output arrays, let ``a = real(start)``, ``b = imag(start)``, ``c = real(stop)``, and ``d = imag(stop)``. The spacing between complex values is given by
226+
227+
.. math::
228+
\delta_{\textrm{complex}} = \frac{c-a}{N-1} + \frac{d-b}{N-1} j
229+
220230
Parameters
221231
----------
222-
start: Union[int, float]
232+
start: Union[int, float, complex]
223233
the start of the interval.
224-
stop: Union[int, float]
234+
stop: Union[int, float, complex]
225235
the end of the interval. If ``endpoint`` is ``False``, the function must generate a sequence of ``num+1`` evenly spaced numbers starting with ``start`` and ending with ``stop`` and exclude the ``stop`` from the returned array such that the returned array consists of evenly spaced numbers over the half-open interval ``[start, stop)``. If ``endpoint`` is ``True``, the output array must consist of evenly spaced numbers over the closed interval ``[start, stop]``. Default: ``True``.
226236
227237
.. note::
228238
The step size changes when `endpoint` is `False`.
229239
230240
num: int
231-
number of samples. Must be a non-negative integer value; otherwise, the function must raise an exception.
241+
number of samples. Must be a nonnegative integer value.
232242
dtype: Optional[dtype]
233-
output array data type. Should be a real-valued floating-point data type. If ``dtype`` is ``None``, the output array data type must be the default real-valued floating-point data type. Default: ``None``.
243+
output array data type. Should be a floating-point data type. If ``dtype`` is ``None``,
244+
245+
- if either ``start`` or ``stop`` is a ``complex`` number, the output data type must be the default complex floating-point data type.
246+
- if both ``start`` and ``stop`` are real-valued, the output data type must be the default real-valued floating-point data type.
247+
248+
Default: ``None``.
249+
250+
.. admonition:: Note
251+
:class: note
252+
253+
If ``dtype`` is not ``None``, conversion of ``start`` and ``stop`` should obey :ref:`type-promotion` rules. Conversions not specified according to :ref:`type-promotion` rules may or may not be permitted by a conforming array library.
254+
234255
device: Optional[device]
235256
device on which to place the created array. Default: ``None``.
236257
endpoint: bool
@@ -243,10 +264,10 @@ def linspace(start: Union[int, float], stop: Union[int, float], /, num: int, *,
243264
244265
245266
.. note::
246-
While this specification recommends that this function only return arrays having a real-valued floating-point data type, specification-compliant array libraries may choose to support output arrays having an integer data type (e.g., due to backward compatibility concerns). However, function behavior when generating integer output arrays is unspecified and, thus, is implementation-defined. Accordingly, using this function to generate integer output arrays is not portable.
267+
While this specification recommends that this function only return arrays having a floating-point data type, specification-compliant array libraries may choose to support output arrays having an integer data type (e.g., due to backward compatibility concerns). However, function behavior when generating integer output arrays is unspecified and, thus, is implementation-defined. Accordingly, using this function to generate integer output arrays is not portable.
247268
248269
.. note::
249-
As mixed data type promotion is implementation-defined, behavior when ``start`` or ``stop`` exceeds the maximum safe integer of an output real-valued floating-point data type is implementation-defined. An implementation may choose to overflow or raise an exception.
270+
As mixed data type promotion is implementation-defined, behavior when ``start`` or ``stop`` exceeds the maximum safe integer of an output floating-point data type is implementation-defined. An implementation may choose to overflow or raise an exception.
250271
"""
251272

252273
def meshgrid(*arrays: array, indexing: str = 'xy') -> List[array]:

0 commit comments

Comments
 (0)