Skip to content

Commit

Permalink
feat(math): impl remove zeros for sparse univariate poly
Browse files Browse the repository at this point in the history
  • Loading branch information
batzor committed Jun 12, 2024
1 parent b3ea3d7 commit 802ef70
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 12 deletions.
1 change: 1 addition & 0 deletions tachyon/math/polynomials/univariate/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ tachyon_cc_library(
"//tachyon/base/buffer:copyable",
"//tachyon/base/containers:adapters",
"//tachyon/base/containers:container_util",
"//tachyon/base/containers:cxx20_erase",
"//tachyon/base/json",
"//tachyon/base/ranges:algorithm",
"//tachyon/base/strings:string_util",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

#include "tachyon/base/buffer/copyable.h"
#include "tachyon/base/containers/adapters.h"
#include "tachyon/base/containers/cxx20_erase_vector.h"
#include "tachyon/base/json/json.h"
#include "tachyon/base/logging.h"
#include "tachyon/base/optional.h"
Expand Down Expand Up @@ -104,13 +105,13 @@ class UnivariateSparseCoefficients {
constexpr explicit UnivariateSparseCoefficients(
const std::vector<Term>& terms, bool cleanup = false)
: terms_(terms) {
if (cleanup) RemoveHighDegreeZeros();
if (cleanup) RemoveZeros();
CHECK_LE(Degree(), kMaxDegree);
}
constexpr explicit UnivariateSparseCoefficients(std::vector<Term>&& terms,
bool cleanup = false)
: terms_(std::move(terms)) {
if (cleanup) RemoveHighDegreeZeros();
if (cleanup) RemoveZeros();
CHECK_LE(Degree(), kMaxDegree);
}

Expand Down Expand Up @@ -283,6 +284,10 @@ class UnivariateSparseCoefficients {
return ss.str();
}

void RemoveZeros() {
base::EraseIf(terms_, [](const Term& x) { return x.coefficient.IsZero(); });
}

private:
friend class internal::UnivariatePolynomialOp<
UnivariateDenseCoefficients<F, MaxDegree>>;
Expand All @@ -299,16 +304,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<Term> terms_;
};

Expand Down

0 comments on commit 802ef70

Please sign in to comment.