20 #ifndef GAIA_GAIAMATH_H 21 #define GAIA_GAIAMATH_H 23 #ifndef _USE_MATH_DEFINES 24 #define _USE_MATH_DEFINES // needed for M_PI in Windows 35 static const float EPSILON = 1e-7f;
42 SortNode(
float d = 0.0, T* p = 0) : dist(d), ptr(p) {}
43 bool operator<(
const SortNode& x)
const {
return dist < x.dist; }
55 inline bool isnan(T x) {
return _isnan(x) != 0; }
60 inline bool isinf(T x) {
return _finite(x) == 0; }
65 inline bool isnan(T x) {
return std::isnan(x); }
68 inline bool isinf(T x) {
return std::isinf(x); }
81 T
mean(
const T* array, uint n) {
83 for (uint i=0; i<n; i++) {
90 template <
typename T> T
mean(
const std::vector<T>& v) {
91 return mean(&v.front(), v.size());
101 for (uint i=0; i<n; i++) {
109 template <
typename T> T
variance(
const std::vector<T>& v) {
110 return variance(&v.front(), v.size());
117 template <
typename T>
119 T m =
mean(array, n);
122 for (uint i=0; i<n; i++) {
124 result += tmp * tmp * tmp;
126 result /= pow(v, (T)1.5);
130 template <
typename T> T
skewness(
const std::vector<T>& v) {
131 return skewness(&v.front(), v.size());
138 template <
typename T>
140 T m =
mean(array, n);
143 for (uint i=0; i<n; i++) {
145 result += tmp * tmp * tmp * tmp;
151 template <
typename T> T
kurtosis(
const std::vector<T>& v) {
152 return kurtosis(&v.front(), v.size());
155 template <
typename RandomAccessIterator>
156 void sort(RandomAccessIterator first, RandomAccessIterator last) {
157 return std::sort(first, last);
160 template <
typename RandomAccessIterator,
typename StrictWeakOrdering>
161 void sort(RandomAccessIterator first, RandomAccessIterator last,
162 StrictWeakOrdering comp) {
163 return std::sort(first, last, comp);
166 template <
typename Container>
167 void sort(Container& container) {
168 return sort(container.begin(), container.end());
183 template <
typename T>
184 void hist(
const T* array, uint n,
int* n_array, T* x_array, uint n_bins) {
185 T miny = *min_element(array, array+n);
186 T maxy = *max_element(array, array+n);
189 for (uint i=0; i<n_bins; i++) {
190 x_array[i] = (0.5 + i)*(maxy - miny)/n_bins + miny;
194 std::vector<T> cutoff(n_bins - 1);
195 for (uint i=0; i<n_bins-1; i++) {
196 cutoff[i] = (x_array[i] + x_array[i+1]) / 2.0;
206 std::vector<T> dist(array, array+n);
207 sort(dist.begin(), dist.end());
208 uint current_cutoff_idx = 0;
209 T current_cutoff = cutoff[0];
210 for (uint i=0; i<n_bins; i++) n_array[i] = 0;
212 for (uint i=0; i<n; i++) {
213 while (dist[i] > current_cutoff) {
215 if (current_cutoff_idx == n_bins-2) {
216 n_array[n_bins-1] = n-i;
221 current_cutoff_idx++;
222 current_cutoff = cutoff[current_cutoff_idx];
224 n_array[current_cutoff_idx]++;
228 template <
typename T>
229 T sgn(T x) {
return x==0.0 ? 0.0 : (x<0.0 ? -1.0 : 1.0); }
234 template <
typename T>
236 if (n < 0)
throw GaiaException(
"intpow: power needs to be >= 1");
237 if (n == 0)
return 1;
255 double norminv(
double P,
double mu = 0,
double sigma = 1);
260 double chi2inv(
double P,
int v);
269 template <
template<
typename>
class Container,
typename T>
271 int start = 0, end = v.size()-1;
273 if (value < v[0])
return 0;
274 if (value > v[end])
return end + 1;
276 while ((end - start) > 1) {
277 uint pivotIdx = (start + end) / 2;
278 T pivot = v[pivotIdx];
293 template <
typename T> T sqrt(
const T& x) {
300 inline Real
clip(Real value, Real minv, Real maxv) {
301 return qMax(qMin(value, maxv), minv);
307 #endif // GAIA_GAIAMATH_H Real clip(Real value, Real minv, Real maxv)
Returns the given value clipped inside the specified interval.
Definition: gaiamath.h:300
double norminv(double P, double mu, double sigma)
Computes the inverse of the normal cdf with parameter mu and sigma.
Definition: gaiamath.cpp:30
T mean(const T *array, uint n)
Computes the mean of an array.
Definition: gaiamath.h:81
double erfinv(double P)
Computes the inverse error function (http://en.wikipedia.org/wiki/Error_function).
Definition: gaiamath.cpp:26
Definition: gaiamath.h:38
T variance(const T *array, uint n)
Computes the variance of an array.
Definition: gaiamath.h:98
int binarySearch(const Container< T > &v, T value)
Iterative function for binary search (more efficient than recursive one).
Definition: gaiamath.h:270
Main Gaia namespace, which contains all the library functions.
Definition: addfield.cpp:22
double chi2inv(double P, int v)
Computes the inverse of the chi-square cdf with v degrees of freedom.
Definition: gaiamath.cpp:34
T kurtosis(const T *array, uint n)
Computes the kurtosis of an array.
Definition: gaiamath.h:139
T skewness(const T *array, uint n)
Computes the skewness of an array.
Definition: gaiamath.h:118
Exception class that can take up to 3 arguments of any type, which will be serialized into a QString ...
Definition: gaiaexception.h:46
void hist(const T *array, uint n, int *n_array, T *x_array, uint n_bins)
Given a set of values, computes the associated histogram.
Definition: gaiamath.h:184
T intpow(T x, int n)
Computes the integral power n of x.
Definition: gaiamath.h:235