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 11, 2024
1 parent b3ea3d7 commit 65131d2
Showing 1 changed file with 9 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,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 +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<F, MaxDegree>>;
Expand All @@ -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<Term> terms_;
};

Expand Down

0 comments on commit 65131d2

Please sign in to comment.