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

Zero-dimensional arrays FAQ #27772

Merged
merged 7 commits into from
Jun 27, 2018
Merged

Zero-dimensional arrays FAQ #27772

merged 7 commits into from
Jun 27, 2018

Conversation

cossio
Copy link
Contributor

@cossio cossio commented Jun 25, 2018

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.

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.
Copy link
Member

@StefanKarpinski StefanKarpinski left a 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!

@cossio
Copy link
Contributor Author

cossio commented Jun 25, 2018

Thanks to @mbauman , @nalimilan , @KristofferC


### 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:
Copy link
Member

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Break long lines
Copy link
Contributor Author

@cossio cossio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Break long lines

Copy link
Contributor Author

@cossio cossio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done


### 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:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@StefanKarpinski
Copy link
Member

There are white space issues.

@cossio
Copy link
Contributor Author

cossio commented Jun 25, 2018

What exactly? What do I have to do?

@StefanKarpinski
Copy link
Member

Run make check-whitespace and it will report that certain lines have trailing whitespace. Get rid of the trailing whitespace, get make check-whitespace passing and then push the fixed version.

@mbauman
Copy link
Member

mbauman commented Jun 25, 2018

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.

@@ -732,6 +732,47 @@ julia> @sync for i in 1:3
1 Foo Bar 2 Foo Bar 3 Foo Bar
```

## Arrays

### Zero-dimensional arrays
Copy link
Member

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.

@cossio
Copy link
Contributor Author

cossio commented Jun 25, 2018

Yep, I was editing in Github directly. Anyway I think I addressed the issues. Let me know if something else is needed.

@garrison
Copy link
Member

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 Ref would communicate intent more clearly (as mentioned on Discourse). In short: I think this write up leaves out the following: why might one not want to use zero-dimensional arrays in a given circumstance? Do they exist mainly for conceptual completeness, or are there real-world use cases where they should be encouraged?

Updated question to focus on differences between scalars and zero-dimensional arrays.
@mbauman
Copy link
Member

mbauman commented Jun 25, 2018

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 view(A, 1) which provides a view into the first element slot of an array. Returning an unwrapped scalar would no longer be a view. I'd wager they're most likely obtained by accidentally forgetting args in zeros() or Array{Int}(undef) or some such.

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 rand(3,3) .* fill(2), we have not defined the multiplication rand(3,3) * fill(2).

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. :)

@cossio
Copy link
Contributor Author

cossio commented Jun 25, 2018

Doing fine @mbauman. Unfortunately I don't have the necessary knowledge to contribute much to this discussion.

cossio added 2 commits June 25, 2018 22:51
more differences between scalars and 0-dim arrays. 
0-dim arrays cannot participate in linear algebra ops.
@StefanKarpinski
Copy link
Member

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.

@simonbyrne simonbyrne added the docs This change adds or pertains to documentation label Jun 25, 2018
@cossio
Copy link
Contributor Author

cossio commented Jun 26, 2018

Why CI fails? I don't understand the error message:

!! 'Arrays' is not unique. [src/manual/mathematical-operations.md]

@fredrikekre
Copy link
Member

I believe there is a clash between the header you are adding and some existing ## Arrays header in the manual. Lets wait for #27769 to resolve that.

* 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.
Copy link
Contributor

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

Copy link
Member

@StefanKarpinski StefanKarpinski Jun 27, 2018

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

@nalimilan nalimilan closed this Jun 27, 2018
@nalimilan nalimilan reopened this Jun 27, 2018
@nalimilan
Copy link
Member

Locally the build passes with that PR after rebasing on master (now that #27769 is merged).

@cossio
Copy link
Contributor Author

cossio commented Jun 27, 2018

@nalimilan Should I do something?

@nalimilan
Copy link
Member

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.

@simonbyrne simonbyrne merged commit 554b13e into JuliaLang:master Jun 27, 2018
@simonbyrne
Copy link
Contributor

Thanks!

@cossio
Copy link
Contributor Author

cossio commented Jun 27, 2018

awesome!

@cossio cossio deleted the patch-3 branch June 27, 2018 21:45
@cossio
Copy link
Contributor Author

cossio commented Jun 28, 2018

@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?

@KristofferC
Copy link
Member

This should fix it: #27840.

Seems that the markdown parser doesn't do a correct job here?

@cossio
Copy link
Contributor Author

cossio commented Jun 28, 2018

@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
Copy link
Member

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?)

jrevels pushed a commit that referenced this pull request Jul 2, 2018
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs This change adds or pertains to documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants