-
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
Conversation
Better than `Array{T,0}` and `Ref` as scalar containers.
I still need to do tests and documentation, but feedback welcome |
@andyferris My implementation had these methods also. I don't remember why all of them were necessary, or if they even are. Base.show{T}(io::IO, x::Scalar{T}) = print(io, "Scalar{$T}(", x.val, ")")
Base.show{T}(io::IO, ::MIME"text/plain", x::Scalar{T}) = show(io, x)
Base.indices(x::Scalar) = ()
Base.size(x::Scalar) = ()
Base.getindex(x::Scalar) = x.val
Base.getindex(x::Scalar, i::Integer) = i == 1 ? x.val : throw(BoundsError(x, i)) Perhaps add these to the test cases? @test Scalar(2) .* [1, 2, 3] == [2, 4, 6]
@test Scalar([1 2; 3 4]) .+ [[1 1; 1 1], [2 2; 2 2]] == [[2 3; 4 5], [3 4; 5 6]] |
The StaticArrays interface provides many methods for free
Thanks, will do! |
data::T | ||
end | ||
|
||
@inline (::Type{Scalar}){T}(x::Tuple{T}) = Scalar{T}(x[1]) |
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 one T
(as quite distinct from Array{T}
containing many T
).
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 😄.
@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 comment
The reason will be displayed to describe this comment to others. Learn more.
Are these @inline
annotations actually needed? Also, this function can probably be defined as getindex(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.
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.
This looks great! |
done |
Thanks @TotalVerb |
I'm still amused that a good way of making a scalar is to make it a subtype of |
I think this is good and it fits the needs discussed in #45. As discussed there the name |
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 looks to be a great replacement for the bad temporary measure I'm using now, and all my concerns have been addressed.
Thanks for the reviews |
Better than
Array{T,0}
andRef
as scalar containers since this is immutable. It's useful for controlling broadcasting. Here is an example:See some relevant discussions at:
#45 (comment)
JuliaLang/julia#18379
JuliaLang/julia#18766 (comment)