Skip to content

Commit

Permalink
News and docs for JuliaLang#18965
Browse files Browse the repository at this point in the history
  • Loading branch information
pabloferz committed Oct 22, 2016
1 parent ccacee6 commit bc247a4
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 9 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ This section lists changes that do not have deprecation warnings.
* `broadcast` now handles tuples, and treats any argument that is not a tuple
or an array as a "scalar" ([#16986]).

* `broadcast` now treats `Ref` arguments as 0-dimensional arrays ([#18965]).

Library improvements
--------------------

Expand Down Expand Up @@ -688,4 +690,5 @@ Language tooling improvements
[#18339]: https://github.com/JuliaLang/julia/issues/18339
[#18346]: https://github.com/JuliaLang/julia/issues/18346
[#18839]: https://github.com/JuliaLang/julia/issues/18839
[#18965]: https://github.com/JuliaLang/julia/issues/18965
[#19018]: https://github.com/JuliaLang/julia/issues/19018
16 changes: 11 additions & 5 deletions base/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -265,15 +265,16 @@ end
"""
broadcast(f, As...)
Broadcasts the arrays, tuples and/or scalars `As` to a container of the
Broadcasts the arrays, tuples, `Ref` and/or scalars `As` to a container of the
appropriate type and dimensions. In this context, anything that is not a
subtype of `AbstractArray` or `Tuple` is considered a scalar. The resulting
subtype of `AbstractArray`, `Ref` or `Tuple` is considered a scalar. The resulting
container is established by the following rules:
- If all the arguments are scalars, it returns a scalar.
- If the arguments are tuples and zero or more scalars, it returns a tuple.
- 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,
respectively) expanding singleton dimensions.
A special syntax exists for broadcasting: `f.(args...)` is equivalent to
`broadcast(f, args...)`, and nested `f.(g.(args...))` calls are fused into a
Expand Down Expand Up @@ -315,11 +316,16 @@ julia> abs.((1, -2))
julia> broadcast(+, 1.0, (0, -2.0))
(1.0,-1.0)
julia> broadcast(+, 1.0, (0, -2.0), [1])
julia> broadcast(+, 1.0, (0, -2.0), Ref(1))
2-element Array{Float64,1}:
2.0
0.0
julia> (+).([[0,2], [1,3]], Ref{Vector{Int}}([1,-1]))
2-element Array{Array{Int64,1},1}:
[1,1]
[2,2]
julia> string.(("one","two","three","four"), ": ", 1:4)
4-element Array{String,1}:
"one: 1"
Expand Down
2 changes: 1 addition & 1 deletion doc/manual/arrays.rst
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ function elementwise:

Elementwise operators such as ``.+`` and ``.*`` perform broadcasting if necessary. There is also a :func:`broadcast!` function to specify an explicit destination, and :func:`broadcast_getindex` and :func:`broadcast_setindex!` that broadcast the indices before indexing. Moreover, ``f.(args...)`` is equivalent to ``broadcast(f, args...)``, providing a convenient syntax to broadcast any function (:ref:`man-dot-vectorizing`).

Additionally, :func:`broadcast` is not limited to arrays (see the function documentation), it also handles tuples and treats any argument that is not an array or a tuple as a "scalar".
Additionally, :func:`broadcast` is not limited to arrays (see the function documentation), it also handles tuples and treats any argument that is not an array, tuple or `Ref` as a "scalar".

.. doctest::

Expand Down
11 changes: 8 additions & 3 deletions doc/stdlib/arrays.rst
Original file line number Diff line number Diff line change
Expand Up @@ -640,11 +640,11 @@ All mathematical operations and functions are supported for arrays

.. Docstring generated from Julia source
Broadcasts the arrays, tuples and/or scalars ``As`` to a container of the appropriate type and dimensions. In this context, anything that is not a subtype of ``AbstractArray`` or ``Tuple`` is considered a scalar. The resulting container is established by the following rules:
Broadcasts the arrays, tuples, ``Ref`` and/or scalars ``As`` to a container of the appropriate type and dimensions. In this context, anything that is not a subtype of ``AbstractArray``\ , ``Ref`` or ``Tuple`` is considered a scalar. The resulting container is established by the following rules:

* If all the arguments are scalars, it returns a scalar.
* If the arguments are tuples and zero or more scalars, it returns a tuple.
* 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, respectively) expanding singleton dimensions.

A special syntax exists for broadcasting: ``f.(args...)`` is equivalent to ``broadcast(f, args...)``\ , and nested ``f.(g.(args...))`` calls are fused into a single broadcast loop.

Expand Down Expand Up @@ -685,11 +685,16 @@ All mathematical operations and functions are supported for arrays
julia> broadcast(+, 1.0, (0, -2.0))
(1.0,-1.0)

julia> broadcast(+, 1.0, (0, -2.0), [1])
julia> broadcast(+, 1.0, (0, -2.0), Ref(1))
2-element Array{Float64,1}:
2.0
0.0

julia> (+).([[0,2], [1,3]], Ref{Vector{Int}}([1,-1]))
2-element Array{Array{Int64,1},1}:
[1,1]
[2,2]

julia> string.(("one","two","three","four"), ": ", 1:4)
4-element Array{String,1}:
"one: 1"
Expand Down

0 comments on commit bc247a4

Please sign in to comment.