Kstars

gslhelpers.h
1/*
2 SPDX-FileCopyrightText: 2022 Sophie Taylor <sophie@spacekitteh.moe>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#pragma once
8
9#include <memory>
10#include <gsl/gsl_vector.h>
11#include <gsl/gsl_sort.h>
12#include <gsl/gsl_statistics.h>
13
14// gslhelpers is used to provide utility routines to access GNU Science Library (GSL) routines
15// iterators to access strided arrays are supported and these routines help to access the correct
16// GSL rotines (C based) for the datatype of the input data.
17//
18// gslheklpers is currently used by robuststatistics.h/cpp to access GSL robust statistics routines but
19// could be extended in future to access other GSL functions.
20
21namespace Mathematics
22{
23
24namespace GSLHelpers
25{
26
27// int64 code is currently deactiovated by GSLHELPERS_INT64. It doesn't compile on Mac becuase it
28// won't cast a long long * to a long * even though they are both 64bit pointers.
29// On 32bit systems this would be an issue because they are not the same.
30// If the int64 versions are required in future then this "problem" will need to
31// be resolved.
32#undef GSLHELPERS_INT64
33
34/**
35 * @short creates a new shared pointer to a GSL vector which gets automatically freed.
36 * The vector is initialised to 0.
37 *
38 * @param n the size of the vector.
39 */
40std::shared_ptr<gsl_vector> NewGSLVector(size_t n);
41
42template<class Iter_T>
43class stride_iter
44{
45 public:
46 // public typedefs
47 typedef typename std::iterator_traits<Iter_T>::value_type value_type;
48 typedef typename std::iterator_traits<Iter_T>::reference reference;
49 typedef typename std::iterator_traits<Iter_T>::difference_type
50 difference_type;
51 typedef typename std::iterator_traits<Iter_T>::pointer pointer;
52 typedef std::random_access_iterator_tag iterator_category;
53 typedef stride_iter self;
54
55 // constructors
56 constexpr stride_iter( ) : iter(nullptr), step(0) { }
57 constexpr stride_iter(const self &x) : iter(x.iter), step(x.step) { }
58 constexpr stride_iter(Iter_T x, difference_type n) : iter(x), step(n) { }
59
60 // operators
61 constexpr self &operator++( )
62 {
63 iter += step;
64 return *this;
65 }
66 constexpr self operator++(int)
67 {
68 self tmp = *this;
69 iter += step;
70 return tmp;
71 }
72 constexpr self &operator+=(difference_type x)
73 {
74 iter += x * step;
75 return *this;
76 }
77 constexpr self &operator--( )
78 {
79 iter -= step;
80 return *this;
81 }
82 constexpr self operator--(int)
83 {
84 self tmp = *this;
85 iter -= step;
86 return tmp;
87 }
88 constexpr self &operator-=(difference_type x)
89 {
90 iter -= x * step;
91 return *this;
92 }
93 constexpr reference operator[](difference_type n)
94 {
95 return iter[n * step];
96 }
97 reference operator*( ) const
98 {
99 return *iter;
100 }
101
102 // friend operators
103 constexpr friend bool operator==(const self &x, const self &y)
104 {
105 assert(x.step == y.step);
106 return x.iter == y.iter;
107 }
108 constexpr friend bool operator!=(const self &x, const self &y)
109 {
110 assert(x.step == y.step);
111 return x.iter != y.iter;
112 }
113 constexpr friend bool operator<(const self &x, const self &y)
114 {
115 assert(x.step == y.step);
116 return x.iter < y.iter;
117 }
118 constexpr friend difference_type operator-(const self &x, const self &y)
119 {
120 assert(x.step == y.step);
121 return (x.iter - y.iter) / x.step;
122 }
123 constexpr friend self operator+(const self &x, difference_type y)
124 {
125 assert(x.step == y.step);
126 return x += y * x.step;
127 }
128 constexpr friend self operator+(difference_type x, const self &y)
129 {
130 assert(x.step == y.step);
131 return y += x * x.step;
132 }
133 private:
134 Iter_T iter;
135 difference_type step;
136};
137
138template< class Iter_T >
139constexpr static stride_iter<Iter_T> make_strided_iter(Iter_T i, typename stride_iter<Iter_T>::difference_type stride)
140{
141 return stride_iter<Iter_T>(i, stride);
142}
143
144
145
146
147inline void gslSort(uint8_t data[], const size_t stride, const size_t n)
148{
149 gsl_sort_uchar(data, stride, n);
150}
151inline void gslSort(double data[], const size_t stride, const size_t n)
152{
153 gsl_sort(data, stride, n);
154}
155inline void gslSort(float data[], const size_t stride, const size_t n)
156{
157 gsl_sort_float(data, stride, n);
158}
159inline void gslSort(uint16_t data[], const size_t stride, const size_t n)
160{
161 gsl_sort_ushort(data, stride, n);
162}
163inline void gslSort(int16_t data[], const size_t stride, const size_t n)
164{
165 gsl_sort_short(data, stride, n);
166}
167inline void gslSort(uint32_t data[], const size_t stride, const size_t n)
168{
169 gsl_sort_uint(data, stride, n);
170}
171inline void gslSort(int32_t data[], const size_t stride, const size_t n)
172{
173 gsl_sort_int(data, stride, n);
174}
175#ifdef GSLHELPERS_INT64
176inline void gslSort(int64_t data[], const size_t stride, const size_t n)
177{
178 gsl_sort_long(data, stride, n);
179}
180#endif
181
182
183
184
185
186
187
188inline double gslMean(const uint8_t data[], const size_t stride, const size_t n)
189{
190 return gsl_stats_uchar_mean(data, stride, n);
191}
192inline double gslMean(const double data[], const size_t stride, const size_t n)
193{
194 return gsl_stats_mean(data, stride, n);
195}
196inline double gslMean(const float data[], const size_t stride, const size_t n)
197{
198 return gsl_stats_float_mean(data, stride, n);
199}
200inline double gslMean(const uint16_t data[], const size_t stride, const size_t n)
201{
202 return gsl_stats_ushort_mean(data, stride, n);
203}
204inline double gslMean(const int16_t data[], const size_t stride, const size_t n)
205{
206 return gsl_stats_short_mean(data, stride, n);
207}
208inline double gslMean(const uint32_t data[], const size_t stride, const size_t n)
209{
210 return gsl_stats_uint_mean(data, stride, n);
211}
212inline double gslMean(const int32_t data[], const size_t stride, const size_t n)
213{
214 return gsl_stats_int_mean(data, stride, n);
215}
216#ifdef GSLHELPERS_INT64
217inline double gslMean(const int64_t data[], const size_t stride, const size_t n)
218{
219 return gsl_stats_long_mean(data, stride, n);
220}
221#endif
222
223
224inline double gslGastwirthMeanFromSortedData(const uint8_t data[], const size_t stride, const size_t n)
225{
226 return gsl_stats_uchar_gastwirth_from_sorted_data(data, stride, n);
227}
228inline double gslGastwirthMeanFromSortedData(const double data[], const size_t stride, const size_t n)
229{
230 return gsl_stats_gastwirth_from_sorted_data(data, stride, n);
231}
232inline double gslGastwirthMeanFromSortedData(const float data[], const size_t stride, const size_t n)
233{
234 return gsl_stats_float_gastwirth_from_sorted_data(data, stride, n);
235}
236inline double gslGastwirthMeanFromSortedData(const uint16_t data[], const size_t stride, const size_t n)
237{
238 return gsl_stats_ushort_gastwirth_from_sorted_data(data, stride, n);
239}
240inline double gslGastwirthMeanFromSortedData(const int16_t data[], const size_t stride, const size_t n)
241{
242 return gsl_stats_short_gastwirth_from_sorted_data(data, stride, n);
243}
244inline double gslGastwirthMeanFromSortedData(const uint32_t data[], const size_t stride, const size_t n)
245{
246 return gsl_stats_uint_gastwirth_from_sorted_data(data, stride, n);
247}
248inline double gslGastwirthMeanFromSortedData(const int32_t data[], const size_t stride, const size_t n)
249{
250 return gsl_stats_int_gastwirth_from_sorted_data(data, stride, n);
251}
252#ifdef GSLHELPERS_INT64
253inline double gslGastwirthMeanFromSortedData(const int64_t data[], const size_t stride, const size_t n)
254{
255 return gsl_stats_long_gastwirth_from_sorted_data(data, stride, n);
256}
257#endif
258
259
260inline double gslTrimmedMeanFromSortedData(const double trim, const uint8_t data[], const size_t stride, const size_t n)
261{
262 return gsl_stats_uchar_trmean_from_sorted_data(trim, data, stride, n);
263}
264inline double gslTrimmedMeanFromSortedData(const double trim, const double data[], const size_t stride, const size_t n)
265{
266 return gsl_stats_trmean_from_sorted_data(trim, data, stride, n);
267}
268inline double gslTrimmedMeanFromSortedData(const double trim, const float data[], const size_t stride, const size_t n)
269{
270 return gsl_stats_float_trmean_from_sorted_data(trim, data, stride, n);
271}
272inline double gslTrimmedMeanFromSortedData(const double trim, const uint16_t data[], const size_t stride,
273 const size_t n)
274{
275 return gsl_stats_ushort_trmean_from_sorted_data(trim, data, stride, n);
276}
277inline double gslTrimmedMeanFromSortedData(const double trim, const int16_t data[], const size_t stride, const size_t n)
278{
279 return gsl_stats_short_trmean_from_sorted_data(trim, data, stride, n);
280}
281inline double gslTrimmedMeanFromSortedData(const double trim, const uint32_t data[], const size_t stride,
282 const size_t n)
283{
284 return gsl_stats_uint_trmean_from_sorted_data(trim, data, stride, n);
285}
286inline double gslTrimmedMeanFromSortedData(const double trim, const int32_t data[], const size_t stride, const size_t n)
287{
288 return gsl_stats_int_trmean_from_sorted_data(trim, data, stride, n);
289}
290#ifdef GSLHELPERS_INT64
291inline double gslTrimmedMeanFromSortedData(const double trim, const int64_t data[], const size_t stride, const size_t n)
292{
293 return gsl_stats_long_trmean_from_sorted_data(trim, data, stride, n);
294}
295#endif
296
297
298
299
300inline double gslMedianFromSortedData(const uint8_t data[], const size_t stride, const size_t n)
301{
302 return gsl_stats_uchar_median_from_sorted_data(data, stride, n);
303}
304inline double gslMedianFromSortedData(const double data[], const size_t stride, const size_t n)
305{
306 return gsl_stats_median_from_sorted_data(data, stride, n);
307}
308inline double gslMedianFromSortedData(const float data[], const size_t stride, const size_t n)
309{
310 return gsl_stats_float_median_from_sorted_data(data, stride, n);
311}
312inline double gslMedianFromSortedData(const uint16_t data[], const size_t stride, const size_t n)
313{
314 return gsl_stats_ushort_median_from_sorted_data(data, stride, n);
315}
316inline double gslMedianFromSortedData(const int16_t data[], const size_t stride, const size_t n)
317{
318 return gsl_stats_short_median_from_sorted_data(data, stride, n);
319}
320inline double gslMedianFromSortedData(const uint32_t data[], const size_t stride, const size_t n)
321{
322 return gsl_stats_uint_median_from_sorted_data(data, stride, n);
323}
324inline double gslMedianFromSortedData(const int32_t data[], const size_t stride, const size_t n)
325{
326 return gsl_stats_int_median_from_sorted_data(data, stride, n);
327}
328#ifdef GSLHELPERS_INT64
329inline double gslMedianFromSortedData(const int64_t data[], const size_t stride, const size_t n)
330{
331 return gsl_stats_long_median_from_sorted_data(data, stride, n);
332}
333#endif
334
335
336inline double gslVariance(const uint8_t data[], const size_t stride, const size_t n)
337{
338 return gsl_stats_uchar_variance(data, stride, n);
339}
340inline double gslVariance(const double data[], const size_t stride, const size_t n)
341{
342 return gsl_stats_variance(data, stride, n);
343}
344inline double gslVariance(const float data[], const size_t stride, const size_t n)
345{
346 return gsl_stats_float_variance(data, stride, n);
347}
348inline double gslVariance(const uint16_t data[], const size_t stride, const size_t n)
349{
350 return gsl_stats_ushort_variance(data, stride, n);
351}
352inline double gslVariance(const int16_t data[], const size_t stride, const size_t n)
353{
354 return gsl_stats_short_variance(data, stride, n);
355}
356inline double gslVariance(const uint32_t data[], const size_t stride, const size_t n)
357{
358 return gsl_stats_uint_variance(data, stride, n);
359}
360inline double gslVariance(const int32_t data[], const size_t stride, const size_t n)
361{
362 return gsl_stats_int_variance(data, stride, n);
363}
364#ifdef GSLHELPERS_INT64
365inline double gslVariance(const int64_t data[], const size_t stride, const size_t n)
366{
367 return gsl_stats_long_variance(data, stride, n);
368}
369#endif
370
371
372
373inline double gslStandardDeviation(const uint8_t data[], const size_t stride, const size_t n)
374{
375 return gsl_stats_uchar_sd(data, stride, n);
376}
377inline double gslStandardDeviation(const double data[], const size_t stride, const size_t n)
378{
379 return gsl_stats_sd(data, stride, n);
380}
381inline double gslStandardDeviation(const float data[], const size_t stride, const size_t n)
382{
383 return gsl_stats_float_sd(data, stride, n);
384}
385inline double gslStandardDeviation(const uint16_t data[], const size_t stride, const size_t n)
386{
387 return gsl_stats_ushort_sd(data, stride, n);
388}
389inline double gslStandardDeviation(const int16_t data[], const size_t stride, const size_t n)
390{
391 return gsl_stats_short_sd(data, stride, n);
392}
393inline double gslStandardDeviation(const uint32_t data[], const size_t stride, const size_t n)
394{
395 return gsl_stats_uint_sd(data, stride, n);
396}
397inline double gslStandardDeviation(const int32_t data[], const size_t stride, const size_t n)
398{
399 return gsl_stats_int_sd(data, stride, n);
400}
401#ifdef GSLHELPERS_INT64
402inline double gslStandardDeviation(const int64_t data[], const size_t stride, const size_t n)
403{
404 return gsl_stats_long_sd(data, stride, n);
405}
406#endif
407
408
409
410inline double gslMAD(const uint8_t data[], const size_t stride, const size_t n, double work[])
411{
412 return gsl_stats_uchar_mad(data, stride, n, work);
413}
414inline double gslMAD(const double data[], const size_t stride, const size_t n, double work[])
415{
416 return gsl_stats_mad(data, stride, n, work);
417}
418inline double gslMAD(const float data[], const size_t stride, const size_t n, double work[])
419{
420 return gsl_stats_float_mad(data, stride, n, work);
421}
422inline double gslMAD(const uint16_t data[], const size_t stride, const size_t n, double work[])
423{
424 return gsl_stats_ushort_mad(data, stride, n, work);
425}
426inline double gslMAD(const int16_t data[], const size_t stride, const size_t n, double work[])
427{
428 return gsl_stats_short_mad(data, stride, n, work);
429}
430inline double gslMAD(const uint32_t data[], const size_t stride, const size_t n, double work[])
431{
432 return gsl_stats_uint_mad(data, stride, n, work);
433}
434inline double gslMAD(const int32_t data[], const size_t stride, const size_t n, double work[])
435{
436 return gsl_stats_int_mad(data, stride, n, work);
437}
438#ifdef GSLHELPERS_INT64
439inline double gslMAD(const int64_t data[], const size_t stride, const size_t n, double work[])
440{
441 return gsl_stats_long_mad(data, stride, n, work);
442}
443#endif
444
445
446inline double gslSnFromSortedData(const uint8_t data[], const size_t stride, const size_t n, uint8_t work[])
447{
448 return gsl_stats_uchar_Sn_from_sorted_data(data, stride, n, work);
449}
450inline double gslSnFromSortedData(const double data[], const size_t stride, const size_t n, double work[])
451{
452 return gsl_stats_Sn_from_sorted_data(data, stride, n, work);
453}
454inline double gslSnFromSortedData(const float data[], const size_t stride, const size_t n, float work[])
455{
456 return gsl_stats_float_Sn_from_sorted_data(data, stride, n, work);
457}
458inline double gslSnFromSortedData(const uint16_t data[], const size_t stride, const size_t n, uint16_t work[])
459{
460 return gsl_stats_ushort_Sn_from_sorted_data(data, stride, n, work);
461}
462inline double gslSnFromSortedData(const int16_t data[], const size_t stride, const size_t n, int16_t work[])
463{
464 return gsl_stats_short_Sn_from_sorted_data(data, stride, n, work);
465}
466inline double gslSnFromSortedData(const uint32_t data[], const size_t stride, const size_t n, uint32_t work[])
467{
468 return gsl_stats_uint_Sn_from_sorted_data(data, stride, n, work);
469}
470inline double gslSnFromSortedData(const int32_t data[], const size_t stride, const size_t n, int32_t work[])
471{
472 return gsl_stats_int_Sn_from_sorted_data(data, stride, n, work);
473}
474#ifdef GSLHELPERS_INT64
475inline double gslSnFromSortedData(const int64_t data[], const size_t stride, const size_t n, int64_t work[])
476{
477 return gsl_stats_long_Sn_from_sorted_data(data, stride, n, work);
478}
479#endif
480
481inline double gslQnFromSortedData(const uint8_t data[], const size_t stride, const size_t n, uint8_t work[],
482 int work_int[])
483{
484 return gsl_stats_uchar_Qn_from_sorted_data(data, stride, n, work, work_int);
485}
486inline double gslQnFromSortedData(const double data[], const size_t stride, const size_t n, double work[],
487 int work_int[])
488{
489 return gsl_stats_Qn_from_sorted_data(data, stride, n, work, work_int);
490}
491inline double gslQnFromSortedData(const float data[], const size_t stride, const size_t n, float work[], int work_int[])
492{
493 return gsl_stats_float_Qn_from_sorted_data(data, stride, n, work, work_int);
494}
495inline double gslQnFromSortedData(const uint16_t data[], const size_t stride, const size_t n, uint16_t work[],
496 int work_int[])
497{
498 return gsl_stats_ushort_Qn_from_sorted_data(data, stride, n, work, work_int);
499}
500inline double gslQnFromSortedData(const int16_t data[], const size_t stride, const size_t n, int16_t work[],
501 int work_int[])
502{
503 return gsl_stats_short_Qn_from_sorted_data(data, stride, n, work, work_int);
504}
505inline double gslQnFromSortedData(const uint32_t data[], const size_t stride, const size_t n, uint32_t work[],
506 int work_int[])
507{
508 return gsl_stats_uint_Qn_from_sorted_data(data, stride, n, work, work_int);
509}
510inline double gslQnFromSortedData(const int32_t data[], const size_t stride, const size_t n, int32_t work[],
511 int work_int[])
512{
513 return gsl_stats_int_Qn_from_sorted_data(data, stride, n, work, work_int);
514}
515#ifdef GSLHELPERS_INT64
516inline double gslQnFromSortedData(const int64_t data[], const size_t stride, const size_t n, int64_t work[],
517 int work_int[])
518{
519 return gsl_stats_long_Qn_from_sorted_data(data, stride, n, work, work_int);
520}
521#endif
522
523}
524} // namespace Mathematics
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Dec 13 2024 11:47:10 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.