6

Does any Python library offer a function that implements the "fast inverse square root" algorithm described in following link? http://en.wikipedia.org/wiki/Fast_inverse_square_root Perhaps numpy/SciPy?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Andreas Gschossmann
  • 669
  • 1
  • 6
  • 21
  • 11
    If the speed of an operation like inverse sqrt is that important to you, maybe Python isn't the right language to use. – interjay May 22 '13 at 22:04
  • 1
    Have you run into a bottleneck that is causing you to ask this? – Steven Rumbalski May 22 '13 at 22:13
  • 1
    While it would be easy to implement the algorithm in python, it is only 4 times faster than doing the floating point calculation. Since the floating point operation from `(x**-0.5)` is done in 1 operation of python code, and the actual square root will be calculated using the C math lib, any speed advantage of the algorithm will be lost if it is implemented in python. C executes ~10 times faster than python, so you'll be giving up a 10x speed boost to get a 4x speed boos. If you really need the algorithm, implement it in C and import it into python. – Perkins May 22 '13 at 22:25
  • 1
    Apart from anything else, the body of the C function in the Wikipedia article is literally ten lines of code. Typing this SO question took more effort than it would have to port it yourself. – Cairnarvon May 22 '13 at 22:46
  • 3
    @ Interjay: I use python as a TestBench for the development of a filter. Later on the filter will run on C/C++ on a microcontroller. I choosed python as a replacement for octave since it is a rapid way for engineering the filter algorythms. Python is not slowly and very compareable to octave since in the background there are C/C++ libraries. @ Steven Rumbalski: No actually not. I just have to calculate the inverse square root several times and since the described way is the fastest and most elegant way to do this I thought there might be a python command to which is using this. – Andreas Gschossmann May 24 '13 at 19:42
  • 2
    @ Perkins: That is what I have been doing until now. I just thought there already might be a command, that runs exactly this algorithm in C/C++ behind the scenes. @ Cairnarvon: Of course it is no big deal. Actually that is what I have been doing till now. But if there already was one command in python, it would have been more convenient to use it. – Andreas Gschossmann May 24 '13 at 19:43
  • 1
    Thank you all for your answers :). – Andreas Gschossmann May 24 '13 at 19:44
  • Here is a detailed blog post with three different implementations of the inverse square root algorithm in python, and benchmarks to compare their time of execution with `x ** -0.5`: https://github.com/ajcr/ajcr.github.io/blob/master/_posts/2016-04-01-fast-inverse-square-root-python.md – Stef Nov 27 '20 at 12:00
  • Jax and tensorflow both have RSQRT functions that will use GPU or other hardware support. – John Henckel Mar 02 '22 at 18:32

2 Answers2

8

You can do this in Python, but not in a very direct way (ie. lots of function calls), so doing x**-.5 will likely be much faster.

So it might be an interesting exercise, but not practical at all.

John La Rooy
  • 295,403
  • 53
  • 369
  • 502
  • https://github.com/ajcr/ajcr.github.io/blob/master/_posts/2016-04-01-fast-inverse-square-root-python.md#implementing-the-method-in-python – abhiTronix Jan 04 '21 at 15:51
0

You can already do the inverse square root just do x**-1/2 so you don't need to make a complicated function to do it and its probably faster to do it this way anyway and its much easier

and like interjay said if you're really worried about the speed of something like that you probably should use a faster more exact language to get a faster way

the only library that maybe has it that i found is mpmath

Good Luck!!

Serial
  • 7,925
  • 13
  • 52
  • 71
  • 2
    Thank you. Yeah I am thinking about developing the time relevant parts in C/C++ from the first place since this is just a TestBench for a filter (with some gui elements for visualisation). Later on it will run in C/C++ on a microcontroller anyway. – Andreas Gschossmann May 24 '13 at 19:48