From 8f1cf8f98deed269d7f5cb33d73f83839b501141 Mon Sep 17 00:00:00 2001 From: Thomas Coratger Date: Tue, 12 Nov 2024 23:38:11 +0100 Subject: [PATCH 1/3] poly: small refactor for SparsePolynomial --- poly/src/polynomial/univariate/sparse.rs | 33 +++++++++++++----------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/poly/src/polynomial/univariate/sparse.rs b/poly/src/polynomial/univariate/sparse.rs index 313bb1f5a..6b3f86851 100644 --- a/poly/src/polynomial/univariate/sparse.rs +++ b/poly/src/polynomial/univariate/sparse.rs @@ -262,8 +262,7 @@ impl SparsePolynomial { *cur_coeff += &(*self_coeff * other_coeff); } } - let result = result.into_iter().collect::>(); - SparsePolynomial::from_coefficients_vec(result) + SparsePolynomial::from_coefficients_vec(result.into_iter().collect()) } } @@ -272,9 +271,7 @@ impl SparsePolynomial { // 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); } } @@ -285,13 +282,13 @@ impl SparsePolynomial { domain: D, ) -> Evaluations { let poly: DenseOrSparsePolynomial<'_, F> = self.into(); - DenseOrSparsePolynomial::::evaluate_over_domain(poly, domain) + DenseOrSparsePolynomial::evaluate_over_domain(poly, domain) } /// Evaluate `self` over `domain`. pub fn evaluate_over_domain>(self, domain: D) -> Evaluations { let poly: DenseOrSparsePolynomial<'_, F> = self.into(); - DenseOrSparsePolynomial::::evaluate_over_domain(poly, domain) + DenseOrSparsePolynomial::evaluate_over_domain(poly, domain) } } @@ -307,14 +304,20 @@ impl From> for DensePolynomial { impl From> for SparsePolynomial { fn from(dense_poly: DensePolynomial) -> SparsePolynomial { - 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)| { + if !coeff.is_zero() { + Some((i, *coeff)) + } else { + None + } + }) + .collect(), + ) } } From 121f82cf9df29c8b4be69db7e86bfd1f59eb8526 Mon Sep 17 00:00:00 2001 From: Thomas Coratger <60488569+tcoratger@users.noreply.github.com> Date: Thu, 14 Nov 2024 15:51:31 +0100 Subject: [PATCH 2/3] Update poly/src/polynomial/univariate/sparse.rs Co-authored-by: Pratyush Mishra --- poly/src/polynomial/univariate/sparse.rs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/poly/src/polynomial/univariate/sparse.rs b/poly/src/polynomial/univariate/sparse.rs index 6b3f86851..ef9ed5525 100644 --- a/poly/src/polynomial/univariate/sparse.rs +++ b/poly/src/polynomial/univariate/sparse.rs @@ -310,11 +310,7 @@ impl From> for SparsePolynomial { .iter() .enumerate() .filter_map(|(i, coeff)| { - if !coeff.is_zero() { - Some((i, *coeff)) - } else { - None - } + (!coeff.is_zero()).then((i, *coeff)) }) .collect(), ) From be3b9ea06b7970814c2110306dfe61bd2f3bb90c Mon Sep 17 00:00:00 2001 From: Thomas Coratger Date: Thu, 14 Nov 2024 16:20:44 +0100 Subject: [PATCH 3/3] fix --- poly/src/polynomial/univariate/sparse.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/poly/src/polynomial/univariate/sparse.rs b/poly/src/polynomial/univariate/sparse.rs index ef9ed5525..460cb8206 100644 --- a/poly/src/polynomial/univariate/sparse.rs +++ b/poly/src/polynomial/univariate/sparse.rs @@ -309,9 +309,7 @@ impl From> for SparsePolynomial { .coeffs() .iter() .enumerate() - .filter_map(|(i, coeff)| { - (!coeff.is_zero()).then((i, *coeff)) - }) + .filter_map(|(i, coeff)| (!coeff.is_zero()).then(|| (i, *coeff))) .collect(), ) }