matrix_shape.h Source File

CPP API: matrix_shape.h Source File
matrix_shape.h
Go to the documentation of this file.
1 /*
2 * Copyright (C) 2020-2026 MEmilio
3 *
4 * Authors: Daniel Abele
5 *
6 * Contact: Martin J. Kuehn <Martin.Kuehn@DLR.de>
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
32 #ifndef MIO_MATH_MATRIX_SHAPE_H
33 #define MIO_MATH_MATRIX_SHAPE_H
34 
35 #include "memilio/math/eigen.h" // IWYU pragma: keep
36 #include "memilio/io/io.h"
37 
38 namespace mio
39 {
44 template <typename FP>
46 {
47 public:
48  using Matrix = Eigen::MatrixX<FP>;
54  RectMatrixShape(Eigen::Index r, Eigen::Index c)
55  : m_rows(r)
56  , m_cols(c)
57  {
58  }
59 
65  template <class ME>
66  static RectMatrixShape get_shape_of(const Eigen::MatrixBase<ME>& m)
67  {
68  return {m.rows(), m.cols()};
69  }
70 
74  Eigen::Index rows() const
75  {
76  return m_rows;
77  }
81  Eigen::Index cols() const
82  {
83  return m_cols;
84  }
85 
89  bool operator==(const RectMatrixShape& other) const
90  {
91  return other.m_rows == m_rows && other.m_cols == m_cols;
92  }
93  bool operator!=(const RectMatrixShape& other) const
94  {
95  return !(*this == other);
96  }
97 
102  template <class IOContext>
103  void serialize(IOContext& io) const
104  {
105  auto obj = io.create_object("RectMatrixShape");
106  obj.add_element("Rows", rows());
107  obj.add_element("Columns", cols());
108  }
109 
114  template <class IOContext>
116  {
117  auto obj = io.expect_object("RectMatrixShape");
118  auto r = obj.expect_element("Rows", Tag<Eigen::Index>{});
119  auto c = obj.expect_element("Columns", Tag<Eigen::Index>{});
120  return apply(
121  io,
122  [](auto&& r_, auto&& c_) -> IOResult<RectMatrixShape> {
123  if (r_ > 0 && c_ > 0)
124  return success(RectMatrixShape{r_, c_});
125  else
126  return failure(StatusCode::OutOfRange, "Rows and Columns of RectMatrixShape must be positive.");
127  },
128  r, c);
129  }
130 
131 private:
132  Eigen::Index m_rows;
133  Eigen::Index m_cols;
134 };
135 
140 template <typename FP>
142 {
143 public:
144  using Matrix = Eigen::MatrixX<FP>;
149  explicit SquareMatrixShape(Eigen::Index r)
150  : m_rows(r)
151  {
152  }
153 
159  template <class ME>
160  static SquareMatrixShape get_shape_of(const Eigen::MatrixBase<ME>& m)
161  {
162  assert(m.rows() == m.cols());
163  return SquareMatrixShape{m.rows()};
164  }
165 
170  Eigen::Index rows() const
171  {
172  return m_rows;
173  }
178  Eigen::Index cols() const
179  {
180  return m_rows;
181  }
185  Eigen::Index size() const
186  {
187  return m_rows;
188  }
189 
193  bool operator==(const SquareMatrixShape& other) const
194  {
195  return other.m_rows == m_rows;
196  }
197  bool operator!=(const SquareMatrixShape& other) const
198  {
199  return !(*this == other);
200  }
201 
206  template <class IOContext>
207  void serialize(IOContext& io) const
208  {
209  auto obj = io.create_object("SquareMatrixShape");
210  obj.add_element("Rows", rows());
211  }
212 
217  template <class IOContext>
219  {
220  auto obj = io.expect_object("SquareMatrixShape");
221  auto r = obj.expect_element("Rows", Tag<Eigen::Index>{});
222  return apply(
223  io,
224  [](auto&& r_) -> IOResult<SquareMatrixShape> {
225  if (r_ > 0)
226  return success(SquareMatrixShape{r_});
227  else
228  return failure(StatusCode::OutOfRange, "Rows of SquareMatrixShape must be positive.");
229  },
230  r);
231  }
232 
233 private:
234  Eigen::Index m_rows;
235 };
236 
241 template <typename FP>
243 {
244 
245 public:
246  using Matrix = Eigen::VectorX<FP>;
251  explicit ColumnVectorShape(Eigen::Index r)
252  : m_rows(r)
253  {
254  }
255 
261  template <class ME>
262  static ColumnVectorShape get_shape_of(const Eigen::MatrixBase<ME>& m)
263  {
264  assert(m.cols() == 1);
265  return ColumnVectorShape{m.rows()};
266  }
267 
271  Eigen::Index rows() const
272  {
273  return m_rows;
274  }
279  Eigen::Index cols() const
280  {
281  return 1;
282  }
286  Eigen::Index size() const
287  {
288  return m_rows;
289  }
290 
294  bool operator==(const ColumnVectorShape& other) const
295  {
296  return other.m_rows == m_rows;
297  }
298  bool operator!=(const ColumnVectorShape& other) const
299  {
300  return !(*this == other);
301  }
302 
307  template <class IOContext>
308  void serialize(IOContext& io) const
309  {
310  auto obj = io.create_object("ColumnVectorShape");
311  obj.add_element("Rows", rows());
312  }
313 
318  template <class IOContext>
320  {
321  auto obj = io.expect_object("ColumnVectorShape");
322  auto r = obj.expect_element("Rows", Tag<Eigen::Index>{});
323  return apply(
324  io,
325  [](auto&& r_) -> IOResult<ColumnVectorShape> {
326  if (r_ > 0)
327  return success(ColumnVectorShape{r_});
328  else
329  return failure(StatusCode::OutOfRange, "Rows of ColumnVectorShape must be positive");
330  },
331  r);
332  }
333 
334 private:
335  Eigen::Index m_rows;
336 };
337 
338 } // namespace mio
339 
340 #endif // MIO_MATH_MATRIX_SHAPE_H
shape of a column vector.
Definition: matrix_shape.h:243
Eigen::Index size() const
number of rows.
Definition: matrix_shape.h:286
Eigen::Index rows() const
number of rows.
Definition: matrix_shape.h:271
Eigen::Index cols() const
number of columns.
Definition: matrix_shape.h:279
Eigen::Index m_rows
Definition: matrix_shape.h:335
Eigen::VectorX< FP > Matrix
Definition: matrix_shape.h:246
bool operator==(const ColumnVectorShape &other) const
equality comparators.
Definition: matrix_shape.h:294
static IOResult< ColumnVectorShape > deserialize(IOContext &io)
deserialize an object of this class.
Definition: matrix_shape.h:319
ColumnVectorShape(Eigen::Index r)
construct the shape of a column vector.
Definition: matrix_shape.h:251
static ColumnVectorShape get_shape_of(const Eigen::MatrixBase< ME > &m)
extract the shape of a column vector.
Definition: matrix_shape.h:262
bool operator!=(const ColumnVectorShape &other) const
Definition: matrix_shape.h:298
void serialize(IOContext &io) const
serialize this.
Definition: matrix_shape.h:308
shape of a rectangular matrix.
Definition: matrix_shape.h:46
static IOResult< RectMatrixShape > deserialize(IOContext &io)
deserialize an object of this class.
Definition: matrix_shape.h:115
static RectMatrixShape get_shape_of(const Eigen::MatrixBase< ME > &m)
extract the shape of a rectangular matrix.
Definition: matrix_shape.h:66
Eigen::MatrixX< FP > Matrix
Definition: matrix_shape.h:48
RectMatrixShape(Eigen::Index r, Eigen::Index c)
construct the shape of a rectangular matrix.
Definition: matrix_shape.h:54
Eigen::Index rows() const
number of rows.
Definition: matrix_shape.h:74
bool operator!=(const RectMatrixShape &other) const
Definition: matrix_shape.h:93
Eigen::Index m_rows
Definition: matrix_shape.h:132
Eigen::Index cols() const
number of columns.
Definition: matrix_shape.h:81
bool operator==(const RectMatrixShape &other) const
equality comparators.
Definition: matrix_shape.h:89
void serialize(IOContext &io) const
serialize this.
Definition: matrix_shape.h:103
Eigen::Index m_cols
Definition: matrix_shape.h:133
shape of a square matrix.
Definition: matrix_shape.h:142
static IOResult< SquareMatrixShape > deserialize(IOContext &io)
deserialize an object of this class.
Definition: matrix_shape.h:218
Eigen::Index cols() const
number of columns.
Definition: matrix_shape.h:178
static SquareMatrixShape get_shape_of(const Eigen::MatrixBase< ME > &m)
extract the shape of a square matrix.
Definition: matrix_shape.h:160
Eigen::Index size() const
number of rows or columns.
Definition: matrix_shape.h:185
bool operator!=(const SquareMatrixShape &other) const
Definition: matrix_shape.h:197
SquareMatrixShape(Eigen::Index r)
construct a square matrix of dimensions r
Definition: matrix_shape.h:149
Eigen::Index m_rows
Definition: matrix_shape.h:234
Eigen::Index rows() const
number of rows.
Definition: matrix_shape.h:170
bool operator==(const SquareMatrixShape &other) const
equality comparators.
Definition: matrix_shape.h:193
Eigen::MatrixX< FP > Matrix
Definition: matrix_shape.h:144
void serialize(IOContext &io) const
serialize this.
Definition: matrix_shape.h:207
A collection of classes to simplify handling of matrix shapes in meta programming.
Definition: models/abm/analyze_result.h:30
auto failure(const IOStatus &s)
Create an object that is implicitly convertible to an error IOResult<T>.
Definition: io.h:380
boost::outcome_v2::in_place_type_t< T > Tag
Type that is used for overload resolution.
Definition: io.h:407
details::ApplyResultT< F, T... > apply(IOContext &io, F f, const IOResult< T > &... rs)
Evaluate a function with zero or more unpacked IOResults as arguments.
Definition: io.h:481
auto success()
Create an object that is implicitly convertible to a succesful IOResult<void>.
Definition: io.h:359
boost::outcome_v2::unchecked< T, IOStatus > IOResult
Value-or-error type for operations that return a value but can fail.
Definition: io.h:353