Blame view

src/ExternLib/StatisticFunctions/Toolbox.hh 5.13 KB
74a11471   Benjamin Renard   Add min, max, var...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
 * Toolbox.hh
 *
 *  Created on: Jun 23, 2018
 *      Author: benjamin
 */

#ifndef TOOLBOX_HH_
#define TOOLBOX_HH_

#include "ParamData.hh"

namespace AMDA {
namespace Parameters {
namespace StatisticFunctions {

template <typename Type>
Type min(Type a, Type b) {
	if (!std::isfinite(a) || isNAN(a))
		return b;
	if (!std::isfinite(b) || isNAN(b))
		return a;
	return std::min(a,b);
}

template <typename Type>
std::vector<Type> min(std::vector<Type> a, std::vector<Type> b) {
	std::vector<Type> res = a;
	if (a.size() != b.size()) {
		BOOST_THROW_EXCEPTION(AMDA::AMDA_exception() << AMDA::errno_code(AMDA_OPER_NOT_ALLOWED) << AMDA::ex_msg("Vectors don't have the same size"));
	}
	for (unsigned int i = 0; i < a.size(); ++i) {
		res[i] = StatisticFunctions::min(a[i],b[i]);
	}
        return res;
}

template <typename Type>
Tab2DData<Type> min(Tab2DData<Type> a, Tab2DData<Type> b) {
	Tab2DData<Type> res(a);
	if (a.getDim1Size() != b.getDim1Size() || a.getDim2Size() != b.getDim2Size()) {
                BOOST_THROW_EXCEPTION(AMDA::AMDA_exception() << AMDA::errno_code(AMDA_OPER_NOT_ALLOWED) << AMDA::ex_msg("Tab2DData don't have the same dimensions"));
        }
        for (int i = 0; i < a.getDim1Size(); ++i) {
		for (int j = 0; j < a.getDim2Size(); ++j) {
			res[i][j] = StatisticFunctions::min(a[i][j],b[i][j]);
		}
	}
	return res;
}

template <typename Type>
Type max(Type a, Type b) {
	if (!std::isfinite(a) || isNAN(a))
		return b;
	if (!std::isfinite(b) || isNAN(b))
		return a;
	return std::max(a,b);
}

template <typename Type>
std::vector<Type> max(std::vector<Type> a, std::vector<Type> b) {
	std::vector<Type> res = a;
	if (a.size() != b.size()) {
		BOOST_THROW_EXCEPTION(AMDA::AMDA_exception() << AMDA::errno_code(AMDA_OPER_NOT_ALLOWED) << AMDA::ex_msg("Vectors don't have the same size"));
	}
	for (unsigned int i = 0; i < a.size(); ++i) {
		res[i] = StatisticFunctions::max(a[i],b[i]);
	}
	return res;
}

template <typename Type>
Tab2DData<Type> max(Tab2DData<Type> a, Tab2DData<Type> b) {
	Tab2DData<Type> res(a);
	if (a.getDim1Size() != b.getDim1Size() || a.getDim2Size() != b.getDim2Size()) {
		BOOST_THROW_EXCEPTION(AMDA::AMDA_exception() << AMDA::errno_code(AMDA_OPER_NOT_ALLOWED) << AMDA::ex_msg("Tab2DData don't have the same dimensions"));
	}
	for (int i = 0; i < a.getDim1Size(); ++i) {
		for (int j = 0; j < a.getDim2Size(); ++j) {
			res[i][j] = StatisticFunctions::max(a[i][j],b[i][j]);
		}
	}
	return res;
}

template <typename Type>
Type pow(Type a, double exp) {
	return std::pow(a,exp);
}

template <typename Type>
std::vector<Type> pow(std::vector<Type> a, double exp) {
	std::vector<Type> res = a;
	for (unsigned int i = 0; i < a.size(); ++i) {
		res[i] = StatisticFunctions::pow(a[i],exp);
	}
	return res;
}

template <typename Type>
Tab2DData<Type> pow(Tab2DData<Type> a, double exp) {
	Tab2DData<Type> res = a;
	for (int i = 0; i < a.getDim1Size(); ++i) {
		for (int j = 0; j < a.getDim2Size(); ++j) {
			res[i][j] = StatisticFunctions::pow(a[i][j],exp);
		}
	}
	return res;
}

template <typename Type>
Type square(Type a) {
	return StatisticFunctions::pow(a,2);
}

template <typename Type>
std::vector<Type> square(std::vector<Type> a) {
	return StatisticFunctions::pow(a,2);
}

template <typename Type>
Tab2DData<Type> square(Tab2DData<Type> a) {
	return StatisticFunctions::pow(a,2);
}

template <typename Type>
Type root_square(Type a) {
	if (isNAN(a) || a < 0) {
		Type res;
		res << NotANumber();
		return res;
	}
	return std::sqrt(a);
}

template <typename Type>
std::vector<Type> root_square(std::vector<Type> a) {
	std::vector<Type> res = a;
	for (unsigned int i = 0; i < a.size(); ++i) {
		res[i] = StatisticFunctions::root_square(a[i]);
	}
	return res;
}

template <typename Type>
Tab2DData<Type> root_square(Tab2DData<Type> a) {
	Tab2DData<Type> res = a;
	for (int i = 0; i < a.getDim1Size(); ++i) {
		for (int j = 0; j < a.getDim2Size(); ++j) {
			res[i][j] = StatisticFunctions::root_square(a[i][j]);
		}
	}
	return res;
}

template <typename Type>
Type div(Type a, Type b) {
        if (isNAN(a) || isNAN(b) || (b == 0)) {
                Type res;
                res << NotANumber();
                return res;
        }
        return a/b;
}

template <typename Type>
std::vector<Type> div(std::vector<Type> a, std::vector<Type> b) {
	std::vector<Type> res = a;
	if (a.size() != b.size()) {
		BOOST_THROW_EXCEPTION(AMDA::AMDA_exception() << AMDA::errno_code(AMDA_OPER_NOT_ALLOWED) << AMDA::ex_msg("Vectors don't have the same size"));
	}
	for (unsigned int i = 0; i < a.size(); ++i) {
		res[i] = StatisticFunctions::div(a[i],b[i]);
	}
	return res;
}

template <typename Type>
Tab2DData<Type> div(Tab2DData<Type> a, Tab2DData<Type> b) {
	Tab2DData<Type> res(a);
	if (a.getDim1Size() != b.getDim1Size() || a.getDim2Size() != b.getDim2Size()) {
		BOOST_THROW_EXCEPTION(AMDA::AMDA_exception() << AMDA::errno_code(AMDA_OPER_NOT_ALLOWED) << AMDA::ex_msg("Tab2DData don't have the same dimensions"));
	}
	for (int i = 0; i < a.getDim1Size(); ++i) {
		for (int j = 0; j < a.getDim2Size(); ++j) {
			res[i][j] = StatisticFunctions::div(a[i][j],b[i][j]);
		}
	}
	return res;
}

} /* namespace StatisticFunctions */
} /* namespace Parameters */
} /* namespace AMDA */
#endif /* TOOLBOX_HH_ */