Averages/Pythagorean means: Difference between revisions
→{{header|Lasso}}: Adding Lasso Example |
imported>Regattaguru |
||
(142 intermediate revisions by 68 users not shown) | |||
Line 1: | Line 1: | ||
[[Category:Geometry]] |
|||
{{task}} |
{{task}} |
||
Compute all three of the [[wp:Pythagorean means|Pythagorean means]] of the set of integers 1 through 10. |
|||
;Task |
|||
Show that <math>A(x_1,\ldots,x_n) \geq G(x_1,\ldots,x_n) \geq H(x_1,\ldots,x_n)</math> for this set of positive integers. |
|||
Compute all three of the [[wp:Pythagorean means|Pythagorean means]] of the set of integers <big>1</big> through <big>10</big> (inclusive). |
|||
Show that <big><math>A(x_1,\ldots,x_n) \geq G(x_1,\ldots,x_n) \geq H(x_1,\ldots,x_n)</math></big> for this set of positive integers. |
|||
* The most common of the three means, the [[Averages/Arithmetic mean|arithmetic mean]], is the sum of the list divided by its length: |
* The most common of the three means, the [[Averages/Arithmetic mean|arithmetic mean]], is the sum of the list divided by its length: |
||
: <math> A(x_1, \ldots, x_n) = \frac{x_1 + \cdots + x_n}{n}</math> |
: <big><math> A(x_1, \ldots, x_n) = \frac{x_1 + \cdots + x_n}{n}</math></big> |
||
* The [[wp:Geometric mean|geometric mean]] is the <math>n</math>th root of the product of the list: |
* The [[wp:Geometric mean|geometric mean]] is the <math>n</math>th root of the product of the list: |
||
: <math> G(x_1, \ldots, x_n) = \sqrt[n]{x_1 \cdots x_n} </math> |
: <big><math> G(x_1, \ldots, x_n) = \sqrt[n]{x_1 \cdots x_n} </math></big> |
||
* The [[wp:Harmonic mean|harmonic mean]] is <math>n</math> divided by the sum of the reciprocal of each item in the list: |
* The [[wp:Harmonic mean|harmonic mean]] is <math>n</math> divided by the sum of the reciprocal of each item in the list: |
||
: <math> H(x_1, \ldots, x_n) = \frac{n}{\frac{1}{x_1} + \cdots + \frac{1}{x_n}} </math> |
: <big><math> H(x_1, \ldots, x_n) = \frac{n}{\frac{1}{x_1} + \cdots + \frac{1}{x_n}} </math></big> |
||
C.f. [[Averages/Root mean square]] |
|||
{{task heading|See also}} |
|||
{{Related tasks/Statistical measures}} |
|||
<br><hr> |
|||
=={{header|11l}}== |
|||
<syntaxhighlight lang="11l">F amean(num) |
|||
R sum(num)/Float(num.len) |
|||
F gmean(num) |
|||
R product(num) ^ (1.0/num.len) |
|||
F hmean(num) |
|||
return num.len / sum(num.map(n -> 1.0/n)) |
|||
V numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] |
|||
print(amean(numbers)) |
|||
print(gmean(numbers)) |
|||
print(hmean(numbers))</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
5.5 |
|||
4.52873 |
|||
3.41417 |
|||
</pre> |
|||
=={{header|Action!}}== |
|||
{{libheader|Action! Tool Kit}} |
|||
<syntaxhighlight lang="action!">INCLUDE "D2:REAL.ACT" ;from the Action! Tool Kit |
|||
PROC InverseI(INT a,result) |
|||
REAL one,x |
|||
IntToReal(1,one) |
|||
IntToReal(a,x) |
|||
RealDiv(one,x,result) |
|||
RETURN |
|||
PROC ArithmeticMean(INT ARRAY a INT count REAL POINTER result) |
|||
INT i |
|||
REAL x,sum,tmp |
|||
IntToReal(0,sum) |
|||
FOR i=0 TO count-1 |
|||
DO |
|||
IntToReal(a(i),x) |
|||
RealAdd(sum,x,tmp) |
|||
RealAssign(tmp,sum) |
|||
OD |
|||
IntToReal(count,tmp) |
|||
RealDiv(sum,tmp,result) |
|||
RETURN |
|||
PROC GeometricMean(INT ARRAY a INT count REAL POINTER result) |
|||
INT i |
|||
REAL x,prod,tmp |
|||
IntToReal(1,prod) |
|||
FOR i=0 TO count-1 |
|||
DO |
|||
IntToReal(a(i),x) |
|||
RealMult(prod,x,tmp) |
|||
RealAssign(tmp,prod) |
|||
OD |
|||
InverseI(count,tmp) |
|||
Power(prod,tmp,result) |
|||
RETURN |
|||
PROC HarmonicMean(INT ARRAY a INT count REAL POINTER result) |
|||
INT i |
|||
REAL x,sum,tmp |
|||
IntToReal(0,sum) |
|||
FOR i=0 TO count-1 |
|||
DO |
|||
InverseI(a(i),x) |
|||
RealAdd(sum,x,tmp) |
|||
RealAssign(tmp,sum) |
|||
OD |
|||
IntToReal(count,tmp) |
|||
RealDiv(tmp,sum,result) |
|||
RETURN |
|||
PROC Main() |
|||
BYTE i |
|||
INT ARRAY a=[1 2 3 4 5 6 7 8 9 10] |
|||
REAL result |
|||
Put(125) PutE() ;clear screen |
|||
ArithmeticMean(a,10,result) |
|||
Print("Arithmetic mean=") PrintRE(result) |
|||
GeometricMean(a,10,result) |
|||
Print(" Geometric mean=") PrintRE(result) |
|||
HarmonicMean(a,10,result) |
|||
Print(" Harmonic mean=") PrintRE(result) |
|||
RETURN</syntaxhighlight> |
|||
{{out}} |
|||
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Pythagorean_means.png Screenshot from Atari 8-bit computer] |
|||
<pre> |
|||
Arithmetic mean=5.5 |
|||
Geometric mean=4.52872861 |
|||
Harmonic mean=3.41417153 |
|||
</pre> |
|||
=={{header|ActionScript}}== |
=={{header|ActionScript}}== |
||
< |
<syntaxhighlight lang="actionscript">function arithmeticMean(v:Vector.<Number>):Number |
||
{ |
{ |
||
var sum:Number = 0; |
var sum:Number = 0; |
||
Line 38: | Line 151: | ||
trace("Arithmetic: ", arithmeticMean(list)); |
trace("Arithmetic: ", arithmeticMean(list)); |
||
trace("Geometric: ", geometricMean(list)); |
trace("Geometric: ", geometricMean(list)); |
||
trace("Harmonic: ", harmonicMean(list));</ |
trace("Harmonic: ", harmonicMean(list));</syntaxhighlight> |
||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
pythagorean_means.ads: |
pythagorean_means.ads: |
||
< |
<syntaxhighlight lang="ada">package Pythagorean_Means is |
||
type Set is array (Positive range <>) of Float; |
type Set is array (Positive range <>) of Float; |
||
function Arithmetic_Mean (Data : Set) return Float; |
function Arithmetic_Mean (Data : Set) return Float; |
||
function Geometric_Mean (Data : Set) return Float; |
function Geometric_Mean (Data : Set) return Float; |
||
function Harmonic_Mean (Data : Set) return Float; |
function Harmonic_Mean (Data : Set) return Float; |
||
end Pythagorean_Means;</ |
end Pythagorean_Means;</syntaxhighlight> |
||
pythagorean_means.adb: |
pythagorean_means.adb: |
||
< |
<syntaxhighlight lang="ada">with Ada.Numerics.Generic_Elementary_Functions; |
||
package body Pythagorean_Means is |
package body Pythagorean_Means is |
||
package Math is new Ada.Numerics.Generic_Elementary_Functions (Float); |
package Math is new Ada.Numerics.Generic_Elementary_Functions (Float); |
||
Line 83: | Line 196: | ||
end Harmonic_Mean; |
end Harmonic_Mean; |
||
end Pythagorean_Means;</ |
end Pythagorean_Means;</syntaxhighlight> |
||
example main.adb: |
example main.adb: |
||
< |
<syntaxhighlight lang="ada">with Ada.Text_IO; |
||
with Pythagorean_Means; |
with Pythagorean_Means; |
||
procedure Main is |
procedure Main is |
||
Line 98: | Line 211: | ||
Float'Image (Geometric_Mean) & " >= " & |
Float'Image (Geometric_Mean) & " >= " & |
||
Float'Image (Harmonic_Mean)); |
Float'Image (Harmonic_Mean)); |
||
end Main;</ |
end Main;</syntaxhighlight> |
||
=={{header|ALGOL 68}}== |
=={{header|ALGOL 68}}== |
||
Line 105: | Line 218: | ||
{{works with|ALGOL 68G|Any - tested with release [http://sourceforge.net/projects/algol68/files/algol68g/algol68g-1.18.0/algol68g-1.18.0-9h.tiny.el5.centos.fc11.i386.rpm/download 1.18.0-9h.tiny]}} |
{{works with|ALGOL 68G|Any - tested with release [http://sourceforge.net/projects/algol68/files/algol68g/algol68g-1.18.0/algol68g-1.18.0-9h.tiny.el5.centos.fc11.i386.rpm/download 1.18.0-9h.tiny]}} |
||
{{wont work with|ELLA ALGOL 68|Any (with appropriate job cards) - argc and argv implementation dependent}} |
{{wont work with|ELLA ALGOL 68|Any (with appropriate job cards) - argc and argv implementation dependent}} |
||
< |
<syntaxhighlight lang="algol68">main: ( |
||
INT count:=0; |
INT count:=0; |
||
LONG REAL f, sum:=0, prod:=1, resum:=0; |
LONG REAL f, sum:=0, prod:=1, resum:=0; |
||
Line 130: | Line 243: | ||
printf(($"Geometric mean = "f(real)l$,prod**(1/count))); |
printf(($"Geometric mean = "f(real)l$,prod**(1/count))); |
||
printf(($"Harmonic mean = "f(real)l$,count/resum)) |
printf(($"Harmonic mean = "f(real)l$,count/resum)) |
||
)</ |
)</syntaxhighlight> |
||
Lunix command: |
Lunix command: |
||
a68g Averages_Pythagorean_means.a68 - 1 2 3 4 5 6 7 8 9 10 |
a68g Averages_Pythagorean_means.a68 - 1 2 3 4 5 6 7 8 9 10 |
||
{{out}} |
|||
Output: |
|||
<pre> |
<pre> |
||
c: 10.0000 |
c: 10.0000 |
||
Line 142: | Line 255: | ||
Geometric mean = 4.5287 |
Geometric mean = 4.5287 |
||
Harmonic mean = 3.4142 |
Harmonic mean = 3.4142 |
||
</pre> |
|||
=={{header|ALGOL W}}== |
|||
<syntaxhighlight lang="algolw">begin |
|||
% returns the arithmetic mean of the elements of n from lo to hi % |
|||
real procedure arithmeticMean ( real array n ( * ); integer value lo, hi ) ; |
|||
begin |
|||
real sum; |
|||
sum := 0; |
|||
for i := lo until hi do sum := sum + n( i ); |
|||
sum / ( 1 + ( hi - lo ) ) |
|||
end arithmeticMean ; |
|||
% returns the geometric mean of the elements of n from lo to hi % |
|||
real procedure geometricMean ( real array n ( * ); integer value lo, hi ) ; |
|||
begin |
|||
real product; |
|||
product := 1; |
|||
for i := lo until hi do product := product * n( i ); |
|||
exp( ln( product ) / ( 1 + ( hi - lo ) ) ) |
|||
end geometricMean ; |
|||
% returns the harminic mean of the elements of n from lo to hi % |
|||
real procedure harmonicMean ( real array n ( * ); integer value lo, hi ) ; |
|||
begin |
|||
real sum; |
|||
sum := 0; |
|||
for i := lo until hi do sum := sum + ( 1 / n( i ) ); |
|||
( 1 + ( hi - lo ) ) / sum |
|||
end harmonicMean ; |
|||
real array v ( 1 :: 10 ); |
|||
for i := 1 until 10 do v( i ) := i; |
|||
r_w := 10; r_d := 5; r_format := "A"; s_w := 0; % set output format % |
|||
write( "Arithmetic mean: ", arithmeticMean( v, 1, 10 ) ); |
|||
write( "Geometric mean: ", geometricMean( v, 1, 10 ) ); |
|||
write( "Harmonic mean: ", harmonicMean( v, 1, 10 ) ) |
|||
end.</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
Arithmetic mean: 5.50000 |
|||
Geometric mean: 4.52872 |
|||
Harmonic mean: 3.41417 |
|||
</pre> |
|||
=={{header|Amazing Hopper}}== |
|||
Think about "talk" programming... |
|||
<syntaxhighlight lang="amazing hopper"> |
|||
#include <hopper.h> |
|||
/* An example of definitions in pseudo-natural language, with synonimous. |
|||
These definitions can be inside a definition file (xxxx.h) */ |
|||
#define getasinglelistof(_X_) {_X_}, |
|||
#synon getasinglelistof getalistof |
|||
#define integerrandomnumbers _V1000_=-1,rand array(_V1000_),mulby(10),ceil, |
|||
#define randomnumbers _V1000_=-1,rand array(_V1000_) |
|||
#define rememberitin(_X_) _X_=0,cpy(_X_) |
|||
#synon rememberitin rememberthisnumbersin |
|||
#define rememberas(_X_) mov(_X_) |
|||
#define remember(_X_) {_X_} |
|||
//#synon remember with ---> this exist in HOPPER.H |
|||
#defn nowconsiderthis(_X_) #ATOM#CMPLX,print |
|||
#synon nowconsiderthis nowconsider,considerthis,consider,nowputtext,puttext,andprint |
|||
#define andprintwithanewline {"\n"}print |
|||
#synon andprintwithanewline printwithanewline |
|||
//#defn andprint(_X_) #ATOM#CMPLX,print |
|||
#define putanewline {"\n"} |
|||
#define withanewline "\n" |
|||
#define andprintit print |
|||
#synon andprintit printit,andprint |
|||
#define showit show |
|||
#define afterdoingit emptystack?,not,do{ {"I cannot continue due to retentive data "},throw(1001) } |
|||
#synon afterdoingit secondly,finally |
|||
#define then emptystack?do{ {"I cannot continue because data is missing "},throw(1000) } |
|||
/* why "#context" and not "#define"? |
|||
becose "#context" need a value in the stack for continue. |
|||
Internally, "domeanit" tranform to "gosub(calculatearithmeticmean)", |
|||
and "gosub" works only if it finds a data in the stack */ |
|||
#context calculatethegeometricmean |
|||
#synon calculatethegeometricmean calculategeometricmean,getgeometricmean |
|||
#context calculatetheharmonicmean |
|||
#synon calculatetheharmonicmean calculateharmonicmean,getharmonicmean |
|||
#context calculatearitmethicmean |
|||
#synon calculatearitmethicmean calculatesinglemean,calculatemean,domeanit |
|||
main: |
|||
consider this ("Arithmetic Mean: ") |
|||
get a list of '10,10' integer random numbers; remember this numbers in 'list of numbers'; |
|||
then, do mean it, and print with a new line. |
|||
after doing it, consider ("Geometric Mean: "), remember 'list of numbers', calculate the geometric mean; |
|||
then, put a new line, and print it. |
|||
/* |
|||
Okay. This can be a bit long, if we have to write the program; |
|||
But what if we just had to talk, and the language interpreter takes care of the rest? |
|||
*/ |
|||
secondly, now consider ("Harmonic Mean: "), with 'list of numbers', get harmonic mean, and print with a new line. |
|||
finally, put text ("Original Array:\n"), and print (list of numbers, with a new line) |
|||
exit(0) |
|||
.locals |
|||
calculate aritmethic mean: |
|||
stats(MEAN) |
|||
back |
|||
calculate the geometric mean: |
|||
stats(GEOMEAN) |
|||
back |
|||
calculatetheharmonicmean: |
|||
stats(HARMEAN) |
|||
back |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
Arithmetic Mean: 5.51 |
|||
Geometric Mean: 4.47333 |
|||
Harmonic Mean: 3.29515 |
|||
Original Array: |
|||
4 3 10 6 10 1 1 10 4 1 |
|||
7 4 5 8 9 7 3 8 1 10 |
|||
5 5 2 5 8 9 1 5 10 10 |
|||
6 4 3 5 9 2 6 9 2 9 |
|||
10 9 3 4 6 2 1 8 10 1 |
|||
7 5 5 9 9 3 7 9 7 7 |
|||
9 2 10 1 7 9 3 2 7 4 |
|||
1 6 2 4 10 7 5 1 5 5 |
|||
1 2 9 5 10 7 7 6 6 4 |
|||
3 4 5 2 4 1 10 6 3 7 |
|||
</pre> |
|||
Notice: |
|||
<pre> |
|||
The line "stats(GEOMEAN)" is identical to: |
|||
stats(MULTIPLICATORY),POW BY({1}div by (100)) |
|||
and... |
|||
#defn reciprocalof(_X_) {1},#ATOM#CMPLX,postfix,div,postfix |
|||
then: |
|||
stats(MULTIPLICATORY),POW BY( reciprocal of (100) ) |
|||
or... |
|||
#defn N-ROOTof(_DATA_,_N_) #ATOM#CMPLX,{1}div by(_N_),postfix,pow,postfix |
|||
#define Multiplicatoryof(_DATA_) {_DATA_}stats(MULTIPLICATORY) |
|||
then: |
|||
N-ROOT of ( Multiplicatory of (list of numbers), 100) |
|||
etc. |
|||
</pre> |
</pre> |
||
=={{header|APL}}== |
=={{header|APL}}== |
||
<syntaxhighlight lang="apl"> |
|||
<lang APL> |
|||
arithmetic←{(+/⍵)÷⍴⍵} |
arithmetic←{(+/⍵)÷⍴⍵} |
||
geometric←{(×/⍵)*÷⍴⍵} |
geometric←{(×/⍵)*÷⍴⍵} |
||
Line 158: | Line 412: | ||
4.528728688 |
4.528728688 |
||
harmonic x |
harmonic x |
||
3.414171521 |
3.414171521</syntaxhighlight> |
||
</lang> |
|||
=={{header|AppleScript}}== |
|||
{{trans|JavaScript}} |
|||
<syntaxhighlight lang="applescript">-- arithmetic_mean :: [Number] -> Number |
|||
on arithmetic_mean(xs) |
|||
-- sum :: Number -> Number -> Number |
|||
script sum |
|||
on |λ|(accumulator, x) |
|||
accumulator + x |
|||
end |λ| |
|||
end script |
|||
foldl(sum, 0, xs) / (length of xs) |
|||
end arithmetic_mean |
|||
-- geometric_mean :: [Number] -> Number |
|||
on geometric_mean(xs) |
|||
-- product :: Number -> Number -> Number |
|||
script product |
|||
on |λ|(accumulator, x) |
|||
accumulator * x |
|||
end |λ| |
|||
end script |
|||
foldl(product, 1, xs) ^ (1 / (length of xs)) |
|||
end geometric_mean |
|||
-- harmonic_mean :: [Number] -> Number |
|||
on harmonic_mean(xs) |
|||
-- addInverse :: Number -> Number -> Number |
|||
script addInverse |
|||
on |λ|(accumulator, x) |
|||
accumulator + (1 / x) |
|||
end |λ| |
|||
end script |
|||
(length of xs) / (foldl(addInverse, 0, xs)) |
|||
end harmonic_mean |
|||
-- TEST ----------------------------------------------------------------------- |
|||
on run |
|||
set {A, G, H} to ap({arithmetic_mean, geometric_mean, harmonic_mean}, ¬ |
|||
{{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}}) |
|||
{values:{arithmetic:A, geometric:G, harmonic:H}, inequalities:¬ |
|||
{|A >= G|:A ≥ G}, |G >= H|:G ≥ H} |
|||
end run |
|||
-- GENERIC FUNCTIONS ---------------------------------------------------------- |
|||
-- A list of functions applied to a list of arguments |
|||
-- (<*> | ap) :: [(a -> b)] -> [a] -> [b] |
|||
on ap(fs, xs) |
|||
set {nf, nx} to {length of fs, length of xs} |
|||
set acc to {} |
|||
repeat with i from 1 to nf |
|||
tell mReturn(item i of fs) |
|||
repeat with j from 1 to nx |
|||
set end of acc to |λ|(contents of (item j of xs)) |
|||
end repeat |
|||
end tell |
|||
end repeat |
|||
return acc |
|||
end ap |
|||
-- foldl :: (a -> b -> a) -> a -> [b] -> a |
|||
on foldl(f, startValue, xs) |
|||
tell mReturn(f) |
|||
set v to startValue |
|||
set lng to length of xs |
|||
repeat with i from 1 to lng |
|||
set v to |λ|(v, item i of xs, i, xs) |
|||
end repeat |
|||
return v |
|||
end tell |
|||
end foldl |
|||
-- map :: (a -> b) -> [a] -> [b] |
|||
on map(f, xs) |
|||
tell mReturn(f) |
|||
set lng to length of xs |
|||
set lst to {} |
|||
repeat with i from 1 to lng |
|||
set end of lst to |λ|(item i of xs, i, xs) |
|||
end repeat |
|||
return lst |
|||
end tell |
|||
end map |
|||
-- Lift 2nd class handler function into 1st class script wrapper |
|||
-- mReturn :: Handler -> Script |
|||
on mReturn(f) |
|||
if class of f is script then |
|||
f |
|||
else |
|||
script |
|||
property |λ| : f |
|||
end script |
|||
end if |
|||
end mReturn</syntaxhighlight> |
|||
{{Out}} |
|||
<syntaxhighlight lang="applescript">{values:{arithmetic:5.5, geometric:4.528728688117, harmonic:3.414171521474}, |
|||
inequalities:{|A >= G|:true}, |G >= H|:true}</syntaxhighlight> |
|||
=={{header|Arturo}}== |
|||
<syntaxhighlight lang="rebol">arithmeticMean: function [arr]-> |
|||
average arr |
|||
geometricMean: function [arr]-> |
|||
(product arr) ^ 1//size arr |
|||
harmonicMean: function [arr]-> |
|||
(size arr) // sum map arr 'i [1//i] |
|||
print arithmeticMean 1..10 |
|||
print geometricMean 1..10 |
|||
print harmonicMean 1..10</syntaxhighlight> |
|||
{{out}} |
|||
<pre>5.5 |
|||
4.528728688116765 |
|||
3.414171521474055</pre> |
|||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
< |
<syntaxhighlight lang="autohotkey">A := ArithmeticMean(1, 10) |
||
G := GeometricMean(1, 10) |
G := GeometricMean(1, 10) |
||
H := HarmonicMean(1, 10) |
H := HarmonicMean(1, 10) |
||
Line 203: | Line 583: | ||
Sum += 1 / (a + A_Index - 1) |
Sum += 1 / (a + A_Index - 1) |
||
Return, n / Sum |
Return, n / Sum |
||
}</ |
}</syntaxhighlight> |
||
Message box shows: |
Message box shows: |
||
<pre> |
<pre> |
||
Line 213: | Line 593: | ||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
< |
<syntaxhighlight lang="awk">#!/usr/bin/awk -f |
||
{ |
{ |
||
x = $1; # value of 1st column |
x = $1; # value of 1st column |
||
Line 226: | Line 606: | ||
print "Geometric mean : ",exp(G/N); |
print "Geometric mean : ",exp(G/N); |
||
print "Harmonic mean : ",N/H; |
print "Harmonic mean : ",N/H; |
||
}</ |
}</syntaxhighlight> |
||
=={{header|BBC BASIC}}== |
=={{header|BBC BASIC}}== |
||
The arithmetic and harmonic means use BBC BASIC's built-in array operations; only the geometric mean needs a loop. |
The arithmetic and harmonic means use BBC BASIC's built-in array operations; only the geometric mean needs a loop. |
||
< |
<syntaxhighlight lang="bbcbasic"> DIM a(9) |
||
a() = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 |
a() = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 |
||
PRINT "Arithmetic mean = " ; FNarithmeticmean(a()) |
PRINT "Arithmetic mean = " ; FNarithmeticmean(a()) |
||
Line 253: | Line 633: | ||
b() = 1/a() |
b() = 1/a() |
||
= (DIM(a(),1)+1) / SUM(b()) |
= (DIM(a(),1)+1) / SUM(b()) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
|||
Output: |
|||
<pre>Arithmetic mean = 5.5 |
<pre>Arithmetic mean = 5.5 |
||
Geometric mean = 4.52872869 |
Geometric mean = 4.52872869 |
||
Harmonic mean = 3.41417152</pre> |
Harmonic mean = 3.41417152</pre> |
||
=={{header|BQN}}== |
|||
<syntaxhighlight lang="bqn">A ← +´÷≠ |
|||
G ← ≠√×´ |
|||
H ← A⌾÷ |
|||
⋈⟜(∧´ ¯1⊸↓ ≥ 1⊸↓) (A∾G∾H) 1+↕10</syntaxhighlight> |
|||
{{out}} |
|||
<pre>⟨ ⟨ 5.5 4.528728688116765 3.414171521474055 ⟩ 1 ⟩</pre> |
|||
=={{header|C}}== |
=={{header|C}}== |
||
< |
<syntaxhighlight lang="c">#include <stdio.h> |
||
#include <stdlib.h> // atoi() |
#include <stdlib.h> // atoi() |
||
#include <math.h> // pow() |
#include <math.h> // pow() |
||
Line 282: | Line 671: | ||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
=={{header|C++}}== |
|||
<lang cpp>#include <vector> |
|||
#include <iostream> |
|||
#include <numeric> |
|||
#include <cmath> |
|||
#include <algorithm> |
|||
double toInverse ( int i ) { |
|||
return 1.0 / i ; |
|||
} |
|||
int main( ) { |
|||
std::vector<int> numbers ; |
|||
for ( int i = 1 ; i < 11 ; i++ ) |
|||
numbers.push_back( i ) ; |
|||
double arithmetic_mean = std::accumulate( numbers.begin( ) , numbers.end( ) , 0 ) / 10.0 ; |
|||
double geometric_mean = |
|||
pow( std::accumulate( numbers.begin( ) , numbers.end( ) , 1 , std::multiplies<int>( ) ), 0.1 ) ; |
|||
std::vector<double> inverses ; |
|||
inverses.resize( numbers.size( ) ) ; |
|||
std::transform( numbers.begin( ) , numbers.end( ) , inverses.begin( ) , toInverse ) ; |
|||
double harmonic_mean = 10 / std::accumulate( inverses.begin( ) , inverses.end( ) , 0.0 ); //initial value of accumulate must be a double! |
|||
std::cout << "The arithmetic mean is " << arithmetic_mean << " , the geometric mean " |
|||
<< geometric_mean << " and the harmonic mean " << harmonic_mean << " !\n" ; |
|||
return 0 ; |
|||
}</lang> |
|||
Output: |
|||
<pre> |
|||
The arithmetic mean is 5.5 , the geometric mean 4.52873 and the harmonic mean 3.41417 ! |
|||
</pre> |
|||
=={{header|C sharp|C#}}== |
=={{header|C sharp|C#}}== |
||
Line 320: | Line 678: | ||
{{works with|C sharp|C#|3}} |
{{works with|C sharp|C#|3}} |
||
< |
<syntaxhighlight lang="csharp">using System; |
||
using System.Collections.Generic; |
using System.Collections.Generic; |
||
using System.Diagnostics; |
using System.Diagnostics; |
||
Line 350: | Line 708: | ||
// Harmonic mean extension method. |
// Harmonic mean extension method. |
||
static double Hmean(this IEnumerable<double> n) { |
static double Hmean(this IEnumerable<double> n) { |
||
return n.Count() / n.Sum(i => 1 / i); |
return n.Count() / n.Sum(i => 1.0 / i); |
||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
|||
Output: |
|||
<pre> |
<pre> |
||
Arithmetic mean 5.5 |
Arithmetic mean 5.5 |
||
Geometric mean 4.52872868811677 |
Geometric mean 4.52872868811677 |
||
Harmonic mean 3.41417152147406 |
Harmonic mean 3.41417152147406</pre> |
||
</pre> |
|||
=={{header|C++}}== |
|||
<syntaxhighlight lang="cpp">#include <vector> |
|||
#include <iostream> |
|||
#include <numeric> |
|||
#include <cmath> |
|||
#include <algorithm> |
|||
double toInverse ( int i ) { |
|||
return 1.0 / i ; |
|||
} |
|||
int main( ) { |
|||
std::vector<int> numbers ; |
|||
for ( int i = 1 ; i < 11 ; i++ ) |
|||
numbers.push_back( i ) ; |
|||
double arithmetic_mean = std::accumulate( numbers.begin( ) , numbers.end( ) , 0 ) / 10.0 ; |
|||
double geometric_mean = |
|||
pow( std::accumulate( numbers.begin( ) , numbers.end( ) , 1 , std::multiplies<int>( ) ), 0.1 ) ; |
|||
std::vector<double> inverses ; |
|||
inverses.resize( numbers.size( ) ) ; |
|||
std::transform( numbers.begin( ) , numbers.end( ) , inverses.begin( ) , toInverse ) ; |
|||
double harmonic_mean = 10 / std::accumulate( inverses.begin( ) , inverses.end( ) , 0.0 ); //initial value of accumulate must be a double! |
|||
std::cout << "The arithmetic mean is " << arithmetic_mean << " , the geometric mean " |
|||
<< geometric_mean << " and the harmonic mean " << harmonic_mean << " !\n" ; |
|||
return 0 ; |
|||
}</syntaxhighlight> |
|||
{{out}} |
|||
<pre>The arithmetic mean is 5.5 , the geometric mean 4.52873 and the harmonic mean 3.41417 !</pre> |
|||
=={{header|Clojure}}== |
|||
<syntaxhighlight lang="clojure">(use '[clojure.contrib.math :only (expt)]) |
|||
(defn a-mean [coll] |
|||
(/ (apply + coll) (count coll))) |
|||
(defn g-mean [coll] |
|||
(expt (apply * coll) (/ (count coll)))) |
|||
(defn h-mean [coll] |
|||
(/ (count coll) (apply + (map / coll)))) |
|||
(let [numbers (range 1 11) |
|||
a (a-mean numbers) g (g-mean numbers) h (h-mean numbers)] |
|||
(println a ">=" g ">=" h) |
|||
(>= a g h))</syntaxhighlight> |
|||
=={{header|CoffeeScript}}== |
|||
<syntaxhighlight lang="coffeescript">a = [ 1..10 ] |
|||
arithmetic_mean = (a) -> a.reduce(((s, x) -> s + x), 0) / a.length |
|||
geometic_mean = (a) -> Math.pow(a.reduce(((s, x) -> s * x), 1), (1 / a.length)) |
|||
harmonic_mean = (a) -> a.length / a.reduce(((s, x) -> s + 1 / x), 0) |
|||
A = arithmetic_mean a |
|||
G = geometic_mean a |
|||
H = harmonic_mean a |
|||
console.log "A = ", A, " G = ", G, " H = ", H |
|||
console.log "A >= G : ", A >= G, " G >= H : ", G >= H</syntaxhighlight> |
|||
{{out}} |
|||
<pre>A = 5.5 G = 4.528728688116765 H = 3.414171521474055 |
|||
A >= G : true G >= H : true</pre> |
|||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |
||
<syntaxhighlight lang="lisp">(defun generic-mean (nums reduce-op final-op) |
|||
<lang lisp> |
|||
(defun generic-mean (nums reduce-op final-op) |
|||
(funcall final-op (reduce reduce-op nums))) |
(funcall final-op (reduce reduce-op nums))) |
||
Line 386: | Line 803: | ||
(format t "a-mean ~a~%" a-mean) |
(format t "a-mean ~a~%" a-mean) |
||
(format t "g-mean ~a~%" g-mean) |
(format t "g-mean ~a~%" g-mean) |
||
(format t "h-mean ~a~%" h-mean)))</ |
(format t "h-mean ~a~%" h-mean)))</syntaxhighlight> |
||
=={{header|Craft Basic}}== |
|||
<syntaxhighlight lang="basic">precision 6 |
|||
define bxsum = 1, sum = 0, sum1i = 0 |
|||
define average = 0, geometric = 0, harmonic = 0 |
|||
for i = 1 to 10 |
|||
let sum = sum + i |
|||
let bxsum = bxsum * i |
|||
let sum1i = sum1i + (1 / i) |
|||
next i |
|||
let average = sum / 10 |
|||
let geometric = bxsum ^ (1 / 10) |
|||
let harmonic = 10 / sum1i |
|||
print "arithmetic mean: ", average |
|||
print "geometric mean: ", geometric |
|||
print "harmonic mean: ", harmonic |
|||
if average >= geometric and geometric >= harmonic then |
|||
print "true" |
|||
end |
|||
endif |
|||
print "false"</syntaxhighlight> |
|||
{{out| Output}}<pre> |
|||
arithmetic mean: 5.500000 |
|||
geometric mean: 4.528729 |
|||
harmonic mean: 3.414172 |
|||
true |
|||
</pre> |
|||
=={{header|Clojure}}== |
|||
<lang lisp>(use '[clojure.contrib.math :only (expt)]) |
|||
(defn a-mean [coll] |
|||
(/ (apply + coll) (count coll))) |
|||
(defn g-mean [coll] |
|||
(expt (apply * coll) (/ (count coll)))) |
|||
(defn h-mean [coll] |
|||
(/ (count coll) (apply + (map / coll)))) |
|||
(let [numbers (range 1 11) |
|||
a (a-mean numbers) g (g-mean numbers) h (h-mean numbers)] |
|||
(println a ">=" g ">=" h) |
|||
(>= a g h))</lang> |
|||
=={{header|D}}== |
=={{header|D}}== |
||
The output for the harmonic mean is wrong. |
|||
<lang d>import std.stdio, std.algorithm, std.range, std.functional; |
|||
<syntaxhighlight lang="d">import std.stdio, std.algorithm, std.range, std.functional; |
|||
auto |
auto aMean(T)(T data) pure nothrow @nogc { |
||
return data. |
return data.sum / data.length; |
||
} |
} |
||
auto |
auto gMean(T)(T data) pure /*@nogc*/ { |
||
return data.reduce!q{a * b} |
return data.reduce!q{a * b} ^^ (1.0 / data.length); |
||
} |
} |
||
auto |
auto hMean(T)(T data) pure /*@nogc*/ { |
||
return data.length / data.reduce!q{1.0/a + b} |
return data.length / data.reduce!q{ 1.0 / a + b }; |
||
} |
} |
||
void main() { |
void main() { |
||
immutable m = [adjoin!(hMean, gMean, aMean)(iota(1.0L, 11.0L))[]]; |
|||
writefln("% |
writefln("%(%.19f %)", m); |
||
assert( |
assert(m.isSorted); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>0.9891573712076470036 4.5287286881167647619 5.5000000000000000000</pre> |
<pre>0.9891573712076470036 4.5287286881167647619 5.5000000000000000000</pre> |
||
=={{header|Delphi}}== |
=={{header|Delphi}}== |
||
< |
<syntaxhighlight lang="delphi">program AveragesPythagoreanMeans; |
||
{$APPTYPE CONSOLE} |
{$APPTYPE CONSOLE} |
||
Line 477: | Line 916: | ||
else |
else |
||
writeln("Error!"); |
writeln("Error!"); |
||
end.</ |
end.</syntaxhighlight> |
||
=={{header|E}}== |
=={{header|E}}== |
||
Line 483: | Line 922: | ||
Given that we're defining all three together, it makes sense to express their regularities: |
Given that we're defining all three together, it makes sense to express their regularities: |
||
< |
<syntaxhighlight lang="e">def makeMean(base, include, finish) { |
||
return def mean(numbers) { |
return def mean(numbers) { |
||
var count := 0 |
var count := 0 |
||
Line 497: | Line 936: | ||
def A := makeMean(0, fn b,x { b+x }, fn acc,n { acc / n }) |
def A := makeMean(0, fn b,x { b+x }, fn acc,n { acc / n }) |
||
def G := makeMean(1, fn b,x { b*x }, fn acc,n { acc ** (1/n) }) |
def G := makeMean(1, fn b,x { b*x }, fn acc,n { acc ** (1/n) }) |
||
def H := makeMean(0, fn b,x { b+1/x }, fn acc,n { n / acc })</ |
def H := makeMean(0, fn b,x { b+1/x }, fn acc,n { n / acc })</syntaxhighlight> |
||
< |
<syntaxhighlight lang="e">? A(1..10) |
||
# value: 5.5 |
# value: 5.5 |
||
Line 506: | Line 945: | ||
? H(1..10) |
? H(1..10) |
||
# value: 3.414171521474055</ |
# value: 3.414171521474055</syntaxhighlight> |
||
=={{header|EasyLang}}== |
|||
{{trans|C}} |
|||
<syntaxhighlight> |
|||
proc mean . v[] a g h . |
|||
prod = 1 |
|||
for v in v[] |
|||
sum += v |
|||
prod *= v |
|||
resum += 1 / v |
|||
. |
|||
a = sum / len v[] |
|||
g = pow prod (1 / len v[]) |
|||
h = len v[] / resum |
|||
. |
|||
a[] = [ 1 2 3 4 5 6 7 8 9 10 ] |
|||
mean a[] a g h |
|||
print a |
|||
print g |
|||
print h |
|||
</syntaxhighlight> |
|||
=={{header|EchoLisp}}== |
|||
<syntaxhighlight lang="scheme"> |
|||
(define (A xs) (// (for/sum ((x xs)) x) (length xs))) |
|||
(define (G xs) (expt (for/product ((x xs)) x) (// (length xs)))) |
|||
(define (H xs) (// (length xs) (for/sum ((x xs)) (// x)))) |
|||
(define xs (range 1 11)) |
|||
(and (>= (A xs) (G xs)) (>= (G xs) (H xs))) |
|||
→ #t |
|||
</syntaxhighlight> |
|||
=={{header|Elixir}}== |
|||
<syntaxhighlight lang="elixir">defmodule Means do |
|||
def arithmetic(list) do |
|||
Enum.sum(list) / length(list) |
|||
end |
|||
def geometric(list) do |
|||
:math.pow(Enum.reduce(list, &(*/2)), 1 / length(list)) |
|||
end |
|||
def harmonic(list) do |
|||
1 / arithmetic(Enum.map(list, &(1 / &1))) |
|||
end |
|||
end |
|||
list = Enum.to_list(1..10) |
|||
IO.puts "Arithmetic mean: #{am = Means.arithmetic(list)}" |
|||
IO.puts "Geometric mean: #{gm = Means.geometric(list)}" |
|||
IO.puts "Harmonic mean: #{hm = Means.harmonic(list)}" |
|||
IO.puts "(#{am} >= #{gm} >= #{hm}) is #{am >= gm and gm >= hm}"</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
Arithmetic mean: 5.5 |
|||
Geometric mean: 4.528728688116765 |
|||
Harmonic mean: 3.414171521474055 |
|||
(5.5 >= 4.528728688116765 >= 3.414171521474055) is true |
|||
</pre> |
|||
=={{header|Erlang}}== |
=={{header|Erlang}}== |
||
< |
<syntaxhighlight lang="erlang">%% Author: Abhay Jain <abhay_1303@yahoo.co.in> |
||
-module(mean_calculator). |
-module(mean_calculator). |
||
Line 544: | Line 1,043: | ||
harmonic_mean(Number, Sum) -> |
harmonic_mean(Number, Sum) -> |
||
NewSum = Sum + (1/Number), |
NewSum = Sum + (1/Number), |
||
harmonic_mean(Number+1, NewSum). </ |
harmonic_mean(Number+1, NewSum). </syntaxhighlight> |
||
{{out}} |
|||
Output: |
|||
< |
<pre>Arithmetic Mean 5.5 |
||
Geometric Mean 4.528728688116765 |
Geometric Mean 4.528728688116765 |
||
Harmonic Mean 3.414171521474055 </ |
Harmonic Mean 3.414171521474055 </pre> |
||
=={{header|ERRE}}== |
|||
<syntaxhighlight lang="text"> |
|||
PROGRAM MEANS |
|||
DIM A[9] |
|||
PROCEDURE ARITHMETIC_MEAN(A[]->M) |
|||
LOCAL S,I% |
|||
NEL%=UBOUND(A,1) |
|||
S=0 |
|||
FOR I%=0 TO NEL% DO |
|||
S+=A[I%] |
|||
END FOR |
|||
M=S/(NEL%+1) |
|||
END PROCEDURE |
|||
PROCEDURE GEOMETRIC_MEAN(A[]->M) |
|||
LOCAL S,I% |
|||
NEL%=UBOUND(A,1) |
|||
S=1 |
|||
FOR I%=0 TO NEL% DO |
|||
S*=A[I%] |
|||
END FOR |
|||
M=S^(1/(NEL%+1)) |
|||
END PROCEDURE |
|||
PROCEDURE HARMONIC_MEAN(A[]->M) |
|||
LOCAL S,I% |
|||
NEL%=UBOUND(A,1) |
|||
S=0 |
|||
FOR I%=0 TO NEL% DO |
|||
S+=1/A[I%] |
|||
END FOR |
|||
M=(NEL%+1)/S |
|||
END PROCEDURE |
|||
BEGIN |
|||
A[]=(1,2,3,4,5,6,7,8,9,10) |
|||
ARITHMETIC_MEAN(A[]->M) |
|||
PRINT("Arithmetic mean = ";M) |
|||
GEOMETRIC_MEAN(A[]->M) |
|||
PRINT("Geometric mean = ";M) |
|||
HARMONIC_MEAN(A[]->M) |
|||
PRINT("Harmonic mean = ";M) |
|||
END PROGRAM |
|||
</syntaxhighlight> |
|||
=={{header|Euler Math Toolbox}}== |
=={{header|Euler Math Toolbox}}== |
||
<syntaxhighlight lang="euler math toolbox"> |
|||
<lang Euler Math Toolbox> |
|||
>function A(x) := mean(x) |
>function A(x) := mean(x) |
||
>function G(x) := exp(mean(log(x))) |
>function G(x) := exp(mean(log(x))) |
||
Line 561: | Line 1,107: | ||
4.52872868812 |
4.52872868812 |
||
3.41417152147 |
3.41417152147 |
||
</syntaxhighlight> |
|||
</lang> |
|||
Alternatively, e.g., |
Alternatively, e.g., |
||
<syntaxhighlight lang="euler math toolbox"> |
|||
<lang Euler Math Toolbox> |
|||
>function G(x) := prod(x)^(1/length(x)) |
>function G(x) := prod(x)^(1/length(x)) |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Euphoria}}== |
=={{header|Euphoria}}== |
||
< |
<syntaxhighlight lang="euphoria">function arithmetic_mean(sequence s) |
||
atom sum |
atom sum |
||
if length(s) = 0 then |
if length(s) = 0 then |
||
Line 621: | Line 1,167: | ||
printf(1,"Harmonic: %g\n", harmonic) |
printf(1,"Harmonic: %g\n", harmonic) |
||
printf(1,"Arithmetic>=Geometric>=Harmonic: %s\n", |
printf(1,"Arithmetic>=Geometric>=Harmonic: %s\n", |
||
{true_or_false(arithmetic>=geometric and geometric>=harmonic)})</ |
{true_or_false(arithmetic>=geometric and geometric>=harmonic)})</syntaxhighlight> |
||
{{out}} |
|||
Output: |
|||
<pre>Arithmetic: 5.5 |
<pre>Arithmetic: 5.5 |
||
Geometric: 4.52873 |
Geometric: 4.52873 |
||
Line 629: | Line 1,175: | ||
Arithmetic>=Geometric>=Harmonic: true |
Arithmetic>=Geometric>=Harmonic: true |
||
</pre> |
</pre> |
||
=={{header|Excel}}== |
|||
Use the functions : AVERAGE, GEOMEAN and HARMEAN |
|||
<syntaxhighlight lang="excel"> |
|||
=AVERAGE(1;2;3;4;5;6;7;8;9;10) |
|||
=GEOMEAN(1;2;3;4;5;6;7;8;9;10) |
|||
=HARMEAN(1;2;3;4;5;6;7;8;9;10) |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
5.5 |
|||
4.528728688 |
|||
3,414171521 |
|||
</pre> |
|||
=={{header|F_Sharp|F#}}== |
|||
<syntaxhighlight lang="fsharp">let P = [1.0; 2.0; 3.0; 4.0; 5.0; 6.0; 7.0; 8.0; 9.0; 10.0] |
|||
let arithmeticMean (x : float list) = |
|||
x |> List.sum |
|||
|> (fun acc -> acc / float (List.length(x))) |
|||
let geometricMean (x: float list) = |
|||
x |> List.reduce (*) |
|||
|> (fun acc -> Math.Pow(acc, 1.0 / (float (List.length(x))))) |
|||
let harmonicMean (x: float list) = |
|||
x |> List.map (fun a -> 1.0 / a) |
|||
|> List.sum |
|||
|> (fun acc -> float (List.length(x)) / acc) |
|||
printfn "Arithmetic Mean: %A" (arithmeticMean P) |
|||
printfn "Geometric Mean: %A" (geometricMean P) |
|||
printfn "Harmonic Mean: %A" (harmonicMean P)</syntaxhighlight> |
|||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
< |
<syntaxhighlight lang="factor">: a-mean ( seq -- mean ) |
||
[ sum ] [ length ] bi / ; |
[ sum ] [ length ] bi / ; |
||
Line 638: | Line 1,220: | ||
: h-mean ( seq -- mean ) |
: h-mean ( seq -- mean ) |
||
[ length ] [ [ recip ] map-sum ] bi / ;</ |
[ length ] [ [ recip ] map-sum ] bi / ;</syntaxhighlight> |
||
( scratchpad ) 10 [1,b] [ a-mean ] [ g-mean ] [ h-mean ] tri |
( scratchpad ) 10 [1,b] [ a-mean ] [ g-mean ] [ h-mean ] tri |
||
Line 646: | Line 1,228: | ||
=={{header|Fantom}}== |
=={{header|Fantom}}== |
||
< |
<syntaxhighlight lang="fantom"> |
||
class Main |
class Main |
||
{ |
{ |
||
Line 688: | Line 1,270: | ||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Forth}}== |
=={{header|Forth}}== |
||
< |
<syntaxhighlight lang="forth">: famean ( faddr n -- f ) |
||
0e |
0e |
||
tuck floats bounds do |
tuck floats bounds do |
||
Line 717: | Line 1,299: | ||
test 10 fhmean fdup f. |
test 10 fhmean fdup f. |
||
( A G G H ) |
( A G G H ) |
||
f>= . f>= . \ -1 -1</ |
f>= . f>= . \ -1 -1</syntaxhighlight> |
||
=={{header|Fortran}}== |
=={{header|Fortran}}== |
||
{{works with|Fortran|90}} |
{{works with|Fortran|90}} |
||
< |
<syntaxhighlight lang="fortran">program Mean |
||
real :: a(10) = (/ (i, i=1,10) /) |
real :: a(10) = (/ (i, i=1,10) /) |
||
Line 736: | Line 1,318: | ||
end if |
end if |
||
end program Mean</ |
end program Mean</syntaxhighlight> |
||
=={{header|FreeBASIC}}== |
|||
<syntaxhighlight lang="freebasic"> |
|||
' FB 1.05.0 Win64 |
|||
Function ArithmeticMean(array() As Double) As Double |
|||
Dim length As Integer = Ubound(array) - Lbound(array) + 1 |
|||
Dim As Double sum = 0.0 |
|||
For i As Integer = LBound(array) To UBound(array) |
|||
sum += array(i) |
|||
Next |
|||
Return sum/length |
|||
End Function |
|||
Function GeometricMean(array() As Double) As Double |
|||
Dim length As Integer = Ubound(array) - Lbound(array) + 1 |
|||
Dim As Double product = 1.0 |
|||
For i As Integer = LBound(array) To UBound(array) |
|||
product *= array(i) |
|||
Next |
|||
Return product ^ (1.0 / length) |
|||
End Function |
|||
Function HarmonicMean(array() As Double) As Double |
|||
Dim length As Integer = Ubound(array) - Lbound(array) + 1 |
|||
Dim As Double sum = 0.0 |
|||
For i As Integer = LBound(array) To UBound(array) |
|||
sum += 1.0 / array(i) |
|||
Next |
|||
Return length / sum |
|||
End Function |
|||
Dim vector(1 To 10) As Double |
|||
For i As Integer = 1 To 10 |
|||
vector(i) = i |
|||
Next |
|||
Print "Arithmetic mean is :"; ArithmeticMean(vector()) |
|||
Print "Geometric mean is :"; GeometricMean(vector()) |
|||
Print "Harmonic mean is :"; HarmonicMean(vector()) |
|||
Print |
|||
Print "Press any key to quit the program" |
|||
Sleep |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
Arithmetic mean is : 5.5 |
|||
Geometric mean is : 4.528728688116765 |
|||
Harmonic mean is : 3.414171521474055 |
|||
</pre> |
|||
=={{header|FunL}}== |
|||
<syntaxhighlight lang="funl">import lists.zip |
|||
def |
|||
mean( s, 0 ) = product( s )^(1/s.length()) |
|||
mean( s, p ) = (1/s.length() sum( x^p | x <- s ))^(1/p) |
|||
def |
|||
monotone( [_], _ ) = true |
|||
monotone( a1:a2:as, p ) = p( a1, a2 ) and monotone( a2:as, p ) |
|||
means = [mean( 1..10, m ) | m <- [1, 0, -1]] |
|||
for (m, l) <- zip( means, ['Arithmetic', 'Geometric', 'Harmonic'] ) |
|||
println( "$l: $m" + (if m is Rational then " or ${m.doubleValue()}" else '') ) |
|||
println( monotone(means, (>=)) )</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
Arithmetic: 11/2 or 5.5 |
|||
Geometric: 4.528728688116765 |
|||
Harmonic: 25200/7381 or 3.414171521474055 |
|||
true |
|||
</pre> |
|||
=={{header|Futhark}}== |
|||
<syntaxhighlight lang="futhark"> |
|||
fun arithmetic_mean(as: [n]f64): f64 = |
|||
reduce (+) 0.0 (map (/f64(n)) as) |
|||
fun geometric_mean(as: [n]f64): f64 = |
|||
reduce (*) 1.0 (map (**(1.0/f64(n))) as) |
|||
fun harmonic_mean(as: [n]f64): f64 = |
|||
f64(n) / reduce (+) 0.0 (map (1.0/) as) |
|||
fun main(as: [n]f64): (f64,f64,f64) = |
|||
(arithmetic_mean as, |
|||
geometric_mean as, |
|||
harmonic_mean as) |
|||
</syntaxhighlight> |
|||
=={{header|FutureBasic}}== |
|||
<syntaxhighlight lang="FutureBasic> |
|||
Double ari = 1, geo = 0, har = 0 |
|||
Short i, n = 10 |
|||
for i = 1 to n |
|||
ari += i |
|||
geo *= i |
|||
har += 1 \ i |
|||
next |
|||
print "ari:", ari \ n |
|||
print "geo:", geo^( 1 \ n ) |
|||
print "har:", n \ har |
|||
handleevents |
|||
</syntaxhighlight> |
|||
<pre> |
|||
ari: 5.5 |
|||
geo: 4.528728688116765 |
|||
har: 3.414171521474055 |
|||
</pre> |
|||
=={{header|GAP}}== |
=={{header|GAP}}== |
||
< |
<syntaxhighlight lang="gap"># The first two work with rationals or with floats |
||
# (but bear in mind that support of floating point is very poor in GAP) |
# (but bear in mind that support of floating point is very poor in GAP) |
||
mean := v -> Sum(v) / Length(v); |
mean := v -> Sum(v) / Length(v); |
||
Line 755: | Line 1,460: | ||
# 3.41417 |
# 3.41417 |
||
geomean(v); |
geomean(v); |
||
# 4.52873</ |
# 4.52873</syntaxhighlight> |
||
=={{header|Go}}== |
=={{header|Go}}== |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 774: | Line 1,480: | ||
fmt.Println("A:", a, "G:", g, "H:", h) |
fmt.Println("A:", a, "G:", g, "H:", h) |
||
fmt.Println("A >= G >= H:", a >= g && g >= h) |
fmt.Println("A >= G >= H:", a >= g && g >= h) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 783: | Line 1,489: | ||
=={{header|Groovy}}== |
=={{header|Groovy}}== |
||
Solution: |
Solution: |
||
< |
<syntaxhighlight lang="groovy">def arithMean = { list -> |
||
list == null \ |
list == null \ |
||
? null \ |
? null \ |
||
Line 805: | Line 1,511: | ||
? 0 \ |
? 0 \ |
||
: list.size() / list.collect { 1.0/it }.sum() |
: list.size() / list.collect { 1.0/it }.sum() |
||
}</ |
}</syntaxhighlight> |
||
Test: |
Test: |
||
< |
<syntaxhighlight lang="groovy">def list = 1..10 |
||
def A = arithMean(list) |
def A = arithMean(list) |
||
def G = geomMean(list) |
def G = geomMean(list) |
||
Line 819: | Line 1,525: | ||
G: ${G} |
G: ${G} |
||
H: ${H} |
H: ${H} |
||
"""</ |
"""</syntaxhighlight> |
||
{{out}} |
|||
Output: |
|||
<pre>list: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] |
<pre>list: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] |
||
A: 5.5 |
A: 5.5 |
||
Line 828: | Line 1,534: | ||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
====One generalized function==== |
|||
The [[wp:Generalized mean|general function]] given here yields an arithmetic mean when its first argument is <code>1</code>, a geometric mean when its first argument is <code>0</code>, and a harmonic mean when its first argument is <code>-1</code>. |
The [[wp:Generalized mean|general function]] given here yields an arithmetic mean when its first argument is <code>1</code>, a geometric mean when its first argument is <code>0</code>, and a harmonic mean when its first argument is <code>-1</code>. |
||
< |
<syntaxhighlight lang="haskell">import Data.List (genericLength) |
||
import Control.Monad (zipWithM_) |
import Control.Monad (zipWithM_) |
||
Line 840: | Line 1,547: | ||
let ms = zipWith ((. flip mean [1..10]). (,)) "agh" [1, 0, -1] |
let ms = zipWith ((. flip mean [1..10]). (,)) "agh" [1, 0, -1] |
||
mapM_ (\(t,m) -> putStrLn $ t : ": " ++ show m) ms |
mapM_ (\(t,m) -> putStrLn $ t : ": " ++ show m) ms |
||
putStrLn $ " a >= g >= h is " ++ show ((\(_,[a,g,h])-> a>=g && g>=h) (unzip ms))</ |
putStrLn $ " a >= g >= h is " ++ show ((\(_,[a,g,h])-> a>=g && g>=h) (unzip ms))</syntaxhighlight> |
||
====Three applicatively defined functions==== |
|||
These three functions (each combining the length of a list with some kind of fold over the elements of that same list), all share the same applicative structure. |
|||
<syntaxhighlight lang="haskell">import Data.List (genericLength) |
|||
-- ARITHMETIC, GEOMETRIC AND HARMONIC MEANS --------------- |
|||
arithmetic, geometric, harmonic :: [Double] -> Double |
|||
arithmetic = (/) . sum <*> genericLength |
|||
geometric = (**) . product <*> ((1 /) . genericLength) |
|||
harmonic = (/) . genericLength <*> foldr ((+) . (1 /)) 0 |
|||
-- TEST --------------------------------------------------- |
|||
xs :: [Double] |
|||
xs = [arithmetic, geometric, harmonic] <*> [[1 .. 10]] |
|||
main :: IO () |
|||
main = |
|||
(putStrLn . unlines) |
|||
[ zip ["Arithmetic", "Geometric", "Harmonic"] xs >>= show |
|||
, mappend "\n A >= G >= H is " $ -- |
|||
(show . and) $ zipWith (>=) xs (tail xs) |
|||
]</syntaxhighlight> |
|||
{{Out}} |
|||
<pre>("Arithmetic",5.5)("Geometric",4.528728688116765)("Harmonic",3.414171521474055) |
|||
a >= g >= h is True</pre> |
|||
=={{header|HicEst}}== |
=={{header|HicEst}}== |
||
< |
<syntaxhighlight lang="hicest">AGH = ALIAS( A, G, H ) ! named vector elements |
||
AGH = (0, 1, 0) |
AGH = (0, 1, 0) |
||
DO i = 1, 10 |
DO i = 1, 10 |
||
Line 852: | Line 1,588: | ||
AGH = (A/10, G^0.1, 10/H) |
AGH = (A/10, G^0.1, 10/H) |
||
WRITE(ClipBoard, Name) AGH, "Result = " // (A>=G) * (G>=H)</ |
WRITE(ClipBoard, Name) AGH, "Result = " // (A>=G) * (G>=H)</syntaxhighlight> |
||
! A=5.5; G=4.528728688; H=3.414171521; Result = 1; |
! A=5.5; G=4.528728688; H=3.414171521; Result = 1; |
||
=={{header|Icon}} and {{header|Unicon}}== |
=={{header|Icon}} and {{header|Unicon}}== |
||
< |
<syntaxhighlight lang="icon">link numbers # for a/g/h means |
||
procedure main() |
procedure main() |
||
Line 867: | Line 1,603: | ||
write(" a >= g >= h is ", if a >= g >= h then "true" else "false") |
write(" a >= g >= h is ", if a >= g >= h then "true" else "false") |
||
end |
end |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{libheader|Icon Programming Library}} |
{{libheader|Icon Programming Library}} |
||
[http://www.cs.arizona.edu/icon/library/src/procs/numbers.icn numbers:amean, numbers:gmean, and numbers:hmean] are shown below: |
[http://www.cs.arizona.edu/icon/library/src/procs/numbers.icn numbers:amean, numbers:gmean, and numbers:hmean] are shown below: |
||
< |
<syntaxhighlight lang="icon">procedure amean(L[]) #: arithmetic mean |
||
local m |
local m |
||
if *L = 0 then fail |
if *L = 0 then fail |
||
Line 900: | Line 1,636: | ||
} |
} |
||
return *L / m |
return *L / m |
||
end</ |
end</syntaxhighlight> |
||
{{out}} |
|||
Sample output: |
|||
<pre>#means.exe |
<pre>#means.exe |
||
x := [ 1 2 3 4 5 6 7 8 9 10 ] |
x := [ 1 2 3 4 5 6 7 8 9 10 ] |
||
Line 909: | Line 1,645: | ||
Harmonic mean:3.414171521474055 |
Harmonic mean:3.414171521474055 |
||
a >= g >= h is true</pre> |
a >= g >= h is true</pre> |
||
=={{header|IS-BASIC}}== |
|||
<syntaxhighlight lang="is-basic">100 PROGRAM "Averages.bas" |
|||
110 NUMERIC ARR(1 TO 10) |
|||
120 FOR I=LBOUND(ARR) TO UBOUND(ARR) |
|||
130 LET ARR(I)=I |
|||
140 NEXT |
|||
150 PRINT "Arithmetic mean =";ARITHM(ARR) |
|||
160 PRINT "Geometric mean =";GEOMETRIC(ARR) |
|||
170 PRINT "Harmonic mean =";HARMONIC(ARR) |
|||
180 DEF ARITHM(REF A) |
|||
190 LET T=0 |
|||
200 FOR I=LBOUND(A) TO UBOUND(A) |
|||
210 LET T=T+A(I) |
|||
220 NEXT |
|||
230 LET ARITHM=T/SIZE(A) |
|||
240 END DEF |
|||
250 DEF GEOMETRIC(REF A) |
|||
260 LET T=1 |
|||
270 FOR I=LBOUND(A) TO UBOUND(A) |
|||
280 LET T=T*A(I) |
|||
290 NEXT |
|||
300 LET GEOMETRIC=T^(1/SIZE(A)) |
|||
310 END DEF |
|||
320 DEF HARMONIC(REF A) |
|||
330 LET T=0 |
|||
340 FOR I=LBOUND(A) TO UBOUND(A) |
|||
350 LET T=T+(1/A(I)) |
|||
360 NEXT |
|||
370 LET HARMONIC=SIZE(A)/T |
|||
380 END DEF</syntaxhighlight> |
|||
=={{header|J}}== |
=={{header|J}}== |
||
'''Solution:''' |
'''Solution:''' |
||
< |
<syntaxhighlight lang="j">amean=: +/ % # |
||
gmean=: # %: */ |
gmean=: # %: */ |
||
hmean=: amean&.:%</ |
hmean=: amean&.:%</syntaxhighlight> |
||
'''Example Usage:''' |
'''Example Usage:''' |
||
< |
<syntaxhighlight lang="j"> (amean , gmean , hmean) >: i. 10 |
||
5.5 4.528729 3.414172 |
5.5 4.528729 3.414172 |
||
assert 2 >:/\ (amean , gmean , hmean) >: i. 10 NB. check amean >= gmean and gmean >= hmean</ |
assert 2 >:/\ (amean , gmean , hmean) >: i. 10 NB. check amean >= gmean and gmean >= hmean</syntaxhighlight> |
||
Note that gmean could have instead been defined as mean under logarithm, for example: |
Note that gmean could have instead been defined as mean under logarithm, for example: |
||
< |
<syntaxhighlight lang="j">gmean=:amean&.:^.</syntaxhighlight> |
||
(and this variant should probably be preferred - especially if the argument list is long, to avoid problems with floating point infinity.) |
|||
=={{header|Java}}== |
=={{header|Java}}== |
||
< |
<syntaxhighlight lang="java">import java.util.Arrays; |
||
import java.util.List; |
import java.util.List; |
||
Line 966: | Line 1,735: | ||
System.out.format("A >= G is %b, G >= H is %b%n", (arithmetic >= geometric), (geometric >= harmonic)); |
System.out.format("A >= G is %b, G >= H is %b%n", (arithmetic >= geometric), (geometric >= harmonic)); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
|||
Output: |
|||
<pre>A = 5.500000 G = 4.528729 H = 3.414172 |
<pre>A = 5.500000 G = 4.528729 H = 3.414172 |
||
A >= G is true, G >= H is true</pre> |
A >= G is true, G >= H is true</pre> |
||
{{works with|Java|1.8}} |
|||
We can rewrite the 3 methods using the new JAVA Stream API: |
|||
<syntaxhighlight lang="java"> |
|||
public static double arithmAverage(double array[]){ |
|||
if (array == null ||array.length == 0) { |
|||
return 0.0; |
|||
} |
|||
else { |
|||
return DoubleStream.of(array).average().getAsDouble(); |
|||
} |
|||
} |
|||
public static double geomAverage(double array[]){ |
|||
if (array == null ||array.length == 0) { |
|||
return 0.0; |
|||
} |
|||
else { |
|||
double aver = DoubleStream.of(array).reduce(1, (x, y) -> x * y); |
|||
return Math.pow(aver, 1.0 / array.length); |
|||
} |
|||
} |
|||
public static double harmAverage(double array[]){ |
|||
if (array == null ||array.length == 0) { |
|||
return 0.0; |
|||
} |
|||
else { |
|||
double aver = DoubleStream.of(array) |
|||
// remove null values |
|||
.filter(n -> n > 0.0) |
|||
// generate 1/n array |
|||
.map( n-> 1.0/n) |
|||
// accumulating |
|||
.reduce(0, (x, y) -> x + y); |
|||
// just this reduce is not working- need to do in 2 steps |
|||
// .reduce(0, (x, y) -> 1.0/x + 1.0/y); |
|||
return array.length / aver ; |
|||
} |
|||
} |
|||
</syntaxhighlight> |
|||
=={{header|JavaScript}}== |
=={{header|JavaScript}}== |
||
{{works with|JavaScript|1.8}}, |
|||
{{works with|Firefox|3.0}} |
|||
<lang javascript>function arithmetic_mean(ary) { |
|||
var sum = ary.reduce(function(s,x) {return (s+x)}, 0); |
|||
return (sum / ary.length); |
|||
} |
|||
===ES5=== |
|||
function geometic_mean(ary) { |
|||
<syntaxhighlight lang="javascript">(function () { |
|||
var product = ary.reduce(function(s,x) {return (s*x)}, 1); |
|||
'use strict'; |
|||
return Math.pow(product, 1/ary.length); |
|||
} |
|||
// arithmetic_mean :: [Number] -> Number |
|||
function harmonic_mean(ary) { |
|||
function arithmetic_mean(ns) { |
|||
var sum_of_inv = ary.reduce(function(s,x) {return (s + 1/x)}, 0); |
|||
return ( |
|||
return (ary.length / sum_of_inv); |
|||
ns.reduce( // sum |
|||
} |
|||
function (sum, n) { |
|||
return (sum + n); |
|||
}, |
|||
0 |
|||
) / ns.length |
|||
); |
|||
} |
|||
// geometric_mean :: [Number] -> Number |
|||
var ary = [1,2,3,4,5,6,7,8,9,10]; |
|||
function geometric_mean(ns) { |
|||
var A = arithmetic_mean(ary); |
|||
return Math.pow( |
|||
var G = geometic_mean(ary); |
|||
ns.reduce( // product |
|||
var H = harmonic_mean(ary); |
|||
function (product, n) { |
|||
return (product * n); |
|||
}, |
|||
1 |
|||
), |
|||
1 / ns.length |
|||
); |
|||
} |
|||
// harmonic_mean :: [Number] -> Number |
|||
print("is A >= G >= H ? " + (A >= G && G >= H ? "yes" : "no"));</lang> |
|||
function harmonic_mean(ns) { |
|||
return ( |
|||
ns.length / ns.reduce( // sum of inverses |
|||
function (invSum, n) { |
|||
return (invSum + (1 / n)); |
|||
}, |
|||
0 |
|||
) |
|||
); |
|||
} |
|||
var values = [arithmetic_mean, geometric_mean, harmonic_mean] |
|||
.map(function (f) { |
|||
return f([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); |
|||
}), |
|||
mean = { |
|||
Arithmetic: values[0], // arithmetic |
|||
Geometric: values[1], // geometric |
|||
Harmonic: values[2] // harmonic |
|||
} |
|||
return JSON.stringify({ |
|||
values: mean, |
|||
test: "is A >= G >= H ? " + |
|||
( |
|||
mean.Arithmetic >= mean.Geometric && |
|||
mean.Geometric >= mean.Harmonic ? "yes" : "no" |
|||
) |
|||
}, null, 2); |
|||
})(); |
|||
</syntaxhighlight> |
|||
{{Out}} |
|||
<syntaxhighlight lang="javascript">{ |
|||
"values": { |
|||
"Arithmetic": 5.5, |
|||
"Geometric": 4.528728688116765, |
|||
"Harmonic": 3.414171521474055 |
|||
}, |
|||
"test": "is A >= G >= H ? yes" |
|||
}</syntaxhighlight> |
|||
===ES6=== |
|||
<syntaxhighlight lang="javascript">(() => { |
|||
// arithmeticMean :: [Number] -> Number |
|||
const arithmeticMean = xs => |
|||
foldl((sum, n) => sum + n, 0, xs) / length(xs); |
|||
// geometricMean :: [Number] -> Number |
|||
const geometricMean = xs => |
|||
raise(foldl((product, x) => product * x, 1, xs), 1 / length(xs)); |
|||
// harmonicMean :: [Number] -> Number |
|||
const harmonicMean = xs => |
|||
length(xs) / foldl((invSum, n) => invSum + (1 / n), 0, xs); |
|||
// GENERIC FUNCTIONS ------------------------------------------------------ |
|||
// A list of functions applied to a list of arguments |
|||
// <*> :: [(a -> b)] -> [a] -> [b] |
|||
const ap = (fs, xs) => // |
|||
[].concat.apply([], fs.map(f => // |
|||
[].concat.apply([], xs.map(x => [f(x)])))); |
|||
// foldl :: (b -> a -> b) -> b -> [a] -> b |
|||
const foldl = (f, a, xs) => xs.reduce(f, a); |
|||
// length :: [a] -> Int |
|||
const length = xs => xs.length; |
|||
// mapFromList :: [(k, v)] -> Dictionary |
|||
const mapFromList = kvs => |
|||
foldl((a, [k, v]) => |
|||
(a[(typeof k === 'string' && k) || show(k)] = v, a), {}, kvs); |
|||
// raise :: Num -> Int -> Num |
|||
const raise = (n, e) => Math.pow(n, e); |
|||
// show :: a -> String |
|||
// show :: a -> Int -> String |
|||
const show = (...x) => |
|||
JSON.stringify.apply( |
|||
null, x.length > 1 ? [x[0], null, x[1]] : x |
|||
); |
|||
// zip :: [a] -> [b] -> [(a,b)] |
|||
const zip = (xs, ys) => |
|||
xs.slice(0, Math.min(xs.length, ys.length)) |
|||
.map((x, i) => [x, ys[i]]); |
|||
// TEST ------------------------------------------------------------------- |
|||
// mean :: Dictionary |
|||
const mean = mapFromList(zip( |
|||
['Arithmetic', 'Geometric', 'Harmonic'], |
|||
ap([arithmeticMean, geometricMean, harmonicMean], [ |
|||
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10] |
|||
]) |
|||
)); |
|||
return show({ |
|||
values: mean, |
|||
test: `is A >= G >= H ? ${mean.Arithmetic >= mean.Geometric && |
|||
mean.Geometric >= mean.Harmonic ? "yes" : "no"}` |
|||
}, 2); |
|||
})();</syntaxhighlight> |
|||
{{Out}} |
|||
<syntaxhighlight lang="javascript">{ |
|||
"values": { |
|||
"Arithmetic": 5.5, |
|||
"Geometric": 4.528728688116765, |
|||
"Harmonic": 3.414171521474055 |
|||
}, |
|||
"test": "is A >= G >= H ? yes" |
|||
}</syntaxhighlight> |
|||
=={{header|jq}}== |
|||
<syntaxhighlight lang="jq">def amean: add/length; |
|||
def logProduct: map(log) | add; |
|||
def gmean: (logProduct / length) | exp; |
|||
def hmean: length / (map(1/.) | add); |
|||
# Tasks: |
|||
[range(1;11) ] | [amean, gmean, hmean] as $ans |
|||
| ( $ans[], |
|||
"amean > gmean > hmean => \($ans[0] > $ans[1] and $ans[1] > $ans[2] )" ) |
|||
</syntaxhighlight> |
|||
{{Out}} |
|||
<pre>5.5 |
|||
4.528728688116766 |
|||
3.414171521474055 |
|||
"amean > gmean > hmean => true"</pre> |
|||
=={{header|Julia}}== |
|||
Julia has a `mean` function to compute the arithmetic mean of a collections |
|||
of numbers. We can redefine it as follows. |
|||
<syntaxhighlight lang="julia">amean(A) = sum(A)/length(A) |
|||
gmean(A) = prod(A)^(1/length(A)) |
|||
hmean(A) = length(A)/sum(1./A)</syntaxhighlight> |
|||
{{Out}} |
|||
<pre>julia> map(f-> f(1:10), [amean, gmean, hmean]) |
|||
3-element Array{Float64,1}: |
|||
5.5 |
|||
4.52873 |
|||
3.41417 |
|||
julia> ans[1] > ans[2] > ans[3] |
|||
true</pre> |
|||
=={{header|K}}== |
|||
<syntaxhighlight lang="k"> |
|||
am:{(+/x)%#x} |
|||
gm:{(*/x)^(%#x)} |
|||
hm:{(#x)%+/%:'x} |
|||
{(am x;gm x;hm x)} 1+!10 |
|||
5.5 4.528729 3.414172 |
|||
</syntaxhighlight> |
|||
=={{header|Kotlin}}== |
|||
<syntaxhighlight lang="kotlin">import kotlin.math.round |
|||
import kotlin.math.pow |
|||
fun Collection<Double>.geometricMean() = |
|||
if (isEmpty()) Double.NaN |
|||
else (reduce { n1, n2 -> n1 * n2 }).pow(1.0 / size) |
|||
fun Collection<Double>.harmonicMean() = |
|||
if (isEmpty() || contains(0.0)) Double.NaN |
|||
else size / fold(0.0) { n1, n2 -> n1 + 1.0 / n2 } |
|||
fun Double.toFixed(len: Int = 6) = |
|||
round(this * 10.0.pow(len)) / 10.0.pow(len) |
|||
fun main() { |
|||
val list = listOf(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0) |
|||
val a = list.average() // arithmetic mean |
|||
val g = list.geometricMean() |
|||
val h = list.harmonicMean() |
|||
println("A = $a G = ${g.toFixed()} H = ${h.toFixed()}") |
|||
println("A >= G is ${a >= g}, G >= H is ${g >= h}") |
|||
require(g in h..a) |
|||
}</syntaxhighlight> |
|||
{{out}} |
|||
<pre>A = 5.500000 G = 4.528729 H = 3.414172 |
|||
A >= G is true, G >= H is true</pre> |
|||
=={{header|Lasso}}== |
=={{header|Lasso}}== |
||
<lang |
<syntaxhighlight lang="lasso">define arithmetic_mean(a::staticarray)::decimal => { |
||
local(o = array) |
|||
loop(-from=#start,-to=#end) => { |
|||
#o->insert(loop_count) |
|||
} |
|||
return #o |
|||
} |
|||
define arithmetic_mean(a::array)::decimal => { |
|||
//sum of the list divided by its length |
//sum of the list divided by its length |
||
return (with e in #a sum #e) / decimal(#a->size) |
return (with e in #a sum #e) / decimal(#a->size) |
||
} |
} |
||
define geometric_mean(a:: |
define geometric_mean(a::staticarray)::decimal => { |
||
// The geometric mean is the nth root of the product of the list |
// The geometric mean is the nth root of the product of the list |
||
local(prod = 1) |
local(prod = 1) |
||
Line 1,016: | Line 2,016: | ||
return math_pow(#prod,1/decimal(#a->size)) |
return math_pow(#prod,1/decimal(#a->size)) |
||
} |
} |
||
define harmonic_mean(a:: |
define harmonic_mean(a::staticarray)::decimal => { |
||
// The harmonic mean is n divided by the sum of the reciprocal of each item in the list |
// The harmonic mean is n divided by the sum of the reciprocal of each item in the list |
||
return decimal(#a->size)/(with e in #a sum 1/decimal(#e)) |
return decimal(#a->size)/(with e in #a sum 1/decimal(#e)) |
||
} |
} |
||
arithmetic_mean( |
arithmetic_mean(generateSeries(1,10)->asStaticArray) |
||
geometric_mean( |
geometric_mean(generateSeries(1,10)->asStaticArray) |
||
harmonic_mean( |
harmonic_mean(generateSeries(1,10)->asStaticArray)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,031: | Line 2,031: | ||
=={{header|Liberty BASIC}}== |
=={{header|Liberty BASIC}}== |
||
< |
<syntaxhighlight lang="lb">for i = 1 to 10 |
||
a = a + i |
a = a + i |
||
next |
next |
||
Line 1,056: | Line 2,056: | ||
print "False" |
print "False" |
||
end if |
end if |
||
</ |
</syntaxhighlight> |
||
=={{header|Logo}}== |
=={{header|Logo}}== |
||
< |
<syntaxhighlight lang="logo">to compute_means :count |
||
local "sum |
local "sum |
||
make "sum 0 |
make "sum 0 |
||
Line 1,080: | Line 2,081: | ||
print sentence [Geometric mean is] item 2 :means |
print sentence [Geometric mean is] item 2 :means |
||
print sentence [Harmonic mean is] item 3 :means |
print sentence [Harmonic mean is] item 3 :means |
||
bye</ |
bye</syntaxhighlight> |
||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
< |
<syntaxhighlight lang="lua">function fsum(f, a, ...) return a and f(a) + fsum(f, ...) or 0 end |
||
function pymean(t, f, finv) return finv(fsum(f, unpack(t)) / #t) end |
function pymean(t, f, finv) return finv(fsum(f, unpack(t)) / #t) end |
||
nums = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} |
nums = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} |
||
Line 1,094: | Line 2,095: | ||
h = pymean(nums, function(n) return 1/n end, function(n) return 1/n end) |
h = pymean(nums, function(n) return 1/n end, function(n) return 1/n end) |
||
print(a, g, h) |
print(a, g, h) |
||
assert(a >= g and g >= h)</ |
assert(a >= g and g >= h)</syntaxhighlight> |
||
=={{header| |
=={{header|M2000 Interpreter}}== |
||
Dimension(m,0) is the base (lower bound) for each dimension in an array, and can be 0 or 1. |
|||
<lang Mathematica>Print["{Arithmetic Mean, Geometric Mean, Harmonic Mean} = ", |
|||
Len(a) or len(M()) return length of a pointer to array and an array, as number of array elements. |
|||
N@Through[{Mean, GeometricMean, HarmonicMean}[Range@10]]]</lang> |
|||
For one dimension arrays len() is equal to Dimension(m(),1) where 1 is the first dimension |
|||
Solution: |
|||
Dim A(10,10) : Print Len(A())=100 |
|||
<syntaxhighlight lang="m2000 interpreter"> |
|||
Module CheckIt { |
|||
sum=lambda -> { |
|||
Read m as array |
|||
if len(m)=0 then =0 : exit |
|||
sum=Array(m, Dimension(m,0)) |
|||
If len(m)=1 then =sum : exit |
|||
k=each(m,2,-1) |
|||
While k { |
|||
sum+=Array(k) |
|||
} |
|||
=sum |
|||
} |
|||
mean=lambda sum (a as array) ->{ |
|||
=sum(a)/len(a) |
|||
} |
|||
prod=lambda -> { |
|||
m=array |
|||
if len(m)=0 then =0 : exit |
|||
prod=Array(m, Dimension(m,0)) |
|||
If len(m)=1 then =prod : exit |
|||
k=each(m,2,-1) |
|||
While k { |
|||
prod*=Array(k) |
|||
} |
|||
=prod |
|||
} |
|||
geomean=lambda prod (a as array) -> { |
|||
=prod(a)^(1/len(a)) |
|||
} |
|||
harmomean=lambda (a as array) -> { |
|||
if len(a)=0 then =0 : exit |
|||
sum=1/Array(a, Dimension(a,0)) |
|||
If len(a)=1 then =1/sum : exit |
|||
k=each(a,2,-1) |
|||
While k { |
|||
sum+=1/Array(k) |
|||
} |
|||
=len(a)/sum |
|||
} |
|||
Print sum((1,2,3,4,5))=15 |
|||
Print prod((1,2,3,4,5))=120 |
|||
Print mean((1,2,3,4,5))==3 |
|||
\\ use == to apply rounding before comparison |
|||
Print geomean((1,2,3,4,5))==2.60517108469735 |
|||
Print harmomean((1,2,3,4,5))==2.18978102189784 |
|||
Generator =lambda x=1 ->{=x : x++} |
|||
dim a(10)<<Generator() |
|||
Print mean(a())==5.5 |
|||
Print geomean(a())==4.52872868811677 |
|||
Print harmomean(a())==3.41417152147412 |
|||
} |
|||
CheckIt |
|||
</syntaxhighlight> |
|||
=={{header|Maple}}== |
|||
<syntaxhighlight lang="maple">x := [ seq( 1 .. 10 ) ]; |
|||
Means := proc( x ) |
|||
uses Statistics; |
|||
return Mean( x ), GeometricMean( x ), HarmonicMean( x ); |
|||
end proc: |
|||
Arithmeticmean, Geometricmean, Harmonicmean := Means( x ); |
|||
is( Arithmeticmean >= Geometricmean and Geometricmean >= Harmonicmean ); |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
Arithmeticmean, Geometricmean, Harmonicmean := 5.50000000000000, 4.52872868811677, 3.41417152147406 |
|||
true |
|||
</pre> |
|||
=={{header|Mathematica}} / {{header|Wolfram Language}}== |
|||
<syntaxhighlight lang="mathematica">Print["{Arithmetic Mean, Geometric Mean, Harmonic Mean} = ", |
|||
N@Through[{Mean, GeometricMean, HarmonicMean}[Range@10]]]</syntaxhighlight> |
|||
{{out}} |
|||
<pre>{Arithmetic Mean, Geometric Mean, Harmonic Mean} = {5.5,4.52873,3.41417}</pre> |
<pre>{Arithmetic Mean, Geometric Mean, Harmonic Mean} = {5.5,4.52873,3.41417}</pre> |
||
=={{header|MATLAB}}== |
=={{header|MATLAB}}== |
||
< |
<syntaxhighlight lang="matlab">function [A,G,H] = pythagoreanMeans(list) |
||
A = mean(list); |
A = mean(list); |
||
Line 1,109: | Line 2,190: | ||
H = harmmean(list); |
H = harmmean(list); |
||
end</ |
end</syntaxhighlight> |
||
A solution that works for both, Matlab and Octave, is this |
A solution that works for both, Matlab and Octave, is this |
||
< |
<syntaxhighlight lang="matlab">function [A,G,H] = pythagoreanMeans(list) |
||
A = mean(list); % arithmetic mean |
A = mean(list); % arithmetic mean |
||
G = exp(mean(log(list))); % geometric mean |
G = exp(mean(log(list))); % geometric mean |
||
H = 1./mean(1./list); % harmonic mean |
H = 1./mean(1./list); % harmonic mean |
||
end</ |
end</syntaxhighlight> |
||
Solution: |
Solution: |
||
< |
<syntaxhighlight lang="matlab">>> [A,G,H]=pythagoreanMeans((1:10)) |
||
A = |
A = |
||
Line 1,134: | Line 2,215: | ||
H = |
H = |
||
3.414171521474055</ |
3.414171521474055</syntaxhighlight> |
||
=={{header|Maxima}}== |
=={{header|Maxima}}== |
||
< |
<syntaxhighlight lang="maxima">/* built-in */ |
||
L: makelist(i, i, 1, 10)$ |
L: makelist(i, i, 1, 10)$ |
||
mean(L), numer; /* 5.5 */ |
mean(L), numer; /* 5.5 */ |
||
geometric_mean(L), numer; /* 4.528728688116765 */ |
geometric_mean(L), numer; /* 4.528728688116765 */ |
||
harmonic_mean(L), numer; /* 3.414171521474055 */</ |
harmonic_mean(L), numer; /* 3.414171521474055 */</syntaxhighlight> |
||
=={{header|min}}== |
|||
<syntaxhighlight lang="min">'avg ^A |
|||
(dup product 1 rolldown size / pow) ^G |
|||
('size keep (1 swap /) (+) map-reduce /) ^H |
|||
(((1 10)) range (((A) (G) (H))) cleave) => (puts!) foreach</syntaxhighlight> |
|||
{{out}} |
|||
<pre>5.5 |
|||
4.528728688116765 |
|||
3.414171521474055</pre> |
|||
=={{header|Modula-2}}== |
|||
<syntaxhighlight lang="modula2">MODULE PythagoreanMeans; |
|||
FROM FormatString IMPORT FormatString; |
|||
FROM LongMath IMPORT power; |
|||
FROM LongStr IMPORT RealToStr; |
|||
FROM Terminal IMPORT WriteString,WriteLn,ReadChar; |
|||
PROCEDURE ArithmeticMean(numbers : ARRAY OF LONGREAL) : LONGREAL; |
|||
VAR |
|||
i,cnt : CARDINAL; |
|||
mean : LONGREAL; |
|||
BEGIN |
|||
mean := 0.0; |
|||
cnt := 0; |
|||
FOR i:=0 TO HIGH(numbers) DO |
|||
mean := mean + numbers[i]; |
|||
INC(cnt); |
|||
END; |
|||
RETURN mean / LFLOAT(cnt) |
|||
END ArithmeticMean; |
|||
PROCEDURE GeometricMean(numbers : ARRAY OF LONGREAL) : LONGREAL; |
|||
VAR |
|||
i,cnt : CARDINAL; |
|||
mean : LONGREAL; |
|||
BEGIN |
|||
mean := 1.0; |
|||
cnt := 0; |
|||
FOR i:=0 TO HIGH(numbers) DO |
|||
mean := mean * numbers[i]; |
|||
INC(cnt); |
|||
END; |
|||
RETURN power(mean, 1.0 / LFLOAT(cnt)) |
|||
END GeometricMean; |
|||
PROCEDURE HarmonicMean(numbers : ARRAY OF LONGREAL) : LONGREAL; |
|||
VAR |
|||
i,cnt : CARDINAL; |
|||
mean : LONGREAL; |
|||
BEGIN |
|||
mean := 0.0; |
|||
cnt := 0; |
|||
FOR i:=0 TO HIGH(numbers) DO |
|||
mean := mean + ( 1.0 / numbers[i]); |
|||
INC(cnt); |
|||
END; |
|||
RETURN LFLOAT(cnt) / mean |
|||
END HarmonicMean; |
|||
CONST Size = 10; |
|||
TYPE DA = ARRAY[1..Size] OF LONGREAL; |
|||
VAR |
|||
buf : ARRAY[0..63] OF CHAR; |
|||
array : DA; |
|||
arithmetic,geometric,harmonic : LONGREAL; |
|||
BEGIN |
|||
array := DA{1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0}; |
|||
arithmetic := ArithmeticMean(array); |
|||
geometric := GeometricMean(array); |
|||
harmonic := HarmonicMean(array); |
|||
WriteString("A = "); |
|||
RealToStr(arithmetic, buf); |
|||
WriteString(buf); |
|||
WriteString(" G = "); |
|||
RealToStr(geometric, buf); |
|||
WriteString(buf); |
|||
WriteString(" H = "); |
|||
RealToStr(harmonic, buf); |
|||
WriteString(buf); |
|||
WriteLn; |
|||
FormatString("A >= G is %b, G >= H is %b\n", buf, arithmetic >= geometric, geometric >= harmonic); |
|||
WriteString(buf); |
|||
ReadChar |
|||
END PythagoreanMeans.</syntaxhighlight> |
|||
=={{header|MUMPS}}== |
=={{header|MUMPS}}== |
||
< |
<syntaxhighlight lang="mumps">Pyth(n) New a,ii,g,h,x |
||
For ii=1:1:n set x(ii)=ii |
For ii=1:1:n set x(ii)=ii |
||
; |
; |
||
Line 1,167: | Line 2,340: | ||
Pythagorean means for 1..10: |
Pythagorean means for 1..10: |
||
Average = 5.5 >= Geometric 4.528728688116178495 >= harmonic 3.414171521474055006</ |
Average = 5.5 >= Geometric 4.528728688116178495 >= harmonic 3.414171521474055006</syntaxhighlight> |
||
=={{header|NetRexx}}== |
=={{header|NetRexx}}== |
||
{{trans|ooRexx}} |
{{trans|ooRexx}} |
||
< |
<syntaxhighlight lang="netrexx">/* NetRexx */ |
||
options replace format comments java crossref symbols nobinary |
options replace format comments java crossref symbols nobinary |
||
Line 1,219: | Line 2,392: | ||
-- problem here... |
-- problem here... |
||
return numbers.size / mean |
return numbers.size / mean |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
|||
'''Output:''' |
|||
<pre> |
<pre> |
||
Arithmetic = 5.5, Geometric = 4.528728688116765, Harmonic = 3.4141715214740550062 |
Arithmetic = 5.5, Geometric = 4.528728688116765, Harmonic = 3.4141715214740550062 |
||
</pre> |
</pre> |
||
=={{header|Nim}}== |
|||
<syntaxhighlight lang="nim">import math, sequtils, sugar |
|||
proc amean(num: seq[float]): float = |
|||
sum(num) / float(len(num)) |
|||
proc gmean(num: seq[float]): float = |
|||
result = 1 |
|||
for n in num: result *= n |
|||
result = pow(result, 1.0 / float(num.len)) |
|||
proc hmean(num: seq[float]): float = |
|||
for n in num: result += 1.0 / n |
|||
result = float(num.len) / result |
|||
proc ameanFunctional(num: seq[float]): float = |
|||
sum(num) / float(num.len) |
|||
proc gmeanFunctional(num: seq[float]): float = |
|||
num.foldl(a * b).pow(1.0 / float(num.len)) |
|||
proc hmeanFunctional(num: seq[float]): float = |
|||
float(num.len) / sum(num.mapIt(1.0 / it)) |
|||
let numbers = toSeq(1..10).map((x: int) => float(x)) |
|||
echo amean(numbers), " ", gmean(numbers), " ", hmean(numbers)</syntaxhighlight> |
|||
{{out}} |
|||
<pre>5.5 4.528728688116765 3.414171521474055</pre> |
|||
=={{header|Oberon-2}}== |
=={{header|Oberon-2}}== |
||
Oxford Oberon-2 |
Oxford Oberon-2 |
||
< |
<syntaxhighlight lang="oberon2"> |
||
MODULE PythMean; |
MODULE PythMean; |
||
IMPORT Out, ML := MathL; |
IMPORT Out, ML := MathL; |
||
Line 1,263: | Line 2,465: | ||
END PythMean. |
END PythMean. |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
|||
Output: |
|||
<pre> |
<pre> |
||
A(1 .. 10): 5.50000000000 |
A(1 .. 10): 5.50000000000 |
||
Line 1,270: | Line 2,472: | ||
H(1 .. 10): 3.41417152147 |
H(1 .. 10): 3.41417152147 |
||
</pre> |
</pre> |
||
=={{header|Objeck}}== |
|||
{{trans|Java}} |
|||
<syntaxhighlight lang="objeck">class PythagMeans { |
|||
function : Main(args : String[]) ~ Nil { |
|||
array := [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0]; |
|||
arithmetic := ArithmeticMean(array); |
|||
geometric := GeometricMean(array); |
|||
harmonic := HarmonicMean(array); |
|||
arith_geo := arithmetic >= geometric; |
|||
geo_harm := geometric >= harmonic; |
|||
"A = {$arithmetic}, G = {$geometric}, H = {$harmonic}"->PrintLine(); |
|||
"A >= G is {$arith_geo}, G >= H is {$geo_harm}"->PrintLine(); |
|||
} |
|||
function : native : ArithmeticMean(numbers : Float[]) ~ Float { |
|||
if(numbers->Size() = 0) { return -1.0; }; |
|||
mean := 0.0; |
|||
each(i : numbers) { |
|||
mean += numbers[i]; |
|||
}; |
|||
return mean / numbers->Size(); |
|||
} |
|||
function : native : GeometricMean(numbers : Float[]) ~ Float { |
|||
if(numbers->Size() = 0) { return -1.0; }; |
|||
mean := 1.0; |
|||
each(i : numbers) { |
|||
mean *= numbers[i]; |
|||
}; |
|||
return mean->Power(1.0 / numbers->Size()); |
|||
} |
|||
function : native : HarmonicMean(numbers : Float[]) ~ Float { |
|||
if(numbers->Size() = 0) { return -1.0; }; |
|||
mean := 0.0; |
|||
each(i : numbers) { |
|||
mean += (1.0 / numbers[i]); |
|||
}; |
|||
return numbers->Size() / mean; |
|||
} |
|||
}</syntaxhighlight> |
|||
Output: |
|||
<pre> |
|||
A = 5.500, G = 4.529, H = 3.414 |
|||
A >= G is true, G >= H is true |
|||
</pre> |
|||
=={{header|OCaml}}== |
=={{header|OCaml}}== |
||
The three means in one function |
The three means in one function |
||
< |
<syntaxhighlight lang="ocaml">let means v = |
||
let n = Array.length v |
let n = Array.length v |
||
and a = ref 0.0 |
and a = ref 0.0 |
||
Line 1,286: | Line 2,545: | ||
let nn = float_of_int n in |
let nn = float_of_int n in |
||
(!a /. nn, !b ** (1.0/.nn), nn /. !c) |
(!a /. nn, !b ** (1.0/.nn), nn /. !c) |
||
;;</ |
;;</syntaxhighlight> |
||
{{out}} |
|||
Sample output: |
|||
< |
<pre>means (Array.init 10 (function i -> (float_of_int (i+1)))) ;; |
||
(* (5.5, 4.5287286881167654, 3.4141715214740551) *)</ |
(* (5.5, 4.5287286881167654, 3.4141715214740551) *)</pre> |
||
Another implementation using <code>[http://caml.inria.fr/pub/docs/manual-ocaml/libref/Array.html#VALfold_left Array.fold_left]</code> instead of a '''for''' loop: |
Another implementation using <code>[http://caml.inria.fr/pub/docs/manual-ocaml/libref/Array.html#VALfold_left Array.fold_left]</code> instead of a '''for''' loop: |
||
< |
<syntaxhighlight lang="ocaml">let means v = |
||
let (a, b, c) = |
let (a, b, c) = |
||
Array.fold_left |
Array.fold_left |
||
Line 1,302: | Line 2,561: | ||
let n = float_of_int (Array.length v) in |
let n = float_of_int (Array.length v) in |
||
(a /. n, b ** (1./.n), n /. c) |
(a /. n, b ** (1./.n), n /. c) |
||
;;</ |
;;</syntaxhighlight> |
||
=={{header|Octave}}== |
=={{header|Octave}}== |
||
<syntaxhighlight lang="octave"> |
|||
<lang Octave> |
|||
A = mean(list); % arithmetic mean |
A = mean(list); % arithmetic mean |
||
G = mean(list,'g'); % geometric mean |
G = mean(list,'g'); % geometric mean |
||
H = mean(list,'a'); % harmonic mean |
H = mean(list,'a'); % harmonic mean |
||
</syntaxhighlight> |
|||
</lang> |
|||
See also Matlab implementation [[#MATLAB]] |
See also Matlab implementation [[#MATLAB]] |
||
=={{header|Oforth}}== |
|||
<syntaxhighlight lang="oforth">import: mapping |
|||
: A ( x ) |
|||
x sum |
|||
x size dup ifZero: [ 2drop null ] else: [ >float / ] |
|||
; |
|||
: G( x ) #* x reduce x size inv powf ; |
|||
: H( x ) x size x map( #inv ) sum / ; |
|||
: averages |
|||
| g | |
|||
"Geometric mean :" . 10 seq G dup .cr ->g |
|||
"Arithmetic mean :" . 10 seq A dup . g >= ifTrue: [ " ==> A >= G" .cr ] |
|||
"Harmonic mean :" . 10 seq H dup . g <= ifTrue: [ " ==> G >= H" .cr ] |
|||
;</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
Geometric mean : 4.52872868811677 |
|||
Arithmetic mean : 5.5 ==> A >= G |
|||
Harmonic mean : 3.41417152147406 ==> G >= H |
|||
</pre> |
|||
=={{header|ooRexx}}== |
=={{header|ooRexx}}== |
||
<syntaxhighlight lang="oorexx">a = .array~of(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0) |
|||
<lang ooRexx> |
|||
a = .array~of(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0) |
|||
say "Arithmetic =" arithmeticMean(a)", Geometric =" geometricMean(a)", Harmonic =" harmonicMean(a) |
say "Arithmetic =" arithmeticMean(a)", Geometric =" geometricMean(a)", Harmonic =" harmonicMean(a) |
||
Line 1,355: | Line 2,640: | ||
return numbers~items / mean |
return numbers~items / mean |
||
::requires rxmath LIBRARY |
::requires rxmath LIBRARY</syntaxhighlight> |
||
{{out}} |
|||
</lang> |
|||
<pre>Arithmetic = 5.5, Geometric = 4.52872869, Harmonic = 3.41417153</pre> |
|||
=={{header|Oz}}== |
=={{header|Oz}}== |
||
< |
<syntaxhighlight lang="oz">declare |
||
%% helpers |
%% helpers |
||
fun {Sum Xs} {FoldL Xs Number.'+' 0.0} end |
fun {Sum Xs} {FoldL Xs Number.'+' 0.0} end |
||
Line 1,389: | Line 2,675: | ||
{Show [A G H]} |
{Show [A G H]} |
||
A >= G = true |
A >= G = true |
||
G >= H = true</ |
G >= H = true</syntaxhighlight> |
||
=={{header|PARI/GP}}== |
=={{header|PARI/GP}}== |
||
General implementations: |
General implementations: |
||
< |
<syntaxhighlight lang="parigp">arithmetic(v)={ |
||
sum(i=1,#v,v[i])/#v |
sum(i=1,#v,v[i])/#v |
||
}; |
}; |
||
Line 1,404: | Line 2,690: | ||
v=vector(10,i,i); |
v=vector(10,i,i); |
||
[arithmetic(v),geometric(v),harmonic(v)]</ |
[arithmetic(v),geometric(v),harmonic(v)]</syntaxhighlight> |
||
Specific to the first ''n'' positive integers: |
Specific to the first ''n'' positive integers: |
||
< |
<syntaxhighlight lang="parigp">arithmetic_first(n)={ |
||
(n+1)/2 |
(n+1)/2 |
||
}; |
}; |
||
Line 1,422: | Line 2,708: | ||
[arithmetic_first(10),geometric_first(10),harmonic_first(10)] |
[arithmetic_first(10),geometric_first(10),harmonic_first(10)] |
||
%[1]>=%[2] && %[2] >= %[3]</ |
%[1]>=%[2] && %[2] >= %[3]</syntaxhighlight> |
||
These are, asymptotically, n/2, n/e, and n/log n. |
These are, asymptotically, n/2, n/e, and n/log n. |
||
Line 1,430: | Line 2,716: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
< |
<syntaxhighlight lang="perl">sub A |
||
{ |
{ |
||
my $a = 0; |
my $a = 0; |
||
Line 1,455: | Line 2,741: | ||
print "A=$a\nG=$g\nH=$h\n"; |
print "A=$a\nG=$g\nH=$h\n"; |
||
die "Error" unless $a >= $g and $g >= $h;</ |
die "Error" unless $a >= $g and $g >= $h;</syntaxhighlight> |
||
=={{header| |
=={{header|Phix}}== |
||
<!--<syntaxhighlight lang="phix">(phixonline)--> |
|||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
|||
<span style="color: #008080;">function</span> <span style="color: #000000;">arithmetic_mean</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> |
|||
<span style="color: #008080;">return</span> <span style="color: #7060A8;">sum</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)/</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> |
|||
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span> |
|||
<span style="color: #008080;">function</span> <span style="color: #000000;">geometric_mean</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> |
|||
<span style="color: #008080;">return</span> <span style="color: #7060A8;">power</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">product</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">),</span><span style="color: #000000;">1</span><span style="color: #0000FF;">/</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">))</span> |
|||
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span> |
|||
<span style="color: #008080;">function</span> <span style="color: #000000;">harmonic_mean</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> |
|||
<span style="color: #008080;">return</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)/</span><span style="color: #7060A8;">sum</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sq_div</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">s</span><span style="color: #0000FF;">))</span> |
|||
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span> |
|||
<span style="color: #008080;">constant</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,</span><span style="color: #000000;">6</span><span style="color: #0000FF;">,</span><span style="color: #000000;">7</span><span style="color: #0000FF;">,</span><span style="color: #000000;">8</span><span style="color: #0000FF;">,</span><span style="color: #000000;">9</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">}</span> |
|||
<span style="color: #008080;">constant</span> <span style="color: #000000;">arithmetic</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">arithmetic_mean</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">),</span> |
|||
<span style="color: #000000;">geometric</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">geometric_mean</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">),</span> |
|||
<span style="color: #000000;">harmonic</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">harmonic_mean</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> |
|||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Arithmetic: %.10g\n"</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">arithmetic</span><span style="color: #0000FF;">)</span> |
|||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Geometric: %.10g\n"</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">geometric</span><span style="color: #0000FF;">)</span> |
|||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Harmonic: %.10g\n"</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">harmonic</span><span style="color: #0000FF;">)</span> |
|||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Arithmetic>=Geometric>=Harmonic: %t\n"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">arithmetic</span><span style="color: #0000FF;">>=</span><span style="color: #000000;">geometric</span> <span style="color: #008080;">and</span> <span style="color: #000000;">geometric</span><span style="color: #0000FF;">>=</span><span style="color: #000000;">harmonic</span><span style="color: #0000FF;">})</span> |
|||
<!--</syntaxhighlight>--> |
|||
{{out}} |
|||
<pre> |
|||
Arithmetic: 5.5 |
|||
Geometric: 4.528728688 |
|||
Harmonic: 3.414171521 |
|||
Arithmetic>=Geometric>=Harmonic: true |
|||
</pre> |
|||
=={{header|PHP}}== |
|||
<lang Perl6>sub A(@x) { ([+] @x) / @x.elems } |
|||
<syntaxhighlight lang="php"><?php |
|||
sub G(@x) { ([*] @x) ** (1 / @x.elems) } |
|||
// Created with PHP 7.0 |
|||
sub H(@x) { @x.elems / [+] @x.map: 1/* } |
|||
function ArithmeticMean(array $values) |
|||
say "A(1,...,10) = ", A(1..10); |
|||
{ |
|||
say "G(1,...,10) = ", G(1..10); |
|||
return array_sum($values) / count($values); |
|||
say "H(1,...,10) = ", H(1..10); |
|||
} |
|||
</lang> |
|||
function GeometricMean(array $values) |
|||
Output: |
|||
{ |
|||
<pre>A(1,...,10) = 5.5 |
|||
return array_product($values) ** (1 / count($values)); |
|||
G(1,...,10) = 4.52872868811677 |
|||
} |
|||
H(1,...,10) = 3.41417152147406</pre> |
|||
function HarmonicMean(array $values) |
|||
{ |
|||
$sum = 0; |
|||
foreach ($values as $value) { |
|||
$sum += 1 / $value; |
|||
} |
|||
return count($values) / $sum; |
|||
} |
|||
$values = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); |
|||
echo "Arithmetic: " . ArithmeticMean($values) . "\n"; |
|||
echo "Geometric: " . GeometricMean($values) . "\n"; |
|||
echo "Harmonic: " . HarmonicMean($values) . "\n"; |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
Arithmetic: 5.5 |
|||
Geometric: 4.5287286881168 |
|||
Harmonic: 3.4141715214741 |
|||
</pre> |
|||
=={{header|PicoLisp}}== |
|||
<syntaxhighlight lang="picolisp">(load "@lib/math.l") |
|||
(let (Lst (1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0) Len (length Lst)) |
|||
(prinl "Arithmetic mean: " |
|||
(format |
|||
(/ (apply + Lst) Len) |
|||
*Scl ) ) |
|||
(prinl "Geometric mean: " |
|||
(format |
|||
(pow (*/ (apply * Lst) (** 1.0 (dec Len))) (/ 1.0 Len)) |
|||
*Scl ) ) |
|||
(prinl "Harmonic mean: " |
|||
(format |
|||
(*/ (* 1.0 Len) 1.0 (sum '((N) (*/ 1.0 1.0 N)) Lst)) |
|||
*Scl ) ) )</syntaxhighlight> |
|||
{{out}} |
|||
<pre>Arithmetic mean: 5.500000 |
|||
Geometric mean: 4.528729 |
|||
Harmonic mean: 3.414172</pre> |
|||
=={{header|PL/I}}== |
=={{header|PL/I}}== |
||
<syntaxhighlight lang="pl/i"> |
|||
<lang PL/I> |
|||
declare n fixed binary, |
declare n fixed binary, |
||
(Average, Geometric, Harmonic) float; |
(Average, Geometric, Harmonic) float; |
||
Line 1,496: | Line 2,857: | ||
if Average < Geometric then put skip list ('Error'); |
if Average < Geometric then put skip list ('Error'); |
||
if Geometric < Harmonic then put skip list ('Error'); |
if Geometric < Harmonic then put skip list ('Error'); |
||
</syntaxhighlight> |
|||
</lang> |
|||
Results: |
Results: |
||
<pre> |
<pre> |
||
Line 1,503: | Line 2,864: | ||
HARMONIC= 3.41417E+0000; |
HARMONIC= 3.41417E+0000; |
||
</pre> |
</pre> |
||
=={{header|PicoLisp}}== |
|||
<lang PicoLisp>(load "@lib/math.l") |
|||
(let (Lst (1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0) Len (length Lst)) |
|||
(prinl "Arithmetic mean: " |
|||
(format |
|||
(/ (apply + Lst) Len) |
|||
*Scl ) ) |
|||
(prinl "Geometric mean: " |
|||
(format |
|||
(pow (*/ (apply * Lst) (** 1.0 (dec Len))) (/ 1.0 Len)) |
|||
*Scl ) ) |
|||
(prinl "Harmonic mean: " |
|||
(format |
|||
(*/ (* 1.0 Len) 1.0 (sum '((N) (*/ 1.0 1.0 N)) Lst)) |
|||
*Scl ) ) )</lang> |
|||
Output: |
|||
<pre>Arithmetic mean: 5.500000 |
|||
Geometric mean: 4.528729 |
|||
Harmonic mean: 3.414172</pre> |
|||
=={{header|PostScript}}== |
=={{header|PostScript}}== |
||
<lang> |
<syntaxhighlight lang="text"> |
||
/pythamean{ |
/pythamean{ |
||
/x exch def |
/x exch def |
||
Line 1,549: | Line 2,889: | ||
10 pythamean |
10 pythamean |
||
</syntaxhighlight> |
|||
</lang> |
|||
Output : |
|||
{{out}} |
|||
<lang> |
|||
<pre> |
|||
Arithmetic Mean : 5.5 |
Arithmetic Mean : 5.5 |
||
Geometric Mean : 4.52873 |
Geometric Mean : 4.52873 |
||
Harmonic Mean : 3.41417 |
Harmonic Mean : 3.41417 |
||
</ |
</pre> |
||
{{libheader|initlib}} |
{{libheader|initlib}} |
||
< |
<syntaxhighlight lang="postscript"> |
||
/numbers {[1 10] 1 range}. |
/numbers {[1 10] 1 range}. |
||
/recip {1 exch div}. |
/recip {1 exch div}. |
||
Line 1,570: | Line 2,909: | ||
% Harmonic mean |
% Harmonic mean |
||
numbers dup 0 {recip +} fold exch length exch div |
numbers dup 0 {recip +} fold exch length exch div |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|PowerShell}}== |
=={{header|PowerShell}}== |
||
< |
<syntaxhighlight lang="powershell">$A = 0 |
||
$LogG = 0 |
$LogG = 0 |
||
$InvH = 0 |
$InvH = 0 |
||
Line 1,595: | Line 2,934: | ||
write-host "Is A >= G ? $($A -ge $G)" |
write-host "Is A >= G ? $($A -ge $G)" |
||
write-host "Is G >= H ? $($G -ge $H)"</ |
write-host "Is G >= H ? $($G -ge $H)"</syntaxhighlight> |
||
{{out}} |
|||
Output: |
|||
<pre>Arithmetic mean: A = 5.5 |
<pre>Arithmetic mean: A = 5.5 |
||
Geometric mean: G = 4.52872868811676 |
Geometric mean: G = 4.52872868811676 |
||
Line 1,603: | Line 2,942: | ||
Is A >= G ? True |
Is A >= G ? True |
||
Is G >= H ? True</pre> |
Is G >= H ? True</pre> |
||
=={{header|Processing}}== |
|||
<syntaxhighlight lang="processing">void setup() { |
|||
float[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; |
|||
println("Arithmetic mean: " + arithmeticMean(numbers)); |
|||
println("Geometric mean: " + geometricMean(numbers)); |
|||
println("Harmonic mean: " + harmonicMean(numbers)); |
|||
} |
|||
float arithmeticMean(float[] nums) { |
|||
float mean = 0; |
|||
for (float n : nums) { |
|||
mean += n; |
|||
} |
|||
mean = mean / nums.length; |
|||
return mean; |
|||
} |
|||
float geometricMean(float[] nums) { |
|||
float mean = 1; |
|||
for (float n : nums) { |
|||
mean *= n; |
|||
} |
|||
mean = pow(mean, 1.0 / nums.length); |
|||
return mean; |
|||
} |
|||
float harmonicMean(float[] nums) { |
|||
float mean = 0; |
|||
for (float n : nums) { |
|||
mean += 1 / n; |
|||
} |
|||
mean = nums.length / mean; |
|||
return mean; |
|||
}</syntaxhighlight> |
|||
{{out}} |
|||
<pre>Arithmetic mean: 5.5 |
|||
Geometric mean: 4.528729 |
|||
Harmonic mean: 3.4141712</pre> |
|||
=={{header|PureBasic}}== |
=={{header|PureBasic}}== |
||
< |
<syntaxhighlight lang="purebasic">Procedure.d ArithmeticMean() |
||
For a = 1 To 10 |
For a = 1 To 10 |
||
mean + a |
mean + a |
||
Line 1,630: | Line 3,008: | ||
Debug ArithmeticMean() |
Debug ArithmeticMean() |
||
Debug GeometricMean() |
Debug GeometricMean() |
||
Debug HarmonicMean()</ |
Debug HarmonicMean()</syntaxhighlight> |
||
=={{header|Python}}== |
=={{header|Python}}== |
||
{{works with|Python|3}} |
{{works with|Python|3}} |
||
< |
<syntaxhighlight lang="python">from operator import mul |
||
from functools import reduce |
from functools import reduce |
||
def amean(num): |
def amean(num): |
||
return sum(num) / len(num) |
|||
def gmean(num): |
def gmean(num): |
||
return reduce(mul, num, 1)**(1 / len(num)) |
|||
def hmean(num): |
def hmean(num): |
||
return len(num) / sum(1 / n for n in num) |
|||
numbers = range(1,11) # 1..10 |
numbers = range(1, 11) # 1..10 |
||
a, g, h = amean(numbers), gmean(numbers), hmean(numbers) |
a, g, h = amean(numbers), gmean(numbers), hmean(numbers) |
||
print(a, g, h) |
print(a, g, h) |
||
assert |
assert a >= g >= h</syntaxhighlight> |
||
{{out}} |
|||
</lang> |
|||
Output: |
|||
<pre>5.5 4.52872868812 3.41417152147</pre> |
<pre>5.5 4.52872868812 3.41417152147</pre> |
||
These are the same in Python 2 apart from requiring explicit float division (either through <code>float()</code> casts or float literals such as <code>1./n</code>); or better, do a <code>from __future__ import division</code>, which works on Python 2.2+ as well as Python 3, and makes division work consistently like it does in Python 3. |
These are the same in Python 2 apart from requiring explicit float division (either through <code>float()</code> casts or float literals such as <code>1./n</code>); or better, do a <code>from __future__ import division</code>, which works on Python 2.2+ as well as Python 3, and makes division work consistently like it does in Python 3. |
||
=={{header|Quackery}}== |
|||
Uses <code>root</code> from [[Integer roots#Quackery]]. |
|||
<syntaxhighlight lang="quackery"> [] 10 times [ i^ 1+ join ] |
|||
say "Arithmetic mean:" sp |
|||
0 over witheach + |
|||
over size 8 point$ echo$ |
|||
cr |
|||
say " Geometric mean:" sp |
|||
1 over witheach * |
|||
over size 80 ** * 10 root |
|||
10 8 ** 8 point$ echo$ |
|||
cr |
|||
say " Harmonic mean:" sp |
|||
dup size dip |
|||
[ 0 n->v rot |
|||
witheach [ n->v 1/v v+ ] ] |
|||
n->v 2swap v/ 8 point$ echo$</syntaxhighlight> |
|||
{{out}} |
|||
<pre>Arithmetic mean: 5.5 |
|||
Geometric mean: 4.52872868 |
|||
Harmonic mean: 3.41417152 |
|||
</pre> |
|||
=={{header|R}}== |
=={{header|R}}== |
||
Initialise x |
Initialise x |
||
<syntaxhighlight lang="r"> |
|||
<lang R> |
|||
x <- 1:10 |
x <- 1:10 |
||
</syntaxhighlight> |
|||
</lang> |
|||
Arithmetic mean |
Arithmetic mean |
||
<syntaxhighlight lang="r"> |
|||
<lang R> |
|||
a <- sum(x)/length(x) |
a <- sum(x)/length(x) |
||
</syntaxhighlight> |
|||
</lang> |
|||
or |
or |
||
<syntaxhighlight lang="r"> |
|||
<lang R> |
|||
a <- mean(x) |
a <- mean(x) |
||
</syntaxhighlight> |
|||
</lang> |
|||
The geometric mean |
The geometric mean |
||
<syntaxhighlight lang="r"> |
|||
<lang R> |
|||
g <- prod(x)^(1/length(x)) |
g <- prod(x)^(1/length(x)) |
||
</syntaxhighlight> |
|||
</lang> |
|||
The harmonic mean (no error checking that <math>x_i\ne 0,\text{ } \forall \text{ }i=1\ldots n</math>) |
The harmonic mean (no error checking that <math>x_i\ne 0,\text{ } \forall \text{ }i=1\ldots n</math>) |
||
<syntaxhighlight lang="r"> |
|||
<lang R> |
|||
h <- length(x)/sum(1/x) |
h <- length(x)/sum(1/x) |
||
</syntaxhighlight> |
|||
</lang> |
|||
Then: |
Then: |
||
<syntaxhighlight lang="r"> |
|||
<lang R> |
|||
a > g |
a > g |
||
</syntaxhighlight> |
|||
</lang> |
|||
and |
and |
||
<syntaxhighlight lang="r"> |
|||
<lang R> |
|||
g > h |
g > h |
||
</syntaxhighlight> |
|||
</lang> |
|||
give both |
give both |
||
Line 1,700: | Line 3,107: | ||
=={{header|Racket}}== |
=={{header|Racket}}== |
||
< |
<syntaxhighlight lang="racket"> |
||
#lang racket |
#lang racket |
||
Line 1,720: | Line 3,127: | ||
(harmonic xs) |
(harmonic xs) |
||
(>= (arithmetic xs) (geometric xs) (harmonic xs)) |
(>= (arithmetic xs) (geometric xs) (harmonic xs)) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
|||
Output: |
|||
<pre> |
<pre> |
||
5 1/2 |
5 1/2 |
||
Line 1,729: | Line 3,136: | ||
</pre> |
</pre> |
||
=={{header| |
=={{header|Raku}}== |
||
(formerly Perl 6) |
|||
REXX doesn't have a '''POW''' function, so an '''IROOT''' ('''i'''nteger '''root''') function is included here. |
|||
{{works with|Rakudo|2015.12}} |
|||
<br><br>Coding note: while the DO loop flow looks nice, it took a bit of work to get exactly right, |
|||
<br>and doesn't lend itself to being updated easily. |
|||
<lang rexx>/*REXX program to compute/show Pythagorean means [Amean, Gmean, Hmean].*/ |
|||
parse arg n . /*maybe get an optional argument.*/ |
|||
if n=='' then n=10 /*None specified? Assume default*/ |
|||
/*══════════════════compute Amean [Arithmetic mean]*/ |
|||
sum=0; do j=1 for n |
|||
@.j=j /*populate the stemmed array @. */ |
|||
sum=sum+@.j /*compute the sum of all elements*/ |
|||
end /*j*/ |
|||
Amean=sum/n /*calculate the Amean. */ |
|||
say 'Amean =' Amean /*show and tell Amean. */ |
|||
/*══════════════════compute Gmean [Geometric mean].*/ |
|||
prod=1; do k=1 for n |
|||
prod=prod*@.k /*comp. product of all elements. */ |
|||
end /*k*/ |
|||
Gmean=iroot(prod,n) /*calculate the Gmean. */ |
|||
say 'Gmean =' Gmean /*show and tell Gmean. */ |
|||
/*══════════════════compute Hmean [Harmonic mean]. */ |
|||
rsum=0; do m=1 for n |
|||
rsum=rsum+1/@.m /*compute the sum of reciprocals.*/ |
|||
end /*m*/ |
|||
Hmean=n/rsum /*calculate the Hmean. */ |
|||
say 'Hmean =' Hmean /*show and tell Hmean. */ |
|||
exit /*stick a fork in it, we're done.*/ |
|||
/*──────────────────────────────────IROOT subroutine────────────────────*/ |
|||
iroot: procedure; arg x 1 ox,y 1 oy /*get both args, and also a copy.*/ |
|||
if x=0 | x=1 then return x /*handle special case of 0 & 1. */ |
|||
if y=0 then return 1 /*handle special case of root 0. */ |
|||
if y=1 then return x /*handle special case of root 1. */ |
|||
if x<0 & y//2==0 then do /*check for illegal combination. */ |
|||
say; say '*** error! *** (from IROOT):'; say |
|||
say 'root' y "can't be even if 1st argument is < 0." |
|||
say; return '[n/a]' /*return a not applicable.*/ |
|||
end |
|||
x=abs(x) /*use the absolute value for X. */ |
|||
y=abs(y) /*use the absolute value for root*/ |
|||
digO=digits() /*save original accuracy (digits)*/ |
|||
a=digO+5 /*use an extra 5 digs (accuracy).*/ |
|||
g=(x+1)/y**y /*use this as the 1st guesstimate*/ |
|||
m=y-1 /*use this as a fast [root-1]. */ |
|||
numeric fuzz 3 /*3 fuzz digits for comparisons. */ |
|||
d=5 /*start with 5 digits accuracy. When the */ |
|||
/*DIGITS is large, CPU time is wasted on */ |
|||
/*large accuracies when the guess isn't */ |
|||
/*close to the final answer. It's best to*/ |
|||
/*take baby steps before going full bore */ |
|||
/*throttle and putting the pedal to the */ |
|||
/*metal, getting it in high gear, and */ |
|||
/*then turning the volume all the way up. */ |
|||
<syntaxhighlight lang="raku" line>sub A { ([+] @_) / @_ } |
|||
do forever /* ◄─────────────────┐ keep plugging as digs increases*/ |
|||
sub G { ([*] @_) ** (1 / @_) } |
|||
d=min(d+d,a) /* │ limit the digits to orig digs┐5*/ |
|||
sub H { @_ / [+] 1 X/ @_ } |
|||
numeric digits d /* │ keep increasing the accuracy. */ |
|||
old=0 /* │ define old (guess). */ |
|||
/* │ */ |
|||
do forever /* ◄────────────┐ │ keep plugging at the Yth root.*/ |
|||
_=(m*g**y+x)/y/g**m /* │ │ this is the nitty-gritty stuff.*/ |
|||
if _=g | _=old then leave /* │ │ are we close enough yet ? */ |
|||
old=g /* │ │ save guess in old (guess). */ |
|||
g=_ /* │ │ set Guess to what's been calc. */ |
|||
end /*forever ►────────────┘ │ */ |
|||
/* │ */ |
|||
if d==a then leave /* │ are we at the desired accuracy?*/ |
|||
end /*forever ►───────────────┘ */ |
|||
say "A(1,...,10) = ", A(1..10); |
|||
_=g*sign(ox) /*adjust for the sign of orig X. */ |
|||
say "G(1,...,10) = ", G(1..10); |
|||
if oy<0 then _=1/_ /*adjust for negative root. */ |
|||
say "H(1,...,10) = ", H(1..10); |
|||
numeric digits digO /*restore the original digits. */ |
|||
</syntaxhighlight> |
|||
return _/1 /*normalize result to orig digits*/ |
|||
</lang> |
|||
{{out}} |
|||
'''output''' |
|||
<pre>A(1,...,10) = 5.5 |
|||
<pre style="overflow:scroll"> |
|||
G(1,...,10) = 4.52872868811677 |
|||
H(1,...,10) = 3.41417152147406</pre> |
|||
=={{header|REXX}}== |
|||
REXX doesn't have a '''POW''' function, so an '''IROOT''' ('''i'''nteger '''root''') function is included here; it includes an |
|||
<br>extra error check if used as a general purpose function that would otherwise yield a complex result. |
|||
<syntaxhighlight lang="rexx">/*REXX program computes and displays the Pythagorean means [Amean, Gmean, Hmean]. */ |
|||
numeric digits 20 /*use a little extra for the precision.*/ |
|||
parse arg n . /*obtain the optional argument from CL.*/ |
|||
if n=='' | n=="," then n= 10 /*None specified? Then use the default*/ |
|||
sum= 0; prod= 1; rSum= 0 /*initialize sum/product/reciprocal sum*/ |
|||
$=; do #=1 for n; $= $ # /*generate list by appending # to list.*/ |
|||
sum = sum + # /*compute the sum of all the elements. */ |
|||
prod= prod * # /*compute the product of all elements. */ |
|||
rSum= rSum + 1/# /*compute the sum of the reciprocals. */ |
|||
end /*#*/ |
|||
say ' list ='$ /*display the list of numbers used. */ |
|||
say 'Amean =' sum / n /*calculate & display arithmetic mean.*/ |
|||
say 'Gmean =' Iroot(prod, n) /* " " " geometric " */ |
|||
if result=="[n/a]" then say '***error***: root' y "can't be even if 1st argument is < 0." |
|||
say 'Hmean =' n / rSum /* " " " harmonic " */ |
|||
exit 0 /*stick a fork in it, we're all done. */ |
|||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
|||
Iroot: procedure; parse arg x 1 ox, y 1 oy /*get both args, and also a copy of X&Y*/ |
|||
if x=0 | x=1 | y=1 then return x /*handle special case of zero and unity*/ |
|||
if y=0 then return 1 /* " " " " a zero root.*/ |
|||
if x<0 & y//2==0 then return '[n/a]' /*indicate result is "not applicable". */ |
|||
x= abs(x); y= abs(y); m= y - 1 /*use the absolute value for X and Y. */ |
|||
oDigs= digits(); a= oDigs + 5 /*save original digits; add five digs.*/ |
|||
g= (x+1) / y*2 /*use this as the first guesstimate. */ |
|||
d= 5 /*start with 5 dec digs, saves CPU time*/ |
|||
do until d==a; d= min(d + d, a) /*keep going as digits are increased. */ |
|||
numeric digits d; f= d - 2 /*limit digits to original digits + 5.*/ |
|||
og= /*use a non─guess for the old G (guess)*/ |
|||
do forever; gm= g**m /*keep computing at the Yth root. */ |
|||
_= format( (m*g*gm + x)/(y*gm),,f) /*this is the nitty─gritty calculation.*/ |
|||
if _=g | _=og then leave /*are we close enough yet? */ |
|||
og= g; g= _ /*save guess ──► OG; set the new guess.*/ |
|||
end /*forever*/ |
|||
end /*until */ |
|||
g= g * sign(ox); if oy<0 then g= 1 / g /*adjust for original X sign; neg. root*/ |
|||
numeric digits oDigs; return g / 1 /*normalize to original decimal digits.*/</syntaxhighlight> |
|||
{{out|output|text= when using the default input:}} |
|||
<pre> |
|||
list = 1 2 3 4 5 6 7 8 9 10 |
|||
Amean = 5.5 |
Amean = 5.5 |
||
Gmean = 4. |
Gmean = 4.5287286881167647622 |
||
Hmean = 3. |
Hmean = 3.4141715214740550062 |
||
</pre> |
|||
=={{header|Ring}}== |
|||
<syntaxhighlight lang="ring"> |
|||
decimals(8) |
|||
array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] |
|||
see "arithmetic mean = " + arithmeticMean(array) + nl |
|||
see "geometric mean = " + geometricMean(array) + nl |
|||
see "harmonic mean = " + harmonicMean(array) + nl |
|||
func arithmeticMean a |
|||
return summary(a) / len(a) |
|||
func geometricMean a |
|||
b = 1 |
|||
for i = 1 to len(a) |
|||
b *= a[i] |
|||
next |
|||
return pow(b, (1/len(a))) |
|||
func harmonicMean a |
|||
b = list(len(a)) |
|||
for nr = 1 to len(a) |
|||
b[nr] = 1/a[nr] |
|||
next |
|||
return len(a) / summary(b) |
|||
func summary s |
|||
sum = 0 |
|||
for n = 1 to len(s) |
|||
sum += s[n] |
|||
next |
|||
return sum |
|||
</syntaxhighlight> |
|||
Output: |
|||
<pre> |
|||
arithmetic mean = 5.50000000 |
|||
geometric mean = 4.52872869 |
|||
harmonic mean = 3.41417152 |
|||
</pre> |
|||
=={{header|RPL}}== |
|||
These words can be used either on vectors or lists. |
|||
{{works with|HP|28}} |
|||
≪ → array op |
|||
≪ array 1 GET 2 array SIZE |
|||
'''IF''' DUP2 > '''THEN''' DROP2 '''ELSE FOR''' j array GET op EVAL '''NEXT END''' |
|||
≫ ≫ '<span style="color:blue">REDUCE</span>' STO |
|||
≪ DUP ≪ + ≫ <span style="color:blue">REDUCE</span> SWAP SIZE / |
|||
≫ '<span style="color:blue">AMEAN</span>' STO |
|||
≪ DUP ≪ * ≫ <span style="color:blue">REDUCE</span> SWAP SIZE INV ^ |
|||
≫ '<span style="color:blue">GMEAN</span>' STO |
|||
≪ SIZE LAST ≪ INV + ≫ <span style="color:blue">REDUCE</span> / |
|||
≫ '<span style="color:blue">HMEAN</span>' STO |
|||
{ 1 2 3 4 5 6 7 8 9 0 } <span style="color:blue">AMEAN</span> |
|||
{ 1 2 3 4 5 6 7 8 9 0 } <span style="color:blue">GMEAN</span> |
|||
[ 1 2 3 4 5 6 7 8 9 0 ] <span style="color:blue">HMEAN</span> |
|||
{{out}} |
|||
<pre> |
|||
3: 5.5 |
|||
2: 4.52872868812 |
|||
1: 3.41417152147 |
|||
</pre> |
</pre> |
||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
{{works with|Ruby|1.9+}} |
{{works with|Ruby|1.9+}} |
||
< |
<syntaxhighlight lang="ruby">class Array |
||
def arithmetic_mean |
def arithmetic_mean |
||
inject(:+) |
inject(0.0, :+) / length |
||
end |
end |
||
def geometric_mean |
def geometric_mean |
||
inject(:*) ** (1.0 / length) |
inject(:*) ** (1.0 / length) |
||
end |
end |
||
def harmonic_mean |
def harmonic_mean |
||
length |
length / inject(0.0) {|s, m| s + 1.0/m} |
||
end |
end |
||
end |
end |
||
Line 1,838: | Line 3,297: | ||
p h = (1..10).harmonic_mean |
p h = (1..10).harmonic_mean |
||
# is h < g < a ?? |
# is h < g < a ?? |
||
p g.between?(h, a)</ |
p g.between?(h, a)</syntaxhighlight> |
||
{{out}} |
|||
outputs |
|||
<pre> |
<pre> |
||
5.5 |
|||
4.52872868811677 |
|||
4.528728688116765 |
|||
3.41417152147406 |
|||
3.414171521474055 |
|||
true</pre> |
|||
true |
|||
</pre> |
|||
=={{header|Run BASIC}}== |
=={{header|Run BASIC}}== |
||
< |
<syntaxhighlight lang="runbasic">bXsum = 1 |
||
for i = 1 to 10 |
for i = 1 to 10 |
||
sum = sum + i ' sum of 1 -> 10 |
sum = sum + i ' sum of 1 -> 10 |
||
Line 1,862: | Line 3,323: | ||
print " Harmonic Mean:";harmonic |
print " Harmonic Mean:";harmonic |
||
if (average >= geometric) and (geometric >= harmonic) then print "True" else print "False"</ |
if (average >= geometric) and (geometric >= harmonic) then print "True" else print "False"</syntaxhighlight> |
||
{{out}} |
|||
Output: |
|||
<pre> |
<pre> |
||
Arithmetic Mean:5.5 |
Arithmetic Mean:5.5 |
||
Line 1,869: | Line 3,330: | ||
Harmonic Mean:3.41417132 |
Harmonic Mean:3.41417132 |
||
True</pre> |
True</pre> |
||
=={{header|Rust}}== |
|||
<syntaxhighlight lang="rust">fn main() { |
|||
let mut sum = 0.0; |
|||
let mut prod = 1; |
|||
let mut recsum = 0.0; |
|||
for i in 1..11{ |
|||
sum += i as f32; |
|||
prod *= i; |
|||
recsum += 1.0/(i as f32); |
|||
} |
|||
let avg = sum/10.0; |
|||
let gmean = (prod as f32).powf(0.1); |
|||
let hmean = 10.0/recsum; |
|||
println!("Average: {}, Geometric mean: {}, Harmonic mean: {}", avg, gmean, hmean); |
|||
assert!( ( (avg >= gmean) && (gmean >= hmean) ), "Incorrect calculation"); |
|||
} |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
Average: 5.5, Geometric mean:4.528729, Harmonic mean: 3.4141712 |
|||
</pre> |
|||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
{{works with|Scala|2.8+}} |
{{works with|Scala|2.8+}} |
||
< |
<syntaxhighlight lang="scala">def arithmeticMean(n: Seq[Int]) = n.sum / n.size.toDouble |
||
def geometricMean(n: Seq[Int]) = math.pow(n.foldLeft(1.0)(_*_), 1.0 / n.size.toDouble) |
def geometricMean(n: Seq[Int]) = math.pow(n.foldLeft(1.0)(_*_), 1.0 / n.size.toDouble) |
||
def harmonicMean(n: Seq[Int]) = n.size / n.map(1.0 / _).sum |
def harmonicMean(n: Seq[Int]) = n.size / n.map(1.0 / _).sum |
||
var nums = 1 to 10 |
var nums = 1 to 10 |
||
var a = arithmeticMean(nums) |
var a = arithmeticMean(nums) |
||
var g = geometricMean(nums) |
var g = geometricMean(nums) |
||
var h = harmonicMean(nums) |
var h = harmonicMean(nums) |
||
println("Arithmetic mean " + a) |
println("Arithmetic mean " + a) |
||
println("Geometric mean " + g) |
println("Geometric mean " + g) |
||
println("Harmonic mean " + h) |
println("Harmonic mean " + h) |
||
assert(a >= g && g >= h)</ |
assert(a >= g && g >= h)</syntaxhighlight> |
||
{{out}} |
|||
<pre>Arithmetic mean 5.5 |
|||
Geometric mean 4.528728688116765 |
|||
Harmonic mean 3.414171521474055</pre> |
|||
=={{header|Scheme}}== |
=={{header|Scheme}}== |
||
{{Works with|Scheme|R<math>^5</math>RS}} |
{{Works with|Scheme|R<math>^5</math>RS}} |
||
< |
<syntaxhighlight lang="scheme">(define (a-mean l) |
||
(/ (apply + l) (length l))) |
(/ (apply + l) (length l))) |
||
Line 1,912: | Line 3,399: | ||
(newline) |
(newline) |
||
(display (>= a g h)) |
(display (>= a g h)) |
||
(newline))</ |
(newline))</syntaxhighlight> |
||
{{out}} |
|||
Output: |
|||
<lang>11/2 >= 4.528728688116765 >= 25200/7381 |
<syntaxhighlight lang="text">11/2 >= 4.528728688116765 >= 25200/7381 |
||
#t</ |
#t</syntaxhighlight> |
||
=={{header|Seed7}}== |
=={{header|Seed7}}== |
||
< |
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i"; |
||
include "float.s7i"; |
include "float.s7i"; |
||
Line 1,938: | Line 3,425: | ||
writeln("Geometric mean: " <& product ** (1.0 / flt(length(numbers)))); |
writeln("Geometric mean: " <& product ** (1.0 / flt(length(numbers)))); |
||
writeln("Harmonic mean: " <& flt(length(numbers)) / reciprocalSum); |
writeln("Harmonic mean: " <& flt(length(numbers)) / reciprocalSum); |
||
end func;</ |
end func;</syntaxhighlight> |
||
{{out}} |
|||
Output: |
|||
<pre> |
<pre> |
||
Arithmetic mean: 5.5 |
Arithmetic mean: 5.5 |
||
Line 1,946: | Line 3,433: | ||
Harmonic mean: 3.4141712188720703125 |
Harmonic mean: 3.4141712188720703125 |
||
</pre> |
</pre> |
||
=={{header|Sidef}}== |
|||
<syntaxhighlight lang="sidef">func A(a) { a.sum / a.len } |
|||
func G(a) { a.prod.root(a.len) } |
|||
func H(a) { a.len / a.map{1/_}.sum }</syntaxhighlight> |
|||
The same thing, using hyper-operators: |
|||
<syntaxhighlight lang="sidef">func A(a) { a«+» / a.len } |
|||
func G(a) { a«*» ** (1/a.len) } |
|||
func H(a) { a.len / (a«/«1 «+») }</syntaxhighlight> |
|||
Calling the functions: |
|||
<syntaxhighlight lang="sidef">say("A(1,...,10) = ", A(1..10)); |
|||
say("G(1,...,10) = ", G(1..10)); |
|||
say("H(1,...,10) = ", H(1..10));</syntaxhighlight> |
|||
{{out}} |
|||
<pre>A(1,...,10) = 5.5 |
|||
G(1,...,10) = 4.528728688116764762203309337195508793499 |
|||
H(1,...,10) = 3.414171521474055006096734859775098225173</pre> |
|||
=={{header|Smalltalk}}== |
=={{header|Smalltalk}}== |
||
Line 1,952: | Line 3,458: | ||
This extends the class Collection, so these three methods can be called over any kind of collection, it is enough the the objects of the collection understand +, *, raisedTo, reciprocal and /. |
This extends the class Collection, so these three methods can be called over any kind of collection, it is enough the the objects of the collection understand +, *, raisedTo, reciprocal and /. |
||
< |
<syntaxhighlight lang="smalltalk">Collection extend |
||
[ |
[ |
||
arithmeticMean |
arithmeticMean |
||
Line 1,978: | Line 3,484: | ||
((a arithmeticMean) >= (a geometricMean)) displayNl. |
((a arithmeticMean) >= (a geometricMean)) displayNl. |
||
((a geometricMean) >= (a harmonicMean)) displayNl.</ |
((a geometricMean) >= (a harmonicMean)) displayNl.</syntaxhighlight> |
||
Output: |
|||
{{out}} |
|||
<pre>5.5 |
<pre>5.5 |
||
4.528728688116765 |
4.528728688116765 |
||
Line 1,987: | Line 3,492: | ||
true |
true |
||
true</pre> |
true</pre> |
||
=={{header|SQL}}== |
|||
It may not be possible to calculate a geometric mean in a query, but the other two are easy enough. |
|||
<syntaxhighlight lang="sql"> |
|||
--setup |
|||
create table averages (val integer); |
|||
insert into averages values (1); |
|||
insert into averages values (2); |
|||
insert into averages values (3); |
|||
insert into averages values (4); |
|||
insert into averages values (5); |
|||
insert into averages values (6); |
|||
insert into averages values (7); |
|||
insert into averages values (8); |
|||
insert into averages values (9); |
|||
insert into averages values (10); |
|||
-- calculate means |
|||
select |
|||
1/avg(1/val) as harm, |
|||
avg(val) as arith |
|||
from |
|||
averages; |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
HARM ARITH |
|||
---------- ---------- |
|||
3.41417152 5.5 |
|||
</pre> |
|||
=={{header|Stata}}== |
|||
The command [http://www.stata.com/help.cgi?ameans ameans] prints the arithmetic, geometric and harmonic means, together with confidence intervals. |
|||
<syntaxhighlight lang="text">clear all |
|||
set obs 10 |
|||
gen x=_n |
|||
ameans x |
|||
Variable | Type Obs Mean [95% Conf. Interval] |
|||
-------------+--------------------------------------------------------------- |
|||
x | Arithmetic 10 5.5 3.334149 7.665851 |
|||
| Geometric 10 4.528729 2.680672 7.650836 |
|||
| Harmonic 10 3.414172 2.035664 10.57602 |
|||
-----------------------------------------------------------------------------</syntaxhighlight> |
|||
=={{header|Swift}}== |
|||
<syntaxhighlight lang="text"> |
|||
// Utility for easy creation of Double from any Numeric |
|||
extension Double { |
|||
init(withNum v: any Numeric) { |
|||
switch v { |
|||
case let ii as any BinaryInteger: self.init(ii) |
|||
case let ff as any BinaryFloatingPoint: self.init(ff) |
|||
default: self.init() |
|||
} |
|||
} |
|||
} |
|||
// Extension for numeric collections |
|||
extension Collection where Element: Numeric { |
|||
var arithmeticMean: Double { |
|||
self.reduce(0.0, {$0 + Double(withNum: $1)})/Double(self.count) |
|||
} |
|||
var geometricMean: Double { |
|||
pow(self.reduce(1.0, {$0 * Double(withNum: $1)}), 1.0/Double(self.count)) |
|||
} |
|||
var harmonicMean: Double { |
|||
Double(self.count) / self.reduce(0.0, {$0 + 1.0/Double(withNum:$1)}) |
|||
} |
|||
} |
|||
//Usage: |
|||
var c: [Int] = (1...10).map {$0} |
|||
print(c.arithmeticMean) |
|||
print(c.geometricMean) |
|||
print(c.harmonicMean) |
|||
// output: |
|||
// 5.5 |
|||
// 4.528728688116765 |
|||
// 3.414171521474055 |
|||
</syntaxhighlight> |
|||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
< |
<syntaxhighlight lang="tcl">proc arithmeticMean list { |
||
set sum 0.0 |
set sum 0.0 |
||
foreach value $list { set sum [expr {$sum + $value}] } |
foreach value $list { set sum [expr {$sum + $value}] } |
||
Line 2,011: | Line 3,595: | ||
puts "A10=$A10, G10=$G10, H10=$H10" |
puts "A10=$A10, G10=$G10, H10=$H10" |
||
if {$A10 >= $G10} { puts "A10 >= G10" } |
if {$A10 >= $G10} { puts "A10 >= G10" } |
||
if {$G10 >= $H10} { puts "G10 >= H10" }</ |
if {$G10 >= $H10} { puts "G10 >= H10" }</syntaxhighlight> |
||
{{out}} |
|||
Output: |
|||
<pre> |
<pre> |
||
A10=5.5, G10=4.528728688116765, H10=3.414171521474055 |
A10=5.5, G10=4.528728688116765, H10=3.414171521474055 |
||
Line 2,022: | Line 3,606: | ||
=={{header|Ursala}}== |
=={{header|Ursala}}== |
||
< |
<syntaxhighlight lang="ursala">#import std |
||
#import flo |
#import flo |
||
Line 2,033: | Line 3,617: | ||
#cast %eLbX |
#cast %eLbX |
||
main = ^(~&,ordered not fleq) <a,g,h></ |
main = ^(~&,ordered not fleq) <a,g,h></syntaxhighlight> |
||
{{out}} |
|||
output: |
|||
<pre>( |
<pre>( |
||
<5.500000e+00,4.528729e+00,3.414172e+00>, |
<5.500000e+00,4.528729e+00,3.414172e+00>, |
||
Line 2,042: | Line 3,626: | ||
Most valac setups will need "-X -lm" added to the compile command to include the C math library. |
Most valac setups will need "-X -lm" added to the compile command to include the C math library. |
||
< |
<syntaxhighlight lang="vala"> |
||
double arithmetic(int[] list){ |
double arithmetic(int[] list){ |
||
double mean; |
double mean; |
||
Line 2,095: | Line 3,679: | ||
stdout.printf("Harmonic mean: %s\n", harmonic_mean.to_string()); |
stdout.printf("Harmonic mean: %s\n", harmonic_mean.to_string()); |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
|||
Output: |
|||
<pre> |
<pre> |
||
Arithmetic mean: 5.5 |
Arithmetic mean: 5.5 |
||
Geometric mean: 4.5287286881167654 |
Geometric mean: 4.5287286881167654 |
||
Harmonic mean: 3.4141715214740551 |
Harmonic mean: 3.4141715214740551 |
||
</pre> |
|||
=={{header|VBA}}== |
|||
Uses Excel VBA. |
|||
<syntaxhighlight lang="vb">Private Function arithmetic_mean(s() As Variant) As Double |
|||
arithmetic_mean = WorksheetFunction.Average(s) |
|||
End Function |
|||
Private Function geometric_mean(s() As Variant) As Double |
|||
geometric_mean = WorksheetFunction.GeoMean(s) |
|||
End Function |
|||
Private Function harmonic_mean(s() As Variant) As Double |
|||
harmonic_mean = WorksheetFunction.HarMean(s) |
|||
End Function |
|||
Public Sub pythagorean_means() |
|||
Dim s() As Variant |
|||
s = [{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}] |
|||
Debug.Print "A ="; arithmetic_mean(s) |
|||
Debug.Print "G ="; geometric_mean(s) |
|||
Debug.Print "H ="; harmonic_mean(s) |
|||
End Sub</syntaxhighlight>{{out}} |
|||
<pre>A = 5,5 |
|||
G = 4,52872868811677 |
|||
H = 3,41417152147406 </pre> |
|||
=={{header|VBScript}}== |
|||
<syntaxhighlight lang="vb"> |
|||
Function arithmetic_mean(arr) |
|||
sum = 0 |
|||
For i = 0 To UBound(arr) |
|||
sum = sum + arr(i) |
|||
Next |
|||
arithmetic_mean = sum / (UBound(arr)+1) |
|||
End Function |
|||
Function geometric_mean(arr) |
|||
product = 1 |
|||
For i = 0 To UBound(arr) |
|||
product = product * arr(i) |
|||
Next |
|||
geometric_mean = product ^ (1/(UBound(arr)+1)) |
|||
End Function |
|||
Function harmonic_mean(arr) |
|||
sum = 0 |
|||
For i = 0 To UBound(arr) |
|||
sum = sum + (1/arr(i)) |
|||
Next |
|||
harmonic_mean = (UBound(arr)+1) / sum |
|||
End Function |
|||
WScript.StdOut.WriteLine arithmetic_mean(Array(1,2,3,4,5,6,7,8,9,10)) |
|||
WScript.StdOut.WriteLine geometric_mean(Array(1,2,3,4,5,6,7,8,9,10)) |
|||
WScript.StdOut.WriteLine harmonic_mean(Array(1,2,3,4,5,6,7,8,9,10)) |
|||
</syntaxhighlight> |
|||
{{Out}} |
|||
<pre> |
|||
5.5 |
|||
4.52872868811677 |
|||
3.41417152147406 |
|||
</pre> |
|||
=={{header|Visual Basic .NET}}== |
|||
{{trans|C#}} |
|||
<syntaxhighlight lang="vbnet">Imports System.Runtime.CompilerServices |
|||
Module Module1 |
|||
<Extension()> |
|||
Function Gmean(n As IEnumerable(Of Double)) As Double |
|||
Return Math.Pow(n.Aggregate(Function(s, i) s * i), 1.0 / n.Count()) |
|||
End Function |
|||
<Extension()> |
|||
Function Hmean(n As IEnumerable(Of Double)) As Double |
|||
Return n.Count() / n.Sum(Function(i) 1.0 / i) |
|||
End Function |
|||
Sub Main() |
|||
Dim nums = From n In Enumerable.Range(1, 10) Select CDbl(n) |
|||
Dim a = nums.Average() |
|||
Dim g = nums.Gmean() |
|||
Dim h = nums.Hmean() |
|||
Console.WriteLine("Arithmetic mean {0}", a) |
|||
Console.WriteLine(" Geometric mean {0}", g) |
|||
Console.WriteLine(" Harmonic mean {0}", h) |
|||
Debug.Assert(a >= g AndAlso g >= h) |
|||
End Sub |
|||
End Module</syntaxhighlight> |
|||
{{out}} |
|||
<pre>Arithmetic mean 5.5 |
|||
Geometric mean 4.52872868811677 |
|||
Harmonic mean 3.41417152147406</pre> |
|||
=={{header|V (Vlang)}}== |
|||
Updated for Vlang version 0.2.2 |
|||
<syntaxhighlight lang="go">import math |
|||
fn main() { |
|||
mut sum := 0.0 |
|||
mut prod :=1.0 |
|||
mut recip_sum := 0.0 |
|||
n := 10 |
|||
for val in 1..(n + 1) { |
|||
sum += val |
|||
prod *= val |
|||
recip_sum = recip_sum + ( 1.0 / val ) |
|||
} |
|||
a := sum / n |
|||
g := math.pow( prod, ( 1.0 / f32(n) ) ) |
|||
h := n / recip_sum |
|||
result := 'Arithmetic Mean: ${a:3.2f} \nGeometric Mean: ${g:3.2f}\nHarmonic Mean: ${h:3.2f}' |
|||
println( result ) |
|||
compare := if a >= g && g >= h { "Yes" } else { "Nope" } |
|||
println('Is A >= G >= H? $compare') |
|||
}</syntaxhighlight> |
|||
{{out}} |
|||
<pre>Arithmetic Mean: 5.50 |
|||
Geometric Mean: 4.53 |
|||
Harmonic Mean: 3.41 |
|||
Is A >= G >= H? Yes</pre> |
|||
=={{header|Wren}}== |
|||
<syntaxhighlight lang="wren">var rng = 1..10 |
|||
var count = rng.count |
|||
var A = rng.reduce { |acc, x| acc + x }/count |
|||
var G = rng.reduce { |prod, x| prod * x}.pow(1/count) |
|||
var H = rng.reduce { |acc, x| acc + 1/x}.pow(-1) * count |
|||
System.print("For the numbers %(rng):") |
|||
System.print(" Arithmetic mean = %(A)") |
|||
System.print(" Geometric mean = %(G)") |
|||
System.print(" Harmonic mean = %(H)") |
|||
System.print(" A >= G >= H = %(A >= G && G >= H)")</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
For the numbers 1..10: |
|||
Arithmetic mean = 5.5 |
|||
Geometric mean = 4.5287286881168 |
|||
Harmonic mean = 3.4141715214741 |
|||
A >= G >= H = true |
|||
</pre> |
</pre> |
||
=={{header|XPL0}}== |
=={{header|XPL0}}== |
||
< |
<syntaxhighlight lang="xpl0">include c:\cxpl\codes; |
||
func real Power(X, Y); \X raised to the Y power |
func real Power(X, Y); \X raised to the Y power |
||
Line 2,132: | Line 3,865: | ||
Text(0, "ALWAYS DECREASING ORDER |
Text(0, "ALWAYS DECREASING ORDER |
||
"); |
"); |
||
]</ |
]</syntaxhighlight> |
||
{{out}} |
|||
Output: |
|||
<pre> |
<pre> |
||
5.50000 |
5.50000 |
||
Line 2,140: | Line 3,873: | ||
3.41417 |
3.41417 |
||
ALWAYS DECREASING ORDER |
ALWAYS DECREASING ORDER |
||
</pre> |
|||
=={{header|zkl}}== |
|||
<syntaxhighlight lang="zkl">ns:=T(1,2,3,4,5,6,7,8,9,10); |
|||
ns.sum(0.0)/ns.len(); // Arithmetic mean |
|||
ns.reduce('*,1.0).pow(1.0/ns.len()); // Geometric mean |
|||
ns.len().toFloat() / ns.reduce(fcn(p,n){ p + 1.0/n },0.0); // Harmonic mean</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
5.5 |
|||
4.52873 |
|||
3.41417 |
|||
</pre> |
</pre> |
Latest revision as of 18:56, 17 February 2024
You are encouraged to solve this task according to the task description, using any language you may know.
- Task
Compute all three of the Pythagorean means of the set of integers 1 through 10 (inclusive).
Show that for this set of positive integers.
- The most common of the three means, the arithmetic mean, is the sum of the list divided by its length:
- The geometric mean is the th root of the product of the list:
- The harmonic mean is divided by the sum of the reciprocal of each item in the list:
|
11l
F amean(num)
R sum(num)/Float(num.len)
F gmean(num)
R product(num) ^ (1.0/num.len)
F hmean(num)
return num.len / sum(num.map(n -> 1.0/n))
V numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print(amean(numbers))
print(gmean(numbers))
print(hmean(numbers))
- Output:
5.5 4.52873 3.41417
Action!
INCLUDE "D2:REAL.ACT" ;from the Action! Tool Kit
PROC InverseI(INT a,result)
REAL one,x
IntToReal(1,one)
IntToReal(a,x)
RealDiv(one,x,result)
RETURN
PROC ArithmeticMean(INT ARRAY a INT count REAL POINTER result)
INT i
REAL x,sum,tmp
IntToReal(0,sum)
FOR i=0 TO count-1
DO
IntToReal(a(i),x)
RealAdd(sum,x,tmp)
RealAssign(tmp,sum)
OD
IntToReal(count,tmp)
RealDiv(sum,tmp,result)
RETURN
PROC GeometricMean(INT ARRAY a INT count REAL POINTER result)
INT i
REAL x,prod,tmp
IntToReal(1,prod)
FOR i=0 TO count-1
DO
IntToReal(a(i),x)
RealMult(prod,x,tmp)
RealAssign(tmp,prod)
OD
InverseI(count,tmp)
Power(prod,tmp,result)
RETURN
PROC HarmonicMean(INT ARRAY a INT count REAL POINTER result)
INT i
REAL x,sum,tmp
IntToReal(0,sum)
FOR i=0 TO count-1
DO
InverseI(a(i),x)
RealAdd(sum,x,tmp)
RealAssign(tmp,sum)
OD
IntToReal(count,tmp)
RealDiv(tmp,sum,result)
RETURN
PROC Main()
BYTE i
INT ARRAY a=[1 2 3 4 5 6 7 8 9 10]
REAL result
Put(125) PutE() ;clear screen
ArithmeticMean(a,10,result)
Print("Arithmetic mean=") PrintRE(result)
GeometricMean(a,10,result)
Print(" Geometric mean=") PrintRE(result)
HarmonicMean(a,10,result)
Print(" Harmonic mean=") PrintRE(result)
RETURN
- Output:
Screenshot from Atari 8-bit computer
Arithmetic mean=5.5 Geometric mean=4.52872861 Harmonic mean=3.41417153
ActionScript
function arithmeticMean(v:Vector.<Number>):Number
{
var sum:Number = 0;
for(var i: uint = 0; i < v.length; i++)
sum += v[i];
return sum/v.length;
}
function geometricMean(v:Vector.<Number>):Number
{
var product:Number = 1;
for(var i: uint = 0; i < v.length; i++)
product *= v[i];
return Math.pow(product, 1/v.length);
}
function harmonicMean(v:Vector.<Number>):Number
{
var sum:Number = 0;
for(var i: uint = 0; i < v.length; i++)
sum += 1/v[i];
return v.length/sum;
}
var list:Vector.<Number> = Vector.<Number>([1,2,3,4,5,6,7,8,9,10]);
trace("Arithmetic: ", arithmeticMean(list));
trace("Geometric: ", geometricMean(list));
trace("Harmonic: ", harmonicMean(list));
Ada
pythagorean_means.ads:
package Pythagorean_Means is
type Set is array (Positive range <>) of Float;
function Arithmetic_Mean (Data : Set) return Float;
function Geometric_Mean (Data : Set) return Float;
function Harmonic_Mean (Data : Set) return Float;
end Pythagorean_Means;
pythagorean_means.adb:
with Ada.Numerics.Generic_Elementary_Functions;
package body Pythagorean_Means is
package Math is new Ada.Numerics.Generic_Elementary_Functions (Float);
function "**" (Left, Right : Float) return Float renames Math."**";
function Arithmetic_Mean (Data : Set) return Float is
Sum : Float := 0.0;
begin
for I in Data'Range loop
Sum := Sum + Data (I);
end loop;
return Sum / Float (Data'Length);
end Arithmetic_Mean;
function Geometric_Mean (Data : Set) return Float is
Product : Float := 1.0;
begin
for I in Data'Range loop
Product := Product * Data (I);
end loop;
return Product**(1.0/Float(Data'Length));
end Geometric_Mean;
function Harmonic_Mean (Data : Set) return Float is
Reciprocal_Sum : Float := 0.0;
begin
for I in Data'Range loop
Reciprocal_Sum := Reciprocal_Sum + Data (I)**(-1);
end loop;
return Float (Data'Length) / Reciprocal_Sum;
end Harmonic_Mean;
end Pythagorean_Means;
example main.adb:
with Ada.Text_IO;
with Pythagorean_Means;
procedure Main is
My_Set : Pythagorean_Means.Set := (1.0, 2.0, 3.0, 4.0, 5.0,
6.0, 7.0, 8.0, 9.0, 10.0);
Arithmetic_Mean : Float := Pythagorean_Means.Arithmetic_Mean (My_Set);
Geometric_Mean : Float := Pythagorean_Means.Geometric_Mean (My_Set);
Harmonic_Mean : Float := Pythagorean_Means.Harmonic_Mean (My_Set);
begin
Ada.Text_IO.Put_Line (Float'Image (Arithmetic_Mean) & " >= " &
Float'Image (Geometric_Mean) & " >= " &
Float'Image (Harmonic_Mean));
end Main;
ALGOL 68
main: (
INT count:=0;
LONG REAL f, sum:=0, prod:=1, resum:=0;
FORMAT real = $g(0,4)$; # preferred real format #
FILE fbuf; STRING sbuf; associate(fbuf,sbuf);
BOOL opts := TRUE;
FOR i TO argc DO
IF opts THEN # skip args up to the - token #
opts := argv(i) NE "-"
ELSE
rewind(fbuf); sbuf := argv(i); get(fbuf,f);
count +:= 1;
sum +:= f;
prod *:= f;
resum +:= 1/f
FI
OD;
printf(($"c: "f(real)l"s: "f(real)l"p: "f(real)l"r: "f(real)l$,count,sum,prod,resum));
printf(($"Arithmetic mean = "f(real)l$,sum/count));
printf(($"Geometric mean = "f(real)l$,prod**(1/count)));
printf(($"Harmonic mean = "f(real)l$,count/resum))
)
Lunix command:
a68g Averages_Pythagorean_means.a68 - 1 2 3 4 5 6 7 8 9 10
- Output:
c: 10.0000 s: 55.0000 p: 3628800.0000 r: 2.9290 Arithmetic mean = 5.5000 Geometric mean = 4.5287 Harmonic mean = 3.4142
ALGOL W
begin
% returns the arithmetic mean of the elements of n from lo to hi %
real procedure arithmeticMean ( real array n ( * ); integer value lo, hi ) ;
begin
real sum;
sum := 0;
for i := lo until hi do sum := sum + n( i );
sum / ( 1 + ( hi - lo ) )
end arithmeticMean ;
% returns the geometric mean of the elements of n from lo to hi %
real procedure geometricMean ( real array n ( * ); integer value lo, hi ) ;
begin
real product;
product := 1;
for i := lo until hi do product := product * n( i );
exp( ln( product ) / ( 1 + ( hi - lo ) ) )
end geometricMean ;
% returns the harminic mean of the elements of n from lo to hi %
real procedure harmonicMean ( real array n ( * ); integer value lo, hi ) ;
begin
real sum;
sum := 0;
for i := lo until hi do sum := sum + ( 1 / n( i ) );
( 1 + ( hi - lo ) ) / sum
end harmonicMean ;
real array v ( 1 :: 10 );
for i := 1 until 10 do v( i ) := i;
r_w := 10; r_d := 5; r_format := "A"; s_w := 0; % set output format %
write( "Arithmetic mean: ", arithmeticMean( v, 1, 10 ) );
write( "Geometric mean: ", geometricMean( v, 1, 10 ) );
write( "Harmonic mean: ", harmonicMean( v, 1, 10 ) )
end.
- Output:
Arithmetic mean: 5.50000 Geometric mean: 4.52872 Harmonic mean: 3.41417
Amazing Hopper
Think about "talk" programming...
#include <hopper.h>
/* An example of definitions in pseudo-natural language, with synonimous.
These definitions can be inside a definition file (xxxx.h) */
#define getasinglelistof(_X_) {_X_},
#synon getasinglelistof getalistof
#define integerrandomnumbers _V1000_=-1,rand array(_V1000_),mulby(10),ceil,
#define randomnumbers _V1000_=-1,rand array(_V1000_)
#define rememberitin(_X_) _X_=0,cpy(_X_)
#synon rememberitin rememberthisnumbersin
#define rememberas(_X_) mov(_X_)
#define remember(_X_) {_X_}
//#synon remember with ---> this exist in HOPPER.H
#defn nowconsiderthis(_X_) #ATOM#CMPLX,print
#synon nowconsiderthis nowconsider,considerthis,consider,nowputtext,puttext,andprint
#define andprintwithanewline {"\n"}print
#synon andprintwithanewline printwithanewline
//#defn andprint(_X_) #ATOM#CMPLX,print
#define putanewline {"\n"}
#define withanewline "\n"
#define andprintit print
#synon andprintit printit,andprint
#define showit show
#define afterdoingit emptystack?,not,do{ {"I cannot continue due to retentive data "},throw(1001) }
#synon afterdoingit secondly,finally
#define then emptystack?do{ {"I cannot continue because data is missing "},throw(1000) }
/* why "#context" and not "#define"?
becose "#context" need a value in the stack for continue.
Internally, "domeanit" tranform to "gosub(calculatearithmeticmean)",
and "gosub" works only if it finds a data in the stack */
#context calculatethegeometricmean
#synon calculatethegeometricmean calculategeometricmean,getgeometricmean
#context calculatetheharmonicmean
#synon calculatetheharmonicmean calculateharmonicmean,getharmonicmean
#context calculatearitmethicmean
#synon calculatearitmethicmean calculatesinglemean,calculatemean,domeanit
main:
consider this ("Arithmetic Mean: ")
get a list of '10,10' integer random numbers; remember this numbers in 'list of numbers';
then, do mean it, and print with a new line.
after doing it, consider ("Geometric Mean: "), remember 'list of numbers', calculate the geometric mean;
then, put a new line, and print it.
/*
Okay. This can be a bit long, if we have to write the program;
But what if we just had to talk, and the language interpreter takes care of the rest?
*/
secondly, now consider ("Harmonic Mean: "), with 'list of numbers', get harmonic mean, and print with a new line.
finally, put text ("Original Array:\n"), and print (list of numbers, with a new line)
exit(0)
.locals
calculate aritmethic mean:
stats(MEAN)
back
calculate the geometric mean:
stats(GEOMEAN)
back
calculatetheharmonicmean:
stats(HARMEAN)
back
- Output:
Arithmetic Mean: 5.51 Geometric Mean: 4.47333 Harmonic Mean: 3.29515 Original Array: 4 3 10 6 10 1 1 10 4 1 7 4 5 8 9 7 3 8 1 10 5 5 2 5 8 9 1 5 10 10 6 4 3 5 9 2 6 9 2 9 10 9 3 4 6 2 1 8 10 1 7 5 5 9 9 3 7 9 7 7 9 2 10 1 7 9 3 2 7 4 1 6 2 4 10 7 5 1 5 5 1 2 9 5 10 7 7 6 6 4 3 4 5 2 4 1 10 6 3 7
Notice:
The line "stats(GEOMEAN)" is identical to: stats(MULTIPLICATORY),POW BY({1}div by (100)) and... #defn reciprocalof(_X_) {1},#ATOM#CMPLX,postfix,div,postfix then: stats(MULTIPLICATORY),POW BY( reciprocal of (100) ) or... #defn N-ROOTof(_DATA_,_N_) #ATOM#CMPLX,{1}div by(_N_),postfix,pow,postfix #define Multiplicatoryof(_DATA_) {_DATA_}stats(MULTIPLICATORY) then: N-ROOT of ( Multiplicatory of (list of numbers), 100) etc.
APL
arithmetic←{(+/⍵)÷⍴⍵}
geometric←{(×/⍵)*÷⍴⍵}
harmonic←{(⍴⍵)÷(+/÷⍵)}
x←⍳10
arithmetic x
5.5
geometric x
4.528728688
harmonic x
3.414171521
AppleScript
-- arithmetic_mean :: [Number] -> Number
on arithmetic_mean(xs)
-- sum :: Number -> Number -> Number
script sum
on |λ|(accumulator, x)
accumulator + x
end |λ|
end script
foldl(sum, 0, xs) / (length of xs)
end arithmetic_mean
-- geometric_mean :: [Number] -> Number
on geometric_mean(xs)
-- product :: Number -> Number -> Number
script product
on |λ|(accumulator, x)
accumulator * x
end |λ|
end script
foldl(product, 1, xs) ^ (1 / (length of xs))
end geometric_mean
-- harmonic_mean :: [Number] -> Number
on harmonic_mean(xs)
-- addInverse :: Number -> Number -> Number
script addInverse
on |λ|(accumulator, x)
accumulator + (1 / x)
end |λ|
end script
(length of xs) / (foldl(addInverse, 0, xs))
end harmonic_mean
-- TEST -----------------------------------------------------------------------
on run
set {A, G, H} to ap({arithmetic_mean, geometric_mean, harmonic_mean}, ¬
{{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}})
{values:{arithmetic:A, geometric:G, harmonic:H}, inequalities:¬
{|A >= G|:A ≥ G}, |G >= H|:G ≥ H}
end run
-- GENERIC FUNCTIONS ----------------------------------------------------------
-- A list of functions applied to a list of arguments
-- (<*> | ap) :: [(a -> b)] -> [a] -> [b]
on ap(fs, xs)
set {nf, nx} to {length of fs, length of xs}
set acc to {}
repeat with i from 1 to nf
tell mReturn(item i of fs)
repeat with j from 1 to nx
set end of acc to |λ|(contents of (item j of xs))
end repeat
end tell
end repeat
return acc
end ap
-- foldl :: (a -> b -> a) -> a -> [b] -> a
on foldl(f, startValue, xs)
tell mReturn(f)
set v to startValue
set lng to length of xs
repeat with i from 1 to lng
set v to |λ|(v, item i of xs, i, xs)
end repeat
return v
end tell
end foldl
-- map :: (a -> b) -> [a] -> [b]
on map(f, xs)
tell mReturn(f)
set lng to length of xs
set lst to {}
repeat with i from 1 to lng
set end of lst to |λ|(item i of xs, i, xs)
end repeat
return lst
end tell
end map
-- Lift 2nd class handler function into 1st class script wrapper
-- mReturn :: Handler -> Script
on mReturn(f)
if class of f is script then
f
else
script
property |λ| : f
end script
end if
end mReturn
- Output:
{values:{arithmetic:5.5, geometric:4.528728688117, harmonic:3.414171521474},
inequalities:{|A >= G|:true}, |G >= H|:true}
Arturo
arithmeticMean: function [arr]->
average arr
geometricMean: function [arr]->
(product arr) ^ 1//size arr
harmonicMean: function [arr]->
(size arr) // sum map arr 'i [1//i]
print arithmeticMean 1..10
print geometricMean 1..10
print harmonicMean 1..10
- Output:
5.5 4.528728688116765 3.414171521474055
AutoHotkey
A := ArithmeticMean(1, 10)
G := GeometricMean(1, 10)
H := HarmonicMean(1, 10)
If G Between %H% And %A%
Result := "True"
Else
Result := "False"
MsgBox, %A%`n%G%`n%H%`n%Result%
;---------------------------------------------------------------------------
ArithmeticMean(a, b) { ; of integers a through b
;---------------------------------------------------------------------------
n := b - a + 1
Loop, %n%
Sum += (a + A_Index - 1)
Return, Sum / n
}
;---------------------------------------------------------------------------
GeometricMean(a, b) { ; of integers a through b
;---------------------------------------------------------------------------
n := b - a + 1
Prod := 1
Loop, %n%
Prod *= (a + A_Index - 1)
Return, Prod ** (1 / n)
}
;---------------------------------------------------------------------------
HarmonicMean(a, b) { ; of integers a through b
;---------------------------------------------------------------------------
n := b - a + 1
Loop, %n%
Sum += 1 / (a + A_Index - 1)
Return, n / Sum
}
Message box shows:
5.500000 4.528729 3.414172 True
AWK
#!/usr/bin/awk -f
{
x = $1; # value of 1st column
A += x;
G += log(x);
H += 1/x;
N++;
}
END {
print "Arithmethic mean: ",A/N;
print "Geometric mean : ",exp(G/N);
print "Harmonic mean : ",N/H;
}
BBC BASIC
The arithmetic and harmonic means use BBC BASIC's built-in array operations; only the geometric mean needs a loop.
DIM a(9)
a() = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
PRINT "Arithmetic mean = " ; FNarithmeticmean(a())
PRINT "Geometric mean = " ; FNgeometricmean(a())
PRINT "Harmonic mean = " ; FNharmonicmean(a())
END
DEF FNarithmeticmean(a())
= SUM(a()) / (DIM(a(),1)+1)
DEF FNgeometricmean(a())
LOCAL a, I%
a = 1
FOR I% = 0 TO DIM(a(),1)
a *= a(I%)
NEXT
= a ^ (1/(DIM(a(),1)+1))
DEF FNharmonicmean(a())
LOCAL b()
DIM b(DIM(a(),1))
b() = 1/a()
= (DIM(a(),1)+1) / SUM(b())
- Output:
Arithmetic mean = 5.5 Geometric mean = 4.52872869 Harmonic mean = 3.41417152
BQN
A ← +´÷≠
G ← ≠√×´
H ← A⌾÷
⋈⟜(∧´ ¯1⊸↓ ≥ 1⊸↓) (A∾G∾H) 1+↕10
- Output:
⟨ ⟨ 5.5 4.528728688116765 3.414171521474055 ⟩ 1 ⟩
C
#include <stdio.h>
#include <stdlib.h> // atoi()
#include <math.h> // pow()
int main(int argc, char* argv[])
{
int i, count=0;
double f, sum=0.0, prod=1.0, resum=0.0;
for (i=1; i<argc; ++i) {
f = atof(argv[i]);
count++;
sum += f;
prod *= f;
resum += (1.0/f);
}
//printf(" c:%d\n s:%f\n p:%f\n r:%f\n",count,sum,prod,resum);
printf("Arithmetic mean = %f\n",sum/count);
printf("Geometric mean = %f\n",pow(prod,(1.0/count)));
printf("Harmonic mean = %f\n",count/resum);
return 0;
}
C#
The standard Linq extension method Average provides arithmetic mean. This example adds two more extension methods for the geometric and harmonic means.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
namespace PythMean
{
static class Program
{
static void Main(string[] args) {
var nums = from n in Enumerable.Range(1, 10) select (double)n;
var a = nums.Average();
var g = nums.Gmean();
var h = nums.Hmean();
Console.WriteLine("Arithmetic mean {0}", a);
Console.WriteLine("Geometric mean {0}", g);
Console.WriteLine("Harmonic mean {0}", h);
Debug.Assert(a >= g && g >= h);
}
// Geometric mean extension method.
static double Gmean(this IEnumerable<double> n) {
return Math.Pow(n.Aggregate((s, i) => s * i), 1.0 / n.Count());
}
// Harmonic mean extension method.
static double Hmean(this IEnumerable<double> n) {
return n.Count() / n.Sum(i => 1.0 / i);
}
}
}
- Output:
Arithmetic mean 5.5 Geometric mean 4.52872868811677 Harmonic mean 3.41417152147406
C++
#include <vector>
#include <iostream>
#include <numeric>
#include <cmath>
#include <algorithm>
double toInverse ( int i ) {
return 1.0 / i ;
}
int main( ) {
std::vector<int> numbers ;
for ( int i = 1 ; i < 11 ; i++ )
numbers.push_back( i ) ;
double arithmetic_mean = std::accumulate( numbers.begin( ) , numbers.end( ) , 0 ) / 10.0 ;
double geometric_mean =
pow( std::accumulate( numbers.begin( ) , numbers.end( ) , 1 , std::multiplies<int>( ) ), 0.1 ) ;
std::vector<double> inverses ;
inverses.resize( numbers.size( ) ) ;
std::transform( numbers.begin( ) , numbers.end( ) , inverses.begin( ) , toInverse ) ;
double harmonic_mean = 10 / std::accumulate( inverses.begin( ) , inverses.end( ) , 0.0 ); //initial value of accumulate must be a double!
std::cout << "The arithmetic mean is " << arithmetic_mean << " , the geometric mean "
<< geometric_mean << " and the harmonic mean " << harmonic_mean << " !\n" ;
return 0 ;
}
- Output:
The arithmetic mean is 5.5 , the geometric mean 4.52873 and the harmonic mean 3.41417 !
Clojure
(use '[clojure.contrib.math :only (expt)])
(defn a-mean [coll]
(/ (apply + coll) (count coll)))
(defn g-mean [coll]
(expt (apply * coll) (/ (count coll))))
(defn h-mean [coll]
(/ (count coll) (apply + (map / coll))))
(let [numbers (range 1 11)
a (a-mean numbers) g (g-mean numbers) h (h-mean numbers)]
(println a ">=" g ">=" h)
(>= a g h))
CoffeeScript
a = [ 1..10 ]
arithmetic_mean = (a) -> a.reduce(((s, x) -> s + x), 0) / a.length
geometic_mean = (a) -> Math.pow(a.reduce(((s, x) -> s * x), 1), (1 / a.length))
harmonic_mean = (a) -> a.length / a.reduce(((s, x) -> s + 1 / x), 0)
A = arithmetic_mean a
G = geometic_mean a
H = harmonic_mean a
console.log "A = ", A, " G = ", G, " H = ", H
console.log "A >= G : ", A >= G, " G >= H : ", G >= H
- Output:
A = 5.5 G = 4.528728688116765 H = 3.414171521474055 A >= G : true G >= H : true
Common Lisp
(defun generic-mean (nums reduce-op final-op)
(funcall final-op (reduce reduce-op nums)))
(defun a-mean (nums)
(generic-mean nums #'+ (lambda (x) (/ x (length nums)))))
(defun g-mean (nums)
(generic-mean nums #'* (lambda (x) (expt x (/ 1 (length nums))))))
(defun h-mean (nums)
(generic-mean nums
(lambda (x y) (+ x
(/ 1 y)))
(lambda (x) (/ (length nums) x))))
(let ((numbers (loop for i from 1 to 10 collect i)))
(let ((a-mean (a-mean numbers))
(g-mean (g-mean numbers))
(h-mean (h-mean numbers)))
(assert (> a-mean g-mean h-mean))
(format t "a-mean ~a~%" a-mean)
(format t "g-mean ~a~%" g-mean)
(format t "h-mean ~a~%" h-mean)))
Craft Basic
precision 6
define bxsum = 1, sum = 0, sum1i = 0
define average = 0, geometric = 0, harmonic = 0
for i = 1 to 10
let sum = sum + i
let bxsum = bxsum * i
let sum1i = sum1i + (1 / i)
next i
let average = sum / 10
let geometric = bxsum ^ (1 / 10)
let harmonic = 10 / sum1i
print "arithmetic mean: ", average
print "geometric mean: ", geometric
print "harmonic mean: ", harmonic
if average >= geometric and geometric >= harmonic then
print "true"
end
endif
print "false"
- Output:
arithmetic mean: 5.500000 geometric mean: 4.528729 harmonic mean: 3.414172 true
D
The output for the harmonic mean is wrong.
import std.stdio, std.algorithm, std.range, std.functional;
auto aMean(T)(T data) pure nothrow @nogc {
return data.sum / data.length;
}
auto gMean(T)(T data) pure /*@nogc*/ {
return data.reduce!q{a * b} ^^ (1.0 / data.length);
}
auto hMean(T)(T data) pure /*@nogc*/ {
return data.length / data.reduce!q{ 1.0 / a + b };
}
void main() {
immutable m = [adjoin!(hMean, gMean, aMean)(iota(1.0L, 11.0L))[]];
writefln("%(%.19f %)", m);
assert(m.isSorted);
}
- Output:
0.9891573712076470036 4.5287286881167647619 5.5000000000000000000
Delphi
program AveragesPythagoreanMeans;
{$APPTYPE CONSOLE}
uses Types, Math;
function ArithmeticMean(aArray: TDoubleDynArray): Double;
var
lValue: Double;
begin
Result := 0;
for lValue in aArray do
Result := Result + lValue;
if Result > 0 then
Result := Result / Length(aArray);
end;
function GeometricMean(aArray: TDoubleDynArray): Double;
var
lValue: Double;
begin
Result := 1;
for lValue in aArray do
Result := Result * lValue;
Result := Power(Result, 1 / Length(aArray));
end;
function HarmonicMean(aArray: TDoubleDynArray): Double;
var
lValue: Double;
begin
Result := 0;
for lValue in aArray do
Result := Result + 1 / lValue;
Result := Length(aArray) / Result;
end;
var
lSourceArray: TDoubleDynArray;
AMean, GMean, HMean: Double;
begin
lSourceArray := TDoubleDynArray.Create(1,2,3,4,5,6,7,8,9,10);
AMean := ArithmeticMean(lSourceArray));
GMean := GeometricMean(lSourceArray));
HMean := HarmonicMean(lSourceArray));
if (AMean >= GMean) and (GMean >= HMean) then
Writeln(AMean, " ≥ ", GMean, " ≥ ", HMean)
else
writeln("Error!");
end.
E
Given that we're defining all three together, it makes sense to express their regularities:
def makeMean(base, include, finish) {
return def mean(numbers) {
var count := 0
var acc := base
for x in numbers {
acc := include(acc, x)
count += 1
}
return finish(acc, count)
}
}
def A := makeMean(0, fn b,x { b+x }, fn acc,n { acc / n })
def G := makeMean(1, fn b,x { b*x }, fn acc,n { acc ** (1/n) })
def H := makeMean(0, fn b,x { b+1/x }, fn acc,n { n / acc })
? A(1..10)
# value: 5.5
? G(1..10)
# value: 4.528728688116765
? H(1..10)
# value: 3.414171521474055
EasyLang
proc mean . v[] a g h .
prod = 1
for v in v[]
sum += v
prod *= v
resum += 1 / v
.
a = sum / len v[]
g = pow prod (1 / len v[])
h = len v[] / resum
.
a[] = [ 1 2 3 4 5 6 7 8 9 10 ]
mean a[] a g h
print a
print g
print h
EchoLisp
(define (A xs) (// (for/sum ((x xs)) x) (length xs)))
(define (G xs) (expt (for/product ((x xs)) x) (// (length xs))))
(define (H xs) (// (length xs) (for/sum ((x xs)) (// x))))
(define xs (range 1 11))
(and (>= (A xs) (G xs)) (>= (G xs) (H xs)))
→ #t
Elixir
defmodule Means do
def arithmetic(list) do
Enum.sum(list) / length(list)
end
def geometric(list) do
:math.pow(Enum.reduce(list, &(*/2)), 1 / length(list))
end
def harmonic(list) do
1 / arithmetic(Enum.map(list, &(1 / &1)))
end
end
list = Enum.to_list(1..10)
IO.puts "Arithmetic mean: #{am = Means.arithmetic(list)}"
IO.puts "Geometric mean: #{gm = Means.geometric(list)}"
IO.puts "Harmonic mean: #{hm = Means.harmonic(list)}"
IO.puts "(#{am} >= #{gm} >= #{hm}) is #{am >= gm and gm >= hm}"
- Output:
Arithmetic mean: 5.5 Geometric mean: 4.528728688116765 Harmonic mean: 3.414171521474055 (5.5 >= 4.528728688116765 >= 3.414171521474055) is true
Erlang
%% Author: Abhay Jain <abhay_1303@yahoo.co.in>
-module(mean_calculator).
-export([find_mean/0]).
find_mean() ->
%% This is function calling. First argument is the the beginning number
%% and second argument is the initial value of sum for AM & HM and initial value of product for GM.
arithmetic_mean(1, 0),
geometric_mean(1, 1),
harmonic_mean(1, 0).
%% Function to calculate Arithmetic Mean
arithmetic_mean(Number, Sum) when Number > 10 ->
AM = Sum / 10,
io:format("Arithmetic Mean ~p~n", [AM]);
arithmetic_mean(Number, Sum) ->
NewSum = Sum + Number,
arithmetic_mean(Number+1, NewSum).
%% Function to calculate Geometric Mean
geometric_mean(Number, Product) when Number > 10 ->
GM = math:pow(Product, 0.1),
io:format("Geometric Mean ~p~n", [GM]);
geometric_mean(Number, Product) ->
NewProd = Product * Number,
geometric_mean(Number+1, NewProd).
%% Function to calculate Harmonic Mean
harmonic_mean(Number, Sum) when Number > 10 ->
HM = 10 / Sum,
io:format("Harmonic Mean ~p~n", [HM]);
harmonic_mean(Number, Sum) ->
NewSum = Sum + (1/Number),
harmonic_mean(Number+1, NewSum).
- Output:
Arithmetic Mean 5.5 Geometric Mean 4.528728688116765 Harmonic Mean 3.414171521474055
ERRE
PROGRAM MEANS
DIM A[9]
PROCEDURE ARITHMETIC_MEAN(A[]->M)
LOCAL S,I%
NEL%=UBOUND(A,1)
S=0
FOR I%=0 TO NEL% DO
S+=A[I%]
END FOR
M=S/(NEL%+1)
END PROCEDURE
PROCEDURE GEOMETRIC_MEAN(A[]->M)
LOCAL S,I%
NEL%=UBOUND(A,1)
S=1
FOR I%=0 TO NEL% DO
S*=A[I%]
END FOR
M=S^(1/(NEL%+1))
END PROCEDURE
PROCEDURE HARMONIC_MEAN(A[]->M)
LOCAL S,I%
NEL%=UBOUND(A,1)
S=0
FOR I%=0 TO NEL% DO
S+=1/A[I%]
END FOR
M=(NEL%+1)/S
END PROCEDURE
BEGIN
A[]=(1,2,3,4,5,6,7,8,9,10)
ARITHMETIC_MEAN(A[]->M)
PRINT("Arithmetic mean = ";M)
GEOMETRIC_MEAN(A[]->M)
PRINT("Geometric mean = ";M)
HARMONIC_MEAN(A[]->M)
PRINT("Harmonic mean = ";M)
END PROGRAM
Euler Math Toolbox
>function A(x) := mean(x)
>function G(x) := exp(mean(log(x)))
>function H(x) := 1/mean(1/x)
>x=1:10; A(x), G(x), H(x)
5.5
4.52872868812
3.41417152147
Alternatively, e.g.,
>function G(x) := prod(x)^(1/length(x))
Euphoria
function arithmetic_mean(sequence s)
atom sum
if length(s) = 0 then
return 0
else
sum = 0
for i = 1 to length(s) do
sum += s[i]
end for
return sum/length(s)
end if
end function
function geometric_mean(sequence s)
atom p
p = 1
for i = 1 to length(s) do
p *= s[i]
end for
return power(p,1/length(s))
end function
function harmonic_mean(sequence s)
atom sum
if length(s) = 0 then
return 0
else
sum = 0
for i = 1 to length(s) do
sum += 1/s[i]
end for
return length(s) / sum
end if
end function
function true_or_false(atom x)
if x then
return "true"
else
return "false"
end if
end function
constant s = {1,2,3,4,5,6,7,8,9,10}
constant arithmetic = arithmetic_mean(s),
geometric = geometric_mean(s),
harmonic = harmonic_mean(s)
printf(1,"Arithmetic: %g\n", arithmetic)
printf(1,"Geometric: %g\n", geometric)
printf(1,"Harmonic: %g\n", harmonic)
printf(1,"Arithmetic>=Geometric>=Harmonic: %s\n",
{true_or_false(arithmetic>=geometric and geometric>=harmonic)})
- Output:
Arithmetic: 5.5 Geometric: 4.52873 Harmonic: 3.41417 Arithmetic>=Geometric>=Harmonic: true
Excel
Use the functions : AVERAGE, GEOMEAN and HARMEAN
=AVERAGE(1;2;3;4;5;6;7;8;9;10)
=GEOMEAN(1;2;3;4;5;6;7;8;9;10)
=HARMEAN(1;2;3;4;5;6;7;8;9;10)
- Output:
5.5 4.528728688 3,414171521
F#
let P = [1.0; 2.0; 3.0; 4.0; 5.0; 6.0; 7.0; 8.0; 9.0; 10.0]
let arithmeticMean (x : float list) =
x |> List.sum
|> (fun acc -> acc / float (List.length(x)))
let geometricMean (x: float list) =
x |> List.reduce (*)
|> (fun acc -> Math.Pow(acc, 1.0 / (float (List.length(x)))))
let harmonicMean (x: float list) =
x |> List.map (fun a -> 1.0 / a)
|> List.sum
|> (fun acc -> float (List.length(x)) / acc)
printfn "Arithmetic Mean: %A" (arithmeticMean P)
printfn "Geometric Mean: %A" (geometricMean P)
printfn "Harmonic Mean: %A" (harmonicMean P)
Factor
: a-mean ( seq -- mean )
[ sum ] [ length ] bi / ;
: g-mean ( seq -- mean )
[ product ] [ length recip ] bi ^ ;
: h-mean ( seq -- mean )
[ length ] [ [ recip ] map-sum ] bi / ;
( scratchpad ) 10 [1,b] [ a-mean ] [ g-mean ] [ h-mean ] tri "%f >= %f >= %f\n" printf 5.500000 >= 4.528729 >= 3.414172
Fantom
class Main
{
static Float arithmeticMean (Int[] nums)
{
if (nums.size == 0) return 0.0f
sum := 0
nums.each |n| { sum += n }
return sum.toFloat / nums.size
}
static Float geometricMean (Int[] nums)
{
if (nums.size == 0) return 0.0f
product := 1
nums.each |n| { product *= n }
return product.toFloat.pow(1f/nums.size)
}
static Float harmonicMean (Int[] nums)
{
if (nums.size == 0) return 0.0f
reciprocals := 0f
nums.each |n| { reciprocals += 1f / n }
return nums.size.toFloat / reciprocals
}
public static Void main ()
{
items := (1..10).toList
// display results
echo (arithmeticMean (items))
echo (geometricMean (items))
echo (harmonicMean (items))
// check given relation
if ((arithmeticMean (items) >= geometricMean (items)) &&
(geometricMean (items) >= harmonicMean (items)))
echo ("relation holds")
else
echo ("relation failed")
}
}
Forth
: famean ( faddr n -- f )
0e
tuck floats bounds do
i f@ f+
float +loop
0 d>f f/ ;
: fgmean ( faddr n -- f )
1e
tuck floats bounds do
i f@ f*
float +loop
0 d>f 1/f f** ;
: fhmean ( faddr n -- f )
dup 0 d>f 0e
floats bounds do
i f@ 1/f f+
float +loop
f/ ;
create test 1e f, 2e f, 3e f, 4e f, 5e f, 6e f, 7e f, 8e f, 9e f, 10e f,
test 10 famean fdup f.
test 10 fgmean fdup fdup f.
test 10 fhmean fdup f.
( A G G H )
f>= . f>= . \ -1 -1
Fortran
program Mean
real :: a(10) = (/ (i, i=1,10) /)
real :: amean, gmean, hmean
amean = sum(a) / size(a)
gmean = product(a)**(1.0/size(a))
hmean = size(a) / sum(1.0/a)
if ((amean < gmean) .or. (gmean < hmean)) then
print*, "Error!"
else
print*, amean, gmean, hmean
end if
end program Mean
FreeBASIC
' FB 1.05.0 Win64
Function ArithmeticMean(array() As Double) As Double
Dim length As Integer = Ubound(array) - Lbound(array) + 1
Dim As Double sum = 0.0
For i As Integer = LBound(array) To UBound(array)
sum += array(i)
Next
Return sum/length
End Function
Function GeometricMean(array() As Double) As Double
Dim length As Integer = Ubound(array) - Lbound(array) + 1
Dim As Double product = 1.0
For i As Integer = LBound(array) To UBound(array)
product *= array(i)
Next
Return product ^ (1.0 / length)
End Function
Function HarmonicMean(array() As Double) As Double
Dim length As Integer = Ubound(array) - Lbound(array) + 1
Dim As Double sum = 0.0
For i As Integer = LBound(array) To UBound(array)
sum += 1.0 / array(i)
Next
Return length / sum
End Function
Dim vector(1 To 10) As Double
For i As Integer = 1 To 10
vector(i) = i
Next
Print "Arithmetic mean is :"; ArithmeticMean(vector())
Print "Geometric mean is :"; GeometricMean(vector())
Print "Harmonic mean is :"; HarmonicMean(vector())
Print
Print "Press any key to quit the program"
Sleep
- Output:
Arithmetic mean is : 5.5 Geometric mean is : 4.528728688116765 Harmonic mean is : 3.414171521474055
FunL
import lists.zip
def
mean( s, 0 ) = product( s )^(1/s.length())
mean( s, p ) = (1/s.length() sum( x^p | x <- s ))^(1/p)
def
monotone( [_], _ ) = true
monotone( a1:a2:as, p ) = p( a1, a2 ) and monotone( a2:as, p )
means = [mean( 1..10, m ) | m <- [1, 0, -1]]
for (m, l) <- zip( means, ['Arithmetic', 'Geometric', 'Harmonic'] )
println( "$l: $m" + (if m is Rational then " or ${m.doubleValue()}" else '') )
println( monotone(means, (>=)) )
- Output:
Arithmetic: 11/2 or 5.5 Geometric: 4.528728688116765 Harmonic: 25200/7381 or 3.414171521474055 true
Futhark
fun arithmetic_mean(as: [n]f64): f64 =
reduce (+) 0.0 (map (/f64(n)) as)
fun geometric_mean(as: [n]f64): f64 =
reduce (*) 1.0 (map (**(1.0/f64(n))) as)
fun harmonic_mean(as: [n]f64): f64 =
f64(n) / reduce (+) 0.0 (map (1.0/) as)
fun main(as: [n]f64): (f64,f64,f64) =
(arithmetic_mean as,
geometric_mean as,
harmonic_mean as)
FutureBasic
Double ari = 1, geo = 0, har = 0
Short i, n = 10
for i = 1 to n
ari += i
geo *= i
har += 1 \ i
next
print "ari:", ari \ n
print "geo:", geo^( 1 \ n )
print "har:", n \ har
handleevents
ari: 5.5 geo: 4.528728688116765 har: 3.414171521474055
GAP
# The first two work with rationals or with floats
# (but bear in mind that support of floating point is very poor in GAP)
mean := v -> Sum(v) / Length(v);
harmean := v -> Length(v) / Sum(v, Inverse);
geomean := v -> EXP_FLOAT(Sum(v, LOG_FLOAT) / Length(v));
mean([1 .. 10]);
# 11/2
harmean([1 .. 10]);
# 25200/7381
v := List([1..10], FLOAT_INT);;
mean(v);
# 5.5
harmean(v);
# 3.41417
geomean(v);
# 4.52873
Go
package main
import (
"fmt"
"math"
)
func main() {
sum, sumr, prod := 0., 0., 1.
for n := 1.; n <= 10; n++ {
sum += n
sumr += 1 / n
prod *= n
}
a, g, h := sum/10, math.Pow(prod, .1), 10/sumr
fmt.Println("A:", a, "G:", g, "H:", h)
fmt.Println("A >= G >= H:", a >= g && g >= h)
}
- Output:
A: 5.5 G: 4.528728688116765 H: 3.414171521474055 A >= G >= H: true
Groovy
Solution:
def arithMean = { list ->
list == null \
? null \
: list.empty \
? 0 \
: list.sum() / list.size()
}
def geomMean = { list ->
list == null \
? null \
: list.empty \
? 1 \
: list.inject(1) { prod, item -> prod*item } ** (1 / list.size())
}
def harmMean = { list ->
list == null \
? null \
: list.empty \
? 0 \
: list.size() / list.collect { 1.0/it }.sum()
}
Test:
def list = 1..10
def A = arithMean(list)
def G = geomMean(list)
assert A >= G
def H = harmMean(list)
assert G >= H
println """
list: ${list}
A: ${A}
G: ${G}
H: ${H}
"""
- Output:
list: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] A: 5.5 G: 4.528728688116765 H: 3.4141715214
Haskell
One generalized function
The general function given here yields an arithmetic mean when its first argument is 1
, a geometric mean when its first argument is 0
, and a harmonic mean when its first argument is -1
.
import Data.List (genericLength)
import Control.Monad (zipWithM_)
mean :: Double -> [Double] -> Double
mean 0 xs = product xs ** (1 / genericLength xs)
mean p xs = (1 / genericLength xs * sum (map (** p) xs)) ** (1/p)
main = do
let ms = zipWith ((. flip mean [1..10]). (,)) "agh" [1, 0, -1]
mapM_ (\(t,m) -> putStrLn $ t : ": " ++ show m) ms
putStrLn $ " a >= g >= h is " ++ show ((\(_,[a,g,h])-> a>=g && g>=h) (unzip ms))
Three applicatively defined functions
These three functions (each combining the length of a list with some kind of fold over the elements of that same list), all share the same applicative structure.
import Data.List (genericLength)
-- ARITHMETIC, GEOMETRIC AND HARMONIC MEANS ---------------
arithmetic, geometric, harmonic :: [Double] -> Double
arithmetic = (/) . sum <*> genericLength
geometric = (**) . product <*> ((1 /) . genericLength)
harmonic = (/) . genericLength <*> foldr ((+) . (1 /)) 0
-- TEST ---------------------------------------------------
xs :: [Double]
xs = [arithmetic, geometric, harmonic] <*> [[1 .. 10]]
main :: IO ()
main =
(putStrLn . unlines)
[ zip ["Arithmetic", "Geometric", "Harmonic"] xs >>= show
, mappend "\n A >= G >= H is " $ --
(show . and) $ zipWith (>=) xs (tail xs)
]
- Output:
("Arithmetic",5.5)("Geometric",4.528728688116765)("Harmonic",3.414171521474055) a >= g >= h is True
HicEst
AGH = ALIAS( A, G, H ) ! named vector elements
AGH = (0, 1, 0)
DO i = 1, 10
A = A + i
G = G * i
H = H + 1/i
ENDDO
AGH = (A/10, G^0.1, 10/H)
WRITE(ClipBoard, Name) AGH, "Result = " // (A>=G) * (G>=H)
! A=5.5; G=4.528728688; H=3.414171521; Result = 1;
Icon and Unicon
numbers:amean, numbers:gmean, and numbers:hmean are shown below:
- Output:
#means.exe x := [ 1 2 3 4 5 6 7 8 9 10 ] Arithmetic mean:5.5 Geometric mean:4.528728688116765 Harmonic mean:3.414171521474055 a >= g >= h is true
IS-BASIC
100 PROGRAM "Averages.bas"
110 NUMERIC ARR(1 TO 10)
120 FOR I=LBOUND(ARR) TO UBOUND(ARR)
130 LET ARR(I)=I
140 NEXT
150 PRINT "Arithmetic mean =";ARITHM(ARR)
160 PRINT "Geometric mean =";GEOMETRIC(ARR)
170 PRINT "Harmonic mean =";HARMONIC(ARR)
180 DEF ARITHM(REF A)
190 LET T=0
200 FOR I=LBOUND(A) TO UBOUND(A)
210 LET T=T+A(I)
220 NEXT
230 LET ARITHM=T/SIZE(A)
240 END DEF
250 DEF GEOMETRIC(REF A)
260 LET T=1
270 FOR I=LBOUND(A) TO UBOUND(A)
280 LET T=T*A(I)
290 NEXT
300 LET GEOMETRIC=T^(1/SIZE(A))
310 END DEF
320 DEF HARMONIC(REF A)
330 LET T=0
340 FOR I=LBOUND(A) TO UBOUND(A)
350 LET T=T+(1/A(I))
360 NEXT
370 LET HARMONIC=SIZE(A)/T
380 END DEF
J
Solution:
amean=: +/ % #
gmean=: # %: */
hmean=: amean&.:%
Example Usage:
(amean , gmean , hmean) >: i. 10
5.5 4.528729 3.414172
assert 2 >:/\ (amean , gmean , hmean) >: i. 10 NB. check amean >= gmean and gmean >= hmean
Note that gmean could have instead been defined as mean under logarithm, for example:
gmean=:amean&.:^.
(and this variant should probably be preferred - especially if the argument list is long, to avoid problems with floating point infinity.)
Java
import java.util.Arrays;
import java.util.List;
public class PythagoreanMeans {
public static double arithmeticMean(List<Double> numbers) {
if (numbers.isEmpty()) return Double.NaN;
double mean = 0.0;
for (Double number : numbers) {
mean += number;
}
return mean / numbers.size();
}
public static double geometricMean(List<Double> numbers) {
if (numbers.isEmpty()) return Double.NaN;
double mean = 1.0;
for (Double number : numbers) {
mean *= number;
}
return Math.pow(mean, 1.0 / numbers.size());
}
public static double harmonicMean(List<Double> numbers) {
if (numbers.isEmpty() || numbers.contains(0.0)) return Double.NaN;
double mean = 0.0;
for (Double number : numbers) {
mean += (1.0 / number);
}
return numbers.size() / mean;
}
public static void main(String[] args) {
Double[] array = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0};
List<Double> list = Arrays.asList(array);
double arithmetic = arithmeticMean(list);
double geometric = geometricMean(list);
double harmonic = harmonicMean(list);
System.out.format("A = %f G = %f H = %f%n", arithmetic, geometric, harmonic);
System.out.format("A >= G is %b, G >= H is %b%n", (arithmetic >= geometric), (geometric >= harmonic));
}
}
- Output:
A = 5.500000 G = 4.528729 H = 3.414172 A >= G is true, G >= H is true
We can rewrite the 3 methods using the new JAVA Stream API:
public static double arithmAverage(double array[]){
if (array == null ||array.length == 0) {
return 0.0;
}
else {
return DoubleStream.of(array).average().getAsDouble();
}
}
public static double geomAverage(double array[]){
if (array == null ||array.length == 0) {
return 0.0;
}
else {
double aver = DoubleStream.of(array).reduce(1, (x, y) -> x * y);
return Math.pow(aver, 1.0 / array.length);
}
}
public static double harmAverage(double array[]){
if (array == null ||array.length == 0) {
return 0.0;
}
else {
double aver = DoubleStream.of(array)
// remove null values
.filter(n -> n > 0.0)
// generate 1/n array
.map( n-> 1.0/n)
// accumulating
.reduce(0, (x, y) -> x + y);
// just this reduce is not working- need to do in 2 steps
// .reduce(0, (x, y) -> 1.0/x + 1.0/y);
return array.length / aver ;
}
}
JavaScript
ES5
(function () {
'use strict';
// arithmetic_mean :: [Number] -> Number
function arithmetic_mean(ns) {
return (
ns.reduce( // sum
function (sum, n) {
return (sum + n);
},
0
) / ns.length
);
}
// geometric_mean :: [Number] -> Number
function geometric_mean(ns) {
return Math.pow(
ns.reduce( // product
function (product, n) {
return (product * n);
},
1
),
1 / ns.length
);
}
// harmonic_mean :: [Number] -> Number
function harmonic_mean(ns) {
return (
ns.length / ns.reduce( // sum of inverses
function (invSum, n) {
return (invSum + (1 / n));
},
0
)
);
}
var values = [arithmetic_mean, geometric_mean, harmonic_mean]
.map(function (f) {
return f([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
}),
mean = {
Arithmetic: values[0], // arithmetic
Geometric: values[1], // geometric
Harmonic: values[2] // harmonic
}
return JSON.stringify({
values: mean,
test: "is A >= G >= H ? " +
(
mean.Arithmetic >= mean.Geometric &&
mean.Geometric >= mean.Harmonic ? "yes" : "no"
)
}, null, 2);
})();
- Output:
{
"values": {
"Arithmetic": 5.5,
"Geometric": 4.528728688116765,
"Harmonic": 3.414171521474055
},
"test": "is A >= G >= H ? yes"
}
ES6
(() => {
// arithmeticMean :: [Number] -> Number
const arithmeticMean = xs =>
foldl((sum, n) => sum + n, 0, xs) / length(xs);
// geometricMean :: [Number] -> Number
const geometricMean = xs =>
raise(foldl((product, x) => product * x, 1, xs), 1 / length(xs));
// harmonicMean :: [Number] -> Number
const harmonicMean = xs =>
length(xs) / foldl((invSum, n) => invSum + (1 / n), 0, xs);
// GENERIC FUNCTIONS ------------------------------------------------------
// A list of functions applied to a list of arguments
// <*> :: [(a -> b)] -> [a] -> [b]
const ap = (fs, xs) => //
[].concat.apply([], fs.map(f => //
[].concat.apply([], xs.map(x => [f(x)]))));
// foldl :: (b -> a -> b) -> b -> [a] -> b
const foldl = (f, a, xs) => xs.reduce(f, a);
// length :: [a] -> Int
const length = xs => xs.length;
// mapFromList :: [(k, v)] -> Dictionary
const mapFromList = kvs =>
foldl((a, [k, v]) =>
(a[(typeof k === 'string' && k) || show(k)] = v, a), {}, kvs);
// raise :: Num -> Int -> Num
const raise = (n, e) => Math.pow(n, e);
// show :: a -> String
// show :: a -> Int -> String
const show = (...x) =>
JSON.stringify.apply(
null, x.length > 1 ? [x[0], null, x[1]] : x
);
// zip :: [a] -> [b] -> [(a,b)]
const zip = (xs, ys) =>
xs.slice(0, Math.min(xs.length, ys.length))
.map((x, i) => [x, ys[i]]);
// TEST -------------------------------------------------------------------
// mean :: Dictionary
const mean = mapFromList(zip(
['Arithmetic', 'Geometric', 'Harmonic'],
ap([arithmeticMean, geometricMean, harmonicMean], [
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
])
));
return show({
values: mean,
test: `is A >= G >= H ? ${mean.Arithmetic >= mean.Geometric &&
mean.Geometric >= mean.Harmonic ? "yes" : "no"}`
}, 2);
})();
- Output:
{
"values": {
"Arithmetic": 5.5,
"Geometric": 4.528728688116765,
"Harmonic": 3.414171521474055
},
"test": "is A >= G >= H ? yes"
}
jq
def amean: add/length;
def logProduct: map(log) | add;
def gmean: (logProduct / length) | exp;
def hmean: length / (map(1/.) | add);
# Tasks:
[range(1;11) ] | [amean, gmean, hmean] as $ans
| ( $ans[],
"amean > gmean > hmean => \($ans[0] > $ans[1] and $ans[1] > $ans[2] )" )
- Output:
5.5 4.528728688116766 3.414171521474055 "amean > gmean > hmean => true"
Julia
Julia has a `mean` function to compute the arithmetic mean of a collections of numbers. We can redefine it as follows.
amean(A) = sum(A)/length(A)
gmean(A) = prod(A)^(1/length(A))
hmean(A) = length(A)/sum(1./A)
- Output:
julia> map(f-> f(1:10), [amean, gmean, hmean]) 3-element Array{Float64,1}: 5.5 4.52873 3.41417 julia> ans[1] > ans[2] > ans[3] true
K
am:{(+/x)%#x}
gm:{(*/x)^(%#x)}
hm:{(#x)%+/%:'x}
{(am x;gm x;hm x)} 1+!10
5.5 4.528729 3.414172
Kotlin
import kotlin.math.round
import kotlin.math.pow
fun Collection<Double>.geometricMean() =
if (isEmpty()) Double.NaN
else (reduce { n1, n2 -> n1 * n2 }).pow(1.0 / size)
fun Collection<Double>.harmonicMean() =
if (isEmpty() || contains(0.0)) Double.NaN
else size / fold(0.0) { n1, n2 -> n1 + 1.0 / n2 }
fun Double.toFixed(len: Int = 6) =
round(this * 10.0.pow(len)) / 10.0.pow(len)
fun main() {
val list = listOf(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0)
val a = list.average() // arithmetic mean
val g = list.geometricMean()
val h = list.harmonicMean()
println("A = $a G = ${g.toFixed()} H = ${h.toFixed()}")
println("A >= G is ${a >= g}, G >= H is ${g >= h}")
require(g in h..a)
}
- Output:
A = 5.500000 G = 4.528729 H = 3.414172 A >= G is true, G >= H is true
Lasso
define arithmetic_mean(a::staticarray)::decimal => {
//sum of the list divided by its length
return (with e in #a sum #e) / decimal(#a->size)
}
define geometric_mean(a::staticarray)::decimal => {
// The geometric mean is the nth root of the product of the list
local(prod = 1)
with e in #a do => { #prod *= #e }
return math_pow(#prod,1/decimal(#a->size))
}
define harmonic_mean(a::staticarray)::decimal => {
// The harmonic mean is n divided by the sum of the reciprocal of each item in the list
return decimal(#a->size)/(with e in #a sum 1/decimal(#e))
}
arithmetic_mean(generateSeries(1,10)->asStaticArray)
geometric_mean(generateSeries(1,10)->asStaticArray)
harmonic_mean(generateSeries(1,10)->asStaticArray)
- Output:
5.500000 4.528729 3.414172
Liberty BASIC
for i = 1 to 10
a = a + i
next
ArithmeticMean = a/10
b = 1
for i = 1 to 10
b = b * i
next
GeometricMean = b ^ (1/10)
for i = 1 to 10
c = c + (1/i)
next
HarmonicMean = 10/c
print "ArithmeticMean: ";ArithmeticMean
print "Geometric Mean: ";GeometricMean
print "Harmonic Mean: ";HarmonicMean
if (ArithmeticMean>=GeometricMean) and (GeometricMean>=HarmonicMean) then
print "True"
else
print "False"
end if
Logo
to compute_means :count
local "sum
make "sum 0
local "product
make "product 1
local "reciprocal_sum
make "reciprocal_sum 0
repeat :count [
make "sum sum :sum repcount
make "product product :product repcount
make "reciprocal_sum sum :reciprocal_sum (quotient repcount)
]
output (sentence (quotient :sum :count) (power :product (quotient :count))
(quotient :count :reciprocal_sum))
end
make "means compute_means 10
print sentence [Arithmetic mean is] item 1 :means
print sentence [Geometric mean is] item 2 :means
print sentence [Harmonic mean is] item 3 :means
bye
Lua
function fsum(f, a, ...) return a and f(a) + fsum(f, ...) or 0 end
function pymean(t, f, finv) return finv(fsum(f, unpack(t)) / #t) end
nums = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
--arithmetic
a = pymean(nums, function(n) return n end, function(n) return n end)
--geometric
g = pymean(nums, math.log, math.exp)
--harmonic
h = pymean(nums, function(n) return 1/n end, function(n) return 1/n end)
print(a, g, h)
assert(a >= g and g >= h)
M2000 Interpreter
Dimension(m,0) is the base (lower bound) for each dimension in an array, and can be 0 or 1. Len(a) or len(M()) return length of a pointer to array and an array, as number of array elements. For one dimension arrays len() is equal to Dimension(m(),1) where 1 is the first dimension Dim A(10,10) : Print Len(A())=100
Module CheckIt {
sum=lambda -> {
Read m as array
if len(m)=0 then =0 : exit
sum=Array(m, Dimension(m,0))
If len(m)=1 then =sum : exit
k=each(m,2,-1)
While k {
sum+=Array(k)
}
=sum
}
mean=lambda sum (a as array) ->{
=sum(a)/len(a)
}
prod=lambda -> {
m=array
if len(m)=0 then =0 : exit
prod=Array(m, Dimension(m,0))
If len(m)=1 then =prod : exit
k=each(m,2,-1)
While k {
prod*=Array(k)
}
=prod
}
geomean=lambda prod (a as array) -> {
=prod(a)^(1/len(a))
}
harmomean=lambda (a as array) -> {
if len(a)=0 then =0 : exit
sum=1/Array(a, Dimension(a,0))
If len(a)=1 then =1/sum : exit
k=each(a,2,-1)
While k {
sum+=1/Array(k)
}
=len(a)/sum
}
Print sum((1,2,3,4,5))=15
Print prod((1,2,3,4,5))=120
Print mean((1,2,3,4,5))==3
\\ use == to apply rounding before comparison
Print geomean((1,2,3,4,5))==2.60517108469735
Print harmomean((1,2,3,4,5))==2.18978102189784
Generator =lambda x=1 ->{=x : x++}
dim a(10)<<Generator()
Print mean(a())==5.5
Print geomean(a())==4.52872868811677
Print harmomean(a())==3.41417152147412
}
CheckIt
Maple
x := [ seq( 1 .. 10 ) ];
Means := proc( x )
uses Statistics;
return Mean( x ), GeometricMean( x ), HarmonicMean( x );
end proc:
Arithmeticmean, Geometricmean, Harmonicmean := Means( x );
is( Arithmeticmean >= Geometricmean and Geometricmean >= Harmonicmean );
- Output:
Arithmeticmean, Geometricmean, Harmonicmean := 5.50000000000000, 4.52872868811677, 3.41417152147406 true
Mathematica / Wolfram Language
Print["{Arithmetic Mean, Geometric Mean, Harmonic Mean} = ",
N@Through[{Mean, GeometricMean, HarmonicMean}[Range@10]]]
- Output:
{Arithmetic Mean, Geometric Mean, Harmonic Mean} = {5.5,4.52873,3.41417}
MATLAB
function [A,G,H] = pythagoreanMeans(list)
A = mean(list);
G = geomean(list);
H = harmmean(list);
end
A solution that works for both, Matlab and Octave, is this
function [A,G,H] = pythagoreanMeans(list)
A = mean(list); % arithmetic mean
G = exp(mean(log(list))); % geometric mean
H = 1./mean(1./list); % harmonic mean
end
Solution:
>> [A,G,H]=pythagoreanMeans((1:10))
A =
5.500000000000000
G =
4.528728688116765
H =
3.414171521474055
Maxima
/* built-in */
L: makelist(i, i, 1, 10)$
mean(L), numer; /* 5.5 */
geometric_mean(L), numer; /* 4.528728688116765 */
harmonic_mean(L), numer; /* 3.414171521474055 */
min
'avg ^A
(dup product 1 rolldown size / pow) ^G
('size keep (1 swap /) (+) map-reduce /) ^H
(((1 10)) range (((A) (G) (H))) cleave) => (puts!) foreach
- Output:
5.5 4.528728688116765 3.414171521474055
Modula-2
MODULE PythagoreanMeans;
FROM FormatString IMPORT FormatString;
FROM LongMath IMPORT power;
FROM LongStr IMPORT RealToStr;
FROM Terminal IMPORT WriteString,WriteLn,ReadChar;
PROCEDURE ArithmeticMean(numbers : ARRAY OF LONGREAL) : LONGREAL;
VAR
i,cnt : CARDINAL;
mean : LONGREAL;
BEGIN
mean := 0.0;
cnt := 0;
FOR i:=0 TO HIGH(numbers) DO
mean := mean + numbers[i];
INC(cnt);
END;
RETURN mean / LFLOAT(cnt)
END ArithmeticMean;
PROCEDURE GeometricMean(numbers : ARRAY OF LONGREAL) : LONGREAL;
VAR
i,cnt : CARDINAL;
mean : LONGREAL;
BEGIN
mean := 1.0;
cnt := 0;
FOR i:=0 TO HIGH(numbers) DO
mean := mean * numbers[i];
INC(cnt);
END;
RETURN power(mean, 1.0 / LFLOAT(cnt))
END GeometricMean;
PROCEDURE HarmonicMean(numbers : ARRAY OF LONGREAL) : LONGREAL;
VAR
i,cnt : CARDINAL;
mean : LONGREAL;
BEGIN
mean := 0.0;
cnt := 0;
FOR i:=0 TO HIGH(numbers) DO
mean := mean + ( 1.0 / numbers[i]);
INC(cnt);
END;
RETURN LFLOAT(cnt) / mean
END HarmonicMean;
CONST Size = 10;
TYPE DA = ARRAY[1..Size] OF LONGREAL;
VAR
buf : ARRAY[0..63] OF CHAR;
array : DA;
arithmetic,geometric,harmonic : LONGREAL;
BEGIN
array := DA{1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0};
arithmetic := ArithmeticMean(array);
geometric := GeometricMean(array);
harmonic := HarmonicMean(array);
WriteString("A = ");
RealToStr(arithmetic, buf);
WriteString(buf);
WriteString(" G = ");
RealToStr(geometric, buf);
WriteString(buf);
WriteString(" H = ");
RealToStr(harmonic, buf);
WriteString(buf);
WriteLn;
FormatString("A >= G is %b, G >= H is %b\n", buf, arithmetic >= geometric, geometric >= harmonic);
WriteString(buf);
ReadChar
END PythagoreanMeans.
MUMPS
Pyth(n) New a,ii,g,h,x
For ii=1:1:n set x(ii)=ii
;
; Average
Set a=0 For ii=1:1:n Set a=a+x(ii)
Set a=a/n
;
; Geometric
Set g=1 For ii=1:1:n Set g=g*x(ii)
Set g=g**(1/n)
;
; Harmonic
Set h=0 For ii=1:1:n Set h=1/x(ii)+h
Set h=n/h
;
Write !,"Pythagorean means for 1..",n,":",!
Write "Average = ",a," >= Geometric ",g," >= harmonic ",h,!
Quit
Do Pyth(10)
Pythagorean means for 1..10:
Average = 5.5 >= Geometric 4.528728688116178495 >= harmonic 3.414171521474055006
NetRexx
/* NetRexx */
options replace format comments java crossref symbols nobinary
numeric digits 20
a1 = ArrayList(Arrays.asList([Rexx 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0]))
say "Arithmetic =" arithmeticMean(a1)", Geometric =" geometricMean(a1)", Harmonic =" harmonicMean(a1)
return
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method arithmeticMean(numbers = java.util.List) public static returns Rexx
-- somewhat arbitrary return for ooRexx
if numbers.isEmpty then return "NaN"
mean = 0
number = Rexx
loop number over numbers
mean = mean + number
end
return mean / numbers.size
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method geometricMean(numbers = java.util.List) public static returns Rexx
-- somewhat arbitrary return for ooRexx
if numbers.isEmpty then return "NaN"
mean = 1
number = Rexx
loop number over numbers
mean = mean * number
end
return Math.pow(mean, 1 / numbers.size)
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method harmonicMean(numbers = java.util.List) public static returns Rexx
-- somewhat arbitrary return for ooRexx
if numbers.isEmpty then return "NaN"
mean = 0
number = Rexx
loop number over numbers
if number = 0 then return "Nan"
mean = mean + (1 / number)
end
-- problem here...
return numbers.size / mean
- Output:
Arithmetic = 5.5, Geometric = 4.528728688116765, Harmonic = 3.4141715214740550062
Nim
import math, sequtils, sugar
proc amean(num: seq[float]): float =
sum(num) / float(len(num))
proc gmean(num: seq[float]): float =
result = 1
for n in num: result *= n
result = pow(result, 1.0 / float(num.len))
proc hmean(num: seq[float]): float =
for n in num: result += 1.0 / n
result = float(num.len) / result
proc ameanFunctional(num: seq[float]): float =
sum(num) / float(num.len)
proc gmeanFunctional(num: seq[float]): float =
num.foldl(a * b).pow(1.0 / float(num.len))
proc hmeanFunctional(num: seq[float]): float =
float(num.len) / sum(num.mapIt(1.0 / it))
let numbers = toSeq(1..10).map((x: int) => float(x))
echo amean(numbers), " ", gmean(numbers), " ", hmean(numbers)
- Output:
5.5 4.528728688116765 3.414171521474055
Oberon-2
Oxford Oberon-2
MODULE PythMean;
IMPORT Out, ML := MathL;
PROCEDURE Triplets(a: ARRAY OF INTEGER;VAR triplet: ARRAY OF LONGREAL);
VAR
i: INTEGER;
BEGIN
triplet[0] := 0.0;triplet[1] := 0.0; triplet[2] := 0.0;
FOR i:= 0 TO LEN(a) - 1 DO
triplet[0] := triplet[0] + a[i];
triplet[1] := triplet[1] + ML.Ln(a[i]);
triplet[2] := triplet[2] + (1 / a[i])
END
END Triplets;
PROCEDURE Means*(a: ARRAY OF INTEGER);
VAR
triplet: ARRAY 3 OF LONGREAL;
BEGIN
Triplets(a,triplet);
Out.String("A(1 .. 10): ");Out.LongReal(triplet[0] / LEN(a));Out.Ln;
Out.String("G(1 .. 10): ");Out.LongReal(ML.Exp(triplet[1]/ LEN(a)));Out.Ln;
Out.String("H(1 .. 10): ");Out.LongReal(LEN(a) / triplet[2]);Out.Ln;
END Means;
VAR
nums: ARRAY 10 OF INTEGER;
i: INTEGER;
BEGIN
FOR i := 0 TO LEN(nums) - 1 DO
nums[i] := i + 1
END;
Means(nums)
END PythMean.
- Output:
A(1 .. 10): 5.50000000000 G(1 .. 10): 4.52872868812 H(1 .. 10): 3.41417152147
Objeck
class PythagMeans {
function : Main(args : String[]) ~ Nil {
array := [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0];
arithmetic := ArithmeticMean(array);
geometric := GeometricMean(array);
harmonic := HarmonicMean(array);
arith_geo := arithmetic >= geometric;
geo_harm := geometric >= harmonic;
"A = {$arithmetic}, G = {$geometric}, H = {$harmonic}"->PrintLine();
"A >= G is {$arith_geo}, G >= H is {$geo_harm}"->PrintLine();
}
function : native : ArithmeticMean(numbers : Float[]) ~ Float {
if(numbers->Size() = 0) { return -1.0; };
mean := 0.0;
each(i : numbers) {
mean += numbers[i];
};
return mean / numbers->Size();
}
function : native : GeometricMean(numbers : Float[]) ~ Float {
if(numbers->Size() = 0) { return -1.0; };
mean := 1.0;
each(i : numbers) {
mean *= numbers[i];
};
return mean->Power(1.0 / numbers->Size());
}
function : native : HarmonicMean(numbers : Float[]) ~ Float {
if(numbers->Size() = 0) { return -1.0; };
mean := 0.0;
each(i : numbers) {
mean += (1.0 / numbers[i]);
};
return numbers->Size() / mean;
}
}
Output:
A = 5.500, G = 4.529, H = 3.414 A >= G is true, G >= H is true
OCaml
The three means in one function
let means v =
let n = Array.length v
and a = ref 0.0
and b = ref 1.0
and c = ref 0.0 in
for i=0 to n-1 do
a := !a +. v.(i);
b := !b *. v.(i);
c := !c +. 1.0/.v.(i);
done;
let nn = float_of_int n in
(!a /. nn, !b ** (1.0/.nn), nn /. !c)
;;
- Output:
means (Array.init 10 (function i -> (float_of_int (i+1)))) ;; (* (5.5, 4.5287286881167654, 3.4141715214740551) *)
Another implementation using Array.fold_left
instead of a for loop:
let means v =
let (a, b, c) =
Array.fold_left
(fun (a, b, c) x -> (a+.x, b*.x, c+.1./.x))
(0.,1.,0.) v
in
let n = float_of_int (Array.length v) in
(a /. n, b ** (1./.n), n /. c)
;;
Octave
A = mean(list); % arithmetic mean
G = mean(list,'g'); % geometric mean
H = mean(list,'a'); % harmonic mean
See also Matlab implementation #MATLAB
Oforth
import: mapping
: A ( x )
x sum
x size dup ifZero: [ 2drop null ] else: [ >float / ]
;
: G( x ) #* x reduce x size inv powf ;
: H( x ) x size x map( #inv ) sum / ;
: averages
| g |
"Geometric mean :" . 10 seq G dup .cr ->g
"Arithmetic mean :" . 10 seq A dup . g >= ifTrue: [ " ==> A >= G" .cr ]
"Harmonic mean :" . 10 seq H dup . g <= ifTrue: [ " ==> G >= H" .cr ]
;
- Output:
Geometric mean : 4.52872868811677 Arithmetic mean : 5.5 ==> A >= G Harmonic mean : 3.41417152147406 ==> G >= H
ooRexx
a = .array~of(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0)
say "Arithmetic =" arithmeticMean(a)", Geometric =" geometricMean(a)", Harmonic =" harmonicMean(a)
::routine arithmeticMean
use arg numbers
-- somewhat arbitrary return for ooRexx
if numbers~isEmpty then return "NaN"
mean = 0
loop number over numbers
mean += number
end
return mean / numbers~items
::routine geometricMean
use arg numbers
-- somewhat arbitrary return for ooRexx
if numbers~isEmpty then return "NaN"
mean = 1
loop number over numbers
mean *= number
end
return rxcalcPower(mean, 1 / numbers~items)
::routine harmonicMean
use arg numbers
-- somewhat arbitrary return for ooRexx
if numbers~isEmpty then return "NaN"
mean = 0
loop number over numbers
if number = 0 then return "Nan"
mean += 1 / number
end
-- problem here....
return numbers~items / mean
::requires rxmath LIBRARY
- Output:
Arithmetic = 5.5, Geometric = 4.52872869, Harmonic = 3.41417153
Oz
declare
%% helpers
fun {Sum Xs} {FoldL Xs Number.'+' 0.0} end
fun {Product Xs} {FoldL Xs Number.'*' 1.0} end
fun {Len Xs} {Int.toFloat {Length Xs}} end
fun {AMean Xs}
{Sum Xs}
/
{Len Xs}
end
fun {GMean Xs}
{Pow
{Product Xs}
1.0/{Len Xs}}
end
fun {HMean Xs}
{Len Xs}
/
{Sum {Map Xs fun {$ X} 1.0 / X end}}
end
Numbers = {Map {List.number 1 10 1} Int.toFloat}
[A G H] = [{AMean Numbers} {GMean Numbers} {HMean Numbers}]
in
{Show [A G H]}
A >= G = true
G >= H = true
PARI/GP
General implementations:
arithmetic(v)={
sum(i=1,#v,v[i])/#v
};
geometric(v)={
prod(i=1,#v,v[i])^(1/#v)
};
harmonic(v)={
#v/sum(i=1,#v,1/v[i])
};
v=vector(10,i,i);
[arithmetic(v),geometric(v),harmonic(v)]
Specific to the first n positive integers:
arithmetic_first(n)={
(n+1)/2
};
geometric_first(n)={
n!^(1/n)
};
harmonic_first(n)={
n/if(n>1000,
log(n)+Euler+1/(n+n)+1/(12*n^2)-1/(120*n^4)+1/(252*n^6)-1/(240*n^8)+1/(132*n^10)
,
n/sum(k=1,n,1/k)
)
};
[arithmetic_first(10),geometric_first(10),harmonic_first(10)]
%[1]>=%[2] && %[2] >= %[3]
These are, asymptotically, n/2, n/e, and n/log n.
Pascal
See Delphi
Perl
sub A
{
my $a = 0;
$a += $_ for @_;
return $a / @_;
}
sub G
{
my $p = 1;
$p *= $_ for @_;
return $p**(1/@_); # power of 1/n == root of n
}
sub H
{
my $h = 0;
$h += 1/$_ for @_;
return @_/$h;
}
my @ints = (1..10);
my $a = A(@ints);
my $g = G(@ints);
my $h = H(@ints);
print "A=$a\nG=$g\nH=$h\n";
die "Error" unless $a >= $g and $g >= $h;
Phix
with javascript_semantics function arithmetic_mean(sequence s) return sum(s)/length(s) end function function geometric_mean(sequence s) return power(product(s),1/length(s)) end function function harmonic_mean(sequence s) return length(s)/sum(sq_div(1,s)) end function constant s = {1,2,3,4,5,6,7,8,9,10} constant arithmetic = arithmetic_mean(s), geometric = geometric_mean(s), harmonic = harmonic_mean(s) printf(1,"Arithmetic: %.10g\n", arithmetic) printf(1,"Geometric: %.10g\n", geometric) printf(1,"Harmonic: %.10g\n", harmonic) printf(1,"Arithmetic>=Geometric>=Harmonic: %t\n", {arithmetic>=geometric and geometric>=harmonic})
- Output:
Arithmetic: 5.5 Geometric: 4.528728688 Harmonic: 3.414171521 Arithmetic>=Geometric>=Harmonic: true
PHP
<?php
// Created with PHP 7.0
function ArithmeticMean(array $values)
{
return array_sum($values) / count($values);
}
function GeometricMean(array $values)
{
return array_product($values) ** (1 / count($values));
}
function HarmonicMean(array $values)
{
$sum = 0;
foreach ($values as $value) {
$sum += 1 / $value;
}
return count($values) / $sum;
}
$values = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
echo "Arithmetic: " . ArithmeticMean($values) . "\n";
echo "Geometric: " . GeometricMean($values) . "\n";
echo "Harmonic: " . HarmonicMean($values) . "\n";
- Output:
Arithmetic: 5.5 Geometric: 4.5287286881168 Harmonic: 3.4141715214741
PicoLisp
(load "@lib/math.l")
(let (Lst (1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0) Len (length Lst))
(prinl "Arithmetic mean: "
(format
(/ (apply + Lst) Len)
*Scl ) )
(prinl "Geometric mean: "
(format
(pow (*/ (apply * Lst) (** 1.0 (dec Len))) (/ 1.0 Len))
*Scl ) )
(prinl "Harmonic mean: "
(format
(*/ (* 1.0 Len) 1.0 (sum '((N) (*/ 1.0 1.0 N)) Lst))
*Scl ) ) )
- Output:
Arithmetic mean: 5.500000 Geometric mean: 4.528729 Harmonic mean: 3.414172
PL/I
declare n fixed binary,
(Average, Geometric, Harmonic) float;
declare A(10) float static initial (1,2,3,4,5,6,7,8,9,10);
n = hbound(A,1);
/* compute the average */
Average = sum(A)/n;
/* Compute the geometric mean: */
Geometric = prod(A)**(1/n);
/* Compute the Harmonic mean: */
Harmonic = n / sum(1/A);
put skip data (Average);
put skip data (Geometric);
put skip data (Harmonic);
if Average < Geometric then put skip list ('Error');
if Geometric < Harmonic then put skip list ('Error');
Results:
AVERAGE= 5.50000E+0000; GEOMETRIC= 4.52873E+0000; HARMONIC= 3.41417E+0000;
PostScript
/pythamean{
/x exch def
/sum 0 def
/prod 1 def
/invsum 0 def
/i 1 def
x{
/sum sum i add def
/prod prod i mul def
/invsum invsum i -1 exp add def
/i i 1 add def
}repeat
(Arithmetic Mean : ) print
sum x div =
(Geometric Mean : ) print
prod x -1 exp exp =
(Harmonic Mean : ) print
x invsum div =
}def
10 pythamean
- Output:
Arithmetic Mean : 5.5 Geometric Mean : 4.52873 Harmonic Mean : 3.41417
/numbers {[1 10] 1 range}.
/recip {1 exch div}.
% Arithmetic mean
numbers dup 0 {+} fold exch length div
% Geometric mean
numbers dup 1 {*} fold exch length recip exp
% Harmonic mean
numbers dup 0 {recip +} fold exch length exch div
PowerShell
$A = 0
$LogG = 0
$InvH = 0
$ii = 1..10
foreach($i in $ii) {
# Arithmetic mean is computed directly
$A += $i / $ii.Count
# Geometric mean is computed using Logarithms
$LogG += [Math]::Log($i) / $ii.Count
# Harmonic mean is computed using its inverse
$InvH += 1 / ($i * $ii.Count)
}
$G = [Math]::Exp($LogG)
$H = 1/$InvH
write-host "Arithmetic mean: A = $A"
write-host "Geometric mean: G = $G"
write-host "Harmonic mean: H = $H"
write-host "Is A >= G ? $($A -ge $G)"
write-host "Is G >= H ? $($G -ge $H)"
- Output:
Arithmetic mean: A = 5.5 Geometric mean: G = 4.52872868811676 Harmonic mean: H = 3.41417152147405 Is A >= G ? True Is G >= H ? True
Processing
void setup() {
float[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
println("Arithmetic mean: " + arithmeticMean(numbers));
println("Geometric mean: " + geometricMean(numbers));
println("Harmonic mean: " + harmonicMean(numbers));
}
float arithmeticMean(float[] nums) {
float mean = 0;
for (float n : nums) {
mean += n;
}
mean = mean / nums.length;
return mean;
}
float geometricMean(float[] nums) {
float mean = 1;
for (float n : nums) {
mean *= n;
}
mean = pow(mean, 1.0 / nums.length);
return mean;
}
float harmonicMean(float[] nums) {
float mean = 0;
for (float n : nums) {
mean += 1 / n;
}
mean = nums.length / mean;
return mean;
}
- Output:
Arithmetic mean: 5.5 Geometric mean: 4.528729 Harmonic mean: 3.4141712
PureBasic
Procedure.d ArithmeticMean()
For a = 1 To 10
mean + a
Next
ProcedureReturn mean / 10
EndProcedure
Procedure.d GeometricMean()
mean = 1
For a = 1 To 10
mean * a
Next
ProcedureReturn Pow(mean, 1 / 10)
EndProcedure
Procedure.d HarmonicMean()
For a = 1 To 10
mean.d + 1 / a
Next
ProcedureReturn 10 / mean
EndProcedure
If HarmonicMean() <= GeometricMean() And GeometricMean() <= ArithmeticMean()
Debug "true"
EndIf
Debug ArithmeticMean()
Debug GeometricMean()
Debug HarmonicMean()
Python
from operator import mul
from functools import reduce
def amean(num):
return sum(num) / len(num)
def gmean(num):
return reduce(mul, num, 1)**(1 / len(num))
def hmean(num):
return len(num) / sum(1 / n for n in num)
numbers = range(1, 11) # 1..10
a, g, h = amean(numbers), gmean(numbers), hmean(numbers)
print(a, g, h)
assert a >= g >= h
- Output:
5.5 4.52872868812 3.41417152147
These are the same in Python 2 apart from requiring explicit float division (either through float()
casts or float literals such as 1./n
); or better, do a from __future__ import division
, which works on Python 2.2+ as well as Python 3, and makes division work consistently like it does in Python 3.
Quackery
Uses root
from Integer roots#Quackery.
[] 10 times [ i^ 1+ join ]
say "Arithmetic mean:" sp
0 over witheach +
over size 8 point$ echo$
cr
say " Geometric mean:" sp
1 over witheach *
over size 80 ** * 10 root
10 8 ** 8 point$ echo$
cr
say " Harmonic mean:" sp
dup size dip
[ 0 n->v rot
witheach [ n->v 1/v v+ ] ]
n->v 2swap v/ 8 point$ echo$
- Output:
Arithmetic mean: 5.5 Geometric mean: 4.52872868 Harmonic mean: 3.41417152
R
Initialise x
x <- 1:10
Arithmetic mean
a <- sum(x)/length(x)
or
a <- mean(x)
The geometric mean
g <- prod(x)^(1/length(x))
The harmonic mean (no error checking that )
h <- length(x)/sum(1/x)
Then:
a > g
and
g > h
give both
[1] TRUE
Racket
#lang racket
(define (arithmetic xs)
(/ (for/sum ([x xs]) x)
(length xs)))
(define (geometric xs)
(expt (for/product ([x xs]) x)
(/ (length xs))))
(define (harmonic xs)
(/ (length xs)
(for/sum ([x xs]) (/ x))))
(define xs (range 1 11))
(arithmetic xs)
(geometric xs)
(harmonic xs)
(>= (arithmetic xs) (geometric xs) (harmonic xs))
- Output:
5 1/2 4.528728688116765 3 3057/7381 #t
Raku
(formerly Perl 6)
sub A { ([+] @_) / @_ }
sub G { ([*] @_) ** (1 / @_) }
sub H { @_ / [+] 1 X/ @_ }
say "A(1,...,10) = ", A(1..10);
say "G(1,...,10) = ", G(1..10);
say "H(1,...,10) = ", H(1..10);
- Output:
A(1,...,10) = 5.5 G(1,...,10) = 4.52872868811677 H(1,...,10) = 3.41417152147406
REXX
REXX doesn't have a POW function, so an IROOT (integer root) function is included here; it includes an
extra error check if used as a general purpose function that would otherwise yield a complex result.
/*REXX program computes and displays the Pythagorean means [Amean, Gmean, Hmean]. */
numeric digits 20 /*use a little extra for the precision.*/
parse arg n . /*obtain the optional argument from CL.*/
if n=='' | n=="," then n= 10 /*None specified? Then use the default*/
sum= 0; prod= 1; rSum= 0 /*initialize sum/product/reciprocal sum*/
$=; do #=1 for n; $= $ # /*generate list by appending # to list.*/
sum = sum + # /*compute the sum of all the elements. */
prod= prod * # /*compute the product of all elements. */
rSum= rSum + 1/# /*compute the sum of the reciprocals. */
end /*#*/
say ' list ='$ /*display the list of numbers used. */
say 'Amean =' sum / n /*calculate & display arithmetic mean.*/
say 'Gmean =' Iroot(prod, n) /* " " " geometric " */
if result=="[n/a]" then say '***error***: root' y "can't be even if 1st argument is < 0."
say 'Hmean =' n / rSum /* " " " harmonic " */
exit 0 /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
Iroot: procedure; parse arg x 1 ox, y 1 oy /*get both args, and also a copy of X&Y*/
if x=0 | x=1 | y=1 then return x /*handle special case of zero and unity*/
if y=0 then return 1 /* " " " " a zero root.*/
if x<0 & y//2==0 then return '[n/a]' /*indicate result is "not applicable". */
x= abs(x); y= abs(y); m= y - 1 /*use the absolute value for X and Y. */
oDigs= digits(); a= oDigs + 5 /*save original digits; add five digs.*/
g= (x+1) / y*2 /*use this as the first guesstimate. */
d= 5 /*start with 5 dec digs, saves CPU time*/
do until d==a; d= min(d + d, a) /*keep going as digits are increased. */
numeric digits d; f= d - 2 /*limit digits to original digits + 5.*/
og= /*use a non─guess for the old G (guess)*/
do forever; gm= g**m /*keep computing at the Yth root. */
_= format( (m*g*gm + x)/(y*gm),,f) /*this is the nitty─gritty calculation.*/
if _=g | _=og then leave /*are we close enough yet? */
og= g; g= _ /*save guess ──► OG; set the new guess.*/
end /*forever*/
end /*until */
g= g * sign(ox); if oy<0 then g= 1 / g /*adjust for original X sign; neg. root*/
numeric digits oDigs; return g / 1 /*normalize to original decimal digits.*/
- output when using the default input:
list = 1 2 3 4 5 6 7 8 9 10 Amean = 5.5 Gmean = 4.5287286881167647622 Hmean = 3.4141715214740550062
Ring
decimals(8)
array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
see "arithmetic mean = " + arithmeticMean(array) + nl
see "geometric mean = " + geometricMean(array) + nl
see "harmonic mean = " + harmonicMean(array) + nl
func arithmeticMean a
return summary(a) / len(a)
func geometricMean a
b = 1
for i = 1 to len(a)
b *= a[i]
next
return pow(b, (1/len(a)))
func harmonicMean a
b = list(len(a))
for nr = 1 to len(a)
b[nr] = 1/a[nr]
next
return len(a) / summary(b)
func summary s
sum = 0
for n = 1 to len(s)
sum += s[n]
next
return sum
Output:
arithmetic mean = 5.50000000 geometric mean = 4.52872869 harmonic mean = 3.41417152
RPL
These words can be used either on vectors or lists.
≪ → array op ≪ array 1 GET 2 array SIZE IF DUP2 > THEN DROP2 ELSE FOR j array GET op EVAL NEXT END ≫ ≫ 'REDUCE' STO ≪ DUP ≪ + ≫ REDUCE SWAP SIZE / ≫ 'AMEAN' STO ≪ DUP ≪ * ≫ REDUCE SWAP SIZE INV ^ ≫ 'GMEAN' STO ≪ SIZE LAST ≪ INV + ≫ REDUCE / ≫ 'HMEAN' STO
{ 1 2 3 4 5 6 7 8 9 0 } AMEAN { 1 2 3 4 5 6 7 8 9 0 } GMEAN [ 1 2 3 4 5 6 7 8 9 0 ] HMEAN
- Output:
3: 5.5 2: 4.52872868812 1: 3.41417152147
Ruby
class Array
def arithmetic_mean
inject(0.0, :+) / length
end
def geometric_mean
inject(:*) ** (1.0 / length)
end
def harmonic_mean
length / inject(0.0) {|s, m| s + 1.0/m}
end
end
class Range
def method_missing(m, *args)
case m
when /_mean$/ then to_a.send(m)
else super
end
end
end
p a = (1..10).arithmetic_mean
p g = (1..10).geometric_mean
p h = (1..10).harmonic_mean
# is h < g < a ??
p g.between?(h, a)
- Output:
5.5 4.528728688116765 3.414171521474055 true
Run BASIC
bXsum = 1
for i = 1 to 10
sum = sum + i ' sum of 1 -> 10
bXsum = bXsum * i ' sum i * i
sum1i = sum1i + (1/i) ' sum 1/i
next
average = sum / 10
geometric = bXsum ^ (1/10)
harmonic = 10/sum1i
print "ArithmeticMean:";average
print "Geometric Mean:";geometric
print " Harmonic Mean:";harmonic
if (average >= geometric) and (geometric >= harmonic) then print "True" else print "False"
- Output:
Arithmetic Mean:5.5 Geometric Mean:4.52872869 Harmonic Mean:3.41417132 True
Rust
fn main() {
let mut sum = 0.0;
let mut prod = 1;
let mut recsum = 0.0;
for i in 1..11{
sum += i as f32;
prod *= i;
recsum += 1.0/(i as f32);
}
let avg = sum/10.0;
let gmean = (prod as f32).powf(0.1);
let hmean = 10.0/recsum;
println!("Average: {}, Geometric mean: {}, Harmonic mean: {}", avg, gmean, hmean);
assert!( ( (avg >= gmean) && (gmean >= hmean) ), "Incorrect calculation");
}
- Output:
Average: 5.5, Geometric mean:4.528729, Harmonic mean: 3.4141712
Scala
def arithmeticMean(n: Seq[Int]) = n.sum / n.size.toDouble
def geometricMean(n: Seq[Int]) = math.pow(n.foldLeft(1.0)(_*_), 1.0 / n.size.toDouble)
def harmonicMean(n: Seq[Int]) = n.size / n.map(1.0 / _).sum
var nums = 1 to 10
var a = arithmeticMean(nums)
var g = geometricMean(nums)
var h = harmonicMean(nums)
println("Arithmetic mean " + a)
println("Geometric mean " + g)
println("Harmonic mean " + h)
assert(a >= g && g >= h)
- Output:
Arithmetic mean 5.5 Geometric mean 4.528728688116765 Harmonic mean 3.414171521474055
Scheme
(define (a-mean l)
(/ (apply + l) (length l)))
(define (g-mean l)
(expt (apply * l) (/ (length l))))
(define (h-mean l)
(/ (length l) (apply + (map / l))))
(define (iota start stop)
(if (> start stop)
(list)
(cons start (iota (+ start 1) stop))))
(let* ((l (iota 1 10)) (a (a-mean l)) (g (g-mean l)) (h (h-mean l)))
(display a)
(display " >= ")
(display g)
(display " >= ")
(display h)
(newline)
(display (>= a g h))
(newline))
- Output:
11/2 >= 4.528728688116765 >= 25200/7381
#t
Seed7
$ include "seed7_05.s7i";
include "float.s7i";
const array float: numbers is [] (1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0);
const func proc: main is func
local
var float: number is 0.0;
var float: sum is 0.0;
var float: product is 1.0;
var float: reciprocalSum is 0.0;
begin
for number range numbers do
sum +:= number;
product *:= number;
reciprocalSum +:= 1.0 / number;
end for;
writeln("Arithmetic mean: " <& sum / flt(length(numbers)));
writeln("Geometric mean: " <& product ** (1.0 / flt(length(numbers))));
writeln("Harmonic mean: " <& flt(length(numbers)) / reciprocalSum);
end func;
- Output:
Arithmetic mean: 5.5 Geometric mean: 4.528728961944580078125 Harmonic mean: 3.4141712188720703125
Sidef
func A(a) { a.sum / a.len }
func G(a) { a.prod.root(a.len) }
func H(a) { a.len / a.map{1/_}.sum }
The same thing, using hyper-operators:
func A(a) { a«+» / a.len }
func G(a) { a«*» ** (1/a.len) }
func H(a) { a.len / (a«/«1 «+») }
Calling the functions:
say("A(1,...,10) = ", A(1..10));
say("G(1,...,10) = ", G(1..10));
say("H(1,...,10) = ", H(1..10));
- Output:
A(1,...,10) = 5.5 G(1,...,10) = 4.528728688116764762203309337195508793499 H(1,...,10) = 3.414171521474055006096734859775098225173
Smalltalk
This extends the class Collection, so these three methods can be called over any kind of collection, it is enough the the objects of the collection understand +, *, raisedTo, reciprocal and /.
Collection extend
[
arithmeticMean
[
^ (self fold: [:a :b| a + b ]) / (self size)
]
geometricMean
[
^ (self fold: [:a :b| a * b]) raisedTo: (self size reciprocal)
]
harmonicMean
[
^ (self size) / ((self collect: [:x|x reciprocal]) fold: [:a :b| a + b ] )
]
]
|a|
a := #(1 2 3 4 5 6 7 8 9 10).
a arithmeticMean asFloat displayNl.
a geometricMean asFloat displayNl.
a harmonicMean asFloat displayNl.
((a arithmeticMean) >= (a geometricMean)) displayNl.
((a geometricMean) >= (a harmonicMean)) displayNl.
- Output:
5.5 4.528728688116765 3.414171521474055 true true
SQL
It may not be possible to calculate a geometric mean in a query, but the other two are easy enough.
--setup
create table averages (val integer);
insert into averages values (1);
insert into averages values (2);
insert into averages values (3);
insert into averages values (4);
insert into averages values (5);
insert into averages values (6);
insert into averages values (7);
insert into averages values (8);
insert into averages values (9);
insert into averages values (10);
-- calculate means
select
1/avg(1/val) as harm,
avg(val) as arith
from
averages;
- Output:
HARM ARITH ---------- ---------- 3.41417152 5.5
Stata
The command ameans prints the arithmetic, geometric and harmonic means, together with confidence intervals.
clear all
set obs 10
gen x=_n
ameans x
Variable | Type Obs Mean [95% Conf. Interval]
-------------+---------------------------------------------------------------
x | Arithmetic 10 5.5 3.334149 7.665851
| Geometric 10 4.528729 2.680672 7.650836
| Harmonic 10 3.414172 2.035664 10.57602
-----------------------------------------------------------------------------
Swift
// Utility for easy creation of Double from any Numeric
extension Double {
init(withNum v: any Numeric) {
switch v {
case let ii as any BinaryInteger: self.init(ii)
case let ff as any BinaryFloatingPoint: self.init(ff)
default: self.init()
}
}
}
// Extension for numeric collections
extension Collection where Element: Numeric {
var arithmeticMean: Double {
self.reduce(0.0, {$0 + Double(withNum: $1)})/Double(self.count)
}
var geometricMean: Double {
pow(self.reduce(1.0, {$0 * Double(withNum: $1)}), 1.0/Double(self.count))
}
var harmonicMean: Double {
Double(self.count) / self.reduce(0.0, {$0 + 1.0/Double(withNum:$1)})
}
}
//Usage:
var c: [Int] = (1...10).map {$0}
print(c.arithmeticMean)
print(c.geometricMean)
print(c.harmonicMean)
// output:
// 5.5
// 4.528728688116765
// 3.414171521474055
Tcl
proc arithmeticMean list {
set sum 0.0
foreach value $list { set sum [expr {$sum + $value}] }
return [expr {$sum / [llength $list]}]
}
proc geometricMean list {
set product 1.0
foreach value $list { set product [expr {$product * $value}] }
return [expr {$product ** (1.0/[llength $list])}]
}
proc harmonicMean list {
set sum 0.0
foreach value $list { set sum [expr {$sum + 1.0/$value}] }
return [expr {[llength $list] / $sum}]
}
set nums {1 2 3 4 5 6 7 8 9 10}
set A10 [arithmeticMean $nums]
set G10 [geometricMean $nums]
set H10 [harmonicMean $nums]
puts "A10=$A10, G10=$G10, H10=$H10"
if {$A10 >= $G10} { puts "A10 >= G10" }
if {$G10 >= $H10} { puts "G10 >= H10" }
- Output:
A10=5.5, G10=4.528728688116765, H10=3.414171521474055 A10 >= G10 G10 >= H10
Ursala
#import std
#import flo
data = ari10(1.,10.) # arithmetic progression, length 10 with endpoints 1 and 10
a = mean data
g = exp mean ln* data
h = div/1. mean div/*1. data
#cast %eLbX
main = ^(~&,ordered not fleq) <a,g,h>
- Output:
( <5.500000e+00,4.528729e+00,3.414172e+00>, true)
Vala
Most valac setups will need "-X -lm" added to the compile command to include the C math library.
double arithmetic(int[] list){
double mean;
double sum = 0;
foreach(int number in list){
sum += number;
} // foreach
mean = sum / list.length;
return mean;
} // end arithmetic mean
double geometric(int[] list){
double mean;
double product = 1;
foreach(int number in list){
product *= number;
} // foreach
mean = Math.pow(product, (1 / (double) list.length));
return mean;
} // end geometric mean
double harmonic(int[] list){
double mean;
double sum_inverse = 0;
foreach(int number in list){
sum_inverse += (1 / (double) number);
} // foreach
mean = (double) list.length / sum_inverse;
return mean;
} // end harmonic mean
public static void main(){
int[] list = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
double arithmetic_mean = arithmetic(list);
double geometric_mean = geometric(list);
double harmonic_mean = harmonic(list);
// should be 5.5
stdout.printf("Arithmetic mean: %s\n", arithmetic_mean.to_string());
// should be 4.528728688116765
stdout.printf("Geometric mean: %s\n", geometric_mean.to_string());
// should be 4.528728688116765
stdout.printf("Harmonic mean: %s\n", harmonic_mean.to_string());
}
- Output:
Arithmetic mean: 5.5 Geometric mean: 4.5287286881167654 Harmonic mean: 3.4141715214740551
VBA
Uses Excel VBA.
Private Function arithmetic_mean(s() As Variant) As Double
arithmetic_mean = WorksheetFunction.Average(s)
End Function
Private Function geometric_mean(s() As Variant) As Double
geometric_mean = WorksheetFunction.GeoMean(s)
End Function
Private Function harmonic_mean(s() As Variant) As Double
harmonic_mean = WorksheetFunction.HarMean(s)
End Function
Public Sub pythagorean_means()
Dim s() As Variant
s = [{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}]
Debug.Print "A ="; arithmetic_mean(s)
Debug.Print "G ="; geometric_mean(s)
Debug.Print "H ="; harmonic_mean(s)
End Sub
- Output:
A = 5,5 G = 4,52872868811677 H = 3,41417152147406
VBScript
Function arithmetic_mean(arr)
sum = 0
For i = 0 To UBound(arr)
sum = sum + arr(i)
Next
arithmetic_mean = sum / (UBound(arr)+1)
End Function
Function geometric_mean(arr)
product = 1
For i = 0 To UBound(arr)
product = product * arr(i)
Next
geometric_mean = product ^ (1/(UBound(arr)+1))
End Function
Function harmonic_mean(arr)
sum = 0
For i = 0 To UBound(arr)
sum = sum + (1/arr(i))
Next
harmonic_mean = (UBound(arr)+1) / sum
End Function
WScript.StdOut.WriteLine arithmetic_mean(Array(1,2,3,4,5,6,7,8,9,10))
WScript.StdOut.WriteLine geometric_mean(Array(1,2,3,4,5,6,7,8,9,10))
WScript.StdOut.WriteLine harmonic_mean(Array(1,2,3,4,5,6,7,8,9,10))
- Output:
5.5 4.52872868811677 3.41417152147406
Visual Basic .NET
Imports System.Runtime.CompilerServices
Module Module1
<Extension()>
Function Gmean(n As IEnumerable(Of Double)) As Double
Return Math.Pow(n.Aggregate(Function(s, i) s * i), 1.0 / n.Count())
End Function
<Extension()>
Function Hmean(n As IEnumerable(Of Double)) As Double
Return n.Count() / n.Sum(Function(i) 1.0 / i)
End Function
Sub Main()
Dim nums = From n In Enumerable.Range(1, 10) Select CDbl(n)
Dim a = nums.Average()
Dim g = nums.Gmean()
Dim h = nums.Hmean()
Console.WriteLine("Arithmetic mean {0}", a)
Console.WriteLine(" Geometric mean {0}", g)
Console.WriteLine(" Harmonic mean {0}", h)
Debug.Assert(a >= g AndAlso g >= h)
End Sub
End Module
- Output:
Arithmetic mean 5.5 Geometric mean 4.52872868811677 Harmonic mean 3.41417152147406
V (Vlang)
Updated for Vlang version 0.2.2
import math
fn main() {
mut sum := 0.0
mut prod :=1.0
mut recip_sum := 0.0
n := 10
for val in 1..(n + 1) {
sum += val
prod *= val
recip_sum = recip_sum + ( 1.0 / val )
}
a := sum / n
g := math.pow( prod, ( 1.0 / f32(n) ) )
h := n / recip_sum
result := 'Arithmetic Mean: ${a:3.2f} \nGeometric Mean: ${g:3.2f}\nHarmonic Mean: ${h:3.2f}'
println( result )
compare := if a >= g && g >= h { "Yes" } else { "Nope" }
println('Is A >= G >= H? $compare')
}
- Output:
Arithmetic Mean: 5.50 Geometric Mean: 4.53 Harmonic Mean: 3.41 Is A >= G >= H? Yes
Wren
var rng = 1..10
var count = rng.count
var A = rng.reduce { |acc, x| acc + x }/count
var G = rng.reduce { |prod, x| prod * x}.pow(1/count)
var H = rng.reduce { |acc, x| acc + 1/x}.pow(-1) * count
System.print("For the numbers %(rng):")
System.print(" Arithmetic mean = %(A)")
System.print(" Geometric mean = %(G)")
System.print(" Harmonic mean = %(H)")
System.print(" A >= G >= H = %(A >= G && G >= H)")
- Output:
For the numbers 1..10: Arithmetic mean = 5.5 Geometric mean = 4.5287286881168 Harmonic mean = 3.4141715214741 A >= G >= H = true
XPL0
include c:\cxpl\codes;
func real Power(X, Y); \X raised to the Y power
real X, Y; \ (from StdLib.xpl)
return Exp(Y * Ln(X));
int N, Order;
real R, A, A1, G, G1, H, H1;
[A1:= 0.0; G1:= 1.0; H1:= 0.0;
Order:= true;
for N:= 1 to 10 do
[R:= float(N); \convert integer N to real R
A1:= A1 + R;
A:= A1/R; \arithmetic mean
G1:= G1 * R;
G:= Power(G1, 1.0/R); \geometric mean (Nth root of G1)
if G>A then Order:= false;
H1:= H1 + 1.0/R;
H:= R/H1; \harmonic mean
if H>G then Order:= false;
];
RlOut(0, A); CrLf(0);
RlOut(0, G); CrLf(0);
RlOut(0, H); CrLf(0);
if not Order then Text(0, "NOT ");
Text(0, "ALWAYS DECREASING ORDER
");
]
- Output:
5.50000 4.52873 3.41417 ALWAYS DECREASING ORDER
zkl
ns:=T(1,2,3,4,5,6,7,8,9,10);
ns.sum(0.0)/ns.len(); // Arithmetic mean
ns.reduce('*,1.0).pow(1.0/ns.len()); // Geometric mean
ns.len().toFloat() / ns.reduce(fcn(p,n){ p + 1.0/n },0.0); // Harmonic mean
- Output:
5.5 4.52873 3.41417
- Geometry
- Programming Tasks
- Solutions by Programming Task
- 11l
- Action!
- Action! Tool Kit
- ActionScript
- Ada
- ALGOL 68
- ALGOL W
- Amazing Hopper
- APL
- AppleScript
- Arturo
- AutoHotkey
- AWK
- BBC BASIC
- BQN
- C
- C sharp
- C++
- Clojure
- CoffeeScript
- Common Lisp
- Craft Basic
- D
- Delphi
- E
- EasyLang
- EchoLisp
- Elixir
- Erlang
- ERRE
- Euler Math Toolbox
- Euphoria
- Excel
- F Sharp
- Factor
- Fantom
- Forth
- Fortran
- FreeBASIC
- FunL
- Futhark
- FutureBasic
- GAP
- Go
- Groovy
- Haskell
- HicEst
- Icon
- Unicon
- Icon Programming Library
- IS-BASIC
- J
- Java
- JavaScript
- Jq
- Julia
- K
- Kotlin
- Lasso
- Liberty BASIC
- Logo
- Lua
- M2000 Interpreter
- Maple
- Mathematica
- Wolfram Language
- MATLAB
- Maxima
- Min
- Modula-2
- MUMPS
- NetRexx
- Nim
- Oberon-2
- Objeck
- OCaml
- Octave
- Oforth
- OoRexx
- Oz
- PARI/GP
- Pascal
- Perl
- Phix
- PHP
- PicoLisp
- PL/I
- PostScript
- Initlib
- PowerShell
- Processing
- PureBasic
- Python
- Quackery
- R
- Racket
- Raku
- REXX
- Ring
- RPL
- Ruby
- Run BASIC
- Rust
- Scala
- Scheme
- Seed7
- Sidef
- Smalltalk
- SQL
- Stata
- Swift
- Tcl
- Ursala
- Vala
- VBA
- VBScript
- Visual Basic .NET
- V (Vlang)
- Wren
- XPL0
- Zkl