@@ -321,38 +321,32 @@ impl<'a, F: Field> Add<&'a SparsePolynomial<F>> for &DensePolynomial<F> {
321
321
322
322
#[ inline]
323
323
fn add ( self , other : & ' a SparsePolynomial < F > ) -> DensePolynomial < F > {
324
- // If `self` is zero, the result is `other` (as adding zero doesn't change anything).
325
324
if self . is_zero ( ) {
326
325
return other. clone ( ) . into ( ) ;
327
326
}
328
327
329
- // If `other` is zero, the result is `self` (as adding zero doesn't change anything).
330
328
if other. is_zero ( ) {
331
329
return self . clone ( ) ;
332
330
}
333
331
334
332
let mut result = self . clone ( ) ;
335
333
336
- // Prepare a vector to store the coefficients of the upper terms of the addition.
334
+ // If `other` has higher degree than `self`, create a dense vector
335
+ // storing the upper coefficients of the addition
337
336
let mut upper_coeffs = vec ! [ F :: zero( ) ; other. degree( ) . saturating_sub( result. degree( ) ) ] ;
338
337
339
- // Iterate over the sparse polynomial's non-zero terms.
340
338
for ( pow, coeff) in other. iter ( ) {
341
- // If the power `pow` is within the degree of `result`, add the coefficient to the corresponding term.
342
339
if let Some ( target) = result. coeffs . get_mut ( * pow) {
343
340
* target += coeff;
344
341
} else {
345
- // Otherwise, store the coefficient in the `upper_coeffs` vector at the appropriate position.
346
- //
347
- // The index is adjusted by subtracting the current length of `result.coeffs`.
348
342
upper_coeffs[ pow - result. coeffs . len ( ) ] = * coeff;
349
343
}
350
344
}
351
345
352
346
// Extend the coefficient vector of `result` with the upper terms.
353
347
result. coeffs . extend ( upper_coeffs) ;
354
348
355
- // Remove any trailing zeros from the coefficient vector to ensure it represents the correct polynomial .
349
+ // Remove any leading zeros.
356
350
// For example: `0 * x^2 + 0 * x + 1` should be represented as `1`.
357
351
result. truncate_leading_zeros ( ) ;
358
352
0 commit comments