math.hpp

概要
プロトタイプ宣言動作説明
float round2even(float n);偶数への丸め (round to even)
double round2even(double n);偶数への丸め (round to even)
float round2odd(float n);奇数への丸め (round to odd)
double round2odd(double n);奇数への丸め (round to odd)
float sum (const std::vector<float>& rhs);合計 [1]
float sum_abs(const std::vector<float>& rhs);絶対値の合計 [1]
float ave (const std::vector<float>& rhs);平均値: average [1]
float med (std::vector<float> rhs);中央値: median (rhs copy will occured. And if rhs.size () is even,
average of the two median values is returned.)
float max (const std::vector<float>& rhs);最大値
float max_abs(const std::vector<float>& rhs);絶対値最大の値
float min (const std::vector<float>& rhs);最小値
float min_abs(const std::vector<float>& rhs);絶対値最小の値
float var (const std::vector<float>& rhs);不偏分散/標本分散 (variance): u^2 = (1/(n-1))*Σ(x_i-μ)^2 [1]
float var_p (const std::vector<float>& rhs);母分散 (variance population): σ^2 = (1/n)*Σ(x_i-μ)^2 [1]
float stdev (const std::vector<float>& rhs);標本標準偏差 (sample standard deviation):
u = SQRT( (1/(n-1))*Σ(x_i-μ)^2 ) [1]
float stdev_p(const std::vector<float>& rhs);標準偏差 (standard deviation): σ = SQRT( (1/n)*Σ(x_i-μ)^2 ) [1]
double sum (const std::vector<double>& rhs);合計 [1]
double sum_abs(const std::vector<double>& rhs);絶対値の合計 [1]
double ave (const std::vector<double>& rhs);平均値: average [1]
double med (std::vector<double> rhs);中央値: median (rhs copy will occured. And if rhs.size () is even,
average of the two median values is returned.)
double max (const std::vector<double>& rhs);最大値
double max_abs(const std::vector<double>& rhs);絶対値最大の値
double min (const std::vector<double>& rhs);最小値
double min_abs(const std::vector<double>& rhs);絶対値最小の値
double var (const std::vector<double>& rhs);不偏分散/標本分散 (variance): u^2 = (1/(n-1))*Σ(x_i-μ)^2 [1]
double var_p (const std::vector<double>& rhs);母分散 (variance population): σ^2 = (1/n)*Σ(x_i-μ)^2 [1]
double stdev (const std::vector<double>& rhs);標本標準偏差 (sample standard deviation):
u = SQRT( (1/(n-1))*Σ(x_i-μ)^2 ) [1]
double stdev_p(const std::vector<double>& rhs);標準偏差 (standard deviation): σ = SQRT( (1/n)*Σ(x_i-μ)^2 ) [1]
std::vector<uint> prime(uint rhs); rhs 以下の素数を返却.get a list of prime number under rhs.
std::vector<struct fact> factor(uint rhs);
struct fact{ uint prime; uint num; };
rhs を素因数分解.prime factorization
std::vector<uint> divisor(uint rhs);約数を全て求める.get a list of all divisors.
uint8 pow(uint8 base, uint8 exp);整数の累乗.base^exp
uint16 pow(uint16 base, uint16 exp);整数の累乗.base^exp
uint32 pow(uint32 base, uint32 exp);整数の累乗.base^exp
uint64 pow(uint64 base, uint64 exp);整数の累乗.base^exp
[1] Using Kahan summation algorithm.
使用例
#include <sstd/sstd.hpp>

int main(){
    // 偶数への丸め (round to even)
    float  re_f1 = sstd::round2even(1.5); printf("round to even: %lf ≒ %lf\n", 1.5, re_f1);
    float  re_f2 = sstd::round2even(2.5); printf("round to even: %lf ≒ %lf\n", 2.5, re_f2);
    double re_d1 = sstd::round2even(1.5); printf("round to even: %lf ≒ %lf\n", 1.5, re_d1);
    double re_d2 = sstd::round2even(2.5); printf("round to even: %lf ≒ %lf\n", 2.5, re_d2);
    
    // 奇数への丸め (round to odd)
    float  ro_f1 = sstd::round2odd(1.5); printf("round to odd: %lf ≒ %lf\n", 1.5, ro_f1);
    float  ro_f2 = sstd::round2odd(2.5); printf("round to odd: %lf ≒ %lf\n", 2.5, ro_f2);
    double ro_d1 = sstd::round2odd(1.5); printf("round to odd: %lf ≒ %lf\n", 1.5, ro_d1);
    double ro_d2 = sstd::round2odd(2.5); printf("round to odd: %lf ≒ %lf\n", 2.5, ro_d2);
    printf("\n");

    std::vector<float> buf_f = {-3, -2, -1, 0, 1, 2, 3, 4};
    printf("sum: %f\n", sstd::sum(buf_f));
    printf("sum_abs: %f\n", sstd::sum_abs(buf_f));
    printf("ave: %f\n", sstd::ave(buf_f));
    printf("med: %f\n", sstd::med(buf_f));
    printf("max: %f\n", sstd::max(buf_f));
    printf("max_abs: %f\n", sstd::max_abs(buf_f));
    printf("min: %f\n", sstd::min(buf_f));
    printf("min_abs: %f\n", sstd::min_abs(buf_f));
    printf("var: %f\n", sstd::var(buf_f));
    printf("var_p: %f\n", sstd::var_p(buf_f));
    printf("stdev: %f\n", sstd::stdev(buf_f));
    printf("stdev_p: %f\n", sstd::stdev_p(buf_f));
    printf("\n");
    
    std::vector<double> buf_d = {-3, -2, -1, 0, 1, 2, 3, 4};
    printf("sum: %f\n", sstd::sum(buf_d));
    printf("sum_abs: %f\n", sstd::sum_abs(buf_d));
    printf("ave: %f\n", sstd::ave(buf_d));
    printf("med: %f\n", sstd::med(buf_d));
    printf("max: %f\n", sstd::max(buf_d));
    printf("max_abs: %f\n", sstd::max_abs(buf_d));
    printf("min: %f\n", sstd::min(buf_d));
    printf("min_abs: %f\n", sstd::min_abs(buf_d));
    printf("var: %f\n", sstd::var(buf_d));
    printf("var_p: %f\n", sstd::var_p(buf_d));
    printf("stdev: %f\n", sstd::stdev(buf_d));
    printf("stdev_p: %f\n", sstd::stdev_p(buf_d));
    printf("\n");

    // get a list of prime number under rhs.
    std::vector<uint> primeList = sstd::prime((uint)100);
    printf("primeList = "); sstd::print(primeList);
    printf("\n");

    uint fact_num = 2*2*3*3*5*5;
    std::vector<struct sstd::fact> factList = sstd::factor(fact_num);
    printf("%u = %u^%u", fact_num, factList[0].prime, factList[0].num);
    for(uint i=1; i<factList.size(); i++){ printf(" + %u^%u", factList[i].prime, factList[i].num); }
    printf("\n");

    uint div_num = 2*2*3*3*5*5;
    std::vector<uint> divList = sstd::divisor(div_num);
    printf("divList = "); sstd::print(divList);
    printf("\n");

    uint   ui  = sstd::pow((uint)2, (uint)4); printf("%u\n", ui );
    uint8  u8  = sstd::pow((uint8)2, (uint8)4); printf("%u\n", u8 );
    uint16 u16 = sstd::pow((uint16)2, (uint16)8); printf("%u\n", u16);
    uint32 u32 = sstd::pow((uint32)2, (uint32)9); printf("%u\n", u32);
    uint64 u64 = sstd::pow((uint64)2, (uint64)10); printf("%lu\n", u64);
}

実行結果
$ ./exe.exe
round to even: 1.500000 ≒ 2.000000
round to even: 2.500000 ≒ 2.000000
round to even: 1.500000 ≒ 2.000000
round to even: 2.500000 ≒ 2.000000
round to odd: 1.500000 ≒ 1.000000
round to odd: 2.500000 ≒ 3.000000
round to odd: 1.500000 ≒ 1.000000
round to odd: 2.500000 ≒ 3.000000

sum: 4.000000
sum_abs: 16.000000
ave: 0.500000
med: 0.500000
max: 4.000000
max_abs: 4.000000
min: -3.000000
min_abs: 0.000000
var: 6.000000
var_p: 5.250000
stdev: 2.449490
stdev_p: 2.291288

sum: 4.000000
sum_abs: 16.000000
ave: 0.500000
med: 0.500000
max: 4.000000
max_abs: 4.000000
min: -3.000000
min_abs: 0.000000
var: 6.000000
var_p: 5.250000
stdev: 2.449490
stdev_p: 2.291288

primeList = [ 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 ]

900 = 2^2 + 3^2 + 5^2
divList = [ 1 2 3 4 5 6 9 10 12 15 18 20 25 30 36 45 50 60 75 90 100 150 180 225 300 450 900 ]

16
16
256
512
1024
実装

0 件のコメント:

コメントを投稿