LambertW_function.rb 568 B

123456789101112131415161718192021222324252627282930
  1. #!/usr/bin/ruby
  2. # Daniel "Trizen" Șuteu
  3. # Date: 29 July 2018
  4. # https://github.com/trizen
  5. # A simple implementation of Lambert's W function in real numbers.
  6. # Example: x^x = 100
  7. # x = exp(lambertW(log(100)))
  8. # x =~ 3.5972850235404...
  9. # See also:
  10. # https://en.wikipedia.org/wiki/Lambert_W_function
  11. def LambertW(c)
  12. x = (Math.sqrt(c) + 1)
  13. y = 0.0
  14. while ((x-y).abs > 1e-10)
  15. y = x
  16. x = ((x + c) / (1.0 + Math.log(x)))
  17. end
  18. Math.log(x)
  19. end
  20. puts Math.exp(LambertW(Math.log(100))) #=> 3.5972850235404175