出自ProgWiki
程式碼
#pragma once
#include <math.h>
#include <vector>
namespace Sys.Math {
template <typename Type>
class Mean
{
public:
typedef std::vector<Type>::iterator iterator;
public inline Type ArithmeticMean(std::vector<Type> num)
{
Type temp = (Type)0;
for (iterator iter = num.begin(); iter < num.end(); iter++ )
temp += *iter;
return (temp / num.size());
}
public inline Type GeometricMean(std::vector<Type> num)
{
Type temp = (Type)1;
for (iterator iter = num.begin(); iter < num.end(); iter++ )
temp *= *iter;
return (::pow(temp, (1.0 / num.size())));
}
public inline Type HarmonicMean(std::vector<Type> num)
{
TYPE temp = (Type)0;
for (iterator iter = num.begin(); iter < num.end(); iter++ )
temp += ( 1.0 / *iter);
return ((Type)num.size() / temp);
}
public inline Type RootMeanSquare(std::vector<TYPE> num)
{
Type temp = (Type)0;
for (iterator iter = num.begin(); iter < num.end(); iter++ )
temp += ::pow( *iter, 2.0);
return ((Type)::pow( (temp / num.size()), 0.5);
}
/* 以後再寫...
public static double HeronianMean(double a, double b)
{
double result = (Math.pow((a * b), 0.5) + a + b) / 3.0;
return result;
}
*/
}
}