libsemigroups  v3.0.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
1326 // clang-format on
1339 template <typename U>
1340 bool operator<(U const& that) const;
1341
1363 template <typename U>
1364 bool operator<(U const& that) const;
1365
1385 Row operator+(StaticRowView const& that);
1386
1405 void operator+=(StaticRowView const& that);
1406
1420 void operator+=(scalar_type a);
1421
1439 Row operator*(scalar_type a) const;
1440
1454 void operator*=(scalar_type a);
1455#endif
1456
1457 private:
1458 static constexpr size_t length_impl() noexcept {
1459 return C;
1460 }
1461 };
1462
1464 // DynamicRowViews - static arithmetic
1466
1467 template <typename... Args>
1468 class DynamicRowView;
1469
1507 template <typename PlusOp,
1508 typename ProdOp,
1509 typename ZeroOp,
1510 typename OneOp,
1511 typename Scalar>
1512 class DynamicRowView<PlusOp, ProdOp, ZeroOp, OneOp, Scalar>
1513 : public detail::RowViewCommon<
1514 DynamicMatrix<PlusOp, ProdOp, ZeroOp, OneOp, Scalar>,
1515 DynamicRowView<PlusOp, ProdOp, ZeroOp, OneOp, Scalar>>,
1516 public detail::
1517 MatrixStaticArithmetic<PlusOp, ProdOp, ZeroOp, OneOp, Scalar> {
1518 private:
1519 using DynamicMatrix_ = DynamicMatrix<PlusOp, ProdOp, ZeroOp, OneOp, Scalar>;
1520 using RowViewCommon = detail::RowViewCommon<
1521 DynamicMatrix_,
1522 DynamicRowView<PlusOp, ProdOp, ZeroOp, OneOp, Scalar>>;
1523 friend RowViewCommon;
1524
1525 public:
1527 using const_iterator = typename RowViewCommon::const_iterator;
1528
1530 using iterator = typename RowViewCommon::iterator;
1531
1533 using scalar_type = Scalar;
1534
1536 using scalar_reference = typename RowViewCommon::scalar_reference;
1537
1539 // clang-format off
1540 // NOLINTNEXTLINE(whitespace/line_length)
1541 using scalar_const_reference = typename RowViewCommon::scalar_const_reference;
1542 // clang-format on
1543
1545 using matrix_type = typename RowViewCommon::matrix_type;
1546
1548 using Row = typename matrix_type::Row;
1549
1551 DynamicRowView() = default;
1552
1554 DynamicRowView(DynamicRowView const&) = default;
1555
1557 DynamicRowView(DynamicRowView&&) = default;
1558
1560 DynamicRowView& operator=(DynamicRowView const&) = default;
1561
1563 DynamicRowView& operator=(DynamicRowView&&) = default;
1564
1566 explicit DynamicRowView(Row const& r)
1567 : RowViewCommon(r), _length(r.number_of_cols()) {}
1568
1569#ifndef LIBSEMIGROUPS_PARSED_BY_DOXYGEN
1570 using RowViewCommon::RowViewCommon;
1571
1572 // TODO(2) This constructor should be private
1573 DynamicRowView(DynamicMatrix_ const*, iterator const& it, size_t N)
1574 : RowViewCommon(it), _length(N) {}
1575
1576 using RowViewCommon::size;
1577#else
1579 size_t size() const noexcept;
1580
1582 iterator begin() noexcept;
1583
1585 iterator end();
1586
1588 const_iterator cbegin() const noexcept;
1589
1591 iterator cend();
1592
1594 scalar_reference operator()(size_t i);
1595
1597 scalar_const_reference operator()(size_t i) const;
1598
1600 scalar_reference operator[](size_t i);
1601
1603 scalar_const_reference operator[](size_t i) const;
1604
1606 template <typename U>
1607 bool operator==(U const& that) const;
1608
1610 template <typename U>
1611 bool operator!=(U const& that) const;
1612
1614 template <typename U>
1615 bool operator<(U const& that) const;
1616
1618 template <typename U>
1619 bool operator<(U const& that) const;
1620
1622 Row operator+(DynamicRowView const& that);
1623
1625 void operator+=(DynamicRowView const& that);
1626
1628 void operator+=(scalar_type a);
1629
1631 Row operator*(scalar_type a) const;
1632
1634 void operator*=(scalar_type a);
1635#endif // LIBSEMIGROUPS_PARSED_BY_DOXYGEN
1636
1637 private:
1638 size_t length_impl() const noexcept {
1639 return _length;
1640 }
1641 size_t _length;
1642 };
1643
1645 // DynamicRowViews - dynamic arithmetic
1647
1668 template <typename Semiring, typename Scalar>
1669 class DynamicRowView<Semiring, Scalar>
1670 : public detail::RowViewCommon<DynamicMatrix<Semiring, Scalar>,
1671 DynamicRowView<Semiring, Scalar>> {
1672 private:
1673 using DynamicMatrix_ = DynamicMatrix<Semiring, Scalar>;
1674 friend DynamicMatrix_;
1675 using RowViewCommon
1676 = detail::RowViewCommon<DynamicMatrix_,
1677 DynamicRowView<Semiring, Scalar>>;
1678 friend RowViewCommon;
1679
1680 public:
1682 using const_iterator = typename RowViewCommon::const_iterator;
1683
1685 using iterator = typename RowViewCommon::iterator;
1686
1688 using scalar_type = Scalar;
1689
1691 using scalar_reference = typename RowViewCommon::scalar_reference;
1692
1694 // clang-format off
1695 // NOLINTNEXTLINE(whitespace/line_length)
1696 using scalar_const_reference = typename RowViewCommon::scalar_const_reference;
1697 // clang-format on
1698
1700 using matrix_type = typename RowViewCommon::matrix_type;
1701
1703 using Row = typename matrix_type::Row;
1704
1706 DynamicRowView() = default;
1707
1709 DynamicRowView(DynamicRowView const&) = default;
1710
1712 DynamicRowView(DynamicRowView&&) = default;
1713
1715 DynamicRowView& operator=(DynamicRowView const&) = default;
1716
1718 DynamicRowView& operator=(DynamicRowView&&) = default;
1719
1721 explicit DynamicRowView(Row const& r) : RowViewCommon(r), _matrix(&r) {}
1722
1723#ifndef LIBSEMIGROUPS_PARSED_BY_DOXYGEN
1724 using RowViewCommon::RowViewCommon;
1725
1726 // TODO(2) This constructor should be private
1727 DynamicRowView(DynamicMatrix_ const* mat, iterator const& it, size_t)
1728 : RowViewCommon(it), _matrix(mat) {}
1729
1730 using RowViewCommon::size;
1731#else
1733 size_t size() const noexcept;
1734
1736 iterator begin() noexcept;
1737
1739 iterator end();
1740
1742 const_iterator cbegin() const noexcept;
1743
1745 iterator cend();
1746
1748 scalar_reference operator()(size_t i);
1749
1751 scalar_const_reference operator()(size_t i) const;
1752
1754 scalar_reference operator[](size_t i);
1755
1757 scalar_const_reference operator[](size_t i) const;
1758
1760 template <typename U>
1761 bool operator==(U const& that) const;
1762
1764 template <typename U>
1765 bool operator!=(U const& that) const;
1766
1768 template <typename U>
1769 bool operator<(U const& that) const;
1770
1772 template <typename U>
1773 bool operator<(U const& that) const;
1774
1776 Row operator+(DynamicRowView const& that);
1777
1779 void operator+=(DynamicRowView const& that);
1780
1782 void operator+=(scalar_type a);
1783
1785 Row operator*(scalar_type a) const;
1786
1788 void operator*=(scalar_type a);
1789#endif
1790
1791 private:
1792 size_t length_impl() const noexcept {
1793 return _matrix->number_of_cols();
1794 }
1795
1796 scalar_type plus_no_checks_impl(scalar_type x,
1797 scalar_type y) const noexcept {
1798 return _matrix->plus_no_checks_impl(x, y);
1799 }
1800
1801 scalar_type product_no_checks_impl(scalar_type x,
1802 scalar_type y) const noexcept {
1803 return _matrix->product_no_checks_impl(x, y);
1804 }
1805
1806 DynamicMatrix_ const* _matrix;
1807 };
1808
1810 // StaticMatrix with compile time semiring arithmetic
1812
1846 template <typename PlusOp,
1847 typename ProdOp,
1848 typename ZeroOp,
1849 typename OneOp,
1850 size_t R,
1851 size_t C,
1852 typename Scalar>
1854 : public detail::
1855 MatrixStaticArithmetic<PlusOp, ProdOp, ZeroOp, OneOp, Scalar>,
1856 public detail::MatrixCommon<
1857 std::array<Scalar, R * C>,
1858 StaticMatrix<PlusOp, ProdOp, ZeroOp, OneOp, R, C, Scalar>,
1859 StaticRowView<PlusOp, ProdOp, ZeroOp, OneOp, C, Scalar>> {
1861 // StaticMatrix - Aliases - private
1863
1864 using MatrixCommon = ::libsemigroups::detail::MatrixCommon<
1868 friend MatrixCommon;
1869
1870 public:
1872 // StaticMatrix - Aliases - public
1874
1876 using scalar_type = typename MatrixCommon::scalar_type;
1877
1879 using scalar_reference = typename MatrixCommon::scalar_reference;
1880
1882 // clang-format off
1883 // NOLINTNEXTLINE(whitespace/line_length)
1884 using scalar_const_reference = typename MatrixCommon::scalar_const_reference;
1885 // clang-format on
1886
1889
1892
1894 using Plus = PlusOp;
1895
1897 using Prod = ProdOp;
1898
1900 using Zero = ZeroOp;
1901
1903 using One = OneOp;
1904
1906 using iterator = typename MatrixCommon::iterator;
1907
1909 using const_iterator = typename MatrixCommon::const_iterator;
1910
1911 static constexpr size_t nr_rows = R;
1912 static constexpr size_t nr_cols = C;
1913
1915 // StaticMatrix - Constructors + destructor - public
1917
1935 : MatrixCommon(c) {
1936 static_assert(R == 1,
1937 "cannot construct Matrix from the given initializer list, "
1938 "incompatible dimensions");
1939 LIBSEMIGROUPS_ASSERT(c.size() == C);
1940 }
1941
1962 : MatrixCommon(m) {}
1963
1977 : MatrixCommon(m) {}
1978
1996 explicit StaticMatrix(RowView const& rv) : MatrixCommon(rv) {
1997 static_assert(
1998 R == 1,
1999 "cannot construct Matrix with more than one row from RowView!");
2000 }
2001
2005 StaticMatrix() = default;
2006
2010 StaticMatrix(StaticMatrix const&) = default;
2011
2016
2021
2026
2027#ifndef LIBSEMIGROUPS_PARSED_BY_DOXYGEN
2028 // For uniformity of interface, the args do nothing
2029 StaticMatrix(size_t r, size_t c) : StaticMatrix() {
2030 (void) r;
2031 (void) c;
2032 LIBSEMIGROUPS_ASSERT(r == number_of_rows());
2033 LIBSEMIGROUPS_ASSERT(c == number_of_cols());
2034 }
2035
2036 // For uniformity of interface, the first arg does nothing
2037 StaticMatrix(void const* ptr, std::initializer_list<scalar_type> const& c)
2038 : StaticMatrix(c) {
2039 (void) ptr;
2040 LIBSEMIGROUPS_ASSERT(ptr == nullptr);
2041 }
2042
2043 // For uniformity of interface, the first arg does nothing
2045 void const* ptr,
2047 : StaticMatrix(m) {
2048 (void) ptr;
2049 LIBSEMIGROUPS_ASSERT(ptr == nullptr);
2050 }
2051
2052 // For uniformity of interface, the first arg does nothing
2053 explicit StaticMatrix(void const* ptr, RowView const& rv)
2054 : StaticMatrix(rv) {
2055 (void) ptr;
2056 LIBSEMIGROUPS_ASSERT(ptr == nullptr);
2057 }
2058
2059 // For uniformity of interface, no arg used for anything
2060 StaticMatrix(void const* ptr, size_t r, size_t c) : StaticMatrix(r, c) {
2061 (void) ptr;
2062 LIBSEMIGROUPS_ASSERT(ptr == nullptr);
2063 }
2064#endif
2065
2066 ~StaticMatrix() = default;
2067
2068#ifndef LIBSEMIGROUPS_PARSED_BY_DOXYGEN
2069 static StaticMatrix one(size_t n) {
2070 // If specified the value of n must equal R or otherwise weirdness will
2071 // ensue...
2072 LIBSEMIGROUPS_ASSERT(n == 0 || n == R);
2073 (void) n;
2074#if defined(__APPLE__) && defined(__clang__) \
2075 && (__clang_major__ == 13 || __clang_major__ == 14)
2076 // With Apple clang version 13.1.6 (clang-1316.0.21.2.5) something goes
2077 // wrong and the value R is optimized away somehow, meaning that the
2078 // values on the diagonal aren't actually set. This only occurs when
2079 // libsemigroups is compiled with -O2 or higher.
2080 size_t volatile const m = R;
2081#else
2082 size_t const m = R;
2083#endif
2084 StaticMatrix x(m, m);
2085 std::fill(x.begin(), x.end(), ZeroOp()());
2086 for (size_t r = 0; r < m; ++r) {
2087 x(r, r) = OneOp()();
2088 }
2089 return x;
2090 }
2091
2092 static StaticMatrix one(void const* ptr, size_t n = 0) {
2093 (void) ptr;
2094 LIBSEMIGROUPS_ASSERT(ptr == nullptr);
2095 LIBSEMIGROUPS_ASSERT(n == 0 || n == R);
2096 return one(n);
2097 }
2098#endif // LIBSEMIGROUPS_PARSED_BY_DOXYGEN
2099
2101 // StaticMatrix - member function aliases - public
2103#ifdef LIBSEMIGROUPS_PARSED_BY_DOXYGEN
2122 scalar_reference operator()(size_t r, size_t c);
2123
2137 scalar_reference at(size_t r, size_t c);
2138
2157 scalar_const_reference operator()(size_t r, size_t c) const;
2158
2172 scalar_const_reference at(size_t r, size_t c) const;
2173
2190 iterator begin() noexcept;
2191
2207
2225 const_iterator cbegin() const noexcept;
2226
2246
2260 bool operator==(StaticMatrix const& that) const;
2261
2263 bool operator==(RowView const& that) const;
2264
2276 bool operator!=(StaticMatrix const& that) const;
2277
2279 bool operator!=(RowView const& that) const;
2280
2294 bool operator<(StaticMatrix const& that) const;
2295
2297 bool operator<(RowView const& that) const;
2298
2312 bool operator>(StaticMatrix const& that) const;
2313
2330
2342 size_t number_of_rows() const noexcept;
2343
2355 size_t number_of_cols() const noexcept;
2356
2374 StaticMatrix operator+(StaticMatrix const& that);
2375
2392 void operator+=(StaticMatrix const& that);
2393
2395 void operator+=(RowView const& that);
2396
2408 void operator+=(scalar_type a);
2409
2427 StaticMatrix operator*(StaticMatrix const& that);
2428
2440 void operator*=(scalar_type a);
2441
2459 StaticMatrix const& y);
2460
2476 RowView row_no_checks(size_t i) const;
2477
2488 RowView row(size_t i) const;
2489
2503 template <typename T>
2504 void rows(T& x) const;
2505
2518 void swap(StaticMatrix& that) noexcept;
2519
2533
2549
2561 static StaticMatrix one() const;
2562
2576 size_t hash_value() const;
2577
2592 template <typename U>
2593 bool operator<=(U const& that) const;
2594
2609 template <typename U>
2610 bool operator>=(U const& that) const;
2611
2625
2640
2651 scalar_type scalar_zero() const noexcept;
2652
2663 scalar_type scalar_one() const noexcept;
2664
2676 semiring_type const* semiring() const noexcept;
2677
2678#else
2679 using MatrixCommon::at;
2680 using MatrixCommon::begin;
2681 using MatrixCommon::cbegin;
2682 using MatrixCommon::cend;
2683 using MatrixCommon::coords;
2684 using MatrixCommon::end;
2685 using MatrixCommon::hash_value;
2686 using MatrixCommon::number_of_cols;
2687 using MatrixCommon::number_of_rows;
2688 using MatrixCommon::one;
2689 using MatrixCommon::operator!=;
2690 using MatrixCommon::operator();
2691 using MatrixCommon::operator*;
2692 using MatrixCommon::operator*=;
2693 using MatrixCommon::operator+;
2694 using MatrixCommon::operator+=;
2695 using MatrixCommon::operator<; // NOLINT(whitespace/operators)
2696 using MatrixCommon::operator<=;
2697 using MatrixCommon::operator==;
2698 using MatrixCommon::operator>; // NOLINT(whitespace/operators)
2699 using MatrixCommon::operator>=;
2700 using MatrixCommon::product_inplace_no_checks;
2701 using MatrixCommon::row;
2702 using MatrixCommon::row_no_checks;
2703 using MatrixCommon::rows;
2704 using MatrixCommon::scalar_one;
2705 using MatrixCommon::scalar_zero;
2706 using MatrixCommon::semiring;
2707 using MatrixCommon::swap;
2708 using MatrixCommon::transpose;
2709 using MatrixCommon::transpose_no_checks;
2710#endif // LIBSEMIGROUPS_PARSED_BY_DOXYGEN
2711
2712 private:
2714 // StaticMatrix - implementation of MatrixCommon requirements - private
2716
2717 static constexpr size_t number_of_rows_impl() noexcept {
2718 return R;
2719 }
2720 static constexpr size_t number_of_cols_impl() noexcept {
2721 return C;
2722 }
2723 };
2724
2726 // DynamicMatrix with compile time semiring arithmetic
2728
2762 template <typename PlusOp,
2763 typename ProdOp,
2764 typename ZeroOp,
2765 typename OneOp,
2766 typename Scalar>
2767 class DynamicMatrix<PlusOp, ProdOp, ZeroOp, OneOp, Scalar>
2768 : public detail::MatrixDynamicDim<Scalar>,
2769 public detail::MatrixCommon<
2770 std::vector<Scalar>,
2771 DynamicMatrix<PlusOp, ProdOp, ZeroOp, OneOp, Scalar>,
2772 DynamicRowView<PlusOp, ProdOp, ZeroOp, OneOp, Scalar>>,
2773 public detail::
2774 MatrixStaticArithmetic<PlusOp, ProdOp, ZeroOp, OneOp, Scalar> {
2775 using MatrixDynamicDim = ::libsemigroups::detail::MatrixDynamicDim<Scalar>;
2776 using MatrixCommon = ::libsemigroups::detail::MatrixCommon<
2779 DynamicRowView<PlusOp, ProdOp, ZeroOp, OneOp, Scalar>>;
2780 friend MatrixCommon;
2781
2782 public:
2784 using scalar_type = typename MatrixCommon::scalar_type;
2785
2787 using scalar_reference = typename MatrixCommon::scalar_reference;
2788
2790 // clang-format off
2791 // NOLINTNEXTLINE(whitespace/line_length)
2792 using scalar_const_reference = typename MatrixCommon::scalar_const_reference;
2793 // clang-format on
2794
2797
2799 using RowView = DynamicRowView<PlusOp, ProdOp, ZeroOp, OneOp, Scalar>;
2800
2802 using Plus = PlusOp;
2803
2805 using Prod = ProdOp;
2806
2808 using Zero = ZeroOp;
2809
2811 using One = OneOp;
2812
2818 using semiring_type = void;
2819
2823 DynamicMatrix() = default;
2824
2828 DynamicMatrix(DynamicMatrix const&) = default;
2829
2834
2839
2844
2862 DynamicMatrix(size_t r, size_t c) : MatrixDynamicDim(r, c), MatrixCommon() {
2863 resize(number_of_rows(), number_of_cols());
2864 }
2865
2885 : MatrixDynamicDim(1, c.size()), MatrixCommon(c) {}
2886
2908 : MatrixDynamicDim(m.size(), m.begin()->size()), MatrixCommon(m) {}
2909
2925 : MatrixDynamicDim(m.size(), m.begin()->size()), MatrixCommon(m) {}
2926
2938 explicit DynamicMatrix(RowView const& rv)
2939 : MatrixDynamicDim(1, rv.size()), MatrixCommon(rv) {}
2940
2941#ifndef LIBSEMIGROUPS_PARSED_BY_DOXYGEN
2942 // For uniformity of interface, the first arg does nothing
2943 DynamicMatrix(void const* ptr, size_t r, size_t c) : DynamicMatrix(r, c) {
2944 (void) ptr;
2945 LIBSEMIGROUPS_ASSERT(ptr == nullptr);
2946 }
2947
2948 // For uniformity of interface, the first arg does nothing
2949 DynamicMatrix(void const* ptr, std::initializer_list<scalar_type> const& c)
2950 : DynamicMatrix(c) {
2951 (void) ptr;
2952 LIBSEMIGROUPS_ASSERT(ptr == nullptr);
2953 }
2954
2955 // For uniformity of interface, the first arg does nothing
2956 DynamicMatrix(
2957 void const* ptr,
2958 std::initializer_list<std::initializer_list<scalar_type>> const& m)
2959 : DynamicMatrix(m) {
2960 (void) ptr;
2961 LIBSEMIGROUPS_ASSERT(ptr == nullptr);
2962 }
2963
2964 static DynamicMatrix one(void const* ptr, size_t n) {
2965 (void) ptr;
2966 LIBSEMIGROUPS_ASSERT(ptr == nullptr);
2967 return one(n);
2968 }
2969#endif // LIBSEMIGROUPS_PARSED_BY_DOXYGEN
2970
2971 ~DynamicMatrix() = default;
2972
2985 static DynamicMatrix one(size_t n) {
2986 DynamicMatrix x(n, n);
2987 std::fill(x.begin(), x.end(), ZeroOp()());
2988 for (size_t r = 0; r < n; ++r) {
2989 x(r, r) = OneOp()();
2990 }
2991 return x;
2992 }
2993
2994#ifdef LIBSEMIGROUPS_PARSED_BY_DOXYGEN
2996 scalar_reference at(size_t r, size_t c);
2997
2999 scalar_reference at(size_t r, size_t c) const;
3000
3002 iterator begin() noexcept;
3003
3005 const_iterator cbegin() noexcept;
3006
3008 const_iterator cend() noexcept;
3009
3011 std::pair<scalar_type, scalar_type> coords(const_iterator it) const;
3012
3014 iterator end() noexcept;
3015
3017 size_t hash_value() const;
3018
3020 size_t number_of_cols() const noexcept;
3021
3023 size_t number_of_rows() const noexcept;
3024
3026 bool operator!=(DynamicMatrix const& that) const;
3027
3029 bool operator!=(RowView const& that) const;
3030
3032 scalar_reference operator()(size_t r, size_t c);
3033
3035 scalar_const_reference operator()(size_t r, size_t c) const;
3048
3050 DynamicMatrix operator*(DynamicMatrix const& that);
3051
3053 void operator*=(scalar_type a);
3054
3056 DynamicMatrix operator+(DynamicMatrix const& that);
3057
3059 void operator+=(DynamicMatrix const& that);
3060
3062 void operator+=(RowView const& that);
3063
3075 void operator+=(scalar_type a);
3076
3078 bool operator<(DynamicMatrix const& that) const;
3079
3081 bool operator<(RowView const& that) const;
3082
3084 template <typename T>
3085 bool operator<=(T const& that) const;
3086
3088 bool operator==(DynamicMatrix const& that) const;
3089
3091 bool operator==(RowView const& that) const;
3092
3094 bool operator>(DynamicMatrix const& that) const;
3095
3097 template <typename T>
3098 bool operator>=(T const& that) const;
3099
3102 DynamicMatrix const& y);
3103
3105 RowView row(size_t i) const;
3106
3108 RowView row_no_checks(size_t i) const;
3109
3111 template <typename T>
3112 void rows(T& x) const;
3113
3115 scalar_type scalar_one() const noexcept;
3116
3118 scalar_type scalar_zero() const noexcept;
3119
3121 semiring_type const* semiring() const noexcept;
3122
3125
3128#else
3129 using MatrixCommon::at;
3130 using MatrixCommon::begin;
3131 using MatrixCommon::cbegin;
3132 using MatrixCommon::cend;
3133 using MatrixCommon::coords;
3134 using MatrixCommon::end;
3135 using MatrixCommon::hash_value;
3136 using MatrixCommon::number_of_cols;
3137 using MatrixCommon::number_of_rows;
3138 using MatrixCommon::one;
3139 using MatrixCommon::operator!=;
3140 using MatrixCommon::operator();
3141 using MatrixCommon::operator*;
3142 using MatrixCommon::operator*=;
3143 using MatrixCommon::operator+;
3144 using MatrixCommon::operator+=;
3145 using MatrixCommon::operator<; // NOLINT(whitespace/operators)
3146 using MatrixCommon::operator<=;
3147 using MatrixCommon::operator==;
3148 using MatrixCommon::operator>; // NOLINT(whitespace/operators)
3149 using MatrixCommon::operator>=;
3150 using MatrixCommon::product_inplace_no_checks;
3151 using MatrixCommon::row;
3152 using MatrixCommon::row_no_checks;
3153 using MatrixCommon::rows;
3154 using MatrixCommon::scalar_one;
3155 using MatrixCommon::scalar_zero;
3156 using MatrixCommon::semiring;
3157 // using MatrixCommon::swap; // Don't want this use the one below.
3158 using MatrixCommon::transpose;
3159 using MatrixCommon::transpose_no_checks;
3160#endif // LIBSEMIGROUPS_PARSED_BY_DOXYGEN
3161
3163 void swap(DynamicMatrix& that) noexcept {
3164 static_cast<MatrixDynamicDim&>(*this).swap(
3165 static_cast<MatrixDynamicDim&>(that));
3166 static_cast<MatrixCommon&>(*this).swap(static_cast<MatrixCommon&>(that));
3167 }
3168
3169 private:
3170 using MatrixCommon::resize;
3171 };
3172
3174 // DynamicMatrix with runtime semiring arithmetic
3176
3211 template <typename Semiring, typename Scalar>
3212 class DynamicMatrix<Semiring, Scalar>
3213 : public detail::MatrixDynamicDim<Scalar>,
3214 public detail::MatrixCommon<std::vector<Scalar>,
3215 DynamicMatrix<Semiring, Scalar>,
3216 DynamicRowView<Semiring, Scalar>,
3217 Semiring> {
3218 using MatrixCommon = detail::MatrixCommon<std::vector<Scalar>,
3220 DynamicRowView<Semiring, Scalar>,
3221 Semiring>;
3222 friend MatrixCommon;
3223 using MatrixDynamicDim = ::libsemigroups::detail::MatrixDynamicDim<Scalar>;
3224
3225 public:
3227 using scalar_type = typename MatrixCommon::scalar_type;
3228
3230 using scalar_reference = typename MatrixCommon::scalar_reference;
3231
3233 // clang-format off
3234 // NOLINTNEXTLINE(whitespace/line_length)
3235 using scalar_const_reference = typename MatrixCommon::scalar_const_reference;
3236 // clang-format on
3237
3240
3242 using RowView = DynamicRowView<Semiring, Scalar>;
3243
3244 friend RowView;
3245
3247 using semiring_type = Semiring;
3248
3254 DynamicMatrix() = delete;
3255
3257 DynamicMatrix(DynamicMatrix const&) = default;
3258
3261
3264
3267
3282 DynamicMatrix(Semiring const* sr, size_t r, size_t c)
3283 : MatrixDynamicDim(r, c), MatrixCommon(), _semiring(sr) {
3284 resize(number_of_rows(), number_of_cols());
3285 }
3286
3303 Semiring const* sr,
3305 : MatrixDynamicDim(rws.size(), rws.begin()->size()),
3306 MatrixCommon(rws),
3307 _semiring(sr) {}
3308
3324 explicit DynamicMatrix(Semiring const* sr,
3326 : MatrixDynamicDim(rws.size(), (rws.empty() ? 0 : rws.begin()->size())),
3327 MatrixCommon(rws),
3328 _semiring(sr) {}
3329
3343 explicit DynamicMatrix(Semiring const* sr,
3345 : MatrixDynamicDim(1, rw.size()), MatrixCommon(rw), _semiring(sr) {}
3346
3358 explicit DynamicMatrix(RowView const& rv)
3359 : MatrixDynamicDim(1, rv.size()),
3360 MatrixCommon(rv),
3361 _semiring(rv._matrix->semiring()) {}
3362
3377 // No static DynamicMatrix::one(size_t n) because we need a semiring!
3378 static DynamicMatrix one(Semiring const* semiring, size_t n) {
3379 DynamicMatrix x(semiring, n, n);
3380 std::fill(x.begin(), x.end(), x.scalar_zero());
3381 for (size_t r = 0; r < n; ++r) {
3382 x(r, r) = x.scalar_one();
3383 }
3384 return x;
3385 }
3386
3387 ~DynamicMatrix() = default;
3388
3389#ifdef LIBSEMIGROUPS_PARSED_BY_DOXYGEN
3391 scalar_reference at(size_t r, size_t c);
3392
3394 scalar_reference at(size_t r, size_t c) const;
3395
3397 iterator begin() noexcept;
3398
3400 const_iterator cbegin() noexcept;
3401
3403 const_iterator cend() noexcept;
3404
3406 std::pair<scalar_type, scalar_type> coords(const_iterator it) const;
3407
3409 iterator end() noexcept;
3410
3412 size_t hash_value() const;
3413
3415 size_t number_of_cols() const noexcept;
3416
3418 size_t number_of_rows() const noexcept;
3419
3421 bool operator!=(DynamicMatrix const& that) const;
3422
3424 bool operator!=(RowView const& that) const;
3425
3427 scalar_reference operator()(size_t r, size_t c);
3428
3430 scalar_const_reference operator()(size_t r, size_t c) const;
3443
3445 DynamicMatrix operator*(DynamicMatrix const& that);
3446
3448 void operator*=(scalar_type a);
3449
3451 DynamicMatrix operator+(DynamicMatrix const& that);
3452
3454 void operator+=(DynamicMatrix const& that);
3455
3457 void operator+=(RowView const& that);
3458
3470 void operator+=(scalar_type a);
3471
3473 bool operator<(DynamicMatrix const& that) const;
3474
3476 bool operator<(RowView const& that) const;
3477
3479 template <typename T>
3480 bool operator<=(T const& that) const;
3481
3483 bool operator==(DynamicMatrix const& that) const;
3484
3486 bool operator==(RowView const& that) const;
3487
3489 bool operator>(DynamicMatrix const& that) const;
3490
3492 template <typename T>
3493 bool operator>=(T const& that) const;
3494
3497 DynamicMatrix const& y);
3498
3500 RowView row(size_t i) const;
3501
3503 RowView row_no_checks(size_t i) const;
3504
3506 template <typename T>
3507 void rows(T& x) const;
3508
3510 scalar_type scalar_one() const noexcept;
3511
3513 scalar_type scalar_zero() const noexcept;
3514
3516 semiring_type const* semiring() const noexcept;
3517
3520
3523#else
3524 using MatrixCommon::at;
3525 using MatrixCommon::begin;
3526 using MatrixCommon::cbegin;
3527 using MatrixCommon::cend;
3528 using MatrixCommon::coords;
3529 using MatrixCommon::end;
3530 using MatrixCommon::hash_value;
3531 using MatrixCommon::number_of_cols;
3532 using MatrixCommon::number_of_rows;
3533 using MatrixCommon::one;
3534 using MatrixCommon::operator!=;
3535 using MatrixCommon::operator();
3536 using MatrixCommon::operator*;
3537 using MatrixCommon::operator*=;
3538 using MatrixCommon::operator+;
3539 using MatrixCommon::operator+=;
3540 using MatrixCommon::operator<; // NOLINT(whitespace/operators)
3541 using MatrixCommon::operator<=;
3542 using MatrixCommon::operator==;
3543 using MatrixCommon::operator>; // NOLINT(whitespace/operators)
3544 using MatrixCommon::operator>=;
3545 using MatrixCommon::product_inplace_no_checks;
3546 using MatrixCommon::row;
3547 using MatrixCommon::row_no_checks;
3548 using MatrixCommon::rows;
3549 using MatrixCommon::scalar_one;
3550 using MatrixCommon::scalar_zero;
3551 using MatrixCommon::semiring;
3552 // using MatrixCommon::swap; // Don't want this use the one below.
3553 using MatrixCommon::transpose;
3554 using MatrixCommon::transpose_no_checks;
3555#endif // LIBSEMIGROUPS_PARSED_BY_DOXYGEN
3556
3558 void swap(DynamicMatrix& that) noexcept {
3559 static_cast<MatrixDynamicDim&>(*this).swap(
3560 static_cast<MatrixDynamicDim&>(that));
3561 static_cast<MatrixCommon&>(*this).swap(static_cast<MatrixCommon&>(that));
3562 std::swap(_semiring, that._semiring);
3563 }
3564
3565 private:
3566 using MatrixCommon::resize;
3567
3568 scalar_type plus_no_checks_impl(scalar_type x,
3569 scalar_type y) const noexcept {
3570 return _semiring->plus_no_checks(x, y);
3571 }
3572
3573 scalar_type product_no_checks_impl(scalar_type x,
3574 scalar_type y) const noexcept {
3575 return _semiring->product_no_checks(x, y);
3576 }
3577
3578 scalar_type one_impl() const noexcept {
3579 return _semiring->scalar_one();
3580 }
3581
3582 scalar_type zero_impl() const noexcept {
3583 return _semiring->scalar_zero();
3584 }
3585
3586 Semiring const* semiring_impl() const noexcept {
3587 return _semiring;
3588 }
3589
3590 Semiring const* _semiring;
3591 };
3592
3594 // Helper structs to check if matrix is static, or has a pointer to a
3595 // semiring
3597
3598 namespace detail {
3599 template <typename T>
3600 struct IsStaticMatrixHelper : std::false_type {};
3601
3602 template <typename PlusOp,
3603 typename ProdOp,
3604 typename ZeroOp,
3605 typename OneOp,
3606 size_t R,
3607 size_t C,
3608 typename Scalar>
3609 struct IsStaticMatrixHelper<
3610 StaticMatrix<PlusOp, ProdOp, ZeroOp, OneOp, R, C, Scalar>>
3611 : std::true_type {};
3612
3613 template <typename T>
3614 struct IsMatWithSemiringHelper : std::false_type {};
3615
3616 template <typename Semiring, typename Scalar>
3617 struct IsMatWithSemiringHelper<DynamicMatrix<Semiring, Scalar>>
3618 : std::true_type {};
3619
3620 template <typename S, typename T = void>
3621 struct IsTruncMatHelper : std::false_type {};
3622
3623 } // namespace detail
3624
3635 template <typename T>
3636 constexpr bool IsStaticMatrix = detail::IsStaticMatrixHelper<T>::value;
3637
3648 template <typename T>
3650
3661 template <typename T>
3662 static constexpr bool IsMatWithSemiring
3663 = detail::IsMatWithSemiringHelper<T>::value;
3664
3665 namespace detail {
3666
3667 template <typename T>
3668 static constexpr bool IsTruncMat = IsTruncMatHelper<T>::value;
3669
3670 template <typename Mat>
3671 void throw_if_semiring_nullptr(Mat const& m) {
3672 if (IsMatWithSemiring<Mat> && m.semiring() == nullptr) {
3674 "the matrix's pointer to a semiring is nullptr!")
3675 }
3676 }
3677
3678 template <typename Mat, typename Container>
3679 auto throw_if_bad_dim(Container const& m)
3680 -> std::enable_if_t<IsStaticMatrix<Mat>> {
3681 // Only call this if you've already called throw_if_any_row_wrong_size
3682 uint64_t const R = m.size();
3683 uint64_t const C = m.begin()->size();
3684 if (R != Mat::nr_rows || C != Mat::nr_cols) {
3686 "invalid argument, cannot initialize an {}x{} matrix with compile "
3687 "time dimension, with an {}x{} container",
3688 Mat::nr_rows,
3689 Mat::nr_cols,
3690 R,
3691 C);
3692 }
3693 }
3694
3695 template <typename Mat, typename Container>
3696 auto throw_if_bad_dim(Container const&)
3697 -> std::enable_if_t<IsDynamicMatrix<Mat>> {}
3698 } // namespace detail
3699
3708 namespace matrix {
3709
3723 template <typename Mat>
3724 constexpr auto threshold(Mat const&) noexcept
3725 -> std::enable_if_t<!detail::IsTruncMat<Mat>,
3726 typename Mat::scalar_type> {
3727 return UNDEFINED;
3728 }
3729
3743 template <typename Mat>
3744 constexpr auto threshold(Mat const&) noexcept
3745 -> std::enable_if_t<detail::IsTruncMat<Mat> && !IsMatWithSemiring<Mat>,
3746 typename Mat::scalar_type> {
3747 return detail::IsTruncMatHelper<Mat>::threshold;
3748 }
3749
3765 template <typename Mat>
3766 auto threshold(Mat const& x) noexcept
3767 -> std::enable_if_t<detail::IsTruncMat<Mat> && IsMatWithSemiring<Mat>,
3768 typename Mat::scalar_type> {
3769 return x.semiring()->threshold();
3770 }
3771 } // namespace matrix
3772
3774 // Boolean matrices - compile time semiring arithmetic
3776
3810
3833 constexpr bool operator()(bool x, bool y) const noexcept {
3834 return x || y;
3835 }
3836 };
3837
3860 constexpr bool operator()(bool x, bool y) const noexcept {
3861 return x & y;
3862 }
3863 };
3864
3874 struct BooleanOne {
3885 constexpr bool operator()() const noexcept {
3886 return true;
3887 }
3888 };
3889
3910 constexpr bool operator()() const noexcept {
3911 return false;
3912 }
3913 };
3914
3923 // The use of `int` rather than `bool` as the scalar type for dynamic
3924 // boolean matrices is intentional, because the bit iterators implemented in
3925 // std::vector<bool> entail a significant performance penalty.
3927 = DynamicMatrix<BooleanPlus, BooleanProd, BooleanZero, BooleanOne, int>;
3928
3940 template <size_t R, size_t C>
3944 BooleanOne,
3945 R,
3946 C,
3947 int>;
3948
3962 // FLS + JDM considered adding BMat8 and decided it wasn't a good idea.
3963 template <size_t R = 0, size_t C = R>
3964 using BMat
3965 = std::conditional_t<R == 0 || C == 0, DynamicBMat, StaticBMat<R, C>>;
3966
3967 namespace detail {
3968 template <typename T>
3969 struct IsBMatHelper : std::false_type {};
3970
3971 template <size_t R, size_t C>
3972 struct IsBMatHelper<StaticBMat<R, C>> : std::true_type {};
3973
3974 template <>
3975 struct IsBMatHelper<DynamicBMat> : std::true_type {};
3976
3977 template <typename T>
3978 struct BitSetCapacity {
3979 static constexpr size_t value = BitSet<1>::max_size();
3980 };
3981
3982 template <size_t R, size_t C>
3983 struct BitSetCapacity<StaticBMat<R, C>> {
3984 static_assert(R == C, "the number of rows and columns must be equal");
3985 static constexpr size_t value = R;
3986 };
3987 } // namespace detail
3988
3999 template <typename T>
4000 static constexpr bool IsBMat = detail::IsBMatHelper<T>::value;
4001
4002 namespace detail {
4003 // This function is required for exceptions and to_human_readable_repr, so
4004 // that if we encounter an entry of a matrix (Scalar type), then it can be
4005 // printed correctly. If we just did fmt::format("{}", val) and val ==
4006 // POSITIVE_INFINITY, but the type of val is, say, size_t, then this
4007 // wouldn't use the formatter for PositiveInfinity.
4008 //
4009 // Also in fmt v11.1.4 the custom formatter for POSITIVE_INFINITY and
4010 // NEGATIVE_INFINITY stopped working (and I wasn't able to figure out why)
4011 template <typename Scalar>
4012 std::string entry_repr(Scalar a) {
4013 if constexpr (std::is_same_v<Scalar, NegativeInfinity>
4014 || std::is_signed_v<Scalar>) {
4015 if (a == NEGATIVE_INFINITY) {
4016 return u8"-\u221E";
4017 }
4018 }
4019 if (a == POSITIVE_INFINITY) {
4020 return u8"+\u221E";
4021 }
4022 return fmt::format("{}", a);
4023 }
4024 } // namespace detail
4025
4026 namespace matrix {
4027
4043 //! but a matrix shouldn't contain values except \c 0 and \c 1.
4044 template <typename Mat>
4045 std::enable_if_t<IsBMat<Mat>> throw_if_bad_entry(Mat const& m) {
4046 using scalar_type = typename Mat::scalar_type;
4047 auto it = std::find_if_not(
4048 m.cbegin(), m.cend(), [](scalar_type x) { return x == 0 || x == 1; });
4049 if (it != m.cend()) {
4050 auto [r, c] = m.coords(it);
4052 "invalid entry, expected 0 or 1 but found {} in entry ({}, {})",
4053 detail::entry_repr(*it),
4054 r,
4055 c);
4056 }
4057 }
4058
4076 template <typename Mat>
4077 std::enable_if_t<IsBMat<Mat>>
4078 throw_if_bad_entry(Mat const&, typename Mat::scalar_type val) {
4079 if (val != 0 && val != 1) {
4080 LIBSEMIGROUPS_EXCEPTION("invalid entry, expected 0 or 1 but found {}",
4081 detail::entry_repr(val));
4082 }
4083 }
4084 } // namespace matrix
4085
4087 // Integer matrices - compile time semiring arithmetic
4089
4117
4129 //! \tparam Scalar the type of the entries in the matrix.
4130 template <typename Scalar>
4131 struct IntegerPlus {
4142 //! \exceptions
4143 //! \noexcept
4144 constexpr Scalar operator()(Scalar x, Scalar y) const noexcept {
4145 return x + y;
4146 }
4147 };
4148
4160 //! \tparam Scalar the type of the entries in the matrix.
4161 template <typename Scalar>
4162 struct IntegerProd {
4173 //! \exceptions
4174 //! \noexcept
4175 constexpr Scalar operator()(Scalar x, Scalar y) const noexcept {
4176 return x * y;
4177 }
4178 };
4179
4188 //! the additive identity of the integer semiring.
4189 template <typename Scalar>
4190 struct IntegerZero {
4198 //! \exceptions
4199 //! \noexcept
4200 constexpr Scalar operator()() const noexcept {
4201 return 0;
4202 }
4203 };
4204
4213 //! the multiplicative identity of the integer semiring.
4214 template <typename Scalar>
4215 struct IntegerOne {
4223 //! \exceptions
4224 //! \noexcept
4225 constexpr Scalar operator()() const noexcept {
4226 return 1;
4227 }
4228 };
4229
4240 template <typename Scalar>
4241 using DynamicIntMat = DynamicMatrix<IntegerPlus<Scalar>,
4245 Scalar>;
4246
4263 template <size_t R, size_t C, typename Scalar>
4268 R,
4269 C,
4270 Scalar>;
4271
4288 template <size_t R = 0, size_t C = R, typename Scalar = int>
4289 using IntMat = std::conditional_t<R == 0 || C == 0,
4292 namespace detail {
4293 template <typename T>
4294 struct IsIntMatHelper : std::false_type {};
4295
4296 template <size_t R, size_t C, typename Scalar>
4297 struct IsIntMatHelper<StaticIntMat<R, C, Scalar>> : std::true_type {};
4298
4299 template <typename Scalar>
4300 struct IsIntMatHelper<DynamicIntMat<Scalar>> : std::true_type {};
4301 } // namespace detail
4302
4313 template <typename T>
4314 static constexpr bool IsIntMat = detail::IsIntMatHelper<T>::value;
4315
4316 namespace matrix {
4330 //! \param x the matrix to check.
4331 template <typename Mat>
4332 std::enable_if_t<IsIntMat<Mat>> throw_if_bad_entry(Mat const& x) {
4333 using scalar_type = typename Mat::scalar_type;
4334 auto it = std::find_if(x.cbegin(), x.cend(), [](scalar_type val) {
4335 return val == POSITIVE_INFINITY || val == NEGATIVE_INFINITY;
4336 });
4337 if (it != x.cend()) {
4338 auto [r, c] = x.coords(it);
4340 "invalid entry, expected entries to be integers, "
4341 "but found {} in entry ({}, {})",
4342 detail::entry_repr(*it),
4343 r,
4344 c);
4345 }
4346 }
4347
4364 template <typename Mat>
4365 std::enable_if_t<IsIntMat<Mat>>
4366 throw_if_bad_entry(Mat const&, typename Mat::scalar_type val) {
4367 if (val == POSITIVE_INFINITY || val == NEGATIVE_INFINITY) {
4369 "invalid entry, expected entries to be integers, "
4370 "but found {}",
4371 detail::entry_repr(val));
4372 }
4373 }
4374 } // namespace matrix
4375
4377 // Max-plus matrices
4407
4431 // Static arithmetic
4432 template <typename Scalar>
4433 struct MaxPlusPlus {
4434 static_assert(std::is_signed<Scalar>::value,
4435 "MaxPlus requires a signed integer type as parameter!");
4446 //! \exceptions
4447 //! \noexcept
4448 Scalar operator()(Scalar x, Scalar y) const noexcept {
4449 if (x == NEGATIVE_INFINITY) {
4450 return y;
4451 } else if (y == NEGATIVE_INFINITY) {
4452 return x;
4453 }
4454 return std::max(x, y);
4455 }
4456 };
4457
4478 //! integer type).
4479 template <typename Scalar>
4480 struct MaxPlusProd {
4481 static_assert(std::is_signed<Scalar>::value,
4482 "MaxPlus requires a signed integer type as parameter!");
4493 //! \exceptions
4494 //! \noexcept
4495 Scalar operator()(Scalar x, Scalar y) const noexcept {
4496 if (x == NEGATIVE_INFINITY || y == NEGATIVE_INFINITY) {
4497 return NEGATIVE_INFINITY;
4498 }
4499 return x + y;
4500 }
4501 };
4502
4515 //! integer type).
4516 template <typename Scalar>
4517 struct MaxPlusZero {
4518 static_assert(std::is_signed<Scalar>::value,
4519 "MaxPlus requires a signed integer type as parameter!");
4527 //! \exceptions
4528 //! \noexcept
4529 constexpr Scalar operator()() const noexcept {
4530 return NEGATIVE_INFINITY;
4531 }
4532 };
4533
4544 template <typename Scalar>
4545 using DynamicMaxPlusMat = DynamicMatrix<MaxPlusPlus<Scalar>,
4549 Scalar>;
4550
4563 template <size_t R, size_t C, typename Scalar>
4568 R,
4569 C,
4570 Scalar>;
4571
4587 template <size_t R = 0, size_t C = R, typename Scalar = int>
4588 using MaxPlusMat = std::conditional_t<R == 0 || C == 0,
4591
4592 namespace detail {
4593 template <typename T>
4594 struct IsMaxPlusMatHelper : std::false_type {};
4595
4596 template <size_t R, size_t C, typename Scalar>
4597 struct IsMaxPlusMatHelper<StaticMaxPlusMat<R, C, Scalar>> : std::true_type {
4598 };
4599
4600 template <typename Scalar>
4601 struct IsMaxPlusMatHelper<DynamicMaxPlusMat<Scalar>> : std::true_type {};
4602 } // namespace detail
4603
4614 template <typename T>
4615 static constexpr bool IsMaxPlusMat = detail::IsMaxPlusMatHelper<T>::value;
4616
4617 namespace matrix {
4632 //! \ref POSITIVE_INFINITY.
4633 template <typename Mat>
4634 auto throw_if_bad_entry(Mat const& x)
4635 -> std::enable_if_t<IsMaxPlusMat<Mat>> {
4636 using scalar_type = typename Mat::scalar_type;
4637 auto it = std::find_if(x.cbegin(), x.cend(), [](scalar_type val) {
4638 return val == POSITIVE_INFINITY;
4639 });
4640 if (it != x.cend()) {
4641 auto [r, c] = x.coords(it);
4643 "invalid entry, expected entries to be integers or {} (= {}), "
4644 "but found {} (= {}) in entry ({}, {})",
4645 entry_repr(NEGATIVE_INFINITY),
4646 static_cast<scalar_type>(NEGATIVE_INFINITY),
4647 entry_repr(POSITIVE_INFINITY),
4648 static_cast<scalar_type>(POSITIVE_INFINITY),
4649 r,
4650 c);
4651 }
4652 }
4653
4669 template <typename Mat>
4670 std::enable_if_t<IsMaxPlusMat<Mat>>
4671 throw_if_bad_entry(Mat const&, typename Mat::scalar_type val) {
4672 if (val == POSITIVE_INFINITY) {
4673 using scalar_type = typename Mat::scalar_type;
4674 LIBSEMIGROUPS_EXCEPTION("invalid entry, expected entries to be "
4675 "integers or {} (= {}) but found {} (= {})",
4676 entry_repr(NEGATIVE_INFINITY),
4677 static_cast<scalar_type>(NEGATIVE_INFINITY),
4678 entry_repr(POSITIVE_INFINITY),
4679 static_cast<scalar_type>(POSITIVE_INFINITY));
4680 }
4681 }
4682 } // namespace matrix
4683
4685 // Min-plus matrices
4687
4716
4739 // Static arithmetic
4740 template <typename Scalar>
4741 struct MinPlusPlus {
4742 static_assert(std::is_signed<Scalar>::value,
4743 "MinPlus requires a signed integer type as parameter!");
4754 //! \exceptions
4755 //! \noexcept
4756 Scalar operator()(Scalar x, Scalar y) const noexcept {
4757 if (x == POSITIVE_INFINITY) {
4758 return y;
4759 } else if (y == POSITIVE_INFINITY) {
4760 return x;
4761 }
4762 return std::min(x, y);
4763 }
4764 };
4765
4786 //! integer type).
4787 template <typename Scalar>
4788 struct MinPlusProd {
4789 static_assert(std::is_signed<Scalar>::value,
4790 "MinPlus requires a signed integer type as parameter!");
4801 //! \exceptions
4802 //! \noexcept
4803 Scalar operator()(Scalar x, Scalar y) const noexcept {
4804 if (x == POSITIVE_INFINITY || y == POSITIVE_INFINITY) {
4805 return POSITIVE_INFINITY;
4806 }
4807 return x + y;
4808 }
4809 };
4810
4823 //! integer type).
4824 template <typename Scalar>
4825 struct MinPlusZero {
4826 static_assert(std::is_signed<Scalar>::value,
4827 "MinPlus requires a signed integer type as parameter!");
4835 //! \exceptions
4836 //! \noexcept
4837 constexpr Scalar operator()() const noexcept {
4838 return POSITIVE_INFINITY;
4839 }
4840 };
4841
4852 template <typename Scalar>
4853 using DynamicMinPlusMat = DynamicMatrix<MinPlusPlus<Scalar>,
4857 Scalar>;
4858
4871 template <size_t R, size_t C, typename Scalar>
4876 R,
4877 C,
4878 Scalar>;
4895 template <size_t R = 0, size_t C = R, typename Scalar = int>
4896 using MinPlusMat = std::conditional_t<R == 0 || C == 0,
4899
4900 namespace detail {
4901 template <typename T>
4902 struct IsMinPlusMatHelper : std::false_type {};
4903
4904 template <size_t R, size_t C, typename Scalar>
4905 struct IsMinPlusMatHelper<StaticMinPlusMat<R, C, Scalar>> : std::true_type {
4906 };
4907
4908 template <typename Scalar>
4909 struct IsMinPlusMatHelper<DynamicMinPlusMat<Scalar>> : std::true_type {};
4910 } // namespace detail
4911
4922 template <typename T>
4923 static constexpr bool IsMinPlusMat = detail::IsMinPlusMatHelper<T>::value;
4924
4925 namespace matrix {
4940 //! \ref NEGATIVE_INFINITY.
4941 template <typename Mat>
4942 std::enable_if_t<IsMinPlusMat<Mat>> throw_if_bad_entry(Mat const& x) {
4943 using scalar_type = typename Mat::scalar_type;
4944 auto it = std::find_if(x.cbegin(), x.cend(), [](scalar_type val) {
4945 return val == NEGATIVE_INFINITY;
4946 });
4947 if (it != x.cend()) {
4948 auto [r, c] = x.coords(it);
4950 "invalid entry, expected entries to be integers or {} (= {}), "
4951 "but found {} (= {}) in entry ({}, {})",
4952 entry_repr(POSITIVE_INFINITY),
4953 static_cast<scalar_type>(POSITIVE_INFINITY),
4954 entry_repr(NEGATIVE_INFINITY),
4955 static_cast<scalar_type>(NEGATIVE_INFINITY),
4956 r,
4957 c);
4958 }
4959 }
4960
4976 template <typename Mat>
4977 std::enable_if_t<IsMinPlusMat<Mat>>
4978 throw_if_bad_entry(Mat const&, typename Mat::scalar_type val) {
4979 if (val == NEGATIVE_INFINITY) {
4980 using scalar_type = typename Mat::scalar_type;
4981 LIBSEMIGROUPS_EXCEPTION("invalid entry, expected entries to be "
4982 "integers or {} (= {}) but found {} (= {})",
4983 entry_repr(POSITIVE_INFINITY),
4984 static_cast<scalar_type>(POSITIVE_INFINITY),
4985 entry_repr(NEGATIVE_INFINITY),
4986 static_cast<scalar_type>(NEGATIVE_INFINITY));
4987 }
4988 }
4989 } // namespace matrix
4990
4992 // Max-plus matrices with threshold
4994
5040
5065 //! integer type).
5066 template <size_t T, typename Scalar>
5067 struct MaxPlusTruncProd {
5068 static_assert(std::is_signed<Scalar>::value,
5069 "MaxPlus requires a signed integer type as parameter!");
5080 //! \exceptions
5081 //! \noexcept
5082 Scalar operator()(Scalar x, Scalar y) const noexcept {
5083 LIBSEMIGROUPS_ASSERT((x >= 0 && x <= static_cast<Scalar>(T))
5084 || x == NEGATIVE_INFINITY);
5085 LIBSEMIGROUPS_ASSERT((y >= 0 && y <= static_cast<Scalar>(T))
5086 || y == NEGATIVE_INFINITY);
5087 if (x == NEGATIVE_INFINITY || y == NEGATIVE_INFINITY) {
5088 return NEGATIVE_INFINITY;
5089 }
5090 return std::min(x + y, static_cast<Scalar>(T));
5091 }
5092 };
5093
5107 //! signed integer type (defaults to \c int).
5108 template <typename Scalar = int>
5109 class MaxPlusTruncSemiring {
5110 static_assert(std::is_signed<Scalar>::value,
5111 "MaxPlus requires a signed integer type as parameter!");
5112
5113 public:
5117 MaxPlusTruncSemiring() = delete;
5118
5122 MaxPlusTruncSemiring(MaxPlusTruncSemiring const&) noexcept = default;
5123
5127 MaxPlusTruncSemiring(MaxPlusTruncSemiring&&) noexcept = default;
5128
5132 MaxPlusTruncSemiring& operator=(MaxPlusTruncSemiring const&) noexcept
5133 = default;
5134
5138 MaxPlusTruncSemiring& operator=(MaxPlusTruncSemiring&&) noexcept = default;
5139
5140 ~MaxPlusTruncSemiring() = default;
5141
5150 //! \complexity
5151 //! Constant.
5152 explicit MaxPlusTruncSemiring(Scalar threshold) : _threshold(threshold) {
5153 if (threshold < 0) {
5154 LIBSEMIGROUPS_EXCEPTION("expected non-negative value, found {}",
5155 threshold);
5156 }
5157 }
5158
5167 //! \exceptions
5168 //! \noexcept
5169 static constexpr Scalar scalar_one() noexcept {
5170 return 0;
5171 }
5172
5181 //! \exceptions
5182 //! \noexcept
5183 static constexpr Scalar scalar_zero() noexcept {
5184 return NEGATIVE_INFINITY;
5185 }
5186
5209 //! \complexity
5210 //! Constant.
5211 Scalar product_no_checks(Scalar x, Scalar y) const noexcept {
5212 LIBSEMIGROUPS_ASSERT((x >= 0 && x <= _threshold)
5213 || x == NEGATIVE_INFINITY);
5214 LIBSEMIGROUPS_ASSERT((y >= 0 && y <= _threshold)
5215 || y == NEGATIVE_INFINITY);
5216 if (x == NEGATIVE_INFINITY || y == NEGATIVE_INFINITY) {
5217 return NEGATIVE_INFINITY;
5218 }
5219 return std::min(x + y, _threshold);
5220 }
5221
5244 //! \complexity
5245 //! Constant.
5246 Scalar plus_no_checks(Scalar x, Scalar y) const noexcept {
5247 LIBSEMIGROUPS_ASSERT((x >= 0 && x <= _threshold)
5248 || x == NEGATIVE_INFINITY);
5249 LIBSEMIGROUPS_ASSERT((y >= 0 && y <= _threshold)
5250 || y == NEGATIVE_INFINITY);
5251 if (x == NEGATIVE_INFINITY) {
5252 return y;
5253 } else if (y == NEGATIVE_INFINITY) {
5254 return x;
5255 }
5256 return std::max(x, y);
5257 }
5258
5269 //! \complexity
5270 //! Constant.
5271 Scalar threshold() const noexcept {
5272 return _threshold;
5273 }
5274
5275 public:
5276 Scalar const _threshold;
5277 };
5278
5291 template <size_t T, typename Scalar>
5292 using DynamicMaxPlusTruncMat = DynamicMatrix<MaxPlusPlus<Scalar>,
5296 Scalar>;
5297
5311 template <size_t T, size_t R, size_t C, typename Scalar>
5316 R,
5317 C,
5318 Scalar>;
5337 template <size_t T = 0, size_t R = 0, size_t C = R, typename Scalar = int>
5338 using MaxPlusTruncMat = std::conditional_t<
5339 R == 0 || C == 0,
5340 std::conditional_t<T == 0,
5341 DynamicMatrix<MaxPlusTruncSemiring<Scalar>, Scalar>,
5344
5345 namespace detail {
5346 template <typename T>
5347 struct IsMaxPlusTruncMatHelper : std::false_type {};
5348
5349 template <size_t T, size_t R, size_t C, typename Scalar>
5350 struct IsMaxPlusTruncMatHelper<StaticMaxPlusTruncMat<T, R, C, Scalar>>
5351 : std::true_type {
5352 static constexpr Scalar threshold = T;
5353 };
5354
5355 template <size_t T, typename Scalar>
5356 struct IsMaxPlusTruncMatHelper<DynamicMaxPlusTruncMat<T, Scalar>>
5357 : std::true_type {
5358 static constexpr Scalar threshold = T;
5359 };
5360
5361 template <typename Scalar>
5362 struct IsMaxPlusTruncMatHelper<
5363 DynamicMatrix<MaxPlusTruncSemiring<Scalar>, Scalar>> : std::true_type {
5364 static constexpr Scalar threshold = UNDEFINED;
5365 };
5366 } // namespace detail
5367
5379 template <typename T>
5380 static constexpr bool IsMaxPlusTruncMat
5381 = detail::IsMaxPlusTruncMatHelper<T>::value;
5382
5383 namespace detail {
5384 template <typename T>
5385 struct IsTruncMatHelper<T, std::enable_if_t<IsMaxPlusTruncMat<T>>>
5386 : std::true_type {
5387 static constexpr typename T::scalar_type threshold
5388 = IsMaxPlusTruncMatHelper<T>::threshold;
5389 };
5390 } // namespace detail
5391
5392 namespace matrix {
5410 //! (only applies to matrices with run time arithmetic).
5411 template <typename Mat>
5412 std::enable_if_t<IsMaxPlusTruncMat<Mat>> throw_if_bad_entry(Mat const& m) {
5413 // TODO(1) to tpp
5414 detail::throw_if_semiring_nullptr(m);
5415
5416 using scalar_type = typename Mat::scalar_type;
5417 scalar_type const t = matrix::threshold(m);
5418 auto it = std::find_if_not(m.cbegin(), m.cend(), [t](scalar_type x) {
5419 return x == NEGATIVE_INFINITY || (0 <= x && x <= t);
5420 });
5421 if (it != m.cend()) {
5422 auto [r, c] = m.coords(it);
5424 "invalid entry, expected values in {{0, 1, ..., {}, {} (= {})}} "
5425 "but found {} in entry ({}, {})",
5426 t,
5427 entry_repr(NEGATIVE_INFINITY),
5428 static_cast<scalar_type>(NEGATIVE_INFINITY),
5429 detail::entry_repr(*it),
5430 r,
5431 c);
5432 }
5433 }
5434
5454 template <typename Mat>
5455 std::enable_if_t<IsMaxPlusTruncMat<Mat>>
5456 throw_if_bad_entry(Mat const& m, typename Mat::scalar_type val) {
5457 detail::throw_if_semiring_nullptr(m);
5458 using scalar_type = typename Mat::scalar_type;
5459 scalar_type const t = matrix::threshold(m);
5460 if (val == POSITIVE_INFINITY || 0 > val || val > t) {
5462 "invalid entry, expected values in {{0, 1, ..., {}, -{} (= {})}} "
5463 "but found {}",
5464 t,
5465 entry_repr(NEGATIVE_INFINITY),
5466 static_cast<scalar_type>(NEGATIVE_INFINITY),
5467 detail::entry_repr(val));
5468 }
5469 }
5470 } // namespace matrix
5471
5473 // Min-plus matrices with threshold
5475
5521
5545 //! \tparam Scalar the type of the values in the semiring.
5546 template <size_t T, typename Scalar>
5547 struct MinPlusTruncProd {
5558 //! \exceptions
5559 //! \noexcept
5560 Scalar operator()(Scalar x, Scalar y) const noexcept {
5561 LIBSEMIGROUPS_ASSERT((x >= 0 && x <= static_cast<Scalar>(T))
5562 || x == POSITIVE_INFINITY);
5563 LIBSEMIGROUPS_ASSERT((y >= 0 && y <= static_cast<Scalar>(T))
5564 || y == POSITIVE_INFINITY);
5565 if (x == POSITIVE_INFINITY || y == POSITIVE_INFINITY) {
5566 return POSITIVE_INFINITY;
5567 }
5568 return std::min(x + y, static_cast<Scalar>(T));
5569 }
5570 };
5571
5584 //! integral type.
5585 template <typename Scalar = int>
5586 class MinPlusTruncSemiring {
5587 static_assert(std::is_integral<Scalar>::value,
5588 "MinPlus requires an integral type as parameter!");
5589
5590 public:
5594 MinPlusTruncSemiring() = delete;
5595
5599 MinPlusTruncSemiring(MinPlusTruncSemiring const&) noexcept = default;
5600
5604 MinPlusTruncSemiring(MinPlusTruncSemiring&&) noexcept = default;
5605
5609 MinPlusTruncSemiring& operator=(MinPlusTruncSemiring const&) noexcept
5610 = default;
5611
5615 MinPlusTruncSemiring& operator=(MinPlusTruncSemiring&&) noexcept = default;
5616
5625 //! \complexity
5626 //! Constant.
5627 explicit MinPlusTruncSemiring(Scalar threshold) : _threshold(threshold) {
5629 LIBSEMIGROUPS_EXCEPTION("expected non-negative value, found {}",
5630 threshold);
5631 }
5632 }
5633
5642 //! \exceptions
5643 //! \noexcept
5644 static constexpr Scalar scalar_one() noexcept {
5645 return 0;
5646 }
5647
5657 //! \noexcept
5658 // TODO(1) These mem fns (one and zero) aren't needed?
5659 static constexpr Scalar scalar_zero() noexcept {
5660 return POSITIVE_INFINITY;
5661 }
5662
5685 //! \complexity
5686 //! Constant.
5687 Scalar product_no_checks(Scalar x, Scalar y) const noexcept {
5688 LIBSEMIGROUPS_ASSERT((x >= 0 && x <= _threshold)
5689 || x == POSITIVE_INFINITY);
5690 LIBSEMIGROUPS_ASSERT((y >= 0 && y <= _threshold)
5691 || y == POSITIVE_INFINITY);
5692 if (x == POSITIVE_INFINITY || y == POSITIVE_INFINITY) {
5693 return POSITIVE_INFINITY;
5694 }
5695 return std::min(x + y, _threshold);
5696 }
5697
5720 //! \complexity
5721 //! Constant.
5722 Scalar plus_no_checks(Scalar x, Scalar y) const noexcept {
5723 LIBSEMIGROUPS_ASSERT((x >= 0 && x <= _threshold)
5724 || x == POSITIVE_INFINITY);
5725 LIBSEMIGROUPS_ASSERT((y >= 0 && y <= _threshold)
5726 || y == POSITIVE_INFINITY);
5727 if (x == POSITIVE_INFINITY) {
5728 return y;
5729 } else if (y == POSITIVE_INFINITY) {
5730 return x;
5731 }
5732 return std::min(x, y);
5733 }
5734
5745 //! \complexity
5746 //! Constant.
5747 Scalar threshold() const noexcept {
5748 return _threshold;
5749 }
5750
5751 public:
5752 Scalar const _threshold;
5753 };
5754
5767 template <size_t T, typename Scalar>
5768 using DynamicMinPlusTruncMat = DynamicMatrix<MinPlusPlus<Scalar>,
5772 Scalar>;
5773
5787 template <size_t T, size_t R, size_t C, typename Scalar>
5792 R,
5793 C,
5794 Scalar>;
5795
5814 template <size_t T = 0, size_t R = 0, size_t C = R, typename Scalar = int>
5815 using MinPlusTruncMat = std::conditional_t<
5816 R == 0 || C == 0,
5817 std::conditional_t<T == 0,
5818 DynamicMatrix<MinPlusTruncSemiring<Scalar>, Scalar>,
5821
5822 namespace detail {
5823 template <typename T>
5824 struct IsMinPlusTruncMatHelper : std::false_type {};
5825
5826 template <size_t T, size_t R, size_t C, typename Scalar>
5827 struct IsMinPlusTruncMatHelper<StaticMinPlusTruncMat<T, R, C, Scalar>>
5828 : std::true_type {
5829 static constexpr Scalar threshold = T;
5830 };
5831
5832 template <size_t T, typename Scalar>
5833 struct IsMinPlusTruncMatHelper<DynamicMinPlusTruncMat<T, Scalar>>
5834 : std::true_type {
5835 static constexpr Scalar threshold = T;
5836 };
5837
5838 template <typename Scalar>
5839 struct IsMinPlusTruncMatHelper<
5840 DynamicMatrix<MinPlusTruncSemiring<Scalar>, Scalar>> : std::true_type {
5841 static constexpr Scalar threshold = UNDEFINED;
5842 };
5843 } // namespace detail
5844
5856 template <typename T>
5857 static constexpr bool IsMinPlusTruncMat
5858 = detail::IsMinPlusTruncMatHelper<T>::value;
5859
5860 namespace detail {
5861 template <typename T>
5862 struct IsTruncMatHelper<T, std::enable_if_t<IsMinPlusTruncMat<T>>>
5863 : std::true_type {
5864 static constexpr typename T::scalar_type threshold
5865 = IsMinPlusTruncMatHelper<T>::threshold;
5866 };
5867 } // namespace detail
5868
5869 namespace matrix {
5888 // TODO(1) to tpp
5889 template <typename Mat>
5890 std::enable_if_t<IsMinPlusTruncMat<Mat>> throw_if_bad_entry(Mat const& m) {
5891 // Check that the semiring pointer isn't the nullptr if it shouldn't be
5892 detail::throw_if_semiring_nullptr(m);
5893
5894 using scalar_type = typename Mat::scalar_type;
5895 scalar_type const t = matrix::threshold(m);
5896 auto it = std::find_if_not(m.cbegin(), m.cend(), [t](scalar_type x) {
5897 return x == POSITIVE_INFINITY || (0 <= x && x <= t);
5898 });
5899 if (it != m.cend()) {
5900 uint64_t r, c;
5901 std::tie(r, c) = m.coords(it);
5902
5904 "invalid entry, expected values in {{0, 1, ..., {}, {} (= {})}} "
5905 "but found {} in entry ({}, {})",
5906 t,
5907 detail::entry_repr(POSITIVE_INFINITY),
5908 static_cast<scalar_type>(POSITIVE_INFINITY),
5909 detail::entry_repr(*it),
5910 r,
5911 c);
5912 }
5913 }
5914
5934 template <typename Mat>
5935 std::enable_if_t<IsMinPlusTruncMat<Mat>>
5936 throw_if_bad_entry(Mat const& m, typename Mat::scalar_type val) {
5937 detail::throw_if_semiring_nullptr(m);
5938
5939 using scalar_type = typename Mat::scalar_type;
5940 scalar_type const t = matrix::threshold(m);
5941 if (!(val == POSITIVE_INFINITY || (0 <= val && val <= t))) {
5943 "invalid entry, expected values in {{0, 1, ..., {}, {} (= {})}} "
5944 "but found {}",
5945 t,
5946 detail::entry_repr(POSITIVE_INFINITY),
5947 static_cast<scalar_type>(POSITIVE_INFINITY),
5948 detail::entry_repr(val));
5949 }
5950 }
5951 } // namespace matrix
5952
5954 // NTP matrices
5956
6009
6010 namespace detail {
6011 template <size_t T, size_t P, typename Scalar>
6012 constexpr Scalar thresholdperiod(Scalar x) noexcept {
6013 if (x > T) {
6014 return T + (x - T) % P;
6015 }
6016 return x;
6017 }
6018
6019 template <typename Scalar>
6020 inline Scalar thresholdperiod(Scalar x,
6021 Scalar threshold,
6022 Scalar period) noexcept {
6023 if (x > threshold) {
6024 return threshold + (x - threshold) % period;
6025 }
6026 return x;
6027 }
6028 } // namespace detail
6029
6052 // Static arithmetic
6053 template <size_t T, size_t P, typename Scalar>
6054 struct NTPPlus {
6064 //! \exceptions
6065 //! \noexcept
6066 constexpr Scalar operator()(Scalar x, Scalar y) const noexcept {
6067 return detail::thresholdperiod<T, P>(x + y);
6068 }
6069 };
6070
6093 //! \tparam Scalar the type of the values in the semiring.
6094 template <size_t T, size_t P, typename Scalar>
6095 struct NTPProd {
6107 //! \exceptions
6108 //! \noexcept
6109 constexpr Scalar operator()(Scalar x, Scalar y) const noexcept {
6110 return detail::thresholdperiod<T, P>(x * y);
6111 }
6112 };
6113
6127 // Dynamic arithmetic
6128 template <typename Scalar = size_t>
6129 class NTPSemiring {
6130 public:
6134 // Deleted to avoid uninitialised values of period and threshold.
6135 NTPSemiring() = delete;
6136
6140 NTPSemiring(NTPSemiring const&) = default;
6141
6145 NTPSemiring(NTPSemiring&&) = default;
6146
6150 NTPSemiring& operator=(NTPSemiring const&) = default;
6151
6155 NTPSemiring& operator=(NTPSemiring&&) = default;
6156
6157 ~NTPSemiring() = default;
6158
6169 //! \complexity
6170 //! Constant.
6171 NTPSemiring(Scalar t, Scalar p) : _period(p), _threshold(t) {
6172 if constexpr (std::is_signed<Scalar>::value) {
6173 if (t < 0) {
6175 "expected non-negative value for 1st argument, found {}", t);
6176 }
6177 }
6178 if (p <= 0) {
6180 "expected positive value for 2nd argument, found {}", p);
6181 }
6182 }
6183
6192 //! \exceptions
6193 //! \noexcept
6194 static constexpr Scalar scalar_one() noexcept {
6195 return 1;
6196 }
6197
6208 //! \complexity
6209 //! Constant.
6210 static constexpr Scalar scalar_zero() noexcept {
6211 return 0;
6212 }
6213
6236 //! \complexity
6237 //! Constant.
6238 Scalar product_no_checks(Scalar x, Scalar y) const noexcept {
6239 LIBSEMIGROUPS_ASSERT(x >= 0 && x <= _period + _threshold - 1);
6240 LIBSEMIGROUPS_ASSERT(y >= 0 && y <= _period + _threshold - 1);
6241 return detail::thresholdperiod(x * y, _threshold, _period);
6242 }
6243
6266 //! \complexity
6267 //! Constant.
6268 Scalar plus_no_checks(Scalar x, Scalar y) const noexcept {
6269 LIBSEMIGROUPS_ASSERT(x >= 0 && x <= _period + _threshold - 1);
6270 LIBSEMIGROUPS_ASSERT(y >= 0 && y <= _period + _threshold - 1);
6271 return detail::thresholdperiod(x + y, _threshold, _period);
6272 }
6273
6284 //! \complexity
6285 //! Constant.
6286 Scalar threshold() const noexcept {
6287 return _threshold;
6288 }
6289
6300 //! \complexity
6301 //! Constant.
6302 Scalar period() const noexcept {
6303 return _period;
6304 }
6305
6306 private:
6307 Scalar _period;
6308 Scalar _threshold;
6309 };
6310
6321 template <typename Scalar>
6322 using DynamicNTPMatWithSemiring = DynamicMatrix<NTPSemiring<Scalar>, Scalar>;
6323
6337 template <size_t T, size_t P, typename Scalar>
6338 using DynamicNTPMatWithoutSemiring = DynamicMatrix<NTPPlus<T, P, Scalar>,
6342 Scalar>;
6343
6363 template <size_t T, size_t P, size_t R, size_t C, typename Scalar>
6368 R,
6369 C,
6370 Scalar>;
6371
6394 template <size_t T = 0,
6395 size_t P = 0,
6396 size_t R = 0,
6397 size_t C = R,
6398 typename Scalar = size_t>
6399 using NTPMat = std::conditional_t<
6400 R == 0 || C == 0,
6401 std::conditional_t<T == 0 && P == 0,
6405
6406 namespace detail {
6407 template <typename T>
6408 struct IsNTPMatHelper : std::false_type {};
6409
6410 template <typename Scalar>
6411 struct IsNTPMatHelper<DynamicNTPMatWithSemiring<Scalar>> : std::true_type {
6412 static constexpr Scalar threshold = UNDEFINED;
6413 static constexpr Scalar period = UNDEFINED;
6414 };
6415
6416 template <size_t T, size_t P, typename Scalar>
6417 struct IsNTPMatHelper<DynamicNTPMatWithoutSemiring<T, P, Scalar>>
6418 : std::true_type {
6419 static constexpr Scalar threshold = T;
6420 static constexpr Scalar period = P;
6421 };
6422
6423 template <size_t T, size_t P, size_t R, size_t C, typename Scalar>
6424 struct IsNTPMatHelper<StaticNTPMat<T, P, R, C, Scalar>> : std::true_type {
6425 static constexpr Scalar threshold = T;
6426 static constexpr Scalar period = P;
6427 };
6428 } // namespace detail
6429
6441 template <typename U>
6442 static constexpr bool IsNTPMat = detail::IsNTPMatHelper<U>::value;
6443
6444 namespace detail {
6445 template <typename T>
6446 struct IsTruncMatHelper<T, std::enable_if_t<IsNTPMat<T>>> : std::true_type {
6447 static constexpr typename T::scalar_type threshold
6448 = IsNTPMatHelper<T>::threshold;
6449 static constexpr typename T::scalar_type period
6450 = IsNTPMatHelper<T>::period;
6451 };
6452
6453 } // namespace detail
6454
6455 namespace matrix {
6476 //! \noexcept
6477 template <size_t T, size_t P, size_t R, size_t C, typename Scalar>
6478 constexpr Scalar period(StaticNTPMat<T, P, R, C, Scalar> const&) noexcept {
6479 return P;
6480 }
6481
6499 template <size_t T, size_t P, typename Scalar>
6500 constexpr Scalar
6502 return P;
6503 }
6504
6519 //! \noexcept
6520 template <typename Scalar>
6521 Scalar period(DynamicNTPMatWithSemiring<Scalar> const& x) noexcept {
6522 return x.semiring()->period();
6523 }
6524 } // namespace matrix
6525
6526 namespace matrix {
6546 //! defined (only applies to matrices with run time arithmetic).
6547 template <typename Mat>
6548 std::enable_if_t<IsNTPMat<Mat>> throw_if_bad_entry(Mat const& m) {
6549 detail::throw_if_semiring_nullptr(m);
6550
6551 using scalar_type = typename Mat::scalar_type;
6552 scalar_type const t = matrix::threshold(m);
6553 scalar_type const p = matrix::period(m);
6554 auto it = std::find_if_not(m.cbegin(), m.cend(), [t, p](scalar_type x) {
6555 return (0 <= x && x < p + t);
6556 });
6557 if (it != m.cend()) {
6558 uint64_t r, c;
6559 std::tie(r, c) = m.coords(it);
6560
6562 "invalid entry, expected values in {{0, 1, ..., {}}}, but "
6563 "found {} in entry ({}, {})",
6564 p + t,
6565 detail::entry_repr(*it),
6566 r,
6567 c);
6568 }
6569 }
6570
6592 template <typename Mat>
6593 std::enable_if_t<IsNTPMat<Mat>>
6594 throw_if_bad_entry(Mat const& m, typename Mat::scalar_type val) {
6595 detail::throw_if_semiring_nullptr(m);
6596 using scalar_type = typename Mat::scalar_type;
6597 scalar_type const t = matrix::threshold(m);
6598 scalar_type const p = matrix::period(m);
6599 if (val < 0 || val >= p + t) {
6601 "invalid entry, expected values in {{0, 1, ..., {}}}, but "
6602 "found {}",
6603 p + t,
6604 detail::entry_repr(val));
6605 }
6606 }
6607 } // namespace matrix
6608
6610 // Projective max-plus matrices
6612
6613 namespace detail {
6614 template <typename T>
6615 class ProjMaxPlusMat : MatrixPolymorphicBase {
6616 public:
6617 using scalar_type = typename T::scalar_type;
6618 using scalar_reference = typename T::scalar_reference;
6619 using scalar_const_reference = typename T::scalar_const_reference;
6620 using semiring_type = void;
6621
6622 using container_type = typename T::container_type;
6623 using iterator = typename T::iterator;
6624 using const_iterator = typename T::const_iterator;
6625
6626 using underlying_matrix_type = T;
6627
6628 using RowView = typename T::RowView;
6629
6630 // Note that Rows are never normalised, and that's why we use the
6631 // underlying matrix Row type and not 1 x n ProjMaxPlusMat's instead
6632 // (since these will be normalised according to their entries, and
6633 // this might not correspond to the normalised entries of the matrix).
6634 using Row = typename T::Row;
6635
6636 scalar_type scalar_one() const noexcept {
6637 return _underlying_mat.scalar_one();
6638 }
6639
6640 scalar_type scalar_zero() const noexcept {
6641 return _underlying_mat.scalar_zero();
6642 }
6643
6645 // ProjMaxPlusMat - Constructors + destructor - public
6647
6648 ProjMaxPlusMat() : _is_normalized(false), _underlying_mat() {}
6649 ProjMaxPlusMat(ProjMaxPlusMat const&) = default;
6650 ProjMaxPlusMat(ProjMaxPlusMat&&) = default;
6651 ProjMaxPlusMat& operator=(ProjMaxPlusMat const&) = default;
6652 ProjMaxPlusMat& operator=(ProjMaxPlusMat&&) = default;
6653
6654 ProjMaxPlusMat(size_t r, size_t c)
6655 : _is_normalized(false), _underlying_mat(r, c) {}
6656
6657 // TODO(1) other missing constructors
6658 ProjMaxPlusMat(
6659 typename underlying_matrix_type::semiring_type const* semiring,
6660 size_t r,
6661 size_t c)
6662 : _is_normalized(false), _underlying_mat(semiring, r, c) {}
6663
6664 explicit ProjMaxPlusMat(std::vector<std::vector<scalar_type>> const& m)
6665 : _is_normalized(false), _underlying_mat(m) {
6666 normalize();
6667 }
6668
6669 ProjMaxPlusMat(
6670 std::initializer_list<std::initializer_list<scalar_type>> const& m)
6671 : ProjMaxPlusMat(
6672 std::vector<std::vector<scalar_type>>(m.begin(), m.end())) {}
6673
6674 ~ProjMaxPlusMat() = default;
6675
6676 ProjMaxPlusMat one() const {
6677 auto result = ProjMaxPlusMat(_underlying_mat.one());
6678 return result;
6679 }
6680
6681 static ProjMaxPlusMat one(size_t n) {
6682 return ProjMaxPlusMat(T::one(n));
6683 }
6684
6686 // Comparison operators
6688
6689 bool operator==(ProjMaxPlusMat const& that) const {
6690 normalize();
6691 that.normalize();
6692 return _underlying_mat == that._underlying_mat;
6693 }
6694
6695 bool operator!=(ProjMaxPlusMat const& that) const {
6696 return !(_underlying_mat == that._underlying_mat);
6697 }
6698
6699 bool operator<(ProjMaxPlusMat const& that) const {
6700 normalize();
6701 that.normalize();
6702 return _underlying_mat < that._underlying_mat;
6703 }
6704
6705 bool operator>(ProjMaxPlusMat const& that) const {
6706 return that < *this;
6707 }
6708
6709 template <typename Thing>
6710 bool operator>=(Thing const& that) const {
6711 static_assert(IsMatrix<Thing> || std::is_same_v<Thing, RowView>);
6712 return that < *this || that == *this;
6713 }
6714
6715 // not noexcept because operator< isn't
6716 template <typename Thing>
6717 bool operator<=(Thing const& that) const {
6718 static_assert(IsMatrix<Thing> || std::is_same_v<Thing, RowView>);
6719 return *this < that || that == *this;
6720 }
6721
6723 // Attributes
6725
6726 scalar_reference operator()(size_t r, size_t c) {
6727 // to ensure the returned value is normalised
6728 normalize();
6729 // to ensure that the matrix is renormalised if the returned scalar is
6730 // assigned.
6731 _is_normalized = false;
6732 return _underlying_mat(r, c);
6733 }
6734
6735 scalar_reference at(size_t r, size_t c) {
6736 matrix::throw_if_bad_coords(*this, r, c);
6737 return this->operator()(r, c);
6738 }
6739
6740 scalar_const_reference operator()(size_t r, size_t c) const {
6741 normalize();
6742 return _underlying_mat(r, c);
6743 }
6744
6745 scalar_const_reference at(size_t r, size_t c) const {
6746 matrix::throw_if_bad_coords(*this, r, c);
6747 return this->operator()(r, c);
6748 }
6749
6750 size_t number_of_rows() const noexcept {
6751 return _underlying_mat.number_of_rows();
6752 }
6753
6754 size_t number_of_cols() const noexcept {
6755 return _underlying_mat.number_of_cols();
6756 }
6757
6758 size_t hash_value() const {
6759 normalize();
6760 return Hash<T>()(_underlying_mat);
6761 }
6762
6764 // Arithmetic operators - in-place
6766
6767 void product_inplace_no_checks(ProjMaxPlusMat const& A,
6768 ProjMaxPlusMat const& B) {
6769 _underlying_mat.product_inplace_no_checks(A._underlying_mat,
6770 B._underlying_mat);
6771 normalize(true); // force normalize
6772 }
6773
6774 void operator+=(ProjMaxPlusMat const& that) {
6775 _underlying_mat += that._underlying_mat;
6776 normalize(true); // force normalize
6777 }
6778
6779 void operator*=(scalar_type a) {
6780 _underlying_mat *= a;
6781 normalize(true); // force normalize
6782 }
6783
6784 void operator+=(scalar_type a) {
6785 _underlying_mat += a;
6786 normalize(true); // force normalize
6787 }
6788
6789 ProjMaxPlusMat operator*(scalar_type a) const {
6790 ProjMaxPlusMat result(*this);
6791 result *= a;
6792 return result;
6793 }
6794
6795 ProjMaxPlusMat operator+(scalar_type a) const {
6796 ProjMaxPlusMat result(*this);
6797 result += a;
6798 return result;
6799 }
6800
6802 // Arithmetic operators - not in-place
6804
6805 ProjMaxPlusMat operator+(ProjMaxPlusMat const& that) const {
6806 return ProjMaxPlusMat(_underlying_mat + that._underlying_mat);
6807 }
6808
6809 ProjMaxPlusMat operator*(ProjMaxPlusMat const& that) const {
6810 return ProjMaxPlusMat(_underlying_mat * that._underlying_mat);
6811 }
6812
6814 // Iterators
6816
6817 // The following should probably be commented out because I can't
6818 // currently think how to ensure that the matrix is normalised if it's
6819 // changed this way.
6820
6821 iterator begin() noexcept {
6822 // to ensure the returned value is normalised
6823 normalize();
6824 // to ensure that the matrix is renormalised if the returned scalar is
6825 // assigned.
6826 _is_normalized = false;
6827 return _underlying_mat.begin();
6828 }
6829
6830 iterator end() noexcept {
6831 // to ensure the returned value is normalised
6832 normalize();
6833 // to ensure that the matrix is renormalised if the returned scalar is
6834 // assigned.
6835 _is_normalized = false;
6836 return _underlying_mat.end();
6837 }
6838
6839 const_iterator begin() const noexcept {
6840 normalize();
6841 return _underlying_mat.begin();
6842 }
6843
6844 const_iterator end() const noexcept {
6845 normalize();
6846 return _underlying_mat.end();
6847 }
6848
6849 const_iterator cbegin() const noexcept {
6850 normalize();
6851 return _underlying_mat.cbegin();
6852 }
6853
6854 const_iterator cend() const noexcept {
6855 normalize();
6856 return _underlying_mat.cend();
6857 }
6858
6860 // Modifiers
6862
6863 void swap(ProjMaxPlusMat& that) noexcept {
6864 std::swap(_underlying_mat, that._underlying_mat);
6865 }
6866
6867 void transpose() noexcept {
6868 _underlying_mat.transpose();
6869 }
6870
6871 void transpose_no_checks() noexcept {
6872 _underlying_mat.transpose_no_checks();
6873 }
6874
6876 // Rows
6878
6879 RowView row(size_t i) const {
6880 normalize();
6881 return _underlying_mat.row(i);
6882 }
6883
6884 template <typename C>
6885 void rows(C& x) const {
6886 normalize();
6887 return _underlying_mat.rows(x);
6888 }
6889
6891 // Friend functions
6893
6894 friend std::ostream& operator<<(std::ostream& os,
6895 ProjMaxPlusMat const& x) {
6896 x.normalize();
6897 os << detail::to_string(x._underlying_mat);
6898 return os;
6899 }
6900
6901 T const& underlying_matrix() const noexcept {
6902 normalize();
6903 return _underlying_mat;
6904 }
6905
6906 private:
6907 explicit ProjMaxPlusMat(T&& mat)
6908 : _is_normalized(false), _underlying_mat(std::move(mat)) {
6909 normalize();
6910 }
6911
6912 void normalize(bool force = false) const {
6913 if ((_is_normalized && !force)
6914 || (_underlying_mat.number_of_rows() == 0)
6915 || (_underlying_mat.number_of_cols() == 0)) {
6916 _is_normalized = true;
6917 return;
6918 }
6919 scalar_type const n = *std::max_element(_underlying_mat.cbegin(),
6920 _underlying_mat.cend());
6921 std::for_each(_underlying_mat.begin(),
6922 _underlying_mat.end(),
6923 [&n](scalar_type& s) {
6924 if (s != NEGATIVE_INFINITY) {
6925 s -= n;
6926 }
6927 });
6928 _is_normalized = true;
6929 }
6930
6931 mutable bool _is_normalized;
6932 mutable T _underlying_mat;
6933 };
6934 } // namespace detail
6935
6980
6994 template <size_t R, size_t C, typename Scalar>
6996 = detail::ProjMaxPlusMat<StaticMaxPlusMat<R, C, Scalar>>;
6997
7009 template <typename Scalar>
7011 = detail::ProjMaxPlusMat<DynamicMaxPlusMat<Scalar>>;
7012
7027 template <size_t R = 0, size_t C = R, typename Scalar = int>
7028 using ProjMaxPlusMat = std::conditional_t<R == 0 || C == 0,
7031
7032 namespace detail {
7033 template <typename T>
7034 struct IsProjMaxPlusMatHelper : std::false_type {};
7035
7036 template <size_t R, size_t C, typename Scalar>
7037 struct IsProjMaxPlusMatHelper<StaticProjMaxPlusMat<R, C, Scalar>>
7038 : std::true_type {};
7039
7040 template <typename Scalar>
7041 struct IsProjMaxPlusMatHelper<DynamicProjMaxPlusMat<Scalar>>
7042 : std::true_type {};
7043 } // namespace detail
7044
7056 template <typename T>
7057 static constexpr bool IsProjMaxPlusMat
7058 = detail::IsProjMaxPlusMatHelper<T>::value;
7059
7060 namespace matrix {
7061 // \ingroup projmaxplus_group
7062 //
7076 //! \throws LibsemigroupsException if
7077 //! `throw_if_bad_entry(x.underlying_matrix())` throws.
7078 template <typename Mat>
7079 constexpr std::enable_if_t<IsProjMaxPlusMat<Mat>>
7080 throw_if_bad_entry(Mat const& x) {
7081 throw_if_bad_entry(x.underlying_matrix());
7082 }
7083
7084 // \ingroup projmaxplus_group
7085 //
7099 //! \throws LibsemigroupsException if
7100 //! `throw_if_bad_entry(x.underlying_matrix(), val)` throws.
7101 template <typename Mat>
7102 constexpr std::enable_if_t<IsProjMaxPlusMat<Mat>>
7103 throw_if_bad_entry(Mat const& x, typename Mat::scalar_type val) {
7104 throw_if_bad_entry(x.underlying_matrix(), val);
7105 }
7106
7108 // Matrix helpers - pow
7110
7144 //! \endcode
7145 // TODO(1) pow_no_checks
7146 // TODO(2) version that changes x in-place
7147 template <typename Mat>
7148 Mat pow(Mat const& x, typename Mat::scalar_type e) {
7149 using scalar_type = typename Mat::scalar_type;
7150
7151 if constexpr (std::is_signed<scalar_type>::value) {
7152 if (e < 0) {
7154 "negative exponent, expected value >= 0, found {}", e);
7155 }
7156 }
7157
7159
7160 typename Mat::semiring_type const* sr = nullptr;
7161
7162 if constexpr (IsMatWithSemiring<Mat>) {
7163 sr = x.semiring();
7164 }
7165
7166 if (e == 0) {
7167 return x.one();
7168 }
7169
7170 auto y = Mat(x);
7171 if (e == 1) {
7172 return y;
7173 }
7174 auto z = (e % 2 == 0 ? x.one() : y);
7175
7176 Mat tmp(sr, x.number_of_rows(), x.number_of_cols());
7177 while (e > 1) {
7178 tmp.product_inplace_no_checks(y, y);
7179 std::swap(y, tmp);
7180 e /= 2;
7181 if (e % 2 == 1) {
7182 tmp.product_inplace_no_checks(z, y);
7183 std::swap(z, tmp);
7184 }
7185 }
7186 return z;
7187 }
7188
7190 // Matrix helpers - rows
7192
7209 //!
7210 //! \complexity
7211 //! \f$O(m)\f$ where \f$m\f$ is the number of rows in the matrix \p x.
7212 template <typename Mat, typename = std::enable_if_t<IsDynamicMatrix<Mat>>>
7215 x.rows(container);
7216 return container;
7217 }
7218
7237 //! \complexity
7238 //! \f$O(m)\f$ where \f$m\f$ is the number of rows in the matrix \p x.
7239 template <typename Mat, typename = std::enable_if_t<IsStaticMatrix<Mat>>>
7240 detail::StaticVector1<typename Mat::RowView, Mat::nr_rows>
7241 rows(Mat const& x) {
7242 detail::StaticVector1<typename Mat::RowView, Mat::nr_rows> container;
7243 x.rows(container);
7244 return container;
7245 }
7246
7248 // Matrix helpers - bitset_rows
7250
7251 // The main function
7280 //! \complexity
7281 //! \f$O(mn)\f$ where \f$m\f$ is the number of rows in `views` and
7282 //! and \f$n\f$ is the number of columns in any vector in `views`.
7283 template <typename Mat, size_t R, size_t C, typename Container>
7284 void bitset_rows(Container&& views,
7285 detail::StaticVector1<BitSet<C>, R>& result) {
7286 using RowView = typename Mat::RowView;
7287 using value_type = typename std::decay_t<Container>::value_type;
7288 // std::vector<bool> is used as value_type in the benchmarks
7289 static_assert(IsBMat<Mat>, "IsBMat<Mat> must be true!");
7292 "Container::value_type must equal Mat::RowView or "
7293 "std::vector<bool>!!");
7294 static_assert(R <= BitSet<1>::max_size(),
7295 "R must be at most BitSet<1>::max_size()!");
7296 static_assert(C <= BitSet<1>::max_size(),
7297 "C must be at most BitSet<1>::max_size()!");
7298 LIBSEMIGROUPS_ASSERT(views.size() <= R);
7299 LIBSEMIGROUPS_ASSERT(views.empty() || views[0].size() <= C);
7300 for (auto const& v : views) {
7301 result.emplace_back(v.cbegin(), v.cend());
7302 }
7303 }
7304
7334 //! \complexity
7335 //! \f$O(mn)\f$ where \f$m\f$ is the number of rows in \p views and
7336 //! and \f$n\f$ is the number of columns in any vector in \p views.
7337 template <typename Mat, size_t R, size_t C, typename Container>
7338 auto bitset_rows(Container&& views) {
7339 using RowView = typename Mat::RowView;
7340 using value_type = typename std::decay_t<Container>::value_type;
7341 static_assert(IsBMat<Mat>, "IsBMat<Mat> must be true!");
7344 "Container::value_type must equal Mat::RowView or "
7345 "std::vector<bool>!!");
7346 static_assert(R <= BitSet<1>::max_size(),
7347 "R must be at most BitSet<1>::max_size()!");
7348 static_assert(C <= BitSet<1>::max_size(),
7349 "C must be at most BitSet<1>::max_size()!");
7350 LIBSEMIGROUPS_ASSERT(views.size() <= R);
7351 LIBSEMIGROUPS_ASSERT(views.empty() || views[0].size() <= C);
7352 detail::StaticVector1<BitSet<C>, R> result;
7354 return result;
7355 }
7356
7357 // Helper
7383 //! \complexity
7384 //! \f$O(mn)\f$ where \f$m\f$ is the number of rows in \p x and and
7385 //! \f$n\f$ is the number of columns in \p x.
7386 template <typename Mat, size_t R, size_t C>
7387 void bitset_rows(Mat const& x,
7388 detail::StaticVector1<BitSet<C>, R>& result) {
7389 static_assert(IsBMat<Mat>, "IsBMat<Mat> must be true!");
7390 static_assert(R <= BitSet<1>::max_size(),
7391 "R must be at most BitSet<1>::max_size()!");
7392 static_assert(C <= BitSet<1>::max_size(),
7393 "C must be at most BitSet<1>::max_size()!");
7394 LIBSEMIGROUPS_ASSERT(x.number_of_cols() <= C);
7395 LIBSEMIGROUPS_ASSERT(x.number_of_rows() <= R);
7396 bitset_rows<Mat>(std::move(rows(x)), result);
7397 }
7398
7399 // Helper
7415 //! \complexity
7416 //! \f$O(mn)\f$ where \f$m\f$ is the number of rows in \p x and
7417 //! and \f$n\f$ is the number of columns in \p x.
7418 template <typename Mat>
7419 auto bitset_rows(Mat const& x) {
7420 static_assert(IsBMat<Mat>, "IsBMat<Mat> must be true!");
7421 LIBSEMIGROUPS_ASSERT(x.number_of_rows() <= BitSet<1>::max_size());
7422 LIBSEMIGROUPS_ASSERT(x.number_of_cols() <= BitSet<1>::max_size());
7423 size_t const M = detail::BitSetCapacity<Mat>::value;
7425 }
7426
7428 // Matrix helpers - bitset_row_basis
7430
7452 //! \f$c\f$ is the size of each bitset in `rows`.
7453 // This works with std::vector and StaticVector1, with value_type equal
7454 // to std::bitset and BitSet.
7455 template <typename Mat, typename Container>
7456 void bitset_row_basis(Container&& rows, std::decay_t<Container>& result) {
7457 using value_type = typename std::decay_t<Container>::value_type;
7458 static_assert(IsBMat<Mat>, "IsBMat<Mat> must be true!");
7459 static_assert(IsBitSet<value_type> || detail::IsStdBitSet<value_type>,
7460 "Container::value_type must be BitSet or std::bitset");
7461 LIBSEMIGROUPS_ASSERT(rows.size() <= BitSet<1>::max_size());
7462 LIBSEMIGROUPS_ASSERT(rows.empty()
7463 || rows[0].size() <= BitSet<1>::max_size());
7464
7465 std::sort(rows.begin(), rows.end(), detail::LessBitSet());
7466 // Remove duplicates
7467 rows.erase(std::unique(rows.begin(), rows.end()), rows.end());
7468 for (size_t i = 0; i < rows.size(); ++i) {
7469 value_type cup;
7470 cup.reset();
7471 for (size_t j = 0; j < i; ++j) {
7472 if ((rows[i] & rows[j]) == rows[j]) {
7473 cup |= rows[j];
7474 }
7475 }
7476 for (size_t j = i + 1; j < rows.size(); ++j) {
7477 if ((rows[i] & rows[j]) == rows[j]) {
7478 cup |= rows[j];
7479 }
7480 }
7481 if (cup != rows[i]) {
7482 result.push_back(std::move(rows[i]));
7483 }
7484 }
7485 }
7486
7507 //! \complexity
7508 //! \f$O(r ^ 2 c)\f$ where \f$r\f$ is the size of \p rows and
7509 //! \f$c\f$ is the size of each bitset in \p rows.
7510 template <typename Mat, typename Container>
7511 std::decay_t<Container> bitset_row_basis(Container&& rows) {
7512 using value_type = typename std::decay_t<Container>::value_type;
7513 static_assert(IsBMat<Mat>, "IsBMat<Mat> must be true!");
7514 static_assert(IsBitSet<value_type> || detail::IsStdBitSet<value_type>,
7515 "Container::value_type must be BitSet or std::bitset");
7516 LIBSEMIGROUPS_ASSERT(rows.size() <= BitSet<1>::max_size());
7517 LIBSEMIGROUPS_ASSERT(rows.empty()
7518 || rows[0].size() <= BitSet<1>::max_size());
7519 std::decay_t<Container> result;
7521 return result;
7522 }
7523
7548 //! \complexity
7549 //! \f$O(r ^ 2 c)\f$ where \f$r\f$ is the number of rows in \p x and
7550 //! \f$c\f$ is the number of columns in \p x.
7551 template <typename Mat, size_t M = detail::BitSetCapacity<Mat>::value>
7552 detail::StaticVector1<BitSet<M>, M> bitset_row_basis(Mat const& x) {
7553 static_assert(IsBMat<Mat>, "IsBMat<Mat> must be true!");
7554 LIBSEMIGROUPS_ASSERT(x.number_of_rows() <= BitSet<1>::max_size());
7555 LIBSEMIGROUPS_ASSERT(x.number_of_cols() <= BitSet<1>::max_size());
7556 detail::StaticVector1<BitSet<M>, M> result;
7558 return result;
7559 }
7560
7581 //! \complexity
7582 //! \f$O(r ^ 2 c)\f$ where \f$r\f$ is the number of rows in \p x
7583 //! and \f$c\f$ is the number of columns in \p x.
7584 template <typename Mat, typename Container>
7585 void bitset_row_basis(Mat const& x, Container& result) {
7586 using value_type = typename Container::value_type;
7587 static_assert(IsBMat<Mat>, "IsBMat<Mat> must be true!");
7588 static_assert(IsBitSet<value_type> || detail::IsStdBitSet<value_type>,
7589 "Container::value_type must be BitSet or std::bitset");
7590 LIBSEMIGROUPS_ASSERT(x.number_of_rows() <= BitSet<1>::max_size());
7591 LIBSEMIGROUPS_ASSERT(x.number_of_cols() <= BitSet<1>::max_size());
7593 }
7594
7596 // Matrix helpers - row_basis - MaxPlusTruncMat
7598
7624 //! \f$O(r ^ 2 c)\f$ where \f$r\f$ is the size of \p views and
7625 //! \f$c\f$ is the size of each row view or bit set in \p views.
7626 template <typename Mat, typename Container>
7627 std::enable_if_t<IsMaxPlusTruncMat<Mat>>
7628 row_basis(Container&& views, std::decay_t<Container>& result) {
7629 using value_type = typename std::decay_t<Container>::value_type;
7631 "Container::value_type must be Mat::RowView");
7632 using scalar_type = typename Mat::scalar_type;
7633 using Row = typename Mat::Row;
7634
7635 if (views.empty()) {
7636 return;
7637 }
7638
7639 LIBSEMIGROUPS_ASSERT(result.empty());
7640
7641 std::sort(views.begin(), views.end());
7642 Row tmp1(views[0]);
7643
7644 for (size_t r1 = 0; r1 < views.size(); ++r1) {
7645 if (r1 == 0 || views[r1] != views[r1 - 1]) {
7646 std::fill(tmp1.begin(), tmp1.end(), tmp1.scalar_zero());
7647 for (size_t r2 = 0; r2 < r1; ++r2) {
7648 scalar_type max_scalar = matrix::threshold(tmp1);
7649 for (size_t c = 0; c < tmp1.number_of_cols(); ++c) {
7650 if (views[r2][c] == tmp1.scalar_zero()) {
7651 continue;
7652 }
7653 if (views[r1][c] >= views[r2][c]) {
7654 if (views[r1][c] != matrix::threshold(tmp1)) {
7655 max_scalar
7656 = std::min(max_scalar, views[r1][c] - views[r2][c]);
7657 }
7658 } else {
7659 max_scalar = tmp1.scalar_zero();
7660 break;
7661 }
7662 }
7663 if (max_scalar != tmp1.scalar_zero()) {
7664 tmp1 += views[r2] * max_scalar;
7665 }
7666 }
7667 if (tmp1 != views[r1]) {
7668 result.push_back(views[r1]);
7669 }
7670 }
7671 }
7672 }
7673
7675 // Matrix helpers - row_basis - BMat
7677
7678 // This version of row_basis for BMat's is for used for compatibility
7679 // with the MatrixCommon framework, i.e. so that BMat's exhibit the same
7680 // interface/behaviour as other matrices.
7681 //
7682 // This version takes a container of row views of BMat, converts it to a
7683 // container of BitSets, computes the row basis using the BitSets, then
7684 // selects those row views in views that belong to the computed basis.
7685
7701 //! \exceptions
7702 //! \no_libsemigroups_except
7703 // TODO(2) complexity
7704 template <typename Mat, typename Container>
7705 std::enable_if_t<IsBMat<Mat>> row_basis(Container&& views,
7706 std::decay_t<Container>& result) {
7707 using RowView = typename Mat::RowView;
7708 using value_type = typename std::decay_t<Container>::value_type;
7709 // std::vector<bool> is used as value_type in the benchmarks
7712 "Container::value_type must equal Mat::RowView or "
7713 "std::vector<bool>!!");
7714
7715 if (views.empty()) {
7716 return;
7717 }
7718
7719 // Convert RowViews to BitSets
7720 size_t const M = detail::BitSetCapacity<Mat>::value;
7722 using bitset_type = typename decltype(br)::value_type;
7723
7724 // Map for converting bitsets back to row views
7726 LIBSEMIGROUPS_ASSERT(br.size() == views.size());
7727 for (size_t i = 0; i < br.size(); ++i) {
7728 lookup.insert({br[i], i});
7729 }
7730
7731 // Compute rowbasis using bitsets + convert back to rowviews
7732 for (auto const& bs : bitset_row_basis<Mat>(br)) {
7733 auto it = lookup.find(bs);
7734 LIBSEMIGROUPS_ASSERT(it != lookup.end());
7735 result.push_back(views[it->second]);
7736 }
7737 }
7738
7740 // Matrix helpers - row_basis - generic helpers
7742
7764 // Row basis of rowspace of matrix <x> appended to <result>
7765 template <typename Mat,
7766 typename Container,
7767 typename = std::enable_if_t<IsMatrix<Mat>>>
7768 void row_basis(Mat const& x, Container& result) {
7769 row_basis<Mat>(std::move(rows(x)), result);
7770 }
7771
7789 //! \f$O(r ^ 2 c)\f$ where \f$r\f$ is the number of rows in \p x
7790 //! and \f$c\f$ is the number of columns in \p x.
7791 // Row basis of rowspace of matrix <x>
7792 template <typename Mat, typename = std::enable_if_t<IsDynamicMatrix<Mat>>>
7795 row_basis(x, container);
7796 return container;
7797 }
7798
7816 //! \f$O(r ^ 2 c)\f$ where \f$r\f$ is the number of rows in \p x
7817 //! and \f$c\f$ is the number of columns in \p x.
7818 template <typename Mat, typename = std::enable_if_t<IsStaticMatrix<Mat>>>
7819 detail::StaticVector1<typename Mat::RowView, Mat::nr_rows>
7820 row_basis(Mat const& x) {
7821 detail::StaticVector1<typename Mat::RowView, Mat::nr_rows> container;
7822 row_basis(x, container);
7823 return container;
7824 }
7825
7842 //! \exceptions
7843 //! \no_libsemigroups_except
7844 // TODO(2) complexity
7845 template <typename Mat, typename Container>
7846 std::decay_t<Container> row_basis(Container&& rows) {
7847 using value_type = typename std::decay_t<Container>::value_type;
7848 static_assert(IsMatrix<Mat>, "IsMatrix<Mat> must be true!");
7850 "Container::value_type must be Mat::RowView");
7851
7852 std::decay_t<Container> result;
7854 return result;
7855 }
7856
7858 // Matrix helpers - row_space_size
7860
7888 //! auto x = make<BMat<>>({{1, 0, 0}, {0, 0, 1}, {0, 1, 0}});
7889 //! matrix::row_space_size(x); // returns 7
7890 //! \endcode
7891 template <typename Mat, typename = std::enable_if_t<IsBMat<Mat>>>
7892 size_t row_space_size(Mat const& x) {
7893 size_t const M = detail::BitSetCapacity<Mat>::value;
7894 auto bitset_row_basis_ = bitset_row_basis<Mat>(
7896
7898 st.insert(bitset_row_basis_.cbegin(), bitset_row_basis_.cend());
7899 std::vector<BitSet<M>> orb(bitset_row_basis_.cbegin(),
7900 bitset_row_basis_.cend());
7901 for (size_t i = 0; i < orb.size(); ++i) {
7902 for (auto& row : bitset_row_basis_) {
7903 auto cup = orb[i];
7904 for (size_t j = 0; j < x.number_of_rows(); ++j) {
7905 cup.set(j, cup[j] || row[j]);
7906 }
7907 if (st.insert(cup).second) {
7908 orb.push_back(std::move(cup));
7909 }
7910 }
7911 }
7912 return orb.size();
7913 }
7914
7915 } // namespace matrix
7916
7933 //! \no_libsemigroups_except
7934 //!
7935 //! \warning This function does not detect overflows of `Mat::scalar_type`.
7936 template <typename Mat>
7937 auto operator+(typename Mat::scalar_type a, Mat const& x)
7938 -> std::enable_if_t<IsMatrix<Mat>, Mat> {
7939 return x + a;
7940 }
7941
7958 //! \no_libsemigroups_except
7959 //!
7960 //! \warning This function does not detect overflows of `Mat::scalar_type`.
7961 template <typename Mat>
7962 auto operator*(typename Mat::scalar_type a, Mat const& x)
7963 -> std::enable_if_t<IsMatrix<Mat>, Mat> {
7964 return x * a;
7965 }
7966
7977
8002 //! \f$n\f$ is the number of columns of the matrix.
8003 template <typename Mat,
8004 typename
8005 = std::enable_if_t<IsMatrix<Mat> && !IsMatWithSemiring<Mat>>>
8007 detail::throw_if_any_row_wrong_size(rows);
8008 detail::throw_if_bad_dim<Mat>(rows);
8009 Mat m(rows);
8011 return m;
8012 }
8013
8038 //! \f$n\f$ is the number of columns of the matrix.
8039 template <typename Mat,
8040 typename
8041 = std::enable_if_t<IsMatrix<Mat> && !IsMatWithSemiring<Mat>>>
8043 rows) {
8045 }
8046
8072 //! parameter \c R is \c 1.
8073 template <typename Mat,
8074 typename
8075 = std::enable_if_t<IsMatrix<Mat> && !IsMatWithSemiring<Mat>>>
8077 // TODO(0) Add row dimension checking for compile-time size matrices
8078 Mat m(row);
8080 return m;
8081 }
8082 // TODO(1) vector version of above
8083
8115 template <typename Mat,
8116 typename Semiring,
8117 typename = std::enable_if_t<IsMatrix<Mat>>>
8118 // TODO(1) pass Semiring by reference, this is hard mostly due to the way
8119 // the tests are written, which is not optimal.
8120 Mat make(Semiring const* semiring,
8123 detail::throw_if_any_row_wrong_size(rows);
8124 detail::throw_if_bad_dim<Mat>(rows);
8125 Mat m(semiring, rows);
8127 return m;
8128 }
8129
8160 //! \f$n\f$ is the number of columns of the matrix.
8161 template <typename Mat,
8162 typename Semiring,
8163 typename = std::enable_if_t<IsMatrix<Mat>>>
8164 Mat make(Semiring const* semiring,
8166 detail::throw_if_any_row_wrong_size(rows);
8167 detail::throw_if_bad_dim<Mat>(rows);
8168 Mat m(semiring, rows);
8170 return m;
8171 }
8172
8194 //! \f$O(n)\f$ where \f$n\f$ is the number of columns of the matrix.
8195 template <typename Mat,
8196 typename Semiring,
8197 typename = std::enable_if_t<IsMatrix<Mat>>>
8198 Mat make(Semiring const* semiring,
8200 // TODO(0) Add row dimension checking for compile-time size matrices
8201 Mat m(semiring, row);
8203 return m;
8204 }
8205
8231 //! \f$O(mn)\f$ where \f$m\f$ is the number of rows and \f$n\f$ is the
8232 //! number of columns of the matrix.
8233 template <size_t R, size_t C, typename Scalar>
8238 }
8239
8241 // Printing etc...
8243
8253 //!
8254 //! \exceptions
8255 //! \no_libsemigroups_except
8256 template <typename S, typename T>
8258 detail::RowViewCommon<S, T> const& x) {
8259 os << "{";
8260 for (auto it = x.cbegin(); it != x.cend(); ++it) {
8261 os << *it;
8262 if (it != x.cend() - 1) {
8263 os << ", ";
8264 }
8265 }
8266 os << "}";
8267 return os;
8268 }
8269
8283 //!
8284 //! \exceptions
8285 //! \no_libsemigroups_except
8286 template <typename Mat>
8287 auto operator<<(std::ostringstream& os, Mat const& x)
8288 -> std::enable_if_t<IsMatrix<Mat>, std::ostringstream&> {
8289 size_t n = 0;
8290 if (x.number_of_rows() != 1) {
8291 os << "{";
8292 }
8293 for (auto&& r : matrix::rows(x)) {
8294 os << r;
8295 if (n != x.number_of_rows() - 1) {
8296 os << ", ";
8297 }
8298 n++;
8299 }
8300 if (x.number_of_rows() != 1) {
8301 os << "}";
8302 }
8303 return os;
8304 }
8305
8319 //! (default: \c 72).
8320 //!
8321 //! \throws LibsemigroupsException if \p braces does not have size \c 2.
8322 template <typename Mat>
8323 auto to_human_readable_repr(Mat const& x,
8324 std::string const& prefix,
8325 std::string const& short_name = "",
8326 std::string const& braces = "{}",
8327 size_t max_width = 72)
8328 -> std::enable_if_t<IsMatrix<Mat>, std::string> {
8329 if (braces.size() != 2) {
8331 "the 4th argument (braces) must have size 2, found {}",
8332 braces.size());
8333 }
8334
8335 size_t const R = x.number_of_rows();
8336 size_t const C = x.number_of_cols();
8337
8338 std::vector<size_t> max_col_widths(C, 0);
8339 std::vector<size_t> row_widths(C, prefix.size() + 1);
8340 for (size_t r = 0; r < R; ++r) {
8341 for (size_t c = 0; c < C; ++c) {
8342 size_t width
8343 = detail::unicode_string_length(detail::entry_repr(x(r, c)));
8344 row_widths[r] += width;
8345 if (width > max_col_widths[c]) {
8346 max_col_widths[c] = width;
8347 }
8348 }
8349 }
8350 auto col_width
8351 = *std::max_element(max_col_widths.begin(), max_col_widths.end());
8352 // The total width if we pad the entries according to the widest column.
8353 auto const total_width = col_width * C + prefix.size() + 1;
8354 if (total_width > max_width) {
8355 // Padding according to the widest column is too wide!
8356 if (*std::max_element(row_widths.begin(), row_widths.end()) > max_width) {
8357 // If the widest row is too wide, then use the short name
8358 return fmt::format(
8359 "<{}x{} {}>", x.number_of_rows(), x.number_of_cols(), short_name);
8360 }
8361 // If the widest row is not too wide, then just don't pad the entries
8362 col_width = 0;
8363 }
8364
8365 std::string result = fmt::format("{}", prefix);
8366 std::string rindent;
8367 auto const lbrace = braces[0], rbrace = braces[1];
8368 if (R != 0 && C != 0) {
8369 result += lbrace;
8370 for (size_t r = 0; r < R; ++r) {
8371 result += fmt::format("{}{}", rindent, lbrace);
8372 rindent = std::string(prefix.size() + 1, ' ');
8373 std::string csep = "";
8374 for (size_t c = 0; c < C; ++c) {
8375 result += fmt::format(
8376 "{}{:>{}}", csep, detail::entry_repr(x(r, c)), col_width);
8377 csep = ", ";
8378 }
8379 result += fmt::format("{}", rbrace);
8380 if (r != R - 1) {
8381 result += ",\n";
8382 }
8383 }
8384 result += rbrace;
8385 }
8386 result += ")";
8387 return result;
8388 }
8389
8391 // Adapters
8393
8408
8416 //! satisfying \ref IsMatrix<Mat>.
8417 //!
8418 //! \tparam Mat the type of matrices.
8419 template <typename Mat>
8420 struct Complexity<Mat, std::enable_if_t<IsMatrix<Mat>>> {
8430 //! \noexcept
8431 //!
8432 //! \complexity
8433 //! Constant.
8434 constexpr size_t operator()(Mat const& x) const noexcept {
8435 return x.number_of_rows() * x.number_of_rows() * x.number_of_rows();
8436 }
8437 };
8438
8446 //! \ref IsMatrix<Mat>.
8447 //!
8448 //! \tparam Mat the type of matrices.
8449 template <typename Mat>
8450 struct Degree<Mat, std::enable_if_t<IsMatrix<Mat>>> {
8459 //! \noexcept
8460 //!
8461 //! \complexity
8462 //! Constant.
8463 constexpr size_t operator()(Mat const& x) const noexcept {
8464 return x.number_of_rows();
8465 }
8466 };
8467
8475 //! \ref IsMatrix<Mat>.
8476 //!
8477 //! \tparam Mat the type of matrices.
8478 template <typename Mat>
8479 struct Hash<Mat, std::enable_if_t<IsMatrix<Mat>>> {
8488 //! \no_libsemigroups_except
8489 //!
8490 //! \complexity
8491 //! Constant.
8492 constexpr size_t operator()(Mat const& x) const {
8493 return x.hash_value();
8494 }
8495 };
8496
8509 //! It is not possible to increase the degree of any of the types
8510 //! satisfying \ref IsMatrix, and as such the call operator of this type
8511 //! does nothing.
8512 template <typename Mat>
8513 struct IncreaseDegree<Mat, std::enable_if_t<IsMatrix<Mat>>> {
8517 constexpr void operator()(Mat&, size_t) const noexcept {
8518 // static_assert(false, "Cannot increase degree for Matrix");
8519 LIBSEMIGROUPS_ASSERT(false);
8520 }
8521 };
8522
8530 //! \ref IsMatrix.
8531 //!
8532 //! \tparam Mat the type of matrices.
8533 template <typename Mat>
8534 struct One<Mat, std::enable_if_t<IsMatrix<Mat>>> {
8544 //!
8545 //! \complexity
8546 //! \f$O(m ^ 2)\f$ where \f$m\f$ is the number of rows of the
8547 //! matrix \p x.
8548 inline Mat operator()(Mat const& x) const {
8549 return x.one();
8550 }
8551 };
8552
8560 //! \ref IsMatrix<Mat>.
8561 //!
8562 //! \tparam Mat the type of matrices.
8563 template <typename Mat>
8564 struct Product<Mat, std::enable_if_t<IsMatrix<Mat>>> {
8580 //!
8581 //! \warning
8582 //! This function only works for square matrices.
8583 inline void
8584 operator()(Mat& xy, Mat const& x, Mat const& y, size_t = 0) const {
8585 xy.product_inplace_no_checks(x, y);
8586 }
8587 };
8588} // namespace libsemigroups
8589
8590namespace std {
8591 template <size_t N,
8592 typename Mat,
8593 std::enable_if_t<libsemigroups::IsMatrix<Mat>>>
8594 inline void swap(Mat& x, Mat& y) noexcept {
8595 x.swap(y);
8596 }
8597} // namespace std
8598
8599#endif // LIBSEMIGROUPS_MATRIX_HPP_
DynamicMatrix(std::initializer_list< scalar_type > const &c)
Construct a vector from a std::initializer_list.
Definition matrix.hpp:2884
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:2906
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:2805
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:2796
PlusOp Plus
Alias for the template parameter PlusOp.
Definition matrix.hpp:2802
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:2792
void swap(DynamicMatrix &that) noexcept
Swaps the contents of *this with the contents of that.
Definition matrix.hpp:3163
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:2985
DynamicMatrix(size_t r, size_t c)
Construct a matrix with given dimensions.
Definition matrix.hpp:2862
ZeroOp Zero
Alias for the template parameter ZeroOp.
Definition matrix.hpp:2808
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:2811
void semiring_type
Alias for the semiring type (void).
Definition matrix.hpp:2818
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:2938
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:2799
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:2924
typename MatrixCommon::scalar_reference scalar_reference
The type of references to the entries in the matrix.
Definition matrix.hpp:2787
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:2784
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:3302
DynamicMatrix & operator=(DynamicMatrix &&)=default
Default move assignment operator.
static DynamicMatrix one(Semiring const *semiring, size_t n)
Construct the identity matrix.
Definition matrix.hpp:3378
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:3324
DynamicMatrix(Semiring const *sr, size_t r, size_t c)
Construct a matrix over a given semiring with given dimensions.
Definition matrix.hpp:3282
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:3239
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:3235
void swap(DynamicMatrix &that) noexcept
Swaps the contents of *this with the contents of that.
Definition matrix.hpp:3558
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:3358
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:3343
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:3230
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:3227
DynamicRowView< Semiring, Scalar > RowView
Alias for the type of row views of a DynamicMatrix.
Definition matrix.hpp:3242
Semiring semiring_type
Alias for the template parameter Semiring.
Definition matrix.hpp:3247
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.
Class representing a truncated max-plus semiring.
Definition matrix.hpp:5107
static constexpr Scalar scalar_zero() noexcept
Get the additive identity.
Definition matrix.hpp:5181
Scalar plus_no_checks(Scalar x, Scalar y) const noexcept
Addition in a truncated max-plus semiring.
Definition matrix.hpp:5244
Scalar product_no_checks(Scalar x, Scalar y) const noexcept
Multiplication in a truncated max-plus semiring.
Definition matrix.hpp:5209
MaxPlusTruncSemiring()=delete
Deleted default constructor.
Scalar threshold() const noexcept
Get the threshold.
Definition matrix.hpp:5269
static constexpr Scalar scalar_one() noexcept
Get the multiplicative identity.
Definition matrix.hpp:5167
Class representing a truncated min-plus semiring.
Definition matrix.hpp:5584
static constexpr Scalar scalar_zero() noexcept
Get the additive identity.
Definition matrix.hpp:5657
Scalar plus_no_checks(Scalar x, Scalar y) const noexcept
Addition in a truncated min-plus semiring.
Definition matrix.hpp:5720
Scalar product_no_checks(Scalar x, Scalar y) const noexcept
Multiplication in a truncated min-plus semiring.
Definition matrix.hpp:5685
Scalar threshold() const noexcept
Get the threshold.
Definition matrix.hpp:5745
static constexpr Scalar scalar_one() noexcept
Get the multiplicative identity.
Definition matrix.hpp:5642
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:6208
Scalar plus_no_checks(Scalar x, Scalar y) const noexcept
Addition in an ntp semiring.
Definition matrix.hpp:6266
NTPSemiring()=delete
Deleted default constructor.
Scalar product_no_checks(Scalar x, Scalar y) const noexcept
Multiplication in an ntp semiring.
Definition matrix.hpp:6236
Scalar period() const noexcept
Get the period.
Definition matrix.hpp:6300
Scalar threshold() const noexcept
Get the threshold.
Definition matrix.hpp:6284
static constexpr Scalar scalar_one() noexcept
Get the multiplicative identity.
Definition matrix.hpp:6192
Static matrix class.
Definition matrix.hpp:1859
typename MatrixCommon::iterator iterator
Definition matrix.hpp:1906
StaticMatrix(std::initializer_list< scalar_type > const &c)
Construct a row (from std::initializer_list).
Definition matrix.hpp:1934
typename MatrixCommon::const_iterator const_iterator
Definition matrix.hpp:1909
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:1884
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:1960
StaticRowView< PlusOp, ProdOp, ZeroOp, OneOp, C, Scalar > RowView
Definition matrix.hpp:1891
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:1996
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:1976
StaticMatrix< PlusOp, ProdOp, ZeroOp, OneOp, 1, C, Scalar > Row
Alias for the type of the rows of a StaticMatrix.
Definition matrix.hpp:1888
StaticMatrix()=default
Default constructor.
typename MatrixCommon::scalar_reference scalar_reference
Alias for references to the template parameter Scalar.
Definition matrix.hpp:1879
typename MatrixCommon::scalar_type scalar_type
Alias for the template parameter Scalar.
Definition matrix.hpp:1876
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(AhoCorasick const &ac)
Return a string representation.
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:3940
static constexpr bool IsBMat
Helper to check if a type is BMat.
Definition matrix.hpp:3998
DynamicMatrix< BooleanPlus, BooleanProd, BooleanZero, BooleanOne, int > DynamicBMat
Alias for dynamic boolean matrices.
Definition matrix.hpp:3926
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:4043
std::conditional_t< R==0||C==0, DynamicBMat, StaticBMat< R, C > > BMat
Alias template for boolean matrices.
Definition matrix.hpp:3963
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:95
std::conditional_t< R==0||C==0, DynamicIntMat< Scalar >, StaticIntMat< R, C, Scalar > > IntMat
Alias template for integer matrices.
Definition matrix.hpp:4287
DynamicMatrix< IntegerPlus< Scalar >, IntegerProd< Scalar >, IntegerZero< Scalar >, IntegerOne< Scalar >, Scalar > DynamicIntMat
Alias for dynamic integer matrices.
Definition matrix.hpp:4239
StaticMatrix< IntegerPlus< Scalar >, IntegerProd< Scalar >, IntegerZero< Scalar >, IntegerOne< Scalar >, R, C, Scalar > StaticIntMat
Alias for static integer matrices.
Definition matrix.hpp:4262
enable_if_is_same< Return, Blocks > make(Container const &cont)
Check the arguments, construct a Blocks object, and check it.
Definition bipart.hpp:798
static constexpr bool IsMaxPlusMat
Helper variable template.
Definition matrix.hpp:4613
constexpr bool IsStaticMatrix
Helper variable template.
Definition matrix.hpp:3636
constexpr bool IsDynamicMatrix
Helper variable template.
Definition matrix.hpp:3649
static constexpr bool IsIntMat
Helper variable template.
Definition matrix.hpp:4312
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:7933
static constexpr bool IsMatWithSemiring
Helper variable template.
Definition matrix.hpp:3663
static constexpr bool IsMinPlusMat
Helper variable template.
Definition matrix.hpp:4921
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:4562
DynamicMatrix< MaxPlusPlus< Scalar >, MaxPlusProd< Scalar >, MaxPlusZero< Scalar >, IntegerZero< Scalar >, Scalar > DynamicMaxPlusMat
Alias for dynamic max-plus matrices.
Definition matrix.hpp:4543
std::conditional_t< R==0||C==0, DynamicMaxPlusMat< Scalar >, StaticMaxPlusMat< R, C, Scalar > > MaxPlusMat
Alias template for max-plus matrices.
Definition matrix.hpp:4586
DynamicMatrix< MaxPlusPlus< Scalar >, MaxPlusTruncProd< T, Scalar >, MaxPlusZero< Scalar >, IntegerZero< Scalar >, Scalar > DynamicMaxPlusTruncMat
Alias for dynamic truncated max-plus matrices.
Definition matrix.hpp:5290
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:5336
StaticMatrix< MaxPlusPlus< Scalar >, MaxPlusTruncProd< T, Scalar >, MaxPlusZero< Scalar >, IntegerZero< Scalar >, R, C, Scalar > StaticMaxPlusTruncMat
Alias for static truncated max-plus matrices.
Definition matrix.hpp:5310
static constexpr bool IsMaxPlusTruncMat
Helper to check if a type is MaxPlusTruncMat.
Definition matrix.hpp:5379
DynamicMatrix< MinPlusPlus< Scalar >, MinPlusProd< Scalar >, MinPlusZero< Scalar >, IntegerZero< Scalar >, Scalar > DynamicMinPlusMat
Alias for dynamic min-plus matrices.
Definition matrix.hpp:4851
StaticMatrix< MinPlusPlus< Scalar >, MinPlusProd< Scalar >, MinPlusZero< Scalar >, IntegerZero< Scalar >, R, C, Scalar > StaticMinPlusMat
Alias for static min-plus matrices.
Definition matrix.hpp:4870
std::conditional_t< R==0||C==0, DynamicMinPlusMat< Scalar >, StaticMinPlusMat< R, C, Scalar > > MinPlusMat
Alias template for min-plus matrices.
Definition matrix.hpp:4894
DynamicMatrix< MinPlusPlus< Scalar >, MinPlusTruncProd< T, Scalar >, MinPlusZero< Scalar >, IntegerZero< Scalar >, Scalar > DynamicMinPlusTruncMat
Alias for dynamic truncated min-plus matrices.
Definition matrix.hpp:5766
StaticMatrix< MinPlusPlus< Scalar >, MinPlusTruncProd< T, Scalar >, MinPlusZero< Scalar >, IntegerZero< Scalar >, R, C, Scalar > StaticMinPlusTruncMat
Alias for static truncated min-plus matrices.
Definition matrix.hpp:5786
static constexpr bool IsMinPlusTruncMat
Helper to check if a type is MinPlusTruncMat.
Definition matrix.hpp:5856
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:5813
DynamicMatrix< NTPSemiring< Scalar >, Scalar > DynamicNTPMatWithSemiring
Alias for ntp matrices with dynamic threshold and period.
Definition matrix.hpp:6320
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:6336
static constexpr bool IsNTPMat
Helper to check if a type is NTPMat.
Definition matrix.hpp:6440
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:6397
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:6362
std::conditional_t< R==0||C==0, DynamicProjMaxPlusMat< Scalar >, StaticProjMaxPlusMat< R, C, Scalar > > ProjMaxPlusMat
Alias template for projective max-plus matrices.
Definition matrix.hpp:7024
detail::ProjMaxPlusMat< DynamicMaxPlusMat< Scalar > > DynamicProjMaxPlusMat
Alias for dynamic projective max-plus matrices with run-time dimensions.
Definition matrix.hpp:7007
static constexpr bool IsProjMaxPlusMat
Helper to check if a type is ProjMaxPlusMat.
Definition matrix.hpp:7054
detail::ProjMaxPlusMat< StaticMaxPlusMat< R, C, Scalar > > StaticProjMaxPlusMat
Alias for static projective max-plus matrices with compile-time arithmetic and dimensions.
Definition matrix.hpp:6993
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:7452
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:7280
constexpr Scalar period(StaticNTPMat< T, P, R, C, Scalar > const &) noexcept
Returns the period of a static ntp matrix.
Definition matrix.hpp:6476
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:3724
size_t row_space_size(Mat const &x)
Returns the size of the row space of a boolean matrix.
Definition matrix.hpp:7888
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:7209
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:7144
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:7624
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:3874
constexpr bool operator()() const noexcept
Call operator returning the multiplication identity true of the boolean semiring.
Definition matrix.hpp:3885
Function object for addition in the boolean semiring.
Definition matrix.hpp:3820
constexpr bool operator()(bool x, bool y) const noexcept
Call operator for addition.
Definition matrix.hpp:3833
Function object for multiplication in the boolean semiring.
Definition matrix.hpp:3847
constexpr bool operator()(bool x, bool y) const noexcept
Call operator for multiplication.
Definition matrix.hpp:3860
Function object for returning the additive identity.
Definition matrix.hpp:3899
constexpr bool operator()() const noexcept
Call operator returning the additive identity false of the boolean semiring.
Definition matrix.hpp:3910
constexpr size_t operator()(Mat const &x) const noexcept
Call operator.
Definition matrix.hpp:8430
Adapter for the complexity of multiplication.
Definition adapters.hpp:121
constexpr size_t operator()(Mat const &x) const noexcept
Call operator.
Definition matrix.hpp:8459
Adapter for the degree of an element.
Definition adapters.hpp:159
constexpr size_t operator()(Mat const &x) const
Call operator.
Definition matrix.hpp:8488
Adapter for hashing.
Definition adapters.hpp:446
constexpr void operator()(Mat &, size_t) const noexcept
Call operator.
Definition matrix.hpp:8513
Adapter for increasing the degree of an element.
Definition adapters.hpp:199
Function object for returning the multiplicative identity.
Definition matrix.hpp:4213
constexpr Scalar operator()() const noexcept
Call operator returning the integer 1.
Definition matrix.hpp:4223
Function object for addition in the ring of integers.
Definition matrix.hpp:4129
constexpr Scalar operator()(Scalar x, Scalar y) const noexcept
Call operator for addition.
Definition matrix.hpp:4142
Function object for multiplication in the ring of integers.
Definition matrix.hpp:4160
constexpr Scalar operator()(Scalar x, Scalar y) const noexcept
Call operator for multiplication.
Definition matrix.hpp:4173
Function object for returning the additive identity.
Definition matrix.hpp:4188
constexpr Scalar operator()() const noexcept
Call operator returning the integer 0.
Definition matrix.hpp:4198
Function object for addition in the max-plus semiring.
Definition matrix.hpp:4431
Scalar operator()(Scalar x, Scalar y) const noexcept
Call operator for addition.
Definition matrix.hpp:4446
Function object for multiplication in the max-plus semiring.
Definition matrix.hpp:4478
Scalar operator()(Scalar x, Scalar y) const noexcept
Call operator for multiplication.
Definition matrix.hpp:4493
Function object for multiplication in truncated max-plus semirings.
Definition matrix.hpp:5065
Scalar operator()(Scalar x, Scalar y) const noexcept
Call operator for multiplication.
Definition matrix.hpp:5080
Function object for returning the additive identity of the max-plus semiring.
Definition matrix.hpp:4515
constexpr Scalar operator()() const noexcept
Call operator for additive identity.
Definition matrix.hpp:4527
Function object for addition in the min-plus semiring.
Definition matrix.hpp:4739
Scalar operator()(Scalar x, Scalar y) const noexcept
Call operator for addition.
Definition matrix.hpp:4754
Function object for multiplication in the min-plus semiring.
Definition matrix.hpp:4786
Scalar operator()(Scalar x, Scalar y) const noexcept
Call operator for multiplication.
Definition matrix.hpp:4801
Function object for multiplication in min-plus truncated semirings.
Definition matrix.hpp:5545
Scalar operator()(Scalar x, Scalar y) const noexcept
Call operator for multiplication.
Definition matrix.hpp:5558
Function object for returning the additive identity of the min-plus semiring.
Definition matrix.hpp:4823
constexpr Scalar operator()() const noexcept
Call operator for additive identity.
Definition matrix.hpp:4835
Function object for addition in ntp semirings.
Definition matrix.hpp:6052
constexpr Scalar operator()(Scalar x, Scalar y) const noexcept
Call operator for addition.
Definition matrix.hpp:6064
Function object for multiplication in an ntp semirings.
Definition matrix.hpp:6093
constexpr Scalar operator()(Scalar x, Scalar y) const noexcept
Call operator for multiplication.
Definition matrix.hpp:6107
Mat operator()(Mat const &x) const
Call operator.
Definition matrix.hpp:8544
Adapter for the identity element of the given type.
Definition adapters.hpp:246
void operator()(Mat &xy, Mat const &x, Mat const &y, size_t=0) const
Call operator.
Definition matrix.hpp:8580
Adapter for the product of two elements.
Definition adapters.hpp:284
T swap(T... args)
T tie(T... args)
T unique(T... args)