From 65131d2dff8b67a594b3983cb69a05df2855ce09 Mon Sep 17 00:00:00 2001 From: batzor <32958247+batzor@users.noreply.github.com> Date: Tue, 11 Jun 2024 15:49:45 +0900 Subject: [PATCH] feat(math): impl remove zeros for sparse univariate poly --- .../univariate_sparse_coefficients.h | 21 ++++++++----------- 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/tachyon/math/polynomials/univariate/univariate_sparse_coefficients.h b/tachyon/math/polynomials/univariate/univariate_sparse_coefficients.h index 6a47ce3835..ffbf66f918 100644 --- a/tachyon/math/polynomials/univariate/univariate_sparse_coefficients.h +++ b/tachyon/math/polynomials/univariate/univariate_sparse_coefficients.h @@ -104,13 +104,13 @@ class UnivariateSparseCoefficients { constexpr explicit UnivariateSparseCoefficients( const std::vector& terms, bool cleanup = false) : terms_(terms) { - if (cleanup) RemoveHighDegreeZeros(); + if (cleanup) RemoveZeros(); CHECK_LE(Degree(), kMaxDegree); } constexpr explicit UnivariateSparseCoefficients(std::vector&& terms, bool cleanup = false) : terms_(std::move(terms)) { - if (cleanup) RemoveHighDegreeZeros(); + if (cleanup) RemoveZeros(); CHECK_LE(Degree(), kMaxDegree); } @@ -283,6 +283,13 @@ class UnivariateSparseCoefficients { return ss.str(); } + void RemoveZeros() { + terms_.erase( + std::remove_if(terms_.begin(), terms_.end(), + [](const Term& x) { return x.coefficient.IsZero(); }), + terms_.end()); + } + private: friend class internal::UnivariatePolynomialOp< UnivariateDenseCoefficients>; @@ -299,16 +306,6 @@ class UnivariateSparseCoefficients { return &it->coefficient; } - void RemoveHighDegreeZeros() { // Fix to RemoveZeros - while (!IsZero()) { - if (terms_.back().coefficient.IsZero()) { - terms_.pop_back(); - } else { - break; - } - } - } - std::vector terms_; };