Skip to content

Commit 4ea217d

Browse files
[linalg] cleanup and hygiene
1 parent ed89b71 commit 4ea217d

File tree

1 file changed

+44
-42
lines changed

1 file changed

+44
-42
lines changed

linalg/naive/fcarouge/linalg.hpp

+44-42
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ For more information, please refer to <https://unlicense.org> */
4646

4747
#include "fcarouge/utility.hpp"
4848

49-
#include <initializer_list>
49+
#include <algorithm>
5050

5151
namespace fcarouge {
5252
//! @name Algebraic Types
@@ -70,57 +70,48 @@ template <typename Type = double, auto Row = 1, auto Column = 1> struct matrix {
7070

7171
inline constexpr matrix &operator=(matrix &&other) = default;
7272

73-
inline constexpr explicit(false) matrix(Type element)
74-
requires(Row == 1 && Column == 1)
75-
{
76-
data[0][0] = element;
77-
}
73+
inline constexpr matrix(const std::same_as<Type> auto &...elements)
74+
requires(Row == 1 && sizeof...(elements) == Column)
75+
: data{{elements...}} {}
7876

79-
inline constexpr explicit matrix(Type(element)[1])
80-
requires(Row == 1 && Column == 1)
77+
inline constexpr matrix(const std::same_as<Type> auto &...elements)
78+
requires(Row != 1 && Column == 1 && sizeof...(elements) == Row)
8179
{
82-
data[0][0] = element[0];
80+
decltype(Row) i{0};
81+
([&] { data[i++][0] = elements; }(), ...);
8382
}
8483

85-
inline constexpr explicit matrix(Type(column)[Row])
86-
requires(Row != 1 && Column == 1)
84+
// Combine?
85+
inline constexpr explicit matrix(Type (&elements)[Column])
86+
requires(Row == 1)
8787
{
88-
for (decltype(Row) i{0}; i < Row; ++i) {
89-
data[i][0] = column[i];
88+
for (decltype(Column) j{0}; j < Column; ++j) {
89+
data[0][j] = elements[j];
9090
}
9191
}
9292

93-
inline constexpr explicit matrix(Type(row)[Column])
94-
requires(Row == 1 && Column != 1)
93+
inline constexpr explicit matrix(Type (&elements)[Row])
94+
requires(Row != 1 && Column == 1)
9595
{
96-
for (decltype(Column) j{0}; j < Column; ++j) {
97-
data[0][j] = row[j];
96+
for (decltype(Row) i{0}; i < Row; ++i) {
97+
data[i][0] = elements[i];
9898
}
9999
}
100100

101-
inline constexpr matrix(const auto &...elements)
102-
requires(Row != 1 && Column == 1 && sizeof...(elements) == Row)
101+
template <typename... Types, auto... Columns>
102+
matrix(const Types (&...rows)[Columns])
103+
requires(std::conjunction_v<std::is_same<Type, Types>...> &&
104+
((Columns == Column) && ... && true))
103105
{
104106
decltype(Row) i{0};
105-
([&] { data[i++][0] = elements; }(), ...);
106-
}
107-
108-
inline constexpr matrix(const auto &...elements)
109-
requires(Row == 1 && Column != 1 && sizeof...(elements) == Column)
110-
{
111-
decltype(Column) j{0};
112-
([&] { data[0][j++] = elements; }(), ...);
113-
}
114-
115-
inline constexpr explicit matrix(
116-
const std::initializer_list<std::initializer_list<Type>> &rows) {
117-
// static_assert
118-
for (decltype(Row) i{0}; auto &&elements : rows) {
119-
for (decltype(Column) j{0}; auto &&element : elements) {
120-
data[i][j++] = element;
121-
}
122-
++i;
123-
}
107+
(
108+
[&] {
109+
for (decltype(Column) j{0}; auto &&element : rows) {
110+
data[i][j++] = element;
111+
}
112+
++i;
113+
}(),
114+
...);
124115
}
125116

126117
[[nodiscard]] inline constexpr explicit(false) operator Type() const
@@ -158,10 +149,6 @@ template <typename Type = double, auto Row = 1, auto Column = 1> struct matrix {
158149
return data[row][column];
159150
}
160151

161-
[[nodiscard]] inline constexpr bool operator==(const matrix &other) const
162-
requires(Row != 1 || Column != 1)
163-
= default;
164-
165152
[[no_unique_address]] Type data[Row][Column]{};
166153
};
167154

@@ -345,6 +332,21 @@ transpose(const matrix<Type, Row, Column> &lhs) {
345332

346333
return result;
347334
}
335+
336+
template <typename Type, auto Row, auto Column>
337+
[[nodiscard]] inline constexpr bool
338+
operator==(const matrix<Type, Row, Column> &lhs,
339+
const matrix<Type, Row, Column> &rhs) {
340+
for (decltype(Row) i{0}; i < Row; ++i) {
341+
for (decltype(Column) j{0}; j < Column; ++j) {
342+
if (lhs.data[i][j] != rhs.data[i][j]) {
343+
return false;
344+
}
345+
}
346+
}
347+
return true;
348+
}
349+
348350
} // namespace fcarouge
349351

350352
#endif // FCAROUGE_LINALG_HPP

0 commit comments

Comments
 (0)