-
Notifications
You must be signed in to change notification settings - Fork 148
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
Added a Scalar
type for 0-dimensional array
#50
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
""" | ||
Scalar{T}(x::T) | ||
|
||
Construct a statically-sized 0-dimensional array that contains a single element, | ||
`x`. This type is particularly useful for influencing broadcasting operations. | ||
""" | ||
immutable Scalar{T} <: StaticArray{T,0} | ||
data::T | ||
end | ||
|
||
@inline (::Type{Scalar}){T}(x::Tuple{T}) = Scalar{T}(x[1]) | ||
|
||
@pure size(::Type{Scalar}) = () | ||
@pure size{T}(::Type{Scalar{T}}) = () | ||
|
||
@inline function getindex(v::Scalar) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are these There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As a rule I haven't been trusting the heuristics of the compiler anywhere (at times a very large amount of code is inlined). But in this case you are right, I think it will always work. |
||
v.data | ||
end | ||
|
||
@inline Tuple(v::Scalar) = (v.data,) |
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.
Doesn't this prevent scalarizing tuples? Can this maybe be a
convert
method instead?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.
The StaticArrays interface requires a constructor that accepts a Tuple. I may have messed this up a little though.
(PS - side effect is small - it only does this for a one-tuple which is scalar-like anyway).
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.
This code looks like it's preventing scalarizing any tuple, though.
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 got caught up on this too, but I think I'm ok after reading Andy's comment -
Tuple{T}
being the tuple containing exactly oneT
(as quite distinct fromArray{T}
containing manyT
).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.
Ah right. The new version does not have the issue with even tuples containing one
T
though, which is even better 😄.