From b6784626ef893c15749db3af27acb954064a2959 Mon Sep 17 00:00:00 2001 From: Jeremy Nimmer Date: Sat, 9 Sep 2023 10:37:51 -0700 Subject: [PATCH] [symbolic] Opt-in to clang-format-lint (#20158) --- common/symbolic/BUILD.bazel | 2 +- common/symbolic/decompose.cc | 5 +- common/symbolic/generic_polynomial.h | 133 +++++++++--------- common/symbolic/latex.cc | 40 +++--- common/symbolic/monomial.cc | 8 +- common/symbolic/monomial.h | 7 +- common/symbolic/monomial_basis_element.h | 1 - common/symbolic/polynomial.cc | 100 +++++++++---- common/symbolic/polynomial.h | 17 ++- common/symbolic/rational_function.cc | 37 +++-- common/symbolic/rational_function.h | 10 +- common/symbolic/test/codegen_test.cc | 4 +- common/symbolic/test/decompose_test.cc | 18 +-- .../test/polynomial_basis_element_test.cc | 3 +- .../symbolic/test/rational_function_test.cc | 6 +- common/symbolic/test/simplification_test.cc | 44 +++--- common/symbolic/trigonometric_polynomial.cc | 5 +- common/symbolic/trigonometric_polynomial.h | 5 +- 18 files changed, 256 insertions(+), 189 deletions(-) diff --git a/common/symbolic/BUILD.bazel b/common/symbolic/BUILD.bazel index 34e659576a0c..8a66ae7ed4ca 100644 --- a/common/symbolic/BUILD.bazel +++ b/common/symbolic/BUILD.bazel @@ -324,4 +324,4 @@ drake_cc_googletest( ], ) -add_lint_tests(enable_clang_format_lint = False) +add_lint_tests() diff --git a/common/symbolic/decompose.cc b/common/symbolic/decompose.cc index 56c0370ef426..6ddaf163f28e 100644 --- a/common/symbolic/decompose.cc +++ b/common/symbolic/decompose.cc @@ -84,9 +84,8 @@ bool IsAffine(const Eigen::Ref>& 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 diff --git a/common/symbolic/generic_polynomial.h b/common/symbolic/generic_polynomial.h index 99c19327698f..991f7fe07ea8 100644 --- a/common/symbolic/generic_polynomial.h +++ b/common/symbolic/generic_polynomial.h @@ -284,190 +284,190 @@ class GenericPolynomial { * https://github.com/robotlocomotion/drake/pull/14053#pullrequestreview-488744679 */ template -using GenericPolynomialEnable = std::enable_if_t< - std::is_base_of_v, - GenericPolynomial>; +using GenericPolynomialEnable = + std::enable_if_t, + GenericPolynomial>; template -GenericPolynomialEnable -operator-(const GenericPolynomial& p) { +GenericPolynomialEnable operator-( + const GenericPolynomial& p) { return -1. * p; } template -GenericPolynomialEnable -operator+(GenericPolynomial p1, - const GenericPolynomial& p2) { +GenericPolynomialEnable operator+( + GenericPolynomial p1, + const GenericPolynomial& p2) { return p1 += p2; } template -GenericPolynomialEnable -operator+(GenericPolynomial p, const BasisElement& m) { +GenericPolynomialEnable operator+( + GenericPolynomial p, const BasisElement& m) { return p += m; } template -GenericPolynomialEnable -operator+(GenericPolynomial p, double c) { +GenericPolynomialEnable operator+( + GenericPolynomial p, double c) { return p += c; } template -GenericPolynomialEnable -operator+(const BasisElement& m, GenericPolynomial p) { +GenericPolynomialEnable operator+( + const BasisElement& m, GenericPolynomial p) { return p += m; } template -GenericPolynomialEnable -operator+(const BasisElement& m1, const BasisElement& m2) { +GenericPolynomialEnable operator+(const BasisElement& m1, + const BasisElement& m2) { return GenericPolynomial(m1) + m2; } template -GenericPolynomialEnable -operator+(const BasisElement& m, double c) { +GenericPolynomialEnable operator+(const BasisElement& m, + double c) { return GenericPolynomial(m) + c; } template -GenericPolynomialEnable -operator+(double c, GenericPolynomial p) { +GenericPolynomialEnable operator+( + double c, GenericPolynomial p) { return p += c; } template -GenericPolynomialEnable -operator+(double c, const BasisElement& m) { +GenericPolynomialEnable operator+(double c, + const BasisElement& m) { return GenericPolynomial(m) + c; } template -GenericPolynomialEnable -operator+(GenericPolynomial p, const Variable& v) { +GenericPolynomialEnable operator+( + GenericPolynomial p, const Variable& v) { return p += v; } template -GenericPolynomialEnable -operator+(const Variable& v, GenericPolynomial p) { +GenericPolynomialEnable operator+( + const Variable& v, GenericPolynomial p) { return p += v; } template -GenericPolynomialEnable -operator-(GenericPolynomial p1, - const GenericPolynomial& p2) { +GenericPolynomialEnable operator-( + GenericPolynomial p1, + const GenericPolynomial& p2) { return p1 -= p2; } template -GenericPolynomialEnable -operator-(GenericPolynomial p, const BasisElement& m) { +GenericPolynomialEnable operator-( + GenericPolynomial p, const BasisElement& m) { return p -= m; } template -GenericPolynomialEnable -operator-(GenericPolynomial p, double c) { +GenericPolynomialEnable operator-( + GenericPolynomial p, double c) { return p -= c; } template -GenericPolynomialEnable -operator-(const BasisElement& m, GenericPolynomial p) { +GenericPolynomialEnable operator-( + const BasisElement& m, GenericPolynomial p) { return p = -1 * p + m; } template -GenericPolynomialEnable -operator-(const BasisElement& m1, const BasisElement& m2) { +GenericPolynomialEnable operator-(const BasisElement& m1, + const BasisElement& m2) { return GenericPolynomial(m1) - m2; } template -GenericPolynomialEnable -operator-(const BasisElement& m, double c) { +GenericPolynomialEnable operator-(const BasisElement& m, + double c) { return GenericPolynomial(m) - c; } template -GenericPolynomialEnable -operator-(double c, GenericPolynomial p) { +GenericPolynomialEnable operator-( + double c, GenericPolynomial p) { return p = -p + c; } template -GenericPolynomialEnable -operator-(double c, const BasisElement& m) { +GenericPolynomialEnable operator-(double c, + const BasisElement& m) { return c - GenericPolynomial(m); } template -GenericPolynomialEnable -operator-(GenericPolynomial p, const Variable& v) { +GenericPolynomialEnable operator-( + GenericPolynomial p, const Variable& v) { return p -= v; } template -GenericPolynomialEnable -operator-(const Variable& v, GenericPolynomial p) { +GenericPolynomialEnable operator-( + const Variable& v, GenericPolynomial p) { return GenericPolynomial(v, p.indeterminates()) - p; } template -GenericPolynomialEnable -operator*(GenericPolynomial p1, - const GenericPolynomial& p2) { +GenericPolynomialEnable operator*( + GenericPolynomial p1, + const GenericPolynomial& p2) { return p1 *= p2; } template -GenericPolynomialEnable -operator*(GenericPolynomial p, const BasisElement& m) { +GenericPolynomialEnable operator*( + GenericPolynomial p, const BasisElement& m) { return p *= m; } template -GenericPolynomialEnable -operator*(GenericPolynomial p, double c) { +GenericPolynomialEnable operator*( + GenericPolynomial p, double c) { return p *= c; } template -GenericPolynomialEnable -operator*(const BasisElement& m, GenericPolynomial p) { +GenericPolynomialEnable operator*( + const BasisElement& m, GenericPolynomial p) { return p *= m; } template -GenericPolynomialEnable -operator*(const BasisElement& m, double c) { +GenericPolynomialEnable operator*(const BasisElement& m, + double c) { return GenericPolynomial(m) * c; } template -GenericPolynomialEnable -operator*(double c, GenericPolynomial p) { +GenericPolynomialEnable operator*( + double c, GenericPolynomial p) { return p *= c; } template -GenericPolynomialEnable -operator*(double c, const BasisElement& m) { +GenericPolynomialEnable operator*(double c, + const BasisElement& m) { return GenericPolynomial(m) * c; } template -GenericPolynomialEnable -operator*(GenericPolynomial p, const Variable& v) { +GenericPolynomialEnable operator*( + GenericPolynomial p, const Variable& v) { return p *= v; } template -GenericPolynomialEnable -operator*(const Variable& v, GenericPolynomial p) { +GenericPolynomialEnable operator*( + const Variable& v, GenericPolynomial p) { return p *= v; } @@ -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 diff --git a/common/symbolic/latex.cc b/common/symbolic/latex.cc index 90aec2b87727..c2262d3510c4 100644 --- a/common/symbolic/latex.cc +++ b/common/symbolic/latex.cc @@ -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 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; @@ -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 { @@ -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, @@ -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(); @@ -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(); } @@ -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; } @@ -395,8 +396,7 @@ string ToLatex(double val, int precision) { multiple_of_famous_constant(val, M_PI, "\\pi")) { return *result; } - if (optional result = - multiple_of_famous_constant(val, M_E, "e")) { + if (optional result = multiple_of_famous_constant(val, M_E, "e")) { return *result; } double intpart; diff --git a/common/symbolic/monomial.cc b/common/symbolic/monomial.cc index d17357bf57c5..ecdee01831c0 100644 --- a/common/symbolic/monomial.cc +++ b/common/symbolic/monomial.cc @@ -176,7 +176,9 @@ bool Monomial::operator==(const Monomial& m) const { return true; } -bool Monomial::operator!=(const Monomial& m) const { return !(*this == m); } +bool Monomial::operator!=(const Monomial& m) const { + return !(*this == m); +} double Monomial::Evaluate(const Environment& env) const { return accumulate( @@ -304,7 +306,9 @@ Monomial operator*(Monomial m1, const Monomial& m2) { return m1; } -Monomial pow(Monomial m, const int p) { return m.pow_in_place(p); } +Monomial pow(Monomial m, const int p) { + return m.pow_in_place(p); +} } // namespace symbolic } // namespace drake diff --git a/common/symbolic/monomial.h b/common/symbolic/monomial.h index c2350ee93a3f..cbfb28048687 100644 --- a/common/symbolic/monomial.h +++ b/common/symbolic/monomial.h @@ -142,8 +142,8 @@ class Monomial { /** Implements the @ref hash_append concept. */ template - friend void hash_append( - HashAlgorithm& hasher, const Monomial& item) noexcept { + friend void hash_append(HashAlgorithm& hasher, + const Monomial& item) noexcept { using drake::hash_append; // We do not send total_degree_ to the hasher, because it is already fully // represented by powers_ -- it is just a cached tally of the exponents. @@ -171,8 +171,7 @@ Monomial pow(Monomial m, int p); namespace std { /* Provides std::hash. */ template <> -struct hash - : public drake::DefaultHash {}; +struct hash : public drake::DefaultHash {}; } // namespace std #if !defined(DRAKE_DOXYGEN_CXX) diff --git a/common/symbolic/monomial_basis_element.h b/common/symbolic/monomial_basis_element.h index f035dffe4135..ecc79ee13c42 100644 --- a/common/symbolic/monomial_basis_element.h +++ b/common/symbolic/monomial_basis_element.h @@ -235,7 +235,6 @@ EIGEN_DEVICE_FUNC inline drake::symbolic::Expression cast( } // namespace Eigen #endif // !defined(DRAKE_DOXYGEN_CXX) - // TODO(jwnimmer-tri) Add a real formatter and deprecate the operator<<. namespace fmt { template <> diff --git a/common/symbolic/polynomial.cc b/common/symbolic/polynomial.cc index 5365f84eae51..bc51df8f0d96 100644 --- a/common/symbolic/polynomial.cc +++ b/common/symbolic/polynomial.cc @@ -412,7 +412,9 @@ Polynomial::Polynomial(const Expression& e, Variables indeterminates) DRAKE_ASSERT_VOID(CheckInvariant()); } -const Variables& Polynomial::indeterminates() const { return indeterminates_; } +const Variables& Polynomial::indeterminates() const { + return indeterminates_; +} void Polynomial::SetIndeterminates(const Variables& new_indeterminates) { if (new_indeterminates.IsSupersetOf(indeterminates_) && @@ -829,8 +831,7 @@ Polynomial& Polynomial::AddProduct(const Expression& coeff, const Monomial& m) { Polynomial Polynomial::SubstituteAndExpand( const std::unordered_map& indeterminate_substitution, - SubstituteAndExpandCacheData* substitutions_cached_data) - const { + SubstituteAndExpandCacheData* substitutions_cached_data) const { SubstituteAndExpandCacheData substitutions_default_obj; SubstituteAndExpandCacheData* cached_data_ptr = substitutions_cached_data == nullptr ? &substitutions_default_obj @@ -1080,23 +1081,39 @@ void Polynomial::CheckInvariant() const { } } -Polynomial operator-(const Polynomial& p) { return -1 * p; } -Polynomial operator+(Polynomial p1, const Polynomial& p2) { return p1 += p2; } -Polynomial operator+(Polynomial p, const Monomial& m) { return p += m; } -Polynomial operator+(const Monomial& m, Polynomial p) { return p += m; } +Polynomial operator-(const Polynomial& p) { + return -1 * p; +} +Polynomial operator+(Polynomial p1, const Polynomial& p2) { + return p1 += p2; +} +Polynomial operator+(Polynomial p, const Monomial& m) { + return p += m; +} +Polynomial operator+(const Monomial& m, Polynomial p) { + return p += m; +} Polynomial operator+(const Monomial& m1, const Monomial& m2) { return Polynomial(m1) + m2; } -Polynomial operator+(Polynomial p, const double c) { return p += c; } -Polynomial operator+(const double c, Polynomial p) { return p += c; } +Polynomial operator+(Polynomial p, const double c) { + return p += c; +} +Polynomial operator+(const double c, Polynomial p) { + return p += c; +} Polynomial operator+(const Monomial& m, const double c) { return Polynomial(m) + c; } Polynomial operator+(const double c, const Monomial& m) { return c + Polynomial(m); } -Polynomial operator+(Polynomial p, const Variable& v) { return p += v; } -Polynomial operator+(const Variable& v, Polynomial p) { return p += v; } +Polynomial operator+(Polynomial p, const Variable& v) { + return p += v; +} +Polynomial operator+(const Variable& v, Polynomial p) { + return p += v; +} Expression operator+(const Expression& e, const Polynomial& p) { return e + p.ToExpression(); } @@ -1104,23 +1121,33 @@ Expression operator+(const Polynomial& p, const Expression& e) { return p.ToExpression() + e; } -Polynomial operator-(Polynomial p1, const Polynomial& p2) { return p1 -= p2; } -Polynomial operator-(Polynomial p, const Monomial& m) { return p -= m; } +Polynomial operator-(Polynomial p1, const Polynomial& p2) { + return p1 -= p2; +} +Polynomial operator-(Polynomial p, const Monomial& m) { + return p -= m; +} Polynomial operator-(const Monomial& m, Polynomial p) { return p = -1 * p + m; // p' = m - p = -1 * p + m. } Polynomial operator-(const Monomial& m1, const Monomial& m2) { return Polynomial(m1) - m2; } -Polynomial operator-(Polynomial p, const double c) { return p -= c; } -Polynomial operator-(const double c, Polynomial p) { return p = -p + c; } +Polynomial operator-(Polynomial p, const double c) { + return p -= c; +} +Polynomial operator-(const double c, Polynomial p) { + return p = -p + c; +} Polynomial operator-(const Monomial& m, const double c) { return Polynomial(m) - c; } Polynomial operator-(const double c, const Monomial& m) { return c - Polynomial(m); } -Polynomial operator-(Polynomial p, const Variable& v) { return p -= v; } +Polynomial operator-(Polynomial p, const Variable& v) { + return p -= v; +} Polynomial operator-(const Variable& v, const Polynomial& p) { return Polynomial(v, p.indeterminates()) - p; } @@ -1131,15 +1158,33 @@ Expression operator-(const Polynomial& p, const Expression& e) { return p.ToExpression() - e; } -Polynomial operator*(Polynomial p1, const Polynomial& p2) { return p1 *= p2; } -Polynomial operator*(Polynomial p, const Monomial& m) { return p *= m; } -Polynomial operator*(const Monomial& m, Polynomial p) { return p *= m; } -Polynomial operator*(const double c, Polynomial p) { return p *= c; } -Polynomial operator*(Polynomial p, const double c) { return p *= c; } -Polynomial operator*(const Monomial& m, double c) { return Polynomial(m) * c; } -Polynomial operator*(double c, const Monomial& m) { return c * Polynomial(m); } -Polynomial operator*(Polynomial p, const Variable& v) { return p *= v; } -Polynomial operator*(const Variable& v, Polynomial p) { return p *= v; } +Polynomial operator*(Polynomial p1, const Polynomial& p2) { + return p1 *= p2; +} +Polynomial operator*(Polynomial p, const Monomial& m) { + return p *= m; +} +Polynomial operator*(const Monomial& m, Polynomial p) { + return p *= m; +} +Polynomial operator*(const double c, Polynomial p) { + return p *= c; +} +Polynomial operator*(Polynomial p, const double c) { + return p *= c; +} +Polynomial operator*(const Monomial& m, double c) { + return Polynomial(m) * c; +} +Polynomial operator*(double c, const Monomial& m) { + return c * Polynomial(m); +} +Polynomial operator*(Polynomial p, const Variable& v) { + return p *= v; +} +Polynomial operator*(const Variable& v, Polynomial p) { + return p *= v; +} Expression operator*(const Expression& e, const Polynomial& p) { return e * p.ToExpression(); } @@ -1202,9 +1247,8 @@ ostream& operator<<(ostream& os, const Polynomial& p) { namespace Eigen { namespace numext { template <> -bool equal_strict( - const drake::symbolic::Polynomial& x, - const drake::symbolic::Polynomial& y) { +bool equal_strict(const drake::symbolic::Polynomial& x, + const drake::symbolic::Polynomial& y) { return static_cast(x == y); } } // namespace numext diff --git a/common/symbolic/polynomial.h b/common/symbolic/polynomial.h index 9c22c059ede6..6434400a27d7 100644 --- a/common/symbolic/polynomial.h +++ b/common/symbolic/polynomial.h @@ -492,6 +492,7 @@ Eigen::Matrix operator*(const MatrixL& lhs, const MatrixR& rhs); #else +// clang-format off template typename std::enable_if_t< std::is_base_of_v, MatrixL> && @@ -518,6 +519,7 @@ Eigen::Matrix() * rhs.template cast(); } +// clang-format on #endif /// Provides the following matrix operations: @@ -537,6 +539,7 @@ Eigen::Matrix operator*(const MatrixL& lhs, const MatrixR& rhs); #else +// clang-format off template typename std::enable_if_t< std::is_base_of_v, MatrixL> && @@ -556,6 +559,7 @@ Eigen::Matrix() * rhs.template cast(); } +// clang-format on #endif } // namespace symbolic @@ -714,9 +718,8 @@ EIGEN_DEVICE_FUNC inline drake::symbolic::Expression cast( } // namespace internal namespace numext { template <> -bool equal_strict( - const drake::symbolic::Polynomial& x, - const drake::symbolic::Polynomial& y); +bool equal_strict(const drake::symbolic::Polynomial& x, + const drake::symbolic::Polynomial& y); template <> EIGEN_STRONG_INLINE bool not_equal_strict( const drake::symbolic::Polynomial& x, @@ -739,7 +742,9 @@ template std::is_same_v, MatrixLikewise> Evaluate(const Eigen::MatrixBase& m, const Environment& env) { - return m.unaryExpr([&env](const Polynomial& p) { return p.Evaluate(env); }); + return m.unaryExpr([&env](const Polynomial& p) { + return p.Evaluate(env); + }); } /// Computes the Jacobian matrix J of the vector function @p f with respect to @@ -791,10 +796,8 @@ CalcPolynomialWLowerTriangularPart( } // namespace symbolic } // namespace drake - // TODO(jwnimmer-tri) Add a real formatter and deprecate the operator<<. namespace fmt { template <> -struct formatter - : drake::ostream_formatter {}; +struct formatter : drake::ostream_formatter {}; } // namespace fmt diff --git a/common/symbolic/rational_function.cc b/common/symbolic/rational_function.cc index de563ae0aaf6..541826223f49 100644 --- a/common/symbolic/rational_function.cc +++ b/common/symbolic/rational_function.cc @@ -114,7 +114,9 @@ RationalFunction& RationalFunction::operator-=(const Monomial& m) { return *this; } -RationalFunction& RationalFunction::operator-=(double c) { return *this += -c; } +RationalFunction& RationalFunction::operator-=(double c) { + return *this += -c; +} RationalFunction& RationalFunction::operator*=(const RationalFunction& f) { numerator_ *= f.numerator(); @@ -201,9 +203,13 @@ RationalFunction operator+(const Monomial& m, RationalFunction f) { return f += m; } -RationalFunction operator+(RationalFunction f, double c) { return f += c; } +RationalFunction operator+(RationalFunction f, double c) { + return f += c; +} -RationalFunction operator+(double c, RationalFunction f) { return f += c; } +RationalFunction operator+(double c, RationalFunction f) { + return f += c; +} RationalFunction operator-(RationalFunction f1, const RationalFunction& f2) { return f1 -= f2; @@ -225,9 +231,13 @@ RationalFunction operator-(const Monomial& m, RationalFunction f) { return -f + m; } -RationalFunction operator-(RationalFunction f, double c) { return f -= c; } +RationalFunction operator-(RationalFunction f, double c) { + return f -= c; +} -RationalFunction operator-(double c, RationalFunction f) { return f = -f + c; } +RationalFunction operator-(double c, RationalFunction f) { + return f = -f + c; +} RationalFunction operator*(RationalFunction f1, const RationalFunction& f2) { return f1 *= f2; @@ -249,9 +259,13 @@ RationalFunction operator*(const Monomial& m, RationalFunction f) { return f *= m; } -RationalFunction operator*(RationalFunction f, double c) { return f *= c; } +RationalFunction operator*(RationalFunction f, double c) { + return f *= c; +} -RationalFunction operator*(double c, RationalFunction f) { return f *= c; } +RationalFunction operator*(double c, RationalFunction f) { + return f *= c; +} RationalFunction operator/(RationalFunction f1, const RationalFunction& f2) { return f1 /= f2; @@ -279,7 +293,9 @@ RationalFunction operator/(const Monomial& m, RationalFunction f) { return {m * f.denominator(), f.numerator()}; } -RationalFunction operator/(RationalFunction f, double c) { return f /= c; } +RationalFunction operator/(RationalFunction f, double c) { + return f /= c; +} RationalFunction operator/(double c, const RationalFunction& f) { if (f.numerator().monomial_to_coefficient_map().empty()) { @@ -311,9 +327,8 @@ void RationalFunction::SetIndeterminates(const Variables& new_indeterminates) { namespace Eigen { namespace numext { template <> -bool equal_strict( - const drake::symbolic::RationalFunction& x, - const drake::symbolic::RationalFunction& y) { +bool equal_strict(const drake::symbolic::RationalFunction& x, + const drake::symbolic::RationalFunction& y) { return static_cast(x == y); } } // namespace numext diff --git a/common/symbolic/rational_function.h b/common/symbolic/rational_function.h index 5189a1b2c2ab..29b64d551ad3 100644 --- a/common/symbolic/rational_function.h +++ b/common/symbolic/rational_function.h @@ -313,9 +313,8 @@ DRAKE_SYMBOLIC_SCALAR_SUM_DIFF_PRODUCT_CONJ_PRODUCT_TRAITS( namespace numext { template <> -bool equal_strict( - const drake::symbolic::RationalFunction& x, - const drake::symbolic::RationalFunction& y); +bool equal_strict(const drake::symbolic::RationalFunction& x, + const drake::symbolic::RationalFunction& y); template <> EIGEN_STRONG_INLINE bool not_equal_strict( const drake::symbolic::RationalFunction& x, @@ -326,10 +325,9 @@ EIGEN_STRONG_INLINE bool not_equal_strict( } // namespace Eigen #endif // !defined(DRAKE_DOXYGEN_CXX) - // TODO(jwnimmer-tri) Add a real formatter and deprecate the operator<<. namespace fmt { template <> -struct formatter - : drake::ostream_formatter {}; +struct formatter : drake::ostream_formatter { +}; } // namespace fmt diff --git a/common/symbolic/test/codegen_test.cc b/common/symbolic/test/codegen_test.cc index 255b4d4b6633..44f90cc6f9d0 100644 --- a/common/symbolic/test/codegen_test.cc +++ b/common/symbolic/test/codegen_test.cc @@ -374,7 +374,9 @@ typedef struct { int inner_indices; } m; } f_meta_t; -f_meta_t f_meta() { return {{3}, {3, 6, 5, 7, 5}}; } +f_meta_t f_meta() { + return {{3}, {3, 6, 5, 7, 5}}; +} TEST_F(SymbolicCodeGenTest, SparseMatrixColMajorExampleUsingEigenMap) { f_meta_t meta = f_meta(); diff --git a/common/symbolic/test/decompose_test.cc b/common/symbolic/test/decompose_test.cc index 8b96e5c96e6a..5ff3c2d12052 100644 --- a/common/symbolic/test/decompose_test.cc +++ b/common/symbolic/test/decompose_test.cc @@ -93,9 +93,9 @@ TEST_F(SymbolicDecomposeTest, DecomposeLinearExpressionsExceptionNonLinear) { x2_ * x2_; // clang-format on DRAKE_EXPECT_THROWS_MESSAGE( - DecomposeLinearExpressions(M_ * x_ + extra_terms_, x_, - &M_expected_static_), - ".*we detected a non-linear expression.*"); + DecomposeLinearExpressions(M_ * x_ + extra_terms_, x_, + &M_expected_static_), + ".*we detected a non-linear expression.*"); } // Adds nonlinear terms to check if we have an exception. @@ -107,9 +107,9 @@ TEST_F(SymbolicDecomposeTest, log(x2_); // clang-format on DRAKE_EXPECT_THROWS_MESSAGE( - DecomposeLinearExpressions(M_ * x_ + extra_terms_, x_, - &M_expected_static_), - ".*we detected a non-polynomial expression.*"); + DecomposeLinearExpressions(M_ * x_ + extra_terms_, x_, + &M_expected_static_), + ".*we detected a non-polynomial expression.*"); } // Adds terms with non-const coefficients to check if we have an exception. @@ -121,9 +121,9 @@ TEST_F(SymbolicDecomposeTest, c_ * x2_; // clang-format on DRAKE_EXPECT_THROWS_MESSAGE( - DecomposeLinearExpressions(M_ * x_ + extra_terms_, x_, - &M_expected_static_), - ".*we detected a non-constant expression.*"); + DecomposeLinearExpressions(M_ * x_ + extra_terms_, x_, + &M_expected_static_), + ".*we detected a non-constant expression.*"); } // Adds constant terms to check if we have an exception. diff --git a/common/symbolic/test/polynomial_basis_element_test.cc b/common/symbolic/test/polynomial_basis_element_test.cc index d2b9f3e087c5..4a20b49665c1 100644 --- a/common/symbolic/test/polynomial_basis_element_test.cc +++ b/common/symbolic/test/polynomial_basis_element_test.cc @@ -167,8 +167,7 @@ TEST_F(SymbolicPolynomialBasisElementTest, Evaluate) { EXPECT_EQ(p1.Evaluate(env2), 9); // p2=xy², but env2 does not contain value for x. double dummy{}; - DRAKE_EXPECT_THROWS_MESSAGE(dummy = p2.Evaluate(env2), - ".* x is not in env"); + DRAKE_EXPECT_THROWS_MESSAGE(dummy = p2.Evaluate(env2), ".* x is not in env"); unused(dummy); } diff --git a/common/symbolic/test/rational_function_test.cc b/common/symbolic/test/rational_function_test.cc index 97fa0c93b4fc..72d0340e04eb 100644 --- a/common/symbolic/test/rational_function_test.cc +++ b/common/symbolic/test/rational_function_test.cc @@ -345,10 +345,10 @@ TEST_F(SymbolicRationalFunctionTest, Evaluate) { {var_y_, -5.0}, {var_z_, -2.0}, }}; - const double expected_numerator{4.0 * -7.0 * -7.0 * -5.0 + - 1.0 * -7.0 * -5.0 + 2.0 * -2.0}; + const double expected_numerator{4.0 * -7.0 * -7.0 * -5.0 + 1.0 * -7.0 * -5.0 + + 2.0 * -2.0}; const double expected_denominator{4.0 * -7.0 * -2.0 + 1.0 * -5.0 * -2.0 + - 2.0 * -7.0 * -5.0}; + 2.0 * -7.0 * -5.0}; EXPECT_EQ(f.Evaluate(env), expected_numerator / expected_denominator); } diff --git a/common/symbolic/test/simplification_test.cc b/common/symbolic/test/simplification_test.cc index bba309069bde..dfd240779cc1 100644 --- a/common/symbolic/test/simplification_test.cc +++ b/common/symbolic/test/simplification_test.cc @@ -270,10 +270,24 @@ TEST_F(SymbolicUnificationTest, MultiplicationFailure4) { // https://github.com/google/googletest/issues/1610 enum UnaryTestOp { - Abs, Log, Exp, Sqrt, Sin, Cos, Tan, Asin, Acos, Atan, Sinh, Cosh, Tanh, Ceil, + Abs, + Log, + Exp, + Sqrt, + Sin, + Cos, + Tan, + Asin, + Acos, + Atan, + Sinh, + Cosh, + Tanh, + Ceil, Floor }; +// clang-format off std::functionUnaryOpToFunction(UnaryTestOp op) { switch (op) { case Abs: return [](const Variable& x) { return abs(x); }; @@ -294,6 +308,7 @@ std::functionUnaryOpToFunction(UnaryTestOp op) { } DRAKE_UNREACHABLE(); } +// clang-format on class SymbolicUnificationTestUnary : public ::testing::TestWithParam { @@ -312,39 +327,33 @@ TEST_P(SymbolicUnificationTestUnary, Check) { EXPECT_PRED2(ExprEqual, rewriter(e2), e2 /* no change */); } -INSTANTIATE_TEST_SUITE_P( - UnaryCases, SymbolicUnificationTestUnary, - ::testing::Values( - Abs, Log, Exp, Sqrt, Sin, Cos, Tan, Asin, Acos, Atan, Sinh, Cosh, Tanh, - Ceil, Floor -)); +INSTANTIATE_TEST_SUITE_P(UnaryCases, SymbolicUnificationTestUnary, + ::testing::Values(Abs, Log, Exp, Sqrt, Sin, Cos, Tan, + Asin, Acos, Atan, Sinh, Cosh, Tanh, + Ceil, Floor)); // https://github.com/google/googletest/issues/1610 -enum BinaryTestOp { - Pow, Div, Min, Max, Atan2 -}; +enum BinaryTestOp { Pow, Div, Min, Max, Atan2 }; +// clang-format off std::function - BinaryOpToFunction(BinaryTestOp op) { +BinaryOpToFunction(BinaryTestOp op) { switch (op) { case Pow: return [](const Variable& x, const Variable& y) { return pow(x, y); }; - case Div: return [](const Variable& x, const Variable& y) { return x / y; }; - case Min: return [](const Variable& x, const Variable& y) { return min(x, y); }; - case Max: return [](const Variable& x, const Variable& y) { return max(x, y); }; - case Atan2: return [](const Variable& x, const Variable& y) { return atan2(x, y); }; } // Should not be reachable. DRAKE_UNREACHABLE(); } +// clang-format on class SymbolicUnificationTestBinary : public ::testing::TestWithParam { @@ -365,9 +374,8 @@ TEST_P(SymbolicUnificationTestBinary, Check) { EXPECT_PRED2(ExprEqual, rewriter(e2), e2 /* no change */); } -INSTANTIATE_TEST_SUITE_P( - BinaryCases, SymbolicUnificationTestBinary, - ::testing::Values(Pow, Div, Min, Max, Atan2)); +INSTANTIATE_TEST_SUITE_P(BinaryCases, SymbolicUnificationTestBinary, + ::testing::Values(Pow, Div, Min, Max, Atan2)); TEST_F(SymbolicUnificationTest, IfThenElse) { // Not supported. diff --git a/common/symbolic/trigonometric_polynomial.cc b/common/symbolic/trigonometric_polynomial.cc index bdb84ffca889..cff0fa7e0f78 100644 --- a/common/symbolic/trigonometric_polynomial.cc +++ b/common/symbolic/trigonometric_polynomial.cc @@ -29,8 +29,7 @@ class SinCosVisitor { public: DRAKE_NO_COPY_NO_MOVE_NO_ASSIGN(SinCosVisitor) - explicit SinCosVisitor(SinCosSubstitution s) - : subs_{std::move(s)} {}; + explicit SinCosVisitor(SinCosSubstitution s) : subs_{std::move(s)} {}; [[nodiscard]] Expression Substitute( const Expression& e, @@ -186,7 +185,7 @@ class SinCosVisitor { } auto iter = subs_.find(get_variable(x)); if (iter == subs_.end()) { - throw std::runtime_error(msg); + throw std::runtime_error(msg); } if (iter->second.type != SinCosSubstitutionType::kAngle) { // Handle special case of cos(±0.5*x). diff --git a/common/symbolic/trigonometric_polynomial.h b/common/symbolic/trigonometric_polynomial.h index 29ca0f1d45ad..95ac93d302a5 100644 --- a/common/symbolic/trigonometric_polynomial.h +++ b/common/symbolic/trigonometric_polynomial.h @@ -74,9 +74,8 @@ Expression Substitute(const Expression& e, const SinCosSubstitution& subs); /** Matrix version of sin/cos substitution. @pydrake_mkdoc_identifier{sincos_matrix} */ template -MatrixLikewise -Substitute(const Eigen::MatrixBase& m, - const SinCosSubstitution& subs) { +MatrixLikewise Substitute( + const Eigen::MatrixBase& m, const SinCosSubstitution& subs) { static_assert(std::is_same_v, "Substitute only accepts a matrix of symbolic::Expression."); // Note that the return type is written out explicitly to help gcc 5 (on