math_funcs.hpp 4 KB
 Thomas Purcell committed May 08, 2020 1 2 3 4 5 6 7 8 9 10 ``````#ifndef MATH_FXN #define MATH_FXN #include #include #include #include #include #include `````` Thomas Purcell committed Jun 12, 2020 11 12 ``````#include `````` Thomas Purcell committed May 08, 2020 13 14 15 16 17 18 19 ``````namespace util_funcs { inline double mean(std::vector& vec){return std::accumulate(vec.begin(), vec.end(), 0.0) / vec.size();}; inline double mean(double* start, int size){return std::accumulate(start, start + size, 0.0) / size;}; `````` Thomas Purcell committed Jun 03, 2020 20 21 22 `````` inline double mean(std::vector& vec){return static_cast(std::accumulate(vec.begin(), vec.end(), 0)) / vec.size();}; inline double mean(int* start, int size){return static_cast(std::accumulate(start, start + size, 0)) / size;}; `````` Thomas Purcell committed May 08, 2020 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 `````` inline double stand_dev(std::vector& vec) { double vec_mean = mean(vec); return std::sqrt(std::accumulate(vec.begin(), vec.end(), 0.0, [&vec_mean](double total, double val){return total + std::pow(val - vec_mean, 2.0);}) / vec.size()); }; inline double stand_dev(double* start, int size) { double vec_mean = mean(start, size); return std::sqrt(std::accumulate(start, start+size, 0.0, [&vec_mean](double total, double val){return total + std::pow(val - vec_mean, 2.0);}) / size); }; inline double stand_dev(std::vector& vec, double vec_mean) { return std::sqrt(std::accumulate(vec.begin(), vec.end(), 0.0, [&vec_mean](double total, double val){return total + std::pow(val - vec_mean, 2.0);}) / vec.size()); }; inline double stand_dev(double* start, int size, double vec_mean) { return std::sqrt(std::accumulate(start, start+size, 0.0, [&vec_mean](double total, double val){return total + std::pow(val - vec_mean, 2.0);}) / size); }; inline double norm(std::vector& vec) { return std::sqrt(ddot_(vec.size(), vec.data(), 1, vec.data(), 1)); }; inline double norm(double* start, int size) { return std::sqrt(ddot_(size, start, 1, start, 1)); }; inline double r(double* a, double* b, int size) { return (ddot_(size, a, 1, b, 1) - static_cast(size) * mean(a, size) * mean(b, size)) / (static_cast(size) * stand_dev(a, size) * stand_dev(b, size)); } `````` Thomas Purcell committed Jun 12, 2020 61 62 63 64 65 66 67 68 69 `````` double r(double* a, double* b, std::vector& sizes); inline double r2(double* a, double* b, int size) { return std::pow((ddot_(size, a, 1, b, 1) - static_cast(size) * mean(a, size) * mean(b, size)) / (static_cast(size) * stand_dev(a, size) * stand_dev(b, size)), 2.0); } double r2(double* a, double* b, std::vector& sizes); `````` Thomas Purcell committed May 08, 2020 70 71 72 73 74 75 76 77 78 79 80 81 82 83 `````` inline std::vector argsort(std::vector& vec) { std::vector index(vec.size()); std::iota(index.begin(), index.end(), 0); std::sort(index.begin(), index.end(), [&vec](int i1, int i2){return vec[i1] < vec[i2];}); return index; } inline void argsort(int* begin, int* end, std::vector& vec) { std::sort(begin, end, [&vec](int i1, int i2){return vec[i1] < vec[i2];}); } `````` Thomas Purcell committed Jun 13, 2020 84 85 86 87 88 89 90 91 92 93 94 95 96 97 `````` inline std::vector argsort(std::vector& vec) { std::vector index(vec.size()); std::iota(index.begin(), index.end(), 0); std::sort(index.begin(), index.end(), [&vec](int i1, int i2){return vec[i1] < vec[i2];}); return index; } inline void argsort(int* begin, int* end, std::vector& vec) { std::sort(begin, end, [&vec](int i1, int i2){return vec[i1] < vec[i2];}); } `````` Thomas Purcell committed May 10, 2020 98 99 100 101 102 `````` inline int factorial(int n) { return (n == 1 || n == 0) ? 1 : factorial(n - 1) * n; } `````` Thomas Purcell committed Jun 09, 2020 103 104 105 106 107 108 109 110 111 112 113 `````` template inline T max_abs_val(T* start, T* end) { return std::abs(*std::max_element(start, end, [](T a, T b){return std::abs(a) < std::abs(b);})); } template inline T max_abs_val(T* start, int size) { return std::abs(*std::max_element(start, start + size, [](T a, T b){return std::abs(a) < std::abs(b);})); } `````` Thomas Purcell committed Jun 10, 2020 114 115 `````` bool iterate(std::vector& inds, int size, int incriment); `````` Thomas Purcell committed May 08, 2020 116 117 118 ``````} #endif``````