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

inconsistent zero-dimensional array operations #30825

Closed
abraunst opened this issue Jan 24, 2019 · 6 comments
Closed

inconsistent zero-dimensional array operations #30825

abraunst opened this issue Jan 24, 2019 · 6 comments
Labels
domain:broadcast Applying a function over a collection

Comments

@abraunst
Copy link
Contributor

abraunst commented Jan 24, 2019

Zero dimensional arrays get converted to scalar on some operations. Is this intended behavior? If yes, it seems undocumented* (and no so consistent IMHO)...

[*] I've found some discussion about zero-dim arrays in faq.md, but no reference to this.

julia> x=ones()
0-dimensional Array{Float64,0}:
1.0

julia> x .= 1
0-dimensional Array{Float64,0}:
1.0

julia> x .*= 1
0-dimensional Array{Float64,0}:
1.0

julia> x .* 1
1.0

julia> 1x
1.0

julia> 1*x
1.0

julia> x+x
2.0                                                                                                                                                                                                                                                                               
@tomaklutfu
Copy link
Contributor

I think there is consistent behaviour because rhs always treats 0-dim as scalar. The difference is the first 2 are broadcast! returning 0-dim x, while the others return just the operation results whether the function broadcast or +. * treating x scalar.

@abraunst
Copy link
Contributor Author

I think there is consistent behaviour because rhs always treats 0-dim as scalar. The difference is the first 2 are broadcast! returning 0-dim x, while the others return just the operation results whether the function broadcast or +. * treating x scalar.

I mean inconsistency wrt arrays of higher dimension. I was expecting to need to explicitly use x[] to convert to scalar.

@mbauman
Copy link
Sponsor Member

mbauman commented Jan 24, 2019

Yeah, this was an intentional design choice specifically for broadcasting, arising from the fact that ndims(1) == 0 and we wanted 1 .+ 1 == 2. It's definitely the kind of thing that I generally dislike, but I made an exception in this case. I still do wonder how much we would have seen complaints if 1 .+ 1 == fill(2).

See also #28866.

@mbauman mbauman added the domain:broadcast Applying a function over a collection label Jan 24, 2019
@abraunst
Copy link
Contributor Author

Thanks for the explanations. I'll close this.

@pablosanjose
Copy link
Contributor

As commented by Andreas Peter on Slack, this makes the following behavior inconsistent

julia> conj(reshape([1]))
0-dimensional Array{Int64,0}:
1

julia> conj(reshape([1 + 1im]))
1 - 1im

The first does not trigger the broadcast machinery due to a conj(x::Array{<:Real}) = x fastpath.

I think we should keep this open.

@mbauman
Copy link
Sponsor Member

mbauman commented Jun 10, 2019

The disconnect there is slated to be fixed in #32122. The bigger broadcast design issue is now being discussed in #28866.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
domain:broadcast Applying a function over a collection
Projects
None yet
Development

No branches or pull requests

4 participants