Optimist  0.0.0
A C++ library for optimization
Loading...
Searching...
No Matches
Optimist.hh
Go to the documentation of this file.
1/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
2 * Copyright (c) 2025, Davide Stocco and Enrico Bertolazzi. *
3 * *
4 * The Optimist project is distributed under the BSD 2-Clause License. *
5 * *
6 * Davide Stocco Enrico Bertolazzi *
7 * University of Trento University of Trento *
8 * davide.stocco@unitn.it enrico.bertolazzi@unitn.it *
9\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
10
11#pragma once
12
13#ifndef INCLUDE_OPTIMIST_HH
14#define INCLUDE_OPTIMIST_HH
15
16// C++ standard libraries
17#include <algorithm>
18#include <cmath>
19#include <iomanip>
20#include <ios>
21#include <iostream>
22#include <limits>
23#include <map>
24#include <memory>
25#include <numeric>
26#include <string>
27#include <type_traits>
28#include <vector>
29
30// Eigen library
31#include <Eigen/Dense>
32#include <Eigen/Sparse>
33#include <Eigen/SparseLU>
34
35// Print Optimist errors
36#ifndef OPTIMIST_ERROR
37#define OPTIMIST_ERROR(MSG) \
38 { \
39 std::ostringstream os; \
40 os << MSG; \
41 throw std::runtime_error(os.str()); \
42 }
43#endif
44
45// Assert for Optimist
46#ifndef OPTIMIST_ASSERT
47#define OPTIMIST_ASSERT(COND, MSG) \
48 if (!(COND)) { \
49 OPTIMIST_ERROR(MSG); \
50 }
51#endif
52
53// Warning for Optimist
54#ifndef OPTIMIST_WARNING
55#define OPTIMIST_WARNING(MSG) \
56 { std::cout << MSG << std::endl; }
57#endif
58
59// Warning assert for Optimist
60#ifndef OPTIMIST_ASSERT_WARNING
61#define OPTIMIST_ASSERT_WARNING(COND, MSG) \
62 if (!(COND)) { \
63 OPTIMIST_WARNING(MSG); \
64 }
65#endif
66
67// Define the basic constants for Optimist
68#ifndef OPTIMIST_BASIC_CONSTANTS
69#define OPTIMIST_BASIC_CONSTANTS(Scalar) \
70 static constexpr Scalar EPSILON{ \
71 std::numeric_limits<Scalar>::epsilon()}; \
73 static constexpr Scalar INFTY{ \
74 std::numeric_limits<Scalar>::infinity()}; \
76 static constexpr Scalar QUIET_NAN{ \
77 std::numeric_limits<Scalar>::quiet_NaN()}; \
79 inline static const Scalar SQRT_EPSILON{ \
80 std::sqrt(EPSILON)}; \
81 inline static const Scalar CBRT_EPSILON{ \
82 std::cbrt(EPSILON)};
83#endif
84
85#ifndef OPTIMIST_DEFAULT_INTEGER_TYPE
86#define OPTIMIST_DEFAULT_INTEGER_TYPE int
87#endif
88
89/**
90 * \brief Namespace for the Optimist library.
91 */
92namespace Optimist {
93
101
102 /*\
103 | _____ _____ _ _
104 | |_ _| _ _ __ __|_ _| __ __ _(_) |_ ___
105 | | || | | | '_ \ / _ \| || '__/ _` | | __/ __|
106 | | || |_| | |_) | __/| || | | (_| | | |_\__ \
107 | |_| \__, | .__/ \___||_||_| \__,_|_|\__|___/
108 | |___/|_|
109 \*/
110
115 template <typename T, typename Enable = void>
116 struct TypeTrait;
117
120 * \tparam Scalar The scalar type.
121 */
122 template <typename ScalarType>
123 struct TypeTrait<
124 ScalarType,
125 std::enable_if_t<std::is_floating_point<ScalarType>::value>> {
126 using Scalar = ScalarType;
127 using Type = ScalarType;
128 static constexpr Integer Dimension{1};
129 static constexpr bool IsScalar{true};
130 static constexpr bool IsEigen{false};
131 static constexpr bool IsFixed{false};
132 static constexpr bool IsDynamic{false};
133 static constexpr bool IsSparse{false};
134 };
135
140 * \tparam Cols The number of columns.
141 */
142 template <typename ScalarType, Integer N, Integer M>
143 struct TypeTrait<Eigen::Matrix<ScalarType, N, M>,
144 std::enable_if_t<(N > 0 && M > 0)>> {
145 using Scalar = ScalarType;
146 using Type = Eigen::Matrix<ScalarType, N, M>;
147 static constexpr Integer Dimension{N * M};
148 static constexpr bool IsScalar{false};
149 static constexpr bool IsEigen{true};
150 static constexpr bool IsFixed{true};
151 static constexpr bool IsDynamic{false};
152 static constexpr bool IsSparse{false};
153 };
154
157 * \tparam Scalar The scalar type.
158 */
159 template <typename ScalarType>
160 struct TypeTrait<Eigen::Matrix<ScalarType, Eigen::Dynamic, 1>> {
161 using Scalar = ScalarType;
162 using Type = Eigen::Matrix<ScalarType, Eigen::Dynamic, 1>;
163 static constexpr Integer Dimension{Eigen::Dynamic};
164 static constexpr bool IsScalar{false};
165 static constexpr bool IsEigen{true};
166 static constexpr bool IsFixed{false};
167 static constexpr bool IsDynamic{true};
168 static constexpr bool IsSparse{false};
169 };
170
173 * \tparam Scalar The scalar type.
174 */
175 template <typename ScalarType>
176 struct TypeTrait<Eigen::Matrix<ScalarType, Eigen::Dynamic, Eigen::Dynamic>> {
177 using Scalar = ScalarType;
178 using Type = Eigen::Matrix<ScalarType, Eigen::Dynamic, Eigen::Dynamic>;
179 static constexpr Integer Dimension{Eigen::Dynamic};
180 static constexpr bool IsScalar{false};
181 static constexpr bool IsEigen{true};
182 static constexpr bool IsFixed{false};
183 static constexpr bool IsDynamic{true};
184 static constexpr bool IsSparse{false};
185 };
186
189 * \tparam Scalar The scalar type.
190 */
191 template <typename ScalarType>
192 struct TypeTrait<Eigen::SparseVector<ScalarType>> {
193 using Scalar = ScalarType;
194 using Type = Eigen::SparseVector<ScalarType>;
195 static constexpr Integer Dimension{Eigen::Dynamic};
196 static constexpr bool IsScalar{false};
197 static constexpr bool IsEigen{true};
198 static constexpr bool IsFixed{false};
199 static constexpr bool IsDynamic{false};
200 static constexpr bool IsSparse{true};
201 };
202
205 * \tparam T The sparse Eigen matrix type.
206 */
207 template <typename ScalarType, Integer Options, typename StorageIndex>
208 struct TypeTrait<Eigen::SparseMatrix<ScalarType, Options, StorageIndex>> {
209 using Scalar = ScalarType;
210 using Type = Eigen::SparseMatrix<ScalarType, Options, StorageIndex>;
211 static constexpr Integer Dimension{Eigen::Dynamic};
212 static constexpr bool IsScalar{false};
213 static constexpr bool IsEigen{true};
214 static constexpr bool IsFixed{false};
215 static constexpr bool IsDynamic{false};
216 static constexpr bool IsSparse{true};
217 };
218
223
230 template <typename T>
231 struct RetrieveType; // primary template
232
233 template <template <typename, auto...> class BaseType,
234 typename FirstType,
235 auto... Rest>
236 struct RetrieveType<BaseType<FirstType, Rest...>> {
237 using Full = BaseType<FirstType, Rest...>;
238 using First = FirstType;
239 };
240 /*\
241 | ____ _ __ __
242 | / ___|| |_ _ _ / _|/ _|
243 | \___ \| __| | | | |_| |_
244 | ___) | |_| |_| | _| _|
245 | |____/ \__|\__,_|_| |_|
246 |
247 \*/
248
250 * \brief Retrieve the Unicode character for the top-left corner of a table.
251 * \return Unicode character for the top-left corner of a table.
252 */
253 static constexpr std::string table_top_left_corner() {
254 return std::string("┌");
255 }
256
258 * \brief Retrieve the Unicode character for the top-right corner of a table.
259 * \return Unicode character for the top-right corner of a table.
260 */
261 static constexpr std::string table_top_right_corner() {
262 return std::string("┐");
263 }
264
267 * table.
268 * \return Unicode character for the bottom-left corner of a table.
269 */
270 static constexpr std::string table_bottom_left_corner() {
271 return std::string("└");
272 }
273
276 * table.
277 * \return Unicode character for the bottom-right corner of a table.
278 */
279 static constexpr std::string table_bottom_right_corner() {
280 return std::string("┘");
281 }
282
284 * \brief Retrieve the Unicode character for the left junction of a table.
285 * \return Unicode character for the left junction of a table.
286 */
287 static constexpr std::string table_left_junction() {
288 return std::string("├");
289 }
290
292 * \brief Retrieve the Unicode character for the right junction of a table.
293 * \return Unicode character for the right junction of a table.
294 */
295 static constexpr std::string table_right_junction() {
296 return std::string("┤");
297 }
298
300 * \brief Retrieve the Unicode character for the top junction of a table.
301 * \return Unicode character for the top junction of a table.
302 */
303 static constexpr std::string table_top_junction() {
304 return std::string("┬");
305 }
306
308 * \brief Retrieve the Unicode character for the bottom junction of a table.
309 * \return Unicode character for the bottom junction of a table.
310 */
311 static constexpr std::string table_bottom_junction() {
312 return std::string("┴");
313 }
314
316 * \brief Retrieve the Unicode character for the center cross of a table.
317 * \return Unicode character for the center cross of a table.
318 */
319 static constexpr std::string table_center_cross() {
320 return std::string("┼");
321 }
322
324 * \brief Retrieve the Unicode character for the horizontal line of a table.
325 * \return Unicode character for the horizontal line of a table.
326 */
327 static constexpr std::string table_horizontal_line() {
328 return std::string("─");
329 }
330
335 * \tparam N Number of horizontal lines.
336 */
337 template <Integer N>
338 static constexpr std::string table_horizontal_line() {
339 std::string line;
340 for (Integer i{0}; i < N; ++i) {
341 line += table_horizontal_line();
342 }
343 return line;
344 }
345
347 * \brief Retrieve the Unicode character for the vertical line of a table.
348 * \return Unicode character for the vertical line of a table.
349 */
350 static constexpr std::string table_vertical_line() {
351 return std::string("│");
352 }
353
354} // namespace Optimist
355
356#endif // INCLUDE_OPTIMIST_HH
#define OPTIMIST_DEFAULT_INTEGER_TYPE
Definition Optimist.hh:83
Namespace for the Optimist library.
Definition Optimist.hh:89
static constexpr std::string table_top_right_corner()
Retrieve the Unicode character for the top-right corner of a table.
Definition Optimist.hh:258
static constexpr std::string table_top_junction()
Retrieve the Unicode character for the top junction of a table.
Definition Optimist.hh:300
static constexpr std::string table_center_cross()
Retrieve the Unicode character for the center cross of a table.
Definition Optimist.hh:316
static constexpr std::string table_top_left_corner()
Retrieve the Unicode character for the top-left corner of a table.
Definition Optimist.hh:250
static constexpr std::string table_vertical_line()
Retrieve the Unicode character for the vertical line of a table.
Definition Optimist.hh:347
static constexpr std::string table_bottom_junction()
Retrieve the Unicode character for the bottom junction of a table.
Definition Optimist.hh:308
OPTIMIST_DEFAULT_INTEGER_TYPE Integer
The Integer type as used for the API.
Definition Optimist.hh:97
static constexpr std::string table_left_junction()
Retrieve the Unicode character for the left junction of a table.
Definition Optimist.hh:284
static constexpr std::string table_bottom_right_corner()
Retrieve the Unicode character for the bottom-right corner of a table.
Definition Optimist.hh:276
static constexpr std::string table_right_junction()
Retrieve the Unicode character for the right junction of a table.
Definition Optimist.hh:292
static constexpr std::string table_bottom_left_corner()
Retrieve the Unicode character for the bottom-left corner of a table.
Definition Optimist.hh:267
static constexpr std::string table_horizontal_line()
Retrieve the Unicode character for the horizontal line of a table.
Definition Optimist.hh:324
BaseType< FirstType, Rest... > Full
Definition Optimist.hh:234
Definition Optimist.hh:228
static constexpr bool IsScalar
Definition Optimist.hh:161
Eigen::Matrix< ScalarType, Eigen::Dynamic, 1 > Type
Definition Optimist.hh:159
static constexpr bool IsEigen
Definition Optimist.hh:162
static constexpr Integer Dimension
Definition Optimist.hh:160
Eigen::Matrix< ScalarType, Eigen::Dynamic, Eigen::Dynamic > Type
Definition Optimist.hh:175
Eigen::SparseMatrix< ScalarType, Options, StorageIndex > Type
Definition Optimist.hh:207
static constexpr Integer Dimension
Definition Optimist.hh:208
Eigen::SparseVector< ScalarType > Type
Definition Optimist.hh:191
ScalarType Scalar
Definition Optimist.hh:190
static constexpr Integer Dimension
Definition Optimist.hh:192
static constexpr bool IsEigen
Definition Optimist.hh:194
static constexpr bool IsScalar
Definition Optimist.hh:193
Definition Optimist.hh:113