math_funcs.hpp 4 KB
Newer Older
Thomas Purcell's avatar
Thomas Purcell committed
1
2
3
4
5
6
7
8
9
10
#ifndef MATH_FXN
#define MATH_FXN

#include <algorithm>
#include <functional>
#include <numeric>
#include <vector>
#include <math.h>
#include <utils/mkl_interface.hpp>

Thomas Purcell's avatar
Thomas Purcell committed
11
12
#include<iostream>

Thomas Purcell's avatar
Thomas Purcell committed
13
14
15
16
17
18
19
namespace util_funcs
{

    inline double mean(std::vector<double>& 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;};

20
21
22
    inline double mean(std::vector<int>& vec){return static_cast<double>(std::accumulate(vec.begin(), vec.end(), 0)) / vec.size();};

    inline double mean(int* start, int size){return static_cast<double>(std::accumulate(start, start + size, 0)) / size;};
Thomas Purcell's avatar
Thomas Purcell committed
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<double>& 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<double>& 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<double>& 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<double>(size) * mean(a, size) * mean(b, size)) / (static_cast<double>(size) * stand_dev(a, size) * stand_dev(b, size));
    }

Thomas Purcell's avatar
Thomas Purcell committed
61
62
63
64
65
66
67
68
69
    double r(double* a, double* b, std::vector<int>& sizes);

    inline double r2(double* a, double* b, int size)
    {
        return std::pow((ddot_(size, a, 1, b, 1) - static_cast<double>(size) * mean(a, size) * mean(b, size)) / (static_cast<double>(size) * stand_dev(a, size) * stand_dev(b, size)), 2.0);
    }

    double r2(double* a, double* b, std::vector<int>& sizes);

Thomas Purcell's avatar
Thomas Purcell committed
70
71
72
73
74
75
76
77
78
79
80
81
82
83
    inline std::vector<int> argsort(std::vector<double>& vec)
    {
        std::vector<int> 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<double>& vec)
    {
        std::sort(begin, end, [&vec](int i1, int i2){return vec[i1] < vec[i2];});
    }

84
85
86
87
88
89
90
91
92
93
94
95
96
97
    inline std::vector<int> argsort(std::vector<int>& vec)
    {
        std::vector<int> 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<int>& vec)
    {
        std::sort(begin, end, [&vec](int i1, int i2){return vec[i1] < vec[i2];});
    }

Thomas Purcell's avatar
Thomas Purcell committed
98
99
100
101
102
    inline int factorial(int n)
    {
        return (n == 1 || n == 0) ? 1 : factorial(n - 1) * n;
    }

103
104
105
106
107
108
109
110
111
112
113
    template<typename T>
    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<typename T>
    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);}));
    }
114
115

    bool iterate(std::vector<int>& inds, int size, int incriment);
Thomas Purcell's avatar
Thomas Purcell committed
116
117
118
}

#endif