cpp-toolbox  0.0.1
A toolbox library for C++
Loading...
Searching...
No Matches
toolbox::math Namespace Reference

Functions

template<typename TContainer >
void check_empty (const TContainer &data, const char *func_name)
 检查容器是否为空并抛出异常/Check if the container is empty and throw an exception
 
template<typename TContainer >
double mean (const TContainer &data)
 计算容器中元素的平均值/Compute the mean (average) of elements in a container
 
template<typename TContainer >
double median (const TContainer &data)
 计算容器中元素的中位数/Compute the median of elements in a container
 
template<typename TContainer >
std::vector< typename TContainer::value_type > mode (const TContainer &data)
 计算容器中元素的众数(可能有多个)/Compute the mode(s) of elements in a container (may be multiple)
 
template<typename TContainer >
double variance (const TContainer &data, bool sample_variance=true)
 计算方差(默认样本方差 N-1)/Compute the variance (default is sample variance N-1)
 
template<typename TContainer >
double stdev (const TContainer &data, bool sample_stdev=true)
 计算标准差(默认样本标准差)/Compute the standard deviation (default is sample standard deviation)
 
template<typename TContainer >
TContainer::value_type sum (const TContainer &data)
 计算容器中元素的总和(返回容器元素类型)/Compute the sum of elements in the container (returns container element type)
 
template<typename TContainer >
double sum_d (const TContainer &data)
 计算容器中元素的总和(返回 double 类型以保证精度和范围)/Compute the sum of elements in the container (returns double for precision and range)
 
template<typename TContainer >
TContainer::value_type min (const TContainer &data)
 查找容器中的最小值/Find the minimum value in the container
 
template<typename TContainer >
TContainer::value_type max (const TContainer &data)
 查找容器中的最大值/Find the maximum value in the container
 
template<typename TContainer >
TContainer::value_type range (const TContainer &data)
 计算容器中元素的全距(最大值-最小值)/Compute the range (max - min) of elements in the container
 
template<typename TContainer >
double percentile (const TContainer &data, double p)
 计算百分位数(使用最近邻等级法和线性插值)/Compute the percentile (using nearest-rank and linear interpolation)
 
template<typename TContainer >
std::vector< typename TContainer::value_type > min_k (const TContainer &data, size_t k)
 返回容器中k个最小的元素,升序排列/Return the k smallest elements in the container, sorted ascending
 
template<typename TContainer >
std::vector< typename TContainer::value_type > max_k (const TContainer &data, size_t k)
 返回容器中k个最大的元素,降序排列/Return the k largest elements in the container, sorted descending
 

Detailed Description

< 用于排序和查找最值/For sorting and finding min/max 用于数学运算/For mathematical operations 用于函数对象/For function objects 用于迭代器操作/For iterator operations 用于映射统计/For map statistics 用于累加/For accumulation 用于异常处理/For exception handling 用于字符串处理/For string handling 用于类型特性/For type traits 用于通用工具/For general utilities 用于动态数组/For dynamic arrays

Function Documentation

◆ check_empty()

template<typename TContainer >
void toolbox::math::check_empty ( const TContainer &  data,
const char *  func_name 
)

检查容器是否为空并抛出异常/Check if the container is empty and throw an exception

Template Parameters
TContainer容器类型/Container type
Parameters
data输入容器/Input container
func_name函数名(用于错误信息)/Function name (for error message)
Note
若容器为空则抛出 std::invalid_argument 异常/Throws std::invalid_argument if container is empty
std::vector<int> v;
check_empty(v, "mean"); // 抛出异常/Throws exception
void check_empty(const TContainer &data, const char *func_name)
检查容器是否为空并抛出异常/Check if the container is empty and throw an exception
Definition statistics.hpp:37

◆ max()

template<typename TContainer >
TContainer::value_type toolbox::math::max ( const TContainer &  data)

查找容器中的最大值/Find the maximum value in the container

Template Parameters
TContainer容器类型,元素需可比较/Container type, elements must be comparable
Parameters
data输入容器/Input container
Returns
ValueType 最大值/Maximum value
Exceptions
std::invalid_argument如果容器为空/If the container is empty
std::vector<int> v{3, 1, 2};
int mx = toolbox::math::max(v); // mx == 3
TContainer::value_type max(const TContainer &data)
查找容器中的最大值/Find the maximum value in the container
Definition statistics.hpp:374

◆ max_k()

template<typename TContainer >
std::vector< typename TContainer::value_type > toolbox::math::max_k ( const TContainer &  data,
size_t  k 
)

返回容器中k个最大的元素,降序排列/Return the k largest elements in the container, sorted descending

Template Parameters
TContainer容器类型,元素需可比较/Container type, elements must be comparable
Parameters
data输入容器/Input container
k需要返回的最大元素个数/Number of largest elements to return
Returns
std::vector<ValueType> k个最大元素/k largest elements
Exceptions
std::invalid_argument如果k>0且容器为空/If k>0 and container is empty
Note
若k为0返回空集合,若k>=容器大小则返回所有元素降序排序/Returns empty if k==0, returns all sorted descending if k>=container size
std::vector<int> v{5, 1, 3, 2, 4};
auto maxs = toolbox::math::max_k(v, 2); // maxs == {5, 4}
std::vector< typename TContainer::value_type > max_k(const TContainer &data, size_t k)
返回容器中k个最大的元素,降序排列/Return the k largest elements in the container, sorted descending
Definition statistics.hpp:554

◆ mean()

template<typename TContainer >
double toolbox::math::mean ( const TContainer &  data)

计算容器中元素的平均值/Compute the mean (average) of elements in a container

Template Parameters
TContainer容器类型,元素需为算术类型/Container type, elements must be arithmetic
Parameters
data输入容器/Input container
Returns
double 平均值/Mean value
Exceptions
std::invalid_argument如果容器为空/If the container is empty
std::vector<int> v{1, 2, 3, 4};
double m = toolbox::math::mean(v); // m == 2.5
double mean(const TContainer &data)
计算容器中元素的平均值/Compute the mean (average) of elements in a container
Definition statistics.hpp:64

◆ median()

template<typename TContainer >
double toolbox::math::median ( const TContainer &  data)

计算容器中元素的中位数/Compute the median of elements in a container

Template Parameters
TContainer容器类型,元素需为算术类型且可比较/Container type, elements must be arithmetic and comparable
Parameters
data输入容器/Input container
Returns
double 中位数/Median value
Exceptions
std::invalid_argument如果容器为空/If the container is empty
std::vector<int> v{1, 3, 2};
double med = toolbox::math::median(v); // med == 2
double median(const TContainer &data)
计算容器中元素的中位数/Compute the median of elements in a container
Definition statistics.hpp:95

◆ min()

template<typename TContainer >
TContainer::value_type toolbox::math::min ( const TContainer &  data)

查找容器中的最小值/Find the minimum value in the container

Template Parameters
TContainer容器类型,元素需可比较/Container type, elements must be comparable
Parameters
data输入容器/Input container
Returns
ValueType 最小值/Minimum value
Exceptions
std::invalid_argument如果容器为空/If the container is empty
std::vector<int> v{3, 1, 2};
int mn = toolbox::math::min(v); // mn == 1
TContainer::value_type min(const TContainer &data)
查找容器中的最小值/Find the minimum value in the container
Definition statistics.hpp:349

◆ min_k()

template<typename TContainer >
std::vector< typename TContainer::value_type > toolbox::math::min_k ( const TContainer &  data,
size_t  k 
)

返回容器中k个最小的元素,升序排列/Return the k smallest elements in the container, sorted ascending

Template Parameters
TContainer容器类型,元素需可比较/Container type, elements must be comparable
Parameters
data输入容器/Input container
k需要返回的最小元素个数/Number of smallest elements to return
Returns
std::vector<ValueType> k个最小元素/k smallest elements
Exceptions
std::invalid_argument如果k>0且容器为空/If k>0 and container is empty
Note
若k为0返回空集合,若k>=容器大小则返回所有元素排序/Returns empty if k==0, returns all sorted if k>=container size
std::vector<int> v{5, 1, 3, 2, 4};
auto mins = toolbox::math::min_k(v, 3); // mins == {1, 2, 3}
std::vector< typename TContainer::value_type > min_k(const TContainer &data, size_t k)
返回容器中k个最小的元素,升序排列/Return the k smallest elements in the container, sorted ascending
Definition statistics.hpp:501

◆ mode()

template<typename TContainer >
std::vector< typename TContainer::value_type > toolbox::math::mode ( const TContainer &  data)

计算容器中元素的众数(可能有多个)/Compute the mode(s) of elements in a container (may be multiple)

Template Parameters
TContainer容器类型,元素需可比较/Container type, elements must be comparable
Parameters
data输入容器/Input container
Returns
std::vector<typename TContainer::value_type> 众数集合/Vector of mode(s)
Note
若容器为空,返回空集合/Returns empty vector if container is empty
std::vector<int> v{1, 2, 2, 3, 3};
auto modes = toolbox::math::mode(v); // modes == {2, 3}
std::vector< typename TContainer::value_type > mode(const TContainer &data)
计算容器中元素的众数(可能有多个)/Compute the mode(s) of elements in a container (may be multiple)
Definition statistics.hpp:138

◆ percentile()

template<typename TContainer >
double toolbox::math::percentile ( const TContainer &  data,
double  p 
)

计算百分位数(使用最近邻等级法和线性插值)/Compute the percentile (using nearest-rank and linear interpolation)

Template Parameters
TContainer容器类型,元素需为算术类型且可比较/Container type, elements must be arithmetic and comparable
Parameters
data输入容器/Input container
p百分位,范围[0.0, 1.0]/Percentile, in [0.0, 1.0]
Returns
double 百分位数/Percentile value
Exceptions
std::invalid_argument如果容器为空/If the container is empty
std::out_of_range如果p不在[0.0, 1.0]范围/If p is not in [0.0, 1.0]
std::vector<int> v{1, 2, 3, 4, 5};
double p50 = toolbox::math::percentile(v, 0.5); // p50 == 3
double p90 = toolbox::math::percentile(v, 0.9); // p90 == 4.6
double percentile(const TContainer &data, double p)
计算百分位数(使用最近邻等级法和线性插值)/Compute the percentile (using nearest-rank and linear interpolation)
Definition statistics.hpp:438

◆ range()

template<typename TContainer >
TContainer::value_type toolbox::math::range ( const TContainer &  data)

计算容器中元素的全距(最大值-最小值)/Compute the range (max - min) of elements in the container

Template Parameters
TContainer容器类型,元素需为算术类型且可比较/Container type, elements must be arithmetic and comparable
Parameters
data输入容器/Input container
Returns
ValueType 全距/Range
Exceptions
std::invalid_argument如果容器为空/If the container is empty
std::vector<int> v{3, 1, 2};
int r = toolbox::math::range(v); // r == 2
TContainer::value_type range(const TContainer &data)
计算容器中元素的全距(最大值-最小值)/Compute the range (max - min) of elements in the container
Definition statistics.hpp:402

◆ stdev()

template<typename TContainer >
double toolbox::math::stdev ( const TContainer &  data,
bool  sample_stdev = true 
)

计算标准差(默认样本标准差)/Compute the standard deviation (default is sample standard deviation)

Template Parameters
TContainer容器类型,元素需为算术类型/Container type, elements must be arithmetic
Parameters
data输入容器/Input container
sample_stdev是否计算样本标准差(true)或总体标准差(false)/Whether to compute sample stdev (true) or population stdev (false)
Returns
double 标准差/Standard deviation
Exceptions
std::invalid_argument如果样本标准差且元素少于2/If sample stdev and less than 2 elements
std::invalid_argument如果总体标准差且容器为空/If population stdev and container is empty
std::vector<double> v{1.0, 2.0, 3.0};
double sd = toolbox::math::stdev(v); // 样本标准差/sample stdev
double sd_pop = toolbox::math::stdev(v, false); // 总体标准差/population
double stdev(const TContainer &data, bool sample_stdev=true)
计算标准差(默认样本标准差)/Compute the standard deviation (default is sample standard deviation)
Definition statistics.hpp:261

◆ sum()

template<typename TContainer >
TContainer::value_type toolbox::math::sum ( const TContainer &  data)

计算容器中元素的总和(返回容器元素类型)/Compute the sum of elements in the container (returns container element type)

Template Parameters
TContainer容器类型,元素需为算术类型/Container type, elements must be arithmetic
Parameters
data输入容器/Input container
Returns
ValueType 总和/Sum
Note
空容器返回0/Returns 0 for empty container
std::vector<int> v{1, 2, 3};
int s = toolbox::math::sum(v); // s == 6
TContainer::value_type sum(const TContainer &data)
计算容器中元素的总和(返回容器元素类型)/Compute the sum of elements in the container (returns container element type)
Definition statistics.hpp:288

◆ sum_d()

template<typename TContainer >
double toolbox::math::sum_d ( const TContainer &  data)

计算容器中元素的总和(返回 double 类型以保证精度和范围)/Compute the sum of elements in the container (returns double for precision and range)

Template Parameters
TContainer容器类型,元素需为算术类型/Container type, elements must be arithmetic
Parameters
data输入容器/Input container
Returns
double 总和/Sum
Note
空容器返回0.0/Returns 0.0 for empty container
std::vector<int> v{1, 2, 3};
double s = toolbox::math::sum_d(v); // s == 6.0
double sum_d(const TContainer &data)
计算容器中元素的总和(返回 double 类型以保证精度和范围)/Compute the sum of elements in the container (returns double for pre...
Definition statistics.hpp:318

◆ variance()

template<typename TContainer >
double toolbox::math::variance ( const TContainer &  data,
bool  sample_variance = true 
)

计算方差(默认样本方差 N-1)/Compute the variance (default is sample variance N-1)

Template Parameters
TContainer容器类型,元素需为算术类型/Container type, elements must be arithmetic
Parameters
data输入容器/Input container
sample_variance是否计算样本方差(true)或总体方差(false)/Whether to compute sample variance (true) or population variance (false)
Returns
double 方差/Variance
Exceptions
std::invalid_argument如果样本方差且元素少于2/If sample variance and less than 2 elements
std::invalid_argument如果总体方差且容器为空/If population variance and container is empty
std::vector<double> v{1.0, 2.0, 3.0};
double var = toolbox::math::variance(v); // 样本方差/sample variance
double var_pop = toolbox::math::variance(v, false); // 总体方差/population
double variance(const TContainer &data, bool sample_variance=true)
计算方差(默认样本方差 N-1)/Compute the variance (default is sample variance N-1)
Definition statistics.hpp:201