function_base.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. import functools
  2. import warnings
  3. import operator
  4. import types
  5. from . import numeric as _nx
  6. from .numeric import result_type, NaN, asanyarray, ndim
  7. from numpy.core.multiarray import add_docstring
  8. from numpy.core import overrides
  9. __all__ = ['logspace', 'linspace', 'geomspace']
  10. array_function_dispatch = functools.partial(
  11. overrides.array_function_dispatch, module='numpy')
  12. def _linspace_dispatcher(start, stop, num=None, endpoint=None, retstep=None,
  13. dtype=None, axis=None):
  14. return (start, stop)
  15. @array_function_dispatch(_linspace_dispatcher)
  16. def linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None,
  17. axis=0):
  18. """
  19. Return evenly spaced numbers over a specified interval.
  20. Returns `num` evenly spaced samples, calculated over the
  21. interval [`start`, `stop`].
  22. The endpoint of the interval can optionally be excluded.
  23. .. versionchanged:: 1.16.0
  24. Non-scalar `start` and `stop` are now supported.
  25. Parameters
  26. ----------
  27. start : array_like
  28. The starting value of the sequence.
  29. stop : array_like
  30. The end value of the sequence, unless `endpoint` is set to False.
  31. In that case, the sequence consists of all but the last of ``num + 1``
  32. evenly spaced samples, so that `stop` is excluded. Note that the step
  33. size changes when `endpoint` is False.
  34. num : int, optional
  35. Number of samples to generate. Default is 50. Must be non-negative.
  36. endpoint : bool, optional
  37. If True, `stop` is the last sample. Otherwise, it is not included.
  38. Default is True.
  39. retstep : bool, optional
  40. If True, return (`samples`, `step`), where `step` is the spacing
  41. between samples.
  42. dtype : dtype, optional
  43. The type of the output array. If `dtype` is not given, infer the data
  44. type from the other input arguments.
  45. .. versionadded:: 1.9.0
  46. axis : int, optional
  47. The axis in the result to store the samples. Relevant only if start
  48. or stop are array-like. By default (0), the samples will be along a
  49. new axis inserted at the beginning. Use -1 to get an axis at the end.
  50. .. versionadded:: 1.16.0
  51. Returns
  52. -------
  53. samples : ndarray
  54. There are `num` equally spaced samples in the closed interval
  55. ``[start, stop]`` or the half-open interval ``[start, stop)``
  56. (depending on whether `endpoint` is True or False).
  57. step : float, optional
  58. Only returned if `retstep` is True
  59. Size of spacing between samples.
  60. See Also
  61. --------
  62. arange : Similar to `linspace`, but uses a step size (instead of the
  63. number of samples).
  64. geomspace : Similar to `linspace`, but with numbers spaced evenly on a log
  65. scale (a geometric progression).
  66. logspace : Similar to `geomspace`, but with the end points specified as
  67. logarithms.
  68. Examples
  69. --------
  70. >>> np.linspace(2.0, 3.0, num=5)
  71. array([2. , 2.25, 2.5 , 2.75, 3. ])
  72. >>> np.linspace(2.0, 3.0, num=5, endpoint=False)
  73. array([2. , 2.2, 2.4, 2.6, 2.8])
  74. >>> np.linspace(2.0, 3.0, num=5, retstep=True)
  75. (array([2. , 2.25, 2.5 , 2.75, 3. ]), 0.25)
  76. Graphical illustration:
  77. >>> import matplotlib.pyplot as plt
  78. >>> N = 8
  79. >>> y = np.zeros(N)
  80. >>> x1 = np.linspace(0, 10, N, endpoint=True)
  81. >>> x2 = np.linspace(0, 10, N, endpoint=False)
  82. >>> plt.plot(x1, y, 'o')
  83. [<matplotlib.lines.Line2D object at 0x...>]
  84. >>> plt.plot(x2, y + 0.5, 'o')
  85. [<matplotlib.lines.Line2D object at 0x...>]
  86. >>> plt.ylim([-0.5, 1])
  87. (-0.5, 1)
  88. >>> plt.show()
  89. """
  90. num = operator.index(num)
  91. if num < 0:
  92. raise ValueError("Number of samples, %s, must be non-negative." % num)
  93. div = (num - 1) if endpoint else num
  94. # Convert float/complex array scalars to float, gh-3504
  95. # and make sure one can use variables that have an __array_interface__, gh-6634
  96. start = asanyarray(start) * 1.0
  97. stop = asanyarray(stop) * 1.0
  98. dt = result_type(start, stop, float(num))
  99. if dtype is None:
  100. dtype = dt
  101. delta = stop - start
  102. y = _nx.arange(0, num, dtype=dt).reshape((-1,) + (1,) * ndim(delta))
  103. # In-place multiplication y *= delta/div is faster, but prevents the multiplicant
  104. # from overriding what class is produced, and thus prevents, e.g. use of Quantities,
  105. # see gh-7142. Hence, we multiply in place only for standard scalar types.
  106. _mult_inplace = _nx.isscalar(delta)
  107. if div > 0:
  108. step = delta / div
  109. if _nx.any(step == 0):
  110. # Special handling for denormal numbers, gh-5437
  111. y /= div
  112. if _mult_inplace:
  113. y *= delta
  114. else:
  115. y = y * delta
  116. else:
  117. if _mult_inplace:
  118. y *= step
  119. else:
  120. y = y * step
  121. else:
  122. # sequences with 0 items or 1 item with endpoint=True (i.e. div <= 0)
  123. # have an undefined step
  124. step = NaN
  125. # Multiply with delta to allow possible override of output class.
  126. y = y * delta
  127. y += start
  128. if endpoint and num > 1:
  129. y[-1] = stop
  130. if axis != 0:
  131. y = _nx.moveaxis(y, 0, axis)
  132. if retstep:
  133. return y.astype(dtype, copy=False), step
  134. else:
  135. return y.astype(dtype, copy=False)
  136. def _logspace_dispatcher(start, stop, num=None, endpoint=None, base=None,
  137. dtype=None, axis=None):
  138. return (start, stop)
  139. @array_function_dispatch(_logspace_dispatcher)
  140. def logspace(start, stop, num=50, endpoint=True, base=10.0, dtype=None,
  141. axis=0):
  142. """
  143. Return numbers spaced evenly on a log scale.
  144. In linear space, the sequence starts at ``base ** start``
  145. (`base` to the power of `start`) and ends with ``base ** stop``
  146. (see `endpoint` below).
  147. .. versionchanged:: 1.16.0
  148. Non-scalar `start` and `stop` are now supported.
  149. Parameters
  150. ----------
  151. start : array_like
  152. ``base ** start`` is the starting value of the sequence.
  153. stop : array_like
  154. ``base ** stop`` is the final value of the sequence, unless `endpoint`
  155. is False. In that case, ``num + 1`` values are spaced over the
  156. interval in log-space, of which all but the last (a sequence of
  157. length `num`) are returned.
  158. num : integer, optional
  159. Number of samples to generate. Default is 50.
  160. endpoint : boolean, optional
  161. If true, `stop` is the last sample. Otherwise, it is not included.
  162. Default is True.
  163. base : float, optional
  164. The base of the log space. The step size between the elements in
  165. ``ln(samples) / ln(base)`` (or ``log_base(samples)``) is uniform.
  166. Default is 10.0.
  167. dtype : dtype
  168. The type of the output array. If `dtype` is not given, infer the data
  169. type from the other input arguments.
  170. axis : int, optional
  171. The axis in the result to store the samples. Relevant only if start
  172. or stop are array-like. By default (0), the samples will be along a
  173. new axis inserted at the beginning. Use -1 to get an axis at the end.
  174. .. versionadded:: 1.16.0
  175. Returns
  176. -------
  177. samples : ndarray
  178. `num` samples, equally spaced on a log scale.
  179. See Also
  180. --------
  181. arange : Similar to linspace, with the step size specified instead of the
  182. number of samples. Note that, when used with a float endpoint, the
  183. endpoint may or may not be included.
  184. linspace : Similar to logspace, but with the samples uniformly distributed
  185. in linear space, instead of log space.
  186. geomspace : Similar to logspace, but with endpoints specified directly.
  187. Notes
  188. -----
  189. Logspace is equivalent to the code
  190. >>> y = np.linspace(start, stop, num=num, endpoint=endpoint)
  191. ... # doctest: +SKIP
  192. >>> power(base, y).astype(dtype)
  193. ... # doctest: +SKIP
  194. Examples
  195. --------
  196. >>> np.logspace(2.0, 3.0, num=4)
  197. array([ 100. , 215.443469 , 464.15888336, 1000. ])
  198. >>> np.logspace(2.0, 3.0, num=4, endpoint=False)
  199. array([100. , 177.827941 , 316.22776602, 562.34132519])
  200. >>> np.logspace(2.0, 3.0, num=4, base=2.0)
  201. array([4. , 5.0396842 , 6.34960421, 8. ])
  202. Graphical illustration:
  203. >>> import matplotlib.pyplot as plt
  204. >>> N = 10
  205. >>> x1 = np.logspace(0.1, 1, N, endpoint=True)
  206. >>> x2 = np.logspace(0.1, 1, N, endpoint=False)
  207. >>> y = np.zeros(N)
  208. >>> plt.plot(x1, y, 'o')
  209. [<matplotlib.lines.Line2D object at 0x...>]
  210. >>> plt.plot(x2, y + 0.5, 'o')
  211. [<matplotlib.lines.Line2D object at 0x...>]
  212. >>> plt.ylim([-0.5, 1])
  213. (-0.5, 1)
  214. >>> plt.show()
  215. """
  216. y = linspace(start, stop, num=num, endpoint=endpoint, axis=axis)
  217. if dtype is None:
  218. return _nx.power(base, y)
  219. return _nx.power(base, y).astype(dtype, copy=False)
  220. def _geomspace_dispatcher(start, stop, num=None, endpoint=None, dtype=None,
  221. axis=None):
  222. return (start, stop)
  223. @array_function_dispatch(_geomspace_dispatcher)
  224. def geomspace(start, stop, num=50, endpoint=True, dtype=None, axis=0):
  225. """
  226. Return numbers spaced evenly on a log scale (a geometric progression).
  227. This is similar to `logspace`, but with endpoints specified directly.
  228. Each output sample is a constant multiple of the previous.
  229. .. versionchanged:: 1.16.0
  230. Non-scalar `start` and `stop` are now supported.
  231. Parameters
  232. ----------
  233. start : array_like
  234. The starting value of the sequence.
  235. stop : array_like
  236. The final value of the sequence, unless `endpoint` is False.
  237. In that case, ``num + 1`` values are spaced over the
  238. interval in log-space, of which all but the last (a sequence of
  239. length `num`) are returned.
  240. num : integer, optional
  241. Number of samples to generate. Default is 50.
  242. endpoint : boolean, optional
  243. If true, `stop` is the last sample. Otherwise, it is not included.
  244. Default is True.
  245. dtype : dtype
  246. The type of the output array. If `dtype` is not given, infer the data
  247. type from the other input arguments.
  248. axis : int, optional
  249. The axis in the result to store the samples. Relevant only if start
  250. or stop are array-like. By default (0), the samples will be along a
  251. new axis inserted at the beginning. Use -1 to get an axis at the end.
  252. .. versionadded:: 1.16.0
  253. Returns
  254. -------
  255. samples : ndarray
  256. `num` samples, equally spaced on a log scale.
  257. See Also
  258. --------
  259. logspace : Similar to geomspace, but with endpoints specified using log
  260. and base.
  261. linspace : Similar to geomspace, but with arithmetic instead of geometric
  262. progression.
  263. arange : Similar to linspace, with the step size specified instead of the
  264. number of samples.
  265. Notes
  266. -----
  267. If the inputs or dtype are complex, the output will follow a logarithmic
  268. spiral in the complex plane. (There are an infinite number of spirals
  269. passing through two points; the output will follow the shortest such path.)
  270. Examples
  271. --------
  272. >>> np.geomspace(1, 1000, num=4)
  273. array([ 1., 10., 100., 1000.])
  274. >>> np.geomspace(1, 1000, num=3, endpoint=False)
  275. array([ 1., 10., 100.])
  276. >>> np.geomspace(1, 1000, num=4, endpoint=False)
  277. array([ 1. , 5.62341325, 31.6227766 , 177.827941 ])
  278. >>> np.geomspace(1, 256, num=9)
  279. array([ 1., 2., 4., 8., 16., 32., 64., 128., 256.])
  280. Note that the above may not produce exact integers:
  281. >>> np.geomspace(1, 256, num=9, dtype=int)
  282. array([ 1, 2, 4, 7, 16, 32, 63, 127, 256])
  283. >>> np.around(np.geomspace(1, 256, num=9)).astype(int)
  284. array([ 1, 2, 4, 8, 16, 32, 64, 128, 256])
  285. Negative, decreasing, and complex inputs are allowed:
  286. >>> np.geomspace(1000, 1, num=4)
  287. array([1000., 100., 10., 1.])
  288. >>> np.geomspace(-1000, -1, num=4)
  289. array([-1000., -100., -10., -1.])
  290. >>> np.geomspace(1j, 1000j, num=4) # Straight line
  291. array([0. +1.j, 0. +10.j, 0. +100.j, 0.+1000.j])
  292. >>> np.geomspace(-1+0j, 1+0j, num=5) # Circle
  293. array([-1.00000000e+00+1.22464680e-16j, -7.07106781e-01+7.07106781e-01j,
  294. 6.12323400e-17+1.00000000e+00j, 7.07106781e-01+7.07106781e-01j,
  295. 1.00000000e+00+0.00000000e+00j])
  296. Graphical illustration of ``endpoint`` parameter:
  297. >>> import matplotlib.pyplot as plt
  298. >>> N = 10
  299. >>> y = np.zeros(N)
  300. >>> plt.semilogx(np.geomspace(1, 1000, N, endpoint=True), y + 1, 'o')
  301. [<matplotlib.lines.Line2D object at 0x...>]
  302. >>> plt.semilogx(np.geomspace(1, 1000, N, endpoint=False), y + 2, 'o')
  303. [<matplotlib.lines.Line2D object at 0x...>]
  304. >>> plt.axis([0.5, 2000, 0, 3])
  305. [0.5, 2000, 0, 3]
  306. >>> plt.grid(True, color='0.7', linestyle='-', which='both', axis='both')
  307. >>> plt.show()
  308. """
  309. start = asanyarray(start)
  310. stop = asanyarray(stop)
  311. if _nx.any(start == 0) or _nx.any(stop == 0):
  312. raise ValueError('Geometric sequence cannot include zero')
  313. dt = result_type(start, stop, float(num), _nx.zeros((), dtype))
  314. if dtype is None:
  315. dtype = dt
  316. else:
  317. # complex to dtype('complex128'), for instance
  318. dtype = _nx.dtype(dtype)
  319. # Promote both arguments to the same dtype in case, for instance, one is
  320. # complex and another is negative and log would produce NaN otherwise.
  321. # Copy since we may change things in-place further down.
  322. start = start.astype(dt, copy=True)
  323. stop = stop.astype(dt, copy=True)
  324. out_sign = _nx.ones(_nx.broadcast(start, stop).shape, dt)
  325. # Avoid negligible real or imaginary parts in output by rotating to
  326. # positive real, calculating, then undoing rotation
  327. if _nx.issubdtype(dt, _nx.complexfloating):
  328. all_imag = (start.real == 0.) & (stop.real == 0.)
  329. if _nx.any(all_imag):
  330. start[all_imag] = start[all_imag].imag
  331. stop[all_imag] = stop[all_imag].imag
  332. out_sign[all_imag] = 1j
  333. both_negative = (_nx.sign(start) == -1) & (_nx.sign(stop) == -1)
  334. if _nx.any(both_negative):
  335. _nx.negative(start, out=start, where=both_negative)
  336. _nx.negative(stop, out=stop, where=both_negative)
  337. _nx.negative(out_sign, out=out_sign, where=both_negative)
  338. log_start = _nx.log10(start)
  339. log_stop = _nx.log10(stop)
  340. result = out_sign * logspace(log_start, log_stop, num=num,
  341. endpoint=endpoint, base=10.0, dtype=dtype)
  342. if axis != 0:
  343. result = _nx.moveaxis(result, 0, axis)
  344. return result.astype(dtype, copy=False)
  345. def _needs_add_docstring(obj):
  346. """
  347. Returns true if the only way to set the docstring of `obj` from python is
  348. via add_docstring.
  349. This function errs on the side of being overly conservative.
  350. """
  351. Py_TPFLAGS_HEAPTYPE = 1 << 9
  352. if isinstance(obj, (types.FunctionType, types.MethodType, property)):
  353. return False
  354. if isinstance(obj, type) and obj.__flags__ & Py_TPFLAGS_HEAPTYPE:
  355. return False
  356. return True
  357. def _add_docstring(obj, doc, warn_on_python):
  358. if warn_on_python and not _needs_add_docstring(obj):
  359. warnings.warn(
  360. "add_newdoc was used on a pure-python object {}. "
  361. "Prefer to attach it directly to the source."
  362. .format(obj),
  363. UserWarning,
  364. stacklevel=3)
  365. try:
  366. add_docstring(obj, doc)
  367. except Exception:
  368. pass
  369. def add_newdoc(place, obj, doc, warn_on_python=True):
  370. """
  371. Add documentation to an existing object, typically one defined in C
  372. The purpose is to allow easier editing of the docstrings without requiring
  373. a re-compile. This exists primarily for internal use within numpy itself.
  374. Parameters
  375. ----------
  376. place : str
  377. The absolute name of the module to import from
  378. obj : str
  379. The name of the object to add documentation to, typically a class or
  380. function name
  381. doc : {str, Tuple[str, str], List[Tuple[str, str]]}
  382. If a string, the documentation to apply to `obj`
  383. If a tuple, then the first element is interpreted as an attribute of
  384. `obj` and the second as the docstring to apply - ``(method, docstring)``
  385. If a list, then each element of the list should be a tuple of length
  386. two - ``[(method1, docstring1), (method2, docstring2), ...]``
  387. warn_on_python : bool
  388. If True, the default, emit `UserWarning` if this is used to attach
  389. documentation to a pure-python object.
  390. Notes
  391. -----
  392. This routine never raises an error if the docstring can't be written, but
  393. will raise an error if the object being documented does not exist.
  394. This routine cannot modify read-only docstrings, as appear
  395. in new-style classes or built-in functions. Because this
  396. routine never raises an error the caller must check manually
  397. that the docstrings were changed.
  398. Since this function grabs the ``char *`` from a c-level str object and puts
  399. it into the ``tp_doc`` slot of the type of `obj`, it violates a number of
  400. C-API best-practices, by:
  401. - modifying a `PyTypeObject` after calling `PyType_Ready`
  402. - calling `Py_INCREF` on the str and losing the reference, so the str
  403. will never be released
  404. If possible it should be avoided.
  405. """
  406. new = getattr(__import__(place, globals(), {}, [obj]), obj)
  407. if isinstance(doc, str):
  408. _add_docstring(new, doc.strip(), warn_on_python)
  409. elif isinstance(doc, tuple):
  410. attr, docstring = doc
  411. _add_docstring(getattr(new, attr), docstring.strip(), warn_on_python)
  412. elif isinstance(doc, list):
  413. for attr, docstring in doc:
  414. _add_docstring(getattr(new, attr), docstring.strip(), warn_on_python)