-
-
Notifications
You must be signed in to change notification settings - Fork 463
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 .item()
to Matrix1
#1231
Add .item()
to Matrix1
#1231
Conversation
I think this is useful functionality that I could have needed myself many times, but I think the name What about |
I'm fine with any name, but the reason I chose |
I see. Well, I think PyTorch is a very different kind of library than
While it's possible to store things that aren't strictly scalars in |
makes sense, certainly while they are quite different, I sometimes just use PyTorch as a linear algebra library. |
I didn't add |
@JulianKnodt: I think |
// be a null-ptr, as specified by the safety guarantees of `as_ptr()`. | ||
// It would be nice to have this not require this though. | ||
unsafe { core::ptr::read(self.as_ptr()) } | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe this is unsound. For example, take a MatrixView1
and call .into_scalar()
.
A safe way to implement this would be to instead implement it directly on Matrix1
and destructure the underlying array with let [ [ scalar ] ] = array
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have no idea how the underlying storage is laid out for MatrixView1
. It somehow seems counterintuitive for me that the pointer would not point to the one element if it's a view.
Edit: Oh I see what you mean, if T doesn't implement Copy
I haven't yet made a full review yet, but I just wanted to ask you to check your docs with |
I have no idea how to get access to the underlying array, but implementing it directly on |
Allows for converting a `Matrix1` to a scalar without having to index.
Thanks @JulianKnodt for the changes and @Andlon for the review! |
Allows for converting a
Matrix1
to a scalar without having to index.The test is implemented as a doctest, since I couldn't find anything else that would check whether or not code compiles.
Under the hood, it still just indexes, but the method is only available on
Matrix1
.One question is whether or not this should return a borrowed value or an owned value. I assume borrowed, but for most cases it would likely be immediately dereferenced.
Closes #1209