constants.py 9.0 KB

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