Averages/Pythagorean means: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
Thundergnat (talk | contribs) m (Automated syntax highlighting fixup (second round - minor fixes)) |
||
Line 1: | Line 1: | ||
⚫ | |||
{{task}} |
{{task}} |
||
;Task |
|||
{{task heading}} |
|||
Compute all three of the [[wp:Pythagorean means|Pythagorean means]] of the set of integers <big>1</big> through <big>10</big> (inclusive). |
Compute all three of the [[wp:Pythagorean means|Pythagorean means]] of the set of integers <big>1</big> through <big>10</big> (inclusive). |
||
Line 25: | Line 26: | ||
=={{header|11l}}== |
=={{header|11l}}== |
||
<syntaxhighlight lang=11l>F amean(num) |
<syntaxhighlight lang="11l">F amean(num) |
||
R sum(num)/Float(num.len) |
R sum(num)/Float(num.len) |
||
Line 48: | Line 49: | ||
=={{header|Action!}}== |
=={{header|Action!}}== |
||
{{libheader|Action! Tool Kit}} |
{{libheader|Action! Tool Kit}} |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="action!">INCLUDE "D2:REAL.ACT" ;from the Action! Tool Kit |
||
PROC InverseI(INT a,result) |
PROC InverseI(INT a,result) |
||
Line 126: | Line 127: | ||
=={{header|ActionScript}}== |
=={{header|ActionScript}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="actionscript">function arithmeticMean(v:Vector.<Number>):Number |
||
{ |
{ |
||
var sum:Number = 0; |
var sum:Number = 0; |
||
Line 155: | Line 156: | ||
pythagorean_means.ads: |
pythagorean_means.ads: |
||
<syntaxhighlight lang= |
<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; |
||
Line 163: | Line 164: | ||
pythagorean_means.adb: |
pythagorean_means.adb: |
||
<syntaxhighlight lang= |
<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 198: | Line 199: | ||
example main.adb: |
example main.adb: |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="ada">with Ada.Text_IO; |
||
with Pythagorean_Means; |
with Pythagorean_Means; |
||
procedure Main is |
procedure Main is |
||
Line 217: | 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: ( |
<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 257: | Line 258: | ||
=={{header|ALGOL W}}== |
=={{header|ALGOL W}}== |
||
<syntaxhighlight lang=algolw>begin |
<syntaxhighlight lang="algolw">begin |
||
% returns the arithmetic mean of the elements of n from lo to hi % |
% returns the arithmetic mean of the elements of n from lo to hi % |
||
real procedure arithmeticMean ( real array n ( * ); integer value lo, hi ) ; |
real procedure arithmeticMean ( real array n ( * ); integer value lo, hi ) ; |
||
Line 302: | Line 303: | ||
=={{header|Amazing Hopper}}== |
=={{header|Amazing Hopper}}== |
||
Think about "talk" programming... |
Think about "talk" programming... |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="amazing hopper"> |
||
#include <hopper.h> |
#include <hopper.h> |
||
Line 398: | Line 399: | ||
=={{header|APL}}== |
=={{header|APL}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="apl"> |
||
arithmetic←{(+/⍵)÷⍴⍵} |
arithmetic←{(+/⍵)÷⍴⍵} |
||
geometric←{(×/⍵)*÷⍴⍵} |
geometric←{(×/⍵)*÷⍴⍵} |
||
Line 415: | Line 416: | ||
=={{header|AppleScript}}== |
=={{header|AppleScript}}== |
||
{{trans|JavaScript}} |
{{trans|JavaScript}} |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="applescript">-- arithmetic_mean :: [Number] -> Number |
||
on arithmetic_mean(xs) |
on arithmetic_mean(xs) |
||
Line 517: | Line 518: | ||
end mReturn</syntaxhighlight> |
end mReturn</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="applescript">{values:{arithmetic:5.5, geometric:4.528728688117, harmonic:3.414171521474}, |
||
inequalities:{|A >= G|:true}, |G >= H|:true}</syntaxhighlight> |
inequalities:{|A >= G|:true}, |G >= H|:true}</syntaxhighlight> |
||
=={{header|Arturo}}== |
=={{header|Arturo}}== |
||
<syntaxhighlight lang=rebol>arithmeticMean: function [arr]-> |
<syntaxhighlight lang="rebol">arithmeticMean: function [arr]-> |
||
average arr |
average arr |
||
Line 542: | Line 543: | ||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
<syntaxhighlight lang=autohotkey>A := ArithmeticMean(1, 10) |
<syntaxhighlight lang="autohotkey">A := ArithmeticMean(1, 10) |
||
G := GeometricMean(1, 10) |
G := GeometricMean(1, 10) |
||
H := HarmonicMean(1, 10) |
H := HarmonicMean(1, 10) |
||
Line 592: | Line 593: | ||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
<syntaxhighlight lang=awk>#!/usr/bin/awk -f |
<syntaxhighlight lang="awk">#!/usr/bin/awk -f |
||
{ |
{ |
||
x = $1; # value of 1st column |
x = $1; # value of 1st column |
||
Line 609: | Line 610: | ||
=={{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) |
<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 639: | Line 640: | ||
=={{header|C}}== |
=={{header|C}}== |
||
<syntaxhighlight lang=c>#include <stdio.h> |
<syntaxhighlight lang="c">#include <stdio.h> |
||
#include <stdlib.h> // atoi() |
#include <stdlib.h> // atoi() |
||
#include <math.h> // pow() |
#include <math.h> // pow() |
||
Line 668: | Line 669: | ||
{{works with|C sharp|C#|3}} |
{{works with|C sharp|C#|3}} |
||
<syntaxhighlight lang=csharp>using System; |
<syntaxhighlight lang="csharp">using System; |
||
using System.Collections.Generic; |
using System.Collections.Generic; |
||
using System.Diagnostics; |
using System.Diagnostics; |
||
Line 709: | Line 710: | ||
=={{header|C++}}== |
=={{header|C++}}== |
||
<syntaxhighlight lang=cpp>#include <vector> |
<syntaxhighlight lang="cpp">#include <vector> |
||
#include <iostream> |
#include <iostream> |
||
#include <numeric> |
#include <numeric> |
||
Line 738: | Line 739: | ||
=={{header|Clojure}}== |
=={{header|Clojure}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="clojure">(use '[clojure.contrib.math :only (expt)]) |
||
(defn a-mean [coll] |
(defn a-mean [coll] |
||
Line 755: | Line 756: | ||
=={{header|CoffeeScript}}== |
=={{header|CoffeeScript}}== |
||
<syntaxhighlight lang=coffeescript>a = [ 1..10 ] |
<syntaxhighlight lang="coffeescript">a = [ 1..10 ] |
||
arithmetic_mean = (a) -> a.reduce(((s, x) -> s + x), 0) / a.length |
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)) |
geometic_mean = (a) -> Math.pow(a.reduce(((s, x) -> s * x), 1), (1 / a.length)) |
||
Line 771: | Line 772: | ||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |
||
<syntaxhighlight lang=lisp>(defun generic-mean (nums reduce-op final-op) |
<syntaxhighlight 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 797: | Line 798: | ||
=={{header|D}}== |
=={{header|D}}== |
||
The output for the harmonic mean is wrong. |
The output for the harmonic mean is wrong. |
||
<syntaxhighlight lang=d>import std.stdio, std.algorithm, std.range, std.functional; |
<syntaxhighlight lang="d">import std.stdio, std.algorithm, std.range, std.functional; |
||
auto aMean(T)(T data) pure nothrow @nogc { |
auto aMean(T)(T data) pure nothrow @nogc { |
||
Line 820: | Line 821: | ||
=={{header|Delphi}}== |
=={{header|Delphi}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="delphi">program AveragesPythagoreanMeans; |
||
{$APPTYPE CONSOLE} |
{$APPTYPE CONSOLE} |
||
Line 875: | Line 876: | ||
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) { |
<syntaxhighlight lang="e">def makeMean(base, include, finish) { |
||
return def mean(numbers) { |
return def mean(numbers) { |
||
var count := 0 |
var count := 0 |
||
Line 891: | Line 892: | ||
def H := makeMean(0, fn b,x { b+1/x }, fn acc,n { n / acc })</syntaxhighlight> |
def H := makeMean(0, fn b,x { b+1/x }, fn acc,n { n / acc })</syntaxhighlight> |
||
<syntaxhighlight lang=e>? A(1..10) |
<syntaxhighlight lang="e">? A(1..10) |
||
# value: 5.5 |
# value: 5.5 |
||
Line 901: | Line 902: | ||
=={{header|EchoLisp}}== |
=={{header|EchoLisp}}== |
||
<syntaxhighlight lang=scheme> |
<syntaxhighlight lang="scheme"> |
||
(define (A xs) (// (for/sum ((x xs)) x) (length xs))) |
(define (A xs) (// (for/sum ((x xs)) x) (length xs))) |
||
Line 914: | Line 915: | ||
=={{header|Elixir}}== |
=={{header|Elixir}}== |
||
<syntaxhighlight lang=elixir>defmodule Means do |
<syntaxhighlight lang="elixir">defmodule Means do |
||
def arithmetic(list) do |
def arithmetic(list) do |
||
Enum.sum(list) / length(list) |
Enum.sum(list) / length(list) |
||
Line 941: | Line 942: | ||
=={{header|Erlang}}== |
=={{header|Erlang}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="erlang">%% Author: Abhay Jain <abhay_1303@yahoo.co.in> |
||
-module(mean_calculator). |
-module(mean_calculator). |
||
Line 983: | Line 984: | ||
=={{header|ERRE}}== |
=={{header|ERRE}}== |
||
<lang> |
<syntaxhighlight lang="text"> |
||
PROGRAM MEANS |
PROGRAM MEANS |
||
Line 1,031: | Line 1,032: | ||
=={{header|Euler Math Toolbox}}== |
=={{header|Euler Math Toolbox}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight 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 1,043: | Line 1,044: | ||
Alternatively, e.g., |
Alternatively, e.g., |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="euler math toolbox"> |
||
>function G(x) := prod(x)^(1/length(x)) |
>function G(x) := prod(x)^(1/length(x)) |
||
</syntaxhighlight> |
</syntaxhighlight> |
||
=={{header|Euphoria}}== |
=={{header|Euphoria}}== |
||
<syntaxhighlight lang=euphoria>function arithmetic_mean(sequence s) |
<syntaxhighlight lang="euphoria">function arithmetic_mean(sequence s) |
||
atom sum |
atom sum |
||
if length(s) = 0 then |
if length(s) = 0 then |
||
Line 1,112: | Line 1,113: | ||
Use the functions : AVERAGE, GEOMEAN and HARMEAN |
Use the functions : AVERAGE, GEOMEAN and HARMEAN |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="excel"> |
||
=AVERAGE(1;2;3;4;5;6;7;8;9;10) |
=AVERAGE(1;2;3;4;5;6;7;8;9;10) |
||
=GEOMEAN(1;2;3;4;5;6;7;8;9;10) |
=GEOMEAN(1;2;3;4;5;6;7;8;9;10) |
||
Line 1,125: | Line 1,126: | ||
=={{header|F_Sharp|F#}}== |
=={{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] |
<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) = |
let arithmeticMean (x : float list) = |
||
Line 1,145: | Line 1,146: | ||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
<syntaxhighlight lang=factor>: a-mean ( seq -- mean ) |
<syntaxhighlight lang="factor">: a-mean ( seq -- mean ) |
||
[ sum ] [ length ] bi / ; |
[ sum ] [ length ] bi / ; |
||
Line 1,160: | Line 1,161: | ||
=={{header|Fantom}}== |
=={{header|Fantom}}== |
||
<syntaxhighlight lang=fantom> |
<syntaxhighlight lang="fantom"> |
||
class Main |
class Main |
||
{ |
{ |
||
Line 1,205: | Line 1,206: | ||
=={{header|Forth}}== |
=={{header|Forth}}== |
||
<syntaxhighlight lang=forth>: famean ( faddr n -- f ) |
<syntaxhighlight lang="forth">: famean ( faddr n -- f ) |
||
0e |
0e |
||
tuck floats bounds do |
tuck floats bounds do |
||
Line 1,235: | Line 1,236: | ||
=={{header|Fortran}}== |
=={{header|Fortran}}== |
||
{{works with|Fortran|90}} |
{{works with|Fortran|90}} |
||
<syntaxhighlight lang=fortran>program Mean |
<syntaxhighlight lang="fortran">program Mean |
||
real :: a(10) = (/ (i, i=1,10) /) |
real :: a(10) = (/ (i, i=1,10) /) |
||
Line 1,253: | Line 1,254: | ||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
<syntaxhighlight lang=freebasic> |
<syntaxhighlight lang="freebasic"> |
||
' FB 1.05.0 Win64 |
' FB 1.05.0 Win64 |
||
Line 1,304: | Line 1,305: | ||
=={{header|FunL}}== |
=={{header|FunL}}== |
||
<syntaxhighlight lang=funl>import lists.zip |
<syntaxhighlight lang="funl">import lists.zip |
||
def |
def |
||
Line 1,332: | Line 1,333: | ||
=={{header|Futhark}}== |
=={{header|Futhark}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="futhark"> |
||
fun arithmetic_mean(as: [n]f64): f64 = |
fun arithmetic_mean(as: [n]f64): f64 = |
||
reduce (+) 0.0 (map (/f64(n)) as) |
reduce (+) 0.0 (map (/f64(n)) as) |
||
Line 1,349: | Line 1,350: | ||
=={{header|GAP}}== |
=={{header|GAP}}== |
||
<syntaxhighlight lang=gap># The first two work with rationals or with floats |
<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 1,369: | Line 1,370: | ||
=={{header|Go}}== |
=={{header|Go}}== |
||
<syntaxhighlight lang=go>package main |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 1,395: | Line 1,396: | ||
=={{header|Groovy}}== |
=={{header|Groovy}}== |
||
Solution: |
Solution: |
||
<syntaxhighlight lang=groovy>def arithMean = { list -> |
<syntaxhighlight lang="groovy">def arithMean = { list -> |
||
list == null \ |
list == null \ |
||
? null \ |
? null \ |
||
Line 1,420: | Line 1,421: | ||
Test: |
Test: |
||
<syntaxhighlight lang=groovy>def list = 1..10 |
<syntaxhighlight lang="groovy">def list = 1..10 |
||
def A = arithMean(list) |
def A = arithMean(list) |
||
def G = geomMean(list) |
def G = geomMean(list) |
||
Line 1,443: | Line 1,444: | ||
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) |
<syntaxhighlight lang="haskell">import Data.List (genericLength) |
||
import Control.Monad (zipWithM_) |
import Control.Monad (zipWithM_) |
||
Line 1,458: | Line 1,459: | ||
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. |
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) |
<syntaxhighlight lang="haskell">import Data.List (genericLength) |
||
-- ARITHMETIC, GEOMETRIC AND HARMONIC MEANS --------------- |
-- ARITHMETIC, GEOMETRIC AND HARMONIC MEANS --------------- |
||
Line 1,485: | Line 1,486: | ||
=={{header|HicEst}}== |
=={{header|HicEst}}== |
||
<syntaxhighlight lang= |
<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 1,498: | Line 1,499: | ||
=={{header|Icon}} and {{header|Unicon}}== |
=={{header|Icon}} and {{header|Unicon}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="icon">link numbers # for a/g/h means |
||
procedure main() |
procedure main() |
||
Line 1,513: | Line 1,514: | ||
{{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= |
<syntaxhighlight lang="icon">procedure amean(L[]) #: arithmetic mean |
||
local m |
local m |
||
if *L = 0 then fail |
if *L = 0 then fail |
||
Line 1,553: | Line 1,554: | ||
=={{header|IS-BASIC}}== |
=={{header|IS-BASIC}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="is-basic">100 PROGRAM "Averages.bas" |
||
110 NUMERIC ARR(1 TO 10) |
110 NUMERIC ARR(1 TO 10) |
||
120 FOR I=LBOUND(ARR) TO UBOUND(ARR) |
120 FOR I=LBOUND(ARR) TO UBOUND(ARR) |
||
Line 1,585: | Line 1,586: | ||
=={{header|J}}== |
=={{header|J}}== |
||
'''Solution:''' |
'''Solution:''' |
||
<syntaxhighlight lang=j>amean=: +/ % # |
<syntaxhighlight lang="j">amean=: +/ % # |
||
gmean=: # %: */ |
gmean=: # %: */ |
||
hmean=: amean&.:%</syntaxhighlight> |
hmean=: amean&.:%</syntaxhighlight> |
||
'''Example Usage:''' |
'''Example Usage:''' |
||
<syntaxhighlight lang=j> (amean , gmean , hmean) >: i. 10 |
<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</syntaxhighlight> |
assert 2 >:/\ (amean , gmean , hmean) >: i. 10 NB. check amean >= gmean and gmean >= hmean</syntaxhighlight> |
||
Line 1,596: | Line 1,597: | ||
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> |
<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.) |
(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; |
<syntaxhighlight lang="java">import java.util.Arrays; |
||
import java.util.List; |
import java.util.List; |
||
Line 1,648: | Line 1,649: | ||
{{works with|Java|1.8}} |
{{works with|Java|1.8}} |
||
We can rewrite the 3 methods using the new JAVA Stream API: |
We can rewrite the 3 methods using the new JAVA Stream API: |
||
<syntaxhighlight lang=java> |
<syntaxhighlight lang="java"> |
||
public static double arithmAverage(double array[]){ |
public static double arithmAverage(double array[]){ |
||
if (array == null ||array.length == 0) { |
if (array == null ||array.length == 0) { |
||
Line 1,690: | Line 1,691: | ||
===ES5=== |
===ES5=== |
||
<syntaxhighlight lang=javascript>(function () { |
<syntaxhighlight lang="javascript">(function () { |
||
'use strict'; |
'use strict'; |
||
Line 1,753: | Line 1,754: | ||
{{Out}} |
{{Out}} |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="javascript">{ |
||
"values": { |
"values": { |
||
"Arithmetic": 5.5, |
"Arithmetic": 5.5, |
||
Line 1,763: | Line 1,764: | ||
===ES6=== |
===ES6=== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="javascript">(() => { |
||
// arithmeticMean :: [Number] -> Number |
// arithmeticMean :: [Number] -> Number |
||
Line 1,827: | Line 1,828: | ||
})();</syntaxhighlight> |
})();</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="javascript">{ |
||
"values": { |
"values": { |
||
"Arithmetic": 5.5, |
"Arithmetic": 5.5, |
||
Line 1,837: | Line 1,838: | ||
=={{header|jq}}== |
=={{header|jq}}== |
||
<syntaxhighlight lang=jq>def amean: add/length; |
<syntaxhighlight lang="jq">def amean: add/length; |
||
def logProduct: map(log) | add; |
def logProduct: map(log) | add; |
||
Line 1,859: | Line 1,860: | ||
Julia has a `mean` function to compute the arithmetic mean of a collections |
Julia has a `mean` function to compute the arithmetic mean of a collections |
||
of numbers. We can redefine it as follows. |
of numbers. We can redefine it as follows. |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="julia">amean(A) = sum(A)/length(A) |
||
gmean(A) = prod(A)^(1/length(A)) |
gmean(A) = prod(A)^(1/length(A)) |
||
Line 1,874: | Line 1,875: | ||
=={{header|K}}== |
=={{header|K}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="k"> |
||
am:{(+/x)%#x} |
am:{(+/x)%#x} |
||
gm:{(*/x)^(%#x)} |
gm:{(*/x)^(%#x)} |
||
Line 1,884: | Line 1,885: | ||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
<syntaxhighlight lang=kotlin>import kotlin.math.round |
<syntaxhighlight lang="kotlin">import kotlin.math.round |
||
import kotlin.math.pow |
import kotlin.math.pow |
||
Line 1,912: | Line 1,913: | ||
=={{header|Lasso}}== |
=={{header|Lasso}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="lasso">define arithmetic_mean(a::staticarray)::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) |
||
Line 1,937: | Line 1,938: | ||
=={{header|Liberty BASIC}}== |
=={{header|Liberty BASIC}}== |
||
<syntaxhighlight lang=lb>for i = 1 to 10 |
<syntaxhighlight lang="lb">for i = 1 to 10 |
||
a = a + i |
a = a + i |
||
next |
next |
||
Line 1,965: | Line 1,966: | ||
=={{header|Logo}}== |
=={{header|Logo}}== |
||
<syntaxhighlight lang=logo>to compute_means :count |
<syntaxhighlight lang="logo">to compute_means :count |
||
local "sum |
local "sum |
||
make "sum 0 |
make "sum 0 |
||
Line 1,990: | Line 1,991: | ||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
<syntaxhighlight lang=lua>function fsum(f, a, ...) return a and f(a) + fsum(f, ...) or 0 end |
<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 2,011: | Line 2,012: | ||
<syntaxhighlight lang= |
<syntaxhighlight lang="m2000 interpreter"> |
||
Module CheckIt { |
Module CheckIt { |
||
sum=lambda -> { |
sum=lambda -> { |
||
Line 2,067: | Line 2,068: | ||
=={{header|Maple}}== |
=={{header|Maple}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="maple">x := [ seq( 1 .. 10 ) ]; |
||
Means := proc( x ) |
Means := proc( x ) |
||
uses Statistics; |
uses Statistics; |
||
Line 2,084: | Line 2,085: | ||
=={{header|Mathematica}} / {{header|Wolfram Language}}== |
=={{header|Mathematica}} / {{header|Wolfram Language}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="mathematica">Print["{Arithmetic Mean, Geometric Mean, Harmonic Mean} = ", |
||
N@Through[{Mean, GeometricMean, HarmonicMean}[Range@10]]]</syntaxhighlight> |
N@Through[{Mean, GeometricMean, HarmonicMean}[Range@10]]]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,090: | Line 2,091: | ||
=={{header|MATLAB}}== |
=={{header|MATLAB}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="matlab">function [A,G,H] = pythagoreanMeans(list) |
||
A = mean(list); |
A = mean(list); |
||
Line 2,100: | Line 2,101: | ||
A solution that works for both, Matlab and Octave, is this |
A solution that works for both, Matlab and Octave, is this |
||
<syntaxhighlight lang= |
<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 |
||
Line 2,107: | Line 2,108: | ||
Solution: |
Solution: |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="matlab">>> [A,G,H]=pythagoreanMeans((1:10)) |
||
A = |
A = |
||
Line 2,124: | Line 2,125: | ||
=={{header|Maxima}}== |
=={{header|Maxima}}== |
||
<syntaxhighlight lang=maxima>/* built-in */ |
<syntaxhighlight lang="maxima">/* built-in */ |
||
L: makelist(i, i, 1, 10)$ |
L: makelist(i, i, 1, 10)$ |
||
Line 2,132: | Line 2,133: | ||
=={{header|Modula-2}}== |
=={{header|Modula-2}}== |
||
<syntaxhighlight lang=modula2>MODULE PythagoreanMeans; |
<syntaxhighlight lang="modula2">MODULE PythagoreanMeans; |
||
FROM FormatString IMPORT FormatString; |
FROM FormatString IMPORT FormatString; |
||
FROM LongMath IMPORT power; |
FROM LongMath IMPORT power; |
||
Line 2,214: | Line 2,215: | ||
=={{header|MUMPS}}== |
=={{header|MUMPS}}== |
||
<syntaxhighlight lang= |
<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 2,239: | Line 2,240: | ||
=={{header|NetRexx}}== |
=={{header|NetRexx}}== |
||
{{trans|ooRexx}} |
{{trans|ooRexx}} |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="netrexx">/* NetRexx */ |
||
options replace format comments java crossref symbols nobinary |
options replace format comments java crossref symbols nobinary |
||
Line 2,294: | Line 2,295: | ||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
<syntaxhighlight lang=nim>import math, sequtils, sugar |
<syntaxhighlight lang="nim">import math, sequtils, sugar |
||
proc amean(num: seq[float]): float = |
proc amean(num: seq[float]): float = |
||
Line 2,324: | Line 2,325: | ||
=={{header|Oberon-2}}== |
=={{header|Oberon-2}}== |
||
Oxford Oberon-2 |
Oxford Oberon-2 |
||
<syntaxhighlight lang=oberon2> |
<syntaxhighlight lang="oberon2"> |
||
MODULE PythMean; |
MODULE PythMean; |
||
IMPORT Out, ML := MathL; |
IMPORT Out, ML := MathL; |
||
Line 2,370: | Line 2,371: | ||
=={{header|Objeck}}== |
=={{header|Objeck}}== |
||
{{trans|Java}} |
{{trans|Java}} |
||
<syntaxhighlight lang=objeck>class PythagMeans { |
<syntaxhighlight lang="objeck">class PythagMeans { |
||
function : Main(args : String[]) ~ Nil { |
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]; |
array := [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0]; |
||
Line 2,428: | Line 2,429: | ||
The three means in one function |
The three means in one function |
||
<syntaxhighlight lang=ocaml>let means v = |
<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 2,448: | Line 2,449: | ||
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 = |
<syntaxhighlight lang="ocaml">let means v = |
||
let (a, b, c) = |
let (a, b, c) = |
||
Array.fold_left |
Array.fold_left |
||
Line 2,459: | Line 2,460: | ||
=={{header|Octave}}== |
=={{header|Octave}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="octave"> |
||
A = mean(list); % arithmetic mean |
A = mean(list); % arithmetic mean |
||
G = mean(list,'g'); % geometric mean |
G = mean(list,'g'); % geometric mean |
||
Line 2,469: | Line 2,470: | ||
=={{header|Oforth}}== |
=={{header|Oforth}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="oforth">import: mapping |
||
: A ( x ) |
: A ( x ) |
||
Line 2,495: | Line 2,496: | ||
=={{header|ooRexx}}== |
=={{header|ooRexx}}== |
||
<syntaxhighlight lang= |
<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) |
||
say "Arithmetic =" arithmeticMean(a)", Geometric =" geometricMean(a)", Harmonic =" harmonicMean(a) |
say "Arithmetic =" arithmeticMean(a)", Geometric =" geometricMean(a)", Harmonic =" harmonicMean(a) |
||
Line 2,540: | Line 2,541: | ||
=={{header|Oz}}== |
=={{header|Oz}}== |
||
<syntaxhighlight lang=oz>declare |
<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 2,574: | Line 2,575: | ||
=={{header|PARI/GP}}== |
=={{header|PARI/GP}}== |
||
General implementations: |
General implementations: |
||
<syntaxhighlight lang=parigp>arithmetic(v)={ |
<syntaxhighlight lang="parigp">arithmetic(v)={ |
||
sum(i=1,#v,v[i])/#v |
sum(i=1,#v,v[i])/#v |
||
}; |
}; |
||
Line 2,588: | Line 2,589: | ||
Specific to the first ''n'' positive integers: |
Specific to the first ''n'' positive integers: |
||
<syntaxhighlight lang=parigp>arithmetic_first(n)={ |
<syntaxhighlight lang="parigp">arithmetic_first(n)={ |
||
(n+1)/2 |
(n+1)/2 |
||
}; |
}; |
||
Line 2,611: | Line 2,612: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
<syntaxhighlight lang=perl>sub A |
<syntaxhighlight lang="perl">sub A |
||
{ |
{ |
||
my $a = 0; |
my $a = 0; |
||
Line 2,639: | Line 2,640: | ||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
<!--<syntaxhighlight lang= |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
<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;">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> |
||
Line 2,671: | Line 2,672: | ||
=={{header|PHP}}== |
=={{header|PHP}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="php"><?php |
||
// Created with PHP 7.0 |
// Created with PHP 7.0 |
||
Line 2,709: | Line 2,710: | ||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |
||
<syntaxhighlight lang= |
<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)) |
(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)) |
||
Line 2,730: | Line 2,731: | ||
=={{header|PL/I}}== |
=={{header|PL/I}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="pl/i"> |
||
declare n fixed binary, |
declare n fixed binary, |
||
(Average, Geometric, Harmonic) float; |
(Average, Geometric, Harmonic) float; |
||
Line 2,761: | Line 2,762: | ||
=={{header|PostScript}}== |
=={{header|PostScript}}== |
||
<lang> |
<syntaxhighlight lang="text"> |
||
/pythamean{ |
/pythamean{ |
||
/x exch def |
/x exch def |
||
Line 2,794: | Line 2,795: | ||
{{libheader|initlib}} |
{{libheader|initlib}} |
||
<syntaxhighlight lang=postscript> |
<syntaxhighlight lang="postscript"> |
||
/numbers {[1 10] 1 range}. |
/numbers {[1 10] 1 range}. |
||
/recip {1 exch div}. |
/recip {1 exch div}. |
||
Line 2,807: | Line 2,808: | ||
=={{header|PowerShell}}== |
=={{header|PowerShell}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="powershell">$A = 0 |
||
$LogG = 0 |
$LogG = 0 |
||
$InvH = 0 |
$InvH = 0 |
||
Line 2,839: | Line 2,840: | ||
=={{header|Processing}}== |
=={{header|Processing}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="processing">void setup() { |
||
float[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; |
float[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; |
||
println("Arithmetic mean: " + arithmeticMean(numbers)); |
println("Arithmetic mean: " + arithmeticMean(numbers)); |
||
Line 2,878: | Line 2,879: | ||
=={{header|PureBasic}}== |
=={{header|PureBasic}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="purebasic">Procedure.d ArithmeticMean() |
||
For a = 1 To 10 |
For a = 1 To 10 |
||
mean + a |
mean + a |
||
Line 2,907: | Line 2,908: | ||
=={{header|Python}}== |
=={{header|Python}}== |
||
{{works with|Python|3}} |
{{works with|Python|3}} |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="python">from operator import mul |
||
from functools import reduce |
from functools import reduce |
||
Line 2,936: | Line 2,937: | ||
Uses <code>root</code> from [[Integer roots#Quackery]]. |
Uses <code>root</code> from [[Integer roots#Quackery]]. |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="quackery"> [] 10 times [ i^ 1+ join ] |
||
say "Arithmetic mean:" sp |
say "Arithmetic mean:" sp |
||
Line 2,962: | Line 2,963: | ||
=={{header|R}}== |
=={{header|R}}== |
||
Initialise x |
Initialise x |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="r"> |
||
x <- 1:10 |
x <- 1:10 |
||
</syntaxhighlight> |
</syntaxhighlight> |
||
Arithmetic mean |
Arithmetic mean |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="r"> |
||
a <- sum(x)/length(x) |
a <- sum(x)/length(x) |
||
</syntaxhighlight> |
</syntaxhighlight> |
||
or |
or |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="r"> |
||
a <- mean(x) |
a <- mean(x) |
||
</syntaxhighlight> |
</syntaxhighlight> |
||
The geometric mean |
The geometric mean |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="r"> |
||
g <- prod(x)^(1/length(x)) |
g <- prod(x)^(1/length(x)) |
||
</syntaxhighlight> |
</syntaxhighlight> |
||
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= |
<syntaxhighlight lang="r"> |
||
h <- length(x)/sum(1/x) |
h <- length(x)/sum(1/x) |
||
</syntaxhighlight> |
</syntaxhighlight> |
||
Line 2,987: | Line 2,988: | ||
Then: |
Then: |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="r"> |
||
a > g |
a > g |
||
</syntaxhighlight> |
</syntaxhighlight> |
||
Line 2,993: | Line 2,994: | ||
and |
and |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="r"> |
||
g > h |
g > h |
||
</syntaxhighlight> |
</syntaxhighlight> |
||
Line 3,002: | Line 3,003: | ||
=={{header|Racket}}== |
=={{header|Racket}}== |
||
<syntaxhighlight lang=racket> |
<syntaxhighlight lang="racket"> |
||
#lang racket |
#lang racket |
||
Line 3,035: | Line 3,036: | ||
{{works with|Rakudo|2015.12}} |
{{works with|Rakudo|2015.12}} |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="raku" line>sub A { ([+] @_) / @_ } |
||
sub G { ([*] @_) ** (1 / @_) } |
sub G { ([*] @_) ** (1 / @_) } |
||
sub H { @_ / [+] 1 X/ @_ } |
sub H { @_ / [+] 1 X/ @_ } |
||
Line 3,052: | Line 3,053: | ||
REXX doesn't have a '''POW''' function, so an '''IROOT''' ('''i'''nteger '''root''') function is included here; it includes an |
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. |
<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]. */ |
<syntaxhighlight lang="rexx">/*REXX program computes and displays the Pythagorean means [Amean, Gmean, Hmean]. */ |
||
numeric digits 20 /*use a little extra for the precision.*/ |
numeric digits 20 /*use a little extra for the precision.*/ |
||
parse arg n . /*obtain the optional argument from CL.*/ |
parse arg n . /*obtain the optional argument from CL.*/ |
||
Line 3,098: | Line 3,099: | ||
=={{header|Ring}}== |
=={{header|Ring}}== |
||
<syntaxhighlight lang=ring> |
<syntaxhighlight lang="ring"> |
||
decimals(8) |
decimals(8) |
||
array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] |
array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] |
||
Line 3,138: | Line 3,139: | ||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
{{works with|Ruby|1.9+}} |
{{works with|Ruby|1.9+}} |
||
<syntaxhighlight lang=ruby>class Array |
<syntaxhighlight lang="ruby">class Array |
||
def arithmetic_mean |
def arithmetic_mean |
||
inject(0.0, :+) / length |
inject(0.0, :+) / length |
||
Line 3,176: | Line 3,177: | ||
=={{header|Run BASIC}}== |
=={{header|Run BASIC}}== |
||
<syntaxhighlight lang=runbasic>bXsum = 1 |
<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 3,200: | Line 3,201: | ||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
<syntaxhighlight lang=rust>fn main() { |
<syntaxhighlight lang="rust">fn main() { |
||
let mut sum = 0.0; |
let mut sum = 0.0; |
||
let mut prod = 1; |
let mut prod = 1; |
||
Line 3,224: | Line 3,225: | ||
=={{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 |
<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 |
||
Line 3,245: | Line 3,246: | ||
=={{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) |
<syntaxhighlight lang="scheme">(define (a-mean l) |
||
(/ (apply + l) (length l))) |
(/ (apply + l) (length l))) |
||
Line 3,269: | Line 3,270: | ||
(newline))</syntaxhighlight> |
(newline))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<lang>11/2 >= 4.528728688116765 >= 25200/7381 |
<syntaxhighlight lang="text">11/2 >= 4.528728688116765 >= 25200/7381 |
||
#t</syntaxhighlight> |
#t</syntaxhighlight> |
||
=={{header|Seed7}}== |
=={{header|Seed7}}== |
||
<syntaxhighlight lang=seed7>$ include "seed7_05.s7i"; |
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i"; |
||
include "float.s7i"; |
include "float.s7i"; |
||
Line 3,303: | Line 3,304: | ||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
<syntaxhighlight lang=sidef>func A(a) { a.sum / a.len } |
<syntaxhighlight lang="sidef">func A(a) { a.sum / a.len } |
||
func G(a) { a.prod.root(a.len) } |
func G(a) { a.prod.root(a.len) } |
||
func H(a) { a.len / a.map{1/_}.sum }</syntaxhighlight> |
func H(a) { a.len / a.map{1/_}.sum }</syntaxhighlight> |
||
The same thing, using hyper-operators: |
The same thing, using hyper-operators: |
||
<syntaxhighlight lang=sidef>func A(a) { a«+» / a.len } |
<syntaxhighlight lang="sidef">func A(a) { a«+» / a.len } |
||
func G(a) { a«*» ** (1/a.len) } |
func G(a) { a«*» ** (1/a.len) } |
||
func H(a) { a.len / (a«/«1 «+») }</syntaxhighlight> |
func H(a) { a.len / (a«/«1 «+») }</syntaxhighlight> |
||
Calling the functions: |
Calling the functions: |
||
<syntaxhighlight lang=sidef>say("A(1,...,10) = ", A(1..10)); |
<syntaxhighlight lang="sidef">say("A(1,...,10) = ", A(1..10)); |
||
say("G(1,...,10) = ", G(1..10)); |
say("G(1,...,10) = ", G(1..10)); |
||
say("H(1,...,10) = ", H(1..10));</syntaxhighlight> |
say("H(1,...,10) = ", H(1..10));</syntaxhighlight> |
||
Line 3,326: | Line 3,327: | ||
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 |
<syntaxhighlight lang="smalltalk">Collection extend |
||
[ |
[ |
||
arithmeticMean |
arithmeticMean |
||
Line 3,363: | Line 3,364: | ||
=={{header|SQL}}== |
=={{header|SQL}}== |
||
It may not be possible to calculate a geometric mean in a query, but the other two are easy enough. |
It may not be possible to calculate a geometric mean in a query, but the other two are easy enough. |
||
<syntaxhighlight lang=sql> |
<syntaxhighlight lang="sql"> |
||
--setup |
--setup |
||
create table averages (val integer); |
create table averages (val integer); |
||
Line 3,392: | Line 3,393: | ||
=={{header|Stata}}== |
=={{header|Stata}}== |
||
The command [http://www.stata.com/help.cgi?ameans ameans] prints the arithmetic, geometric and harmonic means, together with confidence intervals. |
The command [http://www.stata.com/help.cgi?ameans ameans] prints the arithmetic, geometric and harmonic means, together with confidence intervals. |
||
<lang>clear all |
<syntaxhighlight lang="text">clear all |
||
set obs 10 |
set obs 10 |
||
gen x=_n |
gen x=_n |
||
Line 3,405: | Line 3,406: | ||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
<syntaxhighlight lang=tcl>proc arithmeticMean list { |
<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 3,438: | Line 3,439: | ||
=={{header|Ursala}}== |
=={{header|Ursala}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="ursala">#import std |
||
#import flo |
#import flo |
||
Line 3,458: | Line 3,459: | ||
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> |
<syntaxhighlight lang="vala"> |
||
double arithmetic(int[] list){ |
double arithmetic(int[] list){ |
||
double mean; |
double mean; |
||
Line 3,522: | Line 3,523: | ||
=={{header|VBA}}== |
=={{header|VBA}}== |
||
Uses Excel VBA. |
Uses Excel VBA. |
||
<syntaxhighlight lang=vb>Private Function arithmetic_mean(s() As Variant) As Double |
<syntaxhighlight lang="vb">Private Function arithmetic_mean(s() As Variant) As Double |
||
arithmetic_mean = WorksheetFunction.Average(s) |
arithmetic_mean = WorksheetFunction.Average(s) |
||
End Function |
End Function |
||
Line 3,543: | Line 3,544: | ||
=={{header|VBScript}}== |
=={{header|VBScript}}== |
||
<syntaxhighlight lang=vb> |
<syntaxhighlight lang="vb"> |
||
Function arithmetic_mean(arr) |
Function arithmetic_mean(arr) |
||
sum = 0 |
sum = 0 |
||
Line 3,582: | Line 3,583: | ||
=={{header|Visual Basic .NET}}== |
=={{header|Visual Basic .NET}}== |
||
{{trans|C#}} |
{{trans|C#}} |
||
<syntaxhighlight lang=vbnet>Imports System.Runtime.CompilerServices |
<syntaxhighlight lang="vbnet">Imports System.Runtime.CompilerServices |
||
Module Module1 |
Module Module1 |
||
Line 3,617: | Line 3,618: | ||
=={{header|Vlang}}== |
=={{header|Vlang}}== |
||
Updated for Vlang version 0.2.2 |
Updated for Vlang version 0.2.2 |
||
<syntaxhighlight lang=go>import math |
<syntaxhighlight lang="go">import math |
||
fn main() { |
fn main() { |
||
Line 3,648: | Line 3,649: | ||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
<syntaxhighlight lang=ecmascript>var rng = 1..10 |
<syntaxhighlight lang="ecmascript">var rng = 1..10 |
||
var count = rng.count |
var count = rng.count |
||
var A = rng.reduce { |acc, x| acc + x }/count |
var A = rng.reduce { |acc, x| acc + x }/count |
||
Line 3,670: | Line 3,671: | ||
=={{header|XPL0}}== |
=={{header|XPL0}}== |
||
<syntaxhighlight lang= |
<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 3,708: | Line 3,709: | ||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
<syntaxhighlight lang=zkl>ns:=T(1,2,3,4,5,6,7,8,9,10); |
<syntaxhighlight lang="zkl">ns:=T(1,2,3,4,5,6,7,8,9,10); |
||
ns.sum(0.0)/ns.len(); // Arithmetic mean |
ns.sum(0.0)/ns.len(); // Arithmetic mean |
||
ns.reduce('*,1.0).pow(1.0/ns.len()); // Geometric mean |
ns.reduce('*,1.0).pow(1.0/ns.len()); // Geometric mean |
||
Line 3,718: | Line 3,719: | ||
3.41417 |
3.41417 |
||
</pre> |
</pre> |
||
⚫ |