Icarus
Vehicle Simulation as a Transformable Computational Graph, built on Vulcan and Janus
Loading...
Searching...
No Matches
ScalarFormat.hpp
Go to the documentation of this file.
1#pragma once
2
10
12
13#include <iomanip>
14#include <sstream>
15#include <string>
16
17namespace icarus::io {
18
24template <typename Scalar> struct ScalarFormatter {
25 static std::string format(const Scalar &value, int precision = 6) {
26 std::ostringstream ss;
27 ss << std::setprecision(precision) << value;
28 return ss.str();
29 }
30
31 static std::string format_fixed(const Scalar &value, int precision = 6) {
32 std::ostringstream ss;
33 ss << std::fixed << std::setprecision(precision) << value;
34 return ss.str();
35 }
36
37 static std::string format_scientific(const Scalar &value, int precision = 6) {
38 std::ostringstream ss;
39 ss << std::scientific << std::setprecision(precision) << value;
40 return ss.str();
41 }
42};
43
47template <> struct ScalarFormatter<double> {
48 static std::string format(const double &value, int precision = 6) {
49 std::ostringstream ss;
50 ss << std::setprecision(precision) << value;
51 return ss.str();
52 }
53
54 static std::string format_fixed(const double &value, int precision = 6) {
55 std::ostringstream ss;
56 ss << std::fixed << std::setprecision(precision) << value;
57 return ss.str();
58 }
59
60 static std::string format_scientific(const double &value, int precision = 6) {
61 std::ostringstream ss;
62 ss << std::scientific << std::setprecision(precision) << value;
63 return ss.str();
64 }
65
69 static constexpr bool is_numeric() { return true; }
70};
71
75template <> struct ScalarFormatter<SymbolicScalar> {
76 static std::string format(const SymbolicScalar &value, int /*precision*/ = 6) {
77 std::ostringstream ss;
78 ss << value;
79 return ss.str();
80 }
81
82 static std::string format_fixed(const SymbolicScalar &value, int /*precision*/ = 6) {
83 return format(value); // No special formatting for symbolic
84 }
85
86 static std::string format_scientific(const SymbolicScalar &value, int /*precision*/ = 6) {
87 return format(value); // No special formatting for symbolic
88 }
89
93 static constexpr bool is_numeric() { return false; }
94
98 static bool is_constant(const SymbolicScalar &value) { return value.is_constant(); }
99
103 static double get_constant(const SymbolicScalar &value) {
104 return static_cast<double>(casadi::DM(value));
105 }
106};
107
108// =============================================================================
109// Convenience Functions
110// =============================================================================
111
115template <typename Scalar> inline std::string FormatScalar(const Scalar &value, int precision = 6) {
116 return ScalarFormatter<Scalar>::format(value, precision);
117}
118
122template <typename Scalar>
123inline std::string FormatScalarFixed(const Scalar &value, int precision = 6) {
124 return ScalarFormatter<Scalar>::format_fixed(value, precision);
125}
126
130template <typename Scalar>
131inline std::string FormatScalarScientific(const Scalar &value, int precision = 6) {
132 return ScalarFormatter<Scalar>::format_scientific(value, precision);
133}
134
138template <typename Scalar> constexpr bool IsNumericScalar() {
140}
141
145template <typename Scalar> bool CanEvaluateToDouble(const Scalar &value) {
146 if constexpr (std::is_same_v<Scalar, double>) {
147 (void)value; // Unused
148 return true;
149 } else if constexpr (std::is_same_v<Scalar, SymbolicScalar>) {
150 return value.is_constant();
151 } else {
152 (void)value;
153 return false;
154 }
155}
156
160template <typename Scalar> double EvaluateToDouble(const Scalar &value, double default_val = 0.0) {
161 if constexpr (std::is_same_v<Scalar, double>) {
162 return value;
163 } else if constexpr (std::is_same_v<Scalar, SymbolicScalar>) {
164 if (value.is_constant()) {
165 return static_cast<double>(casadi::DM(value));
166 }
167 return default_val;
168 } else {
169 (void)value;
170 return default_val;
171 }
172}
173
174} // namespace icarus::io
Core type definitions, concepts, and configuration for Icarus.
Definition ScalarFormat.hpp:17
std::string FormatScalar(const Scalar &value, int precision=6)
Format a scalar value (auto-dispatch).
Definition ScalarFormat.hpp:115
std::string FormatScalarFixed(const Scalar &value, int precision=6)
Format a scalar with fixed notation.
Definition ScalarFormat.hpp:123
bool CanEvaluateToDouble(const Scalar &value)
Check if a value can be safely converted to double.
Definition ScalarFormat.hpp:145
std::string FormatScalarScientific(const Scalar &value, int precision=6)
Format a scalar with scientific notation.
Definition ScalarFormat.hpp:131
double EvaluateToDouble(const Scalar &value, double default_val=0.0)
Safely get double value if possible.
Definition ScalarFormat.hpp:160
constexpr bool IsNumericScalar()
Check if a scalar type is numeric.
Definition ScalarFormat.hpp:138
static double get_constant(const SymbolicScalar &value)
Get constant value (only valid if is_constant returns true).
Definition ScalarFormat.hpp:103
static std::string format_fixed(const SymbolicScalar &value, int=6)
Definition ScalarFormat.hpp:82
static std::string format(const SymbolicScalar &value, int=6)
Definition ScalarFormat.hpp:76
static std::string format_scientific(const SymbolicScalar &value, int=6)
Definition ScalarFormat.hpp:86
static constexpr bool is_numeric()
Check if value is numeric (always false for symbolic).
Definition ScalarFormat.hpp:93
static bool is_constant(const SymbolicScalar &value)
Check if this MX is a constant (evaluates to a single numeric value).
Definition ScalarFormat.hpp:98
static std::string format(const double &value, int precision=6)
Definition ScalarFormat.hpp:48
static std::string format_fixed(const double &value, int precision=6)
Definition ScalarFormat.hpp:54
static std::string format_scientific(const double &value, int precision=6)
Definition ScalarFormat.hpp:60
static constexpr bool is_numeric()
Check if value is numeric (always true for double).
Definition ScalarFormat.hpp:69
Format a scalar value to string.
Definition ScalarFormat.hpp:24
static std::string format(const Scalar &value, int precision=6)
Definition ScalarFormat.hpp:25
static std::string format_scientific(const Scalar &value, int precision=6)
Definition ScalarFormat.hpp:37
static std::string format_fixed(const Scalar &value, int precision=6)
Definition ScalarFormat.hpp:31