diff --git a/tachyon/math/polynomials/univariate/univariate_polynomial_ops.h b/tachyon/math/polynomials/univariate/univariate_polynomial_ops.h index dd317f3d6c..f33573c22a 100644 --- a/tachyon/math/polynomials/univariate/univariate_polynomial_ops.h +++ b/tachyon/math/polynomials/univariate/univariate_polynomial_ops.h @@ -832,7 +832,7 @@ class UnivariatePolynomialOp> { } c.coefficients_ = S(std::move(c_terms)); - c.coefficients_.RemoveHighDegreeZeros(); + c.coefficients_.RemoveZeros(); } static void DoMul(const UnivariatePolynomial& a, @@ -861,7 +861,7 @@ class UnivariatePolynomialOp> { } pdqsort(c_terms.begin(), c_terms.end()); c.coefficients_ = S(std::move(c_terms)); - c.coefficients_.RemoveHighDegreeZeros(); + c.coefficients_.RemoveZeros(); } }; diff --git a/tachyon/math/polynomials/univariate/univariate_sparse_coefficients.h b/tachyon/math/polynomials/univariate/univariate_sparse_coefficients.h index fa62edbfa1..585f4c0ce5 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) : terms_(terms) { + RemoveZeros(); CHECK_LE(Degree(), kMaxDegree); - RemoveHighDegreeZeros(); } constexpr explicit UnivariateSparseCoefficients(std::vector&& terms) : terms_(std::move(terms)) { + RemoveZeros(); CHECK_LE(Degree(), kMaxDegree); - RemoveHighDegreeZeros(); } constexpr static UnivariateSparseCoefficients CreateChecked( @@ -289,12 +289,12 @@ class UnivariateSparseCoefficients { return &it->coefficient; } - void RemoveHighDegreeZeros() { // Fix to RemoveZeros - while (!IsZero()) { - if (terms_.back().coefficient.IsZero()) { - terms_.pop_back(); + void RemoveZeros() { + for (auto it = terms_.begin(); it != terms_.end(); ) { + if (it->coefficient.IsZero()) { + it = terms_.erase(it); } else { - break; + ++it; } } }