libsemigroups  v3.5.1
C++ library for semigroups and monoids
Loading...
Searching...
No Matches
transf.hpp
1//
2// libsemigroups - C++ library for semigroups and monoids
3// Copyright (C) 2021-2026 James D. Mitchell
4//
5// This program is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// This program is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with this program. If not, see <http://www.gnu.org/licenses/>.
17//
18
19// This file contains the declaration of the partial transformation class and
20// its subclasses.
21
22// TODO(later)
23// * benchmarks
24// * add some tests for PTransf themselves
25// * allocator
26
27#ifndef LIBSEMIGROUPS_TRANSF_HPP_
28#define LIBSEMIGROUPS_TRANSF_HPP_
29
30#include <algorithm> // for sort, max_element, unique
31#include <array> // for array
32#include <cstddef> // for size_t
33#include <cstdint> // for uint64_t, uint32_t
34#include <initializer_list> // for initializer_list
35#include <iterator> // for distance
36#include <limits> // for numeric_limits
37#include <numeric> // for iota
38#include <tuple> // for tuple_size
39#include <type_traits> // for enable_if_t
40#include <unordered_map> // for unordered_map
41#include <unordered_set> // for unordered_set
42#include <utility> // for forward
43#include <vector> // for vector
44
45#include "config.hpp" // for LIBSEMIGROUPS_HPCOMBI_ENABLED
46
47#include "adapters.hpp" // for Hash etc
48#include "bitset.hpp" // for BitSet
49#include "constants.hpp" // for UNDEFINED, Undefined
50#include "debug.hpp" // for LIBSEMIGROUPS_ASSERT
51#include "exception.hpp" // for LIBSEMIGROUPS_EXCEPTION
52#include "hpcombi.hpp" // for HPCombi::Transf16
53#include "is-transf.hpp" // for throw_if_not_perm etc
54#include "is_specialization_of.hpp" // for is_specialization_of
55#include "types.hpp" // for SmallestInteger
56
57#include "detail/stl.hpp" // for is_array_v
58
59namespace libsemigroups {
69
70 namespace detail {
75 } // namespace detail
76
91 // TODO(1) example
92
100 template <typename T>
101 static constexpr bool IsDerivedFromPTransf
102 = std::is_base_of_v<detail::PTransfPolymorphicBase, T>;
103
104 namespace detail {
105
106 template <typename T>
107 struct IsStaticHelper : std::false_type {};
108
109 template <typename T>
110 struct IsDynamicHelper : std::false_type {};
111
112 } // namespace detail
113
127 template <typename Scalar, typename Container>
129 static_assert(std::is_integral_v<Scalar>,
130 "template parameter Scalar must be an integral type");
131 static_assert(std::is_unsigned_v<Scalar>,
132 "template parameter Scalar must be unsigned");
133
134 public:
138 using point_type = Scalar;
139
143 using container_type = Container;
144
145 // Required by python bindings
156 [[nodiscard]] static point_type undef() noexcept {
157 return static_cast<point_type>(UNDEFINED);
158 }
159
169 PTransfBase() = default;
170
171 // No constructor from size_t this is delegated to StaticPTransf and
172 // DynamicPTransf
173
194 explicit PTransfBase(Container const& cont) : _container(cont) {}
195
197 explicit PTransfBase(Container&& cont) : _container(std::move(cont)) {}
198
219 template <typename Iterator>
220 explicit PTransfBase(Iterator first, Iterator last) : PTransfBase() {
221 using OtherScalar = typename std::iterator_traits<Iterator>::value_type;
222 // The below assertions exist to insure that we are not badly assigning
223 // values. The subsequent pragmas exist to suppress the false-positive
224 // warnings produced by g++ 13.2.0
225 static_assert(
226 std::is_same_v<OtherScalar, Undefined>
227 || std::is_convertible_v<OtherScalar, point_type>,
228 "the template parameter Iterator must have "
229 "value_type \"Undefined\" or convertible to \"point_type\"!");
230 static_assert(std::is_same_v<std::decay_t<decltype(*_container.begin())>,
231 point_type>);
232 resize(_container, std::distance(first, last));
233#pragma GCC diagnostic push
234#if defined(__GNUC__) && !defined(__clang__)
235#pragma GCC diagnostic ignored "-Wstringop-overflow"
236#endif
237 std::copy(first, last, _container.begin());
238#pragma GCC diagnostic pop
239 }
240
244
266 template <typename Subclass, typename OtherContainer = Container>
267 [[nodiscard]] static Subclass make(OtherContainer&& cont);
268
289 template <typename Subclass, typename OtherScalar>
290 [[nodiscard]] static Subclass make(std::initializer_list<OtherScalar> cont);
291
295 PTransfBase(PTransfBase const&) = default;
296
301
306
311
327 [[nodiscard]] bool operator<(PTransfBase const& that) const {
328 return _container < that._container;
329 }
330
346 [[nodiscard]] bool operator>(PTransfBase const& that) const {
347 return that < *this;
348 }
349
365 [[nodiscard]] bool operator==(PTransfBase const& that) const {
366 return _container == that._container;
367 }
368
384 [[nodiscard]] bool operator<=(PTransfBase const& that) const {
385 return _container < that._container || _container == that._container;
386 }
387
403 [[nodiscard]] bool operator>=(PTransfBase const& that) const {
404 return that <= *this;
405 }
406
422 [[nodiscard]] bool operator!=(PTransfBase const& that) const {
423 return !(*this == that);
424 }
425
440 [[nodiscard]] point_type& operator[](size_t i) {
441 return _container[i];
442 }
443
458 [[nodiscard]] point_type const& operator[](size_t i) const {
459 return _container[i];
460 }
461
475 [[nodiscard]] point_type& at(size_t i);
476
490 [[nodiscard]] point_type const& at(size_t i) const {
491 return const_cast<PTransfBase&>(*this).at(i);
492 }
493
514 // TODO(later) other operators such as power
515 template <typename Subclass>
516 [[nodiscard]] Subclass operator*(Subclass const& that) const;
517
521 using iterator = typename Container::iterator;
522
526 using const_iterator = typename Container::const_iterator;
527
539 [[nodiscard]] const_iterator cbegin() const noexcept {
540 return _container.cbegin();
541 }
542
554 [[nodiscard]] const_iterator cend() const noexcept {
555 return _container.cend();
556 }
557
559 [[nodiscard]] const_iterator begin() const noexcept {
560 return _container.begin();
561 }
562
564 [[nodiscard]] const_iterator end() const noexcept {
565 return _container.end();
566 }
567
579 [[nodiscard]] iterator begin() noexcept {
580 return _container.begin();
581 }
582
594 [[nodiscard]] iterator end() noexcept {
595 return _container.end();
596 }
597
611 [[nodiscard]] size_t rank() const {
613 return (vals.find(UNDEFINED) == vals.end() ? vals.size()
614 : vals.size() - 1);
615 }
616
627 // not noexcept because Hash<T>::operator() isn't
628 [[nodiscard]] size_t hash_value() const {
629 return Hash<Container>()(_container);
630 }
631
638 void swap(PTransfBase& that) noexcept {
639 std::swap(_container, that._container);
640 }
641
653 [[nodiscard]] size_t degree() const noexcept {
654 return _container.size();
655 }
656
670 template <typename Subclass>
671 [[nodiscard]] static Subclass one(size_t N) {
672 static_assert(IsDerivedFromPTransf<Subclass>,
673 "the template parameter Subclass must be derived from "
674 "PTransfPolymorphicBase");
675 Subclass result(N);
676 std::iota(result.begin(), result.end(), 0);
677 return result;
678 }
679
680 protected:
681 static void resize(container_type& c, size_t N, point_type val = 0);
682 void resize(size_t N, point_type val = 0) {
683 resize(_container, N, val);
684 }
685
686 private:
687 Container _container;
688 };
689
691 // Helper variable templates
693
701 template <typename T>
702 static constexpr bool IsStatic = detail::IsStaticHelper<T>::value;
703
711 template <typename T>
712 static constexpr bool IsDynamic = detail::IsDynamicHelper<T>::value;
713
715 // DynamicPTransf
717
730 template <typename Scalar>
731 class DynamicPTransf : public PTransfBase<Scalar, std::vector<Scalar>> {
733
734 public:
740 using point_type = Scalar;
741
748
750 using base_type::begin;
751 using base_type::degree;
752 using base_type::end;
753
754 // No default constructor, because the degree would be 0, and so we can
755 // just use the PTransfBase default constructor for that. Note that there's
756 // a default constructor for StaticPTransf since there we do know the degree
757 // (at compile time) and we can fill it with UNDEFINED values.
758
771 explicit DynamicPTransf(size_t n) : base_type() {
772 resize(n, UNDEFINED);
773 }
774
790
810 resize(degree() + m);
811 std::iota(end() - m, end(), degree() - m);
812 return *this;
813 }
814
815 protected:
816 using base_type::resize;
817 };
818
820 // StaticPTransf
822
835 template <size_t N, typename Scalar>
836 class StaticPTransf : public PTransfBase<Scalar, std::array<Scalar, N>> {
838
839 public:
841 using point_type = Scalar;
842
847
849 using base_type::begin;
850 using base_type::end;
851
862 StaticPTransf() : base_type() {
864 }
865
876 explicit StaticPTransf(size_t n);
877
884 // do nothing can't increase the degree
885 LIBSEMIGROUPS_EXCEPTION("cannot increase the degree of a StaticPTransf!");
886 return *this;
887 }
888 };
889
891 // PTransf
893
911 template <
912 size_t N = 0,
913 typename Scalar
914 = std::conditional_t<N == 0, uint32_t, typename SmallestInteger<N>::type>>
915 using PTransf = std::
916 conditional_t<N == 0, DynamicPTransf<Scalar>, StaticPTransf<N, Scalar>>;
917
918 namespace detail {
919 template <typename T>
920 struct IsPTransfHelper : std::false_type {};
921
922 template <typename Scalar>
923 struct IsPTransfHelper<DynamicPTransf<Scalar>> : std::true_type {};
924
925 template <size_t N, typename Scalar>
926 struct IsPTransfHelper<StaticPTransf<N, Scalar>> : std::true_type {};
927
928 template <size_t N, typename Scalar>
929 struct IsStaticHelper<StaticPTransf<N, Scalar>> : std::true_type {};
930
931 template <typename Scalar>
932 struct IsDynamicHelper<DynamicPTransf<Scalar>> : std::true_type {};
933
934 } // namespace detail
935
946 template <typename T>
947 static constexpr bool IsPTransf = detail::IsPTransfHelper<T>::value;
948
966 template <typename Scalar, typename Container>
967 [[deprecated]] void
969
971 // Transf
973
1000 template <
1001 size_t N = 0,
1002 typename Scalar
1003 = std::conditional_t<N == 0, uint32_t, typename SmallestInteger<N>::type>>
1004 class Transf : public PTransf<N, Scalar> {
1005 using base_type = PTransf<N, Scalar>;
1006
1007 public:
1011 using point_type = Scalar;
1012
1016 using container_type = typename base_type::container_type;
1017
1018 using PTransf<N, Scalar>::PTransf;
1019 using base_type::degree;
1020
1040 void product_inplace(Transf const& f, Transf const& g);
1041
1055 [[nodiscard]] static Transf one(size_t M) {
1056 return base_type::template one<Transf>(M);
1057 }
1058 };
1059
1060 namespace detail {
1061 template <typename T>
1062 struct IsTransfHelper : std::false_type {};
1063
1064 template <size_t N, typename Scalar>
1065 struct IsTransfHelper<Transf<N, Scalar>> : std::true_type {};
1066
1067 template <size_t N, typename Scalar>
1068 struct IsStaticHelper<Transf<N, Scalar>>
1069 : IsStaticHelper<PTransf<N, Scalar>> {};
1070
1071 template <size_t N, typename Scalar>
1072 struct IsDynamicHelper<Transf<N, Scalar>>
1073 : IsDynamicHelper<PTransf<N, Scalar>> {};
1074 } // namespace detail
1075
1077 // Transf helpers
1079
1087 template <typename T>
1088 static constexpr bool IsTransf = detail::IsTransfHelper<T>::value;
1089
1091 // Transf throw_if_image_value_out_of_range
1093
1109 template <size_t N, typename Scalar>
1110 [[deprecated]] void
1112
1114 // make<Transf>
1116
1127
1150 template <typename Return, typename OtherContainer>
1151 [[nodiscard]] std::enable_if_t<IsTransf<Return>, Return>
1152 make(OtherContainer&& cont) {
1153 return Return::template make<Return>(std::forward<OtherContainer>(cont));
1154 }
1155
1179 template <typename Return, typename OtherScalar>
1180 [[nodiscard]] std::enable_if_t<IsTransf<Return>, Return>
1184
1186 // PPerm
1188
1216 template <
1217 size_t N = 0,
1218 typename Scalar
1219 = std::conditional_t<N == 0, uint32_t, typename SmallestInteger<N>::type>>
1220 class PPerm : public PTransf<N, Scalar> {
1221 using base_type = PTransf<N, Scalar>;
1222
1223 public:
1227 using point_type = Scalar;
1228
1232 using container_type = typename base_type::container_type;
1233
1234 using PTransf<N, point_type>::PTransf;
1235 using base_type::degree;
1236 using base_type::undef;
1237
1257 //
1258 // Note: we use vectors here not container_type (which might be array),
1259 // because the length of dom and img might not equal degree().
1260 // Also we don't use a universal reference because we can't actually use an
1261 // rvalue reference here (we don't store dom or img).
1262 template <typename OtherScalar>
1264 std::vector<OtherScalar> const& img,
1265 size_t M);
1266
1271 size_t M)
1272 : PPerm(std::vector<point_type>(dom), std::vector<point_type>(img), M) {
1273 }
1274
1291 void product_inplace(PPerm const& f, PPerm const& g);
1292
1294 [[nodiscard]] static PPerm one(size_t M) {
1295 return base_type::template one<PPerm>(M);
1296 }
1297 };
1298
1299 namespace detail {
1300
1302 // PPerm helpers
1304
1305 template <typename T>
1306 struct IsPPermHelper : std::false_type {};
1307
1308 template <size_t N, typename Scalar>
1309 struct IsPPermHelper<PPerm<N, Scalar>> : std::true_type {};
1310
1311 template <size_t N, typename Scalar>
1312 struct IsStaticHelper<PPerm<N, Scalar>>
1313 : IsStaticHelper<PTransf<N, Scalar>> {};
1314
1315 template <size_t N, typename Scalar>
1316 struct IsDynamicHelper<PPerm<N, Scalar>>
1317 : IsDynamicHelper<PTransf<N, Scalar>> {};
1318 } // namespace detail
1319
1327 template <typename T>
1328 static constexpr bool IsPPerm = detail::IsPPermHelper<T>::value;
1329
1331 // PPerm check
1333
1350 template <size_t N, typename Scalar>
1351 [[deprecated]] void throw_if_not_pperm(PPerm<N, Scalar> const& f) {
1353 detail::throw_if_duplicates(f.begin(), f.end(), "image");
1354 }
1355
1357 // make<PPerm>
1359
1370
1394 template <typename Return, typename OtherContainer>
1395 [[nodiscard]] std::enable_if_t<IsPPerm<Return>, Return>
1396 make(OtherContainer&& cont) {
1397 return Return::template make<Return>(std::forward<OtherContainer>(cont));
1398 }
1399
1421 template <typename Return>
1422 [[nodiscard]] std::enable_if_t<IsPPerm<Return>, Return>
1427
1451 template <typename Return>
1452 [[nodiscard]] std::enable_if_t<IsPPerm<Return>, Return>
1455 size_t M);
1456
1480 template <typename Return>
1481 [[nodiscard]] std::enable_if_t<IsPPerm<Return>, Return>
1489
1491 // Perm
1493
1520 template <
1521 size_t N = 0,
1522 typename Scalar
1523 = std::conditional_t<N == 0, uint32_t, typename SmallestInteger<N>::type>>
1524 class Perm : public Transf<N, Scalar> {
1525 using base_type = PTransf<N, Scalar>;
1526
1527 public:
1531 using point_type = Scalar;
1532
1537
1538 using Transf<N, Scalar>::Transf;
1539 using base_type::degree;
1540
1542 [[nodiscard]] static Perm one(size_t M) {
1543 return base_type::template one<Perm>(M);
1544 }
1545 };
1546
1548 // Perm helpers
1550
1551 namespace detail {
1552 template <typename T>
1553 struct IsPermHelper : std::false_type {};
1554
1555 template <size_t N, typename Scalar>
1556 struct IsPermHelper<Perm<N, Scalar>> : std::true_type {};
1557
1558 template <size_t N, typename Scalar>
1559 struct IsStaticHelper<Perm<N, Scalar>>
1560 : IsStaticHelper<PTransf<N, Scalar>> {};
1561
1562 template <size_t N, typename Scalar>
1563 struct IsDynamicHelper<Perm<N, Scalar>>
1564 : IsDynamicHelper<PTransf<N, Scalar>> {};
1565 } // namespace detail
1566
1574 template <typename T>
1575 static constexpr bool IsPerm = detail::IsPermHelper<T>::value;
1576
1578 // Perm throw_if_not_perm
1580
1597 template <size_t N, typename Scalar>
1598 [[deprecated]] void throw_if_not_perm(Perm<N, Scalar> const& f) {
1600 detail::throw_if_duplicates(f.begin(), f.end(), "image");
1601 }
1602
1604 // make<Perm>
1606
1617
1641 template <typename Return, typename OtherContainer>
1642 [[nodiscard]] std::enable_if_t<IsPerm<Return>, Return>
1643 make(OtherContainer&& cont) {
1644 return Return::template make<Return>(std::forward<OtherContainer>(cont));
1645 }
1646
1669 template <typename Return>
1670 [[nodiscard]] std::enable_if_t<IsPerm<Return>, Return>
1675
1677 // Helper functions
1679
1706 template <typename T, typename Scalar>
1707 void image(T const& f, std::vector<Scalar>& im);
1708
1730 template <typename T>
1732
1759 template <typename T, typename Scalar>
1760 void domain(T const& f, std::vector<Scalar>& dom);
1761
1783 template <typename T>
1785
1804 template <typename T>
1805 [[nodiscard]] auto one(T const& f)
1806 -> std::enable_if_t<IsDerivedFromPTransf<T>, T> {
1807 return T::one(f.degree());
1808 }
1809
1827 template <size_t N, typename Scalar>
1829 // TODO(1) void pass by reference version
1830
1848 template <size_t N, typename Scalar>
1850 // TODO(1) void pass by reference version
1851
1867 // Put the inverse of this into that
1868 template <size_t N, typename Scalar>
1870
1893 template <size_t N, typename Scalar>
1895 PPerm<N, Scalar> to(f.degree());
1896 inverse(f, to);
1897 return to;
1898 }
1899
1918 template <size_t N, typename Scalar>
1920
1941 template <size_t N, typename Scalar>
1942 [[nodiscard]] Perm<N, Scalar> inverse(Perm<N, Scalar> const& f);
1943
1945 // Adapters
1947
1948 template <typename T>
1949 struct Degree<T, std::enable_if_t<IsDerivedFromPTransf<T>>> {
1950 [[nodiscard]] constexpr size_t operator()(T const& x) const noexcept {
1951 return x.degree();
1952 }
1953 };
1954
1955 template <typename T>
1956 struct One<T, std::enable_if_t<IsDerivedFromPTransf<T>>> {
1957 [[nodiscard]] T operator()(T const& x) const {
1958 return (*this)(x.degree());
1959 }
1960
1961 [[nodiscard]] T operator()(size_t N) const {
1962 return T::one(N);
1963 }
1964 };
1965
1966 template <size_t N, typename Scalar>
1967 struct Inverse<Perm<N, Scalar>> {
1968 [[nodiscard]] Perm<N, Scalar> operator()(Perm<N, Scalar> const& x) {
1969 return inverse(x);
1970 }
1971 };
1972
1973 template <typename Subclass>
1974 struct Product<Subclass, std::enable_if_t<IsDerivedFromPTransf<Subclass>>> {
1975 void
1976 operator()(Subclass& xy, Subclass const& x, Subclass const& y, size_t = 0) {
1977 xy.product_inplace(x, y);
1978 }
1979 };
1980
1981 template <typename T>
1982 struct Hash<T, std::enable_if_t<IsDerivedFromPTransf<T>>> {
1983 [[nodiscard]] constexpr size_t operator()(T const& x) const {
1984 return x.hash_value();
1985 }
1986 };
1987
1988 template <typename T>
1989 struct Complexity<T, std::enable_if_t<IsDerivedFromPTransf<T>>> {
1990 [[nodiscard]] constexpr size_t operator()(T const& x) const noexcept {
1991 return x.degree();
1992 }
1993 };
1994
1999 template <typename T>
2000 struct IncreaseDegree<T, std::enable_if_t<IsDerivedFromPTransf<T>>> {
2002 inline void operator()(T& x, size_t n) const {
2003 x.increase_degree_by(n);
2004 }
2005 };
2006
2008 // ImageRight/LeftAction - Transf
2010
2011 // Equivalent to OnSets in GAP
2012 // Slowest
2013 // works for T = std::vector and T = StaticVector1
2018 template <size_t N, typename Scalar, typename T>
2019 struct ImageRightAction<Transf<N, Scalar>, T> {
2021 void operator()(T& res, T const& pt, Transf<N, Scalar> const& x) const;
2022 };
2023
2024 // Fastest, but limited to at most degree 64
2029 template <size_t N, typename Scalar, size_t M>
2030 struct ImageRightAction<Transf<N, Scalar>, BitSet<M>> {
2032 void operator()(BitSet<M>& res,
2033 BitSet<M> const& pt,
2034 Transf<N, Scalar> const& x) const {
2035 res.reset();
2036 // Apply the lambda to every set bit in pt
2037 pt.apply([&x, &res](size_t i) { res.set(x[i]); });
2038 }
2039 };
2040
2041 // OnKernelAntiAction
2042 // T = StaticVector1<S> or std::vector<S>
2047 template <size_t N, typename Scalar, typename T>
2048 struct ImageLeftAction<Transf<N, Scalar>, T> {
2050 void operator()(T& res, T const& pt, Transf<N, Scalar> const& x) const;
2051 };
2052
2054 // Lambda/Rho - Transformation
2056
2057 // This currently limits the use of Konieczny to transformation of degree at
2058 // most 64 with the default traits class, since we cannot know the degree at
2059 // compile time, only at run time.
2066 template <size_t N, typename Scalar>
2067 struct LambdaValue<Transf<N, Scalar>> {
2070 using type = BitSet<BitSet<1>::max_size()>;
2071 };
2072
2073 // Benchmarks indicate that using std::vector yields similar performance to
2074 // using StaticVector1's.
2078 template <size_t N, typename Scalar>
2079 struct RhoValue<Transf<N, Scalar>> {
2083 };
2084
2085 // T = std::vector or StaticVector1
2090 template <size_t N, typename Scalar, typename T>
2091 struct Lambda<Transf<N, Scalar>, T> {
2092 // not noexcept because std::vector::resize isn't (although
2093 // StaticVector1::resize is).
2096 void operator()(T& res, Transf<N, Scalar> const& x) const;
2097 };
2098
2103 template <size_t N, typename Scalar, size_t M>
2104 struct Lambda<Transf<N, Scalar>, BitSet<M>> {
2105 // not noexcept because it can throw
2108 void operator()(BitSet<M>& res, Transf<N, Scalar> const& x) const;
2109 };
2110
2111 // T = std::vector<S> or StaticVector1<S, N>
2116 template <size_t N, typename Scalar, typename T>
2117 struct Rho<Transf<N, Scalar>, T> {
2129 // not noexcept because std::vector::resize isn't (although
2130 // StaticVector1::resize is).
2131 void operator()(T& res, Transf<N, Scalar> const& x) const;
2132 };
2133
2137 template <size_t N, typename Scalar>
2138 struct Rank<Transf<N, Scalar>> {
2151 [[nodiscard]] size_t operator()(Transf<N, Scalar> const& x) const {
2152 return x.rank();
2153 }
2154 };
2155
2157 // ImageRight/LeftAction - PPerm
2159
2160 // Slowest
2165 template <size_t N, typename Scalar>
2166 struct ImageRightAction<PPerm<N, Scalar>, PPerm<N, Scalar>> {
2169 PPerm<N, Scalar> const& pt,
2170 PPerm<N, Scalar> const& x) const noexcept {
2171 LIBSEMIGROUPS_ASSERT(res.degree() == pt.degree());
2172 LIBSEMIGROUPS_ASSERT(res.degree() == x.degree());
2173 res.product_inplace(pt, x);
2174 res = right_one(res);
2175 }
2176 };
2177
2178 // Faster than the above, but slower than the below
2179 // works for T = std::vector and T = StaticVector1
2184 template <size_t N, typename Scalar, typename T>
2185 struct ImageRightAction<PPerm<N, Scalar>, T> {
2187 void operator()(T& res, T const& pt, PPerm<N, Scalar> const& x) const;
2188 };
2189
2190 // Fastest, but limited to at most degree 64
2195 template <size_t N, typename Scalar, size_t M>
2196 struct ImageRightAction<PPerm<N, Scalar>, BitSet<M>> {
2198 void operator()(BitSet<M>& res,
2199 BitSet<M> const& pt,
2200 PPerm<N, Scalar> const& x) const;
2201 };
2202
2203 // Slowest
2208 template <size_t N, typename Scalar>
2209 struct ImageLeftAction<PPerm<N, Scalar>, PPerm<N, Scalar>> {
2213 PPerm<N, Scalar> const& pt,
2214 PPerm<N, Scalar> const& x) const noexcept {
2215 res.product_inplace(x, pt);
2216 res = left_one(res);
2217 }
2218 };
2219
2220 // Fastest when used with BitSet<N>.
2221 // works for T = std::vector and T = BitSet<N>
2222 // Using BitSet<N> limits this to size 64. However, if we are trying to
2223 // compute a LeftAction object, then the max size of such is 2 ^ 64, which
2224 // is probably not achievable. So, for higher degrees, we will only be able
2225 // to compute relatively sparse LeftActions (i.e. not containing the
2226 // majority of the 2 ^ n possible subsets), in which case using vectors or
2227 // StaticVector1's might be not be appreciable slower anyway. All of this is
2228 // to say that it probably isn't worthwhile trying to make BitSet's work for
2229 // more than 64 bits.
2234 template <size_t N, typename Scalar, typename T>
2235 struct ImageLeftAction<PPerm<N, Scalar>, T> {
2236 void operator()(T& res, T const& pt, PPerm<N, Scalar> const& x) const {
2238 static PPerm<N, Scalar> xx(x.degree());
2239 inverse(x, xx); // invert x into xx
2240 ImageRightAction<PPerm<N, Scalar>, T>()(res, pt, xx);
2241 }
2242 };
2243
2245 // Lambda/Rho - PPerm
2247
2248 // This currently limits the use of Konieczny to partial perms of degree at
2249 // most 64 with the default traits class, since we cannot know the degree
2250 // at compile time, only at run time.
2256 template <size_t N, typename Scalar>
2257 struct LambdaValue<PPerm<N, Scalar>> {
2260 using type = BitSet<BitSet<1>::max_size()>;
2261 };
2262
2268 template <size_t N, typename Scalar>
2269 struct RhoValue<PPerm<N, Scalar>> {
2273 };
2274
2279 template <size_t N, typename Scalar, size_t M>
2280 struct Lambda<PPerm<N, Scalar>, BitSet<M>> {
2283 void operator()(BitSet<M>& res, PPerm<N, Scalar> const& x) const;
2284 };
2285
2290 template <size_t N, typename Scalar, size_t M>
2291 struct Rho<PPerm<N, Scalar>, BitSet<M>> {
2294 void operator()(BitSet<M>& res, PPerm<N, Scalar> const& x) const;
2295 };
2296
2300 template <size_t N, typename Scalar>
2301 struct Rank<PPerm<N, Scalar>> {
2305 [[nodiscard]] size_t operator()(PPerm<N, Scalar> const& x) const {
2306 return x.rank();
2307 }
2308 };
2309
2311 // Perm
2313
2318 // TODO(later) this could work for everything derived from PTransf
2319 template <size_t N, typename Scalar, typename T>
2320 struct ImageRightAction<Perm<N, Scalar>,
2321 T,
2322 std::enable_if_t<std::is_integral_v<T>>> {
2324 void operator()(T& res,
2325 T const& pt,
2326 Perm<N, Scalar> const& p) const noexcept {
2327 LIBSEMIGROUPS_ASSERT(pt < p.degree());
2328 res = p[pt];
2329 }
2330
2332 [[nodiscard]] T operator()(T pt, Perm<N, Scalar> const& x) {
2333 return x[pt];
2334 }
2335 };
2336
2338 // Helpers
2340
2341 namespace detail {
2342 template <size_t N>
2343 struct LeastTransfHelper {
2344#ifdef LIBSEMIGROUPS_HPCOMBI_ENABLED
2345 using type = std::conditional_t<N >= 17, Transf<N>, HPCombi::Transf16>;
2346#else
2347 using type = Transf<N>;
2348#endif
2349 };
2350
2351 template <size_t N>
2352 struct LeastPPermHelper {
2353#ifdef LIBSEMIGROUPS_HPCOMBI_ENABLED
2354 using type = std::conditional_t<N >= 17, PPerm<N>, HPCombi::PPerm16>;
2355#else
2356 using type = PPerm<N>;
2357#endif
2358 };
2359
2360 template <size_t N>
2361 struct LeastPermHelper {
2362#ifdef LIBSEMIGROUPS_HPCOMBI_ENABLED
2363 using type = std::conditional_t<N >= 17, Perm<N>, HPCombi::Perm16>;
2364#else
2365 using type = Perm<N>;
2366#endif
2367 };
2368 } // namespace detail
2369
2382 template <size_t N>
2383 using LeastTransf = typename detail::LeastTransfHelper<N>::type;
2384
2397 template <size_t N>
2398 using LeastPPerm = typename detail::LeastPPermHelper<N>::type;
2399
2412 template <size_t N>
2413 using LeastPerm = typename detail::LeastPermHelper<N>::type;
2414
2439 template <size_t N, typename Scalar>
2441 std::string_view prefix = "",
2442 std::string_view braces = "{}");
2443
2468 template <size_t N, typename Scalar>
2470 std::string_view prefix = "",
2471 std::string_view braces = "{}");
2472
2498 template <size_t N, typename Scalar>
2500 std::string_view prefix = "",
2501 std::string_view braces = "{}");
2502
2532 template <size_t N, typename Scalar>
2534 std::string_view prefix = "",
2535 std::string_view braces
2536 = "{}",
2537 size_t max_width = 72);
2538
2567 template <size_t N, typename Scalar>
2569 std::string_view prefix = "",
2570 std::string_view braces
2571 = "{}",
2572 size_t max_width = 72);
2573
2604 template <size_t N, typename Scalar>
2606 std::string_view prefix = "",
2607 std::string_view braces
2608 = "{}",
2609 size_t max_width = 72);
2610} // namespace libsemigroups
2611
2612#include "transf.tpp"
2613
2614#endif // LIBSEMIGROUPS_TRANSF_HPP_
Partial transformations with dynamic degree.
Definition transf.hpp:731
DynamicPTransf(size_t n)
Construct with given degree.
Definition transf.hpp:771
size_t degree() const noexcept
Returns the degree of a partial transformation.
Definition transf.hpp:653
const_iterator end() const noexcept
Returns a const_iterator (random access iterator) pointing one past the last image value.
Definition transf.hpp:564
DynamicPTransf & increase_degree_by(size_t m)
Increase the degree in-place.
Scalar point_type
Type of the image values.
Definition transf.hpp:740
DynamicPTransf & increase_degree_by_no_checks(size_t m)
Increase the degree in-place.
Definition transf.hpp:809
std::vector< point_type > container_type
Type of the underlying container.
Definition transf.hpp:747
Partial permutations with static or dynamic degree.
Definition transf.hpp:1220
PPerm(std::initializer_list< point_type > dom, std::initializer_list< point_type > img, size_t M)
Construct from domain, range, and degree.
Definition transf.hpp:1269
PPerm(std::vector< OtherScalar > const &dom, std::vector< OtherScalar > const &img, size_t M)
Construct from domain, range, and degree.
Scalar point_type
Type of the image values.
Definition transf.hpp:1227
void product_inplace(PPerm const &f, PPerm const &g)
Multiply two partial perms and store the product in this.
static PPerm one(size_t M)
Returns the identity transformation on the given number of points.
Definition transf.hpp:1294
typename base_type::container_type container_type
Type of the underlying container.
Definition transf.hpp:1232
Base class for partial transformations.
Definition transf.hpp:128
typename Container::iterator iterator
Type of iterators pointing to image values.
Definition transf.hpp:521
bool operator==(PTransfBase const &that) const
Compare for equality.
Definition transf.hpp:365
Subclass operator*(Subclass const &that) const
Multiply by another partial transformation.
size_t rank() const
Returns the number of distinct image values.
Definition transf.hpp:611
typename Container::const_iterator const_iterator
Type of const iterators pointing to image values.
Definition transf.hpp:526
PTransfBase(PTransfBase const &)=default
Default copy constructor.
const_iterator begin() const noexcept
Definition transf.hpp:559
PTransfBase()=default
Default constructor.
bool operator>=(PTransfBase const &that) const
Compare for greater than or equal.
Definition transf.hpp:403
PTransfBase & operator=(PTransfBase const &)=default
Default copy assignment operator.
PTransfBase(std::initializer_list< Scalar > cont)
Construct from a container of images.
Definition transf.hpp:242
point_type const & at(size_t i) const
Get a const reference to the image of a point.
Definition transf.hpp:490
size_t degree() const noexcept
Returns the degree of a partial transformation.
Definition transf.hpp:653
const_iterator end() const noexcept
Definition transf.hpp:564
bool operator>(PTransfBase const &that) const
Compare for greater.
Definition transf.hpp:346
static Subclass make(OtherContainer &&cont)
Construct from universal reference container and check.
PTransfBase & operator=(PTransfBase &&)=default
Default move assignment operator.
PTransfBase(PTransfBase &&)=default
Default move constructor.
Scalar point_type
Type of the image values.
Definition transf.hpp:138
static Subclass make(std::initializer_list< OtherScalar > cont)
Construct from std::initializer_list and check.
iterator begin() noexcept
Returns an iterator (random access iterator) pointing at the first image value.
Definition transf.hpp:579
const_iterator cend() const noexcept
Returns a const_iterator (random access iterator) pointing one past the last image value.
Definition transf.hpp:554
Container container_type
Type of the underlying container.
Definition transf.hpp:143
const_iterator cbegin() const noexcept
Returns a const_iterator (random access iterator) pointing at the first image value.
Definition transf.hpp:539
point_type & at(size_t i)
Get a reference to the image of a point.
static Subclass one(size_t N)
Returns the identity transformation on the given number of points.
Definition transf.hpp:671
PTransfBase(Iterator first, Iterator last)
Construct from a range of images.
Definition transf.hpp:220
point_type & operator[](size_t i)
Get a reference to the image of a point.
Definition transf.hpp:440
static point_type undef() noexcept
Returns the value used to represent "undefined".
Definition transf.hpp:156
size_t hash_value() const
Returns a hash value.
Definition transf.hpp:628
bool operator<=(PTransfBase const &that) const
Compare for less than or equal.
Definition transf.hpp:384
bool operator<(PTransfBase const &that) const
Compare for less.
Definition transf.hpp:327
PTransfBase(Container const &cont)
Construct from a container of images.
Definition transf.hpp:194
point_type const & operator[](size_t i) const
Get a const reference to the image of a point.
Definition transf.hpp:458
void swap(PTransfBase &that) noexcept
Swap with another partial transformation.
Definition transf.hpp:638
PTransfBase(Container &&cont)
Construct from a container of images.
Definition transf.hpp:197
bool operator!=(PTransfBase const &that) const
Compare for inequality.
Definition transf.hpp:422
iterator end() noexcept
Returns an iterator (random access iterator) pointing one past the last image value.
Definition transf.hpp:594
Permutations with static or dynamic degree.
Definition transf.hpp:1524
static Perm one(size_t M)
Returns the identity transformation on the given number of points.
Definition transf.hpp:1542
Scalar point_type
Type of the image values.
Definition transf.hpp:1531
typename PTransf< N, point_type >::container_type container_type
Type of the underlying container.
Definition transf.hpp:1536
Partial transformations with static degree.
Definition transf.hpp:836
StaticPTransf & increase_degree_by(size_t)
Increase the degree in-place.
Definition transf.hpp:883
const_iterator begin() const noexcept
Returns a const_iterator (random access iterator) pointing at the first image value.
Definition transf.hpp:559
std::array< Scalar, N > container_type
Type of the underlying container.
Definition transf.hpp:846
const_iterator end() const noexcept
Returns a const_iterator (random access iterator) pointing one past the last image value.
Definition transf.hpp:564
StaticPTransf(size_t n)
Construct with given degree.
StaticPTransf()
Default constructor.
Definition transf.hpp:862
Scalar point_type
Type of the image values.
Definition transf.hpp:841
Transformations with static or dynamic degree.
Definition transf.hpp:1004
static Transf one(size_t M)
Returns the identity transformation on the given number of points.
Definition transf.hpp:1055
Scalar point_type
Type of the image values.
Definition transf.hpp:1011
void product_inplace(Transf const &f, Transf const &g)
Multiply two transformations and store the product in this.
typename base_type::container_type container_type
Type of the underlying container.
Definition transf.hpp:1016
T copy(T... args)
T distance(T... args)
T fill(T... args)
T forward(T... args)
std::string to_human_readable_repr(Action< Element, Point, Func, Traits, LeftOrRight > const &action)
Return a human readable representation of an Action object.
Undefined const UNDEFINED
Value for something undefined.
#define LIBSEMIGROUPS_EXCEPTION(...)
Throw a LibsemigroupsException.
Definition exception.hpp:99
enable_if_is_same< Return, Blocks > make(Container const &cont)
Check the arguments, construct a Blocks object, and check it.
Definition bipart.hpp:856
typename detail::LeastPermHelper< N >::type LeastPerm
Type of perms using the least memory for a given degree.
Definition transf.hpp:2413
static constexpr bool IsPPerm
Helper variable template.
Definition transf.hpp:1328
auto one(T const &f) -> std::enable_if_t< IsDerivedFromPTransf< T >, T >
Returns the identity transformation of same degree as a sample.
Definition transf.hpp:1805
static constexpr bool IsTransf
Helper variable template.
Definition transf.hpp:1088
void throw_if_not_perm(Perm< N, Scalar > const &f)
Check a permutation.
Definition transf.hpp:1598
typename detail::LeastTransfHelper< N >::type LeastTransf
Type of transformations using the least memory for a given degree.
Definition transf.hpp:2383
PPerm< N, Scalar > left_one(PPerm< N, Scalar > const &f)
Returns the left one of a partial perm.
typename detail::LeastPPermHelper< N >::type LeastPPerm
Type of partial perms using the least memory for a given degree.
Definition transf.hpp:2398
void domain(T const &f, std::vector< Scalar > &dom)
Replace the contents of a vector by the set of points where a partial transformation is defined.
static constexpr bool IsPTransf
Helper variable template.
Definition transf.hpp:947
PPerm< N, Scalar > right_one(PPerm< N, Scalar > const &f)
Returns the right one of a partial perm.
static constexpr bool IsDynamic
Helper variable template.
Definition transf.hpp:712
static constexpr bool IsStatic
Helper variable template.
Definition transf.hpp:702
void throw_if_not_pperm(PPerm< N, Scalar > const &f)
Check a partial perm.
Definition transf.hpp:1351
void image(T const &f, std::vector< Scalar > &im)
Replace the contents of a vector by the set of images of a partial transformation.
void throw_if_image_value_out_of_range(PTransfBase< Scalar, Container > const &f)
Check a partial transformation.
std::string to_input_string(Transf< N, Scalar > const &x, std::string_view prefix="", std::string_view braces="{}")
Return a string that can be used to recreate a transformation.
static constexpr bool IsPerm
Helper variable template.
Definition transf.hpp:1575
static constexpr bool IsDerivedFromPTransf
Helper variable template.
Definition transf.hpp:102
std:: conditional_t< N==0, DynamicPTransf< Scalar >, StaticPTransf< N, Scalar > > PTransf
Partial transformations with static or dynamic degree.
Definition transf.hpp:915
void inverse(PPerm< N, Scalar > const &from, PPerm< N, Scalar > &to)
Replace contents of a partial perm with the inverse of another.
T iota(T... args)
T move(T... args)
Namespace for everything in the libsemigroups library.
Definition action.hpp:44
auto to(detail::KnuthBendixImpl< Rewriter, ReductionOrder > &kb) -> std::enable_if_t< std::is_same_v< Presentation< typename Result::word_type >, Result >, Result >
No doc.
Adapter for the complexity of multiplication.
Definition adapters.hpp:128
Adapter for the degree of an element.
Definition adapters.hpp:166
Adapter for hashing.
Definition adapters.hpp:453
size_t operator()(Value const &x) const
Hash x using std::hash.
Definition adapters.hpp:462
void operator()(PPerm< N, Scalar > &res, PPerm< N, Scalar > const &pt, PPerm< N, Scalar > const &x) const noexcept
Definition transf.hpp:2212
void operator()(T &res, T const &pt, PPerm< N, Scalar > const &x) const
Definition transf.hpp:2236
void operator()(T &res, T const &pt, Transf< N, Scalar > const &x) const
Stores the image of pt under the left action of x in res.
Adapter for the value of a left action.
Definition adapters.hpp:357
void operator()(BitSet< M > &res, BitSet< M > const &pt, PPerm< N, Scalar > const &x) const
Stores the image set of pt under x in res.
void operator()(PPerm< N, Scalar > &res, PPerm< N, Scalar > const &pt, PPerm< N, Scalar > const &x) const noexcept
Stores the idempotent in res.
Definition transf.hpp:2168
void operator()(T &res, T const &pt, PPerm< N, Scalar > const &x) const
Stores the image set of pt under x in res.
void operator()(T &res, T const &pt, Perm< N, Scalar > const &p) const noexcept
Stores the image of pt under the action of p in res.
Definition transf.hpp:2324
T operator()(T pt, Perm< N, Scalar > const &x)
Returns the image of pt under the action of p.
Definition transf.hpp:2332
void operator()(BitSet< M > &res, BitSet< M > const &pt, Transf< N, Scalar > const &x) const
Stores the image set of pt under x in res.
Definition transf.hpp:2032
void operator()(T &res, T const &pt, Transf< N, Scalar > const &x) const
Stores the image set of pt under x in res.
Adapter for the value of a right action.
Definition adapters.hpp:399
void operator()(T &x, size_t n) const
Returns x->increase_degree_by(n).
Definition transf.hpp:2002
Adapter for increasing the degree of an element.
Definition adapters.hpp:206
Adapter for the inverse of an element.
Definition adapters.hpp:326
void operator()(BitSet< M > &res, PPerm< N, Scalar > const &x) const
void operator()(BitSet< M > &res, Transf< N, Scalar > const &x) const
void operator()(T &res, Transf< N, Scalar > const &x) const
Adapter for the action on LambdaValue's.
Definition adapters.hpp:840
BitSet< BitSet< 1 >::max_size()> type
Definition transf.hpp:2260
BitSet< BitSet< 1 >::max_size()> type
Definition transf.hpp:2070
Adapter for lambda functions.
Definition adapters.hpp:800
Adapter for the identity element of the given type.
Definition adapters.hpp:253
Adapter for the product of two elements.
Definition adapters.hpp:291
size_t operator()(PPerm< N, Scalar > const &x) const
Definition transf.hpp:2305
size_t operator()(Transf< N, Scalar > const &x) const
Definition transf.hpp:2151
Adapter for calculating ranks.
Definition adapters.hpp:937
void operator()(BitSet< M > &res, PPerm< N, Scalar > const &x) const
void operator()(T &res, Transf< N, Scalar > const &x) const
Adapter for the action on RhoValue's.
Definition adapters.hpp:861
typename LambdaValue< PPerm< N, Scalar > >::type type
Definition transf.hpp:2272
std::vector< Scalar > type
Definition transf.hpp:2082
Adapter for rho functions.
Definition adapters.hpp:819
T swap(T... args)