Skip to content

Commit

Permalink
[symbolic] Opt-in to clang-format-lint (#20158)
Browse files Browse the repository at this point in the history
  • Loading branch information
jwnimmer-tri authored Sep 9, 2023
1 parent 1621deb commit b678462
Show file tree
Hide file tree
Showing 18 changed files with 256 additions and 189 deletions.
2 changes: 1 addition & 1 deletion common/symbolic/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -324,4 +324,4 @@ drake_cc_googletest(
],
)

add_lint_tests(enable_clang_format_lint = False)
add_lint_tests()
5 changes: 2 additions & 3 deletions common/symbolic/decompose.cc
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,8 @@ bool IsAffine(const Eigen::Ref<const MatrixX<Expression>>& m) {
namespace {
void ThrowError(const string& type, const string& expression,
const string& additional_msg) {
throw runtime_error("While decomposing an expression, we detected a " +
type + " expression: " + expression + additional_msg +
".");
throw runtime_error("While decomposing an expression, we detected a " + type +
" expression: " + expression + additional_msg + ".");
}

// A helper function to implement DecomposeLinearExpressions and
Expand Down
133 changes: 66 additions & 67 deletions common/symbolic/generic_polynomial.h
Original file line number Diff line number Diff line change
Expand Up @@ -284,190 +284,190 @@ class GenericPolynomial {
* https://github.com/robotlocomotion/drake/pull/14053#pullrequestreview-488744679
*/
template <typename BasisElement>
using GenericPolynomialEnable = std::enable_if_t<
std::is_base_of_v<PolynomialBasisElement, BasisElement>,
GenericPolynomial<BasisElement>>;
using GenericPolynomialEnable =
std::enable_if_t<std::is_base_of_v<PolynomialBasisElement, BasisElement>,
GenericPolynomial<BasisElement>>;

template <typename BasisElement>
GenericPolynomialEnable<BasisElement>
operator-(const GenericPolynomial<BasisElement>& p) {
GenericPolynomialEnable<BasisElement> operator-(
const GenericPolynomial<BasisElement>& p) {
return -1. * p;
}

template <typename BasisElement>
GenericPolynomialEnable<BasisElement>
operator+(GenericPolynomial<BasisElement> p1,
const GenericPolynomial<BasisElement>& p2) {
GenericPolynomialEnable<BasisElement> operator+(
GenericPolynomial<BasisElement> p1,
const GenericPolynomial<BasisElement>& p2) {
return p1 += p2;
}

template <typename BasisElement>
GenericPolynomialEnable<BasisElement>
operator+(GenericPolynomial<BasisElement> p, const BasisElement& m) {
GenericPolynomialEnable<BasisElement> operator+(
GenericPolynomial<BasisElement> p, const BasisElement& m) {
return p += m;
}

template <typename BasisElement>
GenericPolynomialEnable<BasisElement>
operator+(GenericPolynomial<BasisElement> p, double c) {
GenericPolynomialEnable<BasisElement> operator+(
GenericPolynomial<BasisElement> p, double c) {
return p += c;
}

template <typename BasisElement>
GenericPolynomialEnable<BasisElement>
operator+(const BasisElement& m, GenericPolynomial<BasisElement> p) {
GenericPolynomialEnable<BasisElement> operator+(
const BasisElement& m, GenericPolynomial<BasisElement> p) {
return p += m;
}

template <typename BasisElement>
GenericPolynomialEnable<BasisElement>
operator+(const BasisElement& m1, const BasisElement& m2) {
GenericPolynomialEnable<BasisElement> operator+(const BasisElement& m1,
const BasisElement& m2) {
return GenericPolynomial<BasisElement>(m1) + m2;
}

template <typename BasisElement>
GenericPolynomialEnable<BasisElement>
operator+(const BasisElement& m, double c) {
GenericPolynomialEnable<BasisElement> operator+(const BasisElement& m,
double c) {
return GenericPolynomial<BasisElement>(m) + c;
}

template <typename BasisElement>
GenericPolynomialEnable<BasisElement>
operator+(double c, GenericPolynomial<BasisElement> p) {
GenericPolynomialEnable<BasisElement> operator+(
double c, GenericPolynomial<BasisElement> p) {
return p += c;
}

template <typename BasisElement>
GenericPolynomialEnable<BasisElement>
operator+(double c, const BasisElement& m) {
GenericPolynomialEnable<BasisElement> operator+(double c,
const BasisElement& m) {
return GenericPolynomial<BasisElement>(m) + c;
}

template <typename BasisElement>
GenericPolynomialEnable<BasisElement>
operator+(GenericPolynomial<BasisElement> p, const Variable& v) {
GenericPolynomialEnable<BasisElement> operator+(
GenericPolynomial<BasisElement> p, const Variable& v) {
return p += v;
}

template <typename BasisElement>
GenericPolynomialEnable<BasisElement>
operator+(const Variable& v, GenericPolynomial<BasisElement> p) {
GenericPolynomialEnable<BasisElement> operator+(
const Variable& v, GenericPolynomial<BasisElement> p) {
return p += v;
}

template <typename BasisElement>
GenericPolynomialEnable<BasisElement>
operator-(GenericPolynomial<BasisElement> p1,
const GenericPolynomial<BasisElement>& p2) {
GenericPolynomialEnable<BasisElement> operator-(
GenericPolynomial<BasisElement> p1,
const GenericPolynomial<BasisElement>& p2) {
return p1 -= p2;
}

template <typename BasisElement>
GenericPolynomialEnable<BasisElement>
operator-(GenericPolynomial<BasisElement> p, const BasisElement& m) {
GenericPolynomialEnable<BasisElement> operator-(
GenericPolynomial<BasisElement> p, const BasisElement& m) {
return p -= m;
}

template <typename BasisElement>
GenericPolynomialEnable<BasisElement>
operator-(GenericPolynomial<BasisElement> p, double c) {
GenericPolynomialEnable<BasisElement> operator-(
GenericPolynomial<BasisElement> p, double c) {
return p -= c;
}

template <typename BasisElement>
GenericPolynomialEnable<BasisElement>
operator-(const BasisElement& m, GenericPolynomial<BasisElement> p) {
GenericPolynomialEnable<BasisElement> operator-(
const BasisElement& m, GenericPolynomial<BasisElement> p) {
return p = -1 * p + m;
}

template <typename BasisElement>
GenericPolynomialEnable<BasisElement>
operator-(const BasisElement& m1, const BasisElement& m2) {
GenericPolynomialEnable<BasisElement> operator-(const BasisElement& m1,
const BasisElement& m2) {
return GenericPolynomial<BasisElement>(m1) - m2;
}

template <typename BasisElement>
GenericPolynomialEnable<BasisElement>
operator-(const BasisElement& m, double c) {
GenericPolynomialEnable<BasisElement> operator-(const BasisElement& m,
double c) {
return GenericPolynomial<BasisElement>(m) - c;
}

template <typename BasisElement>
GenericPolynomialEnable<BasisElement>
operator-(double c, GenericPolynomial<BasisElement> p) {
GenericPolynomialEnable<BasisElement> operator-(
double c, GenericPolynomial<BasisElement> p) {
return p = -p + c;
}

template <typename BasisElement>
GenericPolynomialEnable<BasisElement>
operator-(double c, const BasisElement& m) {
GenericPolynomialEnable<BasisElement> operator-(double c,
const BasisElement& m) {
return c - GenericPolynomial<BasisElement>(m);
}

template <typename BasisElement>
GenericPolynomialEnable<BasisElement>
operator-(GenericPolynomial<BasisElement> p, const Variable& v) {
GenericPolynomialEnable<BasisElement> operator-(
GenericPolynomial<BasisElement> p, const Variable& v) {
return p -= v;
}

template <typename BasisElement>
GenericPolynomialEnable<BasisElement>
operator-(const Variable& v, GenericPolynomial<BasisElement> p) {
GenericPolynomialEnable<BasisElement> operator-(
const Variable& v, GenericPolynomial<BasisElement> p) {
return GenericPolynomial<BasisElement>(v, p.indeterminates()) - p;
}

template <typename BasisElement>
GenericPolynomialEnable<BasisElement>
operator*(GenericPolynomial<BasisElement> p1,
const GenericPolynomial<BasisElement>& p2) {
GenericPolynomialEnable<BasisElement> operator*(
GenericPolynomial<BasisElement> p1,
const GenericPolynomial<BasisElement>& p2) {
return p1 *= p2;
}

template <typename BasisElement>
GenericPolynomialEnable<BasisElement>
operator*(GenericPolynomial<BasisElement> p, const BasisElement& m) {
GenericPolynomialEnable<BasisElement> operator*(
GenericPolynomial<BasisElement> p, const BasisElement& m) {
return p *= m;
}

template <typename BasisElement>
GenericPolynomialEnable<BasisElement>
operator*(GenericPolynomial<BasisElement> p, double c) {
GenericPolynomialEnable<BasisElement> operator*(
GenericPolynomial<BasisElement> p, double c) {
return p *= c;
}

template <typename BasisElement>
GenericPolynomialEnable<BasisElement>
operator*(const BasisElement& m, GenericPolynomial<BasisElement> p) {
GenericPolynomialEnable<BasisElement> operator*(
const BasisElement& m, GenericPolynomial<BasisElement> p) {
return p *= m;
}

template <typename BasisElement>
GenericPolynomialEnable<BasisElement>
operator*(const BasisElement& m, double c) {
GenericPolynomialEnable<BasisElement> operator*(const BasisElement& m,
double c) {
return GenericPolynomial<BasisElement>(m) * c;
}

template <typename BasisElement>
GenericPolynomialEnable<BasisElement>
operator*(double c, GenericPolynomial<BasisElement> p) {
GenericPolynomialEnable<BasisElement> operator*(
double c, GenericPolynomial<BasisElement> p) {
return p *= c;
}

template <typename BasisElement>
GenericPolynomialEnable<BasisElement>
operator*(double c, const BasisElement& m) {
GenericPolynomialEnable<BasisElement> operator*(double c,
const BasisElement& m) {
return GenericPolynomial<BasisElement>(m) * c;
}

template <typename BasisElement>
GenericPolynomialEnable<BasisElement>
operator*(GenericPolynomial<BasisElement> p, const Variable& v) {
GenericPolynomialEnable<BasisElement> operator*(
GenericPolynomial<BasisElement> p, const Variable& v) {
return p *= v;
}

template <typename BasisElement>
GenericPolynomialEnable<BasisElement>
operator*(const Variable& v, GenericPolynomial<BasisElement> p) {
GenericPolynomialEnable<BasisElement> operator*(
const Variable& v, GenericPolynomial<BasisElement> p) {
return p *= v;
}

Expand Down Expand Up @@ -560,7 +560,6 @@ struct NumTraits<
} // namespace Eigen
#endif // !defined(DRAKE_DOXYGEN_CXX)


// TODO(jwnimmer-tri) Add a real formatter and deprecate the operator<<.
namespace fmt {
template <typename BasisElement>
Expand Down
40 changes: 20 additions & 20 deletions common/symbolic/latex.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ namespace {
// If `value` is an integer multiple of `famous_constant_value`, returns the
// latex for the multiplied constant `{int_coeff}{famous_constant_latex}`.
std::optional<string> multiple_of_famous_constant(
double value, double famous_constant_value,
string famous_constant_latex) {
double value, double famous_constant_value, string famous_constant_latex) {
const double epsilon = 1e-14;
if (std::abs(value) < epsilon) { // Handle zero.
return std::nullopt;
Expand Down Expand Up @@ -115,7 +114,9 @@ class LatexVisitor {
print_space = true;
}
for (const auto& [e_1, e_2] : base_to_exponent_map) {
if (print_space) { oss << " "; }
if (print_space) {
oss << " ";
}
if (is_one(e_2)) {
oss << Latex(e_1);
} else {
Expand Down Expand Up @@ -254,38 +255,32 @@ class LatexVisitor {
}
[[nodiscard]] std::string VisitEqualTo(const Formula& f,
const bool polarity) const {
return Latex(get_lhs_expression(f)) +
(polarity ? " = " : " \\neq ") +
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 " : " = ") +
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 ") +
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 " : " < ") +
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 ") +
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 " : " > ") +
return Latex(get_lhs_expression(f)) + (polarity ? " \\le " : " > ") +
Latex(get_rhs_expression(f));
}
[[nodiscard]] std::string VisitConjunction(const Formula& f,
Expand Down Expand Up @@ -326,7 +321,9 @@ class LatexVisitor {
// ∃v₁...vₙ. (¬f). However, we do not have a representation
// FormulaExists(∃) yet. Revisit this when we add FormulaExists.
ostringstream oss;
if (!polarity) { oss << "\\neg "; }
if (!polarity) {
oss << "\\neg ";
}
oss << "\\forall " << VisitVariables(get_quantified_variables(f)) << ": "
<< get_quantified_formula(f);
return oss.str();
Expand All @@ -335,7 +332,9 @@ class LatexVisitor {
[[nodiscard]] std::string VisitIsnan(const Formula& f,
const bool polarity) const {
ostringstream oss;
if (!polarity) { oss << "\\neg "; }
if (!polarity) {
oss << "\\neg ";
}
oss << "\\text{isnan}(" << Latex(get_unary_expression(f)) << ")";
return oss.str();
}
Expand All @@ -353,7 +352,9 @@ class LatexVisitor {
ostringstream oss;
bool delimiter = false;
for (const auto& v : vars) {
if (delimiter) { oss << ", "; }
if (delimiter) {
oss << ", ";
}
oss << VisitVariable(v);
delimiter = true;
}
Expand Down Expand Up @@ -395,8 +396,7 @@ string ToLatex(double val, int precision) {
multiple_of_famous_constant(val, M_PI, "\\pi")) {
return *result;
}
if (optional<string> result =
multiple_of_famous_constant(val, M_E, "e")) {
if (optional<string> result = multiple_of_famous_constant(val, M_E, "e")) {
return *result;
}
double intpart;
Expand Down
Loading

0 comments on commit b678462

Please sign in to comment.