Dot syntax with tuples

In Julia 0.6 the it was possible to do:

julia> x = ones(2), zeros(3,4);

julia> x .+ 3
([4.0, 4.0], [3.0 3.0 3.0 3.0; 3.0 3.0 3.0 3.0; 3.0 3.0 3.0 3.0])

Now in 1.0 this is no longer possible:


julia> x .+ 3
ERROR: MethodError: no method matching +(::Array{Float64,1}, ::Int64)

Is there an elegant syntax to do this in 1.0?

Possibly I would prefer avoiding something like:

julia> ([xi.+3 for xi in x]...,)
([4.0, 4.0], [3.0 3.0 3.0 3.0; 3.0 3.0 3.0 3.0; 3.0 3.0 3.0 3.0])

since I would like x to be either an Array or a Tuple of Arrays.
I thank you!

You can broadcast a broadcasted function:

julia> (x -> x .+ 3).(x)
([4.0, 4.0], [3.0 3.0 3.0 3.0; 3.0 3.0 3.0 3.0; 3.0 3.0 3.0 3.0])

See also https://github.com/JuliaLang/julia/issues/20502, https://github.com/JuliaLang/julia/issues/22622

2 Likes

You can still get the recursive broadcast as 0.6 like this:

julia> +ᵥ(x,y) = x .+ y
+ᵥ (generic function with 1 method)

julia> x = ones(2), zeros(3,4)
([1.0, 1.0], [0.0 0.0 0.0 0.0; 0.0 0.0 0.0 0.0; 0.0 0.0 0.0 0.0])

julia> x .+ᵥ 3
([4.0, 4.0], [3.0 3.0 3.0 3.0; 3.0 3.0 3.0 3.0; 3.0 3.0 3.0 3.0])
2 Likes

Is there any recommended (pretty) way to solve broadcast with tuples like

x=(a=2,b=3); y=rand(10)
f(y,x)=(y+x.a)*x.b
f.(y,x)

which now gives ERROR: ArgumentError: broadcasting over dictionaries and NamedTuples is reserved

you can prevent broadcasting by Ref: f.(y,Ref(x))

2 Likes