This commit is contained in:
Ingy döt Net 2013-04-10 21:29:02 -07:00
parent 764da6cbbb
commit db842d013d
19005 changed files with 197040 additions and 7 deletions

View file

@ -0,0 +1,4 @@
USING: math math.statistics ;
: arithmetic-mean ( seq -- n )
[ 0 ] [ mean ] if-empty ;

View file

@ -0,0 +1,2 @@
( scratchpad ) { 2 3 5 } arithmetic-mean >float
3.333333333333333

View file

@ -0,0 +1,18 @@
class Main
{
static Float average (Float[] nums)
{
if (nums.size == 0) return 0.0f
Float sum := 0f
nums.each |num| { sum += num }
return sum / nums.size.toFloat
}
public static Void main ()
{
[[,], [1f], [1f,2f,3f,4f]].each |Float[] i|
{
echo ("Average of $i is: " + average(i))
}
}
}

View file

@ -0,0 +1,3 @@
!vl0=?vl1=?vl&!
v< +<>0n; >n;
>l1)?^&,n;

View file

@ -0,0 +1,12 @@
Mean := function(v)
local n;
n := Length(v);
if n = 0 then
return 0;
else
return Sum(v)/n;
fi;
end;
Mean([3, 1, 4, 1, 5, 9]);
# 23/6

View file

@ -0,0 +1 @@
def avg = { list -> list == [] ? 0 : list.sum() / list.size() }

View file

@ -0,0 +1,3 @@
println avg(0..9)
println avg([2,2,2,4,2])
println avg ([])

View file

@ -0,0 +1,5 @@
REAL :: vec(100) ! no zero-length arrays in HicEst
vec = $ - 1/2 ! 0.5 ... 99.5
mean = SUM(vec) / LEN(vec) ! 50
END

View file

@ -0,0 +1,2 @@
x = [3,1,4,1,5,9]
print,mean(x)

View file

@ -0,0 +1,3 @@
print,moment(x)
; ==>
3.83333 8.96667 0.580037 -1.25081

View file

@ -0,0 +1,4 @@
procedure main(args)
every (s := 0) +:= !args
write((real(s)/(0 ~= *args)) | 0)
end

View file

@ -0,0 +1 @@
mean=: +/ % #

View file

@ -0,0 +1,7 @@
mean 3 1 4 1 5 9
3.83333
mean $0 NB. $0 is a zero-length vector
0
x=: 20 4 ?@$ 0 NB. a 20-by-4 table of random (0,1) numbers
mean x
0.58243 0.402948 0.477066 0.511155

View file

@ -0,0 +1,11 @@
mean1=: 3 : 0
z=. 0
for_i. i.#y do. z=. z+i{y end.
z % #y
)
mean1 3 1 4 1 5 9
3.83333
mean1 $0
0
mean1 x
0.58243 0.402948 0.477066 0.511155

View file

@ -0,0 +1,5 @@
julia> mean([1,2,3])
2.0
julia> mean([])
ERROR: mean of empty collection undefined: []

View file

@ -0,0 +1,5 @@
mean: {(+/x)%#x}
mean 1 2 3 5 7
3.6
mean@!0 / empty array
0.0

View file

@ -0,0 +1,22 @@
integer MAX_ELEMENTS = 10;
integer MAX_VALUE = 100;
default {
state_entry() {
list lst = [];
integer x = 0;
for(x=0 ; x<MAX_ELEMENTS ; x++) {
lst += llFrand(MAX_VALUE);
}
llOwnerSay("lst=["+llList2CSV(lst)+"]");
llOwnerSay("Geometric Mean: "+(string)llListStatistics(LIST_STAT_GEOMETRIC_MEAN, lst));
llOwnerSay(" Max: "+(string)llListStatistics(LIST_STAT_MAX, lst));
llOwnerSay(" Mean: "+(string)llListStatistics(LIST_STAT_MEAN, lst));
llOwnerSay(" Median: "+(string)llListStatistics(LIST_STAT_MEDIAN, lst));
llOwnerSay(" Min: "+(string)llListStatistics(LIST_STAT_MIN, lst));
llOwnerSay(" Num Count: "+(string)llListStatistics(LIST_STAT_NUM_COUNT, lst));
llOwnerSay(" Range: "+(string)llListStatistics(LIST_STAT_RANGE, lst));
llOwnerSay(" Std Dev: "+(string)llListStatistics(LIST_STAT_STD_DEV, lst));
llOwnerSay(" Sum: "+(string)llListStatistics(LIST_STAT_SUM, lst));
llOwnerSay(" Sum Squares: "+(string)llListStatistics(LIST_STAT_SUM_SQUARES, lst));
}
}

View file

@ -0,0 +1,11 @@
total=17
dim nums(total)
for i = 1 to total
nums(i)=i-1
next
for j = 1 to total
sum=sum+nums(j)
next
if total=0 then mean=0 else mean=sum/total
print "Arithmetic mean: ";mean

View file

@ -0,0 +1,5 @@
to average :l
if empty? :l [output 0]
output quotient apply "sum :l count :l
end
print average [1 2 3 4] ; 2.5

View file

@ -0,0 +1,6 @@
avg(x)
where
sum = first(x) fby sum + next(x);
n = 1 fby n + 1;
avg = sum / n;
end

View file

@ -0,0 +1,5 @@
define(`extractdec', `ifelse(eval(`$1%100 < 10'),1,`0',`')eval($1%100)')dnl
define(`fmean', `eval(`($2/$1)/100').extractdec(eval(`$2/$1'))')dnl
define(`mean', `rmean(`$#', $@)')dnl
define(`rmean', `ifelse(`$3', `', `fmean($1,$2)',dnl
`rmean($1, eval($2+$3), shift(shift(shift($@))))')')dnl

View file

@ -0,0 +1 @@
mean(0,100,200,300,400,500,600,700,800,900,1000)

View file

@ -0,0 +1,11 @@
fn mean data =
(
total = 0
for i in data do
(
total += i
)
if data.count == 0 then 0 else total as float/data.count
)
print (mean #(3, 1, 4, 1, 5, 9))

View file

@ -0,0 +1,8 @@
MEAN(X)
;X is assumed to be a list of numbers separated by "^"
QUIT:'$DATA(X) "No data"
QUIT:X="" "Empty Set"
NEW S,I
SET S=0,I=1
FOR QUIT:I>$L(X,"^") SET S=S+$P(X,"^",I),I=I+1
QUIT (S/$L(X,"^"))

View file

@ -0,0 +1,4 @@
mean := proc( a :: indexable )
local i;
Normalizer( add( i, i in a ) / numelems( a ) )
end proc:

View file

@ -0,0 +1,21 @@
> mean( { 1/2, 2/3, 3/4, 4/5, 5/6 } ); # set
71
---
100
> mean( [ a, 2, c, 2.3, e ] ); # list
0.8600000000 + a/5 + c/5 + e/5
> mean( Array( [ 1, sin( s ), 3, exp( I*t ), 5 ] ) ); # array
9/5 + 1/5 sin(s) + 1/5 exp(t I)
> mean( [ sin(s)^2, cos(s)^2 ] );
2 2
1/2 sin(s) + 1/2 cos(s)
> Normalizer := simplify: # use a stronger normalizer than the default
> mean( [ sin(s)^2, cos(s)^2 ] );
1/2
> mean([]); # empty argument causes an exception to be raised.
Error, (in mean) numeric exception: division by zero

View file

@ -0,0 +1 @@
mean := () -> Normalizer( `+`( args ) / nargs ):

View file

@ -0,0 +1,10 @@
> mean( 1, 2, 3, 4, 5 );
3
> mean( a + b, b + c, c + d, d + e, e + a );
2 a 2 b 2 c 2 d 2 e
--- + --- + --- + --- + ---
5 5 5 5 5
> mean(); # again, an exception is raised
Error, (in mean) numeric exception: division by zero

View file

@ -0,0 +1 @@
mean := ( s :: seq(algebraic) ) -> Normalizer( `+`( args ) / nargs ):

View file

@ -0,0 +1,2 @@
Unprotect[Mean];
Mean[{}] := 0

View file

@ -0,0 +1,6 @@
Mean[{3,4,5}]
Mean[{3.2,4.5,5.9}]
Mean[{-4, 1.233}]
Mean[{}]
Mean[{1/2,1/3,1/4,1/5}]
Mean[{a,c,Pi,-3,a}]

View file

@ -0,0 +1,6 @@
4
4.53333
-1.3835
0
77/240
1/5 (-3+2 a+c+Pi)

View file

@ -0,0 +1,24 @@
/*Arithmetic Mean of a large number of Integers
- or - solve a very large constraint matrix
over 1 million rows and columns
Nigel_Galloway
March 18th., 2008.
*/
param e := 20;
set Sample := {1..2**e-1};
var Mean;
var E{z in Sample};
/* sum of variances is zero */
zumVariance: sum{z in Sample} E[z] = 0;
/* Mean + variance[n] = Sample[n] */
variances{z in Sample}: Mean + E[z] = z;
solve;
printf "The arithmetic mean of the integers from 1 to %d is %f\n", 2**e-1, Mean;
end;

View file

@ -0,0 +1,22 @@
GLPSOL: GLPK LP/MIP Solver, v4.47
Parameter(s) specified in the command line:
--nopresol --math AM.mprog
Reading model section from AM.mprog...
24 lines were read
Generating zumVariance...
Generating variances...
Model has been successfully generated
Scaling...
A: min|aij| = 1.000e+000 max|aij| = 1.000e+000 ratio = 1.000e+000
Problem data seem to be well scaled
Constructing initial basis...
Size of triangular part = 1048575
GLPK Simplex Optimizer, v4.47
1048576 rows, 1048576 columns, 3145725 non-zeros
0: obj = 0.000000000e+000 infeas = 5.498e+011 (1)
* 1: obj = 0.000000000e+000 infeas = 0.000e+000 (0)
OPTIMAL SOLUTION FOUND
Time used: 2.0 secs
Memory used: 1393.8 Mb (1461484590 bytes)
The arithmetic mean of the integers from 1 to 1048575 is 524288.000000
Model has been successfully processed

View file

@ -0,0 +1 @@
mean([2, 7, 11, 17]);

View file

@ -0,0 +1,10 @@
PROCEDURE Avg;
VAR avg : REAL;
BEGIN
avg := sx / n;
InOut.WriteString ("Average = ");
InOut.WriteReal (avg, 8, 2);
InOut.WriteLn
END Avg;

View file

@ -0,0 +1,14 @@
PROCEDURE Average (Data : ARRAY OF REAL; Samples : CARDINAL) : REAL;
(* Calculate the average over 'Samples' values, stored in array 'Data'. *)
VAR sum : REAL;
n : CARDINAL;
BEGIN
sum := 0.0;
FOR n := 0 TO Samples - 1 DO
sum := sum + Data [n]
END;
RETURN sum / FLOAT(Samples)
END Average;