From d701f65d97d925a79b7d912e794366661f4df1da Mon Sep 17 00:00:00 2001 From: TomTaehoonKim Date: Wed, 27 Sep 2023 22:38:59 +0900 Subject: [PATCH] fix(math): fix `IsOne` for sparse univariate polynomials Before this commit, `IsOne` did not check whether the degree is 0 or not. This commit fixes it. --- tachyon/math/polynomials/univariate/sparse_coefficients.h | 3 ++- .../math/polynomials/univariate/sparse_polynomial_unittest.cc | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/tachyon/math/polynomials/univariate/sparse_coefficients.h b/tachyon/math/polynomials/univariate/sparse_coefficients.h index ea8f4b647..5c79916c0 100644 --- a/tachyon/math/polynomials/univariate/sparse_coefficients.h +++ b/tachyon/math/polynomials/univariate/sparse_coefficients.h @@ -109,7 +109,8 @@ class SparseCoefficients { constexpr bool IsZero() const { return terms_.empty(); } constexpr bool IsOne() const { - return terms_.size() == 1 && terms_[0].coefficient.IsOne(); + return terms_.size() == 1 && terms_[0].degree == 0 && + terms_[0].coefficient.IsOne(); } constexpr size_t Degree() const { diff --git a/tachyon/math/polynomials/univariate/sparse_polynomial_unittest.cc b/tachyon/math/polynomials/univariate/sparse_polynomial_unittest.cc index a7158f9a4..ec3ac388f 100644 --- a/tachyon/math/polynomials/univariate/sparse_polynomial_unittest.cc +++ b/tachyon/math/polynomials/univariate/sparse_polynomial_unittest.cc @@ -51,6 +51,7 @@ TEST_F(SparseUnivariatePolynomialTest, IsZero) { TEST_F(SparseUnivariatePolynomialTest, IsOne) { EXPECT_TRUE(Poly::One().IsOne()); EXPECT_TRUE(Poly(Coeffs({{0, GF7(1)}})).IsOne()); + EXPECT_FALSE(Poly(Coeffs({{1, GF7(1)}})).IsOne()); for (size_t i = 0; i < polys_.size() - 1; ++i) { EXPECT_FALSE(polys_[i].IsOne()); }