index.html 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <!doctype html>
  2. <meta charset="utf-8">
  3. <script src="https://distill.pub/template.v1.js"></script>
  4. <link rel="stylesheet" href="style.css">
  5. <script defer src="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.9.0/katex.min.js" integrity="sha384-jmxIlussZWB7qCuB+PgKG1uLjjxbVVIayPJwi6cG6Zb4YKq0JIw+OMnkkEC7kYCq" crossorigin="anonymous"></script>
  6. <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.9.0/katex.min.css" integrity="sha384-TEMocfGvRuD1rIAacqrknm5BQZ7W7uWitoih+jMNFXQIbNl16bO8OZmylH/Vi/Ei" crossorigin="anonymous">
  7. <script defer src="bundle.js"></script>
  8. <script type="text/front-matter">
  9. title: "WebAssembly Cephes - mathematical special functions in JavaScript"
  10. description: >
  11. Using Emscripten to compile a WebAssembly version of Cephes, we finally
  12. have a fast and correct implementations of most mathematical special
  13. functions.
  14. authors:
  15. - Andreas Madsen: https://github.com/AndreasMadsen
  16. affiliations:
  17. - nearForm: https://nearform.com
  18. </script>
  19. <dt-article>
  20. <h1>WebAssembly Cephes - mathematical special functions in JavaScript</h1>
  21. <dt-byline></dt-byline>
  22. </dt-article>
  23. <dt-article>
  24. <h2>Introduction</h2>
  25. <p>
  26. A lot of the mathematical implementations we do at NearForm depends on
  27. so-called <a href="https://en.wikipedia.org/wiki/Special_functions">
  28. mathematical special functions</a>. There is no clearly defined
  29. criteria for what a special function is, but suffice to say they are
  30. functions that often turn up in mathematics and most of them are
  31. notoriously hard to compute. Often they are defined as the solution to
  32. a well-posed problem. However the solution itself is not easy to compute.
  33. </p>
  34. <p>
  35. JavaScript is severely lacking in implementations of these special functions,
  36. especially trusted implementations. If you are lucky, you can find
  37. a basic implementation in npm. However, the implementations often have
  38. numerous undocumented issues and only work somewhat well for a
  39. small and undocumented input range.
  40. </p>
  41. <p>
  42. In R these functions are often built-in, and in Python,
  43. <a href="https://www.scipy.org/">SciPy</a> implements these functions.
  44. Deep within the <a href="https://github.com/scipy/scipy/tree/master/scipy/special/cephes">
  45. SciPy source code</a>, we
  46. find the <a href="http://www.netlib.org/cephes/">Cephes library</a>
  47. which is what implements many of these functions.
  48. </p>
  49. <p>
  50. Cephes is a pure C library, that doesn't depend on anything, including
  51. the <a href="https://en.wikipedia.org/wiki/C_standard_library">
  52. C standard library</a>. This makes it ideal to be compiled to WebAssembly
  53. using <a href="http://emscripten.org">emscripten</a>, as it means there won't be calls between JavaScript
  54. and WebAssembly which could otherwise cause performance issues.
  55. </p>
  56. <p>
  57. We have released the WebAssembly version of Cephes on npm, see
  58. <a href="https://www.npmjs.com/package/cephes">https://www.npmjs.com/package/cephes</a>.
  59. So all you need is to install it with <code>npm install cephes</code>.
  60. </p>
  61. <p>
  62. Using our Cephes module is very simple, as we have wrapped each WebAssembly
  63. function in a custom JavaScript function that takes care of all the
  64. memory management.
  65. </p>
  66. <dt-code block language="js">
  67. const cephes = require('cephes');
  68. const value = cephes.gamma(2);
  69. </dt-code>
  70. </dt-article>
  71. <dt-article>
  72. <h2>Examples</h2>
  73. <p>
  74. Below are just a few examples of the 125 available functions. You can see all of them
  75. in our <a href="https://github.com/nearform/node-cephes">README file</a>.
  76. </p>
  77. <p>
  78. All of the values that make up the graph are calculated dynamically in your
  79. browser. This gives you an idea of just how fast Cephes in WebAssembly is.
  80. </p>
  81. <figure>
  82. <div id="ar-walkthrough">
  83. <div class="ar-walkthrough-step">
  84. <div class="ar-walkthrough-number">1</div>
  85. <p class="ar-walkthrough-text">
  86. The <a href="https://en.wikipedia.org/wiki/Bessel_function">Bessel function</a> is the solution to the equation
  87. <math-latex display-mode latex="x^{2}{\frac {d^{2}y}{dx^{2}}}+x{\frac {dy}{dx}}+\left(x^{2}-\alpha ^{2}\right)y=0"></math-latex>
  88. The Bessel function is typically used in partial differential equations,
  89. where <math-latex latex="\alpha"></math-latex> will often be a fixed integer.
  90. </p>
  91. </div>
  92. <div class="ar-walkthrough-step">
  93. <div class="ar-walkthrough-number">2</div>
  94. <p class="ar-walkthrough-text">
  95. The <a href="https://en.wikipedia.org/wiki/Gamma">Gamma function</a> <math-latex latex="\Gamma(x)"></math-latex>
  96. is used in many probability distributions.
  97. <math-latex display-mode latex="\Gamma (x)=\int _{0}^{\infty }y^{x-1}e^{-y}\,dy"></math-latex>
  98. As its value increases
  99. very quickly, it is often the <math-latex latex="\ln(\Gamma(x))"></math-latex>
  100. function that is used in practice. Cephes has dedicated
  101. implementations for both. The DiGamma function
  102. <math-latex latex="\psi(x)"></math-latex>, is its derivative and
  103. also appear frequently in probability theory.
  104. <math-latex display-mode latex="\psi (x)={\frac {d}{dx}}\ln {\big (}\Gamma (x){\big )}={\frac {\Gamma '(x)}{\Gamma (x)}}"></math-latex>
  105. </p>
  106. </div>
  107. <div class="ar-walkthrough-step">
  108. <div class="ar-walkthrough-number">3</div>
  109. <p class="ar-walkthrough-text">
  110. The <a href="https://en.wikipedia.org/wiki/Beta_distribution">beta distribution</a> is central in a lot of Bayesian statistics.
  111. <math-latex display-mode latex="f_{\mathrm{B}}(x; \alpha, \beta) = \frac{x^{\alpha-1}(1-x)^{\beta-1}}{\mathrm{B}(\alpha,\beta)}"></math-latex>
  112. The cumulative distribution function is notoriously hard to compute,
  113. but this function is also implemented by Cephes.
  114. <math-latex display-mode latex="I_x(\alpha, \beta) = \frac{1}{\mathrm{B}(\alpha,\beta)} \int _{0}^{x}t^{a-1}\,(1-t)^{b-1}\,dt"></math-latex>
  115. </p>
  116. </div>
  117. <div class="ar-walkthrough-step">
  118. <div class="ar-walkthrough-number">4</div>
  119. <p class="ar-walkthrough-text">
  120. The <a href="https://en.wikipedia.org/wiki/Airy_function">Airy function</a> <math-latex latex="Ai(x)"></math-latex> is the solution to the differential equation
  121. <math-latex display-mode latex="\frac{d^2y}{dx^2} - xy = 0"></math-latex>
  122. It is often used in physics such as quantum mechanics. Cephes also
  123. defines it compimentary function <math-latex latex="Bi(x)"></math-latex>
  124. and their derivatives <math-latex latex="Ai'(x)"></math-latex> and
  125. <math-latex latex="Bi'(x)"></math-latex>.
  126. </p>
  127. </div>
  128. <div class="ar-walkthrough-pages">
  129. <span class="ar-walkthrough-page">1</span>
  130. <span class="ar-walkthrough-page">2</span>
  131. <span class="ar-walkthrough-page">3</span>
  132. <span class="ar-walkthrough-page">4</span>
  133. </div>
  134. </div>
  135. <div id="ar-line-graph"></div>
  136. <figcaption>
  137. <strong>Mathematical special functions:</strong> shows various mathematical
  138. special functions. Try clicking the page numbers 1, 2, 3, or 4 to change
  139. the function.
  140. </figcaption>
  141. </figure>
  142. </dt-article>
  143. <dt-article>
  144. <h2>Cephes as an Open Source project</h2>
  145. <p>
  146. Whilst Cephes lacks a general license, its author, Stephen L. Moshier, has kindly
  147. <a href="https://github.com/nearform/node-cephes/blob/master/LICENSE">
  148. granted us permission to license it </a> as
  149. <a href="https://tldrlegal.com/license/bsd-3-clause-license-(revised)">
  150. BSD-3-Clause</a>. Whilst the library continues to be maintained, it doesn't
  151. use some of the more modern approximation methods for many of the special functions.
  152. </p>
  153. <p>
  154. The good news is that R and SciPy
  155. <a href="https://qz.com/1270139/r-and-python-are-joining-forces-in-the-most-ambitious-crossover-event-of-the-year-for-programmers/">
  156. are working together on making a better Open Source library of these
  157. special functions</a>. But it will take a while before that library is
  158. ready and until then Cephes is by far the best choice.
  159. </p>
  160. </dt-article>
  161. <dt-article>
  162. <h2>Contact</h2>
  163. <p>
  164. The mathematical special functions have so many use cases. If you think
  165. your business could benefit from math, such as statistics, logistics, or
  166. something else. Then please feel free to contact us at
  167. <a href="https://www.nearform.com/contact/">https://www.nearform.com/contact/</a>.
  168. </p>
  169. </dt-article>
  170. <dt-appendix>
  171. </dt-appendix>
  172. <script type="text/bibliography">
  173. </script>