Skip to content

Commit 35cd87c

Browse files
committed
News and documentation
1 parent 90fe112 commit 35cd87c

File tree

4 files changed

+121
-2
lines changed

4 files changed

+121
-2
lines changed

NEWS.md

+4
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ This section lists changes that do not have deprecation warnings.
2828
for `real(z) < 0`, which differs from `log(gamma(z))` by multiples of 2π
2929
in the imaginary part ([#18330]).
3030

31+
* `broadcast` now handles tuples, and treats any argument that is not a tuple
32+
or an array as a "scalar" ([#16986]).
33+
3134
Library improvements
3235
--------------------
3336

@@ -638,6 +641,7 @@ Language tooling improvements
638641
[#16854]: https://github.com/JuliaLang/julia/issues/16854
639642
[#16953]: https://github.com/JuliaLang/julia/issues/16953
640643
[#16972]: https://github.com/JuliaLang/julia/issues/16972
644+
[#16986]: https://github.com/JuliaLang/julia/issues/16986
641645
[#17033]: https://github.com/JuliaLang/julia/issues/17033
642646
[#17037]: https://github.com/JuliaLang/julia/issues/17037
643647
[#17075]: https://github.com/JuliaLang/julia/issues/17075

base/broadcast.jl

+66-1
Original file line numberDiff line numberDiff line change
@@ -271,8 +271,73 @@ end
271271
@inline broadcast_c(f, ::Type{Any}, a...) = f(a...)
272272
@inline broadcast_c(f, ::Type{Array}, As...) = broadcast_t(f, promote_eltype_op(f, As...), As...)
273273

274-
@inline broadcast(f, As...) = broadcast_c(f, containertype(As...), As...)
274+
"""
275+
broadcast(f, As...)
276+
277+
Broadcasts the arrays, tuples and/or scalars `As` to a container of the
278+
appropriate type and dimensions. In this context, anything that is not a
279+
subtype of `AbstractArray` or `Tuple` is considered a scalar. The resulting
280+
container is stablished by the following rules:
281+
282+
- If all the arguments are scalars, it returns a scalar.
283+
- If the arguments are tuples and zero or more scalars, it returns a tuple.
284+
- If there is at least an array in the arguments, it returns an array
285+
(and treats tuples as 1-dimensional arrays) expanding singleton dimensions.
286+
287+
A special syntax exists for broadcasting: `f.(args...)` is equivalent to
288+
`broadcast(f, args...)`, and nested `f.(g.(args...))` calls are fused into a
289+
single broadcast loop.
290+
291+
```jldoctest
292+
julia> A = [1, 2, 3, 4, 5]
293+
5-element Array{Int64,1}:
294+
1
295+
2
296+
3
297+
4
298+
5
299+
300+
julia> B = [1 2; 3 4; 5 6; 7 8; 9 10]
301+
5×2 Array{Int64,2}:
302+
1 2
303+
3 4
304+
5 6
305+
7 8
306+
9 10
307+
308+
julia> broadcast(+, A, B)
309+
5×2 Array{Int64,2}:
310+
2 3
311+
5 6
312+
8 9
313+
11 12
314+
14 15
275315
316+
julia> parse.(Int, ["1", "2"])
317+
2-element Array{Int64,1}:
318+
1
319+
2
320+
321+
julia> abs.((1, -2))
322+
(1,2)
323+
324+
julia> broadcast(+, 1.0, (0, -2.0))
325+
(1.0,-1.0)
326+
327+
julia> broadcast(+, 1.0, (0, -2.0), [1])
328+
2-element Array{Float64,1}:
329+
2.0
330+
0.0
331+
332+
julia> string.(("one","two","three","four"), ": ", 1:4)
333+
4-element Array{String,1}:
334+
"one: 1"
335+
"two: 2"
336+
"three: 3"
337+
"four: 4"
338+
```
339+
"""
340+
@inline broadcast(f, As...) = broadcast_c(f, containertype(As...), As...)
276341

277342
"""
278343
bitbroadcast(f, As...)

doc/manual/arrays.rst

+21
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,27 @@ function elementwise:
526526

527527
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`).
528528

529+
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".
530+
531+
.. doctest::
532+
533+
julia> convert.(Float32, [1, 2])
534+
2-element Array{Float32,1}:
535+
1.0
536+
2.0
537+
538+
julia> ceil.((UInt8,), [1.2 3.4; 5.6 6.7])
539+
2×2 Array{UInt8,2}:
540+
0x02 0x04
541+
0x06 0x07
542+
543+
julia> string.(1:3, ". ", ["First", "Second", "Third"])
544+
3-element Array{String,1}:
545+
"1. First"
546+
"2. Second"
547+
"3. Third"
548+
549+
529550
Implementation
530551
--------------
531552

doc/stdlib/arrays.rst

+30-1
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,13 @@ All mathematical operations and functions are supported for arrays
597597

598598
.. Docstring generated from Julia source
599599
600-
Broadcasts the arrays ``As`` to a common size by expanding singleton dimensions, and returns an array of the results ``f(as...)`` for each position.
600+
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 stablished by the following rules:
601+
602+
* If all the arguments are scalars, it returns a scalar.
603+
* If the arguments are tuples and zero or more scalars, it returns a tuple.
604+
* If there is at least an array in the arguments, it returns an array (and treats tuples as 1-dimensional arrays) expanding singleton dimensions.
605+
606+
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.
601607

602608
.. doctest::
603609

@@ -625,6 +631,29 @@ All mathematical operations and functions are supported for arrays
625631
11 12
626632
14 15
627633

634+
julia> parse.(Int, ["1", "2"])
635+
2-element Array{Int64,1}:
636+
1
637+
2
638+
639+
julia> abs.((1, -2))
640+
(1,2)
641+
642+
julia> broadcast(+, 1.0, (0, -2.0))
643+
(1.0,-1.0)
644+
645+
julia> broadcast(+, 1.0, (0, -2.0), [1])
646+
2-element Array{Float64,1}:
647+
2.0
648+
0.0
649+
650+
julia> string.(("one","two","three","four"), ": ", 1:4)
651+
4-element Array{String,1}:
652+
"one: 1"
653+
"two: 2"
654+
"three: 3"
655+
"four: 4"
656+
628657
.. function:: broadcast!(f, dest, As...)
629658

630659
.. Docstring generated from Julia source

0 commit comments

Comments
 (0)