2

After reading this question, I'm wondering what happens under the hood when np.exp is called: what is the mathematical/numerical routine used to derive the values in the returned array? For example, I think that to compute np.sqrt(x), a solution in y to y ** 2 - x = 0 is found using Newton's method.

(np.exp's docstring does not state how this is done)

P. Camilleri
  • 12,664
  • 7
  • 41
  • 76
  • 2
    Generations worth of deception and trickery from numerical math went into optimizing stuff like this. Depending how deep of an answer you want, this might be a better fit for the math stackexchange. – timgeb Nov 21 '18 at 19:30
  • @timgeb: The math site might handle questions regarding how an exponent could be calculated, but questions about how some system actually calculates it aren't appropriate for there. – user2357112 Nov 21 '18 at 19:40
  • 2
    In most cases, the underlying implementation will be the C function `exp` in your platform's standard C math library. – Warren Weckesser Nov 21 '18 at 19:41
  • The exponential function is supported natively by the coprocessor. I doubt that Numpy implements it. –  Nov 21 '18 at 21:48
  • Numpy doesn't do basic mathematic operations on its own, therefor it depends to which cmath library numpy is linked during compilation. The implementation of each function can vary. In some edge-cases performance can also show a high variety, based on the linked cmath lib. eg. https://stackoverflow.com/q/53415531/4045774 – max9111 Nov 23 '18 at 13:56

1 Answers1

5

(Slightly updated to correct the original link line-set, and add a separate one for the originally-linked lines:) numpy binds to the C library implementation of exp, using a self-defined proxy if the appropriate-precision C99 variant is not available. Its performance will depend on the library's performance.

I am not an expert on numerical algorithms, but these two StackOverflow questions look like they have good answers: Efficient implementation of natural logarithm (ln) and exponentiation and Fastest Implementation of Exponential Function Using SSE. Note that the accepted answer to the first question (suggesting a simple Taylor expansion) is not used in practice; instead, as this answer suggests, we find polynomial expansions for particular ranges, and add range reduction and subsequent correction, in real implementations.

See also Python source code for math exponent function? and this paper by Sushant Sachdeva and Nisheeth K. Vishnoi. For more, see Google Search.

torek
  • 448,244
  • 59
  • 642
  • 775