libsemigroups  v3.3.0
C++ library for semigroups and monoids
Loading...
Searching...
No Matches
matrix.hpp
1//
2// libsemigroups - C++ library for semigroups and monoids
3// Copyright (C) 2020-2025 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// TODO(1) tpp file
20// TODO(1) put the detail stuff into detail/matrix-common.hpp
21// TODO(1) there're no complete set of init methods for matrices
22
23#ifndef LIBSEMIGROUPS_MATRIX_HPP_
24#define LIBSEMIGROUPS_MATRIX_HPP_
25
26#include <algorithm> // for min
27#include <array> // for array
28#include <bitset> // for bitset
29#include <cstddef> // for size_t
30#include <cstdint> // for uint64_t
31#include <initializer_list> // for initializer_list
32#include <iosfwd> // for ostringstream
33#include <iterator> // for distance
34#include <numeric> // for inner_product
35#include <ostream> // for operator<<, basic_ostream
36#include <string> // for string
37#include <tuple> // for tie
38#include <type_traits> // for false_type, is_signed, true_type
39#include <unordered_map> // for unordered_map
40#include <unordered_set> // for unordered_set
41#include <utility> // for forward, make_pair, pair
42#include <vector> // for vector
43
44#include "adapters.hpp" // for Degree
45#include "bitset.hpp" // for BitSet
46#include "config.hpp" // for LIBSEMIGROUPS_PARSED_BY_DOXYGEN
47#include "constants.hpp" // for POSITIVE_INFINITY
48#include "debug.hpp" // for LIBSEMIGROUPS_ASSERT
49#include "exception.hpp" // for LIBSEMIGROUPS_EXCEPTION
50
51#include "detail/containers.hpp" // for StaticVector1
52#include "detail/formatters.hpp" // for formatter of POSITIVE_INFINITY ...
53#include "detail/string.hpp" // for detail::to_string
54
55namespace libsemigroups {
56
128
130 // Detail
132
133 namespace detail {
134
135 template <typename T>
136 struct IsStdBitSetHelper : std::false_type {};
137
138 template <size_t N>
139 struct IsStdBitSetHelper<std::bitset<N>> : std::true_type {};
140
141 template <typename T>
142 static constexpr bool IsStdBitSet = IsStdBitSetHelper<T>::value;
143
144 struct MatrixPolymorphicBase {};
145
146 template <typename T>
147 struct IsMatrixHelper {
148 static constexpr bool value
149 = std::is_base_of<detail::MatrixPolymorphicBase, T>::value;
150 };
151 } // namespace detail
152
167 template <typename T>
168 constexpr bool IsMatrix = detail::IsMatrixHelper<T>::value;
169
170 namespace matrix {
171
183 template <typename Mat>
184 auto throw_if_not_square(Mat const& x) -> std::enable_if_t<IsMatrix<Mat>> {
185 if (x.number_of_rows() != x.number_of_cols()) {
186 LIBSEMIGROUPS_EXCEPTION("expected a square matrix, but found {}x{}",
187 x.number_of_rows(),
188 x.number_of_cols());
189 }
190 }
191
205 template <typename Mat>
206 auto throw_if_bad_dim(Mat const& x, Mat const& y)
207 -> std::enable_if_t<IsMatrix<Mat>> {
208 if (x.number_of_rows() != y.number_of_rows()
209 || x.number_of_cols() != y.number_of_cols()) {
211 "expected matrices with the same dimensions, the 1st argument is a "
212 "{}x{} matrix, and the 2nd is a {}x{} matrix",
213 x.number_of_rows(),
214 x.number_of_cols(),
215 y.number_of_rows(),
216 y.number_of_cols());
217 }
218 }
219
234 template <typename Mat>
235 auto throw_if_bad_coords(Mat const& x, size_t r, size_t c)
236 -> std::enable_if_t<IsMatrix<Mat>> {
237 if (r >= x.number_of_rows()) {
238 LIBSEMIGROUPS_EXCEPTION("invalid row index in ({}, {}), expected "
239 "values in [0, {}) x [0, {})",
240 r,
241 c,
242 x.number_of_rows(),
243 x.number_of_cols(),
244 r);
245 }
246 if (c >= x.number_of_cols()) {
247 LIBSEMIGROUPS_EXCEPTION("invalid column index in ({}, {}), expected "
248 "values in [0, {}) x [0, {})",
249 r,
250 c,
251 x.number_of_rows(),
252 x.number_of_cols(),
253 r);
254 }
255 }
256 } // namespace matrix
257
259 // Detail
261 namespace detail {
262 template <typename Container,
263 typename Subclass,
264 typename TRowView,
265 typename Semiring = void>
266 class MatrixCommon : MatrixPolymorphicBase {
267 public:
269 // MatrixCommon - Aliases - public
271
272 using scalar_type = typename Container::value_type;
273 using scalar_reference = typename Container::reference;
274 using scalar_const_reference = typename Container::const_reference;
275 using semiring_type = Semiring;
276
277 using container_type = Container;
278 using iterator = typename Container::iterator;
279 using const_iterator = typename Container::const_iterator;
280
281 using RowView = TRowView;
282
283 scalar_type scalar_one() const noexcept {
284 return static_cast<Subclass const*>(this)->one_impl();
285 }
286
287 scalar_type scalar_zero() const noexcept {
288 return static_cast<Subclass const*>(this)->zero_impl();
289 }
290
291 Semiring const* semiring() const noexcept {
292 return static_cast<Subclass const*>(this)->semiring_impl();
293 }
294
295 private:
297 // MatrixCommon - Semiring arithmetic - private
299
300 scalar_type plus_no_checks(scalar_type x, scalar_type y) const noexcept {
301 return static_cast<Subclass const*>(this)->plus_no_checks_impl(y, x);
302 }
303
304 scalar_type product_no_checks(scalar_type x,
305 scalar_type y) const noexcept {
306 return static_cast<Subclass const*>(this)->product_no_checks_impl(y, x);
307 }
308
309 protected:
311 // MatrixCommon - Container functions - protected
313
314 // TODO(1) use constexpr-if, not SFINAE
315 template <typename SFINAE = container_type>
316 auto resize(size_t r, size_t c) -> std::enable_if_t<
317 std::is_same<SFINAE, std::vector<scalar_type>>::value> {
318 _container.resize(r * c);
319 }
320
321 template <typename SFINAE = container_type>
322 auto resize(size_t, size_t) -> std::enable_if_t<
323 !std::is_same<SFINAE, std::vector<scalar_type>>::value> {}
324
325 public:
327 // MatrixCommon - Constructors + destructor - public
329
330 // none of the constructors are noexcept because they allocate
331 MatrixCommon() = default;
332 MatrixCommon(MatrixCommon const&) = default;
333 MatrixCommon(MatrixCommon&&) = default;
334 MatrixCommon& operator=(MatrixCommon const&) = default;
335 MatrixCommon& operator=(MatrixCommon&&) = default;
336
337 explicit MatrixCommon(std::initializer_list<scalar_type> const& c)
338 : MatrixCommon() {
339 resize(1, c.size());
340 std::copy(c.begin(), c.end(), _container.begin());
341 }
342
343 explicit MatrixCommon(std::vector<std::vector<scalar_type>> const& m)
344 : MatrixCommon() {
345 init(m);
346 }
347
348 MatrixCommon(
349 std::initializer_list<std::initializer_list<scalar_type>> const& m)
350 : MatrixCommon() {
351 init(m);
352 }
353
354 private:
355 // not noexcept because resize isn't
356 template <typename T>
357 void init(T const& m) {
358 size_t const R = m.size();
359 if (R == 0) {
360 return;
361 }
362 size_t const C = m.begin()->size();
363 resize(R, C);
364 for (size_t r = 0; r < R; ++r) {
365 auto row = m.begin() + r;
366 for (size_t c = 0; c < C; ++c) {
367 _container[r * C + c] = *(row->begin() + c);
368 }
369 }
370 }
371
372 // not noexcept because init isn't
373 void
374 init(std::initializer_list<std::initializer_list<scalar_type>> const& m) {
375 init<std::initializer_list<std::initializer_list<scalar_type>>>(m);
376 }
377
378 public:
379 explicit MatrixCommon(RowView const& rv) : MatrixCommon() {
380 resize(1, rv.size());
381 std::copy(rv.cbegin(), rv.cend(), _container.begin());
382 }
383
384 ~MatrixCommon() = default;
385
386 // not noexcept because mem allocate is required
387 Subclass one() const {
388 size_t const n = number_of_rows();
389 Subclass x(semiring(), n, n);
390 std::fill(x.begin(), x.end(), scalar_zero());
391 for (size_t r = 0; r < n; ++r) {
392 x(r, r) = scalar_one();
393 }
394 return x;
395 }
396
398 // Comparison operators
400
401 // not noexcept because apparently vector::operator== isn't
402 bool operator==(MatrixCommon const& that) const {
403 return _container == that._container;
404 }
405
406 // not noexcept because apparently vector::operator== isn't
407 bool operator==(RowView const& that) const {
408 return number_of_rows() == 1
409 && static_cast<RowView>(*static_cast<Subclass const*>(this))
410 == that;
411 }
412
413 // not noexcept because apparently vector::operator< isn't
414 bool operator<(MatrixCommon const& that) const {
415 return _container < that._container;
416 }
417
418 // not noexcept because apparently vector::operator< isn't
419 bool operator<(RowView const& that) const {
420 return number_of_rows() == 1
421 && static_cast<RowView>(*static_cast<Subclass const*>(this))
422 < that;
423 }
424
425 // not noexcept because operator== isn't
426 template <typename T>
427 bool operator!=(T const& that) const {
428 static_assert(IsMatrix<T> || std::is_same_v<T, RowView>);
429 return !(*this == that);
430 }
431
432 // not noexcept because operator< isn't
433 template <typename T>
434 bool operator>(T const& that) const {
435 static_assert(IsMatrix<T> || std::is_same_v<T, RowView>);
436 return that < *this;
437 }
438
439 // not noexcept because operator< isn't
440 template <typename T>
441 bool operator>=(T const& that) const {
442 static_assert(IsMatrix<T> || std::is_same_v<T, RowView>);
443 return that < *this || that == *this;
444 }
445
446 // not noexcept because operator< isn't
447 template <typename T>
448 bool operator<=(T const& that) const {
449 static_assert(IsMatrix<T> || std::is_same_v<T, RowView>);
450 return *this < that || that == *this;
451 }
452
454 // Attributes
456
457 // not noexcept because vector::operator[] isn't, and neither is
458 // array::operator[]
459 scalar_reference operator()(size_t r, size_t c) {
460 return this->_container[r * number_of_cols() + c];
461 }
462
463 scalar_reference at(size_t r, size_t c) {
464 matrix::throw_if_bad_coords(static_cast<Subclass const&>(*this), r, c);
465 return this->operator()(r, c);
466 }
467
468 // not noexcept because vector::operator[] isn't, and neither is
469 // array::operator[]
470 scalar_const_reference operator()(size_t r, size_t c) const {
471 return this->_container[r * number_of_cols() + c];
472 }
473
474 scalar_const_reference at(size_t r, size_t c) const {
475 matrix::throw_if_bad_coords(static_cast<Subclass const&>(*this), r, c);
476 return this->operator()(r, c);
477 }
478
479 // noexcept because number_of_rows_impl is noexcept
480 size_t number_of_rows() const noexcept {
481 return static_cast<Subclass const*>(this)->number_of_rows_impl();
482 }
483
484 // noexcept because number_of_cols_impl is noexcept
485 size_t number_of_cols() const noexcept {
486 return static_cast<Subclass const*>(this)->number_of_cols_impl();
487 }
488
489 // not noexcept because Hash<T>::operator() isn't
490 size_t hash_value() const {
491 return Hash<Container>()(_container);
492 }
493
495 // Arithmetic operators - in-place
497
498 // not noexcept because memory is allocated
499 void product_inplace_no_checks(Subclass const& A, Subclass const& B) {
500 LIBSEMIGROUPS_ASSERT(number_of_rows() == number_of_cols());
501 LIBSEMIGROUPS_ASSERT(A.number_of_rows() == number_of_rows());
502 LIBSEMIGROUPS_ASSERT(B.number_of_rows() == number_of_rows());
503 LIBSEMIGROUPS_ASSERT(A.number_of_cols() == number_of_cols());
504 LIBSEMIGROUPS_ASSERT(B.number_of_cols() == number_of_cols());
505 LIBSEMIGROUPS_ASSERT(&A != this);
506 LIBSEMIGROUPS_ASSERT(&B != this);
507
508 // Benchmarking boolean matrix multiplication reveals that using a
509 // non-static container_type gives the best performance, when compared
510 // to static container_type the performance is more or less the same
511 // (but not thread-safe), and there appears to be a performance
512 // penalty of about 50% when using static thread_local container_type
513 // (when compiling with clang).
514 size_t const N = A.number_of_rows();
515 std::vector<scalar_type> tmp(N, 0);
516
517 for (size_t c = 0; c < N; c++) {
518 for (size_t i = 0; i < N; i++) {
519 tmp[i] = B(i, c);
520 }
521 for (size_t r = 0; r < N; r++) {
522 (*this)(r, c) = std::inner_product(
523 A._container.begin() + r * N,
524 A._container.begin() + (r + 1) * N,
525 tmp.begin(),
526 scalar_zero(),
527 [this](scalar_type x, scalar_type y) {
528 return this->plus_no_checks(x, y);
529 },
530 [this](scalar_type x, scalar_type y) {
531 return this->product_no_checks(x, y);
532 });
533 }
534 }
535 }
536
537 // not noexcept because iterator increment isn't
538 void operator*=(scalar_type a) {
539 for (auto it = _container.begin(); it < _container.end(); ++it) {
540 *it = product_no_checks(*it, a);
541 }
542 }
543
544 // not noexcept because vector::operator[] and array::operator[] aren't
545 void operator+=(Subclass const& that) {
546 LIBSEMIGROUPS_ASSERT(that.number_of_rows() == number_of_rows());
547 LIBSEMIGROUPS_ASSERT(that.number_of_cols() == number_of_cols());
548 for (size_t i = 0; i < _container.size(); ++i) {
549 _container[i] = plus_no_checks(_container[i], that._container[i]);
550 }
551 }
552
553 void operator+=(RowView const& that) {
554 LIBSEMIGROUPS_ASSERT(number_of_rows() == 1);
555 RowView(*static_cast<Subclass const*>(this)) += that;
556 }
557
558 void operator+=(scalar_type a) {
559 for (auto it = _container.begin(); it < _container.end(); ++it) {
560 *it = plus_no_checks(*it, a);
561 }
562 }
563
564 // TODO(2) implement operator*=(Subclass const&)
565
567 // Arithmetic operators - not in-place
569
570 // not noexcept because operator+= isn't
571 Subclass operator+(Subclass const& y) const {
572 Subclass result(*static_cast<Subclass const*>(this));
573 result += y;
574 return result;
575 }
576
577 // not noexcept because product_inplace_no_checks isn't
578 Subclass operator*(Subclass const& y) const {
579 Subclass result(*static_cast<Subclass const*>(this));
580 result.product_inplace_no_checks(*static_cast<Subclass const*>(this),
581 y);
582 return result;
583 }
584
585 Subclass operator*(scalar_type a) const {
586 Subclass result(*static_cast<Subclass const*>(this));
587 result *= a;
588 return result;
589 }
590
591 Subclass operator+(scalar_type a) const {
592 Subclass result(*static_cast<Subclass const*>(this));
593 result += a;
594 return result;
595 }
596
598 // Iterators
600
601 // noexcept because vector::begin and array::begin are noexcept
602 iterator begin() noexcept {
603 return _container.begin();
604 }
605
606 // noexcept because vector::end and array::end are noexcept
607 iterator end() noexcept {
608 return _container.end();
609 }
610
611 // noexcept because vector::begin and array::begin are noexcept
612 const_iterator begin() const noexcept {
613 return _container.begin();
614 }
615
616 // noexcept because vector::end and array::end are noexcept
617 const_iterator end() const noexcept {
618 return _container.end();
619 }
620
621 // noexcept because vector::cbegin and array::cbegin are noexcept
622 const_iterator cbegin() const noexcept {
623 return _container.cbegin();
624 }
625
626 // noexcept because vector::cend and array::cend are noexcept
627 const_iterator cend() const noexcept {
628 return _container.cend();
629 }
630
631 template <typename U>
632 std::pair<scalar_type, scalar_type> coords(U const& it) const {
633 static_assert(
634 std::is_same<U, iterator>::value
635 || std::is_same<U, const_iterator>::value,
636 "the parameter it must be of type iterator or const_iterator");
637 scalar_type const v = std::distance(_container.begin(), it);
638 return std::make_pair(v / number_of_cols(), v % number_of_cols());
639 }
640
642 // Modifiers
644
645 // noexcept because vector::swap and array::swap are noexcept
646 void swap(MatrixCommon& that) noexcept {
647 std::swap(_container, that._container);
648 }
649
650 // noexcept because swap is noexcept, and so too are number_of_rows and
651 // number_of_cols
652 void transpose_no_checks() noexcept {
653 LIBSEMIGROUPS_ASSERT(number_of_rows() == number_of_cols());
654 if (number_of_rows() == 0) {
655 return;
656 }
657 auto& x = *this;
658 for (size_t r = 0; r < number_of_rows() - 1; ++r) {
659 for (size_t c = r + 1; c < number_of_cols(); ++c) {
660 std::swap(x(r, c), x(c, r));
661 }
662 }
663 }
664
665 void transpose() {
666 matrix::throw_if_not_square(static_cast<Subclass&>(*this));
667 transpose_no_checks();
668 }
669
671 // Rows
673
674 // not noexcept because there's an allocation
675 RowView row_no_checks(size_t i) const {
676 auto& container = const_cast<Container&>(_container);
677 return RowView(static_cast<Subclass const*>(this),
678 container.begin() + i * number_of_cols(),
679 number_of_cols());
680 }
681
682 RowView row(size_t i) const {
683 if (i >= number_of_rows()) {
685 "index out of range, expected value in [{}, {}), found {}",
686 0,
687 number_of_rows(),
688 i);
689 }
690 return row_no_checks(i);
691 }
692
693 // not noexcept because there's an allocation
694 template <typename T>
695 void rows(T& x) const {
696 auto& container = const_cast<Container&>(_container);
697 for (auto itc = container.begin(); itc != container.end();
698 itc += number_of_cols()) {
699 x.emplace_back(
700 static_cast<Subclass const*>(this), itc, number_of_cols());
701 }
702 LIBSEMIGROUPS_ASSERT(x.size() == number_of_rows());
703 }
704
706 // Friend functions
708
709 friend std::ostream& operator<<(std::ostream& os, MatrixCommon const& x) {
710 os << detail::to_string(x);
711 return os;
712 }
713
714 private:
716 // Private data
718 container_type _container;
719 };
720
721 template <typename Scalar>
722 class MatrixDynamicDim {
723 public:
724 MatrixDynamicDim() : _number_of_cols(0), _number_of_rows(0) {}
725 MatrixDynamicDim(MatrixDynamicDim const&) = default;
726 MatrixDynamicDim(MatrixDynamicDim&&) = default;
727 MatrixDynamicDim& operator=(MatrixDynamicDim const&) = default;
728 MatrixDynamicDim& operator=(MatrixDynamicDim&&) = default;
729
730 MatrixDynamicDim(size_t r, size_t c)
731 : _number_of_cols(c), _number_of_rows(r) {}
732
733 ~MatrixDynamicDim() = default;
734
735 void swap(MatrixDynamicDim& that) noexcept {
736 std::swap(_number_of_cols, that._number_of_cols);
737 std::swap(_number_of_rows, that._number_of_rows);
738 }
739
740 protected:
741 size_t number_of_rows_impl() const noexcept {
742 return _number_of_rows;
743 }
744
745 size_t number_of_cols_impl() const noexcept {
746 return _number_of_cols;
747 }
748
749 private:
750 size_t _number_of_cols;
751 size_t _number_of_rows;
752 };
753
754 template <typename PlusOp,
755 typename ProdOp,
756 typename ZeroOp,
757 typename OneOp,
758 typename Scalar>
759 struct MatrixStaticArithmetic {
760 MatrixStaticArithmetic() = default;
761 MatrixStaticArithmetic(MatrixStaticArithmetic const&) = default;
762 MatrixStaticArithmetic(MatrixStaticArithmetic&&) = default;
763 MatrixStaticArithmetic& operator=(MatrixStaticArithmetic const&)
764 = default;
765 MatrixStaticArithmetic& operator=(MatrixStaticArithmetic&&) = default;
766
767 // TODO(2) from here to the end of MatrixStaticArithmetic should be
768 // private or protected
769 using scalar_type = Scalar;
770
771 static constexpr scalar_type plus_no_checks_impl(scalar_type x,
772 scalar_type y) noexcept {
773 return PlusOp()(x, y);
774 }
775
776 static constexpr scalar_type
777 product_no_checks_impl(scalar_type x, scalar_type y) noexcept {
778 return ProdOp()(x, y);
779 }
780
781 static constexpr scalar_type one_impl() noexcept {
782 return OneOp()();
783 }
784
785 static constexpr scalar_type zero_impl() noexcept {
786 return ZeroOp()();
787 }
788
789 static constexpr void const* semiring_impl() noexcept {
790 return nullptr;
791 }
792 };
793
795 // RowViews - class for cheaply storing iterators to rows
797
798 template <typename Mat, typename Subclass>
799 class RowViewCommon {
800 static_assert(IsMatrix<Mat>,
801 "the template parameter Mat must be derived from "
802 "MatrixPolymorphicBase");
803
804 public:
805 using const_iterator = typename Mat::const_iterator;
806 using iterator = typename Mat::iterator;
807
808 using scalar_type = typename Mat::scalar_type;
809 using scalar_reference = typename Mat::scalar_reference;
810 using scalar_const_reference = typename Mat::scalar_const_reference;
811
812 using Row = typename Mat::Row;
813 using matrix_type = Mat;
814
815 size_t size() const noexcept {
816 return static_cast<Subclass const*>(this)->length_impl();
817 }
818
819 private:
820 scalar_type plus_no_checks(scalar_type x, scalar_type y) const noexcept {
821 return static_cast<Subclass const*>(this)->plus_no_checks_impl(y, x);
822 }
823
824 scalar_type product_no_checks(scalar_type x,
825 scalar_type y) const noexcept {
826 return static_cast<Subclass const*>(this)->product_no_checks_impl(y, x);
827 }
828
829 public:
830 RowViewCommon() = default;
831 RowViewCommon(RowViewCommon const&) = default;
832 RowViewCommon(RowViewCommon&&) = default;
833 RowViewCommon& operator=(RowViewCommon const&) = default;
834 RowViewCommon& operator=(RowViewCommon&&) = default;
835
836 explicit RowViewCommon(Row const& r)
837 : RowViewCommon(const_cast<Row&>(r).begin()) {}
838
839 // Not noexcept because iterator::operator[] isn't
840 scalar_const_reference operator[](size_t i) const {
841 return _begin[i];
842 }
843
844 // Not noexcept because iterator::operator[] isn't
845 scalar_reference operator[](size_t i) {
846 return _begin[i];
847 }
848
849 // Not noexcept because iterator::operator[] isn't
850 scalar_const_reference operator()(size_t i) const {
851 return (*this)[i];
852 }
853
854 // Not noexcept because iterator::operator[] isn't
855 scalar_reference operator()(size_t i) {
856 return (*this)[i];
857 }
858
859 // noexcept because begin() is
860 const_iterator cbegin() const noexcept {
861 return _begin;
862 }
863
864 // not noexcept because iterator arithmetic isn't
865 const_iterator cend() const {
866 return _begin + size();
867 }
868
869 // noexcept because begin() is
870 const_iterator begin() const noexcept {
871 return _begin;
872 }
873
874 // not noexcept because iterator arithmetic isn't
875 const_iterator end() const {
876 return _begin + size();
877 }
878
879 // noexcept because begin() is
880 iterator begin() noexcept {
881 return _begin;
882 }
883
884 // not noexcept because iterator arithmetic isn't
885 iterator end() noexcept {
886 return _begin + size();
887 }
888
890 // Arithmetic operators
892
893 // not noexcept because operator[] isn't
894 void operator+=(RowViewCommon const& x) {
895 auto& this_ = *this;
896 for (size_t i = 0; i < size(); ++i) {
897 this_[i] = plus_no_checks(this_[i], x[i]);
898 }
899 }
900
901 // not noexcept because iterator arithmeic isn't
902 void operator+=(scalar_type a) {
903 for (auto& x : *this) {
904 x = plus_no_checks(x, a);
905 }
906 }
907
908 // not noexcept because iterator arithmeic isn't
909 void operator*=(scalar_type a) {
910 for (auto& x : *this) {
911 x = product_no_checks(x, a);
912 }
913 }
914
915 // not noexcept because operator*= isn't
916 Row operator*(scalar_type a) const {
917 Row result(*static_cast<Subclass const*>(this));
918 result *= a;
919 return result;
920 }
921
922 // not noexcept because operator+= isn't
923 Row operator+(RowViewCommon const& that) const {
924 Row result(*static_cast<Subclass const*>(this));
925 result += static_cast<Subclass const&>(that);
926 return result;
927 }
928
929 template <typename U>
930 bool operator==(U const& that) const {
931 // TODO(1) static assert that U is Row or RowView
932 return std::equal(begin(), end(), that.begin());
933 }
934
935 template <typename U>
936 bool operator!=(U const& that) const {
937 return !(*this == that);
938 }
939
940 template <typename U>
941 bool operator<(U const& that) const {
943 cbegin(), cend(), that.cbegin(), that.cend());
944 }
945
946 template <typename U>
947 bool operator>(U const& that) const {
948 return that < *this;
949 }
950
951 void swap(RowViewCommon& that) noexcept {
952 std::swap(that._begin, _begin);
953 }
954
955 friend std::ostream& operator<<(std::ostream& os,
956 RowViewCommon const& x) {
957 os << detail::to_string(x);
958 return os;
959 }
960
961 protected:
962 explicit RowViewCommon(iterator first) : _begin(first) {}
963
964 private:
965 iterator _begin;
966 };
967
968 template <typename Container>
969 void throw_if_any_row_wrong_size(Container const& m) {
970 if (m.size() <= 1) {
971 return;
972 }
973 uint64_t const C = m.begin()->size();
974 auto it = std::find_if_not(
975 m.begin() + 1, m.end(), [&C](typename Container::const_reference r) {
976 return r.size() == C;
977 });
978 if (it != m.end()) {
979 LIBSEMIGROUPS_EXCEPTION("invalid argument, expected every item to "
980 "have length {}, found {} in entry {}",
981 C,
982 it->size(),
983 std::distance(m.begin(), it));
984 }
985 }
986
987 template <typename Scalar>
988 void throw_if_any_row_wrong_size(
989 std::initializer_list<std::initializer_list<Scalar>> m) {
990 throw_if_any_row_wrong_size<
991 std::initializer_list<std::initializer_list<Scalar>>>(m);
992 }
993
994 } // namespace detail
995
997 // Matrix forward declarations
999
1000#ifndef LIBSEMIGROUPS_PARSED_BY_DOXYGEN
1001 template <typename PlusOp,
1002 typename ProdOp,
1003 typename ZeroOp,
1004 typename OneOp,
1005 size_t R,
1006 size_t C,
1007 typename Scalar>
1008 class StaticMatrix;
1009
1010 template <typename... Args>
1011 class DynamicMatrix;
1012
1013 template <typename PlusOp,
1014 typename ProdOp,
1015 typename ZeroOp,
1016 typename OneOp,
1017 typename Scalar>
1018 class DynamicMatrix<PlusOp, ProdOp, ZeroOp, OneOp, Scalar>;
1019
1020 template <typename Semiring, typename Scalar>
1021 class DynamicMatrix<Semiring, Scalar>;
1022#endif
1023
1028
1030 // StaticRowViews - static arithmetic
1032
1069 template <typename PlusOp,
1070 typename ProdOp,
1071 typename ZeroOp,
1072 typename OneOp,
1073 size_t C,
1074 typename Scalar>
1076 : public detail::RowViewCommon<
1077 StaticMatrix<PlusOp, ProdOp, ZeroOp, OneOp, 1, C, Scalar>,
1078 StaticRowView<PlusOp, ProdOp, ZeroOp, OneOp, C, Scalar>>,
1079 public detail::
1080 MatrixStaticArithmetic<PlusOp, ProdOp, ZeroOp, OneOp, Scalar> {
1081 private:
1082 using RowViewCommon = detail::RowViewCommon<
1085 friend RowViewCommon;
1086
1087 template <size_t R>
1088 using StaticMatrix_ = ::libsemigroups::
1089 StaticMatrix<PlusOp, ProdOp, ZeroOp, OneOp, R, C, Scalar>;
1090
1091 public:
1093 using const_iterator = typename RowViewCommon::const_iterator;
1094
1096 using iterator = typename RowViewCommon::iterator;
1097
1099 using scalar_type = Scalar;
1100
1102 using scalar_reference = typename RowViewCommon::scalar_reference;
1103
1105 // clang-format off
1106 // NOLINTNEXTLINE(whitespace/line_length)
1107 using scalar_const_reference = typename RowViewCommon::scalar_const_reference;
1108 // clang-format on
1109
1111 using matrix_type = typename RowViewCommon::matrix_type;
1112
1114 using Row = typename matrix_type::Row;
1115
1117 StaticRowView() = default;
1118
1120 StaticRowView(StaticRowView const&) = default;
1121
1124
1127
1130
1131#ifndef LIBSEMIGROUPS_PARSED_BY_DOXYGEN
1132 using RowViewCommon::RowViewCommon;
1133
1134 // TODO(2) This constructor should be private
1135 template <size_t R>
1136 StaticRowView(StaticMatrix_<R> const*,
1137 typename RowViewCommon::iterator it,
1138 size_t)
1139 : RowViewCommon(it) {}
1140
1141 using RowViewCommon::size;
1142#else
1154 explicit StaticRowView(Row const& r);
1155
1168 static constexpr size_t size() const noexcept;
1169
1182 iterator begin() noexcept;
1183
1198
1211 const_iterator cbegin() const noexcept;
1212
1227
1245 scalar_reference operator()(size_t i);
1246
1264 scalar_const_reference operator()(size_t i) const;
1265
1267 scalar_reference operator[](size_t i);
1268
1270 scalar_const_reference operator[](size_t i) const;
1271
1292 template <typename U>
1293 bool operator==(U const& that) const;
1294
1315 template <typename U>
1316 bool operator!=(U const& that) const;
1317
1323 // clang-format off
1324 // NOLINTNEXTLINE(whitespace/line_length)
1328 // clang-format on
1341 template <typename U>
1342 bool operator<(U const& that) const;
1343
1365 template <typename U>
1366 bool operator<(U const& that) const;
1367
1387 Row operator+(StaticRowView const& that);
1388
1407 void operator+=(StaticRowView const& that);
1408
1422 void operator+=(scalar_type a);
1423
1441 Row operator*(scalar_type a) const;
1442
1456 void operator*=(scalar_type a);
1457#endif
1458
1459 private:
1460 static constexpr size_t length_impl() noexcept {
1461 return C;
1462 }
1463 };
1464
1466 // DynamicRowViews - static arithmetic
1468
1469#ifndef LIBSEMIGROUPS_PARSED_BY_DOXYGEN
1470 // Doxygen needs to ignore this so that the actual implementation of
1471 // DynamicRowView gets documented.
1472 template <typename... Args>
1473 class DynamicRowView;
1474#endif
1475
1513 template <typename PlusOp,
1514 typename ProdOp,
1515 typename ZeroOp,
1516 typename OneOp,
1517 typename Scalar>
1518 class DynamicRowView<PlusOp, ProdOp, ZeroOp, OneOp, Scalar>
1519 : public detail::RowViewCommon<
1520 DynamicMatrix<PlusOp, ProdOp, ZeroOp, OneOp, Scalar>,
1521 DynamicRowView<PlusOp, ProdOp, ZeroOp, OneOp, Scalar>>,
1522 public detail::
1523 MatrixStaticArithmetic<PlusOp, ProdOp, ZeroOp, OneOp, Scalar> {
1524 private:
1525 using DynamicMatrix_ = DynamicMatrix<PlusOp, ProdOp, ZeroOp, OneOp, Scalar>;
1526 using RowViewCommon = detail::RowViewCommon<
1527 DynamicMatrix_,
1529 friend RowViewCommon;
1530
1531 public:
1533 using const_iterator = typename RowViewCommon::const_iterator;
1534
1536 using iterator = typename RowViewCommon::iterator;
1537
1539 using scalar_type = Scalar;
1540
1542 using scalar_reference = typename RowViewCommon::scalar_reference;
1543
1545 // clang-format off
1546 // NOLINTNEXTLINE(whitespace/line_length)
1547 using scalar_const_reference = typename RowViewCommon::scalar_const_reference;
1548 // clang-format on
1549
1551 using matrix_type = typename RowViewCommon::matrix_type;
1552
1554 using Row = typename matrix_type::Row;
1555
1557 DynamicRowView() = default;
1558
1561
1564
1567
1570
1572 explicit DynamicRowView(Row const& r)
1573 : RowViewCommon(r), _length(r.number_of_cols()) {}
1574
1575#ifndef LIBSEMIGROUPS_PARSED_BY_DOXYGEN
1576 using RowViewCommon::RowViewCommon;
1577
1578 // TODO(2) This constructor should be private
1579 DynamicRowView(DynamicMatrix_ const*, iterator const& it, size_t N)
1580 : RowViewCommon(it), _length(N) {}
1581
1582 using RowViewCommon::size;
1583#else
1585 size_t size() const noexcept;
1586
1588 iterator begin() noexcept;
1589
1592
1594 const_iterator cbegin() const noexcept;
1595
1598
1600 scalar_reference operator()(size_t i);
1601
1603 scalar_const_reference operator()(size_t i) const;
1604
1606 scalar_reference operator[](size_t i);
1607
1609 scalar_const_reference operator[](size_t i) const;
1610
1612 template <typename U>
1613 bool operator==(U const& that) const;
1614
1616 template <typename U>
1617 bool operator!=(U const& that) const;
1618
1620 template <typename U>
1621 bool operator<(U const& that) const;
1622
1624 template <typename U>
1625 bool operator<(U const& that) const;
1626
1628 Row operator+(DynamicRowView const& that);
1629
1631 void operator+=(DynamicRowView const& that);
1632
1634 void operator+=(scalar_type a);
1635
1637 Row operator*(scalar_type a) const;
1638
1640 void operator*=(scalar_type a);
1641#endif // LIBSEMIGROUPS_PARSED_BY_DOXYGEN
1642
1643 private:
1644 size_t length_impl() const noexcept {
1645 return _length;
1646 }
1647 size_t _length;
1648 };
1649
1651 // DynamicRowViews - dynamic arithmetic
1653
1674 template <typename Semiring, typename Scalar>
1675 class DynamicRowView<Semiring, Scalar>
1676 : public detail::RowViewCommon<DynamicMatrix<Semiring, Scalar>,
1677 DynamicRowView<Semiring, Scalar>> {
1678 private:
1679 using DynamicMatrix_ = DynamicMatrix<Semiring, Scalar>;
1680 friend DynamicMatrix_;
1681 using RowViewCommon
1682 = detail::RowViewCommon<DynamicMatrix_,
1684 friend RowViewCommon;
1685
1686 public:
1688 using const_iterator = typename RowViewCommon::const_iterator;
1689
1691 using iterator = typename RowViewCommon::iterator;
1692
1694 using scalar_type = Scalar;
1695
1697 using scalar_reference = typename RowViewCommon::scalar_reference;
1698
1700 // clang-format off
1701 // NOLINTNEXTLINE(whitespace/line_length)
1702 using scalar_const_reference = typename RowViewCommon::scalar_const_reference;
1703 // clang-format on
1704
1706 using matrix_type = typename RowViewCommon::matrix_type;
1707
1709 using Row = typename matrix_type::Row;
1710
1712 DynamicRowView() = default;
1713
1715 DynamicRowView(DynamicRowView const&) = default;
1716
1718 DynamicRowView(DynamicRowView&&) = default;
1719
1721 DynamicRowView& operator=(DynamicRowView const&) = default;
1722
1725
1727 explicit DynamicRowView(Row const& r) : RowViewCommon(r), _matrix(&r) {}
1728
1729#ifndef LIBSEMIGROUPS_PARSED_BY_DOXYGEN
1730 using RowViewCommon::RowViewCommon;
1731
1732 // TODO(2) This constructor should be private
1733 DynamicRowView(DynamicMatrix_ const* mat, iterator const& it, size_t)
1734 : RowViewCommon(it), _matrix(mat) {}
1735
1736 using RowViewCommon::size;
1737#else
1739 size_t size() const noexcept;
1740
1742 iterator begin() noexcept;
1743
1745 iterator end();
1746
1748 const_iterator cbegin() const noexcept;
1749
1751 iterator cend();
1752
1754 scalar_reference operator()(size_t i);
1755
1757 scalar_const_reference operator()(size_t i) const;
1758
1760 scalar_reference operator[](size_t i);
1761
1763 scalar_const_reference operator[](size_t i) const;
1764
1766 template <typename U>
1767 bool operator==(U const& that) const;
1768
1770 template <typename U>
1771 bool operator!=(U const& that) const;
1772
1774 template <typename U>
1775 bool operator<(U const& that) const;
1776
1778 template <typename U>
1779 bool operator<(U const& that) const;
1780
1782 Row operator+(DynamicRowView const& that);
1783
1785 void operator+=(DynamicRowView const& that);
1786
1788 void operator+=(scalar_type a);
1789
1791 Row operator*(scalar_type a) const;
1792
1794 void operator*=(scalar_type a);
1795#endif
1796
1797 private:
1798 size_t length_impl() const noexcept {
1799 return _matrix->number_of_cols();
1800 }
1801
1802 scalar_type plus_no_checks_impl(scalar_type x,
1803 scalar_type y) const noexcept {
1804 return _matrix->plus_no_checks_impl(x, y);
1805 }
1806
1807 scalar_type product_no_checks_impl(scalar_type x,
1808 scalar_type y) const noexcept {
1809 return _matrix->product_no_checks_impl(x, y);
1810 }
1811
1812 DynamicMatrix_ const* _matrix;
1813 };
1814
1816 // StaticMatrix with compile time semiring arithmetic
1818
1852 template <typename PlusOp,
1853 typename ProdOp,
1854 typename ZeroOp,
1855 typename OneOp,
1856 size_t R,
1857 size_t C,
1858 typename Scalar>
1860 : public detail::
1861 MatrixStaticArithmetic<PlusOp, ProdOp, ZeroOp, OneOp, Scalar>,
1862 public detail::MatrixCommon<
1863 std::array<Scalar, R * C>,
1864 StaticMatrix<PlusOp, ProdOp, ZeroOp, OneOp, R, C, Scalar>,
1865 StaticRowView<PlusOp, ProdOp, ZeroOp, OneOp, C, Scalar>> {
1867 // StaticMatrix - Aliases - private
1869
1870 using MatrixCommon = ::libsemigroups::detail::MatrixCommon<
1874 friend MatrixCommon;
1875
1876 public:
1878 // StaticMatrix - Aliases - public
1880
1882 using scalar_type = typename MatrixCommon::scalar_type;
1883
1885 using scalar_reference = typename MatrixCommon::scalar_reference;
1886
1888 // clang-format off
1889 // NOLINTNEXTLINE(whitespace/line_length)
1890 using scalar_const_reference = typename MatrixCommon::scalar_const_reference;
1891 // clang-format on
1892
1895
1898
1900 using Plus = PlusOp;
1901
1903 using Prod = ProdOp;
1904
1906 using Zero = ZeroOp;
1907
1909 using One = OneOp;
1910
1912 using iterator = typename MatrixCommon::iterator;
1913
1915 using const_iterator = typename MatrixCommon::const_iterator;
1916
1917 static constexpr size_t nr_rows = R;
1918 static constexpr size_t nr_cols = C;
1919
1921 // StaticMatrix - Constructors + destructor - public
1923
1941 : MatrixCommon(c) {
1942 static_assert(R == 1,
1943 "cannot construct Matrix from the given initializer list, "
1944 "incompatible dimensions");
1945 LIBSEMIGROUPS_ASSERT(c.size() == C);
1946 }
1947
1969 : MatrixCommon(m) {}
1970
1984 : MatrixCommon(m) {}
1985
2003 explicit StaticMatrix(RowView const& rv) : MatrixCommon(rv) {
2004 static_assert(
2005 R == 1,
2006 "cannot construct Matrix with more than one row from RowView!");
2007 }
2008
2012 StaticMatrix() = default;
2013
2017 StaticMatrix(StaticMatrix const&) = default;
2018
2023
2028
2033
2034#ifndef LIBSEMIGROUPS_PARSED_BY_DOXYGEN
2035 // For uniformity of interface, the args do nothing
2036 StaticMatrix(size_t r, size_t c) : StaticMatrix() {
2037 (void) r;
2038 (void) c;
2039 LIBSEMIGROUPS_ASSERT(r == number_of_rows());
2040 LIBSEMIGROUPS_ASSERT(c == number_of_cols());
2041 }
2042
2043 // For uniformity of interface, the first arg does nothing
2044 StaticMatrix(void const* ptr, std::initializer_list<scalar_type> const& c)
2045 : StaticMatrix(c) {
2046 (void) ptr;
2047 LIBSEMIGROUPS_ASSERT(ptr == nullptr);
2048 }
2049
2050 // For uniformity of interface, the first arg does nothing
2052 void const* ptr,
2054 : StaticMatrix(m) {
2055 (void) ptr;
2056 LIBSEMIGROUPS_ASSERT(ptr == nullptr);
2057 }
2058
2059 // For uniformity of interface, the first arg does nothing
2060 explicit StaticMatrix(void const* ptr, RowView const& rv)
2061 : StaticMatrix(rv) {
2062 (void) ptr;
2063 LIBSEMIGROUPS_ASSERT(ptr == nullptr);
2064 }
2065
2066 // For uniformity of interface, no arg used for anything
2067 StaticMatrix(void const* ptr, size_t r, size_t c) : StaticMatrix(r, c) {
2068 (void) ptr;
2069 LIBSEMIGROUPS_ASSERT(ptr == nullptr);
2070 }
2071#endif
2072
2073 ~StaticMatrix() = default;
2074
2075#ifndef LIBSEMIGROUPS_PARSED_BY_DOXYGEN
2076 static StaticMatrix one(size_t n) {
2077 // If specified the value of n must equal R or otherwise weirdness will
2078 // ensue...
2079 LIBSEMIGROUPS_ASSERT(n == 0 || n == R);
2080 (void) n;
2081#if defined(__APPLE__) && defined(__clang__) \
2082 && (__clang_major__ == 13 || __clang_major__ == 14)
2083 // With Apple clang version 13.1.6 (clang-1316.0.21.2.5) something goes
2084 // wrong and the value R is optimized away somehow, meaning that the
2085 // values on the diagonal aren't actually set. This only occurs when
2086 // libsemigroups is compiled with -O2 or higher.
2087 size_t volatile const m = R;
2088#else
2089 size_t const m = R;
2090#endif
2091 StaticMatrix x(m, m);
2092 std::fill(x.begin(), x.end(), ZeroOp()());
2093 for (size_t r = 0; r < m; ++r) {
2094 x(r, r) = OneOp()();
2095 }
2096 return x;
2097 }
2098
2099 static StaticMatrix one(void const* ptr, size_t n = 0) {
2100 (void) ptr;
2101 LIBSEMIGROUPS_ASSERT(ptr == nullptr);
2102 LIBSEMIGROUPS_ASSERT(n == 0 || n == R);
2103 return one(n);
2104 }
2105#endif // LIBSEMIGROUPS_PARSED_BY_DOXYGEN
2106
2108 // StaticMatrix - member function aliases - public
2110#ifdef LIBSEMIGROUPS_PARSED_BY_DOXYGEN
2129 scalar_reference operator()(size_t r, size_t c);
2130
2144 scalar_reference at(size_t r, size_t c);
2145
2164 scalar_const_reference operator()(size_t r, size_t c) const;
2165
2179 scalar_const_reference at(size_t r, size_t c) const;
2180
2197 iterator begin() noexcept;
2198
2214
2232 const_iterator cbegin() const noexcept;
2233
2253
2267 bool operator==(StaticMatrix const& that) const;
2268
2270 bool operator==(RowView const& that) const;
2271
2283 bool operator!=(StaticMatrix const& that) const;
2284
2286 bool operator!=(RowView const& that) const;
2287
2301 bool operator<(StaticMatrix const& that) const;
2302
2304 bool operator<(RowView const& that) const;
2305
2319 bool operator>(StaticMatrix const& that) const;
2320
2337
2349 size_t number_of_rows() const noexcept;
2350
2362 size_t number_of_cols() const noexcept;
2363
2381 StaticMatrix operator+(StaticMatrix const& that);
2382
2399 void operator+=(StaticMatrix const& that);
2400
2402 void operator+=(RowView const& that);
2403
2415 void operator+=(scalar_type a);
2416
2434 StaticMatrix operator*(StaticMatrix const& that);
2435
2447 void operator*=(scalar_type a);
2448
2466 StaticMatrix const& y);
2467
2483 RowView row_no_checks(size_t i) const;
2484
2495 RowView row(size_t i) const;
2496
2510 template <typename T>
2511 void rows(T& x) const;
2512
2525 void swap(StaticMatrix& that) noexcept;
2526
2540
2556
2568 static StaticMatrix one() const;
2569
2583 size_t hash_value() const;
2584
2599 template <typename U>
2600 bool operator<=(U const& that) const;
2601
2616 template <typename U>
2617 bool operator>=(U const& that) const;
2618
2632
2647
2658 scalar_type scalar_zero() const noexcept;
2659
2670 scalar_type scalar_one() const noexcept;
2671
2683 semiring_type const* semiring() const noexcept;
2684
2685#else
2686 using MatrixCommon::at;
2687 using MatrixCommon::begin;
2688 using MatrixCommon::cbegin;
2689 using MatrixCommon::cend;
2690 using MatrixCommon::coords;
2691 using MatrixCommon::end;
2692 using MatrixCommon::hash_value;
2693 using MatrixCommon::number_of_cols;
2694 using MatrixCommon::number_of_rows;
2695 using MatrixCommon::one;
2696 using MatrixCommon::operator!=;
2697 using MatrixCommon::operator();
2698 using MatrixCommon::operator*;
2699 using MatrixCommon::operator*=;
2700 using MatrixCommon::operator+;
2701 using MatrixCommon::operator+=;
2702 using MatrixCommon::operator<; // NOLINT(whitespace/operators)
2703 using MatrixCommon::operator<=;
2704 using MatrixCommon::operator==;
2705 using MatrixCommon::operator>; // NOLINT(whitespace/operators)
2706 using MatrixCommon::operator>=;
2707 using MatrixCommon::product_inplace_no_checks;
2708 using MatrixCommon::row;
2709 using MatrixCommon::row_no_checks;
2710 using MatrixCommon::rows;
2711 using MatrixCommon::scalar_one;
2712 using MatrixCommon::scalar_zero;
2713 using MatrixCommon::semiring;
2714 using MatrixCommon::swap;
2715 using MatrixCommon::transpose;
2716 using MatrixCommon::transpose_no_checks;
2717#endif // LIBSEMIGROUPS_PARSED_BY_DOXYGEN
2718
2719 private:
2721 // StaticMatrix - implementation of MatrixCommon requirements - private
2723
2724 static constexpr size_t number_of_rows_impl() noexcept {
2725 return R;
2726 }
2727 static constexpr size_t number_of_cols_impl() noexcept {
2728 return C;
2729 }
2730 };
2731
2733 // DynamicMatrix with compile time semiring arithmetic
2735
2769 template <typename PlusOp,
2770 typename ProdOp,
2771 typename ZeroOp,
2772 typename OneOp,
2773 typename Scalar>
2774 class DynamicMatrix<PlusOp, ProdOp, ZeroOp, OneOp, Scalar>
2775 : public detail::MatrixDynamicDim<Scalar>,
2776 public detail::MatrixCommon<
2777 std::vector<Scalar>,
2778 DynamicMatrix<PlusOp, ProdOp, ZeroOp, OneOp, Scalar>,
2779 DynamicRowView<PlusOp, ProdOp, ZeroOp, OneOp, Scalar>>,
2780 public detail::
2781 MatrixStaticArithmetic<PlusOp, ProdOp, ZeroOp, OneOp, Scalar> {
2782 using MatrixDynamicDim = ::libsemigroups::detail::MatrixDynamicDim<Scalar>;
2783 using MatrixCommon = ::libsemigroups::detail::MatrixCommon<
2786 DynamicRowView<PlusOp, ProdOp, ZeroOp, OneOp, Scalar>>;
2787 friend MatrixCommon;
2788
2789 public:
2791 using scalar_type = typename MatrixCommon::scalar_type;
2792
2794 using scalar_reference = typename MatrixCommon::scalar_reference;
2795
2797 // clang-format off
2798 // NOLINTNEXTLINE(whitespace/line_length)
2799 using scalar_const_reference = typename MatrixCommon::scalar_const_reference;
2800 // clang-format on
2801
2804
2806 using RowView = DynamicRowView<PlusOp, ProdOp, ZeroOp, OneOp, Scalar>;
2807
2809 using Plus = PlusOp;
2810
2812 using Prod = ProdOp;
2813
2815 using Zero = ZeroOp;
2816
2818 using One = OneOp;
2819
2825 using semiring_type = void;
2826
2830 DynamicMatrix() = default;
2831
2835 DynamicMatrix(DynamicMatrix const&) = default;
2836
2841
2846
2851
2870 DynamicMatrix(size_t r, size_t c) : MatrixDynamicDim(r, c), MatrixCommon() {
2871 resize(number_of_rows(), number_of_cols());
2872 }
2873
2894 : MatrixDynamicDim(1, c.size()), MatrixCommon(c) {}
2895
2918 : MatrixDynamicDim(m.size(), std::empty(m) ? 0 : m.begin()->size()),
2919 MatrixCommon(m) {}
2920
2936 : MatrixDynamicDim(m.size(), std::empty(m) ? 0 : m.begin()->size()),
2937 MatrixCommon(m) {}
2938
2950 explicit DynamicMatrix(RowView const& rv)
2951 : MatrixDynamicDim(1, rv.size()), MatrixCommon(rv) {}
2952
2953#ifndef LIBSEMIGROUPS_PARSED_BY_DOXYGEN
2954 // For uniformity of interface, the first arg does nothing
2955 DynamicMatrix(void const* ptr, size_t r, size_t c) : DynamicMatrix(r, c) {
2956 (void) ptr;
2957 LIBSEMIGROUPS_ASSERT(ptr == nullptr);
2958 }
2959
2960 // For uniformity of interface, the first arg does nothing
2961 DynamicMatrix(void const* ptr, std::initializer_list<scalar_type> const& c)
2962 : DynamicMatrix(c) {
2963 (void) ptr;
2964 LIBSEMIGROUPS_ASSERT(ptr == nullptr);
2965 }
2966
2967 // For uniformity of interface, the first arg does nothing
2968 DynamicMatrix(
2969 void const* ptr,
2970 std::initializer_list<std::initializer_list<scalar_type>> const& m)
2971 : DynamicMatrix(m) {
2972 (void) ptr;
2973 LIBSEMIGROUPS_ASSERT(ptr == nullptr);
2974 }
2975
2976 static DynamicMatrix one(void const* ptr, size_t n) {
2977 (void) ptr;
2978 LIBSEMIGROUPS_ASSERT(ptr == nullptr);
2979 return one(n);
2980 }
2981#endif // LIBSEMIGROUPS_PARSED_BY_DOXYGEN
2982
2983 ~DynamicMatrix() = default;
2984
2997 static DynamicMatrix one(size_t n) {
2998 DynamicMatrix x(n, n);
2999 std::fill(x.begin(), x.end(), ZeroOp()());
3000 for (size_t r = 0; r < n; ++r) {
3001 x(r, r) = OneOp()();
3002 }
3003 return x;
3004 }
3005
3006#ifdef LIBSEMIGROUPS_PARSED_BY_DOXYGEN
3008 scalar_reference at(size_t r, size_t c);
3009
3011 scalar_reference at(size_t r, size_t c) const;
3012
3014 iterator begin() noexcept;
3015
3017 const_iterator cbegin() noexcept;
3018
3020 const_iterator cend() noexcept;
3021
3023 std::pair<scalar_type, scalar_type> coords(const_iterator it) const;
3024
3026 iterator end() noexcept;
3027
3029 size_t hash_value() const;
3030
3032 size_t number_of_cols() const noexcept;
3033
3035 size_t number_of_rows() const noexcept;
3036
3038 bool operator!=(DynamicMatrix const& that) const;
3039
3041 bool operator!=(RowView const& that) const;
3042
3044 scalar_reference operator()(size_t r, size_t c);
3045
3047 scalar_const_reference operator()(size_t r, size_t c) const;
3060
3062 DynamicMatrix operator*(DynamicMatrix const& that);
3063
3065 void operator*=(scalar_type a);
3066
3068 DynamicMatrix operator+(DynamicMatrix const& that);
3069
3071 void operator+=(DynamicMatrix const& that);
3072
3074 void operator+=(RowView const& that);
3075
3087 void operator+=(scalar_type a);
3088
3090 bool operator<(DynamicMatrix const& that) const;
3091
3093 bool operator<(RowView const& that) const;
3094
3096 template <typename T>
3097 bool operator<=(T const& that) const;
3098
3100 bool operator==(DynamicMatrix const& that) const;
3101
3103 bool operator==(RowView const& that) const;
3104
3106 bool operator>(DynamicMatrix const& that) const;
3107
3109 template <typename T>
3110 bool operator>=(T const& that) const;
3111
3114 DynamicMatrix const& y);
3115
3117 RowView row(size_t i) const;
3118
3120 RowView row_no_checks(size_t i) const;
3121
3123 template <typename T>
3124 void rows(T& x) const;
3125
3127 scalar_type scalar_one() const noexcept;
3128
3130 scalar_type scalar_zero() const noexcept;
3131
3133 semiring_type const* semiring() const noexcept;
3134
3137
3140#else
3141 using MatrixCommon::at;
3142 using MatrixCommon::begin;
3143 using MatrixCommon::cbegin;
3144 using MatrixCommon::cend;
3145 using MatrixCommon::coords;
3146 using MatrixCommon::end;
3147 using MatrixCommon::hash_value;
3148 using MatrixCommon::number_of_cols;
3149 using MatrixCommon::number_of_rows;
3150 using MatrixCommon::one;
3151 using MatrixCommon::operator!=;
3152 using MatrixCommon::operator();
3153 using MatrixCommon::operator*;
3154 using MatrixCommon::operator*=;
3155 using MatrixCommon::operator+;
3156 using MatrixCommon::operator+=;
3157 using MatrixCommon::operator<; // NOLINT(whitespace/operators)
3158 using MatrixCommon::operator<=;
3159 using MatrixCommon::operator==;
3160 using MatrixCommon::operator>; // NOLINT(whitespace/operators)
3161 using MatrixCommon::operator>=;
3162 using MatrixCommon::product_inplace_no_checks;
3163 using MatrixCommon::row;
3164 using MatrixCommon::row_no_checks;
3165 using MatrixCommon::rows;
3166 using MatrixCommon::scalar_one;
3167 using MatrixCommon::scalar_zero;
3168 using MatrixCommon::semiring;
3169 // using MatrixCommon::swap; // Don't want this use the one below.
3170 using MatrixCommon::transpose;
3171 using MatrixCommon::transpose_no_checks;
3172#endif // LIBSEMIGROUPS_PARSED_BY_DOXYGEN
3173
3175 void swap(DynamicMatrix& that) noexcept {
3176 static_cast<MatrixDynamicDim&>(*this).swap(
3177 static_cast<MatrixDynamicDim&>(that));
3178 static_cast<MatrixCommon&>(*this).swap(static_cast<MatrixCommon&>(that));
3179 }
3180
3181 private:
3182 using MatrixCommon::resize;
3183 };
3184
3186 // DynamicMatrix with runtime semiring arithmetic
3188
3223 template <typename Semiring, typename Scalar>
3224 class DynamicMatrix<Semiring, Scalar>
3225 : public detail::MatrixDynamicDim<Scalar>,
3226 public detail::MatrixCommon<std::vector<Scalar>,
3227 DynamicMatrix<Semiring, Scalar>,
3228 DynamicRowView<Semiring, Scalar>,
3229 Semiring> {
3230 using MatrixCommon = detail::MatrixCommon<std::vector<Scalar>,
3232 DynamicRowView<Semiring, Scalar>,
3233 Semiring>;
3234 friend MatrixCommon;
3235 using MatrixDynamicDim = ::libsemigroups::detail::MatrixDynamicDim<Scalar>;
3236
3237 public:
3239 using scalar_type = typename MatrixCommon::scalar_type;
3240
3242 using scalar_reference = typename MatrixCommon::scalar_reference;
3243
3245 // clang-format off
3246 // NOLINTNEXTLINE(whitespace/line_length)
3247 using scalar_const_reference = typename MatrixCommon::scalar_const_reference;
3248 // clang-format on
3249
3252
3254 using RowView = DynamicRowView<Semiring, Scalar>;
3255
3256 friend RowView;
3257
3259 using semiring_type = Semiring;
3260
3266 DynamicMatrix() = delete;
3267
3269 DynamicMatrix(DynamicMatrix const&) = default;
3270
3273
3276
3279
3294 DynamicMatrix(Semiring const* sr, size_t r, size_t c)
3295 : MatrixDynamicDim(r, c), MatrixCommon(), _semiring(sr) {
3296 resize(number_of_rows(), number_of_cols());
3297 }
3298
3315 Semiring const* sr,
3317 : MatrixDynamicDim(rws.size(),
3318 std::empty(rws) ? 0 : rws.begin()->size()),
3319 MatrixCommon(rws),
3320 _semiring(sr) {}
3321
3337 explicit DynamicMatrix(Semiring const* sr,
3339 : MatrixDynamicDim(rws.size(), (rws.empty() ? 0 : rws.begin()->size())),
3340 MatrixCommon(rws),
3341 _semiring(sr) {}
3342
3356 explicit DynamicMatrix(Semiring const* sr,
3358 : MatrixDynamicDim(1, rw.size()), MatrixCommon(rw), _semiring(sr) {}
3359
3371 explicit DynamicMatrix(RowView const& rv)
3372 : MatrixDynamicDim(1, rv.size()),
3373 MatrixCommon(rv),
3374 _semiring(rv._matrix->semiring()) {}
3375
3390 // No static DynamicMatrix::one(size_t n) because we need a semiring!
3391 static DynamicMatrix one(Semiring const* semiring, size_t n) {
3392 DynamicMatrix x(semiring, n, n);
3393 std::fill(x.begin(), x.end(), x.scalar_zero());
3394 for (size_t r = 0; r < n; ++r) {
3395 x(r, r) = x.scalar_one();
3396 }
3397 return x;
3398 }
3399
3400 ~DynamicMatrix() = default;
3401
3402#ifdef LIBSEMIGROUPS_PARSED_BY_DOXYGEN
3404 scalar_reference at(size_t r, size_t c);
3405
3407 scalar_reference at(size_t r, size_t c) const;
3408
3410 iterator begin() noexcept;
3411
3413 const_iterator cbegin() noexcept;
3414
3416 const_iterator cend() noexcept;
3417
3419 std::pair<scalar_type, scalar_type> coords(const_iterator it) const;
3420
3422 iterator end() noexcept;
3423
3425 size_t hash_value() const;
3426
3428 size_t number_of_cols() const noexcept;
3429
3431 size_t number_of_rows() const noexcept;
3432
3434 bool operator!=(DynamicMatrix const& that) const;
3435
3437 bool operator!=(RowView const& that) const;
3438
3440 scalar_reference operator()(size_t r, size_t c);
3441
3443 scalar_const_reference operator()(size_t r, size_t c) const;
3456
3458 DynamicMatrix operator*(DynamicMatrix const& that);
3459
3461 void operator*=(scalar_type a);
3462
3464 DynamicMatrix operator+(DynamicMatrix const& that);
3465
3467 void operator+=(DynamicMatrix const& that);
3468
3470 void operator+=(RowView const& that);
3471
3483 void operator+=(scalar_type a);
3484
3486 bool operator<(DynamicMatrix const& that) const;
3487
3489 bool operator<(RowView const& that) const;
3490
3492 template <typename T>
3493 bool operator<=(T const& that) const;
3494
3496 bool operator==(DynamicMatrix const& that) const;
3497
3499 bool operator==(RowView const& that) const;
3500
3502 bool operator>(DynamicMatrix const& that) const;
3503
3505 template <typename T>
3506 bool operator>=(T const& that) const;
3507
3510 DynamicMatrix const& y);
3511
3513 RowView row(size_t i) const;
3514
3516 RowView row_no_checks(size_t i) const;
3517
3519 template <typename T>
3520 void rows(T& x) const;
3521
3523 scalar_type scalar_one() const noexcept;
3524
3526 scalar_type scalar_zero() const noexcept;
3527
3529 semiring_type const* semiring() const noexcept;
3530
3533
3536#else
3537 using MatrixCommon::at;
3538 using MatrixCommon::begin;
3539 using MatrixCommon::cbegin;
3540 using MatrixCommon::cend;
3541 using MatrixCommon::coords;
3542 using MatrixCommon::end;
3543 using MatrixCommon::hash_value;
3544 using MatrixCommon::number_of_cols;
3545 using MatrixCommon::number_of_rows;
3546 using MatrixCommon::one;
3547 using MatrixCommon::operator!=;
3548 using MatrixCommon::operator();
3549 using MatrixCommon::operator*;
3550 using MatrixCommon::operator*=;
3551 using MatrixCommon::operator+;
3552 using MatrixCommon::operator+=;
3553 using MatrixCommon::operator<; // NOLINT(whitespace/operators)
3554 using MatrixCommon::operator<=;
3555 using MatrixCommon::operator==;
3556 using MatrixCommon::operator>; // NOLINT(whitespace/operators)
3557 using MatrixCommon::operator>=;
3558 using MatrixCommon::product_inplace_no_checks;
3559 using MatrixCommon::row;
3560 using MatrixCommon::row_no_checks;
3561 using MatrixCommon::rows;
3562 using MatrixCommon::scalar_one;
3563 using MatrixCommon::scalar_zero;
3564 using MatrixCommon::semiring;
3565 // using MatrixCommon::swap; // Don't want this use the one below.
3566 using MatrixCommon::transpose;
3567 using MatrixCommon::transpose_no_checks;
3568#endif // LIBSEMIGROUPS_PARSED_BY_DOXYGEN
3569
3571 void swap(DynamicMatrix& that) noexcept {
3572 static_cast<MatrixDynamicDim&>(*this).swap(
3573 static_cast<MatrixDynamicDim&>(that));
3574 static_cast<MatrixCommon&>(*this).swap(static_cast<MatrixCommon&>(that));
3575 std::swap(_semiring, that._semiring);
3576 }
3577
3578 private:
3579 using MatrixCommon::resize;
3580
3581 scalar_type plus_no_checks_impl(scalar_type x,
3582 scalar_type y) const noexcept {
3583 return _semiring->plus_no_checks(x, y);
3584 }
3585
3586 scalar_type product_no_checks_impl(scalar_type x,
3587 scalar_type y) const noexcept {
3588 return _semiring->product_no_checks(x, y);
3589 }
3590
3591 scalar_type one_impl() const noexcept {
3592 return _semiring->scalar_one();
3593 }
3594
3595 scalar_type zero_impl() const noexcept {
3596 return _semiring->scalar_zero();
3597 }
3598
3599 Semiring const* semiring_impl() const noexcept {
3600 return _semiring;
3601 }
3602
3603 Semiring const* _semiring;
3604 };
3605
3607 // Helper structs to check if matrix is static, or has a pointer to a
3608 // semiring
3610
3611 namespace detail {
3612 template <typename T>
3613 struct IsStaticMatrixHelper : std::false_type {};
3614
3615 template <typename PlusOp,
3616 typename ProdOp,
3617 typename ZeroOp,
3618 typename OneOp,
3619 size_t R,
3620 size_t C,
3621 typename Scalar>
3622 struct IsStaticMatrixHelper<
3623 StaticMatrix<PlusOp, ProdOp, ZeroOp, OneOp, R, C, Scalar>>
3624 : std::true_type {};
3625
3626 template <typename T>
3627 struct IsMatWithSemiringHelper : std::false_type {};
3628
3629 template <typename Semiring, typename Scalar>
3630 struct IsMatWithSemiringHelper<DynamicMatrix<Semiring, Scalar>>
3631 : std::true_type {};
3632
3633 template <typename S, typename T = void>
3634 struct IsTruncMatHelper : std::false_type {};
3635
3636 } // namespace detail
3637
3648 template <typename T>
3649 constexpr bool IsStaticMatrix = detail::IsStaticMatrixHelper<T>::value;
3650
3661 template <typename T>
3663
3674 template <typename T>
3675 static constexpr bool IsMatWithSemiring
3676 = detail::IsMatWithSemiringHelper<T>::value;
3677
3678 namespace detail {
3679
3680 template <typename T>
3681 static constexpr bool IsTruncMat = IsTruncMatHelper<T>::value;
3682
3683 template <typename Mat>
3684 void throw_if_semiring_nullptr(Mat const& m) {
3685 if (IsMatWithSemiring<Mat> && m.semiring() == nullptr) {
3687 "the matrix's pointer to a semiring is nullptr!")
3688 }
3689 }
3690
3691 template <typename Mat, typename Container>
3692 auto throw_if_bad_dim(Container const& m)
3693 -> std::enable_if_t<IsStaticMatrix<Mat>> {
3694 // Only call this if you've already called throw_if_any_row_wrong_size
3695 uint64_t const R = m.size();
3696 uint64_t const C = std::empty(m) ? 0 : m.begin()->size();
3697 if (R != Mat::nr_rows || C != Mat::nr_cols) {
3699 "invalid argument, cannot initialize an {}x{} matrix with compile "
3700 "time dimension, with an {}x{} container",
3701 Mat::nr_rows,
3702 Mat::nr_cols,
3703 R,
3704 C);
3705 }
3706 }
3707
3708 template <typename Mat, typename Container>
3709 auto throw_if_bad_dim(Container const&)
3710 -> std::enable_if_t<IsDynamicMatrix<Mat>> {}
3711 } // namespace detail
3712
3721 namespace matrix {
3722
3736 template <typename Mat>
3737 constexpr auto threshold(Mat const&) noexcept
3738 -> std::enable_if_t<!detail::IsTruncMat<Mat>,
3739 typename Mat::scalar_type> {
3740 return UNDEFINED;
3741 }
3742
3756 template <typename Mat>
3757 constexpr auto threshold(Mat const&) noexcept
3758 -> std::enable_if_t<detail::IsTruncMat<Mat> && !IsMatWithSemiring<Mat>,
3759 typename Mat::scalar_type> {
3760 return detail::IsTruncMatHelper<Mat>::threshold;
3761 }
3762
3778 template <typename Mat>
3779 auto threshold(Mat const& x) noexcept
3780 -> std::enable_if_t<detail::IsTruncMat<Mat> && IsMatWithSemiring<Mat>,
3781 typename Mat::scalar_type> {
3782 return x.semiring()->threshold();
3783 }
3784 } // namespace matrix
3785
3787 // Boolean matrices - compile time semiring arithmetic
3789
3823
3846 constexpr bool operator()(bool x, bool y) const noexcept {
3847 return x || y;
3848 }
3849 };
3850
3873 constexpr bool operator()(bool x, bool y) const noexcept {
3874 return x & y;
3875 }
3876 };
3877
3887 struct BooleanOne {
3898 constexpr bool operator()() const noexcept {
3899 return true;
3900 }
3901 };
3902
3923 constexpr bool operator()() const noexcept {
3924 return false;
3925 }
3926 };
3927
3936 // The use of `int` rather than `bool` as the scalar type for dynamic
3937 // boolean matrices is intentional, because the bit iterators implemented in
3938 // std::vector<bool> entail a significant performance penalty.
3940 = DynamicMatrix<BooleanPlus, BooleanProd, BooleanZero, BooleanOne, int>;
3941
3953 template <size_t R, size_t C>
3957 BooleanOne,
3958 R,
3959 C,
3960 int>;
3961
3975 // FLS + JDM considered adding BMat8 and decided it wasn't a good idea.
3976 template <size_t R = 0, size_t C = R>
3977 using BMat
3978 = std::conditional_t<R == 0 || C == 0, DynamicBMat, StaticBMat<R, C>>;
3979
3980 namespace detail {
3981 template <typename T>
3982 struct IsBMatHelper : std::false_type {};
3983
3984 template <size_t R, size_t C>
3985 struct IsBMatHelper<StaticBMat<R, C>> : std::true_type {};
3986
3987 template <>
3988 struct IsBMatHelper<DynamicBMat> : std::true_type {};
3989
3990 template <typename T>
3991 struct BitSetCapacity {
3992 static constexpr size_t value = BitSet<1>::max_size();
3993 };
3994
3995 template <size_t R, size_t C>
3996 struct BitSetCapacity<StaticBMat<R, C>> {
3997 static_assert(R == C, "the number of rows and columns must be equal");
3998 static constexpr size_t value = R;
3999 };
4000 } // namespace detail
4001
4012 template <typename T>
4013 static constexpr bool IsBMat = detail::IsBMatHelper<T>::value;
4014
4015 namespace detail {
4016 // This function is required for exceptions and to_human_readable_repr, so
4017 // that if we encounter an entry of a matrix (Scalar type), then it can be
4018 // printed correctly. If we just did fmt::format("{}", val) and val ==
4019 // POSITIVE_INFINITY, but the type of val is, say, size_t, then this
4020 // wouldn't use the formatter for PositiveInfinity.
4021 //
4022 // Also in fmt v11.1.4 the custom formatter for POSITIVE_INFINITY and
4023 // NEGATIVE_INFINITY stopped working (and I wasn't able to figure out why)
4024 template <typename Scalar>
4025 std::string entry_repr(Scalar a) {
4026 if constexpr (std::is_same_v<Scalar, NegativeInfinity>
4027 || std::is_signed_v<Scalar>) {
4028 if (a == NEGATIVE_INFINITY) {
4029 return u8"-\u221E";
4030 }
4031 }
4032 if (a == POSITIVE_INFINITY) {
4033 return u8"+\u221E";
4034 }
4035 return fmt::format("{}", a);
4036 }
4037 } // namespace detail
4038
4039 namespace matrix {
4040
4056 //! but a matrix shouldn't contain values except \c 0 and \c 1.
4057 template <typename Mat>
4058 std::enable_if_t<IsBMat<Mat>> throw_if_bad_entry(Mat const& m) {
4059 using scalar_type = typename Mat::scalar_type;
4060 auto it = std::find_if_not(
4061 m.cbegin(), m.cend(), [](scalar_type x) { return x == 0 || x == 1; });
4062 if (it != m.cend()) {
4063 auto [r, c] = m.coords(it);
4065 "invalid entry, expected 0 or 1 but found {} in entry ({}, {})",
4066 detail::entry_repr(*it),
4067 r,
4068 c);
4069 }
4070 }
4071
4089 template <typename Mat>
4090 std::enable_if_t<IsBMat<Mat>>
4091 throw_if_bad_entry(Mat const&, typename Mat::scalar_type val) {
4092 if (val != 0 && val != 1) {
4093 LIBSEMIGROUPS_EXCEPTION("invalid entry, expected 0 or 1 but found {}",
4094 detail::entry_repr(val));
4095 }
4096 }
4097 } // namespace matrix
4098
4100 // Integer matrices - compile time semiring arithmetic
4102
4130
4142 //! \tparam Scalar the type of the entries in the matrix.
4143 template <typename Scalar>
4144 struct IntegerPlus {
4155 //! \exceptions
4156 //! \noexcept
4157 constexpr Scalar operator()(Scalar x, Scalar y) const noexcept {
4158 return x + y;
4159 }
4160 };
4161
4173 //! \tparam Scalar the type of the entries in the matrix.
4174 template <typename Scalar>
4175 struct IntegerProd {
4186 //! \exceptions
4187 //! \noexcept
4188 constexpr Scalar operator()(Scalar x, Scalar y) const noexcept {
4189 return x * y;
4190 }
4191 };
4192
4201 //! the additive identity of the integer semiring.
4202 template <typename Scalar>
4203 struct IntegerZero {
4211 //! \exceptions
4212 //! \noexcept
4213 constexpr Scalar operator()() const noexcept {
4214 return 0;
4215 }
4216 };
4217
4226 //! the multiplicative identity of the integer semiring.
4227 template <typename Scalar>
4228 struct IntegerOne {
4236 //! \exceptions
4237 //! \noexcept
4238 constexpr Scalar operator()() const noexcept {
4239 return 1;
4240 }
4241 };
4242
4253 template <typename Scalar>
4254 using DynamicIntMat = DynamicMatrix<IntegerPlus<Scalar>,
4258 Scalar>;
4259
4276 template <size_t R, size_t C, typename Scalar>
4281 R,
4282 C,
4283 Scalar>;
4284
4301 template <size_t R = 0, size_t C = R, typename Scalar = int>
4302 using IntMat = std::conditional_t<R == 0 || C == 0,
4305 namespace detail {
4306 template <typename T>
4307 struct IsIntMatHelper : std::false_type {};
4308
4309 template <size_t R, size_t C, typename Scalar>
4310 struct IsIntMatHelper<StaticIntMat<R, C, Scalar>> : std::true_type {};
4311
4312 template <typename Scalar>
4313 struct IsIntMatHelper<DynamicIntMat<Scalar>> : std::true_type {};
4314 } // namespace detail
4315
4326 template <typename T>
4327 static constexpr bool IsIntMat = detail::IsIntMatHelper<T>::value;
4328
4329 namespace matrix {
4343 //! \param x the matrix to check.
4344 template <typename Mat>
4345 std::enable_if_t<IsIntMat<Mat>> throw_if_bad_entry(Mat const& x) {
4346 using scalar_type = typename Mat::scalar_type;
4347 auto it = std::find_if(x.cbegin(), x.cend(), [](scalar_type val) {
4348 return val == POSITIVE_INFINITY || val == NEGATIVE_INFINITY;
4349 });
4350 if (it != x.cend()) {
4351 auto [r, c] = x.coords(it);
4353 "invalid entry, expected entries to be integers, "
4354 "but found {} in entry ({}, {})",
4355 detail::entry_repr(*it),
4356 r,
4357 c);
4358 }
4359 }
4360
4377 template <typename Mat>
4378 std::enable_if_t<IsIntMat<Mat>>
4379 throw_if_bad_entry(Mat const&, typename Mat::scalar_type val) {
4380 if (val == POSITIVE_INFINITY || val == NEGATIVE_INFINITY) {
4382 "invalid entry, expected entries to be integers, "
4383 "but found {}",
4384 detail::entry_repr(val));
4385 }
4386 }
4387 } // namespace matrix
4388
4390 // Max-plus matrices
4420
4444 // Static arithmetic
4445 template <typename Scalar>
4446 struct MaxPlusPlus {
4447 static_assert(std::is_signed<Scalar>::value,
4448 "MaxPlus requires a signed integer type as parameter!");
4459 //! \exceptions
4460 //! \noexcept
4461 Scalar operator()(Scalar x, Scalar y) const noexcept {
4462 if (x == NEGATIVE_INFINITY) {
4463 return y;
4464 } else if (y == NEGATIVE_INFINITY) {
4465 return x;
4466 }
4467 return std::max(x, y);
4468 }
4469 };
4470
4491 //! integer type).
4492 template <typename Scalar>
4493 struct MaxPlusProd {
4494 static_assert(std::is_signed<Scalar>::value,
4495 "MaxPlus requires a signed integer type as parameter!");
4506 //! \exceptions
4507 //! \noexcept
4508 Scalar operator()(Scalar x, Scalar y) const noexcept {
4509 if (x == NEGATIVE_INFINITY || y == NEGATIVE_INFINITY) {
4510 return NEGATIVE_INFINITY;
4511 }
4512 return x + y;
4513 }
4514 };
4515
4528 //! integer type).
4529 template <typename Scalar>
4530 struct MaxPlusZero {
4531 static_assert(std::is_signed<Scalar>::value,
4532 "MaxPlus requires a signed integer type as parameter!");
4540 //! \exceptions
4541 //! \noexcept
4542 constexpr Scalar operator()() const noexcept {
4543 return NEGATIVE_INFINITY;
4544 }
4545 };
4546
4557 template <typename Scalar>
4558 using DynamicMaxPlusMat = DynamicMatrix<MaxPlusPlus<Scalar>,
4562 Scalar>;
4563
4576 template <size_t R, size_t C, typename Scalar>
4581 R,
4582 C,
4583 Scalar>;
4584
4600 template <size_t R = 0, size_t C = R, typename Scalar = int>
4601 using MaxPlusMat = std::conditional_t<R == 0 || C == 0,
4604
4605 namespace detail {
4606 template <typename T>
4607 struct IsMaxPlusMatHelper : std::false_type {};
4608
4609 template <size_t R, size_t C, typename Scalar>
4610 struct IsMaxPlusMatHelper<StaticMaxPlusMat<R, C, Scalar>> : std::true_type {
4611 };
4612
4613 template <typename Scalar>
4614 struct IsMaxPlusMatHelper<DynamicMaxPlusMat<Scalar>> : std::true_type {};
4615 } // namespace detail
4616
4627 template <typename T>
4628 static constexpr bool IsMaxPlusMat = detail::IsMaxPlusMatHelper<T>::value;
4629
4630 namespace matrix {
4645 //! \ref POSITIVE_INFINITY.
4646 template <typename Mat>
4647 auto throw_if_bad_entry(Mat const& x)
4648 -> std::enable_if_t<IsMaxPlusMat<Mat>> {
4649 using scalar_type = typename Mat::scalar_type;
4650 auto it = std::find_if(x.cbegin(), x.cend(), [](scalar_type val) {
4651 return val == POSITIVE_INFINITY;
4652 });
4653 if (it != x.cend()) {
4654 auto [r, c] = x.coords(it);
4656 "invalid entry, expected entries to be integers or {} (= {}), "
4657 "but found {} (= {}) in entry ({}, {})",
4658 entry_repr(NEGATIVE_INFINITY),
4659 static_cast<scalar_type>(NEGATIVE_INFINITY),
4660 entry_repr(POSITIVE_INFINITY),
4661 static_cast<scalar_type>(POSITIVE_INFINITY),
4662 r,
4663 c);
4664 }
4665 }
4666
4682 template <typename Mat>
4683 std::enable_if_t<IsMaxPlusMat<Mat>>
4684 throw_if_bad_entry(Mat const&, typename Mat::scalar_type val) {
4685 if (val == POSITIVE_INFINITY) {
4686 using scalar_type = typename Mat::scalar_type;
4687 LIBSEMIGROUPS_EXCEPTION("invalid entry, expected entries to be "
4688 "integers or {} (= {}) but found {} (= {})",
4689 entry_repr(NEGATIVE_INFINITY),
4690 static_cast<scalar_type>(NEGATIVE_INFINITY),
4691 entry_repr(POSITIVE_INFINITY),
4692 static_cast<scalar_type>(POSITIVE_INFINITY));
4693 }
4694 }
4695 } // namespace matrix
4696
4698 // Min-plus matrices
4700
4729
4752 // Static arithmetic
4753 template <typename Scalar>
4754 struct MinPlusPlus {
4755 static_assert(std::is_signed<Scalar>::value,
4756 "MinPlus requires a signed integer type as parameter!");
4767 //! \exceptions
4768 //! \noexcept
4769 Scalar operator()(Scalar x, Scalar y) const noexcept {
4770 if (x == POSITIVE_INFINITY) {
4771 return y;
4772 } else if (y == POSITIVE_INFINITY) {
4773 return x;
4774 }
4775 return std::min(x, y);
4776 }
4777 };
4778
4799 //! integer type).
4800 template <typename Scalar>
4801 struct MinPlusProd {
4802 static_assert(std::is_signed<Scalar>::value,
4803 "MinPlus requires a signed integer type as parameter!");
4814 //! \exceptions
4815 //! \noexcept
4816 Scalar operator()(Scalar x, Scalar y) const noexcept {
4817 if (x == POSITIVE_INFINITY || y == POSITIVE_INFINITY) {
4818 return POSITIVE_INFINITY;
4819 }
4820 return x + y;
4821 }
4822 };
4823
4836 //! integer type).
4837 template <typename Scalar>
4838 struct MinPlusZero {
4839 static_assert(std::is_signed<Scalar>::value,
4840 "MinPlus requires a signed integer type as parameter!");
4848 //! \exceptions
4849 //! \noexcept
4850 constexpr Scalar operator()() const noexcept {
4851 return POSITIVE_INFINITY;
4852 }
4853 };
4854
4865 template <typename Scalar>
4866 using DynamicMinPlusMat = DynamicMatrix<MinPlusPlus<Scalar>,
4870 Scalar>;
4871
4884 template <size_t R, size_t C, typename Scalar>
4889 R,
4890 C,
4891 Scalar>;
4908 template <size_t R = 0, size_t C = R, typename Scalar = int>
4909 using MinPlusMat = std::conditional_t<R == 0 || C == 0,
4912
4913 namespace detail {
4914 template <typename T>
4915 struct IsMinPlusMatHelper : std::false_type {};
4916
4917 template <size_t R, size_t C, typename Scalar>
4918 struct IsMinPlusMatHelper<StaticMinPlusMat<R, C, Scalar>> : std::true_type {
4919 };
4920
4921 template <typename Scalar>
4922 struct IsMinPlusMatHelper<DynamicMinPlusMat<Scalar>> : std::true_type {};
4923 } // namespace detail
4924
4935 template <typename T>
4936 static constexpr bool IsMinPlusMat = detail::IsMinPlusMatHelper<T>::value;
4937
4938 namespace matrix {
4953 //! \ref NEGATIVE_INFINITY.
4954 template <typename Mat>
4955 std::enable_if_t<IsMinPlusMat<Mat>> throw_if_bad_entry(Mat const& x) {
4956 using scalar_type = typename Mat::scalar_type;
4957 auto it = std::find_if(x.cbegin(), x.cend(), [](scalar_type val) {
4958 return val == NEGATIVE_INFINITY;
4959 });
4960 if (it != x.cend()) {
4961 auto [r, c] = x.coords(it);
4963 "invalid entry, expected entries to be integers or {} (= {}), "
4964 "but found {} (= {}) in entry ({}, {})",
4965 entry_repr(POSITIVE_INFINITY),
4966 static_cast<scalar_type>(POSITIVE_INFINITY),
4967 entry_repr(NEGATIVE_INFINITY),
4968 static_cast<scalar_type>(NEGATIVE_INFINITY),
4969 r,
4970 c);
4971 }
4972 }
4973
4989 template <typename Mat>
4990 std::enable_if_t<IsMinPlusMat<Mat>>
4991 throw_if_bad_entry(Mat const&, typename Mat::scalar_type val) {
4992 if (val == NEGATIVE_INFINITY) {
4993 using scalar_type = typename Mat::scalar_type;
4994 LIBSEMIGROUPS_EXCEPTION("invalid entry, expected entries to be "
4995 "integers or {} (= {}) but found {} (= {})",
4996 entry_repr(POSITIVE_INFINITY),
4997 static_cast<scalar_type>(POSITIVE_INFINITY),
4998 entry_repr(NEGATIVE_INFINITY),
4999 static_cast<scalar_type>(NEGATIVE_INFINITY));
5000 }
5001 }
5002 } // namespace matrix
5003
5005 // Max-plus matrices with threshold
5007
5053
5078 //! integer type).
5079 template <size_t T, typename Scalar>
5080 struct MaxPlusTruncProd {
5081 static_assert(std::is_signed<Scalar>::value,
5082 "MaxPlus requires a signed integer type as parameter!");
5093 //! \exceptions
5094 //! \noexcept
5095 Scalar operator()(Scalar x, Scalar y) const noexcept {
5096 LIBSEMIGROUPS_ASSERT((x >= 0 && x <= static_cast<Scalar>(T))
5097 || x == NEGATIVE_INFINITY);
5098 LIBSEMIGROUPS_ASSERT((y >= 0 && y <= static_cast<Scalar>(T))
5099 || y == NEGATIVE_INFINITY);
5100 if (x == NEGATIVE_INFINITY || y == NEGATIVE_INFINITY) {
5101 return NEGATIVE_INFINITY;
5102 }
5103 return std::min(x + y, static_cast<Scalar>(T));
5104 }
5105 };
5106
5120 //! signed integer type (defaults to \c int).
5121 template <typename Scalar = int>
5122 class MaxPlusTruncSemiring {
5123 static_assert(std::is_signed<Scalar>::value,
5124 "MaxPlus requires a signed integer type as parameter!");
5125
5126 public:
5130 MaxPlusTruncSemiring() = delete;
5131
5135 MaxPlusTruncSemiring(MaxPlusTruncSemiring const&) noexcept = default;
5136
5140 MaxPlusTruncSemiring(MaxPlusTruncSemiring&&) noexcept = default;
5141
5145 MaxPlusTruncSemiring& operator=(MaxPlusTruncSemiring const&) noexcept
5146 = default;
5147
5151 MaxPlusTruncSemiring& operator=(MaxPlusTruncSemiring&&) noexcept = default;
5152
5153 ~MaxPlusTruncSemiring() = default;
5154
5163 //! \complexity
5164 //! Constant.
5165 explicit MaxPlusTruncSemiring(Scalar threshold) : _threshold(threshold) {
5166 if (threshold < 0) {
5167 LIBSEMIGROUPS_EXCEPTION("expected non-negative value, found {}",
5168 threshold);
5169 }
5170 }
5171
5180 //! \exceptions
5181 //! \noexcept
5182 static constexpr Scalar scalar_one() noexcept {
5183 return 0;
5184 }
5185
5194 //! \exceptions
5195 //! \noexcept
5196 static constexpr Scalar scalar_zero() noexcept {
5197 return NEGATIVE_INFINITY;
5198 }
5199
5222 //! \complexity
5223 //! Constant.
5224 Scalar product_no_checks(Scalar x, Scalar y) const noexcept {
5225 LIBSEMIGROUPS_ASSERT((x >= 0 && x <= _threshold)
5226 || x == NEGATIVE_INFINITY);
5227 LIBSEMIGROUPS_ASSERT((y >= 0 && y <= _threshold)
5228 || y == NEGATIVE_INFINITY);
5229 if (x == NEGATIVE_INFINITY || y == NEGATIVE_INFINITY) {
5230 return NEGATIVE_INFINITY;
5231 }
5232 return std::min(x + y, _threshold);
5233 }
5234
5257 //! \complexity
5258 //! Constant.
5259 Scalar plus_no_checks(Scalar x, Scalar y) const noexcept {
5260 LIBSEMIGROUPS_ASSERT((x >= 0 && x <= _threshold)
5261 || x == NEGATIVE_INFINITY);
5262 LIBSEMIGROUPS_ASSERT((y >= 0 && y <= _threshold)
5263 || y == NEGATIVE_INFINITY);
5264 if (x == NEGATIVE_INFINITY) {
5265 return y;
5266 } else if (y == NEGATIVE_INFINITY) {
5267 return x;
5268 }
5269 return std::max(x, y);
5270 }
5271
5282 //! \complexity
5283 //! Constant.
5284 Scalar threshold() const noexcept {
5285 return _threshold;
5286 }
5287
5288 public:
5289 Scalar const _threshold;
5290 };
5291
5304 template <size_t T, typename Scalar>
5305 using DynamicMaxPlusTruncMat = DynamicMatrix<MaxPlusPlus<Scalar>,
5309 Scalar>;
5310
5324 template <size_t T, size_t R, size_t C, typename Scalar>
5329 R,
5330 C,
5331 Scalar>;
5350 template <size_t T = 0, size_t R = 0, size_t C = R, typename Scalar = int>
5351 using MaxPlusTruncMat = std::conditional_t<
5352 R == 0 || C == 0,
5353 std::conditional_t<T == 0,
5354 DynamicMatrix<MaxPlusTruncSemiring<Scalar>, Scalar>,
5357
5358 namespace detail {
5359 template <typename T>
5360 struct IsMaxPlusTruncMatHelper : std::false_type {};
5361
5362 template <size_t T, size_t R, size_t C, typename Scalar>
5363 struct IsMaxPlusTruncMatHelper<StaticMaxPlusTruncMat<T, R, C, Scalar>>
5364 : std::true_type {
5365 static constexpr Scalar threshold = T;
5366 };
5367
5368 template <size_t T, typename Scalar>
5369 struct IsMaxPlusTruncMatHelper<DynamicMaxPlusTruncMat<T, Scalar>>
5370 : std::true_type {
5371 static constexpr Scalar threshold = T;
5372 };
5373
5374 template <typename Scalar>
5375 struct IsMaxPlusTruncMatHelper<
5376 DynamicMatrix<MaxPlusTruncSemiring<Scalar>, Scalar>> : std::true_type {
5377 static constexpr Scalar threshold = UNDEFINED;
5378 };
5379 } // namespace detail
5380
5392 template <typename T>
5393 static constexpr bool IsMaxPlusTruncMat
5394 = detail::IsMaxPlusTruncMatHelper<T>::value;
5395
5396 namespace detail {
5397 template <typename T>
5398 struct IsTruncMatHelper<T, std::enable_if_t<IsMaxPlusTruncMat<T>>>
5399 : std::true_type {
5400 static constexpr typename T::scalar_type threshold
5401 = IsMaxPlusTruncMatHelper<T>::threshold;
5402 };
5403 } // namespace detail
5404
5405 namespace matrix {
5423 //! (only applies to matrices with run time arithmetic).
5424 template <typename Mat>
5425 std::enable_if_t<IsMaxPlusTruncMat<Mat>> throw_if_bad_entry(Mat const& m) {
5426 // TODO(1) to tpp
5427 detail::throw_if_semiring_nullptr(m);
5428
5429 using scalar_type = typename Mat::scalar_type;
5430 scalar_type const t = matrix::threshold(m);
5431 auto it = std::find_if_not(m.cbegin(), m.cend(), [t](scalar_type x) {
5432 return x == NEGATIVE_INFINITY || (0 <= x && x <= t);
5433 });
5434 if (it != m.cend()) {
5435 auto [r, c] = m.coords(it);
5437 "invalid entry, expected values in {{0, 1, ..., {}, {} (= {})}} "
5438 "but found {} in entry ({}, {})",
5439 t,
5440 entry_repr(NEGATIVE_INFINITY),
5441 static_cast<scalar_type>(NEGATIVE_INFINITY),
5442 detail::entry_repr(*it),
5443 r,
5444 c);
5445 }
5446 }
5447
5467 template <typename Mat>
5468 std::enable_if_t<IsMaxPlusTruncMat<Mat>>
5469 throw_if_bad_entry(Mat const& m, typename Mat::scalar_type val) {
5470 detail::throw_if_semiring_nullptr(m);
5471 using scalar_type = typename Mat::scalar_type;
5472 scalar_type const t = matrix::threshold(m);
5473 if (val == POSITIVE_INFINITY || 0 > val || val > t) {
5475 "invalid entry, expected values in {{0, 1, ..., {}, -{} (= {})}} "
5476 "but found {}",
5477 t,
5478 entry_repr(NEGATIVE_INFINITY),
5479 static_cast<scalar_type>(NEGATIVE_INFINITY),
5480 detail::entry_repr(val));
5481 }
5482 }
5483 } // namespace matrix
5484
5486 // Min-plus matrices with threshold
5488
5534
5558 //! \tparam Scalar the type of the values in the semiring.
5559 template <size_t T, typename Scalar>
5560 struct MinPlusTruncProd {
5571 //! \exceptions
5572 //! \noexcept
5573 Scalar operator()(Scalar x, Scalar y) const noexcept {
5574 LIBSEMIGROUPS_ASSERT((x >= 0 && x <= static_cast<Scalar>(T))
5575 || x == POSITIVE_INFINITY);
5576 LIBSEMIGROUPS_ASSERT((y >= 0 && y <= static_cast<Scalar>(T))
5577 || y == POSITIVE_INFINITY);
5578 if (x == POSITIVE_INFINITY || y == POSITIVE_INFINITY) {
5579 return POSITIVE_INFINITY;
5580 }
5581 return std::min(x + y, static_cast<Scalar>(T));
5582 }
5583 };
5584
5597 //! integral type.
5598 template <typename Scalar = int>
5599 class MinPlusTruncSemiring {
5600 static_assert(std::is_integral<Scalar>::value,
5601 "MinPlus requires an integral type as parameter!");
5602
5603 public:
5607 MinPlusTruncSemiring() = delete;
5608
5612 MinPlusTruncSemiring(MinPlusTruncSemiring const&) noexcept = default;
5613
5617 MinPlusTruncSemiring(MinPlusTruncSemiring&&) noexcept = default;
5618
5622 MinPlusTruncSemiring& operator=(MinPlusTruncSemiring const&) noexcept
5623 = default;
5624
5628 MinPlusTruncSemiring& operator=(MinPlusTruncSemiring&&) noexcept = default;
5629
5638 //! \complexity
5639 //! Constant.
5640 explicit MinPlusTruncSemiring(Scalar threshold) : _threshold(threshold) {
5642 LIBSEMIGROUPS_EXCEPTION("expected non-negative value, found {}",
5643 threshold);
5644 }
5645 }
5646
5655 //! \exceptions
5656 //! \noexcept
5657 static constexpr Scalar scalar_one() noexcept {
5658 return 0;
5659 }
5660
5670 //! \noexcept
5671 // TODO(1) These mem fns (one and zero) aren't needed?
5672 static constexpr Scalar scalar_zero() noexcept {
5673 return POSITIVE_INFINITY;
5674 }
5675
5698 //! \complexity
5699 //! Constant.
5700 Scalar product_no_checks(Scalar x, Scalar y) const noexcept {
5701 LIBSEMIGROUPS_ASSERT((x >= 0 && x <= _threshold)
5702 || x == POSITIVE_INFINITY);
5703 LIBSEMIGROUPS_ASSERT((y >= 0 && y <= _threshold)
5704 || y == POSITIVE_INFINITY);
5705 if (x == POSITIVE_INFINITY || y == POSITIVE_INFINITY) {
5706 return POSITIVE_INFINITY;
5707 }
5708 return std::min(x + y, _threshold);
5709 }
5710
5733 //! \complexity
5734 //! Constant.
5735 Scalar plus_no_checks(Scalar x, Scalar y) const noexcept {
5736 LIBSEMIGROUPS_ASSERT((x >= 0 && x <= _threshold)
5737 || x == POSITIVE_INFINITY);
5738 LIBSEMIGROUPS_ASSERT((y >= 0 && y <= _threshold)
5739 || y == POSITIVE_INFINITY);
5740 if (x == POSITIVE_INFINITY) {
5741 return y;
5742 } else if (y == POSITIVE_INFINITY) {
5743 return x;
5744 }
5745 return std::min(x, y);
5746 }
5747
5758 //! \complexity
5759 //! Constant.
5760 Scalar threshold() const noexcept {
5761 return _threshold;
5762 }
5763
5764 public:
5765 Scalar const _threshold;
5766 };
5767
5780 template <size_t T, typename Scalar>
5781 using DynamicMinPlusTruncMat = DynamicMatrix<MinPlusPlus<Scalar>,
5785 Scalar>;
5786
5800 template <size_t T, size_t R, size_t C, typename Scalar>
5805 R,
5806 C,
5807 Scalar>;
5808
5827 template <size_t T = 0, size_t R = 0, size_t C = R, typename Scalar = int>
5828 using MinPlusTruncMat = std::conditional_t<
5829 R == 0 || C == 0,
5830 std::conditional_t<T == 0,
5831 DynamicMatrix<MinPlusTruncSemiring<Scalar>, Scalar>,
5834
5835 namespace detail {
5836 template <typename T>
5837 struct IsMinPlusTruncMatHelper : std::false_type {};
5838
5839 template <size_t T, size_t R, size_t C, typename Scalar>
5840 struct IsMinPlusTruncMatHelper<StaticMinPlusTruncMat<T, R, C, Scalar>>
5841 : std::true_type {
5842 static constexpr Scalar threshold = T;
5843 };
5844
5845 template <size_t T, typename Scalar>
5846 struct IsMinPlusTruncMatHelper<DynamicMinPlusTruncMat<T, Scalar>>
5847 : std::true_type {
5848 static constexpr Scalar threshold = T;
5849 };
5850
5851 template <typename Scalar>
5852 struct IsMinPlusTruncMatHelper<
5853 DynamicMatrix<MinPlusTruncSemiring<Scalar>, Scalar>> : std::true_type {
5854 static constexpr Scalar threshold = UNDEFINED;
5855 };
5856 } // namespace detail
5857
5869 template <typename T>
5870 static constexpr bool IsMinPlusTruncMat
5871 = detail::IsMinPlusTruncMatHelper<T>::value;
5872
5873 namespace detail {
5874 template <typename T>
5875 struct IsTruncMatHelper<T, std::enable_if_t<IsMinPlusTruncMat<T>>>
5876 : std::true_type {
5877 static constexpr typename T::scalar_type threshold
5878 = IsMinPlusTruncMatHelper<T>::threshold;
5879 };
5880 } // namespace detail
5881
5882 namespace matrix {
5901 // TODO(1) to tpp
5902 template <typename Mat>
5903 std::enable_if_t<IsMinPlusTruncMat<Mat>> throw_if_bad_entry(Mat const& m) {
5904 // Check that the semiring pointer isn't the nullptr if it shouldn't be
5905 detail::throw_if_semiring_nullptr(m);
5906
5907 using scalar_type = typename Mat::scalar_type;
5908 scalar_type const t = matrix::threshold(m);
5909 auto it = std::find_if_not(m.cbegin(), m.cend(), [t](scalar_type x) {
5910 return x == POSITIVE_INFINITY || (0 <= x && x <= t);
5911 });
5912 if (it != m.cend()) {
5913 uint64_t r, c;
5914 std::tie(r, c) = m.coords(it);
5915
5917 "invalid entry, expected values in {{0, 1, ..., {}, {} (= {})}} "
5918 "but found {} in entry ({}, {})",
5919 t,
5920 detail::entry_repr(POSITIVE_INFINITY),
5921 static_cast<scalar_type>(POSITIVE_INFINITY),
5922 detail::entry_repr(*it),
5923 r,
5924 c);
5925 }
5926 }
5927
5947 template <typename Mat>
5948 std::enable_if_t<IsMinPlusTruncMat<Mat>>
5949 throw_if_bad_entry(Mat const& m, typename Mat::scalar_type val) {
5950 detail::throw_if_semiring_nullptr(m);
5951
5952 using scalar_type = typename Mat::scalar_type;
5953 scalar_type const t = matrix::threshold(m);
5954 if (!(val == POSITIVE_INFINITY || (0 <= val && val <= t))) {
5956 "invalid entry, expected values in {{0, 1, ..., {}, {} (= {})}} "
5957 "but found {}",
5958 t,
5959 detail::entry_repr(POSITIVE_INFINITY),
5960 static_cast<scalar_type>(POSITIVE_INFINITY),
5961 detail::entry_repr(val));
5962 }
5963 }
5964 } // namespace matrix
5965
5967 // NTP matrices
5969
6022
6023 namespace detail {
6024 template <size_t T, size_t P, typename Scalar>
6025 constexpr Scalar thresholdperiod(Scalar x) noexcept {
6026 if (x > T) {
6027 return T + (x - T) % P;
6028 }
6029 return x;
6030 }
6031
6032 template <typename Scalar>
6033 inline Scalar thresholdperiod(Scalar x,
6034 Scalar threshold,
6035 Scalar period) noexcept {
6036 if (x > threshold) {
6037 return threshold + (x - threshold) % period;
6038 }
6039 return x;
6040 }
6041 } // namespace detail
6042
6065 // Static arithmetic
6066 template <size_t T, size_t P, typename Scalar>
6067 struct NTPPlus {
6077 //! \exceptions
6078 //! \noexcept
6079 constexpr Scalar operator()(Scalar x, Scalar y) const noexcept {
6080 return detail::thresholdperiod<T, P>(x + y);
6081 }
6082 };
6083
6106 //! \tparam Scalar the type of the values in the semiring.
6107 template <size_t T, size_t P, typename Scalar>
6108 struct NTPProd {
6120 //! \exceptions
6121 //! \noexcept
6122 constexpr Scalar operator()(Scalar x, Scalar y) const noexcept {
6123 return detail::thresholdperiod<T, P>(x * y);
6124 }
6125 };
6126
6140 // Dynamic arithmetic
6141 template <typename Scalar = size_t>
6142 class NTPSemiring {
6143 public:
6147 // Deleted to avoid uninitialised values of period and threshold.
6148 NTPSemiring() = delete;
6149
6153 NTPSemiring(NTPSemiring const&) = default;
6154
6158 NTPSemiring(NTPSemiring&&) = default;
6159
6163 NTPSemiring& operator=(NTPSemiring const&) = default;
6164
6168 NTPSemiring& operator=(NTPSemiring&&) = default;
6169
6170 ~NTPSemiring() = default;
6171
6182 //! \complexity
6183 //! Constant.
6184 NTPSemiring(Scalar t, Scalar p) : _period(p), _threshold(t) {
6185 if constexpr (std::is_signed<Scalar>::value) {
6186 if (t < 0) {
6188 "expected non-negative value for 1st argument, found {}", t);
6189 }
6190 }
6191 if (p <= 0) {
6193 "expected positive value for 2nd argument, found {}", p);
6194 }
6195 }
6196
6205 //! \exceptions
6206 //! \noexcept
6207 static constexpr Scalar scalar_one() noexcept {
6208 return 1;
6209 }
6210
6221 //! \complexity
6222 //! Constant.
6223 static constexpr Scalar scalar_zero() noexcept {
6224 return 0;
6225 }
6226
6249 //! \complexity
6250 //! Constant.
6251 Scalar product_no_checks(Scalar x, Scalar y) const noexcept {
6252 LIBSEMIGROUPS_ASSERT(x >= 0 && x <= _period + _threshold - 1);
6253 LIBSEMIGROUPS_ASSERT(y >= 0 && y <= _period + _threshold - 1);
6254 return detail::thresholdperiod(x * y, _threshold, _period);
6255 }
6256
6279 //! \complexity
6280 //! Constant.
6281 Scalar plus_no_checks(Scalar x, Scalar y) const noexcept {
6282 LIBSEMIGROUPS_ASSERT(x >= 0 && x <= _period + _threshold - 1);
6283 LIBSEMIGROUPS_ASSERT(y >= 0 && y <= _period + _threshold - 1);
6284 return detail::thresholdperiod(x + y, _threshold, _period);
6285 }
6286
6297 //! \complexity
6298 //! Constant.
6299 Scalar threshold() const noexcept {
6300 return _threshold;
6301 }
6302
6313 //! \complexity
6314 //! Constant.
6315 Scalar period() const noexcept {
6316 return _period;
6317 }
6318
6319 private:
6320 Scalar _period;
6321 Scalar _threshold;
6322 };
6323
6334 template <typename Scalar>
6335 using DynamicNTPMatWithSemiring = DynamicMatrix<NTPSemiring<Scalar>, Scalar>;
6336
6350 template <size_t T, size_t P, typename Scalar>
6351 using DynamicNTPMatWithoutSemiring = DynamicMatrix<NTPPlus<T, P, Scalar>,
6355 Scalar>;
6356
6376 template <size_t T, size_t P, size_t R, size_t C, typename Scalar>
6381 R,
6382 C,
6383 Scalar>;
6384
6407 template <size_t T = 0,
6408 size_t P = 0,
6409 size_t R = 0,
6410 size_t C = R,
6411 typename Scalar = size_t>
6412 using NTPMat = std::conditional_t<
6413 R == 0 || C == 0,
6414 std::conditional_t<T == 0 && P == 0,
6418
6419 namespace detail {
6420 template <typename T>
6421 struct IsNTPMatHelper : std::false_type {};
6422
6423 template <typename Scalar>
6424 struct IsNTPMatHelper<DynamicNTPMatWithSemiring<Scalar>> : std::true_type {
6425 static constexpr Scalar threshold = UNDEFINED;
6426 static constexpr Scalar period = UNDEFINED;
6427 };
6428
6429 template <size_t T, size_t P, typename Scalar>
6430 struct IsNTPMatHelper<DynamicNTPMatWithoutSemiring<T, P, Scalar>>
6431 : std::true_type {
6432 static constexpr Scalar threshold = T;
6433 static constexpr Scalar period = P;
6434 };
6435
6436 template <size_t T, size_t P, size_t R, size_t C, typename Scalar>
6437 struct IsNTPMatHelper<StaticNTPMat<T, P, R, C, Scalar>> : std::true_type {
6438 static constexpr Scalar threshold = T;
6439 static constexpr Scalar period = P;
6440 };
6441 } // namespace detail
6442
6454 template <typename U>
6455 static constexpr bool IsNTPMat = detail::IsNTPMatHelper<U>::value;
6456
6457 namespace detail {
6458 template <typename T>
6459 struct IsTruncMatHelper<T, std::enable_if_t<IsNTPMat<T>>> : std::true_type {
6460 static constexpr typename T::scalar_type threshold
6461 = IsNTPMatHelper<T>::threshold;
6462 static constexpr typename T::scalar_type period
6463 = IsNTPMatHelper<T>::period;
6464 };
6465
6466 } // namespace detail
6467
6468 namespace matrix {
6489 //! \noexcept
6490 template <size_t T, size_t P, size_t R, size_t C, typename Scalar>
6491 constexpr Scalar period(StaticNTPMat<T, P, R, C, Scalar> const&) noexcept {
6492 return P;
6493 }
6494
6512 template <size_t T, size_t P, typename Scalar>
6513 constexpr Scalar
6515 return P;
6516 }
6517
6532 //! \noexcept
6533 template <typename Scalar>
6534 Scalar period(DynamicNTPMatWithSemiring<Scalar> const& x) noexcept {
6535 return x.semiring()->period();
6536 }
6537 } // namespace matrix
6538
6539 namespace matrix {
6559 //! defined (only applies to matrices with run time arithmetic).
6560 template <typename Mat>
6561 std::enable_if_t<IsNTPMat<Mat>> throw_if_bad_entry(Mat const& m) {
6562 detail::throw_if_semiring_nullptr(m);
6563
6564 using scalar_type = typename Mat::scalar_type;
6565 scalar_type const t = matrix::threshold(m);
6566 scalar_type const p = matrix::period(m);
6567 auto it = std::find_if_not(m.cbegin(), m.cend(), [t, p](scalar_type x) {
6568 return (0 <= x && x < p + t);
6569 });
6570 if (it != m.cend()) {
6571 uint64_t r, c;
6572 std::tie(r, c) = m.coords(it);
6573
6575 "invalid entry, expected values in {{0, 1, ..., {}}}, but "
6576 "found {} in entry ({}, {})",
6577 p + t,
6578 detail::entry_repr(*it),
6579 r,
6580 c);
6581 }
6582 }
6583
6605 template <typename Mat>
6606 std::enable_if_t<IsNTPMat<Mat>>
6607 throw_if_bad_entry(Mat const& m, typename Mat::scalar_type val) {
6608 detail::throw_if_semiring_nullptr(m);
6609 using scalar_type = typename Mat::scalar_type;
6610 scalar_type const t = matrix::threshold(m);
6611 scalar_type const p = matrix::period(m);
6612 if (val < 0 || val >= p + t) {
6614 "invalid entry, expected values in {{0, 1, ..., {}}}, but "
6615 "found {}",
6616 p + t,
6617 detail::entry_repr(val));
6618 }
6619 }
6620 } // namespace matrix
6621
6623 // Projective max-plus matrices
6625
6626 namespace detail {
6627 template <typename T>
6628 class ProjMaxPlusMat : MatrixPolymorphicBase {
6629 public:
6630 using scalar_type = typename T::scalar_type;
6631 using scalar_reference = typename T::scalar_reference;
6632 using scalar_const_reference = typename T::scalar_const_reference;
6633 using semiring_type = void;
6634
6635 using container_type = typename T::container_type;
6636 using iterator = typename T::iterator;
6637 using const_iterator = typename T::const_iterator;
6638
6639 using underlying_matrix_type = T;
6640
6641 using RowView = typename T::RowView;
6642
6643 // Note that Rows are never normalised, and that's why we use the
6644 // underlying matrix Row type and not 1 x n ProjMaxPlusMat's instead
6645 // (since these will be normalised according to their entries, and
6646 // this might not correspond to the normalised entries of the matrix).
6647 using Row = typename T::Row;
6648
6649 scalar_type scalar_one() const noexcept {
6650 return _underlying_mat.scalar_one();
6651 }
6652
6653 scalar_type scalar_zero() const noexcept {
6654 return _underlying_mat.scalar_zero();
6655 }
6656
6658 // ProjMaxPlusMat - Constructors + destructor - public
6660
6661 ProjMaxPlusMat() : _is_normalized(false), _underlying_mat() {}
6662 ProjMaxPlusMat(ProjMaxPlusMat const&) = default;
6663 ProjMaxPlusMat(ProjMaxPlusMat&&) = default;
6664 ProjMaxPlusMat& operator=(ProjMaxPlusMat const&) = default;
6665 ProjMaxPlusMat& operator=(ProjMaxPlusMat&&) = default;
6666
6667 ProjMaxPlusMat(size_t r, size_t c)
6668 : _is_normalized(false), _underlying_mat(r, c) {}
6669
6670 // TODO(1) other missing constructors
6671 ProjMaxPlusMat(
6672 typename underlying_matrix_type::semiring_type const* semiring,
6673 size_t r,
6674 size_t c)
6675 : _is_normalized(false), _underlying_mat(semiring, r, c) {}
6676
6677 explicit ProjMaxPlusMat(std::vector<std::vector<scalar_type>> const& m)
6678 : _is_normalized(false), _underlying_mat(m) {
6679 normalize();
6680 }
6681
6682 ProjMaxPlusMat(
6683 std::initializer_list<std::initializer_list<scalar_type>> const& m)
6684 : ProjMaxPlusMat(
6685 std::vector<std::vector<scalar_type>>(m.begin(), m.end())) {}
6686
6687 ~ProjMaxPlusMat() = default;
6688
6689 ProjMaxPlusMat one() const {
6690 auto result = ProjMaxPlusMat(_underlying_mat.one());
6691 return result;
6692 }
6693
6694 static ProjMaxPlusMat one(size_t n) {
6695 return ProjMaxPlusMat(T::one(n));
6696 }
6697
6699 // Comparison operators
6701
6702 bool operator==(ProjMaxPlusMat const& that) const {
6703 normalize();
6704 that.normalize();
6705 return _underlying_mat == that._underlying_mat;
6706 }
6707
6708 bool operator!=(ProjMaxPlusMat const& that) const {
6709 return !(_underlying_mat == that._underlying_mat);
6710 }
6711
6712 bool operator<(ProjMaxPlusMat const& that) const {
6713 normalize();
6714 that.normalize();
6715 return _underlying_mat < that._underlying_mat;
6716 }
6717
6718 bool operator>(ProjMaxPlusMat const& that) const {
6719 return that < *this;
6720 }
6721
6722 template <typename Thing>
6723 bool operator>=(Thing const& that) const {
6724 static_assert(IsMatrix<Thing> || std::is_same_v<Thing, RowView>);
6725 return that < *this || that == *this;
6726 }
6727
6728 // not noexcept because operator< isn't
6729 template <typename Thing>
6730 bool operator<=(Thing const& that) const {
6731 static_assert(IsMatrix<Thing> || std::is_same_v<Thing, RowView>);
6732 return *this < that || that == *this;
6733 }
6734
6736 // Attributes
6738
6739 scalar_reference operator()(size_t r, size_t c) {
6740 // to ensure the returned value is normalised
6741 normalize();
6742 // to ensure that the matrix is renormalised if the returned scalar is
6743 // assigned.
6744 _is_normalized = false;
6745 return _underlying_mat(r, c);
6746 }
6747
6748 scalar_reference at(size_t r, size_t c) {
6749 matrix::throw_if_bad_coords(*this, r, c);
6750 return this->operator()(r, c);
6751 }
6752
6753 scalar_const_reference operator()(size_t r, size_t c) const {
6754 normalize();
6755 return _underlying_mat(r, c);
6756 }
6757
6758 scalar_const_reference at(size_t r, size_t c) const {
6759 matrix::throw_if_bad_coords(*this, r, c);
6760 return this->operator()(r, c);
6761 }
6762
6763 size_t number_of_rows() const noexcept {
6764 return _underlying_mat.number_of_rows();
6765 }
6766
6767 size_t number_of_cols() const noexcept {
6768 return _underlying_mat.number_of_cols();
6769 }
6770
6771 size_t hash_value() const {
6772 normalize();
6773 return Hash<T>()(_underlying_mat);
6774 }
6775
6777 // Arithmetic operators - in-place
6779
6780 void product_inplace_no_checks(ProjMaxPlusMat const& A,
6781 ProjMaxPlusMat const& B) {
6782 _underlying_mat.product_inplace_no_checks(A._underlying_mat,
6783 B._underlying_mat);
6784 normalize(true); // force normalize
6785 }
6786
6787 void operator+=(ProjMaxPlusMat const& that) {
6788 _underlying_mat += that._underlying_mat;
6789 normalize(true); // force normalize
6790 }
6791
6792 void operator*=(scalar_type a) {
6793 _underlying_mat *= a;
6794 normalize(true); // force normalize
6795 }
6796
6797 void operator+=(scalar_type a) {
6798 _underlying_mat += a;
6799 normalize(true); // force normalize
6800 }
6801
6802 ProjMaxPlusMat operator*(scalar_type a) const {
6803 ProjMaxPlusMat result(*this);
6804 result *= a;
6805 return result;
6806 }
6807
6808 ProjMaxPlusMat operator+(scalar_type a) const {
6809 ProjMaxPlusMat result(*this);
6810 result += a;
6811 return result;
6812 }
6813
6815 // Arithmetic operators - not in-place
6817
6818 ProjMaxPlusMat operator+(ProjMaxPlusMat const& that) const {
6819 return ProjMaxPlusMat(_underlying_mat + that._underlying_mat);
6820 }
6821
6822 ProjMaxPlusMat operator*(ProjMaxPlusMat const& that) const {
6823 return ProjMaxPlusMat(_underlying_mat * that._underlying_mat);
6824 }
6825
6827 // Iterators
6829
6830 // The following should probably be commented out because I can't
6831 // currently think how to ensure that the matrix is normalised if it's
6832 // changed this way.
6833
6834 iterator begin() noexcept {
6835 // to ensure the returned value is normalised
6836 normalize();
6837 // to ensure that the matrix is renormalised if the returned scalar is
6838 // assigned.
6839 _is_normalized = false;
6840 return _underlying_mat.begin();
6841 }
6842
6843 iterator end() noexcept {
6844 // to ensure the returned value is normalised
6845 normalize();
6846 // to ensure that the matrix is renormalised if the returned scalar is
6847 // assigned.
6848 _is_normalized = false;
6849 return _underlying_mat.end();
6850 }
6851
6852 const_iterator begin() const noexcept {
6853 normalize();
6854 return _underlying_mat.begin();
6855 }
6856
6857 const_iterator end() const noexcept {
6858 normalize();
6859 return _underlying_mat.end();
6860 }
6861
6862 const_iterator cbegin() const noexcept {
6863 normalize();
6864 return _underlying_mat.cbegin();
6865 }
6866
6867 const_iterator cend() const noexcept {
6868 normalize();
6869 return _underlying_mat.cend();
6870 }
6871
6873 // Modifiers
6875
6876 void swap(ProjMaxPlusMat& that) noexcept {
6877 std::swap(_underlying_mat, that._underlying_mat);
6878 }
6879
6880 void transpose() noexcept {
6881 _underlying_mat.transpose();
6882 }
6883
6884 void transpose_no_checks() noexcept {
6885 _underlying_mat.transpose_no_checks();
6886 }
6887
6889 // Rows
6891
6892 RowView row(size_t i) const {
6893 normalize();
6894 return _underlying_mat.row(i);
6895 }
6896
6897 template <typename C>
6898 void rows(C& x) const {
6899 normalize();
6900 return _underlying_mat.rows(x);
6901 }
6902
6904 // Friend functions
6906
6907 friend std::ostream& operator<<(std::ostream& os,
6908 ProjMaxPlusMat const& x) {
6909 x.normalize();
6910 os << detail::to_string(x._underlying_mat);
6911 return os;
6912 }
6913
6914 T const& underlying_matrix() const noexcept {
6915 normalize();
6916 return _underlying_mat;
6917 }
6918
6919 private:
6920 explicit ProjMaxPlusMat(T&& mat)
6921 : _is_normalized(false), _underlying_mat(std::move(mat)) {
6922 normalize();
6923 }
6924
6925 void normalize(bool force = false) const {
6926 if ((_is_normalized && !force)
6927 || (_underlying_mat.number_of_rows() == 0)
6928 || (_underlying_mat.number_of_cols() == 0)) {
6929 _is_normalized = true;
6930 return;
6931 }
6932 scalar_type const n = *std::max_element(_underlying_mat.cbegin(),
6933 _underlying_mat.cend());
6934 std::for_each(_underlying_mat.begin(),
6935 _underlying_mat.end(),
6936 [&n](scalar_type& s) {
6937 if (s != NEGATIVE_INFINITY) {
6938 s -= n;
6939 }
6940 });
6941 _is_normalized = true;
6942 }
6943
6944 mutable bool _is_normalized;
6945 mutable T _underlying_mat;
6946 };
6947 } // namespace detail
6948
6993
7007 template <size_t R, size_t C, typename Scalar>
7009 = detail::ProjMaxPlusMat<StaticMaxPlusMat<R, C, Scalar>>;
7010
7022 template <typename Scalar>
7024 = detail::ProjMaxPlusMat<DynamicMaxPlusMat<Scalar>>;
7025
7040 template <size_t R = 0, size_t C = R, typename Scalar = int>
7041 using ProjMaxPlusMat = std::conditional_t<R == 0 || C == 0,
7044
7045 namespace detail {
7046 template <typename T>
7047 struct IsProjMaxPlusMatHelper : std::false_type {};
7048
7049 template <size_t R, size_t C, typename Scalar>
7050 struct IsProjMaxPlusMatHelper<StaticProjMaxPlusMat<R, C, Scalar>>
7051 : std::true_type {};
7052
7053 template <typename Scalar>
7054 struct IsProjMaxPlusMatHelper<DynamicProjMaxPlusMat<Scalar>>
7055 : std::true_type {};
7056 } // namespace detail
7057
7069 template <typename T>
7070 static constexpr bool IsProjMaxPlusMat
7071 = detail::IsProjMaxPlusMatHelper<T>::value;
7072
7073 namespace matrix {
7074 // \ingroup projmaxplus_group
7075 //
7089 //! \throws LibsemigroupsException if
7090 //! `throw_if_bad_entry(x.underlying_matrix())` throws.
7091 template <typename Mat>
7092 constexpr std::enable_if_t<IsProjMaxPlusMat<Mat>>
7093 throw_if_bad_entry(Mat const& x) {
7094 throw_if_bad_entry(x.underlying_matrix());
7095 }
7096
7097 // \ingroup projmaxplus_group
7098 //
7112 //! \throws LibsemigroupsException if
7113 //! `throw_if_bad_entry(x.underlying_matrix(), val)` throws.
7114 template <typename Mat>
7115 constexpr std::enable_if_t<IsProjMaxPlusMat<Mat>>
7116 throw_if_bad_entry(Mat const& x, typename Mat::scalar_type val) {
7117 throw_if_bad_entry(x.underlying_matrix(), val);
7118 }
7119
7121 // Matrix helpers - pow
7123
7157 //! \endcode
7158 // TODO(1) pow_no_checks
7159 // TODO(2) version that changes x in-place
7160 template <typename Mat>
7161 Mat pow(Mat const& x, typename Mat::scalar_type e) {
7162 using scalar_type = typename Mat::scalar_type;
7163
7164 if constexpr (std::is_signed<scalar_type>::value) {
7165 if (e < 0) {
7167 "negative exponent, expected value >= 0, found {}", e);
7168 }
7169 }
7170
7172
7173 typename Mat::semiring_type const* sr = nullptr;
7174
7175 if constexpr (IsMatWithSemiring<Mat>) {
7176 sr = x.semiring();
7177 }
7178
7179 if (e == 0) {
7180 return x.one();
7181 }
7182
7183 auto y = Mat(x);
7184 if (e == 1) {
7185 return y;
7186 }
7187 auto z = (e % 2 == 0 ? x.one() : y);
7188
7189 Mat tmp(sr, x.number_of_rows(), x.number_of_cols());
7190 while (e > 1) {
7191 tmp.product_inplace_no_checks(y, y);
7192 std::swap(y, tmp);
7193 e /= 2;
7194 if (e % 2 == 1) {
7195 tmp.product_inplace_no_checks(z, y);
7196 std::swap(z, tmp);
7197 }
7198 }
7199 return z;
7200 }
7201
7203 // Matrix helpers - rows
7205
7222 //!
7223 //! \complexity
7224 //! \f$O(m)\f$ where \f$m\f$ is the number of rows in the matrix \p x.
7225 template <typename Mat, typename = std::enable_if_t<IsDynamicMatrix<Mat>>>
7228 x.rows(container);
7229 return container;
7230 }
7231
7250 //! \complexity
7251 //! \f$O(m)\f$ where \f$m\f$ is the number of rows in the matrix \p x.
7252 template <typename Mat, typename = std::enable_if_t<IsStaticMatrix<Mat>>>
7253 detail::StaticVector1<typename Mat::RowView, Mat::nr_rows>
7254 rows(Mat const& x) {
7255 detail::StaticVector1<typename Mat::RowView, Mat::nr_rows> container;
7256 x.rows(container);
7257 return container;
7258 }
7259
7261 // Matrix helpers - bitset_rows
7263
7264 // The main function
7293 //! \complexity
7294 //! \f$O(mn)\f$ where \f$m\f$ is the number of rows in `views` and
7295 //! and \f$n\f$ is the number of columns in any vector in `views`.
7296 template <typename Mat, size_t R, size_t C, typename Container>
7297 void bitset_rows(Container&& views,
7298 detail::StaticVector1<BitSet<C>, R>& result) {
7299 using RowView = typename Mat::RowView;
7300 using value_type = typename std::decay_t<Container>::value_type;
7301 // std::vector<bool> is used as value_type in the benchmarks
7302 static_assert(IsBMat<Mat>, "IsBMat<Mat> must be true!");
7305 "Container::value_type must equal Mat::RowView or "
7306 "std::vector<bool>!!");
7307 static_assert(R <= BitSet<1>::max_size(),
7308 "R must be at most BitSet<1>::max_size()!");
7309 static_assert(C <= BitSet<1>::max_size(),
7310 "C must be at most BitSet<1>::max_size()!");
7311 LIBSEMIGROUPS_ASSERT(views.size() <= R);
7312 LIBSEMIGROUPS_ASSERT(views.empty() || views[0].size() <= C);
7313 for (auto const& v : views) {
7314 result.emplace_back(v.cbegin(), v.cend());
7315 }
7316 }
7317
7347 //! \complexity
7348 //! \f$O(mn)\f$ where \f$m\f$ is the number of rows in \p views and
7349 //! and \f$n\f$ is the number of columns in any vector in \p views.
7350 template <typename Mat, size_t R, size_t C, typename Container>
7351 auto bitset_rows(Container&& views) {
7352 using RowView = typename Mat::RowView;
7353 using value_type = typename std::decay_t<Container>::value_type;
7354 static_assert(IsBMat<Mat>, "IsBMat<Mat> must be true!");
7357 "Container::value_type must equal Mat::RowView or "
7358 "std::vector<bool>!!");
7359 static_assert(R <= BitSet<1>::max_size(),
7360 "R must be at most BitSet<1>::max_size()!");
7361 static_assert(C <= BitSet<1>::max_size(),
7362 "C must be at most BitSet<1>::max_size()!");
7363 LIBSEMIGROUPS_ASSERT(views.size() <= R);
7364 LIBSEMIGROUPS_ASSERT(views.empty() || views[0].size() <= C);
7365 detail::StaticVector1<BitSet<C>, R> result;
7367 return result;
7368 }
7369
7370 // Helper
7396 //! \complexity
7397 //! \f$O(mn)\f$ where \f$m\f$ is the number of rows in \p x and and
7398 //! \f$n\f$ is the number of columns in \p x.
7399 template <typename Mat, size_t R, size_t C>
7400 void bitset_rows(Mat const& x,
7401 detail::StaticVector1<BitSet<C>, R>& result) {
7402 static_assert(IsBMat<Mat>, "IsBMat<Mat> must be true!");
7403 static_assert(R <= BitSet<1>::max_size(),
7404 "R must be at most BitSet<1>::max_size()!");
7405 static_assert(C <= BitSet<1>::max_size(),
7406 "C must be at most BitSet<1>::max_size()!");
7407 LIBSEMIGROUPS_ASSERT(x.number_of_cols() <= C);
7408 LIBSEMIGROUPS_ASSERT(x.number_of_rows() <= R);
7409 bitset_rows<Mat>(std::move(rows(x)), result);
7410 }
7411
7412 // Helper
7428 //! \complexity
7429 //! \f$O(mn)\f$ where \f$m\f$ is the number of rows in \p x and
7430 //! and \f$n\f$ is the number of columns in \p x.
7431 template <typename Mat>
7432 auto bitset_rows(Mat const& x) {
7433 static_assert(IsBMat<Mat>, "IsBMat<Mat> must be true!");
7434 LIBSEMIGROUPS_ASSERT(x.number_of_rows() <= BitSet<1>::max_size());
7435 LIBSEMIGROUPS_ASSERT(x.number_of_cols() <= BitSet<1>::max_size());
7436 size_t const M = detail::BitSetCapacity<Mat>::value;
7438 }
7439
7441 // Matrix helpers - bitset_row_basis
7443
7465 //! \f$c\f$ is the size of each bitset in `rows`.
7466 // This works with std::vector and StaticVector1, with value_type equal
7467 // to std::bitset and BitSet.
7468 template <typename Mat, typename Container>
7469 void bitset_row_basis(Container&& rows, std::decay_t<Container>& result) {
7470 using value_type = typename std::decay_t<Container>::value_type;
7471 static_assert(IsBMat<Mat>, "IsBMat<Mat> must be true!");
7472 static_assert(IsBitSet<value_type> || detail::IsStdBitSet<value_type>,
7473 "Container::value_type must be BitSet or std::bitset");
7474 LIBSEMIGROUPS_ASSERT(rows.size() <= BitSet<1>::max_size());
7475 LIBSEMIGROUPS_ASSERT(rows.empty()
7476 || rows[0].size() <= BitSet<1>::max_size());
7477
7478 std::sort(rows.begin(), rows.end(), detail::LessBitSet());
7479 // Remove duplicates
7480 rows.erase(std::unique(rows.begin(), rows.end()), rows.end());
7481 for (size_t i = 0; i < rows.size(); ++i) {
7482 value_type cup;
7483 cup.reset();
7484 for (size_t j = 0; j < i; ++j) {
7485 if ((rows[i] & rows[j]) == rows[j]) {
7486 cup |= rows[j];
7487 }
7488 }
7489 for (size_t j = i + 1; j < rows.size(); ++j) {
7490 if ((rows[i] & rows[j]) == rows[j]) {
7491 cup |= rows[j];
7492 }
7493 }
7494 if (cup != rows[i]) {
7495 result.push_back(std::move(rows[i]));
7496 }
7497 }
7498 }
7499
7520 //! \complexity
7521 //! \f$O(r ^ 2 c)\f$ where \f$r\f$ is the size of \p rows and
7522 //! \f$c\f$ is the size of each bitset in \p rows.
7523 template <typename Mat, typename Container>
7524 std::decay_t<Container> bitset_row_basis(Container&& rows) {
7525 using value_type = typename std::decay_t<Container>::value_type;
7526 static_assert(IsBMat<Mat>, "IsBMat<Mat> must be true!");
7527 static_assert(IsBitSet<value_type> || detail::IsStdBitSet<value_type>,
7528 "Container::value_type must be BitSet or std::bitset");
7529 LIBSEMIGROUPS_ASSERT(rows.size() <= BitSet<1>::max_size());
7530 LIBSEMIGROUPS_ASSERT(rows.empty()
7531 || rows[0].size() <= BitSet<1>::max_size());
7532 std::decay_t<Container> result;
7534 return result;
7535 }
7536
7561 //! \complexity
7562 //! \f$O(r ^ 2 c)\f$ where \f$r\f$ is the number of rows in \p x and
7563 //! \f$c\f$ is the number of columns in \p x.
7564 template <typename Mat, size_t M = detail::BitSetCapacity<Mat>::value>
7565 detail::StaticVector1<BitSet<M>, M> bitset_row_basis(Mat const& x) {
7566 static_assert(IsBMat<Mat>, "IsBMat<Mat> must be true!");
7567 LIBSEMIGROUPS_ASSERT(x.number_of_rows() <= BitSet<1>::max_size());
7568 LIBSEMIGROUPS_ASSERT(x.number_of_cols() <= BitSet<1>::max_size());
7569 detail::StaticVector1<BitSet<M>, M> result;
7571 return result;
7572 }
7573
7594 //! \complexity
7595 //! \f$O(r ^ 2 c)\f$ where \f$r\f$ is the number of rows in \p x
7596 //! and \f$c\f$ is the number of columns in \p x.
7597 template <typename Mat, typename Container>
7598 void bitset_row_basis(Mat const& x, Container& result) {
7599 using value_type = typename Container::value_type;
7600 static_assert(IsBMat<Mat>, "IsBMat<Mat> must be true!");
7601 static_assert(IsBitSet<value_type> || detail::IsStdBitSet<value_type>,
7602 "Container::value_type must be BitSet or std::bitset");
7603 LIBSEMIGROUPS_ASSERT(x.number_of_rows() <= BitSet<1>::max_size());
7604 LIBSEMIGROUPS_ASSERT(x.number_of_cols() <= BitSet<1>::max_size());
7606 }
7607
7609 // Matrix helpers - row_basis - MaxPlusTruncMat
7611
7637 //! \f$O(r ^ 2 c)\f$ where \f$r\f$ is the size of \p views and
7638 //! \f$c\f$ is the size of each row view or bit set in \p views.
7639 template <typename Mat, typename Container>
7640 std::enable_if_t<IsMaxPlusTruncMat<Mat>>
7641 row_basis(Container&& views, std::decay_t<Container>& result) {
7642 using value_type = typename std::decay_t<Container>::value_type;
7644 "Container::value_type must be Mat::RowView");
7645 using scalar_type = typename Mat::scalar_type;
7646 using Row = typename Mat::Row;
7647
7648 if (views.empty()) {
7649 return;
7650 }
7651
7652 LIBSEMIGROUPS_ASSERT(result.empty());
7653
7654 std::sort(views.begin(), views.end());
7655 Row tmp1(views[0]);
7656
7657 for (size_t r1 = 0; r1 < views.size(); ++r1) {
7658 if (r1 == 0 || views[r1] != views[r1 - 1]) {
7659 std::fill(tmp1.begin(), tmp1.end(), tmp1.scalar_zero());
7660 for (size_t r2 = 0; r2 < r1; ++r2) {
7661 scalar_type max_scalar = matrix::threshold(tmp1);
7662 for (size_t c = 0; c < tmp1.number_of_cols(); ++c) {
7663 if (views[r2][c] == tmp1.scalar_zero()) {
7664 continue;
7665 }
7666 if (views[r1][c] >= views[r2][c]) {
7667 if (views[r1][c] != matrix::threshold(tmp1)) {
7668 max_scalar
7669 = std::min(max_scalar, views[r1][c] - views[r2][c]);
7670 }
7671 } else {
7672 max_scalar = tmp1.scalar_zero();
7673 break;
7674 }
7675 }
7676 if (max_scalar != tmp1.scalar_zero()) {
7677 tmp1 += views[r2] * max_scalar;
7678 }
7679 }
7680 if (tmp1 != views[r1]) {
7681 result.push_back(views[r1]);
7682 }
7683 }
7684 }
7685 }
7686
7688 // Matrix helpers - row_basis - BMat
7690
7691 // This version of row_basis for BMat's is for used for compatibility
7692 // with the MatrixCommon framework, i.e. so that BMat's exhibit the same
7693 // interface/behaviour as other matrices.
7694 //
7695 // This version takes a container of row views of BMat, converts it to a
7696 // container of BitSets, computes the row basis using the BitSets, then
7697 // selects those row views in views that belong to the computed basis.
7698
7714 //! \exceptions
7715 //! \no_libsemigroups_except
7716 // TODO(2) complexity
7717 template <typename Mat, typename Container>
7718 std::enable_if_t<IsBMat<Mat>> row_basis(Container&& views,
7719 std::decay_t<Container>& result) {
7720 using RowView = typename Mat::RowView;
7721 using value_type = typename std::decay_t<Container>::value_type;
7722 // std::vector<bool> is used as value_type in the benchmarks
7725 "Container::value_type must equal Mat::RowView or "
7726 "std::vector<bool>!!");
7727
7728 if (views.empty()) {
7729 return;
7730 }
7731
7732 // Convert RowViews to BitSets
7733 size_t const M = detail::BitSetCapacity<Mat>::value;
7735 using bitset_type = typename decltype(br)::value_type;
7736
7737 // Map for converting bitsets back to row views
7739 LIBSEMIGROUPS_ASSERT(br.size() == views.size());
7740 for (size_t i = 0; i < br.size(); ++i) {
7741 lookup.insert({br[i], i});
7742 }
7743
7744 // Compute rowbasis using bitsets + convert back to rowviews
7745 for (auto const& bs : bitset_row_basis<Mat>(br)) {
7746 auto it = lookup.find(bs);
7747 LIBSEMIGROUPS_ASSERT(it != lookup.end());
7748 result.push_back(views[it->second]);
7749 }
7750 }
7751
7753 // Matrix helpers - row_basis - generic helpers
7755
7777 // Row basis of rowspace of matrix <x> appended to <result>
7778 template <typename Mat,
7779 typename Container,
7780 typename = std::enable_if_t<IsMatrix<Mat>>>
7781 void row_basis(Mat const& x, Container& result) {
7782 row_basis<Mat>(std::move(rows(x)), result);
7783 }
7784
7802 //! \f$O(r ^ 2 c)\f$ where \f$r\f$ is the number of rows in \p x
7803 //! and \f$c\f$ is the number of columns in \p x.
7804 // Row basis of rowspace of matrix <x>
7805 template <typename Mat, typename = std::enable_if_t<IsDynamicMatrix<Mat>>>
7808 row_basis(x, container);
7809 return container;
7810 }
7811
7829 //! \f$O(r ^ 2 c)\f$ where \f$r\f$ is the number of rows in \p x
7830 //! and \f$c\f$ is the number of columns in \p x.
7831 template <typename Mat, typename = std::enable_if_t<IsStaticMatrix<Mat>>>
7832 detail::StaticVector1<typename Mat::RowView, Mat::nr_rows>
7833 row_basis(Mat const& x) {
7834 detail::StaticVector1<typename Mat::RowView, Mat::nr_rows> container;
7835 row_basis(x, container);
7836 return container;
7837 }
7838
7855 //! \exceptions
7856 //! \no_libsemigroups_except
7857 // TODO(2) complexity
7858 template <typename Mat, typename Container>
7859 std::decay_t<Container> row_basis(Container&& rows) {
7860 using value_type = typename std::decay_t<Container>::value_type;
7861 static_assert(IsMatrix<Mat>, "IsMatrix<Mat> must be true!");
7863 "Container::value_type must be Mat::RowView");
7864
7865 std::decay_t<Container> result;
7867 return result;
7868 }
7869
7871 // Matrix helpers - row_space_size
7873
7901 //! auto x = make<BMat<>>({{1, 0, 0}, {0, 0, 1}, {0, 1, 0}});
7902 //! matrix::row_space_size(x); // returns 7
7903 //! \endcode
7904 template <typename Mat, typename = std::enable_if_t<IsBMat<Mat>>>
7905 size_t row_space_size(Mat const& x) {
7906 size_t const M = detail::BitSetCapacity<Mat>::value;
7907 auto bitset_row_basis_ = bitset_row_basis<Mat>(
7909
7911 st.insert(bitset_row_basis_.cbegin(), bitset_row_basis_.cend());
7912 std::vector<BitSet<M>> orb(bitset_row_basis_.cbegin(),
7913 bitset_row_basis_.cend());
7914 for (size_t i = 0; i < orb.size(); ++i) {
7915 for (auto& row : bitset_row_basis_) {
7916 auto cup = orb[i];
7917 for (size_t j = 0; j < x.number_of_rows(); ++j) {
7918 cup.set(j, cup[j] || row[j]);
7919 }
7920 if (st.insert(cup).second) {
7921 orb.push_back(std::move(cup));
7922 }
7923 }
7924 }
7925 return orb.size();
7926 }
7927
7928 } // namespace matrix
7929
7946 //! \no_libsemigroups_except
7947 //!
7948 //! \warning This function does not detect overflows of `Mat::scalar_type`.
7949 template <typename Mat>
7950 auto operator+(typename Mat::scalar_type a, Mat const& x)
7951 -> std::enable_if_t<IsMatrix<Mat>, Mat> {
7952 return x + a;
7953 }
7954
7971 //! \no_libsemigroups_except
7972 //!
7973 //! \warning This function does not detect overflows of `Mat::scalar_type`.
7974 template <typename Mat>
7975 auto operator*(typename Mat::scalar_type a, Mat const& x)
7976 -> std::enable_if_t<IsMatrix<Mat>, Mat> {
7977 return x * a;
7978 }
7979
7990
8015 //! \f$n\f$ is the number of columns of the matrix.
8016 template <typename Mat,
8017 typename
8018 = std::enable_if_t<IsMatrix<Mat> && !IsMatWithSemiring<Mat>>>
8020 detail::throw_if_any_row_wrong_size(rows);
8021 detail::throw_if_bad_dim<Mat>(rows);
8022 Mat m(rows);
8024 return m;
8025 }
8026
8051 //! \f$n\f$ is the number of columns of the matrix.
8052 template <typename Mat,
8053 typename
8054 = std::enable_if_t<IsMatrix<Mat> && !IsMatWithSemiring<Mat>>>
8056 rows) {
8058 }
8059
8085 //! parameter \c R is \c 1.
8086 template <typename Mat,
8087 typename
8088 = std::enable_if_t<IsMatrix<Mat> && !IsMatWithSemiring<Mat>>>
8090 // TODO(0) Add row dimension checking for compile-time size matrices
8091 Mat m(row);
8093 return m;
8094 }
8095 // TODO(1) vector version of above
8096
8128 template <typename Mat,
8129 typename Semiring,
8130 typename = std::enable_if_t<IsMatrix<Mat>>>
8131 // TODO(1) pass Semiring by reference, this is hard mostly due to the way
8132 // the tests are written, which is not optimal.
8133 Mat make(Semiring const* semiring,
8136 detail::throw_if_any_row_wrong_size(rows);
8137 detail::throw_if_bad_dim<Mat>(rows);
8138 Mat m(semiring, rows);
8140 return m;
8141 }
8142
8173 //! \f$n\f$ is the number of columns of the matrix.
8174 template <typename Mat,
8175 typename Semiring,
8176 typename = std::enable_if_t<IsMatrix<Mat>>>
8177 Mat make(Semiring const* semiring,
8179 detail::throw_if_any_row_wrong_size(rows);
8180 detail::throw_if_bad_dim<Mat>(rows);
8181 Mat m(semiring, rows);
8183 return m;
8184 }
8185
8207 //! \f$O(n)\f$ where \f$n\f$ is the number of columns of the matrix.
8208 template <typename Mat,
8209 typename Semiring,
8210 typename = std::enable_if_t<IsMatrix<Mat>>>
8211 Mat make(Semiring const* semiring,
8213 // TODO(0) Add row dimension checking for compile-time size matrices
8214 Mat m(semiring, row);
8216 return m;
8217 }
8218
8244 //! \f$O(mn)\f$ where \f$m\f$ is the number of rows and \f$n\f$ is the
8245 //! number of columns of the matrix.
8246 template <size_t R, size_t C, typename Scalar>
8251 }
8252
8254 // Printing etc...
8256
8266 //!
8267 //! \exceptions
8268 //! \no_libsemigroups_except
8269 template <typename S, typename T>
8271 detail::RowViewCommon<S, T> const& x) {
8272 os << "{";
8273 for (auto it = x.cbegin(); it != x.cend(); ++it) {
8274 os << *it;
8275 if (it != x.cend() - 1) {
8276 os << ", ";
8277 }
8278 }
8279 os << "}";
8280 return os;
8281 }
8282
8296 //!
8297 //! \exceptions
8298 //! \no_libsemigroups_except
8299 template <typename Mat>
8300 auto operator<<(std::ostringstream& os, Mat const& x)
8301 -> std::enable_if_t<IsMatrix<Mat>, std::ostringstream&> {
8302 size_t n = 0;
8303 if (x.number_of_rows() != 1) {
8304 os << "{";
8305 }
8306 for (auto&& r : matrix::rows(x)) {
8307 os << r;
8308 if (n != x.number_of_rows() - 1) {
8309 os << ", ";
8310 }
8311 n++;
8312 }
8313 if (x.number_of_rows() != 1) {
8314 os << "}";
8315 }
8316 return os;
8317 }
8318
8332 //! (default: \c 72).
8333 //!
8334 //! \throws LibsemigroupsException if \p braces does not have size \c 2.
8335 template <typename Mat>
8336 auto to_human_readable_repr(Mat const& x,
8337 std::string const& prefix,
8338 std::string const& short_name = "",
8339 std::string const& braces = "{}",
8340 size_t max_width = 72)
8341 -> std::enable_if_t<IsMatrix<Mat>, std::string> {
8342 if (braces.size() != 2) {
8344 "the 4th argument (braces) must have size 2, found {}",
8345 braces.size());
8346 }
8347
8348 size_t const R = x.number_of_rows();
8349 size_t const C = x.number_of_cols();
8350
8351 std::vector<size_t> max_col_widths(C, 0);
8352 std::vector<size_t> row_widths(C, prefix.size() + 1);
8353 for (size_t r = 0; r < R; ++r) {
8354 for (size_t c = 0; c < C; ++c) {
8355 size_t width
8356 = detail::unicode_string_length(detail::entry_repr(x(r, c)));
8357 row_widths[r] += width;
8358 if (width > max_col_widths[c]) {
8359 max_col_widths[c] = width;
8360 }
8361 }
8362 }
8363 auto col_width
8364 = *std::max_element(max_col_widths.begin(), max_col_widths.end());
8365 // The total width if we pad the entries according to the widest column.
8366 auto const total_width = col_width * C + prefix.size() + 1;
8367 if (total_width > max_width) {
8368 // Padding according to the widest column is too wide!
8369 if (*std::max_element(row_widths.begin(), row_widths.end()) > max_width) {
8370 // If the widest row is too wide, then use the short name
8371 return fmt::format(
8372 "<{}x{} {}>", x.number_of_rows(), x.number_of_cols(), short_name);
8373 }
8374 // If the widest row is not too wide, then just don't pad the entries
8375 col_width = 0;
8376 }
8377
8378 std::string result = fmt::format("{}", prefix);
8379 std::string rindent;
8380 auto const lbrace = braces[0], rbrace = braces[1];
8381 if (R != 0 && C != 0) {
8382 result += lbrace;
8383 for (size_t r = 0; r < R; ++r) {
8384 result += fmt::format("{}{}", rindent, lbrace);
8385 rindent = std::string(prefix.size() + 1, ' ');
8386 std::string csep = "";
8387 for (size_t c = 0; c < C; ++c) {
8388 result += fmt::format(
8389 "{}{:>{}}", csep, detail::entry_repr(x(r, c)), col_width);
8390 csep = ", ";
8391 }
8392 result += fmt::format("{}", rbrace);
8393 if (r != R - 1) {
8394 result += ",\n";
8395 }
8396 }
8397 result += rbrace;
8398 }
8399 result += ")";
8400 return result;
8401 }
8402
8404 // Adapters
8406
8421
8429 //! satisfying \ref IsMatrix<Mat>.
8430 //!
8431 //! \tparam Mat the type of matrices.
8432 template <typename Mat>
8433 struct Complexity<Mat, std::enable_if_t<IsMatrix<Mat>>> {
8443 //! \noexcept
8444 //!
8445 //! \complexity
8446 //! Constant.
8447 constexpr size_t operator()(Mat const& x) const noexcept {
8448 return x.number_of_rows() * x.number_of_rows() * x.number_of_rows();
8449 }
8450 };
8451
8459 //! \ref IsMatrix<Mat>.
8460 //!
8461 //! \tparam Mat the type of matrices.
8462 template <typename Mat>
8463 struct Degree<Mat, std::enable_if_t<IsMatrix<Mat>>> {
8472 //! \noexcept
8473 //!
8474 //! \complexity
8475 //! Constant.
8476 constexpr size_t operator()(Mat const& x) const noexcept {
8477 return x.number_of_rows();
8478 }
8479 };
8480
8488 //! \ref IsMatrix<Mat>.
8489 //!
8490 //! \tparam Mat the type of matrices.
8491 template <typename Mat>
8492 struct Hash<Mat, std::enable_if_t<IsMatrix<Mat>>> {
8501 //! \no_libsemigroups_except
8502 //!
8503 //! \complexity
8504 //! Constant.
8505 constexpr size_t operator()(Mat const& x) const {
8506 return x.hash_value();
8507 }
8508 };
8509
8522 //! It is not possible to increase the degree of any of the types
8523 //! satisfying \ref IsMatrix, and as such the call operator of this type
8524 //! does nothing.
8525 template <typename Mat>
8526 struct IncreaseDegree<Mat, std::enable_if_t<IsMatrix<Mat>>> {
8530 constexpr void operator()(Mat&, size_t) const noexcept {
8531 // static_assert(false, "Cannot increase degree for Matrix");
8532 LIBSEMIGROUPS_ASSERT(false);
8533 }
8534 };
8535
8543 //! \ref IsMatrix.
8544 //!
8545 //! \tparam Mat the type of matrices.
8546 template <typename Mat>
8547 struct One<Mat, std::enable_if_t<IsMatrix<Mat>>> {
8557 //!
8558 //! \complexity
8559 //! \f$O(m ^ 2)\f$ where \f$m\f$ is the number of rows of the
8560 //! matrix \p x.
8561 inline Mat operator()(Mat const& x) const {
8562 return x.one();
8563 }
8564 };
8565
8573 //! \ref IsMatrix<Mat>.
8574 //!
8575 //! \tparam Mat the type of matrices.
8576 template <typename Mat>
8577 struct Product<Mat, std::enable_if_t<IsMatrix<Mat>>> {
8593 //!
8594 //! \warning
8595 //! This function only works for square matrices.
8596 inline void
8597 operator()(Mat& xy, Mat const& x, Mat const& y, size_t = 0) const {
8598 xy.product_inplace_no_checks(x, y);
8599 }
8600 };
8601} // namespace libsemigroups
8602
8603namespace std {
8604 template <size_t N,
8605 typename Mat,
8606 std::enable_if_t<libsemigroups::IsMatrix<Mat>>>
8607 inline void swap(Mat& x, Mat& y) noexcept {
8608 x.swap(y);
8609 }
8610} // namespace std
8611
8612#endif // LIBSEMIGROUPS_MATRIX_HPP_
DynamicMatrix(std::initializer_list< scalar_type > const &c)
Construct a vector from a std::initializer_list.
Definition matrix.hpp:2893
DynamicMatrix(std::initializer_list< std::initializer_list< scalar_type > > const &m)
Construct a matrix from std::initializer_list of std::initializer_list of scalars.
Definition matrix.hpp:2916
DynamicMatrix & operator=(DynamicMatrix &&)=default
Default move assignment operator.
scalar_reference at(size_t r, size_t c)
Returns a reference to the specified entry of the matrix.
ProdOp Prod
Alias for the template parameter ProdOp.
Definition matrix.hpp:2812
void product_inplace_no_checks(DynamicMatrix const &x, DynamicMatrix const &y)
Multiplies x and y and stores the result in this.
DynamicMatrix & operator=(DynamicMatrix const &)=default
Default copy assignment operator.
DynamicMatrix Row
The type of a row of a DynamicMatrix.
Definition matrix.hpp:2803
PlusOp Plus
Alias for the template parameter PlusOp.
Definition matrix.hpp:2809
void rows(T &x) const
Add row views for every row in the matrix to a container.
scalar_type scalar_one() const noexcept
Returns the multiplicative identity of the underlying semiring.
typename MatrixCommon::scalar_const_reference scalar_const_reference
The type of const references to the entries in the matrix.
Definition matrix.hpp:2799
void swap(DynamicMatrix &that) noexcept
Swaps the contents of *this with the contents of that.
Definition matrix.hpp:3175
const_iterator cend() noexcept
Returns a const iterator pointing one beyond the last entry in the matrix.
static DynamicMatrix one(size_t n)
Construct the identity matrix.
Definition matrix.hpp:2997
DynamicMatrix(size_t r, size_t c)
Construct a matrix with given dimensions.
Definition matrix.hpp:2870
ZeroOp Zero
Alias for the template parameter ZeroOp.
Definition matrix.hpp:2815
scalar_type scalar_zero() const noexcept
Returns the additive identity of the underlying semiring.
semiring_type const * semiring() const noexcept
Returns the underlying semiring.
OneOp One
Alias for the template parameter OneOp.
Definition matrix.hpp:2818
void semiring_type
Alias for the semiring type (void).
Definition matrix.hpp:2825
RowView row(size_t i) const
Returns a view into a row.
DynamicMatrix(RowView const &rv)
Construct a row from a row view.
Definition matrix.hpp:2950
iterator begin() noexcept
Returns an iterator pointing at the first entry.
size_t number_of_rows() const noexcept
Returns the number of rows of the matrix.
DynamicRowView< PlusOp, ProdOp, ZeroOp, OneOp, Scalar > RowView
The type of a row view into a DynamicMatrix.
Definition matrix.hpp:2806
RowView row_no_checks(size_t i) const
Returns a view into a row.
DynamicMatrix(std::vector< std::vector< scalar_type > > const &m)
Construct a matrix from std::vector of std::vector of scalars.
Definition matrix.hpp:2935
typename MatrixCommon::scalar_reference scalar_reference
The type of references to the entries in the matrix.
Definition matrix.hpp:2794
scalar_reference at(size_t r, size_t c) const
Returns a const reference to the specified entry of the matrix.
DynamicMatrix(DynamicMatrix const &)=default
Default copy constructor.
size_t hash_value() const
Return a hash value of a matrix.
const_iterator cbegin() noexcept
Returns a const iterator pointing at the first entry.
DynamicMatrix(DynamicMatrix &&)=default
Default move constructor.
typename MatrixCommon::scalar_type scalar_type
The type of the entries in the matrix.
Definition matrix.hpp:2791
size_t number_of_cols() const noexcept
Returns the number of columns of the matrix.
std::pair< scalar_type, scalar_type > coords(const_iterator it) const
Get the coordinates of an iterator.
iterator end() noexcept
Returns an iterator pointing one beyond the last entry in the matrix.
DynamicMatrix(Semiring const *sr, std::initializer_list< std::initializer_list< scalar_type > > const &rws)
Construct a matrix over a given semiring (std::initializer_list of std::initializer_list).
Definition matrix.hpp:3314
DynamicMatrix & operator=(DynamicMatrix &&)=default
Default move assignment operator.
static DynamicMatrix one(Semiring const *semiring, size_t n)
Construct the identity matrix.
Definition matrix.hpp:3391
scalar_reference at(size_t r, size_t c)
Returns a reference to the specified entry of the matrix.
DynamicMatrix(Semiring const *sr, std::vector< std::vector< scalar_type > > const &rws)
Construct a matrix over a given semiring (std::vector of std::vector).
Definition matrix.hpp:3337
DynamicMatrix(Semiring const *sr, size_t r, size_t c)
Construct a matrix over a given semiring with given dimensions.
Definition matrix.hpp:3294
void product_inplace_no_checks(DynamicMatrix const &x, DynamicMatrix const &y)
Multiplies x and y and stores the result in this.
DynamicMatrix & operator=(DynamicMatrix const &)=default
Default copy assignment operator.
DynamicMatrix Row
Alias for the type of the rows of a DynamicMatrix.
Definition matrix.hpp:3251
void rows(T &x) const
Add row views for every row in the matrix to a container.
scalar_type scalar_one() const noexcept
Returns the multiplicative identity of the underlying semiring.
typename MatrixCommon::scalar_const_reference scalar_const_reference
Alias for const references to the template parameter Scalar.
Definition matrix.hpp:3247
void swap(DynamicMatrix &that) noexcept
Swaps the contents of *this with the contents of that.
Definition matrix.hpp:3571
const_iterator cend() noexcept
Returns a const iterator pointing one beyond the last entry in the matrix.
scalar_type scalar_zero() const noexcept
Returns the additive identity of the underlying semiring.
void transpose_no_checks()
Transpose a matrix in-place.
semiring_type const * semiring() const noexcept
Returns the underlying semiring.
RowView row(size_t i) const
Returns a view into a row.
DynamicMatrix(RowView const &rv)
Construct a row over a given semiring (RowView).
Definition matrix.hpp:3371
iterator begin() noexcept
Returns an iterator pointing at the first entry.
size_t number_of_rows() const noexcept
Returns the number of rows of the matrix.
DynamicMatrix(Semiring const *sr, std::initializer_list< scalar_type > const &rw)
Construct a row over a given semiring (std::initializer_list).
Definition matrix.hpp:3356
RowView row_no_checks(size_t i) const
Returns a view into a row.
typename MatrixCommon::scalar_reference scalar_reference
Alias for references to the template parameter Scalar.
Definition matrix.hpp:3242
scalar_reference at(size_t r, size_t c) const
Returns a const reference to the specified entry of the matrix.
DynamicMatrix(DynamicMatrix const &)=default
Default copy constructor.
size_t hash_value() const
Return a hash value of a matrix.
const_iterator cbegin() noexcept
Returns a const iterator pointing at the first entry.
DynamicMatrix(DynamicMatrix &&)=default
Default move constructor.
typename MatrixCommon::scalar_type scalar_type
Alias for the template parameter Scalar.
Definition matrix.hpp:3239
DynamicRowView< Semiring, Scalar > RowView
Alias for the type of row views of a DynamicMatrix.
Definition matrix.hpp:3254
Semiring semiring_type
Alias for the template parameter Semiring.
Definition matrix.hpp:3259
size_t number_of_cols() const noexcept
Returns the number of columns of the matrix.
void transpose()
Transpose a matrix in-place.
std::pair< scalar_type, scalar_type > coords(const_iterator it) const
Get the coordinates of an iterator.
iterator end() noexcept
Returns an iterator pointing one beyond the last entry in the matrix.
DynamicRowView & operator=(DynamicRowView &&)=default
Default move assignment operator.
size_t size() const noexcept
Returns the size of the row.
DynamicRowView & operator=(DynamicRowView const &)=default
Default copy assignment operator.
DynamicRowView(DynamicRowView const &)=default
Default copy constructor.
DynamicRowView(DynamicRowView &&)=default
Default move constructor.
DynamicRowView(Row const &r)
Construct a row view from a Row.
Definition matrix.hpp:1572
typename RowViewCommon::iterator iterator
Alias for const iterators pointing at entries of a matrix.
Definition matrix.hpp:1536
typename RowViewCommon::scalar_const_reference scalar_const_reference
Alias for const references to the template parameter Scalar.
Definition matrix.hpp:1547
typename RowViewCommon::scalar_reference scalar_reference
Alias for references to the template parameter Scalar.
Definition matrix.hpp:1542
iterator begin() noexcept
Returns a iterator pointing at the first entry.
iterator cend()
Returns a const iterator pointing one beyond the last entry of the row.
typename matrix_type::Row Row
Alias for the type of a row in the underlying matrix.
Definition matrix.hpp:1554
typename RowViewCommon::const_iterator const_iterator
Alias for const iterators pointing at entries of a matrix.
Definition matrix.hpp:1533
const_iterator cbegin() const noexcept
Returns a const iterator pointing at the first entry.
iterator end()
Returns a iterator pointing one beyond the last entry of the row.
Scalar scalar_type
Alias for the template parameter Scalar.
Definition matrix.hpp:1539
typename RowViewCommon::matrix_type matrix_type
Alias for the type of the underlying matrix.
Definition matrix.hpp:1551
size_t size() const noexcept
Returns the size of the row.
DynamicRowView & operator=(DynamicRowView const &)=default
Default copy assignment operator.
typename RowViewCommon::iterator iterator
Alias for const iterators pointing at entries of a matrix.
Definition matrix.hpp:1690
typename RowViewCommon::scalar_const_reference scalar_const_reference
Alias for const references to the template parameter Scalar.
Definition matrix.hpp:1701
typename RowViewCommon::scalar_reference scalar_reference
Alias for references to the template parameter Scalar.
Definition matrix.hpp:1696
iterator begin() noexcept
Returns a iterator pointing at the first entry.
iterator cend()
Returns a const iterator pointing one beyond the last entry of the row.
typename matrix_type::Row Row
Alias for the type of a row in the underlying matrix.
Definition matrix.hpp:1708
typename RowViewCommon::const_iterator const_iterator
Alias for const iterators pointing at entries of a matrix.
Definition matrix.hpp:1687
const_iterator cbegin() const noexcept
Returns a const iterator pointing at the first entry.
iterator end()
Returns a iterator pointing one beyond the last entry of the row.
Scalar scalar_type
Alias for the template parameter Scalar.
Definition matrix.hpp:1693
typename RowViewCommon::matrix_type matrix_type
Alias for the type of the underlying matrix.
Definition matrix.hpp:1705
DynamicRowView()=default
Default constructor.
Class representing a truncated max-plus semiring.
Definition matrix.hpp:5120
static constexpr Scalar scalar_zero() noexcept
Get the additive identity.
Definition matrix.hpp:5194
Scalar plus_no_checks(Scalar x, Scalar y) const noexcept
Addition in a truncated max-plus semiring.
Definition matrix.hpp:5257
Scalar product_no_checks(Scalar x, Scalar y) const noexcept
Multiplication in a truncated max-plus semiring.
Definition matrix.hpp:5222
MaxPlusTruncSemiring()=delete
Deleted default constructor.
Scalar threshold() const noexcept
Get the threshold.
Definition matrix.hpp:5282
static constexpr Scalar scalar_one() noexcept
Get the multiplicative identity.
Definition matrix.hpp:5180
Class representing a truncated min-plus semiring.
Definition matrix.hpp:5597
static constexpr Scalar scalar_zero() noexcept
Get the additive identity.
Definition matrix.hpp:5670
Scalar plus_no_checks(Scalar x, Scalar y) const noexcept
Addition in a truncated min-plus semiring.
Definition matrix.hpp:5733
Scalar product_no_checks(Scalar x, Scalar y) const noexcept
Multiplication in a truncated min-plus semiring.
Definition matrix.hpp:5698
Scalar threshold() const noexcept
Get the threshold.
Definition matrix.hpp:5758
static constexpr Scalar scalar_one() noexcept
Get the multiplicative identity.
Definition matrix.hpp:5655
MinPlusTruncSemiring()=delete
Deleted default constructor.
NTPSemiring & operator=(NTPSemiring const &)=default
Default copy assignment operator.
static constexpr Scalar scalar_zero() noexcept
Get the additive identity.
Definition matrix.hpp:6221
Scalar plus_no_checks(Scalar x, Scalar y) const noexcept
Addition in an ntp semiring.
Definition matrix.hpp:6279
NTPSemiring()=delete
Deleted default constructor.
Scalar product_no_checks(Scalar x, Scalar y) const noexcept
Multiplication in an ntp semiring.
Definition matrix.hpp:6249
Scalar period() const noexcept
Get the period.
Definition matrix.hpp:6313
Scalar threshold() const noexcept
Get the threshold.
Definition matrix.hpp:6297
static constexpr Scalar scalar_one() noexcept
Get the multiplicative identity.
Definition matrix.hpp:6205
Static matrix class.
Definition matrix.hpp:1865
typename MatrixCommon::iterator iterator
Definition matrix.hpp:1912
StaticMatrix(std::initializer_list< scalar_type > const &c)
Construct a row (from std::initializer_list).
Definition matrix.hpp:1940
typename MatrixCommon::const_iterator const_iterator
Definition matrix.hpp:1915
scalar_const_reference at(size_t r, size_t c) const
Returns a const reference to the specified entry of the matrix.
scalar_reference at(size_t r, size_t c)
Returns a reference to the specified entry of the matrix.
StaticMatrix(StaticMatrix &&)=default
Default move constructor.
typename MatrixCommon::scalar_const_reference scalar_const_reference
Alias for const references to the template parameter Scalar.
Definition matrix.hpp:1890
scalar_reference operator()(size_t r, size_t c)
Returns a reference to the specified entry of the matrix.
void product_inplace_no_checks(StaticMatrix const &x, StaticMatrix const &y)
StaticMatrix(std::initializer_list< std::initializer_list< scalar_type > > const &m)
Construct a matrix.
Definition matrix.hpp:1967
StaticRowView< PlusOp, ProdOp, ZeroOp, OneOp, C, Scalar > RowView
Definition matrix.hpp:1897
static StaticMatrix one() const
Returns an identity matrix.
StaticMatrix(StaticMatrix const &)=default
Default copy constructor.
iterator begin() noexcept
Returns an iterator pointing at the first entry.
StaticMatrix(RowView const &rv)
Construct a row from a row view.
Definition matrix.hpp:2003
size_t number_of_rows() const noexcept
Returns the number of rows of the matrix.
StaticMatrix(std::vector< std::vector< scalar_type > > const &m)
Construct a matrix.
Definition matrix.hpp:1983
StaticMatrix< PlusOp, ProdOp, ZeroOp, OneOp, 1, C, Scalar > Row
Alias for the type of the rows of a StaticMatrix.
Definition matrix.hpp:1894
StaticMatrix()=default
Default constructor.
typename MatrixCommon::scalar_reference scalar_reference
Alias for references to the template parameter Scalar.
Definition matrix.hpp:1885
typename MatrixCommon::scalar_type scalar_type
Alias for the template parameter Scalar.
Definition matrix.hpp:1882
scalar_const_reference operator()(size_t r, size_t c) const
Returns a const reference to the specified entry of the matrix.
StaticMatrix & operator=(StaticMatrix &&)=default
Default move assignment operator.
size_t number_of_cols() const noexcept
Returns the number of columns of the matrix.
StaticMatrix & operator=(StaticMatrix const &)=default
Default copy assignment operator.
std::pair< scalar_type, scalar_type > coords(const_iterator it) const
Class for views into a row of a matrix over a semiring.
Definition matrix.hpp:1080
typename RowViewCommon::iterator iterator
Alias for const iterators pointing at entries of a matrix.
Definition matrix.hpp:1096
typename RowViewCommon::scalar_const_reference scalar_const_reference
Alias for const references to the template parameter Scalar.
Definition matrix.hpp:1107
StaticRowView & operator=(StaticRowView &&)=default
Default move assignment operator.
typename RowViewCommon::scalar_reference scalar_reference
Alias for references to the template parameter Scalar.
Definition matrix.hpp:1102
iterator begin() noexcept
Returns a iterator pointing at the first entry.
iterator cend()
Returns a const iterator pointing one beyond the last entry of the row.
StaticRowView()=default
Default constructor.
typename matrix_type::Row Row
Alias for the type of a row in the underlying matrix.
Definition matrix.hpp:1114
typename RowViewCommon::const_iterator const_iterator
Alias for const iterators pointing at entries of a matrix.
Definition matrix.hpp:1093
const_iterator cbegin() const noexcept
Returns a const iterator pointing at the first entry.
iterator end()
Returns a iterator pointing one beyond the last entry of the row.
Scalar scalar_type
Alias for the template parameter Scalar.
Definition matrix.hpp:1099
StaticRowView(StaticRowView &&)=default
Default move constructor.
typename RowViewCommon::matrix_type matrix_type
Alias for the type of the underlying matrix.
Definition matrix.hpp:1111
StaticRowView(StaticRowView const &)=default
Default copy constructor.
static constexpr size_t size() const noexcept
Returns the size of the row.
StaticRowView & operator=(StaticRowView const &)=default
Default copy assignment operator.
StaticRowView(Row const &r)
Construct a row view from a Row.
T copy(T... args)
T distance(T... args)
T equal(T... args)
T fill(T... args)
T find_if_not(T... args)
T for_each(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.
Bipartition operator*(Bipartition const &x, Bipartition const &y)
Multiply two bipartitions.
std::ostringstream & operator<<(std::ostringstream &os, BMat8 const &x)
Insertion operator.
StaticMatrix< BooleanPlus, BooleanProd, BooleanZero, BooleanOne, R, C, int > StaticBMat
Alias for static boolean matrices.
Definition matrix.hpp:3953
static constexpr bool IsBMat
Helper to check if a type is BMat.
Definition matrix.hpp:4011
DynamicMatrix< BooleanPlus, BooleanProd, BooleanZero, BooleanOne, int > DynamicBMat
Alias for dynamic boolean matrices.
Definition matrix.hpp:3939
std::enable_if_t< IsBMat< Mat > > throw_if_bad_entry(Mat const &m)
Check the entries in a boolean matrix are valid.
Definition matrix.hpp:4056
std::conditional_t< R==0||C==0, DynamicBMat, StaticBMat< R, C > > BMat
Alias template for boolean matrices.
Definition matrix.hpp:3976
NegativeInfinity const NEGATIVE_INFINITY
Value for negative infinity.
Undefined const UNDEFINED
Value for something undefined.
PositiveInfinity const POSITIVE_INFINITY
Value for positive infinity.
#define LIBSEMIGROUPS_EXCEPTION(...)
Throw a LibsemigroupsException.
Definition exception.hpp:99
std::conditional_t< R==0||C==0, DynamicIntMat< Scalar >, StaticIntMat< R, C, Scalar > > IntMat
Alias template for integer matrices.
Definition matrix.hpp:4300
DynamicMatrix< IntegerPlus< Scalar >, IntegerProd< Scalar >, IntegerZero< Scalar >, IntegerOne< Scalar >, Scalar > DynamicIntMat
Alias for dynamic integer matrices.
Definition matrix.hpp:4252
StaticMatrix< IntegerPlus< Scalar >, IntegerProd< Scalar >, IntegerZero< Scalar >, IntegerOne< Scalar >, R, C, Scalar > StaticIntMat
Alias for static integer matrices.
Definition matrix.hpp:4275
enable_if_is_same< Return, Blocks > make(Container const &cont)
Check the arguments, construct a Blocks object, and check it.
Definition bipart.hpp:856
static constexpr bool IsMaxPlusMat
Helper variable template.
Definition matrix.hpp:4626
constexpr bool IsStaticMatrix
Helper variable template.
Definition matrix.hpp:3649
constexpr bool IsDynamicMatrix
Helper variable template.
Definition matrix.hpp:3662
static constexpr bool IsIntMat
Helper variable template.
Definition matrix.hpp:4325
auto operator+(typename Mat::scalar_type a, Mat const &x) -> std::enable_if_t< IsMatrix< Mat >, Mat >
Add a scalar to a matrix.
Definition matrix.hpp:7946
static constexpr bool IsMatWithSemiring
Helper variable template.
Definition matrix.hpp:3676
static constexpr bool IsMinPlusMat
Helper variable template.
Definition matrix.hpp:4934
constexpr bool IsMatrix
Helper variable template.
Definition matrix.hpp:168
StaticMatrix< MaxPlusPlus< Scalar >, MaxPlusProd< Scalar >, MaxPlusZero< Scalar >, IntegerZero< Scalar >, R, C, Scalar > StaticMaxPlusMat
Alias for static max-plus matrices.
Definition matrix.hpp:4575
DynamicMatrix< MaxPlusPlus< Scalar >, MaxPlusProd< Scalar >, MaxPlusZero< Scalar >, IntegerZero< Scalar >, Scalar > DynamicMaxPlusMat
Alias for dynamic max-plus matrices.
Definition matrix.hpp:4556
std::conditional_t< R==0||C==0, DynamicMaxPlusMat< Scalar >, StaticMaxPlusMat< R, C, Scalar > > MaxPlusMat
Alias template for max-plus matrices.
Definition matrix.hpp:4599
DynamicMatrix< MaxPlusPlus< Scalar >, MaxPlusTruncProd< T, Scalar >, MaxPlusZero< Scalar >, IntegerZero< Scalar >, Scalar > DynamicMaxPlusTruncMat
Alias for dynamic truncated max-plus matrices.
Definition matrix.hpp:5303
std::conditional_t< R==0||C==0, std::conditional_t< T==0, DynamicMatrix< MaxPlusTruncSemiring< Scalar >, Scalar >, DynamicMaxPlusTruncMat< T, Scalar > >, StaticMaxPlusTruncMat< T, R, C, Scalar > > MaxPlusTruncMat
Alias template for truncated max-plus matrices.
Definition matrix.hpp:5349
StaticMatrix< MaxPlusPlus< Scalar >, MaxPlusTruncProd< T, Scalar >, MaxPlusZero< Scalar >, IntegerZero< Scalar >, R, C, Scalar > StaticMaxPlusTruncMat
Alias for static truncated max-plus matrices.
Definition matrix.hpp:5323
static constexpr bool IsMaxPlusTruncMat
Helper to check if a type is MaxPlusTruncMat.
Definition matrix.hpp:5392
DynamicMatrix< MinPlusPlus< Scalar >, MinPlusProd< Scalar >, MinPlusZero< Scalar >, IntegerZero< Scalar >, Scalar > DynamicMinPlusMat
Alias for dynamic min-plus matrices.
Definition matrix.hpp:4864
StaticMatrix< MinPlusPlus< Scalar >, MinPlusProd< Scalar >, MinPlusZero< Scalar >, IntegerZero< Scalar >, R, C, Scalar > StaticMinPlusMat
Alias for static min-plus matrices.
Definition matrix.hpp:4883
std::conditional_t< R==0||C==0, DynamicMinPlusMat< Scalar >, StaticMinPlusMat< R, C, Scalar > > MinPlusMat
Alias template for min-plus matrices.
Definition matrix.hpp:4907
DynamicMatrix< MinPlusPlus< Scalar >, MinPlusTruncProd< T, Scalar >, MinPlusZero< Scalar >, IntegerZero< Scalar >, Scalar > DynamicMinPlusTruncMat
Alias for dynamic truncated min-plus matrices.
Definition matrix.hpp:5779
StaticMatrix< MinPlusPlus< Scalar >, MinPlusTruncProd< T, Scalar >, MinPlusZero< Scalar >, IntegerZero< Scalar >, R, C, Scalar > StaticMinPlusTruncMat
Alias for static truncated min-plus matrices.
Definition matrix.hpp:5799
static constexpr bool IsMinPlusTruncMat
Helper to check if a type is MinPlusTruncMat.
Definition matrix.hpp:5869
std::conditional_t< R==0||C==0, std::conditional_t< T==0, DynamicMatrix< MinPlusTruncSemiring< Scalar >, Scalar >, DynamicMinPlusTruncMat< T, Scalar > >, StaticMinPlusTruncMat< T, R, C, Scalar > > MinPlusTruncMat
Alias template for truncated min-plus matrices.
Definition matrix.hpp:5826
DynamicMatrix< NTPSemiring< Scalar >, Scalar > DynamicNTPMatWithSemiring
Alias for ntp matrices with dynamic threshold and period.
Definition matrix.hpp:6333
DynamicMatrix< NTPPlus< T, P, Scalar >, NTPProd< T, P, Scalar >, IntegerZero< Scalar >, IntegerOne< Scalar >, Scalar > DynamicNTPMatWithoutSemiring
Alias for ntp matrices with static threshold and period.
Definition matrix.hpp:6349
static constexpr bool IsNTPMat
Helper to check if a type is NTPMat.
Definition matrix.hpp:6453
std::conditional_t< R==0||C==0, std::conditional_t< T==0 &&P==0, DynamicNTPMatWithSemiring< Scalar >, DynamicNTPMatWithoutSemiring< T, P, Scalar > >, StaticNTPMat< T, P, R, C, Scalar > > NTPMat
Alias template for ntp matrices.
Definition matrix.hpp:6410
StaticMatrix< NTPPlus< T, P, Scalar >, NTPProd< T, P, Scalar >, IntegerZero< Scalar >, IntegerOne< Scalar >, R, C, Scalar > StaticNTPMat
Alias for ntp matrices with static threshold and period, and dimensions.
Definition matrix.hpp:6375
std::conditional_t< R==0||C==0, DynamicProjMaxPlusMat< Scalar >, StaticProjMaxPlusMat< R, C, Scalar > > ProjMaxPlusMat
Alias template for projective max-plus matrices.
Definition matrix.hpp:7037
detail::ProjMaxPlusMat< DynamicMaxPlusMat< Scalar > > DynamicProjMaxPlusMat
Alias for dynamic projective max-plus matrices with run-time dimensions.
Definition matrix.hpp:7020
static constexpr bool IsProjMaxPlusMat
Helper to check if a type is ProjMaxPlusMat.
Definition matrix.hpp:7067
detail::ProjMaxPlusMat< StaticMaxPlusMat< R, C, Scalar > > StaticProjMaxPlusMat
Alias for static projective max-plus matrices with compile-time arithmetic and dimensions.
Definition matrix.hpp:7006
T inner_product(T... args)
T insert(T... args)
T lexicographical_compare(T... args)
T make_pair(T... args)
T max_element(T... args)
T max(T... args)
T min(T... args)
T move(T... args)
Bipartition one(Bipartition const &f)
Return the identity bipartition with the same degree as the given bipartition.
constexpr BMat8 transpose(BMat8 const &x) noexcept
Returns the transpose of a BMat8.
Definition bmat8.hpp:704
Namespace for helper functions for matrices.
Definition matrix.hpp:170
void bitset_row_basis(Container &&rows, std::decay_t< Container > &result)
Appends a basis for the space spanned by some bitsets to a container.
Definition matrix.hpp:7465
void bitset_rows(Container &&views, detail::StaticVector1< BitSet< C >, R > &result)
Converts a container of row views of a boolean matrix to bit sets, and append them to another contain...
Definition matrix.hpp:7293
constexpr Scalar period(StaticNTPMat< T, P, R, C, Scalar > const &) noexcept
Returns the period of a static ntp matrix.
Definition matrix.hpp:6489
auto throw_if_bad_coords(Mat const &x, size_t r, size_t c) -> std::enable_if_t< IsMatrix< Mat > >
Throws the arguments do not index an entry of a matrix.
Definition matrix.hpp:235
constexpr auto threshold(Mat const &) noexcept -> std::enable_if_t<!detail::IsTruncMat< Mat >, typename Mat::scalar_type >
Returns the threshold of a matrix.
Definition matrix.hpp:3737
size_t row_space_size(Mat const &x)
Returns the size of the row space of a boolean matrix.
Definition matrix.hpp:7901
std::vector< typename Mat::RowView > rows(Mat const &x)
Returns a std::vector of row views into the rows of a dynamic matrix.
Definition matrix.hpp:7222
auto throw_if_bad_dim(Mat const &x, Mat const &y) -> std::enable_if_t< IsMatrix< Mat > >
Throws if two matrices do not have the same dimensions.
Definition matrix.hpp:206
Mat pow(Mat const &x, typename Mat::scalar_type e)
Returns a power of a matrix.
Definition matrix.hpp:7157
auto throw_if_not_square(Mat const &x) -> std::enable_if_t< IsMatrix< Mat > >
Throws if a matrix is not square.
Definition matrix.hpp:184
std::enable_if_t< IsMaxPlusTruncMat< Mat > > row_basis(Container &&views, std::decay_t< Container > &result)
Appends a basis for a space spanned by row views or bit sets to a container.
Definition matrix.hpp:7637
Namespace for everything in the libsemigroups library.
Definition action.hpp:44
T push_back(T... args)
T sort(T... args)
Function object for returning the multiplicative identity.
Definition matrix.hpp:3887
constexpr bool operator()() const noexcept
Call operator returning the multiplication identity true of the boolean semiring.
Definition matrix.hpp:3898
Function object for addition in the boolean semiring.
Definition matrix.hpp:3833
constexpr bool operator()(bool x, bool y) const noexcept
Call operator for addition.
Definition matrix.hpp:3846
Function object for multiplication in the boolean semiring.
Definition matrix.hpp:3860
constexpr bool operator()(bool x, bool y) const noexcept
Call operator for multiplication.
Definition matrix.hpp:3873
Function object for returning the additive identity.
Definition matrix.hpp:3912
constexpr bool operator()() const noexcept
Call operator returning the additive identity false of the boolean semiring.
Definition matrix.hpp:3923
constexpr size_t operator()(Mat const &x) const noexcept
Call operator.
Definition matrix.hpp:8443
Adapter for the complexity of multiplication.
Definition adapters.hpp:128
constexpr size_t operator()(Mat const &x) const noexcept
Call operator.
Definition matrix.hpp:8472
Adapter for the degree of an element.
Definition adapters.hpp:166
constexpr size_t operator()(Mat const &x) const
Call operator.
Definition matrix.hpp:8501
Adapter for hashing.
Definition adapters.hpp:453
constexpr void operator()(Mat &, size_t) const noexcept
Call operator.
Definition matrix.hpp:8526
Adapter for increasing the degree of an element.
Definition adapters.hpp:206
Function object for returning the multiplicative identity.
Definition matrix.hpp:4226
constexpr Scalar operator()() const noexcept
Call operator returning the integer 1.
Definition matrix.hpp:4236
Function object for addition in the ring of integers.
Definition matrix.hpp:4142
constexpr Scalar operator()(Scalar x, Scalar y) const noexcept
Call operator for addition.
Definition matrix.hpp:4155
Function object for multiplication in the ring of integers.
Definition matrix.hpp:4173
constexpr Scalar operator()(Scalar x, Scalar y) const noexcept
Call operator for multiplication.
Definition matrix.hpp:4186
Function object for returning the additive identity.
Definition matrix.hpp:4201
constexpr Scalar operator()() const noexcept
Call operator returning the integer 0.
Definition matrix.hpp:4211
Function object for addition in the max-plus semiring.
Definition matrix.hpp:4444
Scalar operator()(Scalar x, Scalar y) const noexcept
Call operator for addition.
Definition matrix.hpp:4459
Function object for multiplication in the max-plus semiring.
Definition matrix.hpp:4491
Scalar operator()(Scalar x, Scalar y) const noexcept
Call operator for multiplication.
Definition matrix.hpp:4506
Function object for multiplication in truncated max-plus semirings.
Definition matrix.hpp:5078
Scalar operator()(Scalar x, Scalar y) const noexcept
Call operator for multiplication.
Definition matrix.hpp:5093
Function object for returning the additive identity of the max-plus semiring.
Definition matrix.hpp:4528
constexpr Scalar operator()() const noexcept
Call operator for additive identity.
Definition matrix.hpp:4540
Function object for addition in the min-plus semiring.
Definition matrix.hpp:4752
Scalar operator()(Scalar x, Scalar y) const noexcept
Call operator for addition.
Definition matrix.hpp:4767
Function object for multiplication in the min-plus semiring.
Definition matrix.hpp:4799
Scalar operator()(Scalar x, Scalar y) const noexcept
Call operator for multiplication.
Definition matrix.hpp:4814
Function object for multiplication in min-plus truncated semirings.
Definition matrix.hpp:5558
Scalar operator()(Scalar x, Scalar y) const noexcept
Call operator for multiplication.
Definition matrix.hpp:5571
Function object for returning the additive identity of the min-plus semiring.
Definition matrix.hpp:4836
constexpr Scalar operator()() const noexcept
Call operator for additive identity.
Definition matrix.hpp:4848
Function object for addition in ntp semirings.
Definition matrix.hpp:6065
constexpr Scalar operator()(Scalar x, Scalar y) const noexcept
Call operator for addition.
Definition matrix.hpp:6077
Function object for multiplication in an ntp semirings.
Definition matrix.hpp:6106
constexpr Scalar operator()(Scalar x, Scalar y) const noexcept
Call operator for multiplication.
Definition matrix.hpp:6120
Mat operator()(Mat const &x) const
Call operator.
Definition matrix.hpp:8557
Adapter for the identity element of the given type.
Definition adapters.hpp:253
void operator()(Mat &xy, Mat const &x, Mat const &y, size_t=0) const
Call operator.
Definition matrix.hpp:8593
Adapter for the product of two elements.
Definition adapters.hpp:291
T swap(T... args)
T tie(T... args)
T unique(T... args)