Skip to content

Commit ed89b71

Browse files
[linalg] cleanup and hygiene
1 parent 05d23be commit ed89b71

File tree

3 files changed

+50
-21
lines changed

3 files changed

+50
-21
lines changed

linalg/eigen/fcarouge/linalg.hpp

+6
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ namespace fcarouge {
5454
//! @name Algebraic Types
5555
//! @{
5656
//! @brief Compile-time sized Eigen3 matrix.
57+
//!
58+
//! @details Facade for Eigen3 implementation compatibility.
59+
//!
60+
//! @tparam Type The matrix element type.
61+
//! @tparam Row The number of rows of the matrix.
62+
//! @tparam Column The number of columns of the matrix.
5763
template <typename Type = double, auto Row = 1, auto Column = 1>
5864
using matrix = Eigen::Matrix<Type, Row, Column>;
5965

linalg/lazy/fcarouge/linalg.hpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,9 @@ inline constexpr auto make_generator(Type element) -> std::generator<Type> {
8888
//! @details The matrix is a generator. A coroutine range. Lazily generated
8989
//! elements and computed operations. Commonalities with ranges.
9090
//!
91-
//! @tparam Type The linear algebra underlying element type.
92-
//! @tparam Row The number of rows.
93-
//! @tparam Column The number of columns.
91+
//! @tparam Type The matrix element type.
92+
//! @tparam Row The number of rows of the matrix.
93+
//! @tparam Column The number of columns of the matrix.
9494
//! @tparam Copyable Whether the instance is fully lazy or deeply clones itself
9595
//! on copies. Useful for named values. May not be commonly used.
9696
//!
@@ -161,7 +161,7 @@ struct matrix {
161161
(co_yield elements_copy, ...);
162162
}(elements...)} {}
163163

164-
inline constexpr matrix(Type (&elements)[Row * Column])
164+
inline constexpr explicit matrix(Type (&elements)[Row * Column])
165165
requires(Row == 1 || Column == 1)
166166
: genie{make_generator(std::to_array(elements))} {}
167167

@@ -211,7 +211,7 @@ struct matrix {
211211
}
212212

213213
[[nodiscard]] inline constexpr Type operator[](auto index) const
214-
requires(Row > 1 && Column == 1)
214+
requires(Row != 1 && Column == 1)
215215
{
216216
std::array<Type, Row * Column> elements; // std::ranges::to
217217

@@ -233,7 +233,7 @@ struct matrix {
233233
}
234234

235235
[[nodiscard]] inline constexpr Type operator()(auto index) const
236-
requires(Row > 1 && Column == 1)
236+
requires(Row != 1 && Column == 1)
237237
{
238238
std::array<Type, Row * Column> elements; // std::ranges::to
239239

linalg/naive/fcarouge/linalg.hpp

+38-15
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,25 @@ For more information, please refer to <https://unlicense.org> */
5151
namespace fcarouge {
5252
//! @name Algebraic Types
5353
//! @{
54-
//! @brief Matrix.
54+
//! @brief Naive matrix.
55+
//!
56+
//! @details An array-of-arrays naive implementation matrix. The implementation
57+
//! is constexpr compatible.
58+
//!
59+
//! @tparam Type The matrix element type.
60+
//! @tparam Row The number of rows of the matrix.
61+
//! @tparam Column The number of columns of the matrix.
5562
template <typename Type = double, auto Row = 1, auto Column = 1> struct matrix {
5663
inline constexpr matrix() = default;
5764

65+
inline constexpr matrix(const matrix &other) = default;
66+
67+
inline constexpr matrix &operator=(const matrix &other) = default;
68+
69+
inline constexpr matrix(matrix &&other) = default;
70+
71+
inline constexpr matrix &operator=(matrix &&other) = default;
72+
5873
inline constexpr explicit(false) matrix(Type element)
5974
requires(Row == 1 && Column == 1)
6075
{
@@ -68,30 +83,30 @@ template <typename Type = double, auto Row = 1, auto Column = 1> struct matrix {
6883
}
6984

7085
inline constexpr explicit matrix(Type(column)[Row])
71-
requires(Row > 1 && Column == 1)
86+
requires(Row != 1 && Column == 1)
7287
{
7388
for (decltype(Row) i{0}; i < Row; ++i) {
7489
data[i][0] = column[i];
7590
}
7691
}
7792

7893
inline constexpr explicit matrix(Type(row)[Column])
79-
requires(Row == 1 && Column > 1)
94+
requires(Row == 1 && Column != 1)
8095
{
8196
for (decltype(Column) j{0}; j < Column; ++j) {
8297
data[0][j] = row[j];
8398
}
8499
}
85100

86101
inline constexpr matrix(const auto &...elements)
87-
requires(Row > 1 && Column == 1 && sizeof...(elements) == Row)
102+
requires(Row != 1 && Column == 1 && sizeof...(elements) == Row)
88103
{
89104
decltype(Row) i{0};
90105
([&] { data[i++][0] = elements; }(), ...);
91106
}
92107

93108
inline constexpr matrix(const auto &...elements)
94-
requires(Row == 1 && Column > 1 && sizeof...(elements) == Column)
109+
requires(Row == 1 && Column != 1 && sizeof...(elements) == Column)
95110
{
96111
decltype(Column) j{0};
97112
([&] { data[0][j++] = elements; }(), ...);
@@ -108,42 +123,43 @@ template <typename Type = double, auto Row = 1, auto Column = 1> struct matrix {
108123
}
109124
}
110125

111-
inline constexpr explicit(false) operator Type() const
126+
[[nodiscard]] inline constexpr explicit(false) operator Type() const
112127
requires(Row == 1 && Column == 1)
113128
{
114129
return data[0][0];
115130
}
116131

117-
inline constexpr const Type &operator[](auto index) const
118-
requires(Row > 1 && Column == 1)
132+
[[nodiscard]] inline constexpr const Type &operator[](auto index) const
133+
requires(Row != 1 && Column == 1)
119134
{
120135
return data[index][0];
121136
}
122137

123-
inline constexpr const Type &operator[](auto index) const
138+
[[nodiscard]] inline constexpr const Type &operator[](auto index) const
124139
requires(Row == 1)
125140
{
126141
return data[0][index];
127142
}
128143

129-
inline constexpr const Type &operator()(auto index) const
130-
requires(Row > 1 && Column == 1)
144+
[[nodiscard]] inline constexpr const Type &operator()(auto index) const
145+
requires(Row != 1 && Column == 1)
131146
{
132147
return data[index][0];
133148
}
134149

135-
inline constexpr const Type &operator()(auto index) const
150+
[[nodiscard]] inline constexpr const Type &operator()(auto index) const
136151
requires(Row == 1)
137152
{
138153
return data[0][index];
139154
}
140155

141-
inline constexpr const Type &operator()(auto row, auto column) const {
156+
[[nodiscard]] inline constexpr const Type &operator()(auto row,
157+
auto column) const {
142158
return data[row][column];
143159
}
144160

145161
[[nodiscard]] inline constexpr bool operator==(const matrix &other) const
146-
requires(Row > 1 || Column > 1)
162+
requires(Row != 1 || Column != 1)
147163
= default;
148164

149165
[[no_unique_address]] Type data[Row][Column]{};
@@ -166,7 +182,14 @@ template <typename Type, auto Row, auto Column>
166182
matrix(const Type (&)[Row][Column]) -> matrix<Type, Row, Column>;
167183

168184
template <typename Type, auto Row>
169-
matrix(const Type (&)[Row]) -> matrix<Type, Row, 1>; // column_vector
185+
matrix(const Type (&)[Row]) -> matrix<Type, Row, 1>;
186+
187+
template <typename... Types, auto... Columns>
188+
requires(std::conjunction_v<std::is_same<first_t<Types...>, Types>...> &&
189+
((Columns == first_v<Columns>)&&... && true))
190+
matrix(const Types (&...rows)[Columns])
191+
-> matrix<std::remove_cvref_t<first_t<Types...>>, sizeof...(Columns),
192+
(Columns, ...)>;
170193
//! @}
171194

172195
//! @name Algebraic Named Values

0 commit comments

Comments
 (0)