PolyDiM
C++ library for POLYtopal DIscretization Methods
Loading...
Searching...
No Matches
CommonUtilities.hpp
Go to the documentation of this file.
1// _LICENSE_HEADER_
2//
3// Copyright (C) 2019 - 2025.
4// Terms register on the GPL-3.0 license.
5//
6// This file can be redistributed and/or modified under the license terms.
7//
8// See top level LICENSE file for more details.
9//
10// This file can be used citing references in CITATION.cff file.
11
12#ifndef __GEDIM_UTILITIES_H
13#define __GEDIM_UTILITIES_H
14
15#include "IOUtilities.hpp"
16#include <algorithm>
17#include <numeric>
18#include <random>
19
20namespace Gedim
21{
25{
26 public:
28 template <class T> static void Unused(const T &)
29 {
30 }
31
32 static unsigned int RandomSeed()
33 {
34 return static_cast<unsigned int>(time(nullptr));
35 }
36
40 static std::list<std::vector<int>> CombinationWithRepetition(int n, int k);
41
43 template <class container> static void Shuffle(container &array, const unsigned int &seed = time(nullptr))
44 {
45 std::shuffle(array.begin(), array.end(), std::default_random_engine(seed));
46 }
47
51 static std::vector<unsigned int> RandomArrayNoRepetition(const unsigned int &n,
52 const unsigned int &maxNumber = RAND_MAX,
53 const unsigned int &seed = time(nullptr));
54
58 template <typename T> static std::vector<unsigned int> SortArrayIndices(const std::vector<T> &array)
59 {
60 std::vector<unsigned int> indices(array.size());
61 std::iota(begin(indices), end(indices), 0);
62
63 std::sort(indices.begin(), indices.end(), [&array](int a, int b) { return (array.at(a) < array.at(b)); });
64
65 return indices;
66 }
67
73 template <typename _InputIterator>
74 static double Slope(_InputIterator x_begin, _InputIterator y_begin, const unsigned int &n)
75 {
76 const auto s_x = std::accumulate(x_begin, x_begin + n, 0.0);
77 const auto s_y = std::accumulate(y_begin, y_begin + n, 0.0);
78 const auto s_xx = std::inner_product(x_begin, x_begin + n, x_begin, 0.0);
79 const auto s_xy = std::inner_product(x_begin, x_begin + n, y_begin, 0.0);
80 return (n * s_xy - s_x * s_y) / (n * s_xx - s_x * s_x);
81 }
82
84 static double BinomialCoefficient(const unsigned int &n, const unsigned int &k)
85 {
86 // Base Case
87 if (k > n)
88 return 0;
89
90 if (k == 0 || k == n)
91 return 1;
92
93 return BinomialCoefficient(n - 1, k - 1) + BinomialCoefficient(n - 1, k);
94 }
95
96 template <typename T> static T Union(const std::initializer_list<T> &arrays)
97 {
98 std::unordered_set<typename T::value_type> s;
99
100 for (const auto &array : arrays)
101 s.insert(array.begin(), array.end());
102
103 return T(s.begin(), s.end());
104 }
105};
106} // namespace Gedim
107
108#endif // __GEDIM_UTILITIES_H
Eigen column vector.
Definition Eigen_Array.hpp:23
Static class fot generic functions.
Definition CommonUtilities.hpp:25
static std::list< std::vector< int > > CombinationWithRepetition(int n, int k)
create Combination With Repetition with n elements in k subset
Definition CommonUtilities.cpp:19
static std::vector< unsigned int > SortArrayIndices(const std::vector< T > &array)
Gives you back the array sorted indices.
Definition CommonUtilities.hpp:58
static std::vector< unsigned int > RandomArrayNoRepetition(const unsigned int &n, const unsigned int &maxNumber=RAND_MAX, const unsigned int &seed=time(nullptr))
Definition CommonUtilities.cpp:50
static void Unused(const T &)
Tells the compiler the parameter is unused.
Definition CommonUtilities.hpp:28
static double BinomialCoefficient(const unsigned int &n, const unsigned int &k)
Compute the binomial coefficient (n k)
Definition CommonUtilities.hpp:84
static void Shuffle(container &array, const unsigned int &seed=time(nullptr))
Shuffle an array.
Definition CommonUtilities.hpp:43
static unsigned int RandomSeed()
Definition CommonUtilities.hpp:32
static double Slope(_InputIterator x_begin, _InputIterator y_begin, const unsigned int &n)
Compute the regression line slope of points (x,y).
Definition CommonUtilities.hpp:74
static T Union(const std::initializer_list< T > &arrays)
Definition CommonUtilities.hpp:96
Definition Eigen_Array.cpp:22