-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Make Ref behave as a 0-dimensional array in broadcast #18965
Conversation
Note that |
Use |
@@ -337,3 +337,9 @@ end | |||
@test broadcast(+, 1.0, (0, -2.0)) == (1.0,-1.0) | |||
@test broadcast(+, 1.0, (0, -2.0), [1]) == [2.0, 0.0] | |||
@test broadcast(*, ["Hello"], ", ", ["World"], "!") == ["Hello, World!"] | |||
|
|||
# Issue #18379 | |||
@test (+).(1, Ref(2)) == fill(3) |
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.
cute, I didn't realize that fill
could be used to create a 0-dimensional array like this
Yeah, I guess what we need is just |
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 think the set Ref
minus Ptr
is a better API. The set RefArray
+ RefValue
is rather arbitrary (it's incomplete).
@@ -127,6 +131,7 @@ dumpbitcache(Bc::Vector{UInt64}, bind::Int, C::Vector{Bool}) = | |||
Base.copy_to_bitarray_chunks!(Bc, ((bind - 1) << 6) + 1, C, 1, min(bitcache_size, (length(Bc)-bind+1) << 6)) | |||
|
|||
@inline _broadcast_getindex(A, I) = _broadcast_getindex(containertype(A), A, I) | |||
@inline _broadcast_getindex(A::RefArrOrVal, I) = A.x |
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.
@inline _broadcast_getindex(A::Ref, I) = A[]
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 was thinking in limiting all this to RefValue
. If we allow Ref
and leave this definition of _broadcast_getindex
as A[]
, then we will have
broadcast(+, [[0,2], [1,3]], Ref([1,-1])) == [[1,3], [2,4]]
where we want it to be [[1,1], [2,2]]
, which broadcast(+, [[0,2], [1,3]], RefValue([1,-1]))
would correctly yield.
@@ -48,6 +51,7 @@ broadcast_indices(A) = broadcast_indices(containertype(A), A) | |||
broadcast_indices(::Type{Any}, A) = () | |||
broadcast_indices(::Type{Tuple}, A) = (OneTo(length(A)),) | |||
broadcast_indices(::Type{Array}, A) = indices(A) | |||
broadcast_indices(::Type{Array}, A::RefArrOrVal) = () |
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.
Ref
@@ -30,6 +32,7 @@ end | |||
containertype(x) = containertype(typeof(x)) | |||
containertype(::Type) = Any | |||
containertype{T<:Tuple}(::Type{T}) = Tuple | |||
containertype{T<:RefArrOrVal}(::Type{T}) = 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.
Ref
@@ -9,6 +9,8 @@ import Base: broadcast | |||
export broadcast!, bitbroadcast, dotview | |||
export broadcast_getindex, broadcast_setindex! | |||
|
|||
typealias RefArrOrVal Union{Base.RefArray, Base.RefValue} |
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.
just use Ref
7d120b9
to
09a9f03
Compare
246ff49
to
00ef35f
Compare
00ef35f
to
bc247a4
Compare
I updated the PR addressing @vtjnash comments and added news and docs. In the docs there's now an example of how to use |
Seems that |
Well, until we have something like #18990 (comment), I'd be inclined to throw an error for |
No, because using |
So EDIT: NVM. Considering what |
I believe |
bc247a4
to
8a4cb44
Compare
OK. Now it properly handles |
LGTM. |
- If there is at least an array in the arguments, it returns an array | ||
(and treats tuples as 1-dimensional arrays) expanding singleton dimensions. | ||
- If there is at least an array or a `Ref` in the arguments, it returns an array | ||
(and treats any `Ref` and tuple as 0-dimensional and 1-dimensional arrays, |
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.
Ref
or tuple ?
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.
"any Ref
or tuple as a 0-dimensional or 1-dimensional array, respectively" would be correct, but it would perhaps be clearer to phrase it as "treats any Ref
as a 0-dimensional array of its contents, and any tuple as a 1-dimensional 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.
Fixed
8a4cb44
to
c930cfb
Compare
c930cfb
to
8e3f1d5
Compare
8e3f1d5
to
bfa5477
Compare
bfa5477
to
ffdbbcc
Compare
- If there is at least an array in the arguments, it returns an array | ||
(and treats tuples as 1-dimensional arrays) expanding singleton dimensions. | ||
- If there is at least an array or a `Ref` in the arguments, it returns an array | ||
(and treats any `Ref` as a 0-dimensional array of its contents and any tuple |
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.
missing close paren?
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.
It is in the following line.
ffdbbcc
to
986d575
Compare
Rebased in top of the recent |
LGTM |
Looks like we should define this for |
|
It would be nice to be able to do
|
This is not what |
This implements the idea #18379 (comment) proposed by @stevengj of treating
Ref
s as 0-dimensional arrays in broadcast.