forked from sammy-tri/drake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
symbolic_latex.h
58 lines (49 loc) · 1.73 KB
/
symbolic_latex.h
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
#pragma once
#include <sstream>
#include <string>
#include <unordered_map>
#include <vector>
#include <Eigen/Core>
#include <Eigen/Sparse>
#include "drake/common/drake_copyable.h"
#include "drake/common/symbolic.h"
namespace drake {
namespace symbolic {
/// Generates a LaTeX string representation of `e` with floating point
/// coefficients displayed using `precision`.
/// @pydrake_mkdoc_identifier{expression}
std::string ToLatex(const Expression& e, int precision = 3);
/// Generates a LaTeX string representation of `f` with floating point
/// coefficients displayed using `precision`.
/// @pydrake_mkdoc_identifier{formula}
std::string ToLatex(const Formula& f, int precision = 3);
/// Generates a Latex string representation of `val` displayed with `precision`,
/// with one exception. If the fractional part of `val` is exactly zero, then
/// `val` is represented perfectly as an integer, and is displayed without the
/// trailing decimal point and zeros (in this case, the `precision` argument is
/// ignored).
std::string ToLatex(double val, int precision = 3);
/// Generates a LaTeX string representation of `M` with floating point
/// coefficients displayed using `precision`.
/// @pydrake_mkdoc_identifier{matrix}
template <typename Derived>
std::string ToLatex(const Eigen::PlainObjectBase<Derived>& M,
int precision = 3) {
std::ostringstream oss;
oss << "\\begin{bmatrix}";
for (int i = 0; i < M.rows(); ++i) {
for (int j = 0; j < M.cols(); ++j) {
oss << " " << ToLatex(M(i, j), precision);
if (j < M.cols() - 1) {
oss << " &";
}
}
if (i < M.rows() - 1) {
oss << " \\\\";
}
}
oss << " \\end{bmatrix}";
return oss.str();
}
} // namespace symbolic
} // namespace drake