diff --git a/common/symbolic/polynomial.cc b/common/symbolic/polynomial.cc index 2c2dc5a6ddd5..ff3aecd147f3 100644 --- a/common/symbolic/polynomial.cc +++ b/common/symbolic/polynomial.cc @@ -1056,9 +1056,9 @@ bool Polynomial::IsOdd() const { Eigen::VectorXcd Polynomial::Roots() const { if (indeterminates().size() != 1) { - throw std::runtime_error(fmt::format( - "{} is not a univariate polynomial; it has indeterminates {}.", - ToExpression().to_string(), indeterminates().to_string())); + throw runtime_error(fmt::format( + "{} is not a univariate polynomial; it has indeterminates {}.", *this, + indeterminates())); } // We find the roots by computing the eigenvalues of the companion matrix. @@ -1074,11 +1074,11 @@ Eigen::VectorXcd Polynomial::Roots() const { double leading_coefficient = 0; for (const auto& [monomial, coeff] : monomial_to_coefficient_map()) { if (!is_constant(coeff)) { - throw std::runtime_error( - fmt::format("Polynomial::Roots() only supports polynomials with " - "constant coefficients. This polynomial has coefficient " - "{} for the monomial {}.", - coeff.to_string(), monomial.ToExpression().to_string())); + throw runtime_error(fmt::format( + "Polynomial::Roots() only supports polynomials with constant " + "coefficients. This polynomial has coefficient {} for the " + "monomial {}.", + coeff, monomial)); } const int power = monomial.total_degree(); if (power == degree) { diff --git a/common/symbolic/test/polynomial_test.cc b/common/symbolic/test/polynomial_test.cc index facccf2f2c07..0d81c01f26d1 100644 --- a/common/symbolic/test/polynomial_test.cc +++ b/common/symbolic/test/polynomial_test.cc @@ -1512,7 +1512,7 @@ TEST_F(SymbolicPolynomialTest, Roots) { // Note: the order of the roots is not guaranteed. Sort them here by real, // then imaginary. - auto sort = [](const Eigen::VectorXcd& v) { + auto sorted = [](const Eigen::VectorXcd& v) { Eigen::VectorXcd v_sorted = v; std::sort(v_sorted.data(), v_sorted.data() + v_sorted.size(), [](const std::complex& a, const std::complex& b) { @@ -1526,14 +1526,14 @@ TEST_F(SymbolicPolynomialTest, Roots) { // Include a repeated root. p = symbolic::Polynomial{(x_ - 1.23) * (x_ - 4.56) * (x_ - 4.56)}; - roots = sort(p.Roots()); + roots = sorted(p.Roots()); EXPECT_TRUE( CompareMatrices(roots.real(), Eigen::Vector3d{1.23, 4.56, 4.56}, 1e-7)); EXPECT_TRUE(CompareMatrices(roots.imag(), Eigen::Vector3d::Zero(), 1e-7)); // Complex roots. x^4 - 1 has roots {-1, -i, i, 1}. p = symbolic::Polynomial{pow(x_, 4) - 1}; - roots = sort(p.Roots()); + roots = sorted(p.Roots()); EXPECT_TRUE( CompareMatrices(roots.real(), Eigen::Vector4d{-1, 0, 0, 1}, 1e-7)); EXPECT_TRUE( @@ -1541,7 +1541,7 @@ TEST_F(SymbolicPolynomialTest, Roots) { // Leading coefficient is not 1. p = symbolic::Polynomial{(2.1 * x_ - 1.23) * (x_ - 4.56)}; - roots = sort(p.Roots()); + roots = sorted(p.Roots()); EXPECT_TRUE( CompareMatrices(roots.real(), Eigen::Vector2d{1.23 / 2.1, 4.56}, 1e-7)); EXPECT_TRUE(CompareMatrices(roots.imag(), Eigen::Vector2d::Zero(), 1e-7));