@@ -46,7 +46,7 @@ For more information, please refer to <https://unlicense.org> */
46
46
47
47
#include " fcarouge/utility.hpp"
48
48
49
- #include < initializer_list >
49
+ #include < algorithm >
50
50
51
51
namespace fcarouge {
52
52
// ! @name Algebraic Types
@@ -70,57 +70,48 @@ template <typename Type = double, auto Row = 1, auto Column = 1> struct matrix {
70
70
71
71
inline constexpr matrix &operator =(matrix &&other) = default ;
72
72
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...}} {}
78
76
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 )
81
79
{
82
- data[0 ][0 ] = element[0 ];
80
+ decltype (Row) i{0 };
81
+ ([&] { data[i++][0 ] = elements; }(), ...);
83
82
}
84
83
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 )
87
87
{
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 ];
90
90
}
91
91
}
92
92
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 )
95
95
{
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 ];
98
98
}
99
99
}
100
100
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))
103
105
{
104
106
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
+ ...);
124
115
}
125
116
126
117
[[nodiscard]] inline constexpr explicit (false ) operator Type() const
@@ -158,10 +149,6 @@ template <typename Type = double, auto Row = 1, auto Column = 1> struct matrix {
158
149
return data[row][column];
159
150
}
160
151
161
- [[nodiscard]] inline constexpr bool operator ==(const matrix &other) const
162
- requires (Row != 1 || Column != 1 )
163
- = default ;
164
-
165
152
[[no_unique_address]] Type data[Row][Column]{};
166
153
};
167
154
@@ -345,6 +332,21 @@ transpose(const matrix<Type, Row, Column> &lhs) {
345
332
346
333
return result;
347
334
}
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
+
348
350
} // namespace fcarouge
349
351
350
352
#endif // FCAROUGE_LINALG_HPP
0 commit comments