Averages/Arithmetic mean: Difference between revisions

no edit summary
No edit summary
Line 997:
77/240
1/5 (-3+2 a+c+Pi)</lang>
 
=={{header|Mathprog}}==
 
Summing the vector and then dividing the sum by the vector's length is slightly less boring than calling a builtin function Mean or similar.
 
Mathprog is never boring so this program finds a number M such that when M is subtracted from each value in the vector a second vector is formed with the property that the sum of the elements in the second vector is zero. In this case M is the Arithmetic Mean.
 
Euclid proved that for any vector there is only one such number and from this derived the Division Theorem.
 
To make it more interesting I find the Arithmectic Mean of more than a million Integers.
 
<lang>
/*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;
</lang>
 
When run this produces:
 
<lang>
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
</lang>
 
=={{header|MATLAB}}==
2,172

edits