broadcast_function

broadcast_function(f)

Returns a function broadcast_f such that broadcast_function(f)(As...) === broadcast(f, As...). Most useful in the form const broadcast_f = broadcast_function(f).

Examples

julia> foo = [1 2];
julia> bar = [1; 2];
julia> function f(x,y)
                    -(x+y)
            end;
julia> g = broadcast_function(f);
julia> baz = g(foo, bar)
2x2 Array{Int64,2}:
 -2     -3
 -3     -4
julia> g(foo, [3:1:6])
4x2 Array{Int64,2}:
 -4     -5
 -5     -6
 -6     -7
 -7     -8
julia> f(x) = 2 * x;
julia> broadcast_f = broadcast_function(f);
julia> broadcast_f([1, 2, 3])
3-element Array{Int64,1}:
 2
 4
 6

In this example, the broadcast_function is used to create broadcast_f, which is a function equivalent to broadcast(f, As...). The created broadcast_f function can then be used to apply the broadcasting operation on an input array [1, 2, 3] using the original function f(x).

julia> g(x, y) = x + y;
julia> broadcast_g = broadcast_function(g);
julia> broadcast_g([1, 2, 3], [10, 20, 30])
3-element Array{Int64,1}:
 11
 22
 33

In this example, broadcast_function is used to create broadcast_g, which is a function equivalent to broadcast(g, As...). The broadcast_g function is then used to apply the broadcasting operation on two input arrays [1, 2, 3] and [10, 20, 30] using the original function g(x, y).

Common mistake example:

julia> h(x) = x^2;
julia> broadcast_h = broadcast_function(h);
julia> broadcast_h(1, 2, 3)
ERROR: MethodError: no method matching h(::Int64, ::Int64, ::Int64)

In this example, the broadcast_function is used to create broadcast_h, but the original function h(x) does not accept multiple arguments. It's important to ensure that the original function can handle broadcasting operations with multiple input arrays or values.

See Also

User Contributed Notes

Add a Note

The format of note supported is markdown, use triple backtick to start and end a code block.

*Required Field
Details

Checking you are not a robot: