Skip to content

Commit

Permalink
poly: small refactor for SparsePolynomial (#871)
Browse files Browse the repository at this point in the history
* poly: small refactor for SparsePolynomial

* Update poly/src/polynomial/univariate/sparse.rs

Co-authored-by: Pratyush Mishra <[email protected]>

* fix

---------

Co-authored-by: Pratyush Mishra <[email protected]>
  • Loading branch information
tcoratger and Pratyush authored Nov 19, 2024
1 parent 2ef9d27 commit 322336e
Showing 1 changed file with 12 additions and 15 deletions.
27 changes: 12 additions & 15 deletions poly/src/polynomial/univariate/sparse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,7 @@ impl<F: Field> SparsePolynomial<F> {
*cur_coeff += &(*self_coeff * other_coeff);
}
}
let result = result.into_iter().collect::<Vec<_>>();
SparsePolynomial::from_coefficients_vec(result)
SparsePolynomial::from_coefficients_vec(result.into_iter().collect())
}
}

Expand All @@ -272,9 +271,7 @@ impl<F: Field> SparsePolynomial<F> {
// being higher than self.degree()
fn append_coeffs(&mut self, append_coeffs: &[(usize, F)]) {
assert!(append_coeffs.is_empty() || self.degree() < append_coeffs[0].0);
for (i, elem) in append_coeffs.iter() {
self.coeffs.push((*i, *elem));
}
self.coeffs.extend_from_slice(append_coeffs);
}
}

Expand All @@ -285,13 +282,13 @@ impl<F: FftField> SparsePolynomial<F> {
domain: D,
) -> Evaluations<F, D> {
let poly: DenseOrSparsePolynomial<'_, F> = self.into();
DenseOrSparsePolynomial::<F>::evaluate_over_domain(poly, domain)
DenseOrSparsePolynomial::evaluate_over_domain(poly, domain)
}

/// Evaluate `self` over `domain`.
pub fn evaluate_over_domain<D: EvaluationDomain<F>>(self, domain: D) -> Evaluations<F, D> {
let poly: DenseOrSparsePolynomial<'_, F> = self.into();
DenseOrSparsePolynomial::<F>::evaluate_over_domain(poly, domain)
DenseOrSparsePolynomial::evaluate_over_domain(poly, domain)
}
}

Expand All @@ -307,14 +304,14 @@ impl<F: Field> From<SparsePolynomial<F>> for DensePolynomial<F> {

impl<F: Field> From<DensePolynomial<F>> for SparsePolynomial<F> {
fn from(dense_poly: DensePolynomial<F>) -> SparsePolynomial<F> {
let coeffs = dense_poly.coeffs();
let mut sparse_coeffs = Vec::<(usize, F)>::new();
for (i, coeff) in coeffs.iter().enumerate() {
if !coeff.is_zero() {
sparse_coeffs.push((i, *coeff));
}
}
SparsePolynomial::from_coefficients_vec(sparse_coeffs)
SparsePolynomial::from_coefficients_vec(
dense_poly
.coeffs()
.iter()
.enumerate()
.filter_map(|(i, coeff)| (!coeff.is_zero()).then(|| (i, *coeff)))
.collect(),
)
}
}

Expand Down

0 comments on commit 322336e

Please sign in to comment.