/***********************************************************************
* @file math.h
* @ingroup math
* @author wangqing
* @date 2020-05-14
* @brief 常用统计计算
***********************************************************************/
#ifndef __MATH_H__
#define __MATH_H__
#include <cmath>
#include <iterator>
#include <numeric>
namespace math {
using namespace std;
template <class _InIt, class _Ty>
inline _Ty sum(const _InIt _First, const _InIt _Last, _Ty _Val) {
_Adl_verify_range(_First, _Last);
return _Val = accumulate(_First, _Last, _Val);
}
template <class _InIt, class _Ty>
inline _Ty mean(const _InIt _First, const _InIt _Last, _Ty _Val) {
_Adl_verify_range(_First, _Last);
return _Val = accumulate(_First, _Last, _Val) / distance(_First, _Last);
}
template <class _InIt, class _Ty>
inline _Ty sd(const _InIt _First, const _InIt _Last, _Ty _Val) {
_Adl_verify_range(_First, _Last);
auto m = mean(_First, _Last, _Val);
double accum{};
for_each(
_First, _Last, [&](decltype(*_First) d) { accum += pow((d - m), 2); });
return _Val = sqrt(accum / distance(_First, _Last));
return _Val;
}
template <class _InIt, class _Ty>
inline _Ty cv(const _InIt _First, const _InIt _Last, _Ty _Val) {
_Adl_verify_range(_First, _Last);
auto m = mean(_First, _Last, _Val);
auto s = sd(_First, _Last, _Val);
return _Val = s / m * 100;
}
} // namespace math
#endif // __MATH_H__
#endif // __MATH_H__
标签:__,Last,Val,Ty,sum,const,mean,cv,First
From: https://www.cnblogs.com/faithlocus/p/17565549.html