Skip to content

Commit

Permalink
Zero-dimensional arrays FAQ (#27772)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
cossio authored and simonbyrne committed Jun 27, 2018
1 parent 0786452 commit 554b13e
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions doc/src/manual/faq.md
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,50 @@ julia> @sync for i in 1:3
1 Foo Bar 2 Foo Bar 3 Foo Bar
```

## Arrays

### What are the differences between zero-dimensional arrays and scalars?

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:

```
julia> A = zeros()
0-dimensional Array{Float64,0}:
0.0
```

In this example, `A` is a mutable container that contains one element, which can
be set by `A[] = 1.0` and retrieved with `A[]`. All zero-dimensional arrays have
the same size (`size(A) == ()`), and length (`length(A) == 1`). In particular,
zero-dimensional arrays are not empty. If you find this unintuitive, here are
some ideas that might help to understand Julia's definition.

* Zero-dimensional arrays are the "point" to vector's "line" and matrix's
"plane". Just as a line has no area (but still represents a set of things), a
point has no length or any dimensions at all (but still represents a thing).
* We define `prod(())` to be 1, and the total number of elements in an array is
the product of the size. The size of a zero-dimensional array is `()`, and
therefore its length is `1`.
* 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.

It is also important to understand the differences to ordinary scalars. Scalars
are not mutable containers (even though they are iterable and define things
like `length`, `getindex`, *e.g.* `1[] == 1`). In particular, if `x = 0.0` is
defined as a scalar, it is an error to attempt to change its value via
`x[] = 1.0`. A scalar `x` can be converted into a zero-dimensional array
containing it via `fill(x)`, and conversely, a zero-dimensional array `a` can
be converted to the contained scalar via `a[]`. Another difference is that
a scalar can participate in linear algebra operations such as `2 * rand(2,2)`,
but the analogous operation with a zero-dimensional array
`fill(2) * rand(2,2)` is an error.

## Julia Releases

### Do I want to use a release, beta, or nightly version of Julia?
Expand Down

0 comments on commit 554b13e

Please sign in to comment.