HPCombi
High Performance Combinatorics in C++ using vector instructions v1.0.3
Loading...
Searching...
No Matches
power.hpp
Go to the documentation of this file.
1//****************************************************************************//
2// Copyright (C) 2016-2024 Florent Hivert <Florent.Hivert@lisn.fr>, //
3// //
4// This file is part of HP-Combi <https://github.com/libsemigroups/HPCombi> //
5// //
6// HP-Combi is free software: you can redistribute it and/or modify it //
7// under the terms of the GNU General Public License as published by the //
8// Free Software Foundation, either version 3 of the License, or //
9// (at your option) any later version. //
10// //
11// HP-Combi is distributed in the hope that it will be useful, but WITHOUT //
12// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or //
13// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License //
14// for more details. //
15// //
16// You should have received a copy of the GNU General Public License along //
17// with HP-Combi. If not, see <https://www.gnu.org/licenses/>. //
18//****************************************************************************//
19
46
47#ifndef HPCOMBI_POWER_HPP_
48#define HPCOMBI_POWER_HPP_
49
50namespace HPCombi {
51
52namespace power_helper {
53
54// Forward declaration
55template <typename T> struct Monoid;
56
57} // namespace power_helper
58
69template <typename T, typename M = power_helper::Monoid<T>>
70const T square(const T x) {
71 return M::prod(x, x);
72}
73
90template <unsigned exp, typename T, typename M = power_helper::Monoid<T>>
91const T pow(const T x) {
92 return (exp == 0) ? M::one()
93 : (exp % 2 == 0)
94 ? square<T, M>(pow<unsigned(exp / 2), T, M>(x))
95 : M::prod(x, square<T, M>(pow<unsigned(exp / 2), T, M>(x)));
96}
97
98namespace power_helper {
99
111template <typename T> struct Monoid {
113 static const T one() { return 1; }
114
120 static const T prod(T a, T b) { return a * b; }
121};
122
123} // namespace power_helper
124
125} // namespace HPCombi
126
127#endif // HPCOMBI_POWER_HPP_
Definition perm16_impl.hpp:236
Definition bmat16.hpp:39
const T square(const T x)
A generic compile time squaring function.
Definition power.hpp:70
const T pow(const T x)
A generic compile time exponentiation function.
Definition power.hpp:91
Algebraic monoid structure used by default for type T by the pow function and prod function.
Definition power.hpp:111
static const T prod(T a, T b)
the product of two elements of type T
Definition power.hpp:120
static const T one()
The one of type T.
Definition power.hpp:113