libsemigroups  v3.6.3
C++ library for semigroups and monoids
Loading...
Searching...
No Matches
matrix-common.hpp
1//
2// libsemigroups - C++ library for semigroups and monoids
3// Copyright (C) 2026 James D. Mitchell
4//
5// This program is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// This program is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with this program. If not, see <http://www.gnu.org/licenses/>.
17//
18
19#ifndef LIBSEMIGROUPS_DETAIL_MATRIX_COMMON_HPP_
20#define LIBSEMIGROUPS_DETAIL_MATRIX_COMMON_HPP_
21
22#include <algorithm> // for copy, equal, fill
23#include <cstddef> // for size_t
24#include <initializer_list> // for initializer_list
25#include <iosfwd> // for ostream
26#include <iterator> // for distance
27#include <numeric> // for inner_product
28#include <string> // for basic_string
29#include <string_view> // for basic_string_view
30#include <type_traits> // for is_same_v, ena...
31#include <utility> // for swap, make_pair
32#include <vector> // for vector
33
34#include "libsemigroups/adapters.hpp" // for Hash
35#include "libsemigroups/constants.hpp" // for NEGATIVE_INFINITY
36#include "libsemigroups/debug.hpp" // for LIBSEMIGROUPS_...
37#include "libsemigroups/exception.hpp" // for LIBSEMIGROUPS_...
38#include "libsemigroups/is-matrix.hpp" // for IsMatrix, Matr...
39
40#include "fmt.hpp" // for format
41#include "formatters.hpp" // for formatting POSITIVE_INFINITY
42#include "matrix-exceptions.hpp" // for throw_if_bad_dim
43#include "string.hpp" // for to_string
44
45namespace libsemigroups::detail {
46 // This function is required for exceptions and to_human_readable_repr, so
47 // that if we encounter an entry of a matrix (Scalar type), then it can be
48 // printed correctly. If we just did fmt::format("{}", val) and val ==
49 // POSITIVE_INFINITY, but the type of val is, say, size_t, then this
50 // wouldn't use the formatter for PositiveInfinity.
51 //
52 // Also in fmt v11.1.4 the custom formatter for POSITIVE_INFINITY and
53 // NEGATIVE_INFINITY stopped working (and I wasn't able to figure out why)
54 template <typename Scalar>
55 [[nodiscard]] std::string entry_repr(Scalar a);
56
57 template <typename Container,
58 typename Subclass,
59 typename TRowView,
60 typename Semiring = void>
61 class MatrixCommon : MatrixPolymorphicBase {
62 public:
64 // MatrixCommon - Aliases - public
66
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;
71
72 using container_type = Container;
73 using iterator = typename Container::iterator;
74 using const_iterator = typename Container::const_iterator;
75
76 using RowView = TRowView;
77
78 [[nodiscard]] scalar_type scalar_one() const noexcept {
79 return static_cast<Subclass const*>(this)->one_impl();
80 }
81
82 [[nodiscard]] scalar_type scalar_zero() const noexcept {
83 return static_cast<Subclass const*>(this)->zero_impl();
84 }
85
86 [[nodiscard]] Semiring const* semiring() const noexcept {
87 return static_cast<Subclass const*>(this)->semiring_impl();
88 }
89
90 private:
92 // MatrixCommon - Semiring arithmetic - private
94
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);
98 }
99
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);
103 }
104
105 protected:
107 // MatrixCommon - Container functions - protected
109
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);
113 }
114 }
115
116 private:
117 // not noexcept because resize isn't
118 template <typename T>
119 void init(T const& m);
120
121 // not noexcept because init isn't
122 void
123 init(std::initializer_list<std::initializer_list<scalar_type>> const& m) {
124 init<std::initializer_list<std::initializer_list<scalar_type>>>(m);
125 }
126
127 public:
129 // MatrixCommon - Constructors + destructor - public
131
132 // none of the constructors are noexcept because they allocate
133 MatrixCommon() = default;
134 MatrixCommon(MatrixCommon const&) = default;
135 MatrixCommon(MatrixCommon&&) = default;
136 MatrixCommon& operator=(MatrixCommon const&) = default;
137 MatrixCommon& operator=(MatrixCommon&&) = default;
138
139 ~MatrixCommon() = default;
140
141 explicit MatrixCommon(std::initializer_list<scalar_type> const& c)
142 : MatrixCommon() {
143 resize(1, c.size());
144 std::copy(c.begin(), c.end(), _container.begin());
145 }
146
147 explicit MatrixCommon(std::vector<std::vector<scalar_type>> const& m)
148 : MatrixCommon() {
149 init(m);
150 }
151
152 MatrixCommon(
153 std::initializer_list<std::initializer_list<scalar_type>> const& m)
154 : MatrixCommon() {
155 init(m);
156 }
157
158 public:
159 explicit MatrixCommon(RowView const& rv) : MatrixCommon() {
160 resize(1, rv.size());
161 std::copy(rv.cbegin(), rv.cend(), _container.begin());
162 }
163
164 // not noexcept because mem allocate is required
165 [[nodiscard]] Subclass one() const;
166
168 // Comparison operators
170
171 // not noexcept because apparently vector::operator== isn't
172 [[nodiscard]] bool operator==(MatrixCommon const& that) const {
173 return _container == that._container;
174 }
175
176 // not noexcept because apparently vector::operator== isn't
177 [[nodiscard]] bool operator==(RowView const& that) const {
178 return number_of_rows() == 1
179 && static_cast<RowView>(*static_cast<Subclass const*>(this))
180 == that;
181 }
182
183 // not noexcept because apparently vector::operator< isn't
184 [[nodiscard]] bool operator<(MatrixCommon const& that) const {
185 return _container < that._container;
186 }
187
188 // not noexcept because apparently vector::operator< isn't
189 [[nodiscard]] bool operator<(RowView const& that) const {
190 return number_of_rows() == 1
191 && static_cast<RowView>(*static_cast<Subclass const*>(this))
192 < that;
193 }
194
195 // not noexcept because operator== isn't
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);
200 }
201
202 // not noexcept because operator< isn't
203 template <typename T>
204 [[nodiscard]] bool operator>(T const& that) const {
205 static_assert(IsMatrix<T> || std::is_same_v<T, RowView>);
206 return that < *this;
207 }
208
209 // not noexcept because operator< isn't
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;
214 }
215
216 // not noexcept because operator< isn't
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;
221 }
222
224 // Attributes
226
227 // not noexcept because vector::operator[] isn't, and neither is
228 // array::operator[]
229 [[nodiscard]] scalar_reference operator()(size_t r, size_t c) {
230 return this->_container[r * number_of_cols() + c];
231 }
232
233 [[nodiscard]] scalar_reference at(size_t r, size_t c) {
234 matrix::throw_if_bad_coords(static_cast<Subclass const&>(*this), r, c);
235 return this->operator()(r, c);
236 }
237
238 // not noexcept because vector::operator[] isn't, and neither is
239 // array::operator[]
240 [[nodiscard]] scalar_const_reference operator()(size_t r, size_t c) const {
241 return this->_container[r * number_of_cols() + c];
242 }
243
244 [[nodiscard]] scalar_const_reference at(size_t r, size_t c) const {
245 matrix::throw_if_bad_coords(static_cast<Subclass const&>(*this), r, c);
246 return this->operator()(r, c);
247 }
248
249 // noexcept because number_of_rows_impl is noexcept
250 [[nodiscard]] size_t number_of_rows() const noexcept {
251 return static_cast<Subclass const*>(this)->number_of_rows_impl();
252 }
253
254 // noexcept because number_of_cols_impl is noexcept
255 [[nodiscard]] size_t number_of_cols() const noexcept {
256 return static_cast<Subclass const*>(this)->number_of_cols_impl();
257 }
258
259 // not noexcept because Hash<T>::operator() isn't
260 [[nodiscard]] size_t hash_value() const {
261 return Hash<Container>()(_container);
262 }
263
265 // Arithmetic operators - in-place
267
268 // not noexcept because memory is allocated
269 void product_inplace_no_checks(Subclass const& A, Subclass const& B);
270 void product_inplace(Subclass const& A, Subclass const& B);
271
272 // not noexcept because iterator increment isn't
273 void operator*=(scalar_type a) {
274 for (auto it = _container.begin(); it < _container.end(); ++it) {
275 *it = product_no_checks(*it, a);
276 }
277 }
278
279 void plus_inplace_no_checks(Subclass const& that);
280
281 // not noexcept because vector::operator[] and array::operator[] aren't
282 void operator+=(Subclass const& that);
283
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;
288 }
289
290 void operator+=(RowView const& that);
291
292 void operator+=(scalar_type a) {
293 for (auto it = _container.begin(); it < _container.end(); ++it) {
294 *it = plus_no_checks(*it, a);
295 }
296 }
297
298 // TODO(2) implement operator*=(Subclass const&)
299
301 // Arithmetic operators - not in-place
303
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);
307 return result;
308 }
309
310 [[nodiscard]] Subclass operator+(Subclass const& y) const;
311
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);
315 return result;
316 }
317
318 // not noexcept because product_inplace_no_checks isn't
319 [[nodiscard]] Subclass operator*(Subclass const& y) const;
320
321 [[nodiscard]] Subclass operator*(scalar_type a) const {
322 Subclass result(*static_cast<Subclass const*>(this));
323 result *= a;
324 return result;
325 }
326
327 [[nodiscard]] Subclass operator+(scalar_type a) const {
328 Subclass result(*static_cast<Subclass const*>(this));
329 result += a;
330 return result;
331 }
332
334 // Iterators
336
337 // noexcept because vector::begin and array::begin are noexcept
338 [[nodiscard]] iterator begin() noexcept {
339 return _container.begin();
340 }
341
342 // noexcept because vector::end and array::end are noexcept
343 [[nodiscard]] iterator end() noexcept {
344 return _container.end();
345 }
346
347 // noexcept because vector::begin and array::begin are noexcept
348 [[nodiscard]] const_iterator begin() const noexcept {
349 return _container.begin();
350 }
351
352 // noexcept because vector::end and array::end are noexcept
353 [[nodiscard]] const_iterator end() const noexcept {
354 return _container.end();
355 }
356
357 // noexcept because vector::cbegin and array::cbegin are noexcept
358 [[nodiscard]] const_iterator cbegin() const noexcept {
359 return _container.cbegin();
360 }
361
362 // noexcept because vector::cend and array::cend are noexcept
363 [[nodiscard]] const_iterator cend() const noexcept {
364 return _container.cend();
365 }
366
367 template <typename Iterator>
368 [[nodiscard]] std::pair<scalar_type, scalar_type>
369 coords(Iterator const& it) const;
370
372 // Modifiers
374
375 // noexcept because vector::swap and array::swap are noexcept
376 void swap(MatrixCommon& that) noexcept {
377 std::swap(_container, that._container);
378 }
379
380 // noexcept because swap is noexcept, and so too are number_of_rows and
381 // number_of_cols
382 void transpose_no_checks() noexcept;
383
384 void transpose() {
385 matrix::throw_if_not_square(static_cast<Subclass&>(*this));
386 transpose_no_checks();
387 }
388
390 // Rows
392
393 // not noexcept because there's an allocation
394 [[nodiscard]] RowView row_no_checks(size_t i) const;
395
396 [[nodiscard]] RowView row(size_t i) const;
397
398 // not noexcept because there's an allocation
399 template <typename T>
400 void rows(T& x) const;
401
403 // Friend functions
405
406 friend std::ostream& operator<<(std::ostream& os, MatrixCommon const& x) {
407 os << detail::to_string(x);
408 return os;
409 }
410
411 private:
413 // Private data
415 container_type _container;
416 }; // class MatrixCommon
417
418 template <typename Scalar>
419 class MatrixDynamicDim {
420 public:
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;
426
427 MatrixDynamicDim(size_t r, size_t c)
428 : _number_of_cols(c), _number_of_rows(r) {}
429
430 ~MatrixDynamicDim() = default;
431
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);
435 }
436
437 protected:
438 [[nodiscard]] size_t number_of_rows_impl() const noexcept {
439 return _number_of_rows;
440 }
441
442 [[nodiscard]] size_t number_of_cols_impl() const noexcept {
443 return _number_of_cols;
444 }
445
446 private:
447 size_t _number_of_cols;
448 size_t _number_of_rows;
449 };
450
451 template <typename PlusOp,
452 typename ProdOp,
453 typename ZeroOp,
454 typename OneOp,
455 typename Scalar>
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;
462
463 using scalar_type = Scalar;
464
465 protected:
466 [[nodiscard]] static constexpr scalar_type
467 plus_no_checks_impl(scalar_type x, scalar_type y) noexcept {
468 return PlusOp()(x, y);
469 }
470
471 [[nodiscard]] static constexpr scalar_type
472 product_no_checks_impl(scalar_type x, scalar_type y) noexcept {
473 return ProdOp()(x, y);
474 }
475
476 [[nodiscard]] static constexpr scalar_type one_impl() noexcept {
477 return OneOp()();
478 }
479
480 [[nodiscard]] static constexpr scalar_type zero_impl() noexcept {
481 return ZeroOp()();
482 }
483
484 [[nodiscard]] static constexpr void const* semiring_impl() noexcept {
485 return nullptr;
486 }
487 };
488
490 // RowViews - class for cheaply storing iterators to rows
492
493 template <typename Mat, typename Subclass>
494 class RowViewCommon {
495 static_assert(IsMatrix<Mat>,
496 "the template parameter Mat must be derived from "
497 "MatrixPolymorphicBase");
498
499 public:
500 using const_iterator = typename Mat::const_iterator;
501 using iterator = typename Mat::iterator;
502
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;
506
507 using Row = typename Mat::Row;
508 using matrix_type = Mat;
509
510 [[nodiscard]] size_t size() const noexcept {
511 return static_cast<Subclass const*>(this)->length_impl();
512 }
513
514 private:
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);
517 }
518
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);
521 }
522
523 public:
524 RowViewCommon() = default;
525 RowViewCommon(RowViewCommon const&) = default;
526 RowViewCommon(RowViewCommon&&) = default;
527 RowViewCommon& operator=(RowViewCommon const&) = default;
528 RowViewCommon& operator=(RowViewCommon&&) = default;
529
530 ~RowViewCommon() = default;
531
532 explicit RowViewCommon(Row const& r)
533 : RowViewCommon(const_cast<Row&>(r).begin()) {}
534
535 // Not noexcept because iterator::operator[] isn't
536 [[nodiscard]] scalar_const_reference operator[](size_t i) const {
537 return _begin[i];
538 }
539
540 // Not noexcept because iterator::operator[] isn't
541 [[nodiscard]] scalar_reference operator[](size_t i) {
542 return _begin[i];
543 }
544
545 // Not noexcept because iterator::operator[] isn't
546 [[nodiscard]] scalar_const_reference operator()(size_t i) const {
547 return (*this)[i];
548 }
549
550 // Not noexcept because iterator::operator[] isn't
551 [[nodiscard]] scalar_reference operator()(size_t i) {
552 return (*this)[i];
553 }
554
555 // noexcept because begin() is
556 [[nodiscard]] const_iterator cbegin() const noexcept {
557 return _begin;
558 }
559
560 // not noexcept because iterator arithmetic isn't
561 [[nodiscard]] const_iterator cend() const {
562 return _begin + size();
563 }
564
565 // noexcept because begin() is
566 [[nodiscard]] const_iterator begin() const noexcept {
567 return _begin;
568 }
569
570 // not noexcept because iterator arithmetic isn't
571 [[nodiscard]] const_iterator end() const {
572 return _begin + size();
573 }
574
575 // noexcept because begin() is
576 [[nodiscard]] iterator begin() noexcept {
577 return _begin;
578 }
579
580 // not noexcept because iterator arithmetic isn't
581 [[nodiscard]] iterator end() noexcept {
582 return _begin + size();
583 }
584
586 // Arithmetic operators
588
589 // not noexcept because operator[] isn't
590 void plus_inplace_no_checks(RowViewCommon const& x);
591
592 // TODO add tests
593 void operator+=(RowViewCommon const& x);
594
595 // not noexcept because operator+= isn't
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));
599 return result;
600 }
601
602 // TODO add tests
603 [[nodiscard]] Row operator+(RowViewCommon const& x);
604
605 // not noexcept because iterator arithmetic isn't
606 void operator+=(scalar_type a) {
607 for (auto& x : *this) {
608 x = plus_no_checks(x, a);
609 }
610 }
611
612 // not noexcept because iterator arithmetic isn't
613 void operator*=(scalar_type a) {
614 for (auto& x : *this) {
615 x = product_no_checks(x, a);
616 }
617 }
618
619 // not noexcept because operator*= isn'tl
620 [[nodiscard]] Row operator*(scalar_type a) const {
621 Row result(*static_cast<Subclass const*>(this));
622 result *= a;
623 return result;
624 }
625
626 template <typename U>
627 [[nodiscard]] bool operator==(U const& that) const {
628 // TODO(1) static assert that U is Row or RowView
629 return std::equal(begin(), end(), that.begin());
630 }
631
632 template <typename U>
633 [[nodiscard]] bool operator!=(U const& that) const {
634 return !(*this == that);
635 }
636
637 template <typename U>
638 [[nodiscard]] bool operator<(U const& that) const {
640 cbegin(), cend(), that.cbegin(), that.cend());
641 }
642
643 template <typename U>
644 [[nodiscard]] bool operator>(U const& that) const {
645 return that < *this;
646 }
647
648 void swap(RowViewCommon& that) noexcept {
649 std::swap(that._begin, _begin);
650 }
651
652 friend std::ostream& operator<<(std::ostream& os, RowViewCommon const& x) {
653 os << detail::to_string(x);
654 return os;
655 }
656
657 protected:
658 explicit RowViewCommon(iterator first) : _begin(first) {}
659
660 private:
661 iterator _begin;
662 }; // class RowViewCommon
663
664 template <typename Mat, typename Subclass>
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");
669} // namespace libsemigroups::detail
670
671#include "matrix-common.tpp"
672
673#endif // LIBSEMIGROUPS_DETAIL_MATRIX_COMMON_HPP_
T copy(T... args)
T equal(T... args)
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.
T swap(T... args)