Editing Talk:2435: Geothmetic Meandian
Please sign your posts with ~~~~ |
Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.
The edit can be undone.
Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.
Latest revision | Your text | ||
Line 165: | Line 165: | ||
I'd like to add my own implementation: | I'd like to add my own implementation: | ||
− | + | from math import * | |
− | + | def getMeans(n=list): | |
− | + | n.sort() | |
− | + | mean=sum(n)/len(n) | |
− | + | if len(n)%2==1: | |
− | + | median=n[len(n)//2] | |
− | + | else: | |
− | + | median=(n[1+floor(len(n)/2)]-n[floor(len(n)/2)])/2 | |
− | + | prod=1 | |
− | + | for i in n: | |
− | + | prod*=i | |
− | + | gmean=prod**(1/len(n)) | |
− | + | return [mean,median,gmean] | |
− | + | def gmdn(tol,n=list): | |
− | + | mList=n | |
− | + | mList=getMeans(mList) | |
− | + | while not (isclose(mList[0],mList[1],rel_tol=tol) and isclose(mList[1],mList[2],rel_tol=tol) and isclose(mList[0],mList[2],rel_tol=tol) ) : | |
− | + | mList = getMeans(mList) | |
− | + | return (mList[0]+mList[1]+mList[2])/3 | |
− | + | ||
− | + | ||
+ | |||
+ | print(gmdn(1e-15,[1,1,2,3,5])) | ||
which gives me 2.089057949736859 | which gives me 2.089057949736859 |