libsemigroups  v3.6.0
C++ library for semigroups and monoids
Loading...
Searching...
No Matches
matrix-view.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_MATRIX_VIEW_HPP_
20#define LIBSEMIGROUPS_MATRIX_VIEW_HPP_
21
22#include <cstddef> // for size_t
23
24#include "detail/matrix-common.hpp" // for RowViewCommon
25
26namespace libsemigroups {
27#ifndef LIBSEMIGROUPS_PARSED_BY_DOXYGEN
28 template <typename PlusOp,
29 typename ProdOp,
30 typename ZeroOp,
31 typename OneOp,
32 size_t R,
33 size_t C,
34 typename Scalar>
35 class StaticMatrix;
36
37 template <typename... Args>
38 class DynamicMatrix;
39#endif
40
42 // StaticRowViews - static arithmetic
44
81 template <typename PlusOp,
82 typename ProdOp,
83 typename ZeroOp,
84 typename OneOp,
85 size_t C,
86 typename Scalar>
88 : public detail::RowViewCommon<
89 StaticMatrix<PlusOp, ProdOp, ZeroOp, OneOp, 1, C, Scalar>,
90 StaticRowView<PlusOp, ProdOp, ZeroOp, OneOp, C, Scalar>>,
91 public detail::
92 MatrixStaticArithmetic<PlusOp, ProdOp, ZeroOp, OneOp, Scalar> {
93 private:
94 using RowViewCommon = detail::RowViewCommon<
97 friend RowViewCommon;
98
99 template <size_t R>
100 using StaticMatrix_ = ::libsemigroups::
101 StaticMatrix<PlusOp, ProdOp, ZeroOp, OneOp, R, C, Scalar>;
102
103 public:
105 using const_iterator = typename RowViewCommon::const_iterator;
106
108 using iterator = typename RowViewCommon::iterator;
109
111 using scalar_type = Scalar;
112
114 using scalar_reference = typename RowViewCommon::scalar_reference;
115
117 // clang-format off
118 // NOLINTNEXTLINE(whitespace/line_length)
119 using scalar_const_reference = typename RowViewCommon::scalar_const_reference;
120 // clang-format on
121
123 using matrix_type = typename RowViewCommon::matrix_type;
124
126 using Row = typename matrix_type::Row;
127
129 StaticRowView() = default;
130
132 StaticRowView(StaticRowView const&) = default;
133
136
139
142
143#ifndef LIBSEMIGROUPS_PARSED_BY_DOXYGEN
144 using RowViewCommon::RowViewCommon;
145
146 // TODO(2) This constructor should be private
147 template <size_t R>
148 StaticRowView(StaticMatrix_<R> const*,
149 typename RowViewCommon::iterator it,
150 size_t)
151 : RowViewCommon(it) {}
152
153 using RowViewCommon::size;
154#else
166 explicit StaticRowView(Row const& r);
167
180 static constexpr size_t size() const noexcept;
181
194 iterator begin() noexcept;
195
210
223 const_iterator cbegin() const noexcept;
224
239
257 scalar_reference operator()(size_t i);
258
276 scalar_const_reference operator()(size_t i) const;
277
279 scalar_reference operator[](size_t i);
280
282 scalar_const_reference operator[](size_t i) const;
283
304 template <typename U>
305 bool operator==(U const& that) const;
306
327 template <typename U>
328 bool operator!=(U const& that) const;
329
335 // clang-format off
336 // NOLINTNEXTLINE(whitespace/line_length)
340 // clang-format on
353 template <typename U>
354 bool operator<(U const& that) const;
355
377 template <typename U>
378 bool operator<(U const& that) const;
379
399 Row plus_no_checks(StaticRowView const& that) const;
400
416 Row operator+(StaticRowView const& that);
417
437
452 void operator+=(StaticRowView const& that);
453
467 void operator+=(scalar_type a);
468
486 Row operator*(scalar_type a) const;
487
501 void operator*=(scalar_type a);
502#endif
503
504 private:
505 static constexpr size_t length_impl() noexcept {
506 return C;
507 }
508 };
509
511 // DynamicRowViews - static arithmetic
513
514#ifndef LIBSEMIGROUPS_PARSED_BY_DOXYGEN
515 // Doxygen needs to ignore this so that the actual implementation of
516 // DynamicRowView gets documented.
517 template <typename... Args>
518 class DynamicRowView;
519#endif
520
558 template <typename PlusOp,
559 typename ProdOp,
560 typename ZeroOp,
561 typename OneOp,
562 typename Scalar>
563 class DynamicRowView<PlusOp, ProdOp, ZeroOp, OneOp, Scalar>
564 : public detail::RowViewCommon<
565 DynamicMatrix<PlusOp, ProdOp, ZeroOp, OneOp, Scalar>,
566 DynamicRowView<PlusOp, ProdOp, ZeroOp, OneOp, Scalar>>,
567 public detail::
568 MatrixStaticArithmetic<PlusOp, ProdOp, ZeroOp, OneOp, Scalar> {
569 private:
570 using DynamicMatrix_ = DynamicMatrix<PlusOp, ProdOp, ZeroOp, OneOp, Scalar>;
571 using RowViewCommon = detail::RowViewCommon<
572 DynamicMatrix_,
574 friend RowViewCommon;
575
576 public:
578 using const_iterator = typename RowViewCommon::const_iterator;
579
581 using iterator = typename RowViewCommon::iterator;
582
584 using scalar_type = Scalar;
585
587 using scalar_reference = typename RowViewCommon::scalar_reference;
588
590 // clang-format off
591 // NOLINTNEXTLINE(whitespace/line_length)
592 using scalar_const_reference = typename RowViewCommon::scalar_const_reference;
593 // clang-format on
594
596 using matrix_type = typename RowViewCommon::matrix_type;
597
599 using Row = typename matrix_type::Row;
600
602 DynamicRowView() = default;
603
606
609
612
615
617 explicit DynamicRowView(Row const& r)
618 : RowViewCommon(r), _length(r.number_of_cols()) {}
619
620#ifndef LIBSEMIGROUPS_PARSED_BY_DOXYGEN
621 using RowViewCommon::RowViewCommon;
622
623 // TODO(2) This constructor should be private
624 DynamicRowView(DynamicMatrix_ const*, iterator const& it, size_t N)
625 : RowViewCommon(it), _length(N) {}
626
627 using RowViewCommon::size;
628#else
630 size_t size() const noexcept;
631
633 iterator begin() noexcept;
634
637
639 const_iterator cbegin() const noexcept;
640
643
645 scalar_reference operator()(size_t i);
646
648 scalar_const_reference operator()(size_t i) const;
649
651 scalar_reference operator[](size_t i);
652
654 scalar_const_reference operator[](size_t i) const;
655
657 template <typename U>
658 bool operator==(U const& that) const;
659
661 template <typename U>
662 bool operator!=(U const& that) const;
663
665 template <typename U>
666 bool operator<(U const& that) const;
667
669 template <typename U>
670 bool operator<(U const& that) const;
671
674
676 Row operator+(DynamicRowView const& that);
677
680
682 void operator+=(DynamicRowView const& that);
683
685 void operator+=(scalar_type a);
686
688 Row operator*(scalar_type a) const;
689
691 void operator*=(scalar_type a);
692#endif // LIBSEMIGROUPS_PARSED_BY_DOXYGEN
693
694 private:
695 size_t length_impl() const noexcept {
696 return _length;
697 }
698 size_t _length;
699 };
700
702 // DynamicRowViews - dynamic arithmetic
704
725 template <typename Semiring, typename Scalar>
726 class DynamicRowView<Semiring, Scalar>
727 : public detail::RowViewCommon<DynamicMatrix<Semiring, Scalar>,
728 DynamicRowView<Semiring, Scalar>> {
729 private:
730 using DynamicMatrix_ = DynamicMatrix<Semiring, Scalar>;
731 friend DynamicMatrix_;
732 using RowViewCommon
733 = detail::RowViewCommon<DynamicMatrix_,
735 friend RowViewCommon;
736
737 public:
739 using const_iterator = typename RowViewCommon::const_iterator;
740
742 using iterator = typename RowViewCommon::iterator;
743
745 using scalar_type = Scalar;
746
748 using scalar_reference = typename RowViewCommon::scalar_reference;
749
751 // clang-format off
752 // NOLINTNEXTLINE(whitespace/line_length)
753 using scalar_const_reference = typename RowViewCommon::scalar_const_reference;
754 // clang-format on
755
757 using matrix_type = typename RowViewCommon::matrix_type;
758
760 using Row = typename matrix_type::Row;
761
763 DynamicRowView() = default;
764
766 DynamicRowView(DynamicRowView const&) = default;
767
769 DynamicRowView(DynamicRowView&&) = default;
770
772 DynamicRowView& operator=(DynamicRowView const&) = default;
773
776
778 explicit DynamicRowView(Row const& r) : RowViewCommon(r), _matrix(&r) {}
779
780#ifndef LIBSEMIGROUPS_PARSED_BY_DOXYGEN
781 using RowViewCommon::RowViewCommon;
782
783 // TODO(2) This constructor should be private
784 DynamicRowView(DynamicMatrix_ const* mat, iterator const& it, size_t)
785 : RowViewCommon(it), _matrix(mat) {}
786
787 using RowViewCommon::size;
788#else
790 size_t size() const noexcept;
791
793 iterator begin() noexcept;
794
796 iterator end();
797
799 const_iterator cbegin() const noexcept;
800
802 iterator cend();
803
805 scalar_reference operator()(size_t i);
806
808 scalar_const_reference operator()(size_t i) const;
809
811 scalar_reference operator[](size_t i);
812
814 scalar_const_reference operator[](size_t i) const;
815
817 template <typename U>
818 bool operator==(U const& that) const;
819
821 template <typename U>
822 bool operator!=(U const& that) const;
823
825 template <typename U>
826 bool operator<(U const& that) const;
827
829 template <typename U>
830 bool operator<(U const& that) const;
831
833 Row plus_no_checks(DynamicRowView const& that);
834
836 Row operator+(DynamicRowView const& that);
837
839 void plus_inplace_no_checks(DynamicRowView const& that);
840
842 void operator+=(DynamicRowView const& that);
843
845 void operator+=(scalar_type a);
846
848 Row operator*(scalar_type a) const;
849
851 void operator*=(scalar_type a);
852#endif
853
854 private:
855 size_t length_impl() const noexcept {
856 return _matrix->number_of_cols();
857 }
858
859 scalar_type plus_no_checks_impl(scalar_type x,
860 scalar_type y) const noexcept {
861 return _matrix->plus_no_checks_impl(x, y);
862 }
863
864 scalar_type product_no_checks_impl(scalar_type x,
865 scalar_type y) const noexcept {
866 return _matrix->product_no_checks_impl(x, y);
867 }
868
869 DynamicMatrix_ const* _matrix;
870 };
871} // namespace libsemigroups
872#endif // LIBSEMIGROUPS_MATRIX_VIEW_HPP_
DynamicRowView & operator=(DynamicRowView &&)=default
Default move assignment operator.
size_t size() const noexcept
Returns the size of the row.
DynamicRowView & operator=(DynamicRowView const &)=default
Default copy assignment operator.
DynamicRowView(DynamicRowView const &)=default
Default copy constructor.
DynamicRowView(DynamicRowView &&)=default
Default move constructor.
DynamicRowView(Row const &r)
Construct a row view from a Row.
Definition matrix-view.hpp:617
typename RowViewCommon::iterator iterator
Alias for const iterators pointing at entries of a matrix.
Definition matrix-view.hpp:581
typename RowViewCommon::scalar_const_reference scalar_const_reference
Alias for const references to the template parameter Scalar.
Definition matrix-view.hpp:592
typename RowViewCommon::scalar_reference scalar_reference
Alias for references to the template parameter Scalar.
Definition matrix-view.hpp:587
iterator begin() noexcept
Returns a iterator pointing at the first entry.
iterator cend()
Returns a const iterator pointing one beyond the last entry of the row.
typename matrix_type::Row Row
Alias for the type of a row in the underlying matrix.
Definition matrix-view.hpp:599
typename RowViewCommon::const_iterator const_iterator
Alias for const iterators pointing at entries of a matrix.
Definition matrix-view.hpp:578
const_iterator cbegin() const noexcept
Returns a const iterator pointing at the first entry.
void plus_inplace_no_checks(DynamicRowView const &that)
Sums a row view with another row view in-place.
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-view.hpp:584
Row plus_no_checks(DynamicRowView const &that)
Sum row views.
typename RowViewCommon::matrix_type matrix_type
Alias for the type of the underlying matrix.
Definition matrix-view.hpp:596
size_t size() const noexcept
Returns the size of the row.
DynamicRowView & operator=(DynamicRowView const &)=default
Default copy assignment operator.
typename RowViewCommon::iterator iterator
Alias for const iterators pointing at entries of a matrix.
Definition matrix-view.hpp:741
typename RowViewCommon::scalar_const_reference scalar_const_reference
Alias for const references to the template parameter Scalar.
Definition matrix-view.hpp:752
typename RowViewCommon::scalar_reference scalar_reference
Alias for references to the template parameter Scalar.
Definition matrix-view.hpp:747
iterator begin() noexcept
Returns a iterator pointing at the first entry.
iterator cend()
Returns a const iterator pointing one beyond the last entry of the row.
typename matrix_type::Row Row
Alias for the type of a row in the underlying matrix.
Definition matrix-view.hpp:759
typename RowViewCommon::const_iterator const_iterator
Alias for const iterators pointing at entries of a matrix.
Definition matrix-view.hpp:738
const_iterator cbegin() const noexcept
Returns a const iterator pointing at the first entry.
void plus_inplace_no_checks(DynamicRowView const &that)
Sums a row view with another row view in-place.
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-view.hpp:744
Row plus_no_checks(DynamicRowView const &that)
Sum row views.
typename RowViewCommon::matrix_type matrix_type
Alias for the type of the underlying matrix.
Definition matrix-view.hpp:756
DynamicRowView()=default
Default constructor.
Static matrix class.
Definition matrix-class.hpp:174
void plus_inplace_no_checks(StaticRowView const &that)
Sums a row view with another row view in-place.
typename RowViewCommon::iterator iterator
Alias for const iterators pointing at entries of a matrix.
Definition matrix-view.hpp:108
typename RowViewCommon::scalar_const_reference scalar_const_reference
Alias for const references to the template parameter Scalar.
Definition matrix-view.hpp:119
StaticRowView & operator=(StaticRowView &&)=default
Default move assignment operator.
typename RowViewCommon::scalar_reference scalar_reference
Alias for references to the template parameter Scalar.
Definition matrix-view.hpp:114
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-view.hpp:126
typename RowViewCommon::const_iterator const_iterator
Alias for const iterators pointing at entries of a matrix.
Definition matrix-view.hpp:105
const_iterator cbegin() const noexcept
Returns a const iterator pointing at the first entry.
Row plus_no_checks(StaticRowView const &that) const
Sum row views.
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-view.hpp:111
StaticRowView(StaticRowView &&)=default
Default move constructor.
typename RowViewCommon::matrix_type matrix_type
Alias for the type of the underlying matrix.
Definition matrix-view.hpp:123
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.
Namespace for everything in the libsemigroups library.
Definition action.hpp:44