部分参考: http://blog.csdn.net/peakryuu/article/details/6180711
http://derpvail.iteye.com/blog/260992
不过两个出处都有些小bug,经过修正后如下:
public class StatisticalFunction {
public static double max(double[] p){
if (p.length == 0)
throw new IllegalArgumentException();
int len = p.length;
double max = p[0];
for (int i = 0; i < len; i++) {
if (max < p[i])
max = p[i];
}
return max;
}
public static double min(double[] p){
if (p.length == 0)
throw new IllegalArgumentException();
int len = p.length;
double min = p[0];
for (int i = 0; i < len; i++) {
if (min > p[i])
min = p[i];
}
return min;
}
public static double sum(double[] p){
if (p.length == 0)
return 0;
int len = p.length;
double sum = 0;
for (int i = 0; i < len; i++) {
sum += p[i];
}
return sum;
}
public static double avg(double[] p) {
double sumV = 0; // sum of all the elements
if (p.length == 0)
return sumV;
sumV = sum(p);
return sumV / p.length;
}
public static double square_sum(double[] p) {
int len = p.length;
double sqrsum = 0.0;
for (int i = 0; i