From 9a9b1af434d910494527c631ef5e951cccea7147 Mon Sep 17 00:00:00 2001 From: batzor <32958247+batzor@users.noreply.github.com> Date: Wed, 5 Jun 2024 18:36:22 +0900 Subject: [PATCH] feat(math): impl remove zeros for sparse univariate poly --- .../univariate/univariate_polynomial_ops.h | 4 ++-- .../univariate/univariate_sparse_coefficients.h | 14 +++++++------- 2 files changed, 9 insertions(+), 9 deletions(-) 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..75f1ba702a 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; } } }