libsemigroups  v3.0.0
C++ library for semigroups and monoids
Loading...
Searching...
No Matches
obvinf.hpp
1//
2// libsemigroups - C++ library for semigroups and monoids
3// Copyright (C) 2020-2025 James D. Mitchell + Reinis Cirpons
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 a helper class for checking whether or not a congruence
20// defined by generating pairs or finitely presented semigroup or monoid is
21// obviously infinite.
22
23#ifndef LIBSEMIGROUPS_OBVINF_HPP_
24#define LIBSEMIGROUPS_OBVINF_HPP_
25
26#include <cstddef> // for size_t
27#include <numeric> // for accumulate
28#include <string> // for string
29#include <utility> // for pair
30#include <vector> // for vector
31
32#include "config.hpp" // for LIBSEMIGROUPS_EIGEN_ENABLED
33#include "ranges.hpp" // for rx/ranges
34#include "types.hpp" // for word_type etc
35#include "word-graph.hpp" // for is_acyclic
36
37#include "detail/eigen.hpp" // for eigen
38#include "detail/uf.hpp" // for Duf
39
40namespace libsemigroups {
41#ifndef LIBSEMIGROUPS_PARSED_BY_DOXYGEN
42
43 namespace detail {
44 template <typename Rewriter, typename ReductionOrder>
45 class KnuthBendixImpl; // forward decl
46 class ToddCoxeterImpl; // forward decl
47 } // namespace detail
48
49 template <typename Word>
50 class Congruence; // forward decl
51
52 template <typename Word>
53 class Kambites; // forward decl
54
55 template <typename Word>
56 class Presentation; // forward decl
57#endif
58
59 namespace presentation {
60 template <typename Word>
62 }
63
73
122 // TODO(1) there are definitely some assumptions about the calls to the member
123 // functions of an IsObviouslyInfinite (see for example the function
124 // is_obviously_infinite for a Presentation). These should be documented.
125 //
126 // TODO(1) this class should be more generic, like detail::CongruenceCommon
127 // and its derived classes, allowing arbitrary iterators of rules to be added
128 class IsObviouslyInfinite {
129 // The default constructor is private since an object that is default
130 // constructed isn't usable with the current public API.
131 IsObviouslyInfinite() = default;
132
133 public:
137
145
149
159 explicit IsObviouslyInfinite(size_t n);
160
176 IsObviouslyInfinite& init(size_t n);
177
187 explicit IsObviouslyInfinite(std::string const& lphbt)
188 : IsObviouslyInfinite(lphbt.size()) {}
189
205 IsObviouslyInfinite& init(std::string const& lphbt) {
206 return init(lphbt.size());
207 }
208
210 IsObviouslyInfinite(IsObviouslyInfinite const&) = delete;
211
213 IsObviouslyInfinite(IsObviouslyInfinite&&) = delete;
214
216 IsObviouslyInfinite& operator=(IsObviouslyInfinite const&) = delete;
217
219 IsObviouslyInfinite& operator=(IsObviouslyInfinite&&) = delete;
220
221 ~IsObviouslyInfinite();
222
241 IsObviouslyInfinite& add_rules_no_checks(word_type const&,
244
266 IsObviouslyInfinite& add_rules_no_checks(std::string const& lphbt,
269
288 IsObviouslyInfinite& add_rules_no_checks(std::string const& lphbt,
291
313 IsObviouslyInfinite& add_rules_no_checks(std::string const& lphbt,
316
328 bool result() const;
329
330 // TODO(1) certificate() returning why the thing is obviously infinite
331
332 private:
333 void private_add_rule(size_t const, word_type const&, word_type const&);
334
335 inline void letters_in_word(size_t row, word_type const& w, int64_t adv) {
336 for (size_t const& x : w) {
337 matrix(row, x) += adv;
338 _seen[x] = true;
339 }
340 }
341
342 inline void plus_letters_in_word(size_t row, word_type const& w) {
343 letters_in_word(row, w, 1);
344 }
345
346 inline void minus_letters_in_word(size_t row, word_type const& w) {
347 letters_in_word(row, w, -1);
348 }
349
350 inline int64_t& matrix(size_t row, size_t col) {
351#ifdef LIBSEMIGROUPS_EIGEN_ENABLED
352 return _matrix(row, col);
353#else
354 (void) row;
355 return _matrix[col];
356#endif
357 }
358
359 inline bool matrix_row_sums_to_0(size_t row) {
360#ifdef LIBSEMIGROUPS_EIGEN_ENABLED
361 return _matrix.row(row).sum() == 0;
362#else
363 (void) row;
364 return std::accumulate(_matrix.cbegin(), _matrix.cend(), 0) == 0;
365#endif
366 }
367
368 // letter_type i belongs to "preserve" if there exists a relation where
369 // the number of occurrences of i is not the same on both sides of the
370 // relation letter_type i belongs to "unique" if there is a relation
371 // where one side consists solely of i.
372 bool _empty_word;
373 detail::Duf<> _letter_components;
374 size_t _nr_gens;
375 size_t _nr_letter_components;
376 size_t _nr_relations;
377 bool _preserve_length;
378 std::vector<bool> _preserve;
379 std::vector<bool> _seen;
380 std::vector<bool> _unique;
381
382#ifdef LIBSEMIGROUPS_EIGEN_ENABLED
383 Eigen::Matrix<int64_t, Eigen::Dynamic, Eigen::Dynamic> _matrix;
384#else
385 std::vector<int64_t> _matrix;
386#endif
387 };
388
410 template <typename Word>
413 if (p.alphabet().empty()) {
414 return false;
415 }
416 auto it
418
419 if (*it != p.alphabet().size() - 1) {
420 auto copy_p = p;
422 copy_p,
423 rx::seq<typename Presentation<Word>::letter_type>(0)
424 | rx::take(p.alphabet().size()) | rx::to_vector());
425 IsObviouslyInfinite ioi(copy_p.alphabet().size());
427 p.alphabet(), copy_p.rules.cbegin(), copy_p.rules.cend());
428 return ioi.result();
429 }
430
433 return ioi.result();
434 }
435
455 template <>
457
480 bool is_obviously_infinite(detail::ToddCoxeterImpl const& tc);
481
502 // This function is implemented in cong-class.tpp
503 template <typename Word>
504 bool is_obviously_infinite(Congruence<Word>& c);
505
527 template <typename Word>
528 bool is_obviously_infinite(Kambites<Word>& k) {
529 if (k.finished() && k.small_overlap_class() >= 3) {
530 return true;
531 }
533 return true;
534 }
535 return k.small_overlap_class() >= 3;
536 }
537
559 template <typename Rewriter, typename ReductionOrder>
560 bool
561 is_obviously_infinite(detail::KnuthBendixImpl<Rewriter, ReductionOrder>& kb) {
562 if (kb.finished()) {
564 }
565 auto const& p = kb.internal_presentation();
566 if (p.alphabet().empty()) {
567 return false;
568 }
569 IsObviouslyInfinite ioi(p.alphabet().size());
570 ioi.add_rules_no_checks(p.alphabet(), p.rules.cbegin(), p.rules.cend());
571 ioi.add_rules_no_checks(p.alphabet(),
572 kb.internal_generating_pairs().cbegin(),
573 kb.internal_generating_pairs().cend());
574 return ioi.result();
575 }
576
577} // namespace libsemigroups
578#endif // LIBSEMIGROUPS_OBVINF_HPP_
T accumulate(T... args)
T begin(T... args)
Class for checking if a finitely presented semigroup or monoid is obviously infinite.
Definition obvinf.hpp:128
typename std::vector< std::pair< std::string, std::string > >::const_iterator const_iterator_pair_string
Alias for std::vector< std::pair<std::string, std::string>>::const_iterator.
Definition obvinf.hpp:143
IsObviouslyInfinite & add_rules_no_checks(word_type const &, const_iterator_word_type first, const_iterator_word_type last)
Add rules from iterators to word_type.
IsObviouslyInfinite(size_t n)
Construct from alphabet size.
IsObviouslyInfinite & add_rules_no_checks(std::string const &lphbt, const_iterator_pair_string first, const_iterator_pair_string last)
Add rules from iterators to std::pair of std::string.
IsObviouslyInfinite(IsObviouslyInfinite &&)=delete
Deleted.
IsObviouslyInfinite(std::string const &lphbt)
Construct from alphabet.
Definition obvinf.hpp:187
IsObviouslyInfinite & operator=(IsObviouslyInfinite const &)=delete
Deleted.
IsObviouslyInfinite & add_rules_no_checks(std::string const &lphbt, const_iterator_word_type first, const_iterator_word_type last)
Add rules from iterators to word_type.
IsObviouslyInfinite & add_rules_no_checks(std::string const &lphbt, const_iterator_string first, const_iterator_string last)
Add rules from iterators to std::string.
IsObviouslyInfinite & init(std::string const &lphbt)
Definition obvinf.hpp:205
typename std::vector< word_type >::const_iterator const_iterator_word_type
Alias for std::vector<word_type>::const_iterator.
Definition obvinf.hpp:135
typename std::vector< std::string >::const_iterator const_iterator_string
Alias for std::vector<std::string>::const_iterator.
Definition obvinf.hpp:147
IsObviouslyInfinite(IsObviouslyInfinite const &)=delete
Deleted.
IsObviouslyInfinite & init(size_t n)
bool result() const
Returns whether or not the finitely presented semigroup or monoid is obviously infinite.
IsObviouslyInfinite & operator=(IsObviouslyInfinite &&)=delete
Deleted.
For an implementation of presentations for semigroups or monoids.
Definition presentation.hpp:102
void throw_if_bad_alphabet_or_rules() const
Check if the alphabet and rules are valid.
Definition presentation.hpp:596
std::vector< word_type > rules
Data member holding the rules of the presentation.
Definition presentation.hpp:131
word_type const & alphabet() const noexcept
Return the alphabet of the presentation.
Definition presentation.hpp:183
typename word_type::value_type letter_type
Type of the letters in the words that constitute the rules of a Presentation object.
Definition presentation.hpp:109
bool finished() const
Check if run has been run to completion or not.
T empty(T... args)
T end(T... args)
size_t small_overlap_class()
Get the small overlap class.
Presentation< native_word_type > const & presentation() const noexcept
Get the presentation used to define a Kambites instance (if any).
Definition kambites-class.hpp:343
WordGraph< uint32_t > const & gilman_graph()
Return the Gilman WordGraph.
bool is_obviously_infinite(Presentation< Word > const &p)
Function for checking if the finitely presented semigroup or monoid defined by a Presentation object ...
Definition obvinf.hpp:411
std::vector< letter_type > word_type
Type for a word over the generators of a semigroup.
Definition types.hpp:101
T max_element(T... args)
Namespace for Presentation helper functions.
Definition obvinf.hpp:59
void change_alphabet(Presentation< Word > &, Word const &)
Change or re-order the alphabet.
bool is_acyclic(WordGraph< Node > const &wg)
Check if a word graph is acyclic.
Namespace for everything in the libsemigroups library.
Definition action.hpp:44
T size(T... args)