19#ifndef LIBSEMIGROUPS_DETAIL_MATRIX_COMMON_HPP_
20#define LIBSEMIGROUPS_DETAIL_MATRIX_COMMON_HPP_
24#include <initializer_list>
34#include "libsemigroups/adapters.hpp"
35#include "libsemigroups/constants.hpp"
36#include "libsemigroups/debug.hpp"
37#include "libsemigroups/exception.hpp"
38#include "libsemigroups/is-matrix.hpp"
41#include "formatters.hpp"
42#include "matrix-exceptions.hpp"
45namespace libsemigroups::detail {
54 template <
typename Scalar>
55 [[nodiscard]] std::string entry_repr(Scalar a);
57 template <
typename Container,
60 typename Semiring =
void>
61 class MatrixCommon : MatrixPolymorphicBase {
67 using scalar_type =
typename Container::value_type;
68 using scalar_reference =
typename Container::reference;
69 using scalar_const_reference =
typename Container::const_reference;
70 using semiring_type = Semiring;
72 using container_type = Container;
73 using iterator =
typename Container::iterator;
74 using const_iterator =
typename Container::const_iterator;
76 using RowView = TRowView;
78 [[nodiscard]] scalar_type scalar_one() const noexcept {
79 return static_cast<Subclass const*
>(
this)->one_impl();
82 [[nodiscard]] scalar_type scalar_zero() const noexcept {
83 return static_cast<Subclass const*
>(
this)->zero_impl();
86 [[nodiscard]] Semiring
const* semiring() const noexcept {
87 return static_cast<Subclass const*
>(
this)->semiring_impl();
95 [[nodiscard]] scalar_type plus_no_checks(scalar_type x,
96 scalar_type y)
const noexcept {
97 return static_cast<Subclass const*
>(
this)->plus_no_checks_impl(y, x);
100 [[nodiscard]] scalar_type product_no_checks(scalar_type x,
101 scalar_type y)
const noexcept {
102 return static_cast<Subclass const*
>(
this)->product_no_checks_impl(y, x);
110 void resize(
size_t r,
size_t c) {
111 if constexpr (std::is_same_v<container_type, std::vector<scalar_type>>) {
112 _container.resize(r * c);
118 template <
typename T>
119 void init(T
const& m);
123 init(std::initializer_list<std::initializer_list<scalar_type>>
const& m) {
124 init<std::initializer_list<std::initializer_list<scalar_type>>>(m);
133 MatrixCommon() =
default;
134 MatrixCommon(MatrixCommon
const&) =
default;
135 MatrixCommon(MatrixCommon&&) =
default;
136 MatrixCommon& operator=(MatrixCommon
const&) =
default;
137 MatrixCommon& operator=(MatrixCommon&&) =
default;
139 ~MatrixCommon() =
default;
141 explicit MatrixCommon(std::initializer_list<scalar_type>
const& c)
147 explicit MatrixCommon(std::vector<std::vector<scalar_type>>
const& m)
153 std::initializer_list<std::initializer_list<scalar_type>>
const& m)
159 explicit MatrixCommon(RowView
const& rv) : MatrixCommon() {
160 resize(1, rv.size());
161 std::copy(rv.cbegin(), rv.cend(), _container.begin());
165 [[nodiscard]] Subclass one()
const;
172 [[nodiscard]]
bool operator==(MatrixCommon
const& that)
const {
173 return _container == that._container;
177 [[nodiscard]]
bool operator==(RowView
const& that)
const {
178 return number_of_rows() == 1
179 &&
static_cast<RowView
>(*
static_cast<Subclass const*
>(
this))
184 [[nodiscard]]
bool operator<(MatrixCommon
const& that)
const {
185 return _container < that._container;
189 [[nodiscard]]
bool operator<(RowView
const& that)
const {
190 return number_of_rows() == 1
191 &&
static_cast<RowView
>(*
static_cast<Subclass const*
>(
this))
196 template <
typename T>
197 [[nodiscard]]
bool operator!=(T
const& that)
const {
198 static_assert(
IsMatrix<T> || std::is_same_v<T, RowView>);
199 return !(*
this == that);
203 template <
typename T>
204 [[nodiscard]]
bool operator>(T
const& that)
const {
205 static_assert(
IsMatrix<T> || std::is_same_v<T, RowView>);
210 template <
typename T>
211 [[nodiscard]]
bool operator>=(T
const& that)
const {
212 static_assert(
IsMatrix<T> || std::is_same_v<T, RowView>);
213 return that < *
this || that == *
this;
217 template <
typename T>
218 [[nodiscard]]
bool operator<=(T
const& that)
const {
219 static_assert(
IsMatrix<T> || std::is_same_v<T, RowView>);
220 return *
this < that || that == *
this;
229 [[nodiscard]] scalar_reference operator()(
size_t r,
size_t c) {
230 return this->_container[r * number_of_cols() + c];
233 [[nodiscard]] scalar_reference at(
size_t r,
size_t c) {
235 return this->operator()(r, c);
240 [[nodiscard]] scalar_const_reference operator()(
size_t r,
size_t c)
const {
241 return this->_container[r * number_of_cols() + c];
244 [[nodiscard]] scalar_const_reference at(
size_t r,
size_t c)
const {
246 return this->operator()(r, c);
250 [[nodiscard]]
size_t number_of_rows() const noexcept {
251 return static_cast<Subclass const*
>(
this)->number_of_rows_impl();
255 [[nodiscard]]
size_t number_of_cols() const noexcept {
256 return static_cast<Subclass const*
>(
this)->number_of_cols_impl();
260 [[nodiscard]]
size_t hash_value()
const {
261 return Hash<Container>()(_container);
269 void product_inplace_no_checks(Subclass
const& A, Subclass
const& B);
270 void product_inplace(Subclass
const& A, Subclass
const& B);
273 void operator*=(scalar_type a) {
274 for (
auto it = _container.begin(); it < _container.end(); ++it) {
275 *it = product_no_checks(*it, a);
279 void plus_inplace_no_checks(Subclass
const& that);
282 void operator+=(Subclass
const& that);
284 void plus_inplace_no_checks(RowView
const& that) {
285 LIBSEMIGROUPS_ASSERT(number_of_rows() == 1);
286 LIBSEMIGROUPS_ASSERT(number_of_cols() == that.size());
287 RowView(*
static_cast<Subclass const*
>(
this)) += that;
290 void operator+=(RowView
const& that);
292 void operator+=(scalar_type a) {
293 for (
auto it = _container.begin(); it < _container.end(); ++it) {
294 *it = plus_no_checks(*it, a);
304 [[nodiscard]] Subclass plus_no_checks(Subclass
const& y)
const {
305 Subclass result(*
static_cast<Subclass const*
>(
this));
306 result.plus_inplace_no_checks(y);
310 [[nodiscard]] Subclass operator+(Subclass
const& y)
const;
312 [[nodiscard]] Subclass product_no_checks(Subclass
const& y)
const {
313 Subclass result(*
static_cast<Subclass const*
>(
this));
314 result.product_inplace_no_checks(*
static_cast<Subclass const*
>(
this), y);
319 [[nodiscard]] Subclass operator*(Subclass
const& y)
const;
321 [[nodiscard]] Subclass operator*(scalar_type a)
const {
322 Subclass result(*
static_cast<Subclass const*
>(
this));
327 [[nodiscard]] Subclass operator+(scalar_type a)
const {
328 Subclass result(*
static_cast<Subclass const*
>(
this));
338 [[nodiscard]] iterator begin() noexcept {
339 return _container.begin();
343 [[nodiscard]] iterator end() noexcept {
344 return _container.end();
348 [[nodiscard]] const_iterator begin() const noexcept {
349 return _container.begin();
353 [[nodiscard]] const_iterator end() const noexcept {
354 return _container.end();
358 [[nodiscard]] const_iterator cbegin() const noexcept {
359 return _container.cbegin();
363 [[nodiscard]] const_iterator cend() const noexcept {
364 return _container.cend();
367 template <
typename Iterator>
368 [[nodiscard]] std::pair<scalar_type, scalar_type>
369 coords(Iterator
const& it)
const;
376 void swap(MatrixCommon& that)
noexcept {
382 void transpose_no_checks() noexcept;
386 transpose_no_checks();
394 [[nodiscard]] RowView row_no_checks(
size_t i)
const;
396 [[nodiscard]] RowView row(
size_t i)
const;
399 template <
typename T>
400 void rows(T& x)
const;
406 friend std::ostream& operator<<(std::ostream& os, MatrixCommon
const& x) {
407 os << detail::to_string(x);
415 container_type _container;
418 template <
typename Scalar>
419 class MatrixDynamicDim {
421 MatrixDynamicDim() : _number_of_cols(0), _number_of_rows(0) {}
422 MatrixDynamicDim(MatrixDynamicDim
const&) =
default;
423 MatrixDynamicDim(MatrixDynamicDim&&) =
default;
424 MatrixDynamicDim& operator=(MatrixDynamicDim
const&) =
default;
425 MatrixDynamicDim& operator=(MatrixDynamicDim&&) =
default;
427 MatrixDynamicDim(
size_t r,
size_t c)
428 : _number_of_cols(c), _number_of_rows(r) {}
430 ~MatrixDynamicDim() =
default;
432 void swap(MatrixDynamicDim& that)
noexcept {
433 std::swap(_number_of_cols, that._number_of_cols);
434 std::swap(_number_of_rows, that._number_of_rows);
438 [[nodiscard]]
size_t number_of_rows_impl() const noexcept {
439 return _number_of_rows;
442 [[nodiscard]]
size_t number_of_cols_impl() const noexcept {
443 return _number_of_cols;
447 size_t _number_of_cols;
448 size_t _number_of_rows;
451 template <
typename PlusOp,
456 struct MatrixStaticArithmetic {
457 MatrixStaticArithmetic() =
default;
458 MatrixStaticArithmetic(MatrixStaticArithmetic
const&) =
default;
459 MatrixStaticArithmetic(MatrixStaticArithmetic&&) =
default;
460 MatrixStaticArithmetic& operator=(MatrixStaticArithmetic
const&) =
default;
461 MatrixStaticArithmetic& operator=(MatrixStaticArithmetic&&) =
default;
463 using scalar_type = Scalar;
466 [[nodiscard]]
static constexpr scalar_type
467 plus_no_checks_impl(scalar_type x, scalar_type y)
noexcept {
468 return PlusOp()(x, y);
471 [[nodiscard]]
static constexpr scalar_type
472 product_no_checks_impl(scalar_type x, scalar_type y)
noexcept {
473 return ProdOp()(x, y);
476 [[nodiscard]]
static constexpr scalar_type one_impl() noexcept {
480 [[nodiscard]]
static constexpr scalar_type zero_impl() noexcept {
484 [[nodiscard]]
static constexpr void const* semiring_impl() noexcept {
493 template <
typename Mat,
typename Sub
class>
494 class RowViewCommon {
496 "the template parameter Mat must be derived from "
497 "MatrixPolymorphicBase");
500 using const_iterator =
typename Mat::const_iterator;
501 using iterator =
typename Mat::iterator;
503 using scalar_type =
typename Mat::scalar_type;
504 using scalar_reference =
typename Mat::scalar_reference;
505 using scalar_const_reference =
typename Mat::scalar_const_reference;
507 using Row =
typename Mat::Row;
508 using matrix_type = Mat;
510 [[nodiscard]]
size_t size() const noexcept {
511 return static_cast<Subclass const*
>(
this)->length_impl();
515 scalar_type plus_no_checks(scalar_type x, scalar_type y)
const noexcept {
516 return static_cast<Subclass const*
>(
this)->plus_no_checks_impl(y, x);
519 scalar_type product_no_checks(scalar_type x, scalar_type y)
const noexcept {
520 return static_cast<Subclass const*
>(
this)->product_no_checks_impl(y, x);
524 RowViewCommon() =
default;
525 RowViewCommon(RowViewCommon
const&) =
default;
526 RowViewCommon(RowViewCommon&&) =
default;
527 RowViewCommon& operator=(RowViewCommon
const&) =
default;
528 RowViewCommon& operator=(RowViewCommon&&) =
default;
530 ~RowViewCommon() =
default;
532 explicit RowViewCommon(Row
const& r)
533 : RowViewCommon(const_cast<Row&>(r).begin()) {}
536 [[nodiscard]] scalar_const_reference operator[](
size_t i)
const {
541 [[nodiscard]] scalar_reference operator[](
size_t i) {
546 [[nodiscard]] scalar_const_reference operator()(
size_t i)
const {
551 [[nodiscard]] scalar_reference operator()(
size_t i) {
556 [[nodiscard]] const_iterator cbegin() const noexcept {
561 [[nodiscard]] const_iterator cend()
const {
562 return _begin + size();
566 [[nodiscard]] const_iterator begin() const noexcept {
571 [[nodiscard]] const_iterator end()
const {
572 return _begin + size();
576 [[nodiscard]] iterator begin() noexcept {
581 [[nodiscard]] iterator end() noexcept {
582 return _begin + size();
590 void plus_inplace_no_checks(RowViewCommon
const& x);
593 void operator+=(RowViewCommon
const& x);
596 [[nodiscard]] Row plus_no_checks(RowViewCommon
const& that)
const {
597 Row result(*
static_cast<Subclass const*
>(
this));
598 result.plus_inplace_no_checks(
static_cast<Subclass const&
>(that));
603 [[nodiscard]] Row operator+(RowViewCommon
const& x);
606 void operator+=(scalar_type a) {
607 for (
auto& x : *
this) {
608 x = plus_no_checks(x, a);
613 void operator*=(scalar_type a) {
614 for (
auto& x : *
this) {
615 x = product_no_checks(x, a);
620 [[nodiscard]] Row operator*(scalar_type a)
const {
621 Row result(*
static_cast<Subclass const*
>(
this));
626 template <
typename U>
627 [[nodiscard]]
bool operator==(U
const& that)
const {
629 return std::equal(begin(), end(), that.begin());
632 template <
typename U>
633 [[nodiscard]]
bool operator!=(U
const& that)
const {
634 return !(*
this == that);
637 template <
typename U>
638 [[nodiscard]]
bool operator<(U
const& that)
const {
640 cbegin(), cend(), that.cbegin(), that.cend());
643 template <
typename U>
644 [[nodiscard]]
bool operator>(U
const& that)
const {
648 void swap(RowViewCommon& that)
noexcept {
652 friend std::ostream& operator<<(std::ostream& os, RowViewCommon
const& x) {
653 os << detail::to_string(x);
658 explicit RowViewCommon(iterator first) : _begin(first) {}
664 template <
typename Mat,
typename Sub
class>
665 void throw_if_bad_dim(RowViewCommon<Mat, Subclass>
const& x,
666 RowViewCommon<Mat, Subclass>
const& y,
667 std::string_view arg_desc_x =
"the 1st argument",
668 std::string_view arg_desc_y =
"the 2nd argument");
671#include "matrix-common.tpp"
constexpr bool IsMatrix
Helper variable template.
Definition is-matrix.hpp:87
T lexicographical_compare(T... args)
void throw_if_not_square(Mat const &x, std::string_view arg_desc="the argument")
Throws if a matrix is not square.
void throw_if_bad_coords(Mat const &x, size_t r, size_t c)
Throws the arguments do not index an entry of a matrix.