constants.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. // Copyright 2008-2016 Conrad Sanderson (http://conradsanderson.id.au)
  2. // Copyright 2008-2016 National ICT Australia (NICTA)
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // ------------------------------------------------------------------------
  15. //! \addtogroup constants
  16. //! @{
  17. namespace priv
  18. {
  19. class Datum_helper
  20. {
  21. public:
  22. template<typename eT>
  23. static
  24. typename arma_real_only<eT>::result
  25. nan(typename arma_real_only<eT>::result* junk = 0)
  26. {
  27. arma_ignore(junk);
  28. if(std::numeric_limits<eT>::has_quiet_NaN)
  29. {
  30. return std::numeric_limits<eT>::quiet_NaN();
  31. }
  32. else
  33. {
  34. return eT(0);
  35. }
  36. }
  37. template<typename eT>
  38. static
  39. typename arma_cx_only<eT>::result
  40. nan(typename arma_cx_only<eT>::result* junk = 0)
  41. {
  42. arma_ignore(junk);
  43. typedef typename get_pod_type<eT>::result T;
  44. return eT( Datum_helper::nan<T>(), Datum_helper::nan<T>() );
  45. }
  46. template<typename eT>
  47. static
  48. typename arma_integral_only<eT>::result
  49. nan(typename arma_integral_only<eT>::result* junk = 0)
  50. {
  51. arma_ignore(junk);
  52. return eT(0);
  53. }
  54. template<typename eT>
  55. static
  56. typename arma_real_only<eT>::result
  57. inf(typename arma_real_only<eT>::result* junk = 0)
  58. {
  59. arma_ignore(junk);
  60. if(std::numeric_limits<eT>::has_infinity)
  61. {
  62. return std::numeric_limits<eT>::infinity();
  63. }
  64. else
  65. {
  66. return std::numeric_limits<eT>::max();
  67. }
  68. }
  69. template<typename eT>
  70. static
  71. typename arma_cx_only<eT>::result
  72. inf(typename arma_cx_only<eT>::result* junk = 0)
  73. {
  74. arma_ignore(junk);
  75. typedef typename get_pod_type<eT>::result T;
  76. return eT( Datum_helper::inf<T>(), Datum_helper::inf<T>() );
  77. }
  78. template<typename eT>
  79. static
  80. typename arma_integral_only<eT>::result
  81. inf(typename arma_integral_only<eT>::result* junk = 0)
  82. {
  83. arma_ignore(junk);
  84. return std::numeric_limits<eT>::max();
  85. }
  86. };
  87. }
  88. //! various constants.
  89. //! Physical constants taken from NIST 2018 CODATA values, and some from WolframAlpha (values provided as of 2009-06-23)
  90. //! http://physics.nist.gov/cuu/Constants
  91. //! http://www.wolframalpha.com
  92. //! See also http://en.wikipedia.org/wiki/Physical_constant
  93. template<typename eT>
  94. class Datum
  95. {
  96. public:
  97. static const eT pi; //!< ratio of any circle's circumference to its diameter
  98. static const eT e; //!< base of the natural logarithm
  99. static const eT euler; //!< Euler's constant, aka Euler-Mascheroni constant
  100. static const eT gratio; //!< golden ratio
  101. static const eT sqrt2; //!< square root of 2
  102. static const eT sqrt2pi; //!< square root of 2*pi
  103. static const eT log_sqrt2pi; //!< log of square root of 2*pi
  104. static const eT eps; //!< the difference between 1 and the least value greater than 1 that is representable
  105. static const eT log_min; //!< log of the minimum representable value
  106. static const eT log_max; //!< log of the maximum representable value
  107. static const eT nan; //!< "not a number"
  108. static const eT inf; //!< infinity
  109. //
  110. static const eT m_u; //!< atomic mass constant (in kg)
  111. static const eT N_A; //!< Avogadro constant
  112. static const eT k; //!< Boltzmann constant (in joules per kelvin)
  113. static const eT k_evk; //!< Boltzmann constant (in eV/K)
  114. static const eT a_0; //!< Bohr radius (in meters)
  115. static const eT mu_B; //!< Bohr magneton
  116. static const eT Z_0; //!< characteristic impedance of vacuum (in ohms)
  117. static const eT G_0; //!< conductance quantum (in siemens)
  118. static const eT k_e; //!< Coulomb's constant (in meters per farad)
  119. static const eT eps_0; //!< electric constant (in farads per meter)
  120. static const eT m_e; //!< electron mass (in kg)
  121. static const eT eV; //!< electron volt (in joules)
  122. static const eT ec; //!< elementary charge (in coulombs)
  123. static const eT F; //!< Faraday constant (in coulombs)
  124. static const eT alpha; //!< fine-structure constant
  125. static const eT alpha_inv; //!< inverse fine-structure constant
  126. static const eT K_J; //!< Josephson constant
  127. static const eT mu_0; //!< magnetic constant (in henries per meter)
  128. static const eT phi_0; //!< magnetic flux quantum (in webers)
  129. static const eT R; //!< molar gas constant (in joules per mole kelvin)
  130. static const eT G; //!< Newtonian constant of gravitation (in newton square meters per kilogram squared)
  131. static const eT h; //!< Planck constant (in joule seconds)
  132. static const eT h_bar; //!< Planck constant over 2 pi, aka reduced Planck constant (in joule seconds)
  133. static const eT m_p; //!< proton mass (in kg)
  134. static const eT R_inf; //!< Rydberg constant (in reciprocal meters)
  135. static const eT c_0; //!< speed of light in vacuum (in meters per second)
  136. static const eT sigma; //!< Stefan-Boltzmann constant
  137. static const eT R_k; //!< von Klitzing constant (in ohms)
  138. static const eT b; //!< Wien wavelength displacement law constant
  139. };
  140. // the long lengths of the constants are for future support of "long double"
  141. // and any smart compiler that does high-precision computation at compile-time
  142. template<typename eT> const eT Datum<eT>::pi = eT(3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679);
  143. template<typename eT> const eT Datum<eT>::e = eT(2.7182818284590452353602874713526624977572470936999595749669676277240766303535475945713821785251664274);
  144. template<typename eT> const eT Datum<eT>::euler = eT(0.5772156649015328606065120900824024310421593359399235988057672348848677267776646709369470632917467495);
  145. template<typename eT> const eT Datum<eT>::gratio = eT(1.6180339887498948482045868343656381177203091798057628621354486227052604628189024497072072041893911374);
  146. template<typename eT> const eT Datum<eT>::sqrt2 = eT(1.4142135623730950488016887242096980785696718753769480731766797379907324784621070388503875343276415727);
  147. template<typename eT> const eT Datum<eT>::sqrt2pi = eT(2.5066282746310005024157652848110452530069867406099383166299235763422936546078419749465958383780572661);
  148. template<typename eT> const eT Datum<eT>::log_sqrt2pi = eT(0.9189385332046727417803297364056176398613974736377834128171515404827656959272603976947432986359541976);
  149. template<typename eT> const eT Datum<eT>::eps = std::numeric_limits<eT>::epsilon();
  150. template<typename eT> const eT Datum<eT>::log_min = std::log(std::numeric_limits<eT>::min());
  151. template<typename eT> const eT Datum<eT>::log_max = std::log(std::numeric_limits<eT>::max());
  152. template<typename eT> const eT Datum<eT>::nan = priv::Datum_helper::nan<eT>();
  153. template<typename eT> const eT Datum<eT>::inf = priv::Datum_helper::inf<eT>();
  154. template<typename eT> const eT Datum<eT>::m_u = eT(1.66053906660e-27);
  155. template<typename eT> const eT Datum<eT>::N_A = eT(6.02214076e23);
  156. template<typename eT> const eT Datum<eT>::k = eT(1.380649e-23);
  157. template<typename eT> const eT Datum<eT>::k_evk = eT(8.617333262e-5);
  158. template<typename eT> const eT Datum<eT>::a_0 = eT(5.29177210903e-11);
  159. template<typename eT> const eT Datum<eT>::mu_B = eT(9.2740100783e-24);
  160. template<typename eT> const eT Datum<eT>::Z_0 = eT(376.730313668);
  161. template<typename eT> const eT Datum<eT>::G_0 = eT(7.748091729e-5);
  162. template<typename eT> const eT Datum<eT>::k_e = eT(8.9875517923e9);
  163. template<typename eT> const eT Datum<eT>::eps_0 = eT(8.8541878128e-12);
  164. template<typename eT> const eT Datum<eT>::m_e = eT(9.1093837015e-31);
  165. template<typename eT> const eT Datum<eT>::eV = eT(1.602176634e-19);
  166. template<typename eT> const eT Datum<eT>::ec = eT(1.602176634e-19);
  167. template<typename eT> const eT Datum<eT>::F = eT(96485.33212);
  168. template<typename eT> const eT Datum<eT>::alpha = eT(7.2973525693e-3);
  169. template<typename eT> const eT Datum<eT>::alpha_inv = eT(137.035999084);
  170. template<typename eT> const eT Datum<eT>::K_J = eT(483597.8484e9);
  171. template<typename eT> const eT Datum<eT>::mu_0 = eT(1.25663706212e-6);
  172. template<typename eT> const eT Datum<eT>::phi_0 = eT(2.067833848e-15);
  173. template<typename eT> const eT Datum<eT>::R = eT(8.314462618);
  174. template<typename eT> const eT Datum<eT>::G = eT(6.67430e-11);
  175. template<typename eT> const eT Datum<eT>::h = eT(6.62607015e-34);
  176. template<typename eT> const eT Datum<eT>::h_bar = eT(1.054571817e-34);
  177. template<typename eT> const eT Datum<eT>::m_p = eT(1.67262192369e-27);
  178. template<typename eT> const eT Datum<eT>::R_inf = eT(10973731.568160);
  179. template<typename eT> const eT Datum<eT>::c_0 = eT(299792458.0);
  180. template<typename eT> const eT Datum<eT>::sigma = eT(5.670374419e-8);
  181. template<typename eT> const eT Datum<eT>::R_k = eT(25812.80745);
  182. template<typename eT> const eT Datum<eT>::b = eT(2.897771955e-3);
  183. typedef Datum<float> fdatum;
  184. typedef Datum<double> datum;
  185. namespace priv
  186. {
  187. template<typename eT>
  188. static
  189. arma_inline
  190. typename arma_real_only<eT>::result
  191. most_neg(typename arma_real_only<eT>::result* junk = 0)
  192. {
  193. arma_ignore(junk);
  194. if(std::numeric_limits<eT>::has_infinity)
  195. {
  196. return -(std::numeric_limits<eT>::infinity());
  197. }
  198. else
  199. {
  200. return -(std::numeric_limits<eT>::max());
  201. }
  202. }
  203. template<typename eT>
  204. static
  205. arma_inline
  206. typename arma_integral_only<eT>::result
  207. most_neg(typename arma_integral_only<eT>::result* junk = 0)
  208. {
  209. arma_ignore(junk);
  210. return std::numeric_limits<eT>::min();
  211. }
  212. template<typename eT>
  213. static
  214. arma_inline
  215. typename arma_real_only<eT>::result
  216. most_pos(typename arma_real_only<eT>::result* junk = 0)
  217. {
  218. arma_ignore(junk);
  219. if(std::numeric_limits<eT>::has_infinity)
  220. {
  221. return std::numeric_limits<eT>::infinity();
  222. }
  223. else
  224. {
  225. return std::numeric_limits<eT>::max();
  226. }
  227. }
  228. template<typename eT>
  229. static
  230. arma_inline
  231. typename arma_integral_only<eT>::result
  232. most_pos(typename arma_integral_only<eT>::result* junk = 0)
  233. {
  234. arma_ignore(junk);
  235. return std::numeric_limits<eT>::max();
  236. }
  237. }
  238. //! @}