alpaqa 1.0.0a11
Nonconvex constrained optimization
Loading...
Searching...
No Matches
check-dim.hpp
Go to the documentation of this file.
1#pragma once
2
4#include <optional>
5#include <stdexcept>
6#include <string>
7
8namespace alpaqa::util {
9
10template <Config Conf>
11void check_dim_msg(crvec<Conf> v, auto sz, std::string msg) {
12 if (v.size() != sz) {
13 msg += "\n(should be ";
14 msg += std::to_string(sz);
15 msg += ", got ";
16 msg += std::to_string(v.size());
17 msg += ")";
18 throw std::invalid_argument(msg);
19 }
20}
21
22template <Config Conf>
23void check_dim_msg(std::optional<vec<Conf>> &v, auto sz, std::string msg) {
24 if (!v) {
25 v = vec<Conf>::Zero(sz);
26 } else if (v->size() != sz) {
27 msg += "\n(should be ";
28 msg += std::to_string(sz);
29 msg += ", got ";
30 msg += std::to_string(v->size());
31 msg += ")";
32 throw std::invalid_argument(msg);
33 }
34}
35
36template <Config Conf, class V>
37void check_dim(std::string name, V &&v, auto sz) {
38 name += ": dimension mismatch";
39 check_dim_msg<Conf>(std::forward<V>(v), sz, name);
40}
41
42template <Config Conf>
43void check_dim_msg(crmat<Conf> m, auto rows, auto cols, std::string msg) {
44 if (m.cols() != cols || m.rows() != rows) {
45 msg += "\n(should be ";
46 msg += std::to_string(rows);
47 msg += "×";
48 msg += std::to_string(cols);
49 msg += ", got ";
50 msg += std::to_string(m.rows());
51 msg += "×";
52 msg += std::to_string(m.cols());
53 msg += ")";
54 throw std::invalid_argument(msg);
55 }
56}
57
58template <Config Conf>
59void check_dim(std::string name, crmat<Conf> m, auto rows, auto cols) {
60 name += ": dimension mismatch";
61 check_dim_msg<Conf>(m, rows, cols, name);
62}
63
64} // namespace alpaqa::util
void check_dim(std::string name, V &&v, auto sz)
Definition: check-dim.hpp:37
void check_dim_msg(crvec< Conf > v, auto sz, std::string msg)
Definition: check-dim.hpp:11
typename Conf::crmat crmat
Definition: config.hpp:73
typename Conf::crvec crvec
Definition: config.hpp:68
typename Conf::vec vec
Definition: config.hpp:64