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

Change the SVD methods to return a Result instead of panicking #441

Merged
merged 2 commits into from
Oct 13, 2018

Conversation

JD557
Copy link
Contributor

@JD557 JD557 commented Oct 8, 2018

Fix #358.

I changed the SVD methods to return a Result instead of an Option, since there are multiple types of errors. Knowing the error message might be useful.

I'm pretty new to rust and nalgebra, so I'm not sure if I'm doing everything right. For example, some questions in my mind right now:

  • Should I also change the return types of the methods in nalgebra-lapack/src/svd.rs?
  • Is is OK to return a Result<MatrixMN<N, R, C>, &'static str>:
    • Should I return a custom error type?
    • Is it OK for the lifetime of that string slice to be static (I'm pretty much a noob when it comes to lifetimes)? Should I return a String instead?

Copy link
Member

@sebcrozet sebcrozet left a comment

Choose a reason for hiding this comment

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

Thank you for this!

Should I also change the return types of the methods in nalgebra-lapack/src/svd.rs?

If you have the time, that would be great too!

Is is OK to return a Result<MatrixMN<N, R, C>, &'static str>:
Should I return a custom error type?

I don't think a custom error type is worth the trouble here. All those errors are due to the user not passing the arguments that are right for its use of SVD, so an explicit message should be enough. We can keep the &'static str for now and see of other users find it too limiting in the future.
Using &'static str looks find to me here since all our messages are static strings.

I've made one small comment on your code to address before merging this.

match (self.u, self.v_t) {
(Some(_u), Some(_v_t)) => {
let mut u = _u;
let v_t = _v_t;
Copy link
Member

Choose a reason for hiding this comment

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

You can set the mut inside of the match pattern itself: (Some(mut u), Some(v_t)).
So you don't have to have those _u, _v_t intermediate values.

@JD557
Copy link
Contributor Author

JD557 commented Oct 9, 2018

I've looked again at the lapack codebase and I don't think it makes sense to return Result there, as the API calls never fail.

I just skimmed through the logic, but it appears that there are no compute_u/compute_v flags and the code works just fine if epsilon < 0.0.

What's your opinion on this?

  • Should I keep the lapack API unchanged ("if it ain't broke don't fix it")?
  • Should I change the return type to Result<MatrixMN<N, R, C>, &'static str> and never fail? (Result<MatrixMN<N, R, C>, !> would probably be more correct, but it appears that the bottom type is still experimental)
  • Should I change the lapack API to check that epsilon >= 0?

@sebcrozet sebcrozet changed the base branch from master to dev October 13, 2018 08:54
@sebcrozet
Copy link
Member

@JD557 Thanks! Let's keep the Lapack impl the way it is for now.
Since this is a breaking change, I will merge this to the dev branch until the next point release is made.

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