From b69e95d17bfb7ebffc78f0150f74021992e3741d Mon Sep 17 00:00:00 2001 From: Thomas Coratger Date: Mon, 16 Dec 2024 00:31:39 +0100 Subject: [PATCH] refactor sub_assign DensePolynomial --- poly/src/polynomial/univariate/dense.rs | 34 +++++++++++-------------- 1 file changed, 15 insertions(+), 19 deletions(-) diff --git a/poly/src/polynomial/univariate/dense.rs b/poly/src/polynomial/univariate/dense.rs index 075351523..48b45bb8f 100644 --- a/poly/src/polynomial/univariate/dense.rs +++ b/poly/src/polynomial/univariate/dense.rs @@ -509,8 +509,7 @@ impl<'a, F: Field> Sub<&'a SparsePolynomial> for &DensePolynomial { #[inline] fn sub(self, other: &'a SparsePolynomial) -> DensePolynomial { if self.is_zero() { - let result = other.clone(); - (-result).into() + (-other.clone()).into() } else if other.is_zero() { self.clone() } else { @@ -538,24 +537,21 @@ impl<'a, F: Field> SubAssign<&'a DensePolynomial> for DensePolynomial { #[inline] fn sub_assign(&mut self, other: &'a DensePolynomial) { if self.is_zero() { - self.coeffs.resize(other.coeffs.len(), F::zero()); - } else if other.is_zero() { - return; - } else if self.degree() >= other.degree() { - } else { - // Add the necessary number of zero coefficients. - self.coeffs.resize(other.coeffs.len(), F::zero()); + self.coeffs = other.coeffs.iter().map(|&x| -x).collect(); + } else if !other.is_zero() { + if self.degree() < other.degree() { + self.coeffs.resize(other.coeffs.len(), F::zero()); + } + self.coeffs + .iter_mut() + .zip(&other.coeffs) + .for_each(|(a, b)| *a -= b); + + // If the leading coefficient ends up being zero, pop it off. + // This can happen if they were the same degree, or if other's + // coefficients were constructed with leading zeros. + self.truncate_leading_zeros(); } - self.coeffs - .iter_mut() - .zip(&other.coeffs) - .for_each(|(a, b)| { - *a -= b; - }); - // If the leading coefficient ends up being zero, pop it off. - // This can happen if they were the same degree, or if other's - // coefficients were constructed with leading zeros. - self.truncate_leading_zeros(); } }