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 <string_view>
28#include <type_traits>
29#include <vector>
30
31// Eigen library
32#include <Eigen/Dense>
33#include <Eigen/Sparse>
34#include <Eigen/SparseLU>
35
36// Print Optimist errors
37#ifndef OPTIMIST_ERROR
38#define OPTIMIST_ERROR(MSG) \
39 { \
40 std::ostringstream os; \
41 os << MSG; \
42 throw std::runtime_error(os.str()); \
43 }
44#endif
45
46// Assert for Optimist
47#ifndef OPTIMIST_ASSERT
48#define OPTIMIST_ASSERT(COND, MSG) \
49 if (!(COND)) { \
50 OPTIMIST_ERROR(MSG); \
51 }
52#endif
53
54// Warning for Optimist
55#ifndef OPTIMIST_WARNING
56#define OPTIMIST_WARNING(MSG) \
57 { std::cout << MSG << std::endl; }
58#endif
59
60// Warning assert for Optimist
61#ifndef OPTIMIST_ASSERT_WARNING
62#define OPTIMIST_ASSERT_WARNING(COND, MSG) \
63 if (!(COND)) { \
64 OPTIMIST_WARNING(MSG); \
65 }
66#endif
67
68// Define the basic constants for Optimist
69#ifndef OPTIMIST_BASIC_CONSTANTS
70#define OPTIMIST_BASIC_CONSTANTS(Scalar) \
71 static constexpr Scalar EPSILON{ \
72 std::numeric_limits<Scalar>::epsilon()}; \
74 static constexpr Scalar INFTY{ \
75 std::numeric_limits<Scalar>::infinity()}; \
77 static constexpr Scalar QUIET_NAN{ \
78 std::numeric_limits<Scalar>::quiet_NaN()}; \
80 inline static const Scalar SQRT_EPSILON{ \
81 std::sqrt(EPSILON)}; \
82 inline static const Scalar CBRT_EPSILON{ \
83 std::cbrt(EPSILON)};
84#endif
85
86#ifndef OPTIMIST_DEFAULT_INTEGER_TYPE
87#define OPTIMIST_DEFAULT_INTEGER_TYPE int
88#endif
89
90/**
91 * \brief Namespace for the Optimist library.
92 */
93namespace Optimist {
94
102
103 /*\
104 | _____ _____ _ _
105 | |_ _| _ _ __ __|_ _| __ __ _(_) |_ ___
106 | | || | | | '_ \ / _ \| || '__/ _` | | __/ __|
107 | | || |_| | |_) | __/| || | | (_| | | |_\__ \
108 | |_| \__, | .__/ \___||_||_| \__,_|_|\__|___/
109 | |___/|_|
110 \*/
111
116 template <typename T, typename Enable = void>
117 struct TypeTrait;
118
121 * \tparam Scalar The scalar type.
122 */
123 template <typename ScalarType>
124 struct TypeTrait<
125 ScalarType,
126 std::enable_if_t<std::is_floating_point<ScalarType>::value>> {
127 using Scalar = ScalarType;
128 using Type = ScalarType;
129 static constexpr Integer Dimension{1};
130 static constexpr bool IsScalar{true};
131 static constexpr bool IsEigen{false};
132 static constexpr bool IsFixed{false};
133 static constexpr bool IsDynamic{false};
134 static constexpr bool IsSparse{false};
135 };
136
141 * \tparam Cols The number of columns.
142 */
143 template <typename ScalarType, Integer N, Integer M>
144 struct TypeTrait<Eigen::Matrix<ScalarType, N, M>,
145 std::enable_if_t<(N > 0 && M > 0)>> {
146 using Scalar = ScalarType;
147 using Type = Eigen::Matrix<ScalarType, N, M>;
148 static constexpr Integer Dimension{N * M};
149 static constexpr bool IsScalar{false};
150 static constexpr bool IsEigen{true};
151 static constexpr bool IsFixed{true};
152 static constexpr bool IsDynamic{false};
153 static constexpr bool IsSparse{false};
154 };
155
158 * \tparam Scalar The scalar type.
159 */
160 template <typename ScalarType>
161 struct TypeTrait<Eigen::Matrix<ScalarType, Eigen::Dynamic, 1>> {
162 using Scalar = ScalarType;
163 using Type = Eigen::Matrix<ScalarType, Eigen::Dynamic, 1>;
164 static constexpr Integer Dimension{Eigen::Dynamic};
165 static constexpr bool IsScalar{false};
166 static constexpr bool IsEigen{true};
167 static constexpr bool IsFixed{false};
168 static constexpr bool IsDynamic{true};
169 static constexpr bool IsSparse{false};
170 };
171
174 * \tparam Scalar The scalar type.
175 */
176 template <typename ScalarType>
177 struct TypeTrait<Eigen::Matrix<ScalarType, Eigen::Dynamic, Eigen::Dynamic>> {
178 using Scalar = ScalarType;
179 using Type = Eigen::Matrix<ScalarType, Eigen::Dynamic, Eigen::Dynamic>;
180 static constexpr Integer Dimension{Eigen::Dynamic};
181 static constexpr bool IsScalar{false};
182 static constexpr bool IsEigen{true};
183 static constexpr bool IsFixed{false};
184 static constexpr bool IsDynamic{true};
185 static constexpr bool IsSparse{false};
186 };
187
190 * \tparam Scalar The scalar type.
191 */
192 template <typename ScalarType>
193 struct TypeTrait<Eigen::SparseVector<ScalarType>> {
194 using Scalar = ScalarType;
195 using Type = Eigen::SparseVector<ScalarType>;
196 static constexpr Integer Dimension{Eigen::Dynamic};
197 static constexpr bool IsScalar{false};
198 static constexpr bool IsEigen{true};
199 static constexpr bool IsFixed{false};
200 static constexpr bool IsDynamic{false};
201 static constexpr bool IsSparse{true};
202 };
203
206 * \tparam T The sparse Eigen matrix type.
207 */
208 template <typename ScalarType, Integer Options, typename StorageIndex>
209 struct TypeTrait<Eigen::SparseMatrix<ScalarType, Options, StorageIndex>> {
210 using Scalar = ScalarType;
211 using Type = Eigen::SparseMatrix<ScalarType, Options, StorageIndex>;
212 static constexpr Integer Dimension{Eigen::Dynamic};
213 static constexpr bool IsScalar{false};
214 static constexpr bool IsEigen{true};
215 static constexpr bool IsFixed{false};
216 static constexpr bool IsDynamic{false};
217 static constexpr bool IsSparse{true};
218 };
219
224
231 template <typename T>
232 struct RetrieveType; // primary template
233
234 template <template <typename, auto...> class BaseType,
235 typename FirstType,
236 auto... Rest>
237 struct RetrieveType<BaseType<FirstType, Rest...>> {
238 using Full = BaseType<FirstType, Rest...>;
239 using First = FirstType;
240 };
241 /*\
242 | ____ _ __ __
243 | / ___|| |_ _ _ / _|/ _|
244 | \___ \| __| | | | |_| |_
245 | ___) | |_| |_| | _| _|
246 | |____/ \__|\__,_|_| |_|
247 |
248 \*/
249
251 * \brief Retrieve the Unicode character for the top-left corner of a table.
252 * \return Unicode character for the top-left corner of a table.
253 */
254 static constexpr std::string_view table_top_left_corner() {
255 return "┌";
256 }
257
259 * \brief Retrieve the Unicode character for the top-right corner of a table.
260 * \return Unicode character for the top-right corner of a table.
261 */
262 static constexpr std::string_view table_top_right_corner() {
263 return "┐";
264 }
265
268 * table.
269 * \return Unicode character for the bottom-left corner of a table.
270 */
271 static constexpr std::string_view table_bottom_left_corner() {
272 return "└";
273 }
274
277 * table.
278 * \return Unicode character for the bottom-right corner of a table.
279 */
280 static constexpr std::string_view table_bottom_right_corner() {
281 return "┘";
282 }
283
285 * \brief Retrieve the Unicode character for the left junction of a table.
286 * \return Unicode character for the left junction of a table.
287 */
288 static constexpr std::string_view table_left_junction() {
289 return "├";
290 }
291
293 * \brief Retrieve the Unicode character for the right junction of a table.
294 * \return Unicode character for the right junction of a table.
295 */
296 static constexpr std::string_view table_right_junction() {
297 return "┤";
298 }
299
301 * \brief Retrieve the Unicode character for the top junction of a table.
302 * \return Unicode character for the top junction of a table.
303 */
304 static constexpr std::string_view table_top_junction() {
305 return "┬";
306 }
307
309 * \brief Retrieve the Unicode character for the bottom junction of a table.
310 * \return Unicode character for the bottom junction of a table.
311 */
312 static constexpr std::string_view table_bottom_junction() {
313 return "┴";
314 }
315
317 * \brief Retrieve the Unicode character for the center cross of a table.
318 * \return Unicode character for the center cross of a table.
319 */
320 static constexpr std::string_view table_center_cross() {
321 return "┼";
322 }
323
325 * \brief Retrieve the Unicode character for the horizontal line of a table.
326 * \return Unicode character for the horizontal line of a table.
327 */
328 static constexpr std::string_view table_horizontal_line() {
329 return "─";
330 }
331
336 * \tparam N Number of horizontal lines.
337 */
338 template <Integer N>
339 static std::string table_horizontal_line() {
340 std::string line;
341 for (Integer i{0}; i < N; ++i) {
342 line += table_horizontal_line();
343 }
344 return line;
345 }
346
348 * \brief Retrieve the Unicode character for the vertical line of a table.
349 * \return Unicode character for the vertical line of a table.
350 */
351 static constexpr std::string_view table_vertical_line() {
352 return "│";
353 }
354
355} // namespace Optimist
356
357#endif // INCLUDE_OPTIMIST_HH
#define OPTIMIST_DEFAULT_INTEGER_TYPE
Definition Optimist.hh:84
Namespace for the Optimist library.
Definition Optimist.hh:90
static constexpr std::string_view table_horizontal_line()
Retrieve the Unicode character for the horizontal line of a table.
Definition Optimist.hh:325
static constexpr std::string_view table_top_left_corner()
Retrieve the Unicode character for the top-left corner of a table.
Definition Optimist.hh:251
static constexpr std::string_view table_top_junction()
Retrieve the Unicode character for the top junction of a table.
Definition Optimist.hh:301
OPTIMIST_DEFAULT_INTEGER_TYPE Integer
The Integer type as used for the API.
Definition Optimist.hh:98
static constexpr std::string_view table_center_cross()
Retrieve the Unicode character for the center cross of a table.
Definition Optimist.hh:317
static constexpr std::string_view table_bottom_right_corner()
Retrieve the Unicode character for the bottom-right corner of a table.
Definition Optimist.hh:277
static constexpr std::string_view table_right_junction()
Retrieve the Unicode character for the right junction of a table.
Definition Optimist.hh:293
static constexpr std::string_view table_bottom_left_corner()
Retrieve the Unicode character for the bottom-left corner of a table.
Definition Optimist.hh:268
static constexpr std::string_view table_vertical_line()
Retrieve the Unicode character for the vertical line of a table.
Definition Optimist.hh:348
static constexpr std::string_view table_bottom_junction()
Retrieve the Unicode character for the bottom junction of a table.
Definition Optimist.hh:309
static constexpr std::string_view table_top_right_corner()
Retrieve the Unicode character for the top-right corner of a table.
Definition Optimist.hh:259
static constexpr std::string_view table_left_junction()
Retrieve the Unicode character for the left junction of a table.
Definition Optimist.hh:285
BaseType< FirstType, Rest... > Full
Definition Optimist.hh:235
Definition Optimist.hh:229
static constexpr bool IsScalar
Definition Optimist.hh:162
Eigen::Matrix< ScalarType, Eigen::Dynamic, 1 > Type
Definition Optimist.hh:160
static constexpr bool IsEigen
Definition Optimist.hh:163
static constexpr Integer Dimension
Definition Optimist.hh:161
Eigen::Matrix< ScalarType, Eigen::Dynamic, Eigen::Dynamic > Type
Definition Optimist.hh:176
Eigen::SparseMatrix< ScalarType, Options, StorageIndex > Type
Definition Optimist.hh:208
static constexpr Integer Dimension
Definition Optimist.hh:209
Eigen::SparseVector< ScalarType > Type
Definition Optimist.hh:192
ScalarType Scalar
Definition Optimist.hh:191
static constexpr Integer Dimension
Definition Optimist.hh:193
static constexpr bool IsEigen
Definition Optimist.hh:195
static constexpr bool IsScalar
Definition Optimist.hh:194
Definition Optimist.hh:114