Skip to content

Instantly share code, notes, and snippets.

@danielc192
Last active December 19, 2015 00:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielc192/5870580 to your computer and use it in GitHub Desktop.
Save danielc192/5870580 to your computer and use it in GitHub Desktop.
Code to calculate the Inverse Error function using a power series approximation. Based on information at http://en.wikipedia.org/wiki/Error_function#Inverse_functions. Implemented as a method in the Math module, but it could easily be run independently.
module Math
def self.invErf(x)
c = []
c[0] = 1.0
c[1] = 1.0
result = 0.0
(0..100).each do |k|
# Calculate C sub k
if k > 1 then
c[k] = 0.0
(0..(k-1)).each do |m|
term = (c[m]*c[k-1.0-m])/((m+1.0)*(2.0*m+1.0))
c[k] += term
end
end
result += (c[k]/(2.0*k+1))*((Math.sqrt(Math::PI)/2)*x)**(2.0*k+1)
end
return result
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment