forked from RobotLocomotion/drake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlatex.cc
376 lines (327 loc) · 12 KB
/
latex.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
#include "drake/common/symbolic/latex.h"
#include <sstream>
#include <stdexcept>
namespace drake {
namespace symbolic {
using std::ostringstream;
using std::runtime_error;
using std::string;
using std::to_string;
namespace {
// Visitor class for code generation.
class LatexVisitor {
public:
DRAKE_NO_COPY_NO_MOVE_NO_ASSIGN(LatexVisitor)
explicit LatexVisitor(int precision) : precision_{precision} {};
// Generates latex expression for the expression @p e.
[[nodiscard]] std::string Latex(const Expression& e) const {
return VisitExpression<string>(this, e);
}
[[nodiscard]] std::string Latex(const Formula& f) const {
return VisitFormula(f, true);
}
private:
[[nodiscard]] std::string VisitVariable(const Expression& e) const {
std::string s = get_variable(e).to_string();
// Use subscripts for variable indices.
// x(a) => x_{a}, x(a,b) => x_{a,b}, etc.
const auto start_paren = s.find_first_of('(');
const auto end_paren = s.find_last_of(')');
if (start_paren != std::string::npos && end_paren != std::string::npos &&
end_paren > start_paren) {
s.replace(end_paren, 1, "}");
s.replace(start_paren, 1, "_{");
}
return s;
}
[[nodiscard]] std::string VisitConstant(const Expression& e) const {
return ToLatex(get_constant_value(e), precision_);
}
[[nodiscard]] std::string VisitAddition(const Expression& e) const {
const double c{get_constant_in_addition(e)};
const auto& expr_to_coeff_map{get_expr_to_coeff_map_in_addition(e)};
ostringstream oss;
bool print_plus{false};
oss << "(";
if (c != 0.0) {
oss << ToLatex(c, precision_);
print_plus = true;
}
for (const auto& [term, coeff] : expr_to_coeff_map) {
if (coeff > 0.0) {
if (print_plus) {
oss << " + ";
}
// Do not print "1 * t"
if (coeff != 1.0) {
oss << ToLatex(coeff, precision_);
}
} else {
// Instead of printing "+ (- E)", just print "- E".
oss << " - ";
if (coeff != -1.0) {
oss << ToLatex((-coeff), precision_);
}
}
oss << Latex(term);
print_plus = true;
}
oss << ")";
return oss.str();
}
[[nodiscard]] std::string VisitMultiplication(const Expression& e) const {
const double c{get_constant_in_multiplication(e)};
const auto& base_to_exponent_map{
get_base_to_exponent_map_in_multiplication(e)};
bool print_space = false;
ostringstream oss;
if (c != 1.0) {
oss << ToLatex(c, precision_);
print_space = true;
}
for (const auto& [e_1, e_2] : base_to_exponent_map) {
if (print_space) { oss << " "; }
if (is_one(e_2)) {
oss << Latex(e_1);
} else {
oss << Latex(e_1) << "^{" << Latex(e_2) << "}";
}
print_space = true;
}
return oss.str();
}
// Helper method to handle unary cases.
[[nodiscard]] string VisitUnary(const string& f, const Expression& e) const {
return "\\" + f + "{" + Latex(get_argument(e)) + "}";
}
// Helper method to handle binary cases.
[[nodiscard]] string VisitBinary(const string& f, const Expression& e) const {
return "\\" + f + "\\{" + Latex(get_first_argument(e)) + ", " +
Latex(get_second_argument(e)) + "\\}";
}
[[nodiscard]] string VisitPow(const Expression& e) const {
return Latex(get_first_argument(e)) + "^{" + Latex(get_second_argument(e)) +
"}";
}
[[nodiscard]] string VisitDivision(const Expression& e) const {
return "\\frac{" + Latex(get_first_argument(e)) + "}{" +
Latex(get_second_argument(e)) + "}";
}
[[nodiscard]] string VisitAbs(const Expression& e) const {
return "|" + Latex(get_argument(e)) + "|";
}
[[nodiscard]] string VisitLog(const Expression& e) const {
return VisitUnary("log", e);
}
[[nodiscard]] string VisitExp(const Expression& e) const {
return "e^{" + Latex(get_argument(e)) + "}";
}
[[nodiscard]] string VisitSqrt(const Expression& e) const {
return VisitUnary("sqrt", e);
}
[[nodiscard]] string VisitSin(const Expression& e) const {
return VisitUnary("sin", e);
}
[[nodiscard]] string VisitCos(const Expression& e) const {
return VisitUnary("cos", e);
}
[[nodiscard]] string VisitTan(const Expression& e) const {
return VisitUnary("tan", e);
}
[[nodiscard]] string VisitAsin(const Expression& e) const {
return VisitUnary("asin", e);
}
[[nodiscard]] string VisitAcos(const Expression& e) const {
return VisitUnary("acos", e);
}
[[nodiscard]] string VisitAtan(const Expression& e) const {
return VisitUnary("atan", e);
}
[[nodiscard]] string VisitAtan2(const Expression& e) const {
return "\\atan{\\frac{" + Latex(get_first_argument(e)) + "}{" +
Latex(get_second_argument(e)) + "}}";
}
[[nodiscard]] string VisitSinh(const Expression& e) const {
return VisitUnary("sinh", e);
}
[[nodiscard]] string VisitCosh(const Expression& e) const {
return VisitUnary("cosh", e);
}
[[nodiscard]] string VisitTanh(const Expression& e) const {
return VisitUnary("tanh", e);
}
[[nodiscard]] string VisitMin(const Expression& e) const {
return VisitBinary("min", e);
}
[[nodiscard]] string VisitMax(const Expression& e) const {
return VisitBinary("max", e);
}
[[nodiscard]] string VisitCeil(const Expression& e) const {
return "\\lceil " + Latex(get_argument(e)) + " \\rceil";
}
[[nodiscard]] string VisitFloor(const Expression& e) const {
return "\\lfloor " + Latex(get_argument(e)) + " \\rfloor";
}
[[nodiscard]] string VisitIfThenElse(const Expression& e) const {
std::ostringstream oss;
oss << "\\begin{cases} ";
oss << Latex(get_then_expression(e)) << " & \\text{if } ";
oss << Latex(get_conditional_formula(e)) << ", \\\\ ";
oss << Latex(get_else_expression(e)) << " & \\text{otherwise}.";
oss << "\\end{cases}";
return oss.str();
}
[[nodiscard]] string VisitUninterpretedFunction(const Expression&) const {
throw runtime_error("ToLatex does not support uninterpreted functions.");
}
// The parameter `polarity` is to indicate whether it processes `f` (if
// `polarity` is true) or `¬f` (if `polarity` is false).
[[nodiscard]] std::string VisitFormula(const Formula& f,
const bool polarity = true) const {
return symbolic::VisitFormula<std::string>(this, f, polarity);
}
[[nodiscard]] std::string VisitFalse(const Formula&,
const bool polarity) const {
return polarity ? "\\text{false}" : "\\text{true}";
}
[[nodiscard]] std::string VisitTrue(const Formula&,
const bool polarity) const {
return polarity ? "\\text{true}" : "\\text{false}";
}
[[nodiscard]] std::string VisitVariable(const Formula& f,
const bool polarity) const {
return (polarity ? "" : "\\neg") + get_variable(f).to_string();
}
[[nodiscard]] std::string VisitEqualTo(const Formula& f,
const bool polarity) const {
return Latex(get_lhs_expression(f)) +
(polarity ? " = " : " \\neq ") +
Latex(get_rhs_expression(f));
}
[[nodiscard]] std::string VisitNotEqualTo(const Formula& f,
const bool polarity) const {
return Latex(get_lhs_expression(f)) +
(polarity ? " \\neq " : " = ") +
Latex(get_rhs_expression(f));
}
[[nodiscard]] std::string VisitGreaterThan(const Formula& f,
const bool polarity) const {
return Latex(get_lhs_expression(f)) +
(polarity ? " > " : " \\le ") +
Latex(get_rhs_expression(f));
}
[[nodiscard]] std::string VisitGreaterThanOrEqualTo(
const Formula& f, const bool polarity) const {
return Latex(get_lhs_expression(f)) +
(polarity ? " \\ge " : " < ") +
Latex(get_rhs_expression(f));
}
[[nodiscard]] std::string VisitLessThan(const Formula& f,
const bool polarity) const {
return Latex(get_lhs_expression(f)) +
(polarity ? " < " : " \\ge ") +
Latex(get_rhs_expression(f));
}
[[nodiscard]] std::string VisitLessThanOrEqualTo(const Formula& f,
const bool polarity) const {
return Latex(get_lhs_expression(f)) +
(polarity ? " \\le " : " > ") +
Latex(get_rhs_expression(f));
}
[[nodiscard]] std::string VisitConjunction(const Formula& f,
const bool polarity) const {
ostringstream oss;
bool print_symbol = false;
for (const auto& o : get_operands(f)) {
if (print_symbol) {
oss << (polarity ? " \\land " : " \\lor ");
}
oss << VisitFormula(o, polarity);
print_symbol = true;
}
return oss.str();
}
[[nodiscard]] std::string VisitDisjunction(const Formula& f,
const bool polarity) const {
ostringstream oss;
bool print_symbol = false;
for (const auto& o : get_operands(f)) {
if (print_symbol) {
oss << (polarity ? " \\lor " : " \\land ");
}
oss << VisitFormula(o, polarity);
print_symbol = true;
}
return oss.str();
}
[[nodiscard]] std::string VisitNegation(const Formula& f,
const bool polarity) const {
return VisitFormula(get_operand(f), !polarity);
}
[[nodiscard]] std::string VisitForall(const Formula& f,
const bool polarity) const {
// TODO(russt): The polarity==False case can be further reduced into
// ∃v₁...vₙ. (¬f). However, we do not have a representation
// FormulaExists(∃) yet. Revisit this when we add FormulaExists.
ostringstream oss;
if (!polarity) { oss << "\\neg "; }
oss << "\\forall " << VisitVariables(get_quantified_variables(f)) << ": "
<< get_quantified_formula(f);
return oss.str();
}
[[nodiscard]] std::string VisitIsnan(const Formula& f,
const bool polarity) const {
ostringstream oss;
if (!polarity) { oss << "\\neg "; }
oss << "\\text{isnan}(" << Latex(get_unary_expression(f)) << ")";
return oss.str();
}
[[nodiscard]] std::string VisitPositiveSemidefinite(
const Formula& f, const bool polarity) const {
DRAKE_ASSERT(polarity); // "Not PSD" could be an arbitrary matrix.
return ToLatex(get_matrix_in_positive_semidefinite(f), precision_) +
" \\succeq 0";
}
// Note: This method is not called directly from VisitExpression; but can be
// used by other methods in this class.
[[nodiscard]] std::string VisitVariables(const Variables& vars) const {
ostringstream oss;
bool delimiter = false;
for (const auto& v : vars) {
if (delimiter) { oss << ", "; }
oss << VisitVariable(v);
delimiter = true;
}
return oss.str();
}
// Makes VisitExpression a friend of this class so that it can use private
// methods.
friend std::string VisitExpression<std::string>(const LatexVisitor*,
const Expression&);
// Makes VisitFormula a friend of this class so that it can use private
// methods.
friend std::string symbolic::VisitFormula<std::string>(const LatexVisitor*,
const Formula&,
const bool&);
int precision_;
};
} // namespace
string ToLatex(const Expression& e, int precision) {
return LatexVisitor{precision}.Latex(e);
}
string ToLatex(const Formula& f, int precision) {
return LatexVisitor{precision}.Latex(f);
}
string ToLatex(double val, int precision) {
double intpart;
if (std::modf(val, &intpart) == 0.0) {
// Then it's an integer.
return std::to_string(static_cast<int>(val));
}
std::ostringstream oss;
oss.precision(precision);
oss << std::fixed << val;
return oss.str();
}
} // namespace symbolic
} // namespace drake