libsemigroups  v3.0.3
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
77
126 // TODO(1) there are definitely some assumptions about the calls to the member
127 // functions of an IsObviouslyInfinite (see for example the function
128 // is_obviously_infinite for a Presentation). These should be documented.
129 //
130 // TODO(1) this class should be more generic, like detail::CongruenceCommon
131 // and its derived classes, allowing arbitrary iterators of rules to be added
132 class IsObviouslyInfinite {
133 // The default constructor is private since an object that is default
134 // constructed isn't usable with the current public API.
135 IsObviouslyInfinite() = default;
136
137 public:
141
149
153
163 explicit IsObviouslyInfinite(size_t n);
164
180 IsObviouslyInfinite& init(size_t n);
181
191 explicit IsObviouslyInfinite(std::string const& lphbt)
192 : IsObviouslyInfinite(lphbt.size()) {}
193
209 IsObviouslyInfinite& init(std::string const& lphbt) {
210 return init(lphbt.size());
211 }
212
214 IsObviouslyInfinite(IsObviouslyInfinite const&) = delete;
215
217 IsObviouslyInfinite(IsObviouslyInfinite&&) = delete;
218
220 IsObviouslyInfinite& operator=(IsObviouslyInfinite const&) = delete;
221
223 IsObviouslyInfinite& operator=(IsObviouslyInfinite&&) = delete;
224
225 ~IsObviouslyInfinite();
226
245 IsObviouslyInfinite& add_rules_no_checks(word_type const&,
248
270 IsObviouslyInfinite& add_rules_no_checks(std::string const& lphbt,
273
292 IsObviouslyInfinite& add_rules_no_checks(std::string const& lphbt,
295
317 IsObviouslyInfinite& add_rules_no_checks(std::string const& lphbt,
320
332 bool result() const;
333
334 // TODO(1) certificate() returning why the thing is obviously infinite
335
336 private:
337 void private_add_rule(size_t const, word_type const&, word_type const&);
338
339 inline void letters_in_word(size_t row, word_type const& w, int64_t adv) {
340 for (size_t const& x : w) {
341 matrix(row, x) += adv;
342 _seen[x] = true;
343 }
344 }
345
346 inline void plus_letters_in_word(size_t row, word_type const& w) {
347 letters_in_word(row, w, 1);
348 }
349
350 inline void minus_letters_in_word(size_t row, word_type const& w) {
351 letters_in_word(row, w, -1);
352 }
353
354 inline int64_t& matrix(size_t row, size_t col) {
355#ifdef LIBSEMIGROUPS_EIGEN_ENABLED
356 return _matrix(row, col);
357#else
358 (void) row;
359 return _matrix[col];
360#endif
361 }
362
363 inline bool matrix_row_sums_to_0(size_t row) {
364#ifdef LIBSEMIGROUPS_EIGEN_ENABLED
365 return _matrix.row(row).sum() == 0;
366#else
367 (void) row;
368 return std::accumulate(_matrix.cbegin(), _matrix.cend(), 0) == 0;
369#endif
370 }
371
372 // letter_type i belongs to "preserve" if there exists a relation where
373 // the number of occurrences of i is not the same on both sides of the
374 // relation letter_type i belongs to "unique" if there is a relation
375 // where one side consists solely of i.
376 bool _empty_word;
377 detail::Duf<> _letter_components;
378 size_t _nr_gens;
379 size_t _nr_letter_components;
380 size_t _nr_relations;
381 bool _preserve_length;
382 std::vector<bool> _preserve;
383 std::vector<bool> _seen;
384 std::vector<bool> _unique;
385
386#ifdef LIBSEMIGROUPS_EIGEN_ENABLED
387 Eigen::Matrix<int64_t, Eigen::Dynamic, Eigen::Dynamic> _matrix;
388#else
389 std::vector<int64_t> _matrix;
390#endif
391 };
392
414 template <typename Word>
417 if (p.alphabet().empty()) {
418 return false;
419 }
420 auto it
422
423 if (*it != p.alphabet().size() - 1) {
424 auto copy_p = p;
426 copy_p,
427 rx::seq<typename Presentation<Word>::letter_type>(0)
428 | rx::take(p.alphabet().size()) | rx::to_vector());
429 IsObviouslyInfinite ioi(copy_p.alphabet().size());
431 p.alphabet(), copy_p.rules.cbegin(), copy_p.rules.cend());
432 return ioi.result();
433 }
434
437 return ioi.result();
438 }
439
459 template <>
461
484 bool is_obviously_infinite(detail::ToddCoxeterImpl const& tc);
485
506 // This function is implemented in cong-class.tpp
507 template <typename Word>
508 bool is_obviously_infinite(Congruence<Word>& c);
509
531 template <typename Word>
532 bool is_obviously_infinite(Kambites<Word>& k) {
533 if (k.finished() && k.small_overlap_class() >= 3) {
534 return true;
535 }
537 return true;
538 }
539 return k.small_overlap_class() >= 3;
540 }
541
563 template <typename Rewriter, typename ReductionOrder>
564 bool
565 is_obviously_infinite(detail::KnuthBendixImpl<Rewriter, ReductionOrder>& kb) {
566 if (kb.finished()) {
568 }
569 auto const& p = kb.internal_presentation();
570 if (p.alphabet().empty()) {
571 return false;
572 }
573 IsObviouslyInfinite ioi(p.alphabet().size());
574 ioi.add_rules_no_checks(p.alphabet(), p.rules.cbegin(), p.rules.cend());
575 ioi.add_rules_no_checks(p.alphabet(),
576 kb.internal_generating_pairs().cbegin(),
577 kb.internal_generating_pairs().cend());
578 return ioi.result();
579 }
580
581} // namespace libsemigroups
582#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:132
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:147
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:191
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:209
typename std::vector< word_type >::const_iterator const_iterator_word_type
Alias for std::vector<word_type>::const_iterator.
Definition obvinf.hpp:139
typename std::vector< std::string >::const_iterator const_iterator_string
Alias for std::vector<std::string>::const_iterator.
Definition obvinf.hpp:151
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:415
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)