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 vector normalization to Vector trait #114

Merged
merged 3 commits into from
Sep 5, 2024

Conversation

sunsided
Copy link
Contributor

@sunsided sunsided commented Jul 9, 2024

This addresses #99 and performs vector normalization by reusing the existing magnitude implementation, i.e. it goes through f32, normalizes in f32 and then converts back to C by using FromIterator.


I attempted pre-calculating the inverse of the magnitude and then multiplying by that, but it brought the results too far off in terms of error.

I thought about factoring out invsqrt into a trait and then implement that trait for F32 and f32 alike. This way the magnitude function could be more generic (C: InvSqrt instead of C: Into<f32>) and wouldn't require the use of f32 explicitly.

src/vector.rs Outdated
Comment on lines 62 to 63
/// Compute the squared magnitude of a vector
fn magnitude_sq(self) -> C
where
C: core::iter::Sum,
{
self.iter().map(|n| n * n).sum()
}

/// Compute the magnitude of a vector
fn magnitude(self) -> f32
Copy link
Owner

Choose a reason for hiding this comment

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

It might make sense for magnitude to be composed in terms of magnitude_sq. This change also seems irrelevant to the addition of normalized and could possibly use its own PR. We might also consider changing magnitude to similarly return C, and possibly other methods as well, though that seems like a breaking change.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I had the same thoughts, just didn't want to fiddle with the existing implementations too much. Expressing magnitude in terms of magnitude_sq requires the core::iter::Sum trait bound as well.

I agree that magnitude_sq can be removed here as it doesn't add to normalize. I'll pull it into a separate PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added in #118.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Went a different way now and added the map function that transforms elements individually. Let me know what you think about it; if it doesn't fit, I'll remove it.

@tarcieri tarcieri merged commit f96f696 into tarcieri:main Sep 5, 2024
11 checks passed
@sunsided sunsided deleted the feature/magnitude-sq branch September 5, 2024 16:30
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.

2 participants