Averages/Arithmetic mean: Difference between revisions

m
Fixed lang tags (using MediaWiki::API).
(Added Scala)
m (Fixed lang tags (using MediaWiki::API).)
Line 39:
{{works with|ALGOL 68G|Any - tested with release mk15-0.8b.fc9.i386}}
{{works with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release 1.8.8d.fc9.i386 - note that some necessary LONG REAL operators are missing from ELLA's library.}}
<lang algol68>PROC mean = (REF[]REAL p)REAL:
PROC mean = (REF[]REAL p)REAL:
# Calculates the mean of qty REALs beginning at p. #
IF LWB p > UPB p THEN 0.0
Line 52 ⟶ 51:
[6]REAL test := (1.0, 2.0, 5.0, -5.0, 9.5, 3.14159);
print((mean(test),new line))
)</lang>
)
</lang>
 
=={{header|AmigaE}}==
Line 216 ⟶ 214:
 
=={{header|Clojure}}==
<lang clojurelisp>(defn mean [sq]
(defn mean [sq]
(let [length (count sq)]
(if (zero? length)
0
(/ (reduce + sq) length)))
)</lang>
)
</lang>
 
=={{header|Common Lisp}}==
Line 290 ⟶ 286:
 
=={{header|Erlang}}==
<lang erlang>mean([]) -> 0;
mean([]L) -> 0;lists:sum(L)/length(L).</lang>
mean(L) -> lists:sum(L)/length(L).
</lang>
 
=={{header|Factor}}==
Line 307 ⟶ 301:
 
=={{header|Forth}}==
<lang forth> : fmean ( addr n -- f )
0e
dup 0= if 2drop exit then
tuck floats bounds do
i f@ f+
1 floats +loop
0 d>f f/ ;
 
create test 3e f, 1e f, 4e f, 1e f, 5e f, 9e f,
test 6 fmean f. \ 3.83333333333333</lang>
 
=={{header|Fortran}}==
In ISO Fortran 90 or later, use the SUM intrinsic, the SIZE intrinsic and the MAX intrinsic (to avoid divide by zero):
<lang fortran> real, target, dimension(100) :: a = (/ (i, i=1, 100) /)
real, dimension(5,20) :: b = reshape( a, (/ 5,20 /) )
real, pointer, dimension(:) :: p => a(2:1) ! pointer to zero-length array
real :: mean, zmean, bmean
real, dimension(20) :: colmeans
real, dimension(5) :: rowmeans
 
mean = sum(a)/size(a) ! SUM of A's elements divided by SIZE of A
mean = sum(a)/max(size(a),1) ! Same result, but safer code
! MAX of SIZE and 1 prevents divide by zero if SIZE == 0 (zero-length array)
 
zmean = sum(p)/max(size(p),1) ! Here the safety check pays off. Since P is a zero-length array,
! expression becomes "0 / MAX( 0, 1 ) -> 0 / 1 -> 0", rather than "0 / 0 -> NaN"
 
bmean = sum(b)/max(size(b),1) ! multidimensional SUM over multidimensional SIZE
 
rowmeans = sum(b,1)/max(size(b,2),1) ! SUM elements in each row (dimension 1)
! dividing by the length of the row, which is the number of columns (SIZE of dimension 2)
colmeans = sum(b,2)/max(size(b,1),1) ! SUM elements in each column (dimension 2)
! dividing by the length of the column, which is the number of rows (SIZE of dimension 1)</lang>
 
=={{header|F_Sharp|F#}}==
Line 386 ⟶ 380:
If you want to avoid keeping the list in memory and traversing it twice:
 
<lang haskell>{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE BangPatterns #-}
import Data.List (foldl')
mean :: (Real n, Fractional m) => [n] -> m
mean xs = let (s,l) = foldl' f (0, 0) xs in realToFrac s / l
where f (!s,!l) x = (s+x,l+1)</lang>
</lang>
 
=={{header|IDL}}==
Line 398 ⟶ 390:
If truly only the mean is wanted, one could use
 
<lang idl> x = [3,1,4,1,5,9]
print,mean(x)</lang>
 
But <tt>mean()</tt> is just a thin wrapper returning the zeroth element of <tt>moment()</tt> :
Line 507 ⟶ 499:
=={{header|Mathematica}}==
Modify the built-in Mean function to give 0 for empty vectors (lists in Mathematica):
<lang mathematica>Unprotect[Mean];
<lang Mathematica >
Mean[{}] := 0</lang>
Unprotect[Mean];
Mean[{}] := 0
</lang>
Examples:
<lang Mathematica mathematica>Mean[{3,4,5}]
Mean[{3.2,4.5,5.9}]
Mean[{3.2,-4.5,5 1.9233}]
Mean[{-4, 1.233}]
Mean[{1/2,1/3,1/4,1/5}]
Mean[{1/2a,1/c,Pi,-3,1/4,1/5a}]</lang>
Mean[{a,c,Pi,-3,a}]
</lang>
gives (a set of integers gives back an integer or a rational, a set of floats gives back a float, a set of rationals gives a rational back, a list of symbols and numbers keeps the symbols exact and a mix of exact and approximate numbers gives back an approximate number):
<lang Mathematica mathematica>4
4.53333
4
-1.3835
4.53333
0
-1.3835
77/240
0
1/5 (-3+2 a+c+Pi)</lang>
77/240
1/5 (-3+2 a+c+Pi)
</lang>
 
=={{header|MATLAB}}==
Line 655 ⟶ 641:
=={{header|Pop11}}==
 
<lang pop11>define mean(v);
<pre>
define mean(v);
lvars n = length(v), i, s = 0;
if n = 0 then
Line 666 ⟶ 651:
endif;
return(s/n);
enddefine;</lang>
</pre>
 
=={{header|PowerShell}}==
Line 701 ⟶ 685:
 
Output:
<lang python>3.83333333333333
2.875</lang>
 
Line 714 ⟶ 698:
 
(Notice how the second call gave the wrong result)
<lang python>3.83333333333333
0.0</lang>
</lang>
 
 
{{works with|Python|2.4}}
<lang python>def avg(data):
if len(data)==0:
return 0
else:
return sum(data)/float(len(data))
print avg([3,1,4,1,5,9])</lang>
 
Output:
<lang python>3.83333333333333</lang>
 
=={{header|R}}==
Line 782 ⟶ 765:
=={{header|Slate}}==
<lang slate>[|:list| (list reduce: #+ `er ifEmpty: [0]) / (list isEmpty ifTrue: [1] ifFalse: [list size])] applyWith: {}#(3 1 4 1 5 9).
<lang slate>
[|:list| (list reduce: #+ `er ifEmpty: [0]) / (list isEmpty ifTrue: [1] ifFalse: [list size])] applyWith: #(3 1 4 1 5 9){}.</lang>
[|:list| (list reduce: #+ `er ifEmpty: [0]) / (list isEmpty ifTrue: [1] ifFalse: [list size])] applyWith: {}.
</lang>
 
=={{header|Smalltalk}}==
Line 831 ⟶ 812:
in
(real total / length)
end;</lang>
</lang>
 
=={{header|Tcl}}==
Line 844 ⟶ 824:
=={{header|TI-89 BASIC}}==
 
<lang ti89b>Define rcmean(nums) = when(dim(nums) = 0, 0, mean(nums))</lang>
 
=={{header|UnixPipes}}==
Line 894 ⟶ 874:
=={{header|Vedit macro language}}==
The numeric data is stored in current edit buffer as ASCII strings, one value per line.
<lang vedit>#1 = 0 // Sum
#1 = 0 // Sum
#2 = 0 // Count
BOF
Line 904 ⟶ 883:
}
if (#2) { #1 /= #2 }
Num_Type(#1)</lang>
</lang>
845

edits