23#ifndef LIBSEMIGROUPS_MATRIX_HPP_
24#define LIBSEMIGROUPS_MATRIX_HPP_
31#include <initializer_list>
39#include <unordered_map>
40#include <unordered_set>
44#include "adapters.hpp"
47#include "constants.hpp"
49#include "exception.hpp"
51#include "detail/containers.hpp"
52#include "detail/formatters.hpp"
53#include "detail/string.hpp"
135 template <
typename T>
136 struct IsStdBitSetHelper : std::false_type {};
139 struct IsStdBitSetHelper<std::bitset<N>> : std::true_type {};
141 template <
typename T>
142 static constexpr bool IsStdBitSet = IsStdBitSetHelper<T>::value;
144 struct MatrixPolymorphicBase {};
146 template <
typename T>
147 struct IsMatrixHelper {
148 static constexpr bool value
149 = std::is_base_of<detail::MatrixPolymorphicBase, T>::value;
167 template <
typename T>
168 constexpr bool IsMatrix = detail::IsMatrixHelper<T>::value;
183 template <
typename Mat>
185 if (x.number_of_rows() != x.number_of_cols()) {
205 template <
typename Mat>
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",
234 template <
typename Mat>
236 -> std::enable_if_t<IsMatrix<Mat>> {
237 if (r >= x.number_of_rows()) {
239 "values in [0, {}) x [0, {})",
246 if (c >= x.number_of_cols()) {
248 "values in [0, {}) x [0, {})",
262 template <
typename Container,
265 typename Semiring =
void>
266 class MatrixCommon : MatrixPolymorphicBase {
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;
277 using container_type = Container;
278 using iterator =
typename Container::iterator;
279 using const_iterator =
typename Container::const_iterator;
281 using RowView = TRowView;
283 scalar_type scalar_one() const noexcept {
284 return static_cast<Subclass const*
>(
this)->one_impl();
287 scalar_type scalar_zero() const noexcept {
288 return static_cast<Subclass const*
>(
this)->zero_impl();
291 Semiring
const* semiring() const noexcept {
292 return static_cast<Subclass const*
>(
this)->semiring_impl();
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);
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);
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);
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> {}
331 MatrixCommon() =
default;
332 MatrixCommon(MatrixCommon
const&) =
default;
333 MatrixCommon(MatrixCommon&&) =
default;
334 MatrixCommon& operator=(MatrixCommon
const&) =
default;
335 MatrixCommon& operator=(MatrixCommon&&) =
default;
337 explicit MatrixCommon(std::initializer_list<scalar_type>
const& c)
343 explicit MatrixCommon(std::vector<std::vector<scalar_type>>
const& m)
349 std::initializer_list<std::initializer_list<scalar_type>>
const& m)
356 template <
typename T>
357 void init(T
const& m) {
358 size_t const R = m.size();
362 size_t const C = m.begin()->size();
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);
374 init(std::initializer_list<std::initializer_list<scalar_type>>
const& m) {
375 init<std::initializer_list<std::initializer_list<scalar_type>>>(m);
379 explicit MatrixCommon(RowView
const& rv) : MatrixCommon() {
380 resize(1, rv.size());
381 std::copy(rv.cbegin(), rv.cend(), _container.begin());
384 ~MatrixCommon() =
default;
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();
402 bool operator==(MatrixCommon
const& that)
const {
403 return _container == that._container;
407 bool operator==(RowView
const& that)
const {
408 return number_of_rows() == 1
409 &&
static_cast<RowView
>(*
static_cast<Subclass const*
>(
this))
414 bool operator<(MatrixCommon
const& that)
const {
415 return _container < that._container;
419 bool operator<(RowView
const& that)
const {
420 return number_of_rows() == 1
421 &&
static_cast<RowView
>(*
static_cast<Subclass const*
>(
this))
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);
433 template <
typename T>
434 bool operator>(T
const& that)
const {
435 static_assert(
IsMatrix<T> || std::is_same_v<T, RowView>);
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;
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;
459 scalar_reference operator()(
size_t r,
size_t c) {
460 return this->_container[r * number_of_cols() + c];
463 scalar_reference at(
size_t r,
size_t c) {
465 return this->operator()(r, c);
470 scalar_const_reference operator()(
size_t r,
size_t c)
const {
471 return this->_container[r * number_of_cols() + c];
474 scalar_const_reference at(
size_t r,
size_t c)
const {
476 return this->operator()(r, c);
480 size_t number_of_rows() const noexcept {
481 return static_cast<Subclass const*
>(
this)->number_of_rows_impl();
485 size_t number_of_cols() const noexcept {
486 return static_cast<Subclass const*
>(
this)->number_of_cols_impl();
490 size_t hash_value()
const {
491 return Hash<Container>()(_container);
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);
514 size_t const N = A.number_of_rows();
515 std::vector<scalar_type> tmp(N, 0);
517 for (
size_t c = 0; c < N; c++) {
518 for (
size_t i = 0; i < N; i++) {
521 for (
size_t r = 0; r < N; r++) {
523 A._container.begin() + r * N,
524 A._container.begin() + (r + 1) * N,
527 [
this](scalar_type x, scalar_type y) {
528 return this->plus_no_checks(x, y);
530 [
this](scalar_type x, scalar_type y) {
531 return this->product_no_checks(x, y);
538 void operator*=(scalar_type a) {
539 for (
auto it = _container.begin(); it < _container.end(); ++it) {
540 *it = product_no_checks(*it, a);
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]);
553 void operator+=(RowView
const& that) {
554 LIBSEMIGROUPS_ASSERT(number_of_rows() == 1);
555 RowView(*
static_cast<Subclass const*
>(
this)) += that;
558 void operator+=(scalar_type a) {
559 for (
auto it = _container.begin(); it < _container.end(); ++it) {
560 *it = plus_no_checks(*it, a);
571 Subclass operator+(Subclass
const& y)
const {
572 Subclass result(*
static_cast<Subclass const*
>(
this));
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),
585 Subclass operator*(scalar_type a)
const {
586 Subclass result(*
static_cast<Subclass const*
>(
this));
591 Subclass operator+(scalar_type a)
const {
592 Subclass result(*
static_cast<Subclass const*
>(
this));
602 iterator begin() noexcept {
603 return _container.begin();
607 iterator end() noexcept {
608 return _container.end();
612 const_iterator begin() const noexcept {
613 return _container.begin();
617 const_iterator end() const noexcept {
618 return _container.end();
622 const_iterator cbegin() const noexcept {
623 return _container.cbegin();
627 const_iterator cend() const noexcept {
628 return _container.cend();
631 template <
typename U>
632 std::pair<scalar_type, scalar_type> coords(U
const& it)
const {
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");
638 return std::make_pair(v / number_of_cols(), v % number_of_cols());
646 void swap(MatrixCommon& that)
noexcept {
652 void transpose_no_checks() noexcept {
653 LIBSEMIGROUPS_ASSERT(number_of_rows() == number_of_cols());
654 if (number_of_rows() == 0) {
658 for (
size_t r = 0; r < number_of_rows() - 1; ++r) {
659 for (
size_t c = r + 1; c < number_of_cols(); ++c) {
667 transpose_no_checks();
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(),
682 RowView row(
size_t i)
const {
683 if (i >= number_of_rows()) {
685 "index out of range, expected value in [{}, {}), found {}",
690 return row_no_checks(i);
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()) {
700 static_cast<Subclass const*
>(
this), itc, number_of_cols());
702 LIBSEMIGROUPS_ASSERT(x.size() == number_of_rows());
709 friend std::ostream& operator<<(std::ostream& os, MatrixCommon
const& x) {
710 os << detail::to_string(x);
718 container_type _container;
721 template <
typename Scalar>
722 class MatrixDynamicDim {
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;
730 MatrixDynamicDim(
size_t r,
size_t c)
731 : _number_of_cols(c), _number_of_rows(r) {}
733 ~MatrixDynamicDim() =
default;
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);
741 size_t number_of_rows_impl() const noexcept {
742 return _number_of_rows;
745 size_t number_of_cols_impl() const noexcept {
746 return _number_of_cols;
750 size_t _number_of_cols;
751 size_t _number_of_rows;
754 template <
typename PlusOp,
759 struct MatrixStaticArithmetic {
760 MatrixStaticArithmetic() =
default;
761 MatrixStaticArithmetic(MatrixStaticArithmetic
const&) =
default;
762 MatrixStaticArithmetic(MatrixStaticArithmetic&&) =
default;
763 MatrixStaticArithmetic& operator=(MatrixStaticArithmetic
const&)
765 MatrixStaticArithmetic& operator=(MatrixStaticArithmetic&&) =
default;
769 using scalar_type = Scalar;
771 static constexpr scalar_type plus_no_checks_impl(scalar_type x,
772 scalar_type y)
noexcept {
773 return PlusOp()(x, y);
776 static constexpr scalar_type
777 product_no_checks_impl(scalar_type x, scalar_type y)
noexcept {
778 return ProdOp()(x, y);
781 static constexpr scalar_type one_impl() noexcept {
785 static constexpr scalar_type zero_impl() noexcept {
789 static constexpr void const* semiring_impl() noexcept {
798 template <
typename Mat,
typename Sub
class>
799 class RowViewCommon {
801 "the template parameter Mat must be derived from "
802 "MatrixPolymorphicBase");
805 using const_iterator =
typename Mat::const_iterator;
806 using iterator =
typename Mat::iterator;
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;
812 using Row =
typename Mat::Row;
813 using matrix_type = Mat;
815 size_t size() const noexcept {
816 return static_cast<Subclass const*
>(
this)->length_impl();
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);
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);
830 RowViewCommon() =
default;
831 RowViewCommon(RowViewCommon
const&) =
default;
832 RowViewCommon(RowViewCommon&&) =
default;
833 RowViewCommon& operator=(RowViewCommon
const&) =
default;
834 RowViewCommon& operator=(RowViewCommon&&) =
default;
836 explicit RowViewCommon(Row
const& r)
837 : RowViewCommon(const_cast<Row&>(r).begin()) {}
840 scalar_const_reference operator[](
size_t i)
const {
845 scalar_reference operator[](
size_t i) {
850 scalar_const_reference operator()(
size_t i)
const {
855 scalar_reference operator()(
size_t i) {
860 const_iterator cbegin() const noexcept {
865 const_iterator cend()
const {
866 return _begin + size();
870 const_iterator begin() const noexcept {
875 const_iterator end()
const {
876 return _begin + size();
880 iterator begin() noexcept {
885 iterator end() noexcept {
886 return _begin + size();
894 void operator+=(RowViewCommon
const& x) {
896 for (
size_t i = 0; i < size(); ++i) {
897 this_[i] = plus_no_checks(this_[i], x[i]);
902 void operator+=(scalar_type a) {
903 for (
auto& x : *
this) {
904 x = plus_no_checks(x, a);
909 void operator*=(scalar_type a) {
910 for (
auto& x : *
this) {
911 x = product_no_checks(x, a);
916 Row operator*(scalar_type a)
const {
917 Row result(*
static_cast<Subclass const*
>(
this));
923 Row operator+(RowViewCommon
const& that)
const {
924 Row result(*
static_cast<Subclass const*
>(
this));
925 result +=
static_cast<Subclass const&
>(that);
929 template <
typename U>
930 bool operator==(U
const& that)
const {
932 return std::equal(begin(), end(), that.begin());
935 template <
typename U>
936 bool operator!=(U
const& that)
const {
937 return !(*
this == that);
940 template <
typename U>
941 bool operator<(U
const& that)
const {
943 cbegin(), cend(), that.cbegin(), that.cend());
946 template <
typename U>
947 bool operator>(U
const& that)
const {
951 void swap(RowViewCommon& that)
noexcept {
955 friend std::ostream& operator<<(std::ostream& os,
956 RowViewCommon
const& x) {
957 os << detail::to_string(x);
962 explicit RowViewCommon(iterator first) : _begin(first) {}
968 template <
typename Container>
969 void throw_if_any_row_wrong_size(Container
const& m) {
973 uint64_t
const C = m.begin()->size();
975 m.begin() + 1, m.end(), [&C](
typename Container::const_reference r) {
976 return r.size() == C;
980 "have length {}, found {} in entry {}",
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);
1000#ifndef LIBSEMIGROUPS_PARSED_BY_DOXYGEN
1001 template <
typename PlusOp,
1010 template <
typename... Args>
1011 class DynamicMatrix;
1013 template <
typename PlusOp,
1018 class DynamicMatrix<PlusOp, ProdOp, ZeroOp, OneOp, Scalar>;
1020 template <
typename Semiring,
typename Scalar>
1021 class DynamicMatrix<Semiring, Scalar>;
1069 template <
typename PlusOp,
1076 :
public detail::RowViewCommon<
1077 StaticMatrix<PlusOp, ProdOp, ZeroOp, OneOp, 1, C, Scalar>,
1078 StaticRowView<PlusOp, ProdOp, ZeroOp, OneOp, C, Scalar>>,
1080 MatrixStaticArithmetic<PlusOp, ProdOp, ZeroOp, OneOp, Scalar> {
1082 using RowViewCommon = detail::RowViewCommon<
1085 friend RowViewCommon;
1088 using StaticMatrix_ = ::libsemigroups::
1089 StaticMatrix<PlusOp, ProdOp, ZeroOp, OneOp, R, C, Scalar>;
1114 using Row =
typename matrix_type::Row;
1131#ifndef LIBSEMIGROUPS_PARSED_BY_DOXYGEN
1132 using RowViewCommon::RowViewCommon;
1137 typename RowViewCommon::iterator it,
1139 : RowViewCommon(it) {}
1141 using RowViewCommon::size;
1168 static constexpr size_t size() const noexcept;
1292 template <typename U>
1293 bool operator==(U const& that) const;
1315 template <typename U>
1316 bool operator!=(U const& that) const;
1341 template <typename U>
1342 bool operator<(U const& that) const;
1365 template <typename U>
1366 bool operator<(U const& that) const;
1460 static constexpr size_t length_impl() noexcept {
1469#ifndef LIBSEMIGROUPS_PARSED_BY_DOXYGEN
1472 template <
typename... Args>
1473 class DynamicRowView;
1513 template <
typename PlusOp,
1519 :
public detail::RowViewCommon<
1520 DynamicMatrix<PlusOp, ProdOp, ZeroOp, OneOp, Scalar>,
1521 DynamicRowView<PlusOp, ProdOp, ZeroOp, OneOp, Scalar>>,
1523 MatrixStaticArithmetic<PlusOp, ProdOp, ZeroOp, OneOp, Scalar> {
1525 using DynamicMatrix_ = DynamicMatrix<PlusOp, ProdOp, ZeroOp, OneOp, Scalar>;
1526 using RowViewCommon = detail::RowViewCommon<
1529 friend RowViewCommon;
1554 using Row =
typename matrix_type::Row;
1573 : RowViewCommon(r), _length(r.number_of_cols()) {}
1575#ifndef LIBSEMIGROUPS_PARSED_BY_DOXYGEN
1576 using RowViewCommon::RowViewCommon;
1579 DynamicRowView(DynamicMatrix_
const*, iterator
const& it,
size_t N)
1580 : RowViewCommon(it), _length(N) {}
1582 using RowViewCommon::size;
1612 template <typename U>
1613 bool operator==(U const& that) const;
1616 template <typename U>
1617 bool operator!=(U const& that) const;
1620 template <typename U>
1621 bool operator<(U const& that) const;
1624 template <typename U>
1625 bool operator<(U const& that) const;
1644 size_t length_impl() const noexcept {
1674 template <
typename Semiring,
typename Scalar>
1676 :
public detail::RowViewCommon<DynamicMatrix<Semiring, Scalar>,
1677 DynamicRowView<Semiring, Scalar>> {
1679 using DynamicMatrix_ = DynamicMatrix<Semiring, Scalar>;
1680 friend DynamicMatrix_;
1682 = detail::RowViewCommon<DynamicMatrix_,
1684 friend RowViewCommon;
1691 using iterator =
typename RowViewCommon::iterator;
1706 using matrix_type =
typename RowViewCommon::matrix_type;
1709 using Row =
typename matrix_type::Row;
1729#ifndef LIBSEMIGROUPS_PARSED_BY_DOXYGEN
1730 using RowViewCommon::RowViewCommon;
1733 DynamicRowView(DynamicMatrix_
const* mat, iterator
const& it,
size_t)
1734 : RowViewCommon(it), _matrix(mat) {}
1736 using RowViewCommon::size;
1739 size_t size() const noexcept;
1766 template <typename U>
1767 bool operator==(U const& that) const;
1770 template <typename U>
1771 bool operator!=(U const& that) const;
1774 template <typename U>
1775 bool operator<(U const& that) const;
1778 template <typename U>
1779 bool operator<(U const& that) const;
1798 size_t length_impl() const noexcept {
1799 return _matrix->number_of_cols();
1802 scalar_type plus_no_checks_impl(scalar_type x,
1803 scalar_type y)
const noexcept {
1804 return _matrix->plus_no_checks_impl(x, y);
1807 scalar_type product_no_checks_impl(scalar_type x,
1808 scalar_type y)
const noexcept {
1809 return _matrix->product_no_checks_impl(x, y);
1812 DynamicMatrix_
const* _matrix;
1852 template <
typename PlusOp,
1861 MatrixStaticArithmetic<PlusOp, ProdOp, ZeroOp, OneOp, Scalar>,
1862 public detail::MatrixCommon<
1863 std::array<Scalar, R * C>,
1864 StaticMatrix<PlusOp, ProdOp, ZeroOp, OneOp, R, C, Scalar>,
1865 StaticRowView<PlusOp, ProdOp, ZeroOp, OneOp, C, Scalar>> {
1870 using MatrixCommon = ::libsemigroups::detail::MatrixCommon<
1874 friend MatrixCommon;
1917 static constexpr size_t nr_rows = R;
1918 static constexpr size_t nr_cols = C;
1942 static_assert(R == 1,
1943 "cannot construct Matrix from the given initializer list, "
1944 "incompatible dimensions");
1945 LIBSEMIGROUPS_ASSERT(c.
size() == C);
1969 : MatrixCommon(m) {}
1984 : MatrixCommon(m) {}
2006 "cannot construct Matrix with more than one row from RowView!");
2034#ifndef LIBSEMIGROUPS_PARSED_BY_DOXYGEN
2047 LIBSEMIGROUPS_ASSERT(ptr ==
nullptr);
2056 LIBSEMIGROUPS_ASSERT(ptr ==
nullptr);
2063 LIBSEMIGROUPS_ASSERT(ptr ==
nullptr);
2069 LIBSEMIGROUPS_ASSERT(ptr ==
nullptr);
2075#ifndef LIBSEMIGROUPS_PARSED_BY_DOXYGEN
2079 LIBSEMIGROUPS_ASSERT(n == 0 || n == R);
2081#if defined(__APPLE__) && defined(__clang__) \
2082 && (__clang_major__ == 13 || __clang_major__ == 14)
2087 size_t volatile const m = R;
2092 std::fill(x.begin(), x.end(), ZeroOp()());
2093 for (
size_t r = 0; r < m; ++r) {
2094 x(r, r) = OneOp()();
2101 LIBSEMIGROUPS_ASSERT(ptr ==
nullptr);
2102 LIBSEMIGROUPS_ASSERT(n == 0 || n == R);
2110#ifdef LIBSEMIGROUPS_PARSED_BY_DOXYGEN
2510 template <typename T>
2599 template <typename U>
2600 bool operator<=(U const& that) const;
2616 template <typename U>
2617 bool operator>=(U const& that) const;
2686 using MatrixCommon::at;
2687 using MatrixCommon::begin;
2688 using MatrixCommon::cbegin;
2689 using MatrixCommon::cend;
2690 using MatrixCommon::coords;
2691 using MatrixCommon::end;
2692 using MatrixCommon::hash_value;
2693 using MatrixCommon::number_of_cols;
2694 using MatrixCommon::number_of_rows;
2695 using MatrixCommon::one;
2696 using MatrixCommon::operator!=;
2697 using MatrixCommon::operator();
2698 using MatrixCommon::operator*;
2699 using MatrixCommon::operator*=;
2700 using MatrixCommon::operator+;
2701 using MatrixCommon::operator+=;
2702 using MatrixCommon::operator<;
2703 using MatrixCommon::operator<=;
2704 using MatrixCommon::operator==;
2705 using MatrixCommon::operator>;
2706 using MatrixCommon::operator>=;
2707 using MatrixCommon::product_inplace_no_checks;
2708 using MatrixCommon::row;
2709 using MatrixCommon::row_no_checks;
2710 using MatrixCommon::rows;
2711 using MatrixCommon::scalar_one;
2712 using MatrixCommon::scalar_zero;
2713 using MatrixCommon::semiring;
2714 using MatrixCommon::swap;
2715 using MatrixCommon::transpose;
2716 using MatrixCommon::transpose_no_checks;
2724 static constexpr size_t number_of_rows_impl() noexcept {
2727 static constexpr size_t number_of_cols_impl() noexcept {
2769 template <
typename PlusOp,
2775 :
public detail::MatrixDynamicDim<Scalar>,
2776 public detail::MatrixCommon<
2777 std::vector<Scalar>,
2778 DynamicMatrix<PlusOp, ProdOp, ZeroOp, OneOp, Scalar>,
2779 DynamicRowView<PlusOp, ProdOp, ZeroOp, OneOp, Scalar>>,
2781 MatrixStaticArithmetic<PlusOp, ProdOp, ZeroOp, OneOp, Scalar> {
2782 using MatrixDynamicDim = ::libsemigroups::detail::MatrixDynamicDim<Scalar>;
2783 using MatrixCommon = ::libsemigroups::detail::MatrixCommon<
2786 DynamicRowView<PlusOp, ProdOp, ZeroOp, OneOp, Scalar>>;
2787 friend MatrixCommon;
2806 using RowView = DynamicRowView<PlusOp, ProdOp, ZeroOp, OneOp, Scalar>;
2894 : MatrixDynamicDim(1, c.size()), MatrixCommon(c) {}
2918 : MatrixDynamicDim(m.size(),
std::empty(m) ? 0 : m.
begin()->size()),
2936 : MatrixDynamicDim(m.size(),
std::empty(m) ? 0 : m.
begin()->size()),
2951 : MatrixDynamicDim(1, rv.size()), MatrixCommon(rv) {}
2953#ifndef LIBSEMIGROUPS_PARSED_BY_DOXYGEN
2955 DynamicMatrix(
void const* ptr,
size_t r,
size_t c) : DynamicMatrix(r, c) {
2957 LIBSEMIGROUPS_ASSERT(ptr ==
nullptr);
2962 : DynamicMatrix(c) {
2964 LIBSEMIGROUPS_ASSERT(ptr ==
nullptr);
2970 std::initializer_list<std::initializer_list<scalar_type>>
const& m)
2971 : DynamicMatrix(m) {
2973 LIBSEMIGROUPS_ASSERT(ptr ==
nullptr);
2976 static DynamicMatrix
one(
void const* ptr,
size_t n) {
2978 LIBSEMIGROUPS_ASSERT(ptr ==
nullptr);
2983 ~DynamicMatrix() =
default;
2999 std::fill(x.begin(), x.end(), ZeroOp()());
3000 for (
size_t r = 0; r < n; ++r) {
3001 x(r, r) = OneOp()();
3006#ifdef LIBSEMIGROUPS_PARSED_BY_DOXYGEN
3096 template <typename T>
3097 bool operator<=(T const& that) const;
3109 template <typename T>
3110 bool operator>=(T const& that) const;
3123 template <typename T>
3141 using MatrixCommon::at;
3142 using MatrixCommon::begin;
3143 using MatrixCommon::cbegin;
3144 using MatrixCommon::cend;
3145 using MatrixCommon::coords;
3146 using MatrixCommon::end;
3147 using MatrixCommon::hash_value;
3148 using MatrixCommon::number_of_cols;
3149 using MatrixCommon::number_of_rows;
3150 using MatrixCommon::one;
3151 using MatrixCommon::operator!=;
3152 using MatrixCommon::operator();
3153 using MatrixCommon::operator*;
3154 using MatrixCommon::operator*=;
3155 using MatrixCommon::operator+;
3156 using MatrixCommon::operator+=;
3157 using MatrixCommon::operator<;
3158 using MatrixCommon::operator<=;
3159 using MatrixCommon::operator==;
3160 using MatrixCommon::operator>;
3161 using MatrixCommon::operator>=;
3162 using MatrixCommon::product_inplace_no_checks;
3163 using MatrixCommon::row;
3164 using MatrixCommon::row_no_checks;
3165 using MatrixCommon::rows;
3166 using MatrixCommon::scalar_one;
3167 using MatrixCommon::scalar_zero;
3168 using MatrixCommon::semiring;
3170 using MatrixCommon::transpose;
3171 using MatrixCommon::transpose_no_checks;
3176 static_cast<MatrixDynamicDim&
>(*this).swap(
3177 static_cast<MatrixDynamicDim&
>(that));
3178 static_cast<MatrixCommon&
>(*this).swap(
static_cast<MatrixCommon&
>(that));
3182 using MatrixCommon::resize;
3223 template <
typename Semiring,
typename Scalar>
3225 :
public detail::MatrixDynamicDim<Scalar>,
3226 public detail::MatrixCommon<std::vector<Scalar>,
3227 DynamicMatrix<Semiring, Scalar>,
3228 DynamicRowView<Semiring, Scalar>,
3230 using MatrixCommon = detail::MatrixCommon<std::vector<Scalar>,
3232 DynamicRowView<Semiring, Scalar>,
3234 friend MatrixCommon;
3235 using MatrixDynamicDim = ::libsemigroups::detail::MatrixDynamicDim<Scalar>;
3295 : MatrixDynamicDim(r, c), MatrixCommon(), _semiring(sr) {
3317 : MatrixDynamicDim(rws.size(),
3318 std::empty(rws) ? 0 : rws.
begin()->size()),
3339 : MatrixDynamicDim(rws.size(), (rws.empty() ? 0 : rws.
begin()->size())),
3358 : MatrixDynamicDim(1, rw.size()), MatrixCommon(rw), _semiring(sr) {}
3372 : MatrixDynamicDim(1, rv.size()),
3374 _semiring(rv._matrix->
semiring()) {}
3393 std::fill(x.begin(), x.end(), x.scalar_zero());
3394 for (
size_t r = 0; r < n; ++r) {
3395 x(r, r) = x.scalar_one();
3400 ~DynamicMatrix() =
default;
3402#ifdef LIBSEMIGROUPS_PARSED_BY_DOXYGEN
3492 template <typename T>
3493 bool operator<=(T const& that) const;
3505 template <typename T>
3506 bool operator>=(T const& that) const;
3519 template <typename T>
3537 using MatrixCommon::at;
3538 using MatrixCommon::begin;
3539 using MatrixCommon::cbegin;
3540 using MatrixCommon::cend;
3541 using MatrixCommon::coords;
3542 using MatrixCommon::end;
3543 using MatrixCommon::hash_value;
3544 using MatrixCommon::number_of_cols;
3545 using MatrixCommon::number_of_rows;
3546 using MatrixCommon::one;
3547 using MatrixCommon::operator!=;
3548 using MatrixCommon::operator();
3549 using MatrixCommon::operator*;
3550 using MatrixCommon::operator*=;
3551 using MatrixCommon::operator+;
3552 using MatrixCommon::operator+=;
3553 using MatrixCommon::operator<;
3554 using MatrixCommon::operator<=;
3555 using MatrixCommon::operator==;
3556 using MatrixCommon::operator>;
3557 using MatrixCommon::operator>=;
3558 using MatrixCommon::product_inplace_no_checks;
3559 using MatrixCommon::row;
3560 using MatrixCommon::row_no_checks;
3561 using MatrixCommon::rows;
3562 using MatrixCommon::scalar_one;
3563 using MatrixCommon::scalar_zero;
3564 using MatrixCommon::semiring;
3566 using MatrixCommon::transpose;
3567 using MatrixCommon::transpose_no_checks;
3572 static_cast<MatrixDynamicDim&
>(*this).swap(
3573 static_cast<MatrixDynamicDim&
>(that));
3574 static_cast<MatrixCommon&
>(*this).swap(
static_cast<MatrixCommon&
>(that));
3579 using MatrixCommon::resize;
3581 scalar_type plus_no_checks_impl(scalar_type x,
3582 scalar_type y)
const noexcept {
3583 return _semiring->plus_no_checks(x, y);
3586 scalar_type product_no_checks_impl(scalar_type x,
3587 scalar_type y)
const noexcept {
3588 return _semiring->product_no_checks(x, y);
3591 scalar_type one_impl() const noexcept {
3592 return _semiring->scalar_one();
3595 scalar_type zero_impl() const noexcept {
3596 return _semiring->scalar_zero();
3599 Semiring
const* semiring_impl() const noexcept {
3603 Semiring
const* _semiring;
3612 template <
typename T>
3613 struct IsStaticMatrixHelper : std::false_type {};
3615 template <
typename PlusOp,
3622 struct IsStaticMatrixHelper<
3623 StaticMatrix<PlusOp, ProdOp, ZeroOp, OneOp, R, C, Scalar>>
3624 : std::true_type {};
3626 template <
typename T>
3627 struct IsMatWithSemiringHelper : std::false_type {};
3629 template <
typename Semiring,
typename Scalar>
3630 struct IsMatWithSemiringHelper<DynamicMatrix<Semiring, Scalar>>
3631 : std::true_type {};
3633 template <
typename S,
typename T =
void>
3634 struct IsTruncMatHelper : std::false_type {};
3648 template <
typename T>
3661 template <
typename T>
3674 template <
typename T>
3676 = detail::IsMatWithSemiringHelper<T>::value;
3680 template <
typename T>
3681 static constexpr bool IsTruncMat = IsTruncMatHelper<T>::value;
3683 template <
typename Mat>
3684 void throw_if_semiring_nullptr(Mat
const& m) {
3687 "the matrix's pointer to a semiring is nullptr!")
3691 template <
typename Mat,
typename Container>
3692 auto throw_if_bad_dim(Container
const& m)
3693 -> std::enable_if_t<IsStaticMatrix<Mat>> {
3695 uint64_t
const R = m.size();
3696 uint64_t
const C = std::empty(m) ? 0 : m.begin()->size();
3697 if (R != Mat::nr_rows || C != Mat::nr_cols) {
3699 "invalid argument, cannot initialize an {}x{} matrix with compile "
3700 "time dimension, with an {}x{} container",
3708 template <
typename Mat,
typename Container>
3709 auto throw_if_bad_dim(Container
const&)
3710 -> std::enable_if_t<IsDynamicMatrix<Mat>> {}
3713 template <
typename Mat,
typename Container>
3714 auto throw_if_bad_row_dim(Container
const& row)
3715 -> std::enable_if_t<IsStaticMatrix<Mat>> {
3716 uint64_t
const C = row.size();
3717 if (C != Mat::nr_cols) {
3719 "invalid argument, cannot initialize a row of a matrix with "
3720 "compile time number of columns {} with a container of size {}",
3726 template <
typename Mat,
typename Container>
3727 auto throw_if_bad_row_dim(Container
const&)
3728 -> std::enable_if_t<IsDynamicMatrix<Mat>> {}
3754 template <
typename Mat>
3756 -> std::enable_if_t<!detail::IsTruncMat<Mat>,
3757 typename Mat::scalar_type> {
3774 template <
typename Mat>
3775 constexpr auto threshold(Mat
const&)
noexcept
3777 typename Mat::scalar_type> {
3778 return detail::IsTruncMatHelper<Mat>::threshold;
3796 template <
typename Mat>
3799 typename Mat::scalar_type> {
3800 return x.semiring()->threshold();
3958 = DynamicMatrix<BooleanPlus, BooleanProd, BooleanZero, BooleanOne, int>;
3971 template <
size_t R,
size_t C>
3994 template <
size_t R = 0,
size_t C = R>
3996 = std::conditional_t<R == 0 || C == 0, DynamicBMat, StaticBMat<R, C>>;
3999 template <
typename T>
4002 template <
size_t R,
size_t C>
4008 template <
typename T>
4009 struct BitSetCapacity {
4010 static constexpr size_t value = BitSet<1>::max_size();
4013 template <
size_t R,
size_t C>
4015 static_assert(R == C,
"the number of rows and columns must be equal");
4016 static constexpr size_t value = R;
4030 template <
typename T>
4031 static constexpr bool IsBMat = detail::IsBMatHelper<T>::value;
4042 template <
typename Scalar>
4044 if constexpr (std::is_same_v<Scalar, NegativeInfinity>
4045 || std::is_signed_v<Scalar>) {
4053 return fmt::format(
"{}", a);
4075 template <
typename Mat>
4077 using scalar_type =
typename Mat::scalar_type;
4079 m.cbegin(), m.cend(), [](scalar_type x) { return x == 0 || x == 1; });
4080 if (it != m.cend()) {
4081 auto [r, c] = m.coords(it);
4083 "invalid entry, expected 0 or 1 but found {} in entry ({}, {})",
4084 detail::entry_repr(*it),
4107 template <
typename Mat>
4108 std::enable_if_t<IsBMat<Mat>>
4110 if (val != 0 && val != 1) {
4112 detail::entry_repr(val));
4161 template <
typename Scalar>
4175 constexpr Scalar
operator()(Scalar x, Scalar y)
const noexcept {
4192 template <
typename Scalar>
4206 constexpr Scalar
operator()(Scalar x, Scalar y)
const noexcept {
4220 template <
typename Scalar>
4231 constexpr Scalar
operator()() const noexcept {
4245 template <
typename Scalar>
4256 constexpr Scalar
operator()() const noexcept {
4271 template <
typename Scalar>
4294 template <
size_t R,
size_t C,
typename Scalar>
4319 template <
size_t R = 0,
size_t C = R,
typename Scalar =
int>
4320 using IntMat = std::conditional_t<R == 0 || C == 0,
4324 template <
typename T>
4327 template <
size_t R,
size_t C,
typename Scalar>
4330 template <
typename Scalar>
4344 template <
typename T>
4345 static constexpr bool IsIntMat = detail::IsIntMatHelper<T>::value;
4362 template <
typename Mat>
4364 using scalar_type =
typename Mat::scalar_type;
4365 auto it =
std::find_if(x.cbegin(), x.cend(), [](scalar_type val) {
4366 return val == POSITIVE_INFINITY || val == NEGATIVE_INFINITY;
4368 if (it != x.cend()) {
4369 auto [r, c] = x.coords(it);
4371 "invalid entry, expected entries to be integers, "
4372 "but found {} in entry ({}, {})",
4373 detail::entry_repr(*it),
4395 template <
typename Mat>
4396 std::enable_if_t<IsIntMat<Mat>>
4400 "invalid entry, expected entries to be integers, "
4402 detail::entry_repr(val));
4463 template <
typename Scalar>
4466 "MaxPlus requires a signed integer type as parameter!");
4479 Scalar
operator()(Scalar x, Scalar y)
const noexcept {
4510 template <
typename Scalar>
4513 "MaxPlus requires a signed integer type as parameter!");
4526 Scalar
operator()(Scalar x, Scalar y)
const noexcept {
4547 template <
typename Scalar>
4550 "MaxPlus requires a signed integer type as parameter!");
4560 constexpr Scalar
operator()() const noexcept {
4575 template <
typename Scalar>
4594 template <
size_t R,
size_t C,
typename Scalar>
4618 template <
size_t R = 0,
size_t C = R,
typename Scalar =
int>
4619 using MaxPlusMat = std::conditional_t<R == 0 || C == 0,
4624 template <
typename T>
4627 template <
size_t R,
size_t C,
typename Scalar>
4631 template <
typename Scalar>
4645 template <
typename T>
4646 static constexpr bool IsMaxPlusMat = detail::IsMaxPlusMatHelper<T>::value;
4664 template <
typename Mat>
4666 -> std::enable_if_t<IsMaxPlusMat<Mat>> {
4667 using scalar_type =
typename Mat::scalar_type;
4668 auto it =
std::find_if(x.cbegin(), x.cend(), [](scalar_type val) {
4669 return val == POSITIVE_INFINITY;
4671 if (it != x.cend()) {
4672 auto [r, c] = x.coords(it);
4674 "invalid entry, expected entries to be integers or {} (= {}), "
4675 "but found {} (= {}) in entry ({}, {})",
4700 template <
typename Mat>
4701 std::enable_if_t<IsMaxPlusMat<Mat>>
4704 using scalar_type =
typename Mat::scalar_type;
4706 "integers or {} (= {}) but found {} (= {})",
4771 template <
typename Scalar>
4774 "MinPlus requires a signed integer type as parameter!");
4787 Scalar
operator()(Scalar x, Scalar y)
const noexcept {
4818 template <
typename Scalar>
4821 "MinPlus requires a signed integer type as parameter!");
4834 Scalar
operator()(Scalar x, Scalar y)
const noexcept {
4855 template <
typename Scalar>
4858 "MinPlus requires a signed integer type as parameter!");
4868 constexpr Scalar
operator()() const noexcept {
4883 template <
typename Scalar>
4902 template <
size_t R,
size_t C,
typename Scalar>
4926 template <
size_t R = 0,
size_t C = R,
typename Scalar =
int>
4927 using MinPlusMat = std::conditional_t<R == 0 || C == 0,
4932 template <
typename T>
4935 template <
size_t R,
size_t C,
typename Scalar>
4939 template <
typename Scalar>
4953 template <
typename T>
4954 static constexpr bool IsMinPlusMat = detail::IsMinPlusMatHelper<T>::value;
4972 template <
typename Mat>
4974 using scalar_type =
typename Mat::scalar_type;
4975 auto it =
std::find_if(x.cbegin(), x.cend(), [](scalar_type val) {
4976 return val == NEGATIVE_INFINITY;
4978 if (it != x.cend()) {
4979 auto [r, c] = x.coords(it);
4981 "invalid entry, expected entries to be integers or {} (= {}), "
4982 "but found {} (= {}) in entry ({}, {})",
5007 template <
typename Mat>
5008 std::enable_if_t<IsMinPlusMat<Mat>>
5011 using scalar_type =
typename Mat::scalar_type;
5013 "integers or {} (= {}) but found {} (= {})",
5097 template <
size_t T,
typename Scalar>
5100 "MaxPlus requires a signed integer type as parameter!");
5113 Scalar
operator()(Scalar x, Scalar y)
const noexcept {
5114 LIBSEMIGROUPS_ASSERT((x >= 0 && x <=
static_cast<Scalar
>(T))
5116 LIBSEMIGROUPS_ASSERT((y >= 0 && y <=
static_cast<Scalar
>(T))
5121 return std::min(x + y,
static_cast<Scalar
>(T));
5139 template <
typename Scalar =
int>
5142 "MaxPlus requires a signed integer type as parameter!");
5200 static constexpr Scalar
scalar_one() noexcept {
5243 LIBSEMIGROUPS_ASSERT((x >= 0 && x <= _threshold)
5245 LIBSEMIGROUPS_ASSERT((y >= 0 && y <= _threshold)
5250 return std::min(x + y, _threshold);
5278 LIBSEMIGROUPS_ASSERT((x >= 0 && x <= _threshold)
5280 LIBSEMIGROUPS_ASSERT((y >= 0 && y <= _threshold)
5307 Scalar
const _threshold;
5322 template <
size_t T,
typename Scalar>
5342 template <
size_t T,
size_t R,
size_t C,
typename Scalar>
5368 template <
size_t T = 0,
size_t R = 0,
size_t C = R,
typename Scalar =
int>
5371 std::conditional_t<T == 0,
5372 DynamicMatrix<MaxPlusTruncSemiring<Scalar>, Scalar>,
5377 template <
typename T>
5380 template <
size_t T,
size_t R,
size_t C,
typename Scalar>
5383 static constexpr Scalar threshold = T;
5386 template <
size_t T,
typename Scalar>
5389 static constexpr Scalar threshold = T;
5392 template <
typename Scalar>
5393 struct IsMaxPlusTruncMatHelper<
5395 static constexpr Scalar threshold =
UNDEFINED;
5410 template <
typename T>
5412 = detail::IsMaxPlusTruncMatHelper<T>::value;
5415 template <
typename T>
5416 struct IsTruncMatHelper<T,
std::enable_if_t<IsMaxPlusTruncMat<T>>>
5418 static constexpr typename T::scalar_type threshold
5419 = IsMaxPlusTruncMatHelper<T>::threshold;
5442 template <
typename Mat>
5445 detail::throw_if_semiring_nullptr(m);
5447 using scalar_type =
typename Mat::scalar_type;
5450 return x == NEGATIVE_INFINITY || (0 <= x && x <= t);
5452 if (it != m.cend()) {
5453 auto [r, c] = m.coords(it);
5455 "invalid entry, expected values in {{0, 1, ..., {}, {} (= {})}} "
5456 "but found {} in entry ({}, {})",
5460 detail::entry_repr(*it),
5485 template <
typename Mat>
5486 std::enable_if_t<IsMaxPlusTruncMat<Mat>>
5488 detail::throw_if_semiring_nullptr(m);
5489 using scalar_type =
typename Mat::scalar_type;
5493 "invalid entry, expected values in {{0, 1, ..., {}, -{} (= {})}} "
5498 detail::entry_repr(val));
5577 template <
size_t T,
typename Scalar>
5591 Scalar
operator()(Scalar x, Scalar y)
const noexcept {
5592 LIBSEMIGROUPS_ASSERT((x >= 0 && x <=
static_cast<Scalar
>(T))
5594 LIBSEMIGROUPS_ASSERT((y >= 0 && y <=
static_cast<Scalar
>(T))
5599 return std::min(x + y,
static_cast<Scalar
>(T));
5616 template <
typename Scalar =
int>
5619 "MinPlus requires an integral type as parameter!");
5675 static constexpr Scalar
scalar_one() noexcept {
5719 LIBSEMIGROUPS_ASSERT((x >= 0 && x <= _threshold)
5721 LIBSEMIGROUPS_ASSERT((y >= 0 && y <= _threshold)
5726 return std::min(x + y, _threshold);
5754 LIBSEMIGROUPS_ASSERT((x >= 0 && x <= _threshold)
5756 LIBSEMIGROUPS_ASSERT((y >= 0 && y <= _threshold)
5783 Scalar
const _threshold;
5798 template <
size_t T,
typename Scalar>
5818 template <
size_t T,
size_t R,
size_t C,
typename Scalar>
5845 template <
size_t T = 0,
size_t R = 0,
size_t C = R,
typename Scalar =
int>
5848 std::conditional_t<T == 0,
5849 DynamicMatrix<MinPlusTruncSemiring<Scalar>, Scalar>,
5854 template <
typename T>
5857 template <
size_t T,
size_t R,
size_t C,
typename Scalar>
5860 static constexpr Scalar threshold = T;
5863 template <
size_t T,
typename Scalar>
5866 static constexpr Scalar threshold = T;
5869 template <
typename Scalar>
5870 struct IsMinPlusTruncMatHelper<
5872 static constexpr Scalar threshold =
UNDEFINED;
5887 template <
typename T>
5889 = detail::IsMinPlusTruncMatHelper<T>::value;
5892 template <
typename T>
5893 struct IsTruncMatHelper<T,
std::enable_if_t<IsMinPlusTruncMat<T>>>
5895 static constexpr typename T::scalar_type threshold
5896 = IsMinPlusTruncMatHelper<T>::threshold;
5920 template <
typename Mat>
5923 detail::throw_if_semiring_nullptr(m);
5925 using scalar_type =
typename Mat::scalar_type;
5928 return x == POSITIVE_INFINITY || (0 <= x && x <= t);
5930 if (it != m.cend()) {
5935 "invalid entry, expected values in {{0, 1, ..., {}, {} (= {})}} "
5936 "but found {} in entry ({}, {})",
5940 detail::entry_repr(*it),
5965 template <
typename Mat>
5966 std::enable_if_t<IsMinPlusTruncMat<Mat>>
5968 detail::throw_if_semiring_nullptr(m);
5970 using scalar_type =
typename Mat::scalar_type;
5974 "invalid entry, expected values in {{0, 1, ..., {}, {} (= {})}} "
5979 detail::entry_repr(val));
6042 template <
size_t T,
size_t P,
typename Scalar>
6043 constexpr Scalar thresholdperiod(Scalar x)
noexcept {
6045 return T + (x - T) % P;
6050 template <
typename Scalar>
6051 inline Scalar thresholdperiod(Scalar x,
6053 Scalar period)
noexcept {
6054 if (x > threshold) {
6084 template <
size_t T,
size_t P,
typename Scalar>
6097 constexpr Scalar
operator()(Scalar x, Scalar y)
const noexcept {
6098 return detail::thresholdperiod<T, P>(x + y);
6125 template <
size_t T,
size_t P,
typename Scalar>
6140 constexpr Scalar
operator()(Scalar x, Scalar y)
const noexcept {
6141 return detail::thresholdperiod<T, P>(x * y);
6159 template <
typename Scalar =
size_t>
6202 NTPSemiring(Scalar t, Scalar p) : _period(p), _threshold(t) {
6206 "expected non-negative value for 1st argument, found {}", t);
6211 "expected positive value for 2nd argument, found {}", p);
6225 static constexpr Scalar
scalar_one() noexcept {
6270 LIBSEMIGROUPS_ASSERT(x >= 0 && x <= _period + _threshold - 1);
6271 LIBSEMIGROUPS_ASSERT(y >= 0 && y <= _period + _threshold - 1);
6272 return detail::thresholdperiod(x * y, _threshold, _period);
6300 LIBSEMIGROUPS_ASSERT(x >= 0 && x <= _period + _threshold - 1);
6301 LIBSEMIGROUPS_ASSERT(y >= 0 && y <= _period + _threshold - 1);
6302 return detail::thresholdperiod(x + y, _threshold, _period);
6333 Scalar
period() const noexcept {
6352 template <
typename Scalar>
6368 template <
size_t T,
size_t P,
typename Scalar>
6394 template <
size_t T,
size_t P,
size_t R,
size_t C,
typename Scalar>
6425 template <
size_t T = 0,
6429 typename Scalar =
size_t>
6430 using NTPMat = std::conditional_t<
6432 std::conditional_t<T == 0 && P == 0,
6438 template <
typename T>
6441 template <
typename Scalar>
6443 static constexpr Scalar threshold =
UNDEFINED;
6444 static constexpr Scalar period =
UNDEFINED;
6447 template <
size_t T,
size_t P,
typename Scalar>
6450 static constexpr Scalar threshold = T;
6451 static constexpr Scalar period = P;
6454 template <
size_t T,
size_t P,
size_t R,
size_t C,
typename Scalar>
6456 static constexpr Scalar threshold = T;
6457 static constexpr Scalar period = P;
6472 template <
typename U>
6473 static constexpr bool IsNTPMat = detail::IsNTPMatHelper<U>::value;
6476 template <
typename T>
6478 static constexpr typename T::scalar_type threshold
6479 = IsNTPMatHelper<T>::threshold;
6480 static constexpr typename T::scalar_type period
6481 = IsNTPMatHelper<T>::period;
6508 template <
size_t T,
size_t P,
size_t R,
size_t C,
typename Scalar>
6530 template <
size_t T,
size_t P,
typename Scalar>
6551 template <
typename Scalar>
6553 return x.semiring()->period();
6578 template <
typename Mat>
6580 detail::throw_if_semiring_nullptr(m);
6582 using scalar_type =
typename Mat::scalar_type;
6586 return (0 <= x && x < p + t);
6588 if (it != m.cend()) {
6593 "invalid entry, expected values in {{0, 1, ..., {}}}, but "
6594 "found {} in entry ({}, {})",
6596 detail::entry_repr(*it),
6623 template <
typename Mat>
6624 std::enable_if_t<IsNTPMat<Mat>>
6626 detail::throw_if_semiring_nullptr(m);
6627 using scalar_type =
typename Mat::scalar_type;
6630 if (val < 0 || val >= p + t) {
6632 "invalid entry, expected values in {{0, 1, ..., {}}}, but "
6635 detail::entry_repr(val));
6645 template <
typename T>
6648 using scalar_type =
typename T::scalar_type;
6649 using scalar_reference =
typename T::scalar_reference;
6650 using scalar_const_reference =
typename T::scalar_const_reference;
6651 using semiring_type = void;
6653 using container_type =
typename T::container_type;
6654 using iterator =
typename T::iterator;
6655 using const_iterator =
typename T::const_iterator;
6657 using underlying_matrix_type = T;
6659 using RowView =
typename T::RowView;
6665 using Row =
typename T::Row;
6667 scalar_type scalar_one() const noexcept {
6668 return _underlying_mat.scalar_one();
6671 scalar_type scalar_zero() const noexcept {
6672 return _underlying_mat.scalar_zero();
6679 ProjMaxPlusMat() : _is_normalized(false), _underlying_mat() {}
6680 ProjMaxPlusMat(ProjMaxPlusMat
const&) =
default;
6681 ProjMaxPlusMat(ProjMaxPlusMat&&) =
default;
6682 ProjMaxPlusMat& operator=(ProjMaxPlusMat
const&) =
default;
6683 ProjMaxPlusMat& operator=(ProjMaxPlusMat&&) =
default;
6685 ProjMaxPlusMat(
size_t r,
size_t c)
6686 : _is_normalized(false), _underlying_mat(r, c) {}
6690 typename underlying_matrix_type::semiring_type
const* semiring,
6693 : _is_normalized(false), _underlying_mat(semiring, r, c) {}
6695 explicit ProjMaxPlusMat(std::vector<std::vector<scalar_type>>
const& m)
6696 : _is_normalized(false), _underlying_mat(m) {
6701 std::initializer_list<std::initializer_list<scalar_type>>
const& m)
6703 std::vector<std::vector<scalar_type>>(m.begin(), m.
end())) {}
6705 ~ProjMaxPlusMat() =
default;
6707 ProjMaxPlusMat one()
const {
6708 auto result = ProjMaxPlusMat(_underlying_mat.one());
6712 static ProjMaxPlusMat one(
size_t n) {
6713 return ProjMaxPlusMat(T::one(n));
6720 bool operator==(ProjMaxPlusMat
const& that)
const {
6723 return _underlying_mat == that._underlying_mat;
6726 bool operator!=(ProjMaxPlusMat
const& that)
const {
6727 return !(_underlying_mat == that._underlying_mat);
6730 bool operator<(ProjMaxPlusMat
const& that)
const {
6733 return _underlying_mat < that._underlying_mat;
6736 bool operator>(ProjMaxPlusMat
const& that)
const {
6737 return that < *
this;
6740 template <
typename Thing>
6741 bool operator>=(Thing
const& that)
const {
6743 return that < *
this || that == *
this;
6747 template <
typename Thing>
6748 bool operator<=(Thing
const& that)
const {
6750 return *
this < that || that == *
this;
6757 scalar_reference operator()(
size_t r,
size_t c) {
6762 _is_normalized =
false;
6763 return _underlying_mat(r, c);
6766 scalar_reference at(
size_t r,
size_t c) {
6768 return this->operator()(r, c);
6771 scalar_const_reference operator()(
size_t r,
size_t c)
const {
6773 return _underlying_mat(r, c);
6776 scalar_const_reference at(
size_t r,
size_t c)
const {
6778 return this->operator()(r, c);
6781 size_t number_of_rows() const noexcept {
6782 return _underlying_mat.number_of_rows();
6785 size_t number_of_cols() const noexcept {
6786 return _underlying_mat.number_of_cols();
6789 size_t hash_value()
const {
6791 return Hash<T>()(_underlying_mat);
6798 void product_inplace_no_checks(ProjMaxPlusMat
const& A,
6799 ProjMaxPlusMat
const& B) {
6800 _underlying_mat.product_inplace_no_checks(A._underlying_mat,
6805 void operator+=(ProjMaxPlusMat
const& that) {
6806 _underlying_mat += that._underlying_mat;
6810 void operator*=(scalar_type a) {
6811 _underlying_mat *= a;
6815 void operator+=(scalar_type a) {
6816 _underlying_mat += a;
6820 ProjMaxPlusMat operator*(scalar_type a)
const {
6821 ProjMaxPlusMat result(*
this);
6826 ProjMaxPlusMat operator+(scalar_type a)
const {
6827 ProjMaxPlusMat result(*
this);
6836 ProjMaxPlusMat operator+(ProjMaxPlusMat
const& that)
const {
6837 return ProjMaxPlusMat(_underlying_mat + that._underlying_mat);
6840 ProjMaxPlusMat operator*(ProjMaxPlusMat
const& that)
const {
6841 return ProjMaxPlusMat(_underlying_mat * that._underlying_mat);
6852 iterator begin() noexcept {
6857 _is_normalized =
false;
6858 return _underlying_mat.begin();
6861 iterator
end() noexcept {
6866 _is_normalized =
false;
6867 return _underlying_mat.end();
6870 const_iterator begin() const noexcept {
6872 return _underlying_mat.begin();
6875 const_iterator
end() const noexcept {
6877 return _underlying_mat.end();
6880 const_iterator cbegin() const noexcept {
6882 return _underlying_mat.cbegin();
6885 const_iterator cend() const noexcept {
6887 return _underlying_mat.cend();
6894 void swap(ProjMaxPlusMat& that)
noexcept {
6895 std::swap(_underlying_mat, that._underlying_mat);
6899 _underlying_mat.transpose();
6902 void transpose_no_checks() noexcept {
6903 _underlying_mat.transpose_no_checks();
6910 RowView row(
size_t i)
const {
6912 return _underlying_mat.row(i);
6915 template <
typename C>
6916 void rows(C& x)
const {
6918 return _underlying_mat.rows(x);
6925 friend std::ostream& operator<<(std::ostream& os,
6926 ProjMaxPlusMat
const& x) {
6928 os << detail::to_string(x._underlying_mat);
6932 T
const& underlying_matrix() const noexcept {
6934 return _underlying_mat;
6938 explicit ProjMaxPlusMat(T&& mat)
6939 : _is_normalized(false), _underlying_mat(std::
move(mat)) {
6943 void normalize(
bool force =
false)
const {
6944 if ((_is_normalized && !force)
6945 || (_underlying_mat.number_of_rows() == 0)
6946 || (_underlying_mat.number_of_cols() == 0)) {
6947 _is_normalized =
true;
6951 _underlying_mat.cend());
6953 _underlying_mat.end(),
6954 [&n](scalar_type& s) {
6955 if (s != NEGATIVE_INFINITY) {
6959 _is_normalized =
true;
6962 mutable bool _is_normalized;
6963 mutable T _underlying_mat;
7025 template <
size_t R,
size_t C,
typename Scalar>
7027 = detail::ProjMaxPlusMat<StaticMaxPlusMat<R, C, Scalar>>;
7040 template <
typename Scalar>
7042 = detail::ProjMaxPlusMat<DynamicMaxPlusMat<Scalar>>;
7058 template <
size_t R = 0,
size_t C = R,
typename Scalar =
int>
7064 template <
typename T>
7067 template <
size_t R,
size_t C,
typename Scalar>
7071 template <
typename Scalar>
7087 template <
typename T>
7089 = detail::IsProjMaxPlusMatHelper<T>::value;
7109 template <
typename Mat>
7110 constexpr std::enable_if_t<IsProjMaxPlusMat<Mat>>
7132 template <
typename Mat>
7133 constexpr std::enable_if_t<IsProjMaxPlusMat<Mat>>
7178 template <
typename Mat>
7179 Mat
pow(Mat
const& x,
typename Mat::scalar_type e) {
7180 using scalar_type =
typename Mat::scalar_type;
7185 "negative exponent, expected value >= 0, found {}", e);
7191 typename Mat::semiring_type
const* sr =
nullptr;
7205 auto z = (e % 2 == 0 ? x.one() : y);
7207 Mat tmp(sr, x.number_of_rows(), x.number_of_cols());
7209 tmp.product_inplace_no_checks(y, y);
7213 tmp.product_inplace_no_checks(z, y);
7243 template <
typename Mat,
typename = std::enable_if_t<IsDynamicMatrix<Mat>>>
7270 template <
typename Mat,
typename = std::enable_if_t<IsStaticMatrix<Mat>>>
7271 detail::StaticVector1<typename Mat::RowView, Mat::nr_rows>
7272 rows(Mat
const& x) {
7273 detail::StaticVector1<typename Mat::RowView, Mat::nr_rows> container;
7314 template <
typename Mat,
size_t R,
size_t C,
typename Container>
7316 detail::StaticVector1<BitSet<C>, R>& result) {
7317 using RowView =
typename Mat::RowView;
7318 using value_type =
typename std::decay_t<Container>::value_type;
7320 static_assert(
IsBMat<Mat>,
"IsBMat<Mat> must be true!");
7323 "Container::value_type must equal Mat::RowView or "
7324 "std::vector<bool>!!");
7325 static_assert(R <= BitSet<1>::max_size(),
7326 "R must be at most BitSet<1>::max_size()!");
7327 static_assert(C <= BitSet<1>::max_size(),
7328 "C must be at most BitSet<1>::max_size()!");
7329 LIBSEMIGROUPS_ASSERT(views.size() <= R);
7330 LIBSEMIGROUPS_ASSERT(views.empty() || views[0].size() <= C);
7331 for (
auto const& v : views) {
7332 result.emplace_back(v.cbegin(), v.cend());
7368 template <
typename Mat,
size_t R,
size_t C,
typename Container>
7370 using RowView =
typename Mat::RowView;
7371 using value_type =
typename std::decay_t<Container>::value_type;
7372 static_assert(
IsBMat<Mat>,
"IsBMat<Mat> must be true!");
7375 "Container::value_type must equal Mat::RowView or "
7376 "std::vector<bool>!!");
7377 static_assert(R <= BitSet<1>::max_size(),
7378 "R must be at most BitSet<1>::max_size()!");
7379 static_assert(C <= BitSet<1>::max_size(),
7380 "C must be at most BitSet<1>::max_size()!");
7381 LIBSEMIGROUPS_ASSERT(views.size() <= R);
7382 LIBSEMIGROUPS_ASSERT(views.empty() || views[0].size() <= C);
7383 detail::StaticVector1<BitSet<C>, R> result;
7417 template <
typename Mat,
size_t R,
size_t C>
7419 detail::StaticVector1<BitSet<C>, R>& result) {
7420 static_assert(
IsBMat<Mat>,
"IsBMat<Mat> must be true!");
7421 static_assert(R <= BitSet<1>::max_size(),
7422 "R must be at most BitSet<1>::max_size()!");
7423 static_assert(C <= BitSet<1>::max_size(),
7424 "C must be at most BitSet<1>::max_size()!");
7425 LIBSEMIGROUPS_ASSERT(x.number_of_cols() <= C);
7426 LIBSEMIGROUPS_ASSERT(x.number_of_rows() <= R);
7449 template <
typename Mat>
7451 static_assert(
IsBMat<Mat>,
"IsBMat<Mat> must be true!");
7452 LIBSEMIGROUPS_ASSERT(x.number_of_rows() <= BitSet<1>::max_size());
7453 LIBSEMIGROUPS_ASSERT(x.number_of_cols() <= BitSet<1>::max_size());
7454 size_t const M = detail::BitSetCapacity<Mat>::value;
7486 template <
typename Mat,
typename Container>
7488 using value_type =
typename std::decay_t<Container>::value_type;
7489 static_assert(
IsBMat<Mat>,
"IsBMat<Mat> must be true!");
7490 static_assert(IsBitSet<value_type> || detail::IsStdBitSet<value_type>,
7491 "Container::value_type must be BitSet or std::bitset");
7492 LIBSEMIGROUPS_ASSERT(
rows.size() <= BitSet<1>::max_size());
7493 LIBSEMIGROUPS_ASSERT(
rows.empty()
7494 ||
rows[0].size() <= BitSet<1>::max_size());
7499 for (
size_t i = 0; i <
rows.size(); ++i) {
7502 for (
size_t j = 0; j < i; ++j) {
7507 for (
size_t j = i + 1; j <
rows.size(); ++j) {
7512 if (cup !=
rows[i]) {
7541 template <
typename Mat,
typename Container>
7543 using value_type =
typename std::decay_t<Container>::value_type;
7544 static_assert(
IsBMat<Mat>,
"IsBMat<Mat> must be true!");
7545 static_assert(IsBitSet<value_type> || detail::IsStdBitSet<value_type>,
7546 "Container::value_type must be BitSet or std::bitset");
7547 LIBSEMIGROUPS_ASSERT(
rows.size() <= BitSet<1>::max_size());
7548 LIBSEMIGROUPS_ASSERT(
rows.empty()
7549 ||
rows[0].size() <= BitSet<1>::max_size());
7550 std::decay_t<Container> result;
7582 template <typename Mat, size_t M = detail::BitSetCapacity<Mat>::value>
7584 static_assert(
IsBMat<Mat>,
"IsBMat<Mat> must be true!");
7585 LIBSEMIGROUPS_ASSERT(x.number_of_rows() <= BitSet<1>::max_size());
7586 LIBSEMIGROUPS_ASSERT(x.number_of_cols() <= BitSet<1>::max_size());
7587 detail::StaticVector1<BitSet<M>, M> result;
7615 template <
typename Mat,
typename Container>
7617 using value_type =
typename Container::value_type;
7618 static_assert(
IsBMat<Mat>,
"IsBMat<Mat> must be true!");
7619 static_assert(IsBitSet<value_type> || detail::IsStdBitSet<value_type>,
7620 "Container::value_type must be BitSet or std::bitset");
7621 LIBSEMIGROUPS_ASSERT(x.number_of_rows() <= BitSet<1>::max_size());
7622 LIBSEMIGROUPS_ASSERT(x.number_of_cols() <= BitSet<1>::max_size());
7657 template <
typename Mat,
typename Container>
7658 std::enable_if_t<IsMaxPlusTruncMat<Mat>>
7659 row_basis(Container&& views, std::decay_t<Container>& result) {
7660 using value_type =
typename std::decay_t<Container>::value_type;
7662 "Container::value_type must be Mat::RowView");
7663 using scalar_type =
typename Mat::scalar_type;
7664 using Row =
typename Mat::Row;
7666 if (views.empty()) {
7670 LIBSEMIGROUPS_ASSERT(result.empty());
7675 for (
size_t r1 = 0; r1 < views.size(); ++r1) {
7676 if (r1 == 0 || views[r1] != views[r1 - 1]) {
7677 std::fill(tmp1.begin(), tmp1.end(), tmp1.scalar_zero());
7678 for (
size_t r2 = 0; r2 < r1; ++r2) {
7680 for (
size_t c = 0; c < tmp1.number_of_cols(); ++c) {
7681 if (views[r2][c] == tmp1.scalar_zero()) {
7684 if (views[r1][c] >= views[r2][c]) {
7687 =
std::min(max_scalar, views[r1][c] - views[r2][c]);
7690 max_scalar = tmp1.scalar_zero();
7694 if (max_scalar != tmp1.scalar_zero()) {
7695 tmp1 += views[r2] * max_scalar;
7698 if (tmp1 != views[r1]) {
7699 result.push_back(views[r1]);
7735 template <
typename Mat,
typename Container>
7736 std::enable_if_t<IsBMat<Mat>>
row_basis(Container&& views,
7737 std::decay_t<Container>& result) {
7738 using RowView =
typename Mat::RowView;
7739 using value_type =
typename std::decay_t<Container>::value_type;
7743 "Container::value_type must equal Mat::RowView or "
7744 "std::vector<bool>!!");
7746 if (views.empty()) {
7751 size_t const M = detail::BitSetCapacity<Mat>::value;
7753 using bitset_type =
typename decltype(br)::value_type;
7757 LIBSEMIGROUPS_ASSERT(br.size() == views.size());
7758 for (
size_t i = 0; i < br.size(); ++i) {
7759 lookup.
insert({br[i], i});
7764 auto it = lookup.
find(bs);
7765 LIBSEMIGROUPS_ASSERT(it != lookup.
end());
7766 result.push_back(views[it->second]);
7796 template <
typename Mat,
7798 typename = std::enable_if_t<IsMatrix<Mat>>>
7799 void row_basis(Mat
const& x, Container& result) {
7823 template <
typename Mat,
typename = std::enable_if_t<IsDynamicMatrix<Mat>>>
7849 template <
typename Mat,
typename = std::enable_if_t<IsStaticMatrix<Mat>>>
7850 detail::StaticVector1<typename Mat::RowView, Mat::nr_rows>
7852 detail::StaticVector1<typename Mat::RowView, Mat::nr_rows> container;
7876 template <
typename Mat,
typename Container>
7878 using value_type =
typename std::decay_t<Container>::value_type;
7879 static_assert(
IsMatrix<Mat>,
"IsMatrix<Mat> must be true!");
7881 "Container::value_type must be Mat::RowView");
7883 std::decay_t<Container> result;
7922 template <
typename Mat,
typename = std::enable_if_t<IsBMat<Mat>>>
7924 size_t const M = detail::BitSetCapacity<Mat>::value;
7929 st.
insert(bitset_row_basis_.cbegin(), bitset_row_basis_.cend());
7931 bitset_row_basis_.cend());
7932 for (
size_t i = 0; i < orb.
size(); ++i) {
7933 for (
auto& row : bitset_row_basis_) {
7935 for (
size_t j = 0; j < x.number_of_rows(); ++j) {
7936 cup.set(j, cup[j] || row[j]);
7938 if (st.
insert(cup).second) {
7967 template <
typename Mat>
7968 auto operator+(
typename Mat::scalar_type a, Mat
const& x)
7969 -> std::enable_if_t<IsMatrix<Mat>, Mat> {
7992 template <
typename Mat>
7993 auto operator*(
typename Mat::scalar_type a, Mat
const& x)
7994 -> std::enable_if_t<IsMatrix<Mat>, Mat> {
8034 template <
typename Mat,
8038 detail::throw_if_any_row_wrong_size(rows);
8039 detail::throw_if_bad_dim<Mat>(rows);
8070 template <
typename Mat,
8104 template <
typename Mat,
8108 detail::throw_if_bad_row_dim<Mat>(row);
8146 template <
typename Mat,
8148 typename = std::enable_if_t<IsMatrix<Mat>>>
8151 Mat
make(Semiring
const* semiring,
8154 detail::throw_if_any_row_wrong_size(rows);
8155 detail::throw_if_bad_dim<Mat>(rows);
8156 Mat m(semiring, rows);
8192 template <
typename Mat,
8194 typename = std::enable_if_t<IsMatrix<Mat>>>
8195 Mat
make(Semiring
const* semiring,
8197 detail::throw_if_any_row_wrong_size(rows);
8198 detail::throw_if_bad_dim<Mat>(rows);
8199 Mat m(semiring, rows);
8226 template <
typename Mat,
8228 typename = std::enable_if_t<IsMatrix<Mat>>>
8229 Mat
make(Semiring
const* semiring,
8231 detail::throw_if_bad_row_dim<Mat>(row);
8232 Mat m(semiring, row);
8264 template <
size_t R,
size_t C,
typename Scalar>
8287 template <
typename S,
typename T>
8289 detail::RowViewCommon<S, T>
const& x) {
8291 for (
auto it = x.cbegin(); it != x.cend(); ++it) {
8293 if (it != x.cend() - 1) {
8317 template <
typename Mat>
8321 if (x.number_of_rows() != 1) {
8326 if (n != x.number_of_rows() - 1) {
8331 if (x.number_of_rows() != 1) {
8353 template <
typename Mat>
8358 size_t max_width = 72)
8360 if (braces.size() != 2) {
8362 "the 4th argument (braces) must have size 2, found {}",
8366 size_t const R = x.number_of_rows();
8367 size_t const C = x.number_of_cols();
8371 for (
size_t r = 0; r < R; ++r) {
8372 for (
size_t c = 0; c < C; ++c) {
8374 = detail::unicode_string_length(detail::entry_repr(x(r, c)));
8375 row_widths[r] += width;
8376 if (width > max_col_widths[c]) {
8377 max_col_widths[c] = width;
8384 auto const total_width = col_width * C + prefix.size() + 1;
8385 if (total_width > max_width) {
8390 "<{}x{} {}>", x.number_of_rows(), x.number_of_cols(), short_name);
8398 auto const lbrace = braces[0], rbrace = braces[1];
8399 if (R != 0 && C != 0) {
8401 for (
size_t r = 0; r < R; ++r) {
8402 result += fmt::format(
"{}{}", rindent, lbrace);
8405 for (
size_t c = 0; c < C; ++c) {
8406 result += fmt::format(
8407 "{}{:>{}}", csep, detail::entry_repr(x(r, c)), col_width);
8410 result += fmt::format(
"{}", rbrace);
8450 template <
typename Mat>
8465 constexpr size_t operator()(Mat
const& x)
const noexcept {
8466 return x.number_of_rows() * x.number_of_rows() * x.number_of_rows();
8480 template <
typename Mat>
8481 struct Degree<Mat,
std::enable_if_t<IsMatrix<Mat>>> {
8494 constexpr size_t operator()(Mat
const& x)
const noexcept {
8495 return x.number_of_rows();
8509 template <
typename Mat>
8510 struct Hash<Mat,
std::enable_if_t<IsMatrix<Mat>>> {
8523 constexpr size_t operator()(Mat
const& x)
const {
8524 return x.hash_value();
8543 template <
typename Mat>
8548 constexpr void operator()(Mat&,
size_t)
const noexcept {
8550 LIBSEMIGROUPS_ASSERT(
false);
8567 template <
typename Mat>
8568 struct One<Mat,
std::enable_if_t<IsMatrix<Mat>>> {
8597 template <
typename Mat>
8598 struct Product<Mat,
std::enable_if_t<IsMatrix<Mat>>> {
8618 operator()(Mat& xy, Mat
const& x, Mat
const& y,
size_t = 0)
const {
8619 xy.product_inplace_no_checks(x, y);
8627 std::enable_if_t<libsemigroups::IsMatrix<Mat>>>
8628 inline void swap(Mat& x, Mat& y)
noexcept {
DynamicMatrix(std::initializer_list< scalar_type > const &c)
Construct a vector from a std::initializer_list.
Definition matrix.hpp:2893
DynamicMatrix(std::initializer_list< std::initializer_list< scalar_type > > const &m)
Construct a matrix from std::initializer_list of std::initializer_list of scalars.
Definition matrix.hpp:2916
DynamicMatrix()=default
Default constructor.
DynamicMatrix & operator=(DynamicMatrix &&)=default
Default move assignment operator.
scalar_reference at(size_t r, size_t c)
Returns a reference to the specified entry of the matrix.
ProdOp Prod
Alias for the template parameter ProdOp.
Definition matrix.hpp:2812
void product_inplace_no_checks(DynamicMatrix const &x, DynamicMatrix const &y)
Multiplies x and y and stores the result in this.
DynamicMatrix & operator=(DynamicMatrix const &)=default
Default copy assignment operator.
DynamicMatrix Row
The type of a row of a DynamicMatrix.
Definition matrix.hpp:2803
PlusOp Plus
Alias for the template parameter PlusOp.
Definition matrix.hpp:2809
void rows(T &x) const
Add row views for every row in the matrix to a container.
scalar_type scalar_one() const noexcept
Returns the multiplicative identity of the underlying semiring.
typename MatrixCommon::scalar_const_reference scalar_const_reference
The type of const references to the entries in the matrix.
Definition matrix.hpp:2799
void swap(DynamicMatrix &that) noexcept
Swaps the contents of *this with the contents of that.
Definition matrix.hpp:3175
const_iterator cend() noexcept
Returns a const iterator pointing one beyond the last entry in the matrix.
static DynamicMatrix one(size_t n)
Construct the identity matrix.
Definition matrix.hpp:2997
DynamicMatrix(size_t r, size_t c)
Construct a matrix with given dimensions.
Definition matrix.hpp:2870
ZeroOp Zero
Alias for the template parameter ZeroOp.
Definition matrix.hpp:2815
scalar_type scalar_zero() const noexcept
Returns the additive identity of the underlying semiring.
void transpose_no_checks()
Transpose a matrix in-place.
semiring_type const * semiring() const noexcept
Returns the underlying semiring.
OneOp One
Alias for the template parameter OneOp.
Definition matrix.hpp:2818
void semiring_type
Alias for the semiring type (void).
Definition matrix.hpp:2825
RowView row(size_t i) const
Returns a view into a row.
DynamicMatrix(RowView const &rv)
Construct a row from a row view.
Definition matrix.hpp:2950
iterator begin() noexcept
Returns an iterator pointing at the first entry.
size_t number_of_rows() const noexcept
Returns the number of rows of the matrix.
DynamicRowView< PlusOp, ProdOp, ZeroOp, OneOp, Scalar > RowView
The type of a row view into a DynamicMatrix.
Definition matrix.hpp:2806
RowView row_no_checks(size_t i) const
Returns a view into a row.
DynamicMatrix(std::vector< std::vector< scalar_type > > const &m)
Construct a matrix from std::vector of std::vector of scalars.
Definition matrix.hpp:2935
typename MatrixCommon::scalar_reference scalar_reference
The type of references to the entries in the matrix.
Definition matrix.hpp:2794
scalar_reference at(size_t r, size_t c) const
Returns a const reference to the specified entry of the matrix.
DynamicMatrix(DynamicMatrix const &)=default
Default copy constructor.
size_t hash_value() const
Return a hash value of a matrix.
const_iterator cbegin() noexcept
Returns a const iterator pointing at the first entry.
DynamicMatrix(DynamicMatrix &&)=default
Default move constructor.
typename MatrixCommon::scalar_type scalar_type
The type of the entries in the matrix.
Definition matrix.hpp:2791
size_t number_of_cols() const noexcept
Returns the number of columns of the matrix.
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.
DynamicMatrix(Semiring const *sr, std::initializer_list< std::initializer_list< scalar_type > > const &rws)
Construct a matrix over a given semiring (std::initializer_list of std::initializer_list).
Definition matrix.hpp:3314
DynamicMatrix & operator=(DynamicMatrix &&)=default
Default move assignment operator.
static DynamicMatrix one(Semiring const *semiring, size_t n)
Construct the identity matrix.
Definition matrix.hpp:3391
scalar_reference at(size_t r, size_t c)
Returns a reference to the specified entry of the matrix.
DynamicMatrix(Semiring const *sr, std::vector< std::vector< scalar_type > > const &rws)
Construct a matrix over a given semiring (std::vector of std::vector).
Definition matrix.hpp:3337
DynamicMatrix(Semiring const *sr, size_t r, size_t c)
Construct a matrix over a given semiring with given dimensions.
Definition matrix.hpp:3294
void product_inplace_no_checks(DynamicMatrix const &x, DynamicMatrix const &y)
Multiplies x and y and stores the result in this.
DynamicMatrix & operator=(DynamicMatrix const &)=default
Default copy assignment operator.
DynamicMatrix Row
Alias for the type of the rows of a DynamicMatrix.
Definition matrix.hpp:3251
void rows(T &x) const
Add row views for every row in the matrix to a container.
scalar_type scalar_one() const noexcept
Returns the multiplicative identity of the underlying semiring.
typename MatrixCommon::scalar_const_reference scalar_const_reference
Alias for const references to the template parameter Scalar.
Definition matrix.hpp:3247
void swap(DynamicMatrix &that) noexcept
Swaps the contents of *this with the contents of that.
Definition matrix.hpp:3571
const_iterator cend() noexcept
Returns a const iterator pointing one beyond the last entry in the matrix.
scalar_type scalar_zero() const noexcept
Returns the additive identity of the underlying semiring.
void transpose_no_checks()
Transpose a matrix in-place.
DynamicMatrix()=delete
Deleted.
semiring_type const * semiring() const noexcept
Returns the underlying semiring.
RowView row(size_t i) const
Returns a view into a row.
DynamicMatrix(RowView const &rv)
Construct a row over a given semiring (RowView).
Definition matrix.hpp:3371
iterator begin() noexcept
Returns an iterator pointing at the first entry.
size_t number_of_rows() const noexcept
Returns the number of rows of the matrix.
DynamicMatrix(Semiring const *sr, std::initializer_list< scalar_type > const &rw)
Construct a row over a given semiring (std::initializer_list).
Definition matrix.hpp:3356
RowView row_no_checks(size_t i) const
Returns a view into a row.
typename MatrixCommon::scalar_reference scalar_reference
Alias for references to the template parameter Scalar.
Definition matrix.hpp:3242
scalar_reference at(size_t r, size_t c) const
Returns a const reference to the specified entry of the matrix.
DynamicMatrix(DynamicMatrix const &)=default
Default copy constructor.
size_t hash_value() const
Return a hash value of a matrix.
const_iterator cbegin() noexcept
Returns a const iterator pointing at the first entry.
DynamicMatrix(DynamicMatrix &&)=default
Default move constructor.
typename MatrixCommon::scalar_type scalar_type
Alias for the template parameter Scalar.
Definition matrix.hpp:3239
DynamicRowView< Semiring, Scalar > RowView
Alias for the type of row views of a DynamicMatrix.
Definition matrix.hpp:3254
Semiring semiring_type
Alias for the template parameter Semiring.
Definition matrix.hpp:3259
size_t number_of_cols() const noexcept
Returns the number of columns of the matrix.
void transpose()
Transpose a matrix in-place.
std::pair< scalar_type, scalar_type > coords(const_iterator it) const
Get the coordinates of an iterator.
iterator end() noexcept
Returns an iterator pointing one beyond the last entry in the matrix.
DynamicRowView & operator=(DynamicRowView &&)=default
Default move assignment operator.
size_t size() const noexcept
Returns the size of the row.
DynamicRowView & operator=(DynamicRowView const &)=default
Default copy assignment operator.
DynamicRowView(DynamicRowView const &)=default
Default copy constructor.
DynamicRowView(DynamicRowView &&)=default
Default move constructor.
DynamicRowView(Row const &r)
Construct a row view from a Row.
Definition matrix.hpp:1572
typename RowViewCommon::iterator iterator
Alias for const iterators pointing at entries of a matrix.
Definition matrix.hpp:1536
typename RowViewCommon::scalar_const_reference scalar_const_reference
Alias for const references to the template parameter Scalar.
Definition matrix.hpp:1547
typename RowViewCommon::scalar_reference scalar_reference
Alias for references to the template parameter Scalar.
Definition matrix.hpp:1542
iterator begin() noexcept
Returns a iterator pointing at the first entry.
iterator cend()
Returns a const iterator pointing one beyond the last entry of the row.
typename matrix_type::Row Row
Alias for the type of a row in the underlying matrix.
Definition matrix.hpp:1554
typename RowViewCommon::const_iterator const_iterator
Alias for const iterators pointing at entries of a matrix.
Definition matrix.hpp:1533
const_iterator cbegin() const noexcept
Returns a const iterator pointing at the first entry.
iterator end()
Returns a iterator pointing one beyond the last entry of the row.
Scalar scalar_type
Alias for the template parameter Scalar.
Definition matrix.hpp:1539
typename RowViewCommon::matrix_type matrix_type
Alias for the type of the underlying matrix.
Definition matrix.hpp:1551
DynamicRowView()=default
Default constructor.
size_t size() const noexcept
Returns the size of the row.
DynamicRowView & operator=(DynamicRowView const &)=default
Default copy assignment operator.
typename RowViewCommon::iterator iterator
Alias for const iterators pointing at entries of a matrix.
Definition matrix.hpp:1690
typename RowViewCommon::scalar_const_reference scalar_const_reference
Alias for const references to the template parameter Scalar.
Definition matrix.hpp:1701
typename RowViewCommon::scalar_reference scalar_reference
Alias for references to the template parameter Scalar.
Definition matrix.hpp:1696
iterator begin() noexcept
Returns a iterator pointing at the first entry.
iterator cend()
Returns a const iterator pointing one beyond the last entry of the row.
typename matrix_type::Row Row
Alias for the type of a row in the underlying matrix.
Definition matrix.hpp:1708
typename RowViewCommon::const_iterator const_iterator
Alias for const iterators pointing at entries of a matrix.
Definition matrix.hpp:1687
const_iterator cbegin() const noexcept
Returns a const iterator pointing at the first entry.
iterator end()
Returns a iterator pointing one beyond the last entry of the row.
Scalar scalar_type
Alias for the template parameter Scalar.
Definition matrix.hpp:1693
typename RowViewCommon::matrix_type matrix_type
Alias for the type of the underlying matrix.
Definition matrix.hpp:1705
DynamicRowView()=default
Default constructor.
Class representing a truncated max-plus semiring.
Definition matrix.hpp:5138
static constexpr Scalar scalar_zero() noexcept
Get the additive identity.
Definition matrix.hpp:5212
Scalar plus_no_checks(Scalar x, Scalar y) const noexcept
Addition in a truncated max-plus semiring.
Definition matrix.hpp:5275
Scalar product_no_checks(Scalar x, Scalar y) const noexcept
Multiplication in a truncated max-plus semiring.
Definition matrix.hpp:5240
MaxPlusTruncSemiring()=delete
Deleted default constructor.
Scalar threshold() const noexcept
Get the threshold.
Definition matrix.hpp:5300
static constexpr Scalar scalar_one() noexcept
Get the multiplicative identity.
Definition matrix.hpp:5198
Class representing a truncated min-plus semiring.
Definition matrix.hpp:5615
static constexpr Scalar scalar_zero() noexcept
Get the additive identity.
Definition matrix.hpp:5688
Scalar plus_no_checks(Scalar x, Scalar y) const noexcept
Addition in a truncated min-plus semiring.
Definition matrix.hpp:5751
Scalar product_no_checks(Scalar x, Scalar y) const noexcept
Multiplication in a truncated min-plus semiring.
Definition matrix.hpp:5716
Scalar threshold() const noexcept
Get the threshold.
Definition matrix.hpp:5776
static constexpr Scalar scalar_one() noexcept
Get the multiplicative identity.
Definition matrix.hpp:5673
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:6239
Scalar plus_no_checks(Scalar x, Scalar y) const noexcept
Addition in an ntp semiring.
Definition matrix.hpp:6297
NTPSemiring()=delete
Deleted default constructor.
Scalar product_no_checks(Scalar x, Scalar y) const noexcept
Multiplication in an ntp semiring.
Definition matrix.hpp:6267
Scalar period() const noexcept
Get the period.
Definition matrix.hpp:6331
Scalar threshold() const noexcept
Get the threshold.
Definition matrix.hpp:6315
static constexpr Scalar scalar_one() noexcept
Get the multiplicative identity.
Definition matrix.hpp:6223
Static matrix class.
Definition matrix.hpp:1865
typename MatrixCommon::iterator iterator
Definition matrix.hpp:1912
StaticMatrix(std::initializer_list< scalar_type > const &c)
Construct a row (from std::initializer_list).
Definition matrix.hpp:1940
typename MatrixCommon::const_iterator const_iterator
Definition matrix.hpp:1915
scalar_const_reference at(size_t r, size_t c) const
Returns a const reference to the specified entry of the matrix.
scalar_reference at(size_t r, size_t c)
Returns a reference to the specified entry of the matrix.
ProdOp Prod
Definition matrix.hpp:1903
PlusOp Plus
Definition matrix.hpp:1900
scalar_type scalar_one() const noexcept
StaticMatrix(StaticMatrix &&)=default
Default move constructor.
typename MatrixCommon::scalar_const_reference scalar_const_reference
Alias for const references to the template parameter Scalar.
Definition matrix.hpp:1890
scalar_reference operator()(size_t r, size_t c)
Returns a reference to the specified entry of the matrix.
ZeroOp Zero
Definition matrix.hpp:1906
scalar_type scalar_zero() const noexcept
void transpose_no_checks()
semiring_type const * semiring() const noexcept
void product_inplace_no_checks(StaticMatrix const &x, StaticMatrix const &y)
OneOp One
Definition matrix.hpp:1909
StaticMatrix(std::initializer_list< std::initializer_list< scalar_type > > const &m)
Construct a matrix.
Definition matrix.hpp:1967
StaticRowView< PlusOp, ProdOp, ZeroOp, OneOp, C, Scalar > RowView
Definition matrix.hpp:1897
RowView row(size_t i) const
void swap(StaticMatrix &that) noexcept
static StaticMatrix one() const
Returns an identity matrix.
StaticMatrix(StaticMatrix const &)=default
Default copy constructor.
iterator begin() noexcept
Returns an iterator pointing at the first entry.
StaticMatrix(RowView const &rv)
Construct a row from a row view.
Definition matrix.hpp:2003
size_t number_of_rows() const noexcept
Returns the number of rows of the matrix.
StaticMatrix(std::vector< std::vector< scalar_type > > const &m)
Construct a matrix.
Definition matrix.hpp:1983
StaticMatrix< PlusOp, ProdOp, ZeroOp, OneOp, 1, C, Scalar > Row
Alias for the type of the rows of a StaticMatrix.
Definition matrix.hpp:1894
const_iterator cbegin() const noexcept
RowView row_no_checks(size_t i) const
StaticMatrix()=default
Default constructor.
typename MatrixCommon::scalar_reference scalar_reference
Alias for references to the template parameter Scalar.
Definition matrix.hpp:1885
size_t hash_value() const
typename MatrixCommon::scalar_type scalar_type
Alias for the template parameter Scalar.
Definition matrix.hpp:1882
scalar_const_reference operator()(size_t r, size_t c) const
Returns a const reference to the specified entry of the matrix.
StaticMatrix & operator=(StaticMatrix &&)=default
Default move assignment operator.
size_t number_of_cols() const noexcept
Returns the number of columns of the matrix.
StaticMatrix & operator=(StaticMatrix const &)=default
Default copy assignment operator.
std::pair< scalar_type, scalar_type > coords(const_iterator it) const
Class for views into a row of a matrix over a semiring.
Definition matrix.hpp:1080
typename RowViewCommon::iterator iterator
Alias for const iterators pointing at entries of a matrix.
Definition matrix.hpp:1096
typename RowViewCommon::scalar_const_reference scalar_const_reference
Alias for const references to the template parameter Scalar.
Definition matrix.hpp:1107
StaticRowView & operator=(StaticRowView &&)=default
Default move assignment operator.
typename RowViewCommon::scalar_reference scalar_reference
Alias for references to the template parameter Scalar.
Definition matrix.hpp:1102
iterator begin() noexcept
Returns a iterator pointing at the first entry.
iterator cend()
Returns a const iterator pointing one beyond the last entry of the row.
StaticRowView()=default
Default constructor.
typename matrix_type::Row Row
Alias for the type of a row in the underlying matrix.
Definition matrix.hpp:1114
typename RowViewCommon::const_iterator const_iterator
Alias for const iterators pointing at entries of a matrix.
Definition matrix.hpp:1093
const_iterator cbegin() const noexcept
Returns a const iterator pointing at the first entry.
iterator end()
Returns a iterator pointing one beyond the last entry of the row.
Scalar scalar_type
Alias for the template parameter Scalar.
Definition matrix.hpp:1099
StaticRowView(StaticRowView &&)=default
Default move constructor.
typename RowViewCommon::matrix_type matrix_type
Alias for the type of the underlying matrix.
Definition matrix.hpp:1111
StaticRowView(StaticRowView const &)=default
Default copy constructor.
static constexpr size_t size() const noexcept
Returns the size of the row.
StaticRowView & operator=(StaticRowView const &)=default
Default copy assignment operator.
StaticRowView(Row const &r)
Construct a row view from a Row.
std::string to_human_readable_repr(Action< Element, Point, Func, Traits, LeftOrRight > const &action)
Return a human readable representation of an Action object.
Bipartition operator*(Bipartition const &x, Bipartition const &y)
Multiply two bipartitions.
std::ostringstream & operator<<(std::ostringstream &os, BMat8 const &x)
Insertion operator.
StaticMatrix< BooleanPlus, BooleanProd, BooleanZero, BooleanOne, R, C, int > StaticBMat
Alias for static boolean matrices.
Definition matrix.hpp:3971
static constexpr bool IsBMat
Helper to check if a type is BMat.
Definition matrix.hpp:4029
DynamicMatrix< BooleanPlus, BooleanProd, BooleanZero, BooleanOne, int > DynamicBMat
Alias for dynamic boolean matrices.
Definition matrix.hpp:3957
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:4074
std::conditional_t< R==0||C==0, DynamicBMat, StaticBMat< R, C > > BMat
Alias template for boolean matrices.
Definition matrix.hpp:3994
NegativeInfinity const NEGATIVE_INFINITY
Value for negative infinity.
Undefined const UNDEFINED
Value for something undefined.
PositiveInfinity const POSITIVE_INFINITY
Value for positive infinity.
#define LIBSEMIGROUPS_EXCEPTION(...)
Throw a LibsemigroupsException.
Definition exception.hpp:99
std::conditional_t< R==0||C==0, DynamicIntMat< Scalar >, StaticIntMat< R, C, Scalar > > IntMat
Alias template for integer matrices.
Definition matrix.hpp:4318
DynamicMatrix< IntegerPlus< Scalar >, IntegerProd< Scalar >, IntegerZero< Scalar >, IntegerOne< Scalar >, Scalar > DynamicIntMat
Alias for dynamic integer matrices.
Definition matrix.hpp:4270
StaticMatrix< IntegerPlus< Scalar >, IntegerProd< Scalar >, IntegerZero< Scalar >, IntegerOne< Scalar >, R, C, Scalar > StaticIntMat
Alias for static integer matrices.
Definition matrix.hpp:4293
enable_if_is_same< Return, Blocks > make(Container const &cont)
Check the arguments, construct a Blocks object, and check it.
Definition bipart.hpp:856
static constexpr bool IsMaxPlusMat
Helper variable template.
Definition matrix.hpp:4644
constexpr bool IsStaticMatrix
Helper variable template.
Definition matrix.hpp:3649
constexpr bool IsDynamicMatrix
Helper variable template.
Definition matrix.hpp:3662
static constexpr bool IsIntMat
Helper variable template.
Definition matrix.hpp:4343
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:7964
static constexpr bool IsMatWithSemiring
Helper variable template.
Definition matrix.hpp:3676
static constexpr bool IsMinPlusMat
Helper variable template.
Definition matrix.hpp:4952
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:4593
DynamicMatrix< MaxPlusPlus< Scalar >, MaxPlusProd< Scalar >, MaxPlusZero< Scalar >, IntegerZero< Scalar >, Scalar > DynamicMaxPlusMat
Alias for dynamic max-plus matrices.
Definition matrix.hpp:4574
std::conditional_t< R==0||C==0, DynamicMaxPlusMat< Scalar >, StaticMaxPlusMat< R, C, Scalar > > MaxPlusMat
Alias template for max-plus matrices.
Definition matrix.hpp:4617
DynamicMatrix< MaxPlusPlus< Scalar >, MaxPlusTruncProd< T, Scalar >, MaxPlusZero< Scalar >, IntegerZero< Scalar >, Scalar > DynamicMaxPlusTruncMat
Alias for dynamic truncated max-plus matrices.
Definition matrix.hpp:5321
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:5367
StaticMatrix< MaxPlusPlus< Scalar >, MaxPlusTruncProd< T, Scalar >, MaxPlusZero< Scalar >, IntegerZero< Scalar >, R, C, Scalar > StaticMaxPlusTruncMat
Alias for static truncated max-plus matrices.
Definition matrix.hpp:5341
static constexpr bool IsMaxPlusTruncMat
Helper to check if a type is MaxPlusTruncMat.
Definition matrix.hpp:5410
DynamicMatrix< MinPlusPlus< Scalar >, MinPlusProd< Scalar >, MinPlusZero< Scalar >, IntegerZero< Scalar >, Scalar > DynamicMinPlusMat
Alias for dynamic min-plus matrices.
Definition matrix.hpp:4882
StaticMatrix< MinPlusPlus< Scalar >, MinPlusProd< Scalar >, MinPlusZero< Scalar >, IntegerZero< Scalar >, R, C, Scalar > StaticMinPlusMat
Alias for static min-plus matrices.
Definition matrix.hpp:4901
std::conditional_t< R==0||C==0, DynamicMinPlusMat< Scalar >, StaticMinPlusMat< R, C, Scalar > > MinPlusMat
Alias template for min-plus matrices.
Definition matrix.hpp:4925
DynamicMatrix< MinPlusPlus< Scalar >, MinPlusTruncProd< T, Scalar >, MinPlusZero< Scalar >, IntegerZero< Scalar >, Scalar > DynamicMinPlusTruncMat
Alias for dynamic truncated min-plus matrices.
Definition matrix.hpp:5797
StaticMatrix< MinPlusPlus< Scalar >, MinPlusTruncProd< T, Scalar >, MinPlusZero< Scalar >, IntegerZero< Scalar >, R, C, Scalar > StaticMinPlusTruncMat
Alias for static truncated min-plus matrices.
Definition matrix.hpp:5817
static constexpr bool IsMinPlusTruncMat
Helper to check if a type is MinPlusTruncMat.
Definition matrix.hpp:5887
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:5844
DynamicMatrix< NTPSemiring< Scalar >, Scalar > DynamicNTPMatWithSemiring
Alias for ntp matrices with dynamic threshold and period.
Definition matrix.hpp:6351
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:6367
static constexpr bool IsNTPMat
Helper to check if a type is NTPMat.
Definition matrix.hpp:6471
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:6428
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:6393
std::conditional_t< R==0||C==0, DynamicProjMaxPlusMat< Scalar >, StaticProjMaxPlusMat< R, C, Scalar > > ProjMaxPlusMat
Alias template for projective max-plus matrices.
Definition matrix.hpp:7055
detail::ProjMaxPlusMat< DynamicMaxPlusMat< Scalar > > DynamicProjMaxPlusMat
Alias for dynamic projective max-plus matrices with run-time dimensions.
Definition matrix.hpp:7038
static constexpr bool IsProjMaxPlusMat
Helper to check if a type is ProjMaxPlusMat.
Definition matrix.hpp:7085
detail::ProjMaxPlusMat< StaticMaxPlusMat< R, C, Scalar > > StaticProjMaxPlusMat
Alias for static projective max-plus matrices with compile-time arithmetic and dimensions.
Definition matrix.hpp:7024
T inner_product(T... args)
T lexicographical_compare(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:719
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:7483
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:7311
constexpr Scalar period(StaticNTPMat< T, P, R, C, Scalar > const &) noexcept
Returns the period of a static ntp matrix.
Definition matrix.hpp:6507
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:3755
size_t row_space_size(Mat const &x)
Returns the size of the row space of a boolean matrix.
Definition matrix.hpp:7919
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:7240
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:7175
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:7655
Namespace for everything in the libsemigroups library.
Definition action.hpp:44
Function object for returning the multiplicative identity.
Definition matrix.hpp:3905
constexpr bool operator()() const noexcept
Call operator returning the multiplication identity true of the boolean semiring.
Definition matrix.hpp:3916
Function object for addition in the boolean semiring.
Definition matrix.hpp:3851
constexpr bool operator()(bool x, bool y) const noexcept
Call operator for addition.
Definition matrix.hpp:3864
Function object for multiplication in the boolean semiring.
Definition matrix.hpp:3878
constexpr bool operator()(bool x, bool y) const noexcept
Call operator for multiplication.
Definition matrix.hpp:3891
Function object for returning the additive identity.
Definition matrix.hpp:3930
constexpr bool operator()() const noexcept
Call operator returning the additive identity false of the boolean semiring.
Definition matrix.hpp:3941
constexpr size_t operator()(Mat const &x) const noexcept
Call operator.
Definition matrix.hpp:8461
Adapter for the complexity of multiplication.
Definition adapters.hpp:128
constexpr size_t operator()(Mat const &x) const noexcept
Call operator.
Definition matrix.hpp:8490
Adapter for the degree of an element.
Definition adapters.hpp:166
constexpr size_t operator()(Mat const &x) const
Call operator.
Definition matrix.hpp:8519
Adapter for hashing.
Definition adapters.hpp:451
constexpr void operator()(Mat &, size_t) const noexcept
Call operator.
Definition matrix.hpp:8544
Adapter for increasing the degree of an element.
Definition adapters.hpp:206
Function object for returning the multiplicative identity.
Definition matrix.hpp:4244
constexpr Scalar operator()() const noexcept
Call operator returning the integer 1.
Definition matrix.hpp:4254
Function object for addition in the ring of integers.
Definition matrix.hpp:4160
constexpr Scalar operator()(Scalar x, Scalar y) const noexcept
Call operator for addition.
Definition matrix.hpp:4173
Function object for multiplication in the ring of integers.
Definition matrix.hpp:4191
constexpr Scalar operator()(Scalar x, Scalar y) const noexcept
Call operator for multiplication.
Definition matrix.hpp:4204
Function object for returning the additive identity.
Definition matrix.hpp:4219
constexpr Scalar operator()() const noexcept
Call operator returning the integer 0.
Definition matrix.hpp:4229
Function object for addition in the max-plus semiring.
Definition matrix.hpp:4462
Scalar operator()(Scalar x, Scalar y) const noexcept
Call operator for addition.
Definition matrix.hpp:4477
Function object for multiplication in the max-plus semiring.
Definition matrix.hpp:4509
Scalar operator()(Scalar x, Scalar y) const noexcept
Call operator for multiplication.
Definition matrix.hpp:4524
Function object for multiplication in truncated max-plus semirings.
Definition matrix.hpp:5096
Scalar operator()(Scalar x, Scalar y) const noexcept
Call operator for multiplication.
Definition matrix.hpp:5111
Function object for returning the additive identity of the max-plus semiring.
Definition matrix.hpp:4546
constexpr Scalar operator()() const noexcept
Call operator for additive identity.
Definition matrix.hpp:4558
Function object for addition in the min-plus semiring.
Definition matrix.hpp:4770
Scalar operator()(Scalar x, Scalar y) const noexcept
Call operator for addition.
Definition matrix.hpp:4785
Function object for multiplication in the min-plus semiring.
Definition matrix.hpp:4817
Scalar operator()(Scalar x, Scalar y) const noexcept
Call operator for multiplication.
Definition matrix.hpp:4832
Function object for multiplication in min-plus truncated semirings.
Definition matrix.hpp:5576
Scalar operator()(Scalar x, Scalar y) const noexcept
Call operator for multiplication.
Definition matrix.hpp:5589
Function object for returning the additive identity of the min-plus semiring.
Definition matrix.hpp:4854
constexpr Scalar operator()() const noexcept
Call operator for additive identity.
Definition matrix.hpp:4866
Function object for addition in ntp semirings.
Definition matrix.hpp:6083
constexpr Scalar operator()(Scalar x, Scalar y) const noexcept
Call operator for addition.
Definition matrix.hpp:6095
Function object for multiplication in an ntp semirings.
Definition matrix.hpp:6124
constexpr Scalar operator()(Scalar x, Scalar y) const noexcept
Call operator for multiplication.
Definition matrix.hpp:6138
Mat operator()(Mat const &x) const
Call operator.
Definition matrix.hpp:8578
Adapter for the identity element of the given type.
Definition adapters.hpp:251
void operator()(Mat &xy, Mat const &x, Mat const &y, size_t=0) const
Call operator.
Definition matrix.hpp:8614
Adapter for the product of two elements.
Definition adapters.hpp:289