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;
1339 template <typename U>
1340 bool operator<(U const& that) const;
1363 template <typename U>
1364 bool operator<(U const& that) const;
1458 static constexpr size_t length_impl() noexcept {
1467 template <
typename... Args>
1468 class DynamicRowView;
1507 template <
typename PlusOp,
1512 class DynamicRowView<PlusOp, ProdOp, ZeroOp, OneOp, Scalar>
1513 :
public detail::RowViewCommon<
1514 DynamicMatrix<PlusOp, ProdOp, ZeroOp, OneOp, Scalar>,
1515 DynamicRowView<PlusOp, ProdOp, ZeroOp, OneOp, Scalar>>,
1517 MatrixStaticArithmetic<PlusOp, ProdOp, ZeroOp, OneOp, Scalar> {
1519 using DynamicMatrix_ = DynamicMatrix<PlusOp, ProdOp, ZeroOp, OneOp, Scalar>;
1520 using RowViewCommon = detail::RowViewCommon<
1522 DynamicRowView<PlusOp, ProdOp, ZeroOp, OneOp, Scalar>>;
1523 friend RowViewCommon;
1527 using const_iterator =
typename RowViewCommon::const_iterator;
1530 using iterator =
typename RowViewCommon::iterator;
1533 using scalar_type = Scalar;
1536 using scalar_reference =
typename RowViewCommon::scalar_reference;
1541 using scalar_const_reference =
typename RowViewCommon::scalar_const_reference;
1545 using matrix_type =
typename RowViewCommon::matrix_type;
1548 using Row =
typename matrix_type::Row;
1551 DynamicRowView() =
default;
1554 DynamicRowView(DynamicRowView
const&) =
default;
1557 DynamicRowView(DynamicRowView&&) =
default;
1560 DynamicRowView& operator=(DynamicRowView
const&) =
default;
1563 DynamicRowView& operator=(DynamicRowView&&) =
default;
1566 explicit DynamicRowView(Row
const& r)
1567 : RowViewCommon(r), _length(r.number_of_cols()) {}
1569#ifndef LIBSEMIGROUPS_PARSED_BY_DOXYGEN
1570 using RowViewCommon::RowViewCommon;
1573 DynamicRowView(DynamicMatrix_
const*, iterator
const& it,
size_t N)
1574 : RowViewCommon(it), _length(N) {}
1576 using RowViewCommon::size;
1579 size_t size() const noexcept;
1582 iterator begin() noexcept;
1588 const_iterator cbegin() const noexcept;
1594 scalar_reference operator()(
size_t i);
1597 scalar_const_reference operator()(
size_t i) const;
1600 scalar_reference operator[](
size_t i);
1603 scalar_const_reference operator[](
size_t i) const;
1606 template <typename U>
1607 bool operator==(U const& that) const;
1610 template <typename U>
1611 bool operator!=(U const& that) const;
1614 template <typename U>
1615 bool operator<(U const& that) const;
1618 template <typename U>
1619 bool operator<(U const& that) const;
1622 Row operator+(DynamicRowView const& that);
1625 void operator+=(DynamicRowView const& that);
1628 void operator+=(scalar_type a);
1631 Row operator*(scalar_type a) const;
1634 void operator*=(scalar_type a);
1638 size_t length_impl() const noexcept {
1668 template <
typename Semiring,
typename Scalar>
1669 class DynamicRowView<Semiring, Scalar>
1670 :
public detail::RowViewCommon<DynamicMatrix<Semiring, Scalar>,
1671 DynamicRowView<Semiring, Scalar>> {
1673 using DynamicMatrix_ = DynamicMatrix<Semiring, Scalar>;
1674 friend DynamicMatrix_;
1676 = detail::RowViewCommon<DynamicMatrix_,
1677 DynamicRowView<Semiring, Scalar>>;
1678 friend RowViewCommon;
1682 using const_iterator =
typename RowViewCommon::const_iterator;
1685 using iterator =
typename RowViewCommon::iterator;
1688 using scalar_type = Scalar;
1691 using scalar_reference =
typename RowViewCommon::scalar_reference;
1696 using scalar_const_reference =
typename RowViewCommon::scalar_const_reference;
1700 using matrix_type =
typename RowViewCommon::matrix_type;
1703 using Row =
typename matrix_type::Row;
1706 DynamicRowView() =
default;
1709 DynamicRowView(DynamicRowView
const&) =
default;
1712 DynamicRowView(DynamicRowView&&) =
default;
1715 DynamicRowView& operator=(DynamicRowView
const&) =
default;
1718 DynamicRowView& operator=(DynamicRowView&&) =
default;
1721 explicit DynamicRowView(Row
const& r) : RowViewCommon(r), _matrix(&r) {}
1723#ifndef LIBSEMIGROUPS_PARSED_BY_DOXYGEN
1724 using RowViewCommon::RowViewCommon;
1727 DynamicRowView(DynamicMatrix_
const* mat, iterator
const& it,
size_t)
1728 : RowViewCommon(it), _matrix(mat) {}
1730 using RowViewCommon::size;
1733 size_t size() const noexcept;
1736 iterator begin() noexcept;
1742 const_iterator cbegin() const noexcept;
1748 scalar_reference operator()(
size_t i);
1751 scalar_const_reference operator()(
size_t i) const;
1754 scalar_reference operator[](
size_t i);
1757 scalar_const_reference operator[](
size_t i) const;
1760 template <typename U>
1761 bool operator==(U const& that) const;
1764 template <typename U>
1765 bool operator!=(U const& that) const;
1768 template <typename U>
1769 bool operator<(U const& that) const;
1772 template <typename U>
1773 bool operator<(U const& that) const;
1776 Row operator+(DynamicRowView const& that);
1779 void operator+=(DynamicRowView const& that);
1782 void operator+=(scalar_type a);
1785 Row operator*(scalar_type a) const;
1788 void operator*=(scalar_type a);
1792 size_t length_impl() const noexcept {
1793 return _matrix->number_of_cols();
1796 scalar_type plus_no_checks_impl(scalar_type x,
1797 scalar_type y)
const noexcept {
1798 return _matrix->plus_no_checks_impl(x, y);
1801 scalar_type product_no_checks_impl(scalar_type x,
1802 scalar_type y)
const noexcept {
1803 return _matrix->product_no_checks_impl(x, y);
1806 DynamicMatrix_
const* _matrix;
1846 template <
typename PlusOp,
1855 MatrixStaticArithmetic<PlusOp, ProdOp, ZeroOp, OneOp, Scalar>,
1856 public detail::MatrixCommon<
1857 std::array<Scalar, R * C>,
1858 StaticMatrix<PlusOp, ProdOp, ZeroOp, OneOp, R, C, Scalar>,
1859 StaticRowView<PlusOp, ProdOp, ZeroOp, OneOp, C, Scalar>> {
1864 using MatrixCommon = ::libsemigroups::detail::MatrixCommon<
1868 friend MatrixCommon;
1911 static constexpr size_t nr_rows = R;
1912 static constexpr size_t nr_cols = C;
1936 static_assert(R == 1,
1937 "cannot construct Matrix from the given initializer list, "
1938 "incompatible dimensions");
1939 LIBSEMIGROUPS_ASSERT(c.
size() == C);
1962 : MatrixCommon(m) {}
1977 : MatrixCommon(m) {}
1999 "cannot construct Matrix with more than one row from RowView!");
2027#ifndef LIBSEMIGROUPS_PARSED_BY_DOXYGEN
2040 LIBSEMIGROUPS_ASSERT(ptr ==
nullptr);
2049 LIBSEMIGROUPS_ASSERT(ptr ==
nullptr);
2056 LIBSEMIGROUPS_ASSERT(ptr ==
nullptr);
2062 LIBSEMIGROUPS_ASSERT(ptr ==
nullptr);
2068#ifndef LIBSEMIGROUPS_PARSED_BY_DOXYGEN
2072 LIBSEMIGROUPS_ASSERT(n == 0 || n == R);
2074#if defined(__APPLE__) && defined(__clang__) \
2075 && (__clang_major__ == 13 || __clang_major__ == 14)
2080 size_t volatile const m = R;
2085 std::fill(x.begin(), x.end(), ZeroOp()());
2086 for (
size_t r = 0; r < m; ++r) {
2087 x(r, r) = OneOp()();
2094 LIBSEMIGROUPS_ASSERT(ptr ==
nullptr);
2095 LIBSEMIGROUPS_ASSERT(n == 0 || n == R);
2103#ifdef LIBSEMIGROUPS_PARSED_BY_DOXYGEN
2503 template <typename T>
2592 template <typename U>
2593 bool operator<=(U const& that) const;
2609 template <typename U>
2610 bool operator>=(U const& that) const;
2679 using MatrixCommon::at;
2680 using MatrixCommon::begin;
2681 using MatrixCommon::cbegin;
2682 using MatrixCommon::cend;
2683 using MatrixCommon::coords;
2684 using MatrixCommon::end;
2685 using MatrixCommon::hash_value;
2686 using MatrixCommon::number_of_cols;
2687 using MatrixCommon::number_of_rows;
2688 using MatrixCommon::one;
2689 using MatrixCommon::operator!=;
2690 using MatrixCommon::operator();
2691 using MatrixCommon::operator*;
2692 using MatrixCommon::operator*=;
2693 using MatrixCommon::operator+;
2694 using MatrixCommon::operator+=;
2695 using MatrixCommon::operator<;
2696 using MatrixCommon::operator<=;
2697 using MatrixCommon::operator==;
2698 using MatrixCommon::operator>;
2699 using MatrixCommon::operator>=;
2700 using MatrixCommon::product_inplace_no_checks;
2701 using MatrixCommon::row;
2702 using MatrixCommon::row_no_checks;
2703 using MatrixCommon::rows;
2704 using MatrixCommon::scalar_one;
2705 using MatrixCommon::scalar_zero;
2706 using MatrixCommon::semiring;
2707 using MatrixCommon::swap;
2708 using MatrixCommon::transpose;
2709 using MatrixCommon::transpose_no_checks;
2717 static constexpr size_t number_of_rows_impl() noexcept {
2720 static constexpr size_t number_of_cols_impl() noexcept {
2762 template <
typename PlusOp,
2768 :
public detail::MatrixDynamicDim<Scalar>,
2769 public detail::MatrixCommon<
2770 std::vector<Scalar>,
2771 DynamicMatrix<PlusOp, ProdOp, ZeroOp, OneOp, Scalar>,
2772 DynamicRowView<PlusOp, ProdOp, ZeroOp, OneOp, Scalar>>,
2774 MatrixStaticArithmetic<PlusOp, ProdOp, ZeroOp, OneOp, Scalar> {
2775 using MatrixDynamicDim = ::libsemigroups::detail::MatrixDynamicDim<Scalar>;
2776 using MatrixCommon = ::libsemigroups::detail::MatrixCommon<
2779 DynamicRowView<PlusOp, ProdOp, ZeroOp, OneOp, Scalar>>;
2780 friend MatrixCommon;
2799 using RowView = DynamicRowView<PlusOp, ProdOp, ZeroOp, OneOp, Scalar>;
2885 : MatrixDynamicDim(1, c.size()), MatrixCommon(c) {}
2908 : MatrixDynamicDim(m.size(), m.
begin()->size()), MatrixCommon(m) {}
2925 : MatrixDynamicDim(m.size(), m.
begin()->size()), MatrixCommon(m) {}
2939 : MatrixDynamicDim(1, rv.size()), MatrixCommon(rv) {}
2941#ifndef LIBSEMIGROUPS_PARSED_BY_DOXYGEN
2943 DynamicMatrix(
void const* ptr,
size_t r,
size_t c) : DynamicMatrix(r, c) {
2945 LIBSEMIGROUPS_ASSERT(ptr ==
nullptr);
2950 : DynamicMatrix(c) {
2952 LIBSEMIGROUPS_ASSERT(ptr ==
nullptr);
2958 std::initializer_list<std::initializer_list<scalar_type>>
const& m)
2959 : DynamicMatrix(m) {
2961 LIBSEMIGROUPS_ASSERT(ptr ==
nullptr);
2964 static DynamicMatrix
one(
void const* ptr,
size_t n) {
2966 LIBSEMIGROUPS_ASSERT(ptr ==
nullptr);
2971 ~DynamicMatrix() =
default;
2987 std::fill(x.begin(), x.end(), ZeroOp()());
2988 for (
size_t r = 0; r < n; ++r) {
2989 x(r, r) = OneOp()();
2994#ifdef LIBSEMIGROUPS_PARSED_BY_DOXYGEN
3084 template <typename T>
3085 bool operator<=(T const& that) const;
3097 template <typename T>
3098 bool operator>=(T const& that) const;
3111 template <typename T>
3129 using MatrixCommon::at;
3130 using MatrixCommon::begin;
3131 using MatrixCommon::cbegin;
3132 using MatrixCommon::cend;
3133 using MatrixCommon::coords;
3134 using MatrixCommon::end;
3135 using MatrixCommon::hash_value;
3136 using MatrixCommon::number_of_cols;
3137 using MatrixCommon::number_of_rows;
3138 using MatrixCommon::one;
3139 using MatrixCommon::operator!=;
3140 using MatrixCommon::operator();
3141 using MatrixCommon::operator*;
3142 using MatrixCommon::operator*=;
3143 using MatrixCommon::operator+;
3144 using MatrixCommon::operator+=;
3145 using MatrixCommon::operator<;
3146 using MatrixCommon::operator<=;
3147 using MatrixCommon::operator==;
3148 using MatrixCommon::operator>;
3149 using MatrixCommon::operator>=;
3150 using MatrixCommon::product_inplace_no_checks;
3151 using MatrixCommon::row;
3152 using MatrixCommon::row_no_checks;
3153 using MatrixCommon::rows;
3154 using MatrixCommon::scalar_one;
3155 using MatrixCommon::scalar_zero;
3156 using MatrixCommon::semiring;
3158 using MatrixCommon::transpose;
3159 using MatrixCommon::transpose_no_checks;
3164 static_cast<MatrixDynamicDim&
>(*this).swap(
3165 static_cast<MatrixDynamicDim&
>(that));
3166 static_cast<MatrixCommon&
>(*this).swap(
static_cast<MatrixCommon&
>(that));
3170 using MatrixCommon::resize;
3211 template <
typename Semiring,
typename Scalar>
3213 :
public detail::MatrixDynamicDim<Scalar>,
3214 public detail::MatrixCommon<std::vector<Scalar>,
3215 DynamicMatrix<Semiring, Scalar>,
3216 DynamicRowView<Semiring, Scalar>,
3218 using MatrixCommon = detail::MatrixCommon<std::vector<Scalar>,
3220 DynamicRowView<Semiring, Scalar>,
3222 friend MatrixCommon;
3223 using MatrixDynamicDim = ::libsemigroups::detail::MatrixDynamicDim<Scalar>;
3283 : MatrixDynamicDim(r, c), MatrixCommon(), _semiring(sr) {
3305 : MatrixDynamicDim(rws.size(), rws.
begin()->size()),
3326 : MatrixDynamicDim(rws.size(), (rws.empty() ? 0 : rws.
begin()->size())),
3345 : MatrixDynamicDim(1, rw.size()), MatrixCommon(rw), _semiring(sr) {}
3359 : MatrixDynamicDim(1, rv.size()),
3361 _semiring(rv._matrix->
semiring()) {}
3380 std::fill(x.begin(), x.end(), x.scalar_zero());
3381 for (
size_t r = 0; r < n; ++r) {
3382 x(r, r) = x.scalar_one();
3387 ~DynamicMatrix() =
default;
3389#ifdef LIBSEMIGROUPS_PARSED_BY_DOXYGEN
3479 template <typename T>
3480 bool operator<=(T const& that) const;
3492 template <typename T>
3493 bool operator>=(T const& that) const;
3506 template <typename T>
3524 using MatrixCommon::at;
3525 using MatrixCommon::begin;
3526 using MatrixCommon::cbegin;
3527 using MatrixCommon::cend;
3528 using MatrixCommon::coords;
3529 using MatrixCommon::end;
3530 using MatrixCommon::hash_value;
3531 using MatrixCommon::number_of_cols;
3532 using MatrixCommon::number_of_rows;
3533 using MatrixCommon::one;
3534 using MatrixCommon::operator!=;
3535 using MatrixCommon::operator();
3536 using MatrixCommon::operator*;
3537 using MatrixCommon::operator*=;
3538 using MatrixCommon::operator+;
3539 using MatrixCommon::operator+=;
3540 using MatrixCommon::operator<;
3541 using MatrixCommon::operator<=;
3542 using MatrixCommon::operator==;
3543 using MatrixCommon::operator>;
3544 using MatrixCommon::operator>=;
3545 using MatrixCommon::product_inplace_no_checks;
3546 using MatrixCommon::row;
3547 using MatrixCommon::row_no_checks;
3548 using MatrixCommon::rows;
3549 using MatrixCommon::scalar_one;
3550 using MatrixCommon::scalar_zero;
3551 using MatrixCommon::semiring;
3553 using MatrixCommon::transpose;
3554 using MatrixCommon::transpose_no_checks;
3559 static_cast<MatrixDynamicDim&
>(*this).swap(
3560 static_cast<MatrixDynamicDim&
>(that));
3561 static_cast<MatrixCommon&
>(*this).swap(
static_cast<MatrixCommon&
>(that));
3566 using MatrixCommon::resize;
3568 scalar_type plus_no_checks_impl(scalar_type x,
3569 scalar_type y)
const noexcept {
3570 return _semiring->plus_no_checks(x, y);
3573 scalar_type product_no_checks_impl(scalar_type x,
3574 scalar_type y)
const noexcept {
3575 return _semiring->product_no_checks(x, y);
3578 scalar_type one_impl() const noexcept {
3579 return _semiring->scalar_one();
3582 scalar_type zero_impl() const noexcept {
3583 return _semiring->scalar_zero();
3586 Semiring
const* semiring_impl() const noexcept {
3590 Semiring
const* _semiring;
3599 template <
typename T>
3600 struct IsStaticMatrixHelper : std::false_type {};
3602 template <
typename PlusOp,
3609 struct IsStaticMatrixHelper<
3610 StaticMatrix<PlusOp, ProdOp, ZeroOp, OneOp, R, C, Scalar>>
3611 : std::true_type {};
3613 template <
typename T>
3614 struct IsMatWithSemiringHelper : std::false_type {};
3616 template <
typename Semiring,
typename Scalar>
3617 struct IsMatWithSemiringHelper<DynamicMatrix<Semiring, Scalar>>
3618 : std::true_type {};
3620 template <
typename S,
typename T =
void>
3621 struct IsTruncMatHelper : std::false_type {};
3635 template <
typename T>
3648 template <
typename T>
3661 template <
typename T>
3663 = detail::IsMatWithSemiringHelper<T>::value;
3667 template <
typename T>
3668 static constexpr bool IsTruncMat = IsTruncMatHelper<T>::value;
3670 template <
typename Mat>
3671 void throw_if_semiring_nullptr(Mat
const& m) {
3674 "the matrix's pointer to a semiring is nullptr!")
3678 template <
typename Mat,
typename Container>
3679 auto throw_if_bad_dim(Container
const& m)
3680 -> std::enable_if_t<IsStaticMatrix<Mat>> {
3682 uint64_t
const R = m.size();
3683 uint64_t
const C = m.begin()->size();
3684 if (R != Mat::nr_rows || C != Mat::nr_cols) {
3686 "invalid argument, cannot initialize an {}x{} matrix with compile "
3687 "time dimension, with an {}x{} container",
3695 template <
typename Mat,
typename Container>
3696 auto throw_if_bad_dim(Container
const&)
3697 -> std::enable_if_t<IsDynamicMatrix<Mat>> {}
3723 template <
typename Mat>
3725 -> std::enable_if_t<!detail::IsTruncMat<Mat>,
3726 typename Mat::scalar_type> {
3743 template <
typename Mat>
3744 constexpr auto threshold(Mat
const&)
noexcept
3746 typename Mat::scalar_type> {
3747 return detail::IsTruncMatHelper<Mat>::threshold;
3765 template <
typename Mat>
3768 typename Mat::scalar_type> {
3769 return x.semiring()->threshold();
3927 = DynamicMatrix<BooleanPlus, BooleanProd, BooleanZero, BooleanOne, int>;
3940 template <
size_t R,
size_t C>
3963 template <
size_t R = 0,
size_t C = R>
3965 = std::conditional_t<R == 0 || C == 0, DynamicBMat, StaticBMat<R, C>>;
3968 template <
typename T>
3971 template <
size_t R,
size_t C>
3977 template <
typename T>
3978 struct BitSetCapacity {
3979 static constexpr size_t value = BitSet<1>::max_size();
3982 template <
size_t R,
size_t C>
3984 static_assert(R == C,
"the number of rows and columns must be equal");
3985 static constexpr size_t value = R;
3999 template <
typename T>
4000 static constexpr bool IsBMat = detail::IsBMatHelper<T>::value;
4011 template <
typename Scalar>
4013 if constexpr (std::is_same_v<Scalar, NegativeInfinity>
4014 || std::is_signed_v<Scalar>) {
4022 return fmt::format(
"{}", a);
4044 template <
typename Mat>
4046 using scalar_type =
typename Mat::scalar_type;
4048 m.cbegin(), m.cend(), [](scalar_type x) { return x == 0 || x == 1; });
4049 if (it != m.cend()) {
4050 auto [r, c] = m.coords(it);
4052 "invalid entry, expected 0 or 1 but found {} in entry ({}, {})",
4053 detail::entry_repr(*it),
4076 template <
typename Mat>
4077 std::enable_if_t<IsBMat<Mat>>
4079 if (val != 0 && val != 1) {
4081 detail::entry_repr(val));
4130 template <
typename Scalar>
4144 constexpr Scalar
operator()(Scalar x, Scalar y)
const noexcept {
4161 template <
typename Scalar>
4175 constexpr Scalar
operator()(Scalar x, Scalar y)
const noexcept {
4189 template <
typename Scalar>
4200 constexpr Scalar
operator()() const noexcept {
4214 template <
typename Scalar>
4225 constexpr Scalar
operator()() const noexcept {
4240 template <
typename Scalar>
4263 template <
size_t R,
size_t C,
typename Scalar>
4288 template <
size_t R = 0,
size_t C = R,
typename Scalar =
int>
4289 using IntMat = std::conditional_t<R == 0 || C == 0,
4293 template <
typename T>
4296 template <
size_t R,
size_t C,
typename Scalar>
4299 template <
typename Scalar>
4313 template <
typename T>
4314 static constexpr bool IsIntMat = detail::IsIntMatHelper<T>::value;
4331 template <
typename Mat>
4333 using scalar_type =
typename Mat::scalar_type;
4334 auto it =
std::find_if(x.cbegin(), x.cend(), [](scalar_type val) {
4335 return val == POSITIVE_INFINITY || val == NEGATIVE_INFINITY;
4337 if (it != x.cend()) {
4338 auto [r, c] = x.coords(it);
4340 "invalid entry, expected entries to be integers, "
4341 "but found {} in entry ({}, {})",
4342 detail::entry_repr(*it),
4364 template <
typename Mat>
4365 std::enable_if_t<IsIntMat<Mat>>
4369 "invalid entry, expected entries to be integers, "
4371 detail::entry_repr(val));
4432 template <
typename Scalar>
4435 "MaxPlus requires a signed integer type as parameter!");
4448 Scalar
operator()(Scalar x, Scalar y)
const noexcept {
4479 template <
typename Scalar>
4482 "MaxPlus requires a signed integer type as parameter!");
4495 Scalar
operator()(Scalar x, Scalar y)
const noexcept {
4516 template <
typename Scalar>
4519 "MaxPlus requires a signed integer type as parameter!");
4529 constexpr Scalar
operator()() const noexcept {
4544 template <
typename Scalar>
4563 template <
size_t R,
size_t C,
typename Scalar>
4587 template <
size_t R = 0,
size_t C = R,
typename Scalar =
int>
4588 using MaxPlusMat = std::conditional_t<R == 0 || C == 0,
4593 template <
typename T>
4596 template <
size_t R,
size_t C,
typename Scalar>
4600 template <
typename Scalar>
4614 template <
typename T>
4615 static constexpr bool IsMaxPlusMat = detail::IsMaxPlusMatHelper<T>::value;
4633 template <
typename Mat>
4635 -> std::enable_if_t<IsMaxPlusMat<Mat>> {
4636 using scalar_type =
typename Mat::scalar_type;
4637 auto it =
std::find_if(x.cbegin(), x.cend(), [](scalar_type val) {
4638 return val == POSITIVE_INFINITY;
4640 if (it != x.cend()) {
4641 auto [r, c] = x.coords(it);
4643 "invalid entry, expected entries to be integers or {} (= {}), "
4644 "but found {} (= {}) in entry ({}, {})",
4669 template <
typename Mat>
4670 std::enable_if_t<IsMaxPlusMat<Mat>>
4673 using scalar_type =
typename Mat::scalar_type;
4675 "integers or {} (= {}) but found {} (= {})",
4740 template <
typename Scalar>
4743 "MinPlus requires a signed integer type as parameter!");
4756 Scalar
operator()(Scalar x, Scalar y)
const noexcept {
4787 template <
typename Scalar>
4790 "MinPlus requires a signed integer type as parameter!");
4803 Scalar
operator()(Scalar x, Scalar y)
const noexcept {
4824 template <
typename Scalar>
4827 "MinPlus requires a signed integer type as parameter!");
4837 constexpr Scalar
operator()() const noexcept {
4852 template <
typename Scalar>
4871 template <
size_t R,
size_t C,
typename Scalar>
4895 template <
size_t R = 0,
size_t C = R,
typename Scalar =
int>
4896 using MinPlusMat = std::conditional_t<R == 0 || C == 0,
4901 template <
typename T>
4904 template <
size_t R,
size_t C,
typename Scalar>
4908 template <
typename Scalar>
4922 template <
typename T>
4923 static constexpr bool IsMinPlusMat = detail::IsMinPlusMatHelper<T>::value;
4941 template <
typename Mat>
4943 using scalar_type =
typename Mat::scalar_type;
4944 auto it =
std::find_if(x.cbegin(), x.cend(), [](scalar_type val) {
4945 return val == NEGATIVE_INFINITY;
4947 if (it != x.cend()) {
4948 auto [r, c] = x.coords(it);
4950 "invalid entry, expected entries to be integers or {} (= {}), "
4951 "but found {} (= {}) in entry ({}, {})",
4976 template <
typename Mat>
4977 std::enable_if_t<IsMinPlusMat<Mat>>
4980 using scalar_type =
typename Mat::scalar_type;
4982 "integers or {} (= {}) but found {} (= {})",
5066 template <
size_t T,
typename Scalar>
5069 "MaxPlus requires a signed integer type as parameter!");
5082 Scalar
operator()(Scalar x, Scalar y)
const noexcept {
5083 LIBSEMIGROUPS_ASSERT((x >= 0 && x <=
static_cast<Scalar
>(T))
5085 LIBSEMIGROUPS_ASSERT((y >= 0 && y <=
static_cast<Scalar
>(T))
5090 return std::min(x + y,
static_cast<Scalar
>(T));
5108 template <
typename Scalar =
int>
5111 "MaxPlus requires a signed integer type as parameter!");
5169 static constexpr Scalar
scalar_one() noexcept {
5212 LIBSEMIGROUPS_ASSERT((x >= 0 && x <= _threshold)
5214 LIBSEMIGROUPS_ASSERT((y >= 0 && y <= _threshold)
5219 return std::min(x + y, _threshold);
5247 LIBSEMIGROUPS_ASSERT((x >= 0 && x <= _threshold)
5249 LIBSEMIGROUPS_ASSERT((y >= 0 && y <= _threshold)
5276 Scalar
const _threshold;
5291 template <
size_t T,
typename Scalar>
5311 template <
size_t T,
size_t R,
size_t C,
typename Scalar>
5337 template <
size_t T = 0,
size_t R = 0,
size_t C = R,
typename Scalar =
int>
5340 std::conditional_t<T == 0,
5341 DynamicMatrix<MaxPlusTruncSemiring<Scalar>, Scalar>,
5346 template <
typename T>
5349 template <
size_t T,
size_t R,
size_t C,
typename Scalar>
5352 static constexpr Scalar threshold = T;
5355 template <
size_t T,
typename Scalar>
5358 static constexpr Scalar threshold = T;
5361 template <
typename Scalar>
5362 struct IsMaxPlusTruncMatHelper<
5364 static constexpr Scalar threshold =
UNDEFINED;
5379 template <
typename T>
5381 = detail::IsMaxPlusTruncMatHelper<T>::value;
5384 template <
typename T>
5385 struct IsTruncMatHelper<T,
std::enable_if_t<IsMaxPlusTruncMat<T>>>
5387 static constexpr typename T::scalar_type threshold
5388 = IsMaxPlusTruncMatHelper<T>::threshold;
5411 template <
typename Mat>
5414 detail::throw_if_semiring_nullptr(m);
5416 using scalar_type =
typename Mat::scalar_type;
5419 return x == NEGATIVE_INFINITY || (0 <= x && x <= t);
5421 if (it != m.cend()) {
5422 auto [r, c] = m.coords(it);
5424 "invalid entry, expected values in {{0, 1, ..., {}, {} (= {})}} "
5425 "but found {} in entry ({}, {})",
5429 detail::entry_repr(*it),
5454 template <
typename Mat>
5455 std::enable_if_t<IsMaxPlusTruncMat<Mat>>
5457 detail::throw_if_semiring_nullptr(m);
5458 using scalar_type =
typename Mat::scalar_type;
5462 "invalid entry, expected values in {{0, 1, ..., {}, -{} (= {})}} "
5467 detail::entry_repr(val));
5546 template <
size_t T,
typename Scalar>
5560 Scalar
operator()(Scalar x, Scalar y)
const noexcept {
5561 LIBSEMIGROUPS_ASSERT((x >= 0 && x <=
static_cast<Scalar
>(T))
5563 LIBSEMIGROUPS_ASSERT((y >= 0 && y <=
static_cast<Scalar
>(T))
5568 return std::min(x + y,
static_cast<Scalar
>(T));
5585 template <
typename Scalar =
int>
5588 "MinPlus requires an integral type as parameter!");
5644 static constexpr Scalar
scalar_one() noexcept {
5688 LIBSEMIGROUPS_ASSERT((x >= 0 && x <= _threshold)
5690 LIBSEMIGROUPS_ASSERT((y >= 0 && y <= _threshold)
5695 return std::min(x + y, _threshold);
5723 LIBSEMIGROUPS_ASSERT((x >= 0 && x <= _threshold)
5725 LIBSEMIGROUPS_ASSERT((y >= 0 && y <= _threshold)
5752 Scalar
const _threshold;
5767 template <
size_t T,
typename Scalar>
5787 template <
size_t T,
size_t R,
size_t C,
typename Scalar>
5814 template <
size_t T = 0,
size_t R = 0,
size_t C = R,
typename Scalar =
int>
5817 std::conditional_t<T == 0,
5818 DynamicMatrix<MinPlusTruncSemiring<Scalar>, Scalar>,
5823 template <
typename T>
5826 template <
size_t T,
size_t R,
size_t C,
typename Scalar>
5829 static constexpr Scalar threshold = T;
5832 template <
size_t T,
typename Scalar>
5835 static constexpr Scalar threshold = T;
5838 template <
typename Scalar>
5839 struct IsMinPlusTruncMatHelper<
5841 static constexpr Scalar threshold =
UNDEFINED;
5856 template <
typename T>
5858 = detail::IsMinPlusTruncMatHelper<T>::value;
5861 template <
typename T>
5862 struct IsTruncMatHelper<T,
std::enable_if_t<IsMinPlusTruncMat<T>>>
5864 static constexpr typename T::scalar_type threshold
5865 = IsMinPlusTruncMatHelper<T>::threshold;
5889 template <
typename Mat>
5892 detail::throw_if_semiring_nullptr(m);
5894 using scalar_type =
typename Mat::scalar_type;
5897 return x == POSITIVE_INFINITY || (0 <= x && x <= t);
5899 if (it != m.cend()) {
5904 "invalid entry, expected values in {{0, 1, ..., {}, {} (= {})}} "
5905 "but found {} in entry ({}, {})",
5909 detail::entry_repr(*it),
5934 template <
typename Mat>
5935 std::enable_if_t<IsMinPlusTruncMat<Mat>>
5937 detail::throw_if_semiring_nullptr(m);
5939 using scalar_type =
typename Mat::scalar_type;
5943 "invalid entry, expected values in {{0, 1, ..., {}, {} (= {})}} "
5948 detail::entry_repr(val));
6011 template <
size_t T,
size_t P,
typename Scalar>
6012 constexpr Scalar thresholdperiod(Scalar x)
noexcept {
6014 return T + (x - T) % P;
6019 template <
typename Scalar>
6020 inline Scalar thresholdperiod(Scalar x,
6022 Scalar period)
noexcept {
6023 if (x > threshold) {
6053 template <
size_t T,
size_t P,
typename Scalar>
6066 constexpr Scalar
operator()(Scalar x, Scalar y)
const noexcept {
6067 return detail::thresholdperiod<T, P>(x + y);
6094 template <
size_t T,
size_t P,
typename Scalar>
6109 constexpr Scalar
operator()(Scalar x, Scalar y)
const noexcept {
6110 return detail::thresholdperiod<T, P>(x * y);
6128 template <
typename Scalar =
size_t>
6171 NTPSemiring(Scalar t, Scalar p) : _period(p), _threshold(t) {
6175 "expected non-negative value for 1st argument, found {}", t);
6180 "expected positive value for 2nd argument, found {}", p);
6194 static constexpr Scalar
scalar_one() noexcept {
6239 LIBSEMIGROUPS_ASSERT(x >= 0 && x <= _period + _threshold - 1);
6240 LIBSEMIGROUPS_ASSERT(y >= 0 && y <= _period + _threshold - 1);
6241 return detail::thresholdperiod(x * y, _threshold, _period);
6269 LIBSEMIGROUPS_ASSERT(x >= 0 && x <= _period + _threshold - 1);
6270 LIBSEMIGROUPS_ASSERT(y >= 0 && y <= _period + _threshold - 1);
6271 return detail::thresholdperiod(x + y, _threshold, _period);
6302 Scalar
period() const noexcept {
6321 template <
typename Scalar>
6337 template <
size_t T,
size_t P,
typename Scalar>
6363 template <
size_t T,
size_t P,
size_t R,
size_t C,
typename Scalar>
6394 template <
size_t T = 0,
6398 typename Scalar =
size_t>
6399 using NTPMat = std::conditional_t<
6401 std::conditional_t<T == 0 && P == 0,
6407 template <
typename T>
6410 template <
typename Scalar>
6412 static constexpr Scalar threshold =
UNDEFINED;
6413 static constexpr Scalar period =
UNDEFINED;
6416 template <
size_t T,
size_t P,
typename Scalar>
6419 static constexpr Scalar threshold = T;
6420 static constexpr Scalar period = P;
6423 template <
size_t T,
size_t P,
size_t R,
size_t C,
typename Scalar>
6425 static constexpr Scalar threshold = T;
6426 static constexpr Scalar period = P;
6441 template <
typename U>
6442 static constexpr bool IsNTPMat = detail::IsNTPMatHelper<U>::value;
6445 template <
typename T>
6447 static constexpr typename T::scalar_type threshold
6448 = IsNTPMatHelper<T>::threshold;
6449 static constexpr typename T::scalar_type period
6450 = IsNTPMatHelper<T>::period;
6477 template <
size_t T,
size_t P,
size_t R,
size_t C,
typename Scalar>
6499 template <
size_t T,
size_t P,
typename Scalar>
6520 template <
typename Scalar>
6522 return x.semiring()->period();
6547 template <
typename Mat>
6549 detail::throw_if_semiring_nullptr(m);
6551 using scalar_type =
typename Mat::scalar_type;
6555 return (0 <= x && x < p + t);
6557 if (it != m.cend()) {
6562 "invalid entry, expected values in {{0, 1, ..., {}}}, but "
6563 "found {} in entry ({}, {})",
6565 detail::entry_repr(*it),
6592 template <
typename Mat>
6593 std::enable_if_t<IsNTPMat<Mat>>
6595 detail::throw_if_semiring_nullptr(m);
6596 using scalar_type =
typename Mat::scalar_type;
6599 if (val < 0 || val >= p + t) {
6601 "invalid entry, expected values in {{0, 1, ..., {}}}, but "
6604 detail::entry_repr(val));
6614 template <
typename T>
6617 using scalar_type =
typename T::scalar_type;
6618 using scalar_reference =
typename T::scalar_reference;
6619 using scalar_const_reference =
typename T::scalar_const_reference;
6620 using semiring_type = void;
6622 using container_type =
typename T::container_type;
6623 using iterator =
typename T::iterator;
6624 using const_iterator =
typename T::const_iterator;
6626 using underlying_matrix_type = T;
6628 using RowView =
typename T::RowView;
6634 using Row =
typename T::Row;
6636 scalar_type scalar_one() const noexcept {
6637 return _underlying_mat.scalar_one();
6640 scalar_type scalar_zero() const noexcept {
6641 return _underlying_mat.scalar_zero();
6648 ProjMaxPlusMat() : _is_normalized(false), _underlying_mat() {}
6649 ProjMaxPlusMat(ProjMaxPlusMat
const&) =
default;
6650 ProjMaxPlusMat(ProjMaxPlusMat&&) =
default;
6651 ProjMaxPlusMat& operator=(ProjMaxPlusMat
const&) =
default;
6652 ProjMaxPlusMat& operator=(ProjMaxPlusMat&&) =
default;
6654 ProjMaxPlusMat(
size_t r,
size_t c)
6655 : _is_normalized(false), _underlying_mat(r, c) {}
6659 typename underlying_matrix_type::semiring_type
const* semiring,
6662 : _is_normalized(false), _underlying_mat(semiring, r, c) {}
6664 explicit ProjMaxPlusMat(std::vector<std::vector<scalar_type>>
const& m)
6665 : _is_normalized(false), _underlying_mat(m) {
6670 std::initializer_list<std::initializer_list<scalar_type>>
const& m)
6672 std::vector<std::vector<scalar_type>>(m.begin(), m.
end())) {}
6674 ~ProjMaxPlusMat() =
default;
6676 ProjMaxPlusMat one()
const {
6677 auto result = ProjMaxPlusMat(_underlying_mat.one());
6681 static ProjMaxPlusMat one(
size_t n) {
6682 return ProjMaxPlusMat(T::one(n));
6689 bool operator==(ProjMaxPlusMat
const& that)
const {
6692 return _underlying_mat == that._underlying_mat;
6695 bool operator!=(ProjMaxPlusMat
const& that)
const {
6696 return !(_underlying_mat == that._underlying_mat);
6699 bool operator<(ProjMaxPlusMat
const& that)
const {
6702 return _underlying_mat < that._underlying_mat;
6705 bool operator>(ProjMaxPlusMat
const& that)
const {
6706 return that < *
this;
6709 template <
typename Thing>
6710 bool operator>=(Thing
const& that)
const {
6712 return that < *
this || that == *
this;
6716 template <
typename Thing>
6717 bool operator<=(Thing
const& that)
const {
6719 return *
this < that || that == *
this;
6726 scalar_reference operator()(
size_t r,
size_t c) {
6731 _is_normalized =
false;
6732 return _underlying_mat(r, c);
6735 scalar_reference at(
size_t r,
size_t c) {
6737 return this->operator()(r, c);
6740 scalar_const_reference operator()(
size_t r,
size_t c)
const {
6742 return _underlying_mat(r, c);
6745 scalar_const_reference at(
size_t r,
size_t c)
const {
6747 return this->operator()(r, c);
6750 size_t number_of_rows() const noexcept {
6751 return _underlying_mat.number_of_rows();
6754 size_t number_of_cols() const noexcept {
6755 return _underlying_mat.number_of_cols();
6758 size_t hash_value()
const {
6760 return Hash<T>()(_underlying_mat);
6767 void product_inplace_no_checks(ProjMaxPlusMat
const& A,
6768 ProjMaxPlusMat
const& B) {
6769 _underlying_mat.product_inplace_no_checks(A._underlying_mat,
6774 void operator+=(ProjMaxPlusMat
const& that) {
6775 _underlying_mat += that._underlying_mat;
6779 void operator*=(scalar_type a) {
6780 _underlying_mat *= a;
6784 void operator+=(scalar_type a) {
6785 _underlying_mat += a;
6789 ProjMaxPlusMat operator*(scalar_type a)
const {
6790 ProjMaxPlusMat result(*
this);
6795 ProjMaxPlusMat operator+(scalar_type a)
const {
6796 ProjMaxPlusMat result(*
this);
6805 ProjMaxPlusMat operator+(ProjMaxPlusMat
const& that)
const {
6806 return ProjMaxPlusMat(_underlying_mat + that._underlying_mat);
6809 ProjMaxPlusMat operator*(ProjMaxPlusMat
const& that)
const {
6810 return ProjMaxPlusMat(_underlying_mat * that._underlying_mat);
6821 iterator begin() noexcept {
6826 _is_normalized =
false;
6827 return _underlying_mat.begin();
6830 iterator
end() noexcept {
6835 _is_normalized =
false;
6836 return _underlying_mat.end();
6839 const_iterator begin() const noexcept {
6841 return _underlying_mat.begin();
6844 const_iterator
end() const noexcept {
6846 return _underlying_mat.end();
6849 const_iterator cbegin() const noexcept {
6851 return _underlying_mat.cbegin();
6854 const_iterator cend() const noexcept {
6856 return _underlying_mat.cend();
6863 void swap(ProjMaxPlusMat& that)
noexcept {
6864 std::swap(_underlying_mat, that._underlying_mat);
6868 _underlying_mat.transpose();
6871 void transpose_no_checks() noexcept {
6872 _underlying_mat.transpose_no_checks();
6879 RowView row(
size_t i)
const {
6881 return _underlying_mat.row(i);
6884 template <
typename C>
6885 void rows(C& x)
const {
6887 return _underlying_mat.rows(x);
6894 friend std::ostream& operator<<(std::ostream& os,
6895 ProjMaxPlusMat
const& x) {
6897 os << detail::to_string(x._underlying_mat);
6901 T
const& underlying_matrix() const noexcept {
6903 return _underlying_mat;
6907 explicit ProjMaxPlusMat(T&& mat)
6908 : _is_normalized(false), _underlying_mat(std::
move(mat)) {
6912 void normalize(
bool force =
false)
const {
6913 if ((_is_normalized && !force)
6914 || (_underlying_mat.number_of_rows() == 0)
6915 || (_underlying_mat.number_of_cols() == 0)) {
6916 _is_normalized =
true;
6920 _underlying_mat.cend());
6922 _underlying_mat.end(),
6923 [&n](scalar_type& s) {
6924 if (s != NEGATIVE_INFINITY) {
6928 _is_normalized =
true;
6931 mutable bool _is_normalized;
6932 mutable T _underlying_mat;
6994 template <
size_t R,
size_t C,
typename Scalar>
6996 = detail::ProjMaxPlusMat<StaticMaxPlusMat<R, C, Scalar>>;
7009 template <
typename Scalar>
7011 = detail::ProjMaxPlusMat<DynamicMaxPlusMat<Scalar>>;
7027 template <
size_t R = 0,
size_t C = R,
typename Scalar =
int>
7033 template <
typename T>
7036 template <
size_t R,
size_t C,
typename Scalar>
7040 template <
typename Scalar>
7056 template <
typename T>
7058 = detail::IsProjMaxPlusMatHelper<T>::value;
7078 template <
typename Mat>
7079 constexpr std::enable_if_t<IsProjMaxPlusMat<Mat>>
7101 template <
typename Mat>
7102 constexpr std::enable_if_t<IsProjMaxPlusMat<Mat>>
7147 template <
typename Mat>
7148 Mat
pow(Mat
const& x,
typename Mat::scalar_type e) {
7149 using scalar_type =
typename Mat::scalar_type;
7154 "negative exponent, expected value >= 0, found {}", e);
7160 typename Mat::semiring_type
const* sr =
nullptr;
7174 auto z = (e % 2 == 0 ? x.one() : y);
7176 Mat tmp(sr, x.number_of_rows(), x.number_of_cols());
7178 tmp.product_inplace_no_checks(y, y);
7182 tmp.product_inplace_no_checks(z, y);
7212 template <
typename Mat,
typename = std::enable_if_t<IsDynamicMatrix<Mat>>>
7239 template <
typename Mat,
typename = std::enable_if_t<IsStaticMatrix<Mat>>>
7240 detail::StaticVector1<typename Mat::RowView, Mat::nr_rows>
7241 rows(Mat
const& x) {
7242 detail::StaticVector1<typename Mat::RowView, Mat::nr_rows> container;
7283 template <
typename Mat,
size_t R,
size_t C,
typename Container>
7285 detail::StaticVector1<BitSet<C>, R>& result) {
7286 using RowView =
typename Mat::RowView;
7287 using value_type =
typename std::decay_t<Container>::value_type;
7289 static_assert(
IsBMat<Mat>,
"IsBMat<Mat> must be true!");
7292 "Container::value_type must equal Mat::RowView or "
7293 "std::vector<bool>!!");
7294 static_assert(R <= BitSet<1>::max_size(),
7295 "R must be at most BitSet<1>::max_size()!");
7296 static_assert(C <= BitSet<1>::max_size(),
7297 "C must be at most BitSet<1>::max_size()!");
7298 LIBSEMIGROUPS_ASSERT(views.size() <= R);
7299 LIBSEMIGROUPS_ASSERT(views.empty() || views[0].size() <= C);
7300 for (
auto const& v : views) {
7301 result.emplace_back(v.cbegin(), v.cend());
7337 template <
typename Mat,
size_t R,
size_t C,
typename Container>
7339 using RowView =
typename Mat::RowView;
7340 using value_type =
typename std::decay_t<Container>::value_type;
7341 static_assert(
IsBMat<Mat>,
"IsBMat<Mat> must be true!");
7344 "Container::value_type must equal Mat::RowView or "
7345 "std::vector<bool>!!");
7346 static_assert(R <= BitSet<1>::max_size(),
7347 "R must be at most BitSet<1>::max_size()!");
7348 static_assert(C <= BitSet<1>::max_size(),
7349 "C must be at most BitSet<1>::max_size()!");
7350 LIBSEMIGROUPS_ASSERT(views.size() <= R);
7351 LIBSEMIGROUPS_ASSERT(views.empty() || views[0].size() <= C);
7352 detail::StaticVector1<BitSet<C>, R> result;
7386 template <
typename Mat,
size_t R,
size_t C>
7388 detail::StaticVector1<BitSet<C>, R>& result) {
7389 static_assert(
IsBMat<Mat>,
"IsBMat<Mat> must be true!");
7390 static_assert(R <= BitSet<1>::max_size(),
7391 "R must be at most BitSet<1>::max_size()!");
7392 static_assert(C <= BitSet<1>::max_size(),
7393 "C must be at most BitSet<1>::max_size()!");
7394 LIBSEMIGROUPS_ASSERT(x.number_of_cols() <= C);
7395 LIBSEMIGROUPS_ASSERT(x.number_of_rows() <= R);
7418 template <
typename Mat>
7420 static_assert(
IsBMat<Mat>,
"IsBMat<Mat> must be true!");
7421 LIBSEMIGROUPS_ASSERT(x.number_of_rows() <= BitSet<1>::max_size());
7422 LIBSEMIGROUPS_ASSERT(x.number_of_cols() <= BitSet<1>::max_size());
7423 size_t const M = detail::BitSetCapacity<Mat>::value;
7455 template <
typename Mat,
typename Container>
7457 using value_type =
typename std::decay_t<Container>::value_type;
7458 static_assert(
IsBMat<Mat>,
"IsBMat<Mat> must be true!");
7459 static_assert(IsBitSet<value_type> || detail::IsStdBitSet<value_type>,
7460 "Container::value_type must be BitSet or std::bitset");
7461 LIBSEMIGROUPS_ASSERT(
rows.size() <= BitSet<1>::max_size());
7462 LIBSEMIGROUPS_ASSERT(
rows.empty()
7463 ||
rows[0].size() <= BitSet<1>::max_size());
7468 for (
size_t i = 0; i <
rows.size(); ++i) {
7471 for (
size_t j = 0; j < i; ++j) {
7476 for (
size_t j = i + 1; j <
rows.size(); ++j) {
7481 if (cup !=
rows[i]) {
7510 template <
typename Mat,
typename Container>
7512 using value_type =
typename std::decay_t<Container>::value_type;
7513 static_assert(
IsBMat<Mat>,
"IsBMat<Mat> must be true!");
7514 static_assert(IsBitSet<value_type> || detail::IsStdBitSet<value_type>,
7515 "Container::value_type must be BitSet or std::bitset");
7516 LIBSEMIGROUPS_ASSERT(
rows.size() <= BitSet<1>::max_size());
7517 LIBSEMIGROUPS_ASSERT(
rows.empty()
7518 ||
rows[0].size() <= BitSet<1>::max_size());
7519 std::decay_t<Container> result;
7551 template <typename Mat, size_t M = detail::BitSetCapacity<Mat>::value>
7553 static_assert(
IsBMat<Mat>,
"IsBMat<Mat> must be true!");
7554 LIBSEMIGROUPS_ASSERT(x.number_of_rows() <= BitSet<1>::max_size());
7555 LIBSEMIGROUPS_ASSERT(x.number_of_cols() <= BitSet<1>::max_size());
7556 detail::StaticVector1<BitSet<M>, M> result;
7584 template <
typename Mat,
typename Container>
7586 using value_type =
typename Container::value_type;
7587 static_assert(
IsBMat<Mat>,
"IsBMat<Mat> must be true!");
7588 static_assert(IsBitSet<value_type> || detail::IsStdBitSet<value_type>,
7589 "Container::value_type must be BitSet or std::bitset");
7590 LIBSEMIGROUPS_ASSERT(x.number_of_rows() <= BitSet<1>::max_size());
7591 LIBSEMIGROUPS_ASSERT(x.number_of_cols() <= BitSet<1>::max_size());
7626 template <
typename Mat,
typename Container>
7627 std::enable_if_t<IsMaxPlusTruncMat<Mat>>
7628 row_basis(Container&& views, std::decay_t<Container>& result) {
7629 using value_type =
typename std::decay_t<Container>::value_type;
7631 "Container::value_type must be Mat::RowView");
7632 using scalar_type =
typename Mat::scalar_type;
7633 using Row =
typename Mat::Row;
7635 if (views.empty()) {
7639 LIBSEMIGROUPS_ASSERT(result.empty());
7644 for (
size_t r1 = 0; r1 < views.size(); ++r1) {
7645 if (r1 == 0 || views[r1] != views[r1 - 1]) {
7646 std::fill(tmp1.begin(), tmp1.end(), tmp1.scalar_zero());
7647 for (
size_t r2 = 0; r2 < r1; ++r2) {
7649 for (
size_t c = 0; c < tmp1.number_of_cols(); ++c) {
7650 if (views[r2][c] == tmp1.scalar_zero()) {
7653 if (views[r1][c] >= views[r2][c]) {
7656 =
std::min(max_scalar, views[r1][c] - views[r2][c]);
7659 max_scalar = tmp1.scalar_zero();
7663 if (max_scalar != tmp1.scalar_zero()) {
7664 tmp1 += views[r2] * max_scalar;
7667 if (tmp1 != views[r1]) {
7668 result.push_back(views[r1]);
7704 template <
typename Mat,
typename Container>
7705 std::enable_if_t<IsBMat<Mat>>
row_basis(Container&& views,
7706 std::decay_t<Container>& result) {
7707 using RowView =
typename Mat::RowView;
7708 using value_type =
typename std::decay_t<Container>::value_type;
7712 "Container::value_type must equal Mat::RowView or "
7713 "std::vector<bool>!!");
7715 if (views.empty()) {
7720 size_t const M = detail::BitSetCapacity<Mat>::value;
7722 using bitset_type =
typename decltype(br)::value_type;
7726 LIBSEMIGROUPS_ASSERT(br.size() == views.size());
7727 for (
size_t i = 0; i < br.size(); ++i) {
7728 lookup.
insert({br[i], i});
7733 auto it = lookup.
find(bs);
7734 LIBSEMIGROUPS_ASSERT(it != lookup.
end());
7735 result.push_back(views[it->second]);
7765 template <
typename Mat,
7767 typename = std::enable_if_t<IsMatrix<Mat>>>
7768 void row_basis(Mat
const& x, Container& result) {
7792 template <
typename Mat,
typename = std::enable_if_t<IsDynamicMatrix<Mat>>>
7818 template <
typename Mat,
typename = std::enable_if_t<IsStaticMatrix<Mat>>>
7819 detail::StaticVector1<typename Mat::RowView, Mat::nr_rows>
7821 detail::StaticVector1<typename Mat::RowView, Mat::nr_rows> container;
7845 template <
typename Mat,
typename Container>
7847 using value_type =
typename std::decay_t<Container>::value_type;
7848 static_assert(
IsMatrix<Mat>,
"IsMatrix<Mat> must be true!");
7850 "Container::value_type must be Mat::RowView");
7852 std::decay_t<Container> result;
7891 template <
typename Mat,
typename = std::enable_if_t<IsBMat<Mat>>>
7893 size_t const M = detail::BitSetCapacity<Mat>::value;
7898 st.
insert(bitset_row_basis_.cbegin(), bitset_row_basis_.cend());
7900 bitset_row_basis_.cend());
7901 for (
size_t i = 0; i < orb.
size(); ++i) {
7902 for (
auto& row : bitset_row_basis_) {
7904 for (
size_t j = 0; j < x.number_of_rows(); ++j) {
7905 cup.set(j, cup[j] || row[j]);
7907 if (st.
insert(cup).second) {
7936 template <
typename Mat>
7937 auto operator+(
typename Mat::scalar_type a, Mat
const& x)
7938 -> std::enable_if_t<IsMatrix<Mat>, Mat> {
7961 template <
typename Mat>
7962 auto operator*(
typename Mat::scalar_type a, Mat
const& x)
7963 -> std::enable_if_t<IsMatrix<Mat>, Mat> {
8003 template <
typename Mat,
8007 detail::throw_if_any_row_wrong_size(rows);
8008 detail::throw_if_bad_dim<Mat>(rows);
8039 template <
typename Mat,
8073 template <
typename Mat,
8115 template <
typename Mat,
8117 typename = std::enable_if_t<IsMatrix<Mat>>>
8120 Mat
make(Semiring
const* semiring,
8123 detail::throw_if_any_row_wrong_size(rows);
8124 detail::throw_if_bad_dim<Mat>(rows);
8125 Mat m(semiring, rows);
8161 template <
typename Mat,
8163 typename = std::enable_if_t<IsMatrix<Mat>>>
8164 Mat
make(Semiring
const* semiring,
8166 detail::throw_if_any_row_wrong_size(rows);
8167 detail::throw_if_bad_dim<Mat>(rows);
8168 Mat m(semiring, rows);
8195 template <
typename Mat,
8197 typename = std::enable_if_t<IsMatrix<Mat>>>
8198 Mat
make(Semiring
const* semiring,
8201 Mat m(semiring, row);
8233 template <
size_t R,
size_t C,
typename Scalar>
8256 template <
typename S,
typename T>
8258 detail::RowViewCommon<S, T>
const& x) {
8260 for (
auto it = x.cbegin(); it != x.cend(); ++it) {
8262 if (it != x.cend() - 1) {
8286 template <
typename Mat>
8290 if (x.number_of_rows() != 1) {
8295 if (n != x.number_of_rows() - 1) {
8300 if (x.number_of_rows() != 1) {
8322 template <
typename Mat>
8327 size_t max_width = 72)
8329 if (braces.size() != 2) {
8331 "the 4th argument (braces) must have size 2, found {}",
8335 size_t const R = x.number_of_rows();
8336 size_t const C = x.number_of_cols();
8340 for (
size_t r = 0; r < R; ++r) {
8341 for (
size_t c = 0; c < C; ++c) {
8343 = detail::unicode_string_length(detail::entry_repr(x(r, c)));
8344 row_widths[r] += width;
8345 if (width > max_col_widths[c]) {
8346 max_col_widths[c] = width;
8353 auto const total_width = col_width * C + prefix.size() + 1;
8354 if (total_width > max_width) {
8359 "<{}x{} {}>", x.number_of_rows(), x.number_of_cols(), short_name);
8367 auto const lbrace = braces[0], rbrace = braces[1];
8368 if (R != 0 && C != 0) {
8370 for (
size_t r = 0; r < R; ++r) {
8371 result += fmt::format(
"{}{}", rindent, lbrace);
8374 for (
size_t c = 0; c < C; ++c) {
8375 result += fmt::format(
8376 "{}{:>{}}", csep, detail::entry_repr(x(r, c)), col_width);
8379 result += fmt::format(
"{}", rbrace);
8419 template <
typename Mat>
8434 constexpr size_t operator()(Mat
const& x)
const noexcept {
8435 return x.number_of_rows() * x.number_of_rows() * x.number_of_rows();
8449 template <
typename Mat>
8450 struct Degree<Mat,
std::enable_if_t<IsMatrix<Mat>>> {
8463 constexpr size_t operator()(Mat
const& x)
const noexcept {
8464 return x.number_of_rows();
8478 template <
typename Mat>
8479 struct Hash<Mat,
std::enable_if_t<IsMatrix<Mat>>> {
8492 constexpr size_t operator()(Mat
const& x)
const {
8493 return x.hash_value();
8512 template <
typename Mat>
8517 constexpr void operator()(Mat&,
size_t)
const noexcept {
8519 LIBSEMIGROUPS_ASSERT(
false);
8533 template <
typename Mat>
8534 struct One<Mat,
std::enable_if_t<IsMatrix<Mat>>> {
8563 template <
typename Mat>
8564 struct Product<Mat,
std::enable_if_t<IsMatrix<Mat>>> {
8584 operator()(Mat& xy, Mat
const& x, Mat
const& y,
size_t = 0)
const {
8585 xy.product_inplace_no_checks(x, y);
8593 std::enable_if_t<libsemigroups::IsMatrix<Mat>>>
8594 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:2884
DynamicMatrix(std::initializer_list< std::initializer_list< scalar_type > > const &m)
Construct a matrix from std::initializer_list of std::initializer_list of scalars.
Definition matrix.hpp:2906
DynamicMatrix()=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:2805
void product_inplace_no_checks(DynamicMatrix const &x, DynamicMatrix const &y)
Multiplies x and y and stores the result in this.
DynamicMatrix & operator=(DynamicMatrix const &)=default
Default copy assignment operator.
DynamicMatrix Row
The type of a row of a DynamicMatrix.
Definition matrix.hpp:2796
PlusOp Plus
Alias for the template parameter PlusOp.
Definition matrix.hpp:2802
void rows(T &x) const
Add row views for every row in the matrix to a container.
scalar_type scalar_one() const noexcept
Returns the multiplicative identity of the underlying semiring.
typename MatrixCommon::scalar_const_reference scalar_const_reference
The type of const references to the entries in the matrix.
Definition matrix.hpp:2792
void swap(DynamicMatrix &that) noexcept
Swaps the contents of *this with the contents of that.
Definition matrix.hpp:3163
const_iterator cend() noexcept
Returns a const iterator pointing one beyond the last entry in the matrix.
static DynamicMatrix one(size_t n)
Construct the identity matrix.
Definition matrix.hpp:2985
DynamicMatrix(size_t r, size_t c)
Construct a matrix with given dimensions.
Definition matrix.hpp:2862
ZeroOp Zero
Alias for the template parameter ZeroOp.
Definition matrix.hpp:2808
scalar_type scalar_zero() const noexcept
Returns the additive identity of the underlying semiring.
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:2811
void semiring_type
Alias for the semiring type (void).
Definition matrix.hpp:2818
RowView row(size_t i) const
Returns a view into a row.
DynamicMatrix(RowView const &rv)
Construct a row from a row view.
Definition matrix.hpp:2938
iterator begin() noexcept
Returns an iterator pointing at the first entry.
size_t number_of_rows() const noexcept
Returns the number of rows of the matrix.
DynamicRowView< PlusOp, ProdOp, ZeroOp, OneOp, Scalar > RowView
The type of a row view into a DynamicMatrix.
Definition matrix.hpp:2799
RowView row_no_checks(size_t i) const
Returns a view into a row.
DynamicMatrix(std::vector< std::vector< scalar_type > > const &m)
Construct a matrix from std::vector of std::vector of scalars.
Definition matrix.hpp:2924
typename MatrixCommon::scalar_reference scalar_reference
The type of references to the entries in the matrix.
Definition matrix.hpp:2787
scalar_reference at(size_t r, size_t c) const
Returns a const reference to the specified entry of the matrix.
DynamicMatrix(DynamicMatrix const &)=default
Default copy constructor.
size_t hash_value() const
Return a hash value of a matrix.
const_iterator cbegin() noexcept
Returns a const iterator pointing at the first entry.
DynamicMatrix(DynamicMatrix &&)=default
Default move constructor.
typename MatrixCommon::scalar_type scalar_type
The type of the entries in the matrix.
Definition matrix.hpp:2784
size_t number_of_cols() const noexcept
Returns the number of columns of the matrix.
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:3302
DynamicMatrix & operator=(DynamicMatrix &&)=default
Default move assignment operator.
static DynamicMatrix one(Semiring const *semiring, size_t n)
Construct the identity matrix.
Definition matrix.hpp:3378
scalar_reference at(size_t r, size_t c)
Returns a reference to the specified entry of the matrix.
DynamicMatrix(Semiring const *sr, std::vector< std::vector< scalar_type > > const &rws)
Construct a matrix over a given semiring (std::vector of std::vector).
Definition matrix.hpp:3324
DynamicMatrix(Semiring const *sr, size_t r, size_t c)
Construct a matrix over a given semiring with given dimensions.
Definition matrix.hpp:3282
void product_inplace_no_checks(DynamicMatrix const &x, DynamicMatrix const &y)
Multiplies x and y and stores the result in this.
DynamicMatrix & operator=(DynamicMatrix const &)=default
Default copy assignment operator.
DynamicMatrix Row
Alias for the type of the rows of a DynamicMatrix.
Definition matrix.hpp:3239
void rows(T &x) const
Add row views for every row in the matrix to a container.
scalar_type scalar_one() const noexcept
Returns the multiplicative identity of the underlying semiring.
typename MatrixCommon::scalar_const_reference scalar_const_reference
Alias for const references to the template parameter Scalar.
Definition matrix.hpp:3235
void swap(DynamicMatrix &that) noexcept
Swaps the contents of *this with the contents of that.
Definition matrix.hpp:3558
const_iterator cend() noexcept
Returns a const iterator pointing one beyond the last entry in the matrix.
scalar_type scalar_zero() const noexcept
Returns the additive identity of the underlying semiring.
void transpose_no_checks()
Transpose a matrix in-place.
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:3358
iterator begin() noexcept
Returns an iterator pointing at the first entry.
size_t number_of_rows() const noexcept
Returns the number of rows of the matrix.
DynamicMatrix(Semiring const *sr, std::initializer_list< scalar_type > const &rw)
Construct a row over a given semiring (std::initializer_list).
Definition matrix.hpp:3343
RowView row_no_checks(size_t i) const
Returns a view into a row.
typename MatrixCommon::scalar_reference scalar_reference
Alias for references to the template parameter Scalar.
Definition matrix.hpp:3230
scalar_reference at(size_t r, size_t c) const
Returns a const reference to the specified entry of the matrix.
DynamicMatrix(DynamicMatrix const &)=default
Default copy constructor.
size_t hash_value() const
Return a hash value of a matrix.
const_iterator cbegin() noexcept
Returns a const iterator pointing at the first entry.
DynamicMatrix(DynamicMatrix &&)=default
Default move constructor.
typename MatrixCommon::scalar_type scalar_type
Alias for the template parameter Scalar.
Definition matrix.hpp:3227
DynamicRowView< Semiring, Scalar > RowView
Alias for the type of row views of a DynamicMatrix.
Definition matrix.hpp:3242
Semiring semiring_type
Alias for the template parameter Semiring.
Definition matrix.hpp:3247
size_t number_of_cols() const noexcept
Returns the number of columns of the matrix.
void transpose()
Transpose a matrix in-place.
std::pair< scalar_type, scalar_type > coords(const_iterator it) const
Get the coordinates of an iterator.
iterator end() noexcept
Returns an iterator pointing one beyond the last entry in the matrix.
Class representing a truncated max-plus semiring.
Definition matrix.hpp:5107
static constexpr Scalar scalar_zero() noexcept
Get the additive identity.
Definition matrix.hpp:5181
Scalar plus_no_checks(Scalar x, Scalar y) const noexcept
Addition in a truncated max-plus semiring.
Definition matrix.hpp:5244
Scalar product_no_checks(Scalar x, Scalar y) const noexcept
Multiplication in a truncated max-plus semiring.
Definition matrix.hpp:5209
MaxPlusTruncSemiring()=delete
Deleted default constructor.
Scalar threshold() const noexcept
Get the threshold.
Definition matrix.hpp:5269
static constexpr Scalar scalar_one() noexcept
Get the multiplicative identity.
Definition matrix.hpp:5167
Class representing a truncated min-plus semiring.
Definition matrix.hpp:5584
static constexpr Scalar scalar_zero() noexcept
Get the additive identity.
Definition matrix.hpp:5657
Scalar plus_no_checks(Scalar x, Scalar y) const noexcept
Addition in a truncated min-plus semiring.
Definition matrix.hpp:5720
Scalar product_no_checks(Scalar x, Scalar y) const noexcept
Multiplication in a truncated min-plus semiring.
Definition matrix.hpp:5685
Scalar threshold() const noexcept
Get the threshold.
Definition matrix.hpp:5745
static constexpr Scalar scalar_one() noexcept
Get the multiplicative identity.
Definition matrix.hpp:5642
MinPlusTruncSemiring()=delete
Deleted default constructor.
NTPSemiring & operator=(NTPSemiring const &)=default
Default copy assignment operator.
static constexpr Scalar scalar_zero() noexcept
Get the additive identity.
Definition matrix.hpp:6208
Scalar plus_no_checks(Scalar x, Scalar y) const noexcept
Addition in an ntp semiring.
Definition matrix.hpp:6266
NTPSemiring()=delete
Deleted default constructor.
Scalar product_no_checks(Scalar x, Scalar y) const noexcept
Multiplication in an ntp semiring.
Definition matrix.hpp:6236
Scalar period() const noexcept
Get the period.
Definition matrix.hpp:6300
Scalar threshold() const noexcept
Get the threshold.
Definition matrix.hpp:6284
static constexpr Scalar scalar_one() noexcept
Get the multiplicative identity.
Definition matrix.hpp:6192
Static matrix class.
Definition matrix.hpp:1859
typename MatrixCommon::iterator iterator
Definition matrix.hpp:1906
StaticMatrix(std::initializer_list< scalar_type > const &c)
Construct a row (from std::initializer_list).
Definition matrix.hpp:1934
typename MatrixCommon::const_iterator const_iterator
Definition matrix.hpp:1909
scalar_const_reference at(size_t r, size_t c) const
Returns a const reference to the specified entry of the matrix.
scalar_reference at(size_t r, size_t c)
Returns a reference to the specified entry of the matrix.
ProdOp Prod
Definition matrix.hpp:1897
PlusOp Plus
Definition matrix.hpp:1894
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:1884
scalar_reference operator()(size_t r, size_t c)
Returns a reference to the specified entry of the matrix.
ZeroOp Zero
Definition matrix.hpp:1900
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:1903
StaticMatrix(std::initializer_list< std::initializer_list< scalar_type > > const &m)
Construct a matrix.
Definition matrix.hpp:1960
StaticRowView< PlusOp, ProdOp, ZeroOp, OneOp, C, Scalar > RowView
Definition matrix.hpp:1891
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:1996
size_t number_of_rows() const noexcept
Returns the number of rows of the matrix.
StaticMatrix(std::vector< std::vector< scalar_type > > const &m)
Construct a matrix.
Definition matrix.hpp:1976
StaticMatrix< PlusOp, ProdOp, ZeroOp, OneOp, 1, C, Scalar > Row
Alias for the type of the rows of a StaticMatrix.
Definition matrix.hpp:1888
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:1879
size_t hash_value() const
typename MatrixCommon::scalar_type scalar_type
Alias for the template parameter Scalar.
Definition matrix.hpp:1876
scalar_const_reference operator()(size_t r, size_t c) const
Returns a const reference to the specified entry of the matrix.
StaticMatrix & operator=(StaticMatrix &&)=default
Default move assignment operator.
size_t number_of_cols() const noexcept
Returns the number of columns of the matrix.
StaticMatrix & operator=(StaticMatrix const &)=default
Default copy assignment operator.
std::pair< scalar_type, scalar_type > coords(const_iterator it) const
Class for views into a row of a matrix over a semiring.
Definition matrix.hpp:1080
typename RowViewCommon::iterator iterator
Alias for const iterators pointing at entries of a matrix.
Definition matrix.hpp:1096
typename RowViewCommon::scalar_const_reference scalar_const_reference
Alias for const references to the template parameter Scalar.
Definition matrix.hpp:1107
StaticRowView & operator=(StaticRowView &&)=default
Default move assignment operator.
typename RowViewCommon::scalar_reference scalar_reference
Alias for references to the template parameter Scalar.
Definition matrix.hpp:1102
iterator begin() noexcept
Returns a iterator pointing at the first entry.
iterator cend()
Returns a const iterator pointing one beyond the last entry of the row.
StaticRowView()=default
Default constructor.
typename matrix_type::Row Row
Alias for the type of a row in the underlying matrix.
Definition matrix.hpp:1114
typename RowViewCommon::const_iterator const_iterator
Alias for const iterators pointing at entries of a matrix.
Definition matrix.hpp:1093
const_iterator cbegin() const noexcept
Returns a const iterator pointing at the first entry.
iterator end()
Returns a iterator pointing one beyond the last entry of the row.
Scalar scalar_type
Alias for the template parameter Scalar.
Definition matrix.hpp:1099
StaticRowView(StaticRowView &&)=default
Default move constructor.
typename RowViewCommon::matrix_type matrix_type
Alias for the type of the underlying matrix.
Definition matrix.hpp:1111
StaticRowView(StaticRowView const &)=default
Default copy constructor.
static constexpr size_t size() const noexcept
Returns the size of the row.
StaticRowView & operator=(StaticRowView const &)=default
Default copy assignment operator.
StaticRowView(Row const &r)
Construct a row view from a Row.
std::string to_human_readable_repr(AhoCorasick const &ac)
Return a string representation.
Bipartition operator*(Bipartition const &x, Bipartition const &y)
Multiply two bipartitions.
std::ostringstream & operator<<(std::ostringstream &os, BMat8 const &x)
Insertion operator.
StaticMatrix< BooleanPlus, BooleanProd, BooleanZero, BooleanOne, R, C, int > StaticBMat
Alias for static boolean matrices.
Definition matrix.hpp:3940
static constexpr bool IsBMat
Helper to check if a type is BMat.
Definition matrix.hpp:3998
DynamicMatrix< BooleanPlus, BooleanProd, BooleanZero, BooleanOne, int > DynamicBMat
Alias for dynamic boolean matrices.
Definition matrix.hpp:3926
std::enable_if_t< IsBMat< Mat > > throw_if_bad_entry(Mat const &m)
Check the entries in a boolean matrix are valid.
Definition matrix.hpp:4043
std::conditional_t< R==0||C==0, DynamicBMat, StaticBMat< R, C > > BMat
Alias template for boolean matrices.
Definition matrix.hpp:3963
NegativeInfinity const NEGATIVE_INFINITY
Value for negative infinity.
Undefined const UNDEFINED
Value for something undefined.
PositiveInfinity const POSITIVE_INFINITY
Value for positive infinity.
#define LIBSEMIGROUPS_EXCEPTION(...)
Throw a LibsemigroupsException.
Definition exception.hpp:95
std::conditional_t< R==0||C==0, DynamicIntMat< Scalar >, StaticIntMat< R, C, Scalar > > IntMat
Alias template for integer matrices.
Definition matrix.hpp:4287
DynamicMatrix< IntegerPlus< Scalar >, IntegerProd< Scalar >, IntegerZero< Scalar >, IntegerOne< Scalar >, Scalar > DynamicIntMat
Alias for dynamic integer matrices.
Definition matrix.hpp:4239
StaticMatrix< IntegerPlus< Scalar >, IntegerProd< Scalar >, IntegerZero< Scalar >, IntegerOne< Scalar >, R, C, Scalar > StaticIntMat
Alias for static integer matrices.
Definition matrix.hpp:4262
enable_if_is_same< Return, Blocks > make(Container const &cont)
Check the arguments, construct a Blocks object, and check it.
Definition bipart.hpp:798
static constexpr bool IsMaxPlusMat
Helper variable template.
Definition matrix.hpp:4613
constexpr bool IsStaticMatrix
Helper variable template.
Definition matrix.hpp:3636
constexpr bool IsDynamicMatrix
Helper variable template.
Definition matrix.hpp:3649
static constexpr bool IsIntMat
Helper variable template.
Definition matrix.hpp:4312
auto operator+(typename Mat::scalar_type a, Mat const &x) -> std::enable_if_t< IsMatrix< Mat >, Mat >
Add a scalar to a matrix.
Definition matrix.hpp:7933
static constexpr bool IsMatWithSemiring
Helper variable template.
Definition matrix.hpp:3663
static constexpr bool IsMinPlusMat
Helper variable template.
Definition matrix.hpp:4921
constexpr bool IsMatrix
Helper variable template.
Definition matrix.hpp:168
StaticMatrix< MaxPlusPlus< Scalar >, MaxPlusProd< Scalar >, MaxPlusZero< Scalar >, IntegerZero< Scalar >, R, C, Scalar > StaticMaxPlusMat
Alias for static max-plus matrices.
Definition matrix.hpp:4562
DynamicMatrix< MaxPlusPlus< Scalar >, MaxPlusProd< Scalar >, MaxPlusZero< Scalar >, IntegerZero< Scalar >, Scalar > DynamicMaxPlusMat
Alias for dynamic max-plus matrices.
Definition matrix.hpp:4543
std::conditional_t< R==0||C==0, DynamicMaxPlusMat< Scalar >, StaticMaxPlusMat< R, C, Scalar > > MaxPlusMat
Alias template for max-plus matrices.
Definition matrix.hpp:4586
DynamicMatrix< MaxPlusPlus< Scalar >, MaxPlusTruncProd< T, Scalar >, MaxPlusZero< Scalar >, IntegerZero< Scalar >, Scalar > DynamicMaxPlusTruncMat
Alias for dynamic truncated max-plus matrices.
Definition matrix.hpp:5290
std::conditional_t< R==0||C==0, std::conditional_t< T==0, DynamicMatrix< MaxPlusTruncSemiring< Scalar >, Scalar >, DynamicMaxPlusTruncMat< T, Scalar > >, StaticMaxPlusTruncMat< T, R, C, Scalar > > MaxPlusTruncMat
Alias template for truncated max-plus matrices.
Definition matrix.hpp:5336
StaticMatrix< MaxPlusPlus< Scalar >, MaxPlusTruncProd< T, Scalar >, MaxPlusZero< Scalar >, IntegerZero< Scalar >, R, C, Scalar > StaticMaxPlusTruncMat
Alias for static truncated max-plus matrices.
Definition matrix.hpp:5310
static constexpr bool IsMaxPlusTruncMat
Helper to check if a type is MaxPlusTruncMat.
Definition matrix.hpp:5379
DynamicMatrix< MinPlusPlus< Scalar >, MinPlusProd< Scalar >, MinPlusZero< Scalar >, IntegerZero< Scalar >, Scalar > DynamicMinPlusMat
Alias for dynamic min-plus matrices.
Definition matrix.hpp:4851
StaticMatrix< MinPlusPlus< Scalar >, MinPlusProd< Scalar >, MinPlusZero< Scalar >, IntegerZero< Scalar >, R, C, Scalar > StaticMinPlusMat
Alias for static min-plus matrices.
Definition matrix.hpp:4870
std::conditional_t< R==0||C==0, DynamicMinPlusMat< Scalar >, StaticMinPlusMat< R, C, Scalar > > MinPlusMat
Alias template for min-plus matrices.
Definition matrix.hpp:4894
DynamicMatrix< MinPlusPlus< Scalar >, MinPlusTruncProd< T, Scalar >, MinPlusZero< Scalar >, IntegerZero< Scalar >, Scalar > DynamicMinPlusTruncMat
Alias for dynamic truncated min-plus matrices.
Definition matrix.hpp:5766
StaticMatrix< MinPlusPlus< Scalar >, MinPlusTruncProd< T, Scalar >, MinPlusZero< Scalar >, IntegerZero< Scalar >, R, C, Scalar > StaticMinPlusTruncMat
Alias for static truncated min-plus matrices.
Definition matrix.hpp:5786
static constexpr bool IsMinPlusTruncMat
Helper to check if a type is MinPlusTruncMat.
Definition matrix.hpp:5856
std::conditional_t< R==0||C==0, std::conditional_t< T==0, DynamicMatrix< MinPlusTruncSemiring< Scalar >, Scalar >, DynamicMinPlusTruncMat< T, Scalar > >, StaticMinPlusTruncMat< T, R, C, Scalar > > MinPlusTruncMat
Alias template for truncated min-plus matrices.
Definition matrix.hpp:5813
DynamicMatrix< NTPSemiring< Scalar >, Scalar > DynamicNTPMatWithSemiring
Alias for ntp matrices with dynamic threshold and period.
Definition matrix.hpp:6320
DynamicMatrix< NTPPlus< T, P, Scalar >, NTPProd< T, P, Scalar >, IntegerZero< Scalar >, IntegerOne< Scalar >, Scalar > DynamicNTPMatWithoutSemiring
Alias for ntp matrices with static threshold and period.
Definition matrix.hpp:6336
static constexpr bool IsNTPMat
Helper to check if a type is NTPMat.
Definition matrix.hpp:6440
std::conditional_t< R==0||C==0, std::conditional_t< T==0 &&P==0, DynamicNTPMatWithSemiring< Scalar >, DynamicNTPMatWithoutSemiring< T, P, Scalar > >, StaticNTPMat< T, P, R, C, Scalar > > NTPMat
Alias template for ntp matrices.
Definition matrix.hpp:6397
StaticMatrix< NTPPlus< T, P, Scalar >, NTPProd< T, P, Scalar >, IntegerZero< Scalar >, IntegerOne< Scalar >, R, C, Scalar > StaticNTPMat
Alias for ntp matrices with static threshold and period, and dimensions.
Definition matrix.hpp:6362
std::conditional_t< R==0||C==0, DynamicProjMaxPlusMat< Scalar >, StaticProjMaxPlusMat< R, C, Scalar > > ProjMaxPlusMat
Alias template for projective max-plus matrices.
Definition matrix.hpp:7024
detail::ProjMaxPlusMat< DynamicMaxPlusMat< Scalar > > DynamicProjMaxPlusMat
Alias for dynamic projective max-plus matrices with run-time dimensions.
Definition matrix.hpp:7007
static constexpr bool IsProjMaxPlusMat
Helper to check if a type is ProjMaxPlusMat.
Definition matrix.hpp:7054
detail::ProjMaxPlusMat< StaticMaxPlusMat< R, C, Scalar > > StaticProjMaxPlusMat
Alias for static projective max-plus matrices with compile-time arithmetic and dimensions.
Definition matrix.hpp:6993
T inner_product(T... args)
T 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:704
Namespace for helper functions for matrices.
Definition matrix.hpp:170
void bitset_row_basis(Container &&rows, std::decay_t< Container > &result)
Appends a basis for the space spanned by some bitsets to a container.
Definition matrix.hpp:7452
void bitset_rows(Container &&views, detail::StaticVector1< BitSet< C >, R > &result)
Converts a container of row views of a boolean matrix to bit sets, and append them to another contain...
Definition matrix.hpp:7280
constexpr Scalar period(StaticNTPMat< T, P, R, C, Scalar > const &) noexcept
Returns the period of a static ntp matrix.
Definition matrix.hpp:6476
auto throw_if_bad_coords(Mat const &x, size_t r, size_t c) -> std::enable_if_t< IsMatrix< Mat > >
Throws the arguments do not index an entry of a matrix.
Definition matrix.hpp:235
constexpr auto threshold(Mat const &) noexcept -> std::enable_if_t<!detail::IsTruncMat< Mat >, typename Mat::scalar_type >
Returns the threshold of a matrix.
Definition matrix.hpp:3724
size_t row_space_size(Mat const &x)
Returns the size of the row space of a boolean matrix.
Definition matrix.hpp:7888
std::vector< typename Mat::RowView > rows(Mat const &x)
Returns a std::vector of row views into the rows of a dynamic matrix.
Definition matrix.hpp:7209
auto throw_if_bad_dim(Mat const &x, Mat const &y) -> std::enable_if_t< IsMatrix< Mat > >
Throws if two matrices do not have the same dimensions.
Definition matrix.hpp:206
Mat pow(Mat const &x, typename Mat::scalar_type e)
Returns a power of a matrix.
Definition matrix.hpp:7144
auto throw_if_not_square(Mat const &x) -> std::enable_if_t< IsMatrix< Mat > >
Throws if a matrix is not square.
Definition matrix.hpp:184
std::enable_if_t< IsMaxPlusTruncMat< Mat > > row_basis(Container &&views, std::decay_t< Container > &result)
Appends a basis for a space spanned by row views or bit sets to a container.
Definition matrix.hpp:7624
Namespace for everything in the libsemigroups library.
Definition action.hpp:44
Function object for returning the multiplicative identity.
Definition matrix.hpp:3874
constexpr bool operator()() const noexcept
Call operator returning the multiplication identity true of the boolean semiring.
Definition matrix.hpp:3885
Function object for addition in the boolean semiring.
Definition matrix.hpp:3820
constexpr bool operator()(bool x, bool y) const noexcept
Call operator for addition.
Definition matrix.hpp:3833
Function object for multiplication in the boolean semiring.
Definition matrix.hpp:3847
constexpr bool operator()(bool x, bool y) const noexcept
Call operator for multiplication.
Definition matrix.hpp:3860
Function object for returning the additive identity.
Definition matrix.hpp:3899
constexpr bool operator()() const noexcept
Call operator returning the additive identity false of the boolean semiring.
Definition matrix.hpp:3910
constexpr size_t operator()(Mat const &x) const noexcept
Call operator.
Definition matrix.hpp:8430
Adapter for the complexity of multiplication.
Definition adapters.hpp:121
constexpr size_t operator()(Mat const &x) const noexcept
Call operator.
Definition matrix.hpp:8459
Adapter for the degree of an element.
Definition adapters.hpp:159
constexpr size_t operator()(Mat const &x) const
Call operator.
Definition matrix.hpp:8488
Adapter for hashing.
Definition adapters.hpp:446
constexpr void operator()(Mat &, size_t) const noexcept
Call operator.
Definition matrix.hpp:8513
Adapter for increasing the degree of an element.
Definition adapters.hpp:199
Function object for returning the multiplicative identity.
Definition matrix.hpp:4213
constexpr Scalar operator()() const noexcept
Call operator returning the integer 1.
Definition matrix.hpp:4223
Function object for addition in the ring of integers.
Definition matrix.hpp:4129
constexpr Scalar operator()(Scalar x, Scalar y) const noexcept
Call operator for addition.
Definition matrix.hpp:4142
Function object for multiplication in the ring of integers.
Definition matrix.hpp:4160
constexpr Scalar operator()(Scalar x, Scalar y) const noexcept
Call operator for multiplication.
Definition matrix.hpp:4173
Function object for returning the additive identity.
Definition matrix.hpp:4188
constexpr Scalar operator()() const noexcept
Call operator returning the integer 0.
Definition matrix.hpp:4198
Function object for addition in the max-plus semiring.
Definition matrix.hpp:4431
Scalar operator()(Scalar x, Scalar y) const noexcept
Call operator for addition.
Definition matrix.hpp:4446
Function object for multiplication in the max-plus semiring.
Definition matrix.hpp:4478
Scalar operator()(Scalar x, Scalar y) const noexcept
Call operator for multiplication.
Definition matrix.hpp:4493
Function object for multiplication in truncated max-plus semirings.
Definition matrix.hpp:5065
Scalar operator()(Scalar x, Scalar y) const noexcept
Call operator for multiplication.
Definition matrix.hpp:5080
Function object for returning the additive identity of the max-plus semiring.
Definition matrix.hpp:4515
constexpr Scalar operator()() const noexcept
Call operator for additive identity.
Definition matrix.hpp:4527
Function object for addition in the min-plus semiring.
Definition matrix.hpp:4739
Scalar operator()(Scalar x, Scalar y) const noexcept
Call operator for addition.
Definition matrix.hpp:4754
Function object for multiplication in the min-plus semiring.
Definition matrix.hpp:4786
Scalar operator()(Scalar x, Scalar y) const noexcept
Call operator for multiplication.
Definition matrix.hpp:4801
Function object for multiplication in min-plus truncated semirings.
Definition matrix.hpp:5545
Scalar operator()(Scalar x, Scalar y) const noexcept
Call operator for multiplication.
Definition matrix.hpp:5558
Function object for returning the additive identity of the min-plus semiring.
Definition matrix.hpp:4823
constexpr Scalar operator()() const noexcept
Call operator for additive identity.
Definition matrix.hpp:4835
Function object for addition in ntp semirings.
Definition matrix.hpp:6052
constexpr Scalar operator()(Scalar x, Scalar y) const noexcept
Call operator for addition.
Definition matrix.hpp:6064
Function object for multiplication in an ntp semirings.
Definition matrix.hpp:6093
constexpr Scalar operator()(Scalar x, Scalar y) const noexcept
Call operator for multiplication.
Definition matrix.hpp:6107
Mat operator()(Mat const &x) const
Call operator.
Definition matrix.hpp:8544
Adapter for the identity element of the given type.
Definition adapters.hpp:246
void operator()(Mat &xy, Mat const &x, Mat const &y, size_t=0) const
Call operator.
Definition matrix.hpp:8580
Adapter for the product of two elements.
Definition adapters.hpp:284