-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathUtils.hpp
91 lines (74 loc) · 3.38 KB
/
Utils.hpp
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
/* Copyright(C) ga-developers
*
* Repository: https://github.com/ga-developers/ga-benchmark.git
*
* This file is part of the GA-Benchmark project.
*
* GA-Benchmark is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* GA-Benchmark is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GA-Benchmark. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef __GABM_UTILS_HPP__
#define __GABM_UTILS_HPP__
template<gabm::grade_t Grade>
decltype(auto) MakeBlade(gabm::real_t, gabm::factors_list_t const &);
template<gabm::grade_t Grade, typename ArgumentType>
gabm::real_t SquaredReverseNorm(ArgumentType const &);
namespace gabm {
const static real_t pi = std::acos(static_cast<real_t>(-1));
const static real_t two_pi = 2 * pi;
real_t MakeRandomAngle(std::default_random_engine &random_engine) {
static std::uniform_real_distribution<real_t> uniform_distribution(0, two_pi);
return uniform_distribution(random_engine);
}
template<grade_t Grade>
decltype(auto) MakeRandomBlade(std::default_random_engine &random_engine) {
static std::uniform_real_distribution<real_t> uniform_distribution(-1, +1);
real_t scalar = uniform_distribution(random_engine);
factors_list_t factors;
factors.reserve(Grade);
for (std::size_t factor_ind = 0; factor_ind != Grade; ++factor_ind) {
vector_coords_t factor{};
factor[factor_ind] = 1;
for (std::size_t coord_ind = Grade; coord_ind != GABM_N_DIMENSIONS; ++coord_ind) {
factor[coord_ind] = uniform_distribution(random_engine);
}
factors.push_back(factor);
}
return MakeBlade<Grade>(scalar, factors);
}
template<grade_t Grade>
decltype(auto) MakeRandomInvertibleBlade(std::default_random_engine &random_engine) {
auto result = MakeRandomBlade<Grade>(random_engine);
while (std::abs(SquaredReverseNorm<Grade>(result)) <= GABM_ZERO_TOLERANCE) {
result = MakeRandomBlade<Grade>(random_engine);
}
return result;
}
template<typename... RandomEntryGeneratorClasses>
decltype(auto) WrapRandomArguments(RandomEntries<RandomEntryGeneratorClasses> const &... args) {
std::vector<std::tuple<typename RandomEntries<RandomEntryGeneratorClasses>::const_reference ...> > result;
result.reserve(GABM_ITERATIONS);
for (std::size_t ind = 0; ind != (GABM_ITERATIONS); ++ind) {
result.emplace_back(args[ind]...);
}
return result;
}
}
#define GABM_DEFINE_MAKE_BLADE(SCALAR_FACTOR, VECTOR_FACTORS, GRADE) \
template<gabm::grade_t GRADE> \
decltype(auto) MakeBlade(gabm::real_t SCALAR_FACTOR, gabm::factors_list_t const &VECTOR_FACTORS)
#define GABM_DEFINE_SQUARED_REVERSE_NORM(ARG, GRADE) \
template<gabm::grade_t GRADE, typename ArgumentType> \
gabm::real_t SquaredReverseNorm(ArgumentType const &ARG)
#include <SpecializedUtils.hpp>
#endif // __GABM_UTILS_HPP__