constants.py 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. """
  2. =========
  3. Constants
  4. =========
  5. .. currentmodule:: numpy
  6. NumPy includes several constants:
  7. %(constant_list)s
  8. """
  9. #
  10. # Note: the docstring is autogenerated.
  11. #
  12. import re
  13. import textwrap
  14. # Maintain same format as in numpy.add_newdocs
  15. constants = []
  16. def add_newdoc(module, name, doc):
  17. constants.append((name, doc))
  18. add_newdoc('numpy', 'pi',
  19. """
  20. ``pi = 3.1415926535897932384626433...``
  21. References
  22. ----------
  23. https://en.wikipedia.org/wiki/Pi
  24. """)
  25. add_newdoc('numpy', 'e',
  26. """
  27. Euler's constant, base of natural logarithms, Napier's constant.
  28. ``e = 2.71828182845904523536028747135266249775724709369995...``
  29. See Also
  30. --------
  31. exp : Exponential function
  32. log : Natural logarithm
  33. References
  34. ----------
  35. https://en.wikipedia.org/wiki/E_%28mathematical_constant%29
  36. """)
  37. add_newdoc('numpy', 'euler_gamma',
  38. """
  39. ``γ = 0.5772156649015328606065120900824024310421...``
  40. References
  41. ----------
  42. https://en.wikipedia.org/wiki/Euler-Mascheroni_constant
  43. """)
  44. add_newdoc('numpy', 'inf',
  45. """
  46. IEEE 754 floating point representation of (positive) infinity.
  47. Returns
  48. -------
  49. y : float
  50. A floating point representation of positive infinity.
  51. See Also
  52. --------
  53. isinf : Shows which elements are positive or negative infinity
  54. isposinf : Shows which elements are positive infinity
  55. isneginf : Shows which elements are negative infinity
  56. isnan : Shows which elements are Not a Number
  57. isfinite : Shows which elements are finite (not one of Not a Number,
  58. positive infinity and negative infinity)
  59. Notes
  60. -----
  61. NumPy uses the IEEE Standard for Binary Floating-Point for Arithmetic
  62. (IEEE 754). This means that Not a Number is not equivalent to infinity.
  63. Also that positive infinity is not equivalent to negative infinity. But
  64. infinity is equivalent to positive infinity.
  65. `Inf`, `Infinity`, `PINF` and `infty` are aliases for `inf`.
  66. Examples
  67. --------
  68. >>> np.inf
  69. inf
  70. >>> np.array([1]) / 0.
  71. array([ Inf])
  72. """)
  73. add_newdoc('numpy', 'nan',
  74. """
  75. IEEE 754 floating point representation of Not a Number (NaN).
  76. Returns
  77. -------
  78. y : A floating point representation of Not a Number.
  79. See Also
  80. --------
  81. isnan : Shows which elements are Not a Number.
  82. isfinite : Shows which elements are finite (not one of
  83. Not a Number, positive infinity and negative infinity)
  84. Notes
  85. -----
  86. NumPy uses the IEEE Standard for Binary Floating-Point for Arithmetic
  87. (IEEE 754). This means that Not a Number is not equivalent to infinity.
  88. `NaN` and `NAN` are aliases of `nan`.
  89. Examples
  90. --------
  91. >>> np.nan
  92. nan
  93. >>> np.log(-1)
  94. nan
  95. >>> np.log([-1, 1, 2])
  96. array([ NaN, 0. , 0.69314718])
  97. """)
  98. add_newdoc('numpy', 'newaxis',
  99. """
  100. A convenient alias for None, useful for indexing arrays.
  101. Examples
  102. --------
  103. >>> newaxis is None
  104. True
  105. >>> x = np.arange(3)
  106. >>> x
  107. array([0, 1, 2])
  108. >>> x[:, newaxis]
  109. array([[0],
  110. [1],
  111. [2]])
  112. >>> x[:, newaxis, newaxis]
  113. array([[[0]],
  114. [[1]],
  115. [[2]]])
  116. >>> x[:, newaxis] * x
  117. array([[0, 0, 0],
  118. [0, 1, 2],
  119. [0, 2, 4]])
  120. Outer product, same as ``outer(x, y)``:
  121. >>> y = np.arange(3, 6)
  122. >>> x[:, newaxis] * y
  123. array([[ 0, 0, 0],
  124. [ 3, 4, 5],
  125. [ 6, 8, 10]])
  126. ``x[newaxis, :]`` is equivalent to ``x[newaxis]`` and ``x[None]``:
  127. >>> x[newaxis, :].shape
  128. (1, 3)
  129. >>> x[newaxis].shape
  130. (1, 3)
  131. >>> x[None].shape
  132. (1, 3)
  133. >>> x[:, newaxis].shape
  134. (3, 1)
  135. """)
  136. add_newdoc('numpy', 'NZERO',
  137. """
  138. IEEE 754 floating point representation of negative zero.
  139. Returns
  140. -------
  141. y : float
  142. A floating point representation of negative zero.
  143. See Also
  144. --------
  145. PZERO : Defines positive zero.
  146. isinf : Shows which elements are positive or negative infinity.
  147. isposinf : Shows which elements are positive infinity.
  148. isneginf : Shows which elements are negative infinity.
  149. isnan : Shows which elements are Not a Number.
  150. isfinite : Shows which elements are finite - not one of
  151. Not a Number, positive infinity and negative infinity.
  152. Notes
  153. -----
  154. NumPy uses the IEEE Standard for Binary Floating-Point for Arithmetic
  155. (IEEE 754). Negative zero is considered to be a finite number.
  156. Examples
  157. --------
  158. >>> np.NZERO
  159. -0.0
  160. >>> np.PZERO
  161. 0.0
  162. >>> np.isfinite([np.NZERO])
  163. array([ True])
  164. >>> np.isnan([np.NZERO])
  165. array([False])
  166. >>> np.isinf([np.NZERO])
  167. array([False])
  168. """)
  169. add_newdoc('numpy', 'PZERO',
  170. """
  171. IEEE 754 floating point representation of positive zero.
  172. Returns
  173. -------
  174. y : float
  175. A floating point representation of positive zero.
  176. See Also
  177. --------
  178. NZERO : Defines negative zero.
  179. isinf : Shows which elements are positive or negative infinity.
  180. isposinf : Shows which elements are positive infinity.
  181. isneginf : Shows which elements are negative infinity.
  182. isnan : Shows which elements are Not a Number.
  183. isfinite : Shows which elements are finite - not one of
  184. Not a Number, positive infinity and negative infinity.
  185. Notes
  186. -----
  187. NumPy uses the IEEE Standard for Binary Floating-Point for Arithmetic
  188. (IEEE 754). Positive zero is considered to be a finite number.
  189. Examples
  190. --------
  191. >>> np.PZERO
  192. 0.0
  193. >>> np.NZERO
  194. -0.0
  195. >>> np.isfinite([np.PZERO])
  196. array([ True])
  197. >>> np.isnan([np.PZERO])
  198. array([False])
  199. >>> np.isinf([np.PZERO])
  200. array([False])
  201. """)
  202. add_newdoc('numpy', 'NAN',
  203. """
  204. IEEE 754 floating point representation of Not a Number (NaN).
  205. `NaN` and `NAN` are equivalent definitions of `nan`. Please use
  206. `nan` instead of `NAN`.
  207. See Also
  208. --------
  209. nan
  210. """)
  211. add_newdoc('numpy', 'NaN',
  212. """
  213. IEEE 754 floating point representation of Not a Number (NaN).
  214. `NaN` and `NAN` are equivalent definitions of `nan`. Please use
  215. `nan` instead of `NaN`.
  216. See Also
  217. --------
  218. nan
  219. """)
  220. add_newdoc('numpy', 'NINF',
  221. """
  222. IEEE 754 floating point representation of negative infinity.
  223. Returns
  224. -------
  225. y : float
  226. A floating point representation of negative infinity.
  227. See Also
  228. --------
  229. isinf : Shows which elements are positive or negative infinity
  230. isposinf : Shows which elements are positive infinity
  231. isneginf : Shows which elements are negative infinity
  232. isnan : Shows which elements are Not a Number
  233. isfinite : Shows which elements are finite (not one of Not a Number,
  234. positive infinity and negative infinity)
  235. Notes
  236. -----
  237. NumPy uses the IEEE Standard for Binary Floating-Point for Arithmetic
  238. (IEEE 754). This means that Not a Number is not equivalent to infinity.
  239. Also that positive infinity is not equivalent to negative infinity. But
  240. infinity is equivalent to positive infinity.
  241. Examples
  242. --------
  243. >>> np.NINF
  244. -inf
  245. >>> np.log(0)
  246. -inf
  247. """)
  248. add_newdoc('numpy', 'PINF',
  249. """
  250. IEEE 754 floating point representation of (positive) infinity.
  251. Use `inf` because `Inf`, `Infinity`, `PINF` and `infty` are aliases for
  252. `inf`. For more details, see `inf`.
  253. See Also
  254. --------
  255. inf
  256. """)
  257. add_newdoc('numpy', 'infty',
  258. """
  259. IEEE 754 floating point representation of (positive) infinity.
  260. Use `inf` because `Inf`, `Infinity`, `PINF` and `infty` are aliases for
  261. `inf`. For more details, see `inf`.
  262. See Also
  263. --------
  264. inf
  265. """)
  266. add_newdoc('numpy', 'Inf',
  267. """
  268. IEEE 754 floating point representation of (positive) infinity.
  269. Use `inf` because `Inf`, `Infinity`, `PINF` and `infty` are aliases for
  270. `inf`. For more details, see `inf`.
  271. See Also
  272. --------
  273. inf
  274. """)
  275. add_newdoc('numpy', 'Infinity',
  276. """
  277. IEEE 754 floating point representation of (positive) infinity.
  278. Use `inf` because `Inf`, `Infinity`, `PINF` and `infty` are aliases for
  279. `inf`. For more details, see `inf`.
  280. See Also
  281. --------
  282. inf
  283. """)
  284. if __doc__:
  285. constants_str = []
  286. constants.sort()
  287. for name, doc in constants:
  288. s = textwrap.dedent(doc).replace("\n", "\n ")
  289. # Replace sections by rubrics
  290. lines = s.split("\n")
  291. new_lines = []
  292. for line in lines:
  293. m = re.match(r'^(\s+)[-=]+\s*$', line)
  294. if m and new_lines:
  295. prev = textwrap.dedent(new_lines.pop())
  296. new_lines.append('%s.. rubric:: %s' % (m.group(1), prev))
  297. new_lines.append('')
  298. else:
  299. new_lines.append(line)
  300. s = "\n".join(new_lines)
  301. # Done.
  302. constants_str.append(""".. data:: %s\n %s""" % (name, s))
  303. constants_str = "\n".join(constants_str)
  304. __doc__ = __doc__ % dict(constant_list=constants_str)
  305. del constants_str, name, doc
  306. del line, lines, new_lines, m, s, prev
  307. del constants, add_newdoc