Recognizing numbers

I was playing around with SymPy, a symbolic math package for Python, and ran across nsimplify. It takes a floating point number and tries to simplify it: as a fraction with a small denominator, square root of a small integer, an expression involving famous constants, etc.

For example, suppose some calculation returned 4.242640687119286 and you suspect there’s something special about that number. Here’s how you might test where it came from.

>>> from sympy import *
>>> nsimplify(4.242640687119286)
3*sqrt(2)

Maybe you do a calculation numerically, find a simple expression for the result, and that suggests an analytical solution.

I think a more common application of nsimplify might be to help you remember half-forgotten formulas. For example, maybe you’re rusty on your trig identities, but you remember that cos(π/6) is something special.

>>> nsimplify(cos(pi/6))
sqrt(3)/2

Or to take a more advanced example, suppose that you vaguely remember that the gamma function takes on recognizable values at half integer values, but you don’t quite remember how. Maybe something involving π or e. You can suggest that nsimplify include expressions with π and e in its search.

>>> nsimplify(gamma(3.5), constants=[pi, E])
15*sqrt(pi)/8

You can also give nsimplify a tolerance, asking it to find a simple representation within a neighborhood of the number. For example, here’s a way to find approximations to π.

>>> nsimplify(pi, tolerance=1e-5)
355/113

With a wider tolerance, it will return a simpler approximation.

>>> nsimplify(pi, tolerance=1e-2)
22/7

Finally, here’s higher precision approximation to π that isn’t exactly simple:

>>> nsimplify(pi, tolerance=1e-7)
exp(141/895 + sqrt(780631)/895)
Tagged with: ,
Posted in Math, Python
20 comments on “Recognizing numbers
  1. Ken Butler says:

    This is just quite unreasonably cool!

  2. Johnathan says:

    Obligatory XKCD reference: https://xkcd.com/1047/

  3. BobC says:

    Several of those are marks on my slipstick. Can’t get much simpler than that.

  4. Jan Van lent says:

    Maple has a similar function called identify.
    Also interesting are Plouffe’s inverter and integer relation algorithms.

  5. James Mills says:

    This is also really really cool! :) –JamesMills / prologic

  6. Bob says:

    This reminded me of the Inverse Symbolic Calculator: http://isc.carma.newcastle.edu.au/.

  7. Frank says:

    Ain’t sin(pi/6) == 1/2 ?
    You probably meant sin(pi/3)

  8. Joe Louderback says:

    Odd. On my calculator (and matching my trig knowledge) sin(pi/6) = 0.5, not sqrt(3)/2.

  9. Brian says:

    I have absolutely no use for such a thing.
    However, I’ve bookmarked it anyway.
    Quite interesting.

  10. Alex says:

    Doesn’t anybody find this slightly dangerous? I can imagine people finding a result numerically, and giving an analytical form just to make it look like they found it analytically. – Yes, there’s people who would do that.

  11. John says:

    Frank and Joe: Thanks. I meant to say cosine. I’ve corrected the post.

  12. Yomismo says:

    So cool! Thanks for the info!

  13. Alok says:

    wonderful. Never imagined that one day software will do this!! How fast are such calculations?

  14. John says:

    Alok: The calculations are quick. Most of the time you get a rational approximation, and algorithms for approximation with the smallest possible denominators are well known and fast. I don’t know how extensive it is in considering square roots etc.

  15. Tadashi says:

    Great post and great technique! Recently, I saw two clever uses of it (up to the CAS utilized) that is worth sharing:
    [1] http://blog.wolfram.com/2013/05/01/after-100-years-ramanujan-gap-filled/
    [2] http://blog.wolfram.com/2013/03/28/from-close-to-perfect-a-triangle-problem/

  16. Chris Kemp says:

    Nice find, John!
    @mrpaladin

  17. Pseudonym says:

    @Alex, I was thinking that myself. For some reason, I am reminded of the fine structure constant.

  18. Larry says:

    Such methods played a part in Euler’s solution of the famous Basel Problem: find the exact value of 1 + 1/2^2 + 1/3^2 + 1/4^2 + …
    He computed the value of the series to six decimal places, then somehow recognized that it seemed to be equal to pi^2 / 6. This was a clue which later led him to a proof that this was indeed the correct value of the series.

  19. nineright says:

    So cool! Thanks for sharing.

    PS, I find 0.7182818284590453(e-2) which is not well simplified(143656365691809/200000000000000),:)

  20. John says:

    nineright: If you suggest e is a constant to consider, it’ll work.

    nsimplify(0.7182818284590453, constants=[E]) returns e-2.

4 Pings/Trackbacks for "Recognizing numbers"
  1. [...] Recognizing Numbers – This is very cool!  Use Python’s sympy to discover formulas for numbers.  For example, to discover that an approximation to pi is exp(141/895 + sqrt(780631)/895) [...]

  2. [...] Recognizing numbers (johndcook.com) [...]

  3. [...] Cook shares Recognizing numbers. For Python users, SymPy is a symbolic math package that "takes a floating point number and tries [...]

  4. [...] Oscillating Fibonacci ratios Recognizing numbers [...]