Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add methods for adding/subtracting vectors #1205

Merged
merged 1 commit into from
Jan 23, 2025
Merged

Conversation

cjpatton
Copy link
Collaborator

@cjpatton cjpatton commented Jan 18, 2025

Closes #301: see #301 (comment).

Add a method add_assign_vector that replaces the following zip pattern:

for (x, y) in a.iter_mut().zip(y) {
    *x += *y;
}

The method also panics if b is shorter than a.

Replace the zip pattern with this method wherever we add up vectors. This helps us ensure that we're not accidentally adding a vector that's shorter than the other.

Likewise for subtracting vectors.

Add a method `add_assign_vector` that replaces the following zip
pattern:

```
for (x, y) in a.iter_mut().zip(y) {
    *x += *y;
}
```

The method also panics if `b` is shorter than `a`.

Replace the zip pattern with this method wherever we add up vectors.
This helps us ensure that we're not accidentally adding a vector that's
shorter than the other.

Likewise for subtracting vectors.
Copy link

@jcjones jcjones left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've gone through these changes and they look fine and correct to me.

@divergentdave divergentdave merged commit 9457963 into main Jan 23, 2025
6 checks passed
@divergentdave divergentdave deleted the cjpatton/301 branch January 23, 2025 00:05
*x -= y;
count += 1;
}
assert_eq!(a.len(), count);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we wanted to be real clever, we could add a debug assertion that a.len == b.len using #[cfg(debug_assertions)] to conditionally compile it.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At this point, we only know b produces an iterator, so we'd have to do so in a more specialized method that takes two slices

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement std::ops::Add for vectors of field elements
3 participants