Skip to content

Instantly share code, notes, and snippets.

@dpiponi
Created August 12, 2017 17:07
Show Gist options
  • Save dpiponi/ab40f57a284875f4a43507def8089383 to your computer and use it in GitHub Desktop.
Save dpiponi/ab40f57a284875f4a43507def8089383 to your computer and use it in GitHub Desktop.
Compute perimeter of ellipse
# Based on http://www.ams.org/notices/201208/rtx120801094p.pdf
import math
EPS = 1e-8
# Arithmetico-geometric mean
def agm(a, b):
while True:
a1 = 0.5*(a+b)
b1 = math.sqrt(a*b)
if abs(a-a1) < EPS:
return a1
a = a1
b = b1
# Modified arithmetico-geometric mean
def magm(a, b):
c = 0
while True:
a1 = 0.5*(a+b)
u = math.sqrt((a-c)*(b-c))
b1 = c+u
c1 = c-u
if abs(a-a1) < EPS:
return a1
a = a1
b = b1
c = c1
# Compute elliptic integral of second kind
def E(e):
return math.pi*magm(1, 1-e)/(2*agm(1, math.sqrt(1-e)))
# Assumes a > b and that b != 0
def perimeter(a, b):
return 4*a*E(1-b**2/a**2)
# Example
print perimeter(0.75, 0.321)
@cgdougm
Copy link

cgdougm commented Aug 12, 2017

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment