I am attaching a new file giving program with comments. I have also made 
slight changes so that if parameters are negative, it takes absolute value 
and computes perimeter of ellipse correctly.

On Saturday, November 4, 2017 at 2:41:59 AM UTC+5:30, Panna Lal Patodia 
wrote:
>
> I find the following code for perimeter (or circumference of ellipse):
>
> 12*Integral(sqrt((-8*_x**2/9 + 1)/(-_x**2 + 1)), (_x, 0, 1))
>
> This means the calculation depends on numerical integration which is very 
> time consuming.
>
> I am attaching a file containing Python sympy code to calculate numerical 
> value of perimeter of ellipse to hundreds of decimal places. It computes 
> value of 
> perimeter of Ellipse for 800 decimals in less than 1 second on my computer.
>
> Please incorporate in sympy after necessary modification if you find it 
> suitable.
>
> Regards
>
> Panna Lal Patodia
> [email protected], [email protected]
>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sympy.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sympy/c36d536e-d856-405e-a948-afda332232ab%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
'''
Computes Perimeter of Ellipse very fast correct to hundreds of places
Written by Panna Lal Patodia ([email protected], [email protected])
On November 3, 2017
'''

from sympy import *

'''
Computes Perimeter of an ellipse.
Parameters:
(1) a: semimajor axis 
(2) b: semiminor axis
(3) pr: Precision required in number of decimals to be given as Positive integer 
Example: Compute perimeter of an ellipse whose semimajor axis is 3 and
semiminor axis is 1 correct to 800 decimal places.
ellipse_peri(3, 1, 800)
'''
def ellipse_peri(a, b, pr=15):
	import sympy
	pr = abs(pr)
	tol = N(10**(-pr), pr)
	a, b = abs(a), abs(b)
	s, m = 0.0, 1.0
	x, y = max(a,b), min(a,b)
	while (abs(x - y) > tol):
		x, y = (x + y) / 2, N(sympy.sqrt(x * y), pr)
		m *= 2
		s += N(m * (x - y) * (x - y), pr)
	p = N(sympy.pi * ((a + b) * (a + b) - s) / (x + y), pr)
	return p

print(ellipse_peri(3, 1, 800))

Reply via email to