-
-
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
Zero-dimensional arrays FAQ #27772
Zero-dimensional arrays FAQ #27772
Conversation
Adding some documentation about zero-dimensional arrays. Some discussion on Slack: https://julialang.slack.com/archives/C6GHSTN4T/p1529875426000022 I copied/pasted some comments by more knowledgeable people, with minor edits. Questions about 0-dimensional arrays arise often. And I think they are a bit unintuitive. Some documentation might help. https://discourse.julialang.org/t/what-is-the-reason-for-a-zero-dimensional-array/7175 https://discourse.julialang.org/t/why-have-0-dimensional-arrays/797 Hopefully this clarifies the concept for others as well.
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.
Very nice write up!
Thanks to @mbauman , @nalimilan , @KristofferC |
doc/src/manual/faq.md
Outdated
|
||
### Zero-dimensional arrays | ||
|
||
Zero-dimensional arrays are arrays of the form `Array{T,0}`. They behave similar to scalars, but there are important differences. They deserve a special mention because they are a special case which makes logical sense given the generic definition of arrays, but might be a bit unintuitive at first. The following line defines a zero-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.
Break lines at 92 chars.
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.
Done
Break long lines
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.
Break long lines
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.
Done
doc/src/manual/faq.md
Outdated
|
||
### Zero-dimensional arrays | ||
|
||
Zero-dimensional arrays are arrays of the form `Array{T,0}`. They behave similar to scalars, but there are important differences. They deserve a special mention because they are a special case which makes logical sense given the generic definition of arrays, but might be a bit unintuitive at first. The following line defines a zero-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.
Done
There are white space issues. |
What exactly? What do I have to do? |
Run |
He's editing via GitHub, though, which does indeed make this difficult to see. It's not ideal, but we have this rule since many contributors' editors do automatically remove whitespace in any file they touch… leading to unrelated changes, conflicts, and churn. |
doc/src/manual/faq.md
Outdated
@@ -732,6 +732,47 @@ julia> @sync for i in 1:3 | |||
1 Foo Bar 2 Foo Bar 3 Foo Bar | |||
``` | |||
|
|||
## Arrays | |||
|
|||
### Zero-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.
Now that this lives in a FAQ, let's make this a question.
Yep, I was editing in Github directly. Anyway I think I addressed the issues. Let me know if something else is needed. |
I think this is a great write up. I do worry, though, that it makes zero-dimensional arrays seem too appealing, and that this may lead to people using them in places where an alternative might work better. For instance, in many cases, a |
Updated question to focus on differences between scalars and zero-dimensional arrays.
Well, ideally Refs would also implement all the methods to make them behave largely like 0-dimensional arrays themselves (#26988). But, I think you're right — typically folks don't need or run into them, which is precisely why they cause confusion when it does occur, and which is why it's nice to have it as a FAQ instead of a part of the narrative manual. They do mostly just "fall out" of an implementation in a consistent manner; a good example is We could perhaps emphasize further the fact that 0-dimensional arrays are first and foremost containers and do not behave like the element they wrap. Furthermore, I had been also thinking about calling out the fact that we do not allow 0-dimensional arrays to participate in the linear algebra of matrices. Thus, while you can broadcast Just checking in here: how you doing, @cossio? Getting revision fatigue yet? Let us know before you get too frustrated with us. I know incremental reviews can be a challenge. :) |
Doing fine @mbauman. Unfortunately I don't have the necessary knowledge to contribute much to this discussion. |
more differences between scalars and 0-dim arrays. 0-dim arrays cannot participate in linear algebra ops.
I think this is good as is. If someone wants to tweak the language to make zero-dimensional arrays sound less appealing after this is merged, they can do that. The new text addresses a common point of confusion well, so let's just merge it. |
Why CI fails? I don't understand the error message:
|
I believe there is a clash between the header you are adding and some existing |
* Zero-dimensional arrays don't natively have any dimensions into which you | ||
index -- they’re just `A[]`. We can apply the same "trailing one" rule for them | ||
as for all other array dimensionalities, so you can indeed index them as | ||
`A[1]`, `A[1,1]`, etc. |
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 A[1,1]
would produce an error
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 doesn't:
julia> A = fill(1)
0-dimensional Array{Int64,0}:
1
julia> A[1,1]
1
Same rule that allows indexing a vector like an n×1
matrix (or n×1×1
three-tensor):
julia> v = rand(3)
3-element Array{Float64,1}:
0.7636187688715259
0.6218914012928478
0.22322554192055066
julia> v[3,1]
0.22322554192055066
julia> v[3,1,1]
0.22322554192055066
Locally the build passes with that PR after rebasing on master (now that #27769 is merged). |
@nalimilan Should I do something? |
You could rebase on master and force-push so that CI is run again, but I don't think it's possible from GitHub, so we can just merge as-is. |
Thanks! |
awesome! |
@StefanKarpinski The bulleted list appears broken: https://docs.julialang.org/en/latest/manual/faq/#Arrays-1. I guess to fix that I would have to remove the line breaks in the source markdown, but that's agains the max-chars per line rule. How do I fix this? |
This should fix it: #27840. Seems that the markdown parser doesn't do a correct job here? |
@KristofferC Thanks! |
`A[1]`, `A[1,1]`, etc. | ||
|
||
It is also important to understand the differences to ordinary scalars. Scalars | ||
are not mutable containers (even though they are iterable and define things |
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.
Suggestion: "...(even though some scalar types such as Number
are iterable..."
(I'm pretty sure broadcasting, map
/reduce
/filter
of arrays of scalars, etc doesn't rely on this being defined for all scalar types?)
Adding some documentation about zero-dimensional arrays. Some discussion on Slack: https://julialang.slack.com/archives/C6GHSTN4T/p1529875426000022 I copied/pasted some comments by more knowledgeable people, with minor edits. Questions about 0-dimensional arrays arise often. And I think they are a bit unintuitive. Some documentation might help. https://discourse.julialang.org/t/what-is-the-reason-for-a-zero-dimensional-array/7175 https://discourse.julialang.org/t/why-have-0-dimensional-arrays/797 Hopefully this clarifies the concept for others as well.
Adding some documentation about zero-dimensional arrays. Some discussion on Slack:
https://julialang.slack.com/archives/C6GHSTN4T/p1529875426000022
I copied/pasted some comments by more knowledgeable people, with minor edits.
Questions about 0-dimensional arrays arise often. And I think they are a bit unintuitive. Some documentation might help.
https://discourse.julialang.org/t/what-is-the-reason-for-a-zero-dimensional-array/7175
https://discourse.julialang.org/t/why-have-0-dimensional-arrays/797
Hopefully this clarifies the concept for others as well.
Supersedes #27765.