Averages/Pythagorean means: Difference between revisions
Underscore (talk | contribs) (Edited the task description, dropping the requirement for an explicit comparison. Removed the comparison from each example. Changed the Python implementation to a standalone script.) |
|||
Line 1: | Line 1: | ||
{{task}} |
{{task}} |
||
Compute all three of the [[wp:Pythagorean means|Pythagorean means]] of the |
Compute all three of the [[wp:Pythagorean means|Pythagorean means]] of the list of the integers 1 through 10. |
||
* The most common of the three means, the [[Averages/Arithmetic mean|arithmetic mean]], is the sum of the list divided by its length: |
|||
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 numbers. |
|||
⚫ | |||
* |
* 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 n'th root of the multiplication of all the numbers: |
|||
: <math> G(x_1, \ldots, x_n) = \sqrt[n]{x_1 \cdots x_n} </math> |
: <math> G(x_1, \ldots, x_n) = \sqrt[n]{x_1 \cdots x_n} </math> |
||
* The [[wp:Harmonic mean| |
* 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> |
: <math> H(x_1, \ldots, x_n) = \frac{n}{\frac{1}{x_1} + \cdots + \frac{1}{x_n}} </math> |
||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
{{incorrect|Haskell|Need to show the relationship between A,G and H}} |
|||
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>. |
||
Line 33: | Line 30: | ||
'''Example Usage:''' |
'''Example Usage:''' |
||
<lang j> (amean , gmean , hmean) >: i. 10 |
<lang j> (amean , gmean , hmean) >: i. 10 |
||
5.5 4.528729 3.414172 |
5.5 4.528729 3.414172</lang> |
||
assert 2 >:/\ (amean , gmean , hmean) >: i. 10 NB. check amean >= gmean and gmean >= hmean</lang> |
|||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
Line 47: | Line 43: | ||
--harmonic |
--harmonic |
||
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)</lang> |
||
assert(a >= g and g >= h)</lang> |
|||
=={{header|PL/I}}== |
=={{header|PL/I}}== |
||
⚫ | |||
{{incorrect|PL/I|Need to show the relationship between A,G and H}} |
|||
<lang PL/I> |
|||
⚫ | |||
n = hbound(A,1); |
n = hbound(A,1); |
||
Line 67: | Line 60: | ||
put skip data (Average); |
put skip data (Average); |
||
put skip data (Geometric); |
put skip data (Geometric); |
||
put skip data (Harmonic); |
put skip data (Harmonic);</lang> |
||
</lang> |
|||
=={{header|Python}}== |
=={{header|Python}}== |
||
{{works with|Python 3}} |
|||
<lang Python>>>> from operator import mul |
|||
> |
<lang Python>from operator import mul |
||
from functools import reduce |
|||
⚫ | |||
⚫ | |||
return sum(num)/len(num) |
return sum(num)/len(num) |
||
def gmean(num): |
|||
return reduce(mul, num, 1)**(1/len(num)) |
return reduce(mul, num, 1)**(1/len(num)) |
||
def hmean(num): |
|||
return len(num)/sum(1/n for n in num) |
return len(num)/sum(1/n for n in num) |
||
numbers = range(1,11) # 1..10 |
|||
print(amean(numbers), gmean(numbers), hmean(numbers))</lang> |
|||
(5.5, 4.528728688116765, 3.414171521474055) |
|||
Output: |
|||
>>> assert( amean(numbers) >= gmean(numbers) >= hmean(numbers) ) |
|||
>>> |
|||
<pre>5.5 4.52872868812 3.41417152147</pre> |
|||
</lang> |
|||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
Line 110: | Line 104: | ||
set G10 [geometricMean $nums] |
set G10 [geometricMean $nums] |
||
set H10 [harmonicMean $nums] |
set H10 [harmonicMean $nums] |
||
puts "A10=$A10, G10=$G10, H10=$H10" |
puts "A10=$A10, G10=$G10, H10=$H10"</lang> |
||
if {$A10 >= $G10} { puts "A10 >= G10" } |
|||
if {$G10 >= $H10} { puts "G10 >= H10" }</lang> |
|||
Output: |
Output: |
||
<pre> |
<pre> |
||
A10=5.5, G10=4.528728688116765, H10=3.414171521474055 |
A10=5.5, G10=4.528728688116765, H10=3.414171521474055 |
||
A10 >= G10 |
|||
G10 >= H10 |
|||
</pre> |
</pre> |
Revision as of 23:32, 21 February 2010
You are encouraged to solve this task according to the task description, using any language you may know.
Compute all three of the Pythagorean means of the list of the integers 1 through 10.
- 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:
Haskell
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
.
<lang haskell>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 = zipWithM_ f "agh" (map (flip mean [1 .. 10]) [1, 0, -1])
where f c n = putStrLn $ c : ": " ++ show n</lang>
J
Solution: <lang j>amean=: +/ % # gmean=: # %: */ hmean=: amean&.:%</lang>
Example Usage: <lang j> (amean , gmean , hmean) >: i. 10 5.5 4.528729 3.414172</lang>
Lua
<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 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)</lang>
PL/I
<lang PL/I>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);</lang>
Python
<lang 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 print(amean(numbers), gmean(numbers), hmean(numbers))</lang>
Output:
5.5 4.52872868812 3.41417152147
Tcl
<lang 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"</lang> Output:
A10=5.5, G10=4.528728688116765, H10=3.414171521474055