HPCombi
High Performance Combinatorics in C++ using vector instructions v1.0.0
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
39#ifndef HPCOMBI_POWER_HPP_
40#define HPCOMBI_POWER_HPP_
41
42namespace HPCombi {
43
44namespace power_helper {
45
46// Forward declaration
47template <typename T> struct Monoid;
48
49} // namespace power_helper
50
61template <typename T, typename M = power_helper::Monoid<T>>
62const T square(const T x) {
63 return M::prod(x, x);
64}
65
82template <unsigned exp, typename T, typename M = power_helper::Monoid<T>>
83const T pow(const T x) {
84 return (exp == 0) ? M::one()
85 : (exp % 2 == 0)
86 ? square<T, M>(pow<unsigned(exp / 2), T, M>(x))
87 : M::prod(x, square<T, M>(pow<unsigned(exp / 2), T, M>(x)));
88}
89
90namespace power_helper {
91
103template <typename T> struct Monoid {
105 static const T one() { return 1; }
106
112 static const T prod(T a, T b) { return a * b; }
113};
114
115} // namespace power_helper
116
117} // namespace HPCombi
118
119#endif // HPCOMBI_POWER_HPP_
Definition bmat8.hpp:41
const T square(const T x)
A generic compile time squaring function.
Definition power.hpp:62
const T pow(const T x)
A generic compile time exponentiation function.
Definition power.hpp:83
Algebraic monoid structure used by default for type T by the pow function and prod function.
Definition power.hpp:103
static const T prod(T a, T b)
the product of two elements of type T
Definition power.hpp:112
static const T one()
The one of type T.
Definition power.hpp:105