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 163: | Line 163: | ||
− | I'd like to add my own | + | I'd like to add my own implemetation: |
− | + | 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 | ||
Line 284: | Line 282: | ||
My python program: | My python program: | ||
− | + | 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] | |
− | + | print(gmdn(1e-15,[1,1,2,3,5])) | |
− | |||
It gave me 2.0890579497368584 | It gave me 2.0890579497368584 | ||
Line 400: | Line 397: | ||
Since it uses F(F(F...)) someone should write a Hascal version that actually uses the fixedpoint function. [[Special:Contributions/172.69.35.175|172.69.35.175]] 22:56, 30 March 2021 (UTC) | Since it uses F(F(F...)) someone should write a Hascal version that actually uses the fixedpoint function. [[Special:Contributions/172.69.35.175|172.69.35.175]] 22:56, 30 March 2021 (UTC) | ||
− | |||
− | |||
− |