Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Julia: what's the difference between map and broadcast?

Tags:

julia

I have found some discussions on Julia Users group but they were too technical for me.

I would like to know what are the criteria to choose between the two.

I am following the JuliaBox tutorial but it doesn't explain much. Thanks

like image 704
Pigna Avatar asked Oct 19 '18 12:10

Pigna


People also ask

What is broadcasting in Julia and how to use it?

Broadcasting in Julia helps to write performant code. Think of an application where you want to update an iterable such as an array element-wise, the most straight forward approach is iterating through each entry of the array using a for loop and updating them. But, this can be made more efficient by using Julia’s built-in broadcasting operators.

What are methods in Julia?

The definition of one of these behaviors is termed as a method. In Julia, each time a function is called, the most appropriate method is applied based on it’s arguments. Applying a method when calling a function is known as dispatch. Object-oriented languages ​​traditionally only consider the first argument in dispatch.

How to broadcast on arrays using dot operator in Julia?

We can use the dot operator (.+, .-, .*, .=) to implement the same operations we did with the broadcast () function Julia provides us with many functions that can broadcast on arrays, applying various operations. And we can implement these functions using the dot operator as shown below.

What is @Julia?

Julia combines the implementation of scalar environments such as R and Python with the speed of dynamic programming languages like Java and C++ to provide a systematic solution for big data and analytics problems. As every programming language has its environment to develop dynamic models.


2 Answers

Both methods apply a function to their arguments. The difference between the two is how they treat multidimensional arrays.

map tries to zip the arrays column first, creating an iterator of tuples, which are used as arguments to the given function. Thus

julia> map(+, [1,2,3,4], [10, 20, 30, 40])
4-element Array{Int64,1}:
 11
 22
 33
 44

while

julia> map(+, [1,2,3,4], [10 20; 30 40])
4-element Array{Int64,1}:
 11
 32
 23
 44

Notice the element pairing when changing the layout of the second array. On the other hand, broadcast does an element-wise application of the given function. If the two array arguments have the same dimensions, the pairing of elements is similar to map.

julia> broadcast(+, [1,2,3,4], [10, 20, 30, 40])
4-element Array{Int64,1}:
 11
 22
 33
 44

Otherwise, broadcast checks if the two arrays match in at least one dimension and that the other dimension of at least one array is equal to 1. If this condition is satisfied, broadcast extends the arrays to a common size by duplicating the array data along the dimension equal to 1. This is how you get

julia> broadcast(+, [1, 2, 3, 4], [10 20 30 40])
4×4 Array{Int64,2}:
 11  21  31  41
 12  22  32  42
 13  23  33  43
 14  24  34  44
like image 158
Valeriu Avatar answered Nov 08 '22 16:11

Valeriu


map and broadcast are different when dealing with multiple collections of different dimensions. While broadcast will try to cast all the objects to a common dimension, map will directly apply the given function elementwise.

julia> map(+, 1, [2,2,2])
1-element Array{Int64,1}:
 3

julia> broadcast(+, 1, [2,2,2])
3-element Array{Int64,1}:
 3
 3
 3

the broadcast example has the same result as map(+, [1,1,1], [2,2,2]).

Also note the behavior of broadcast when failing at finding a common dimension between two arguments:

julia> broadcast(+, [1,2,3], [2 2; 2 0])
ERROR: DimensionMismatch("arrays could not be broadcast to a common size")
Stacktrace:
 [1] _bcs1 at ./broadcast.jl:439 [inlined]
 [2] _bcs at ./broadcast.jl:433 [inlined]
 [3] broadcast_shape at ./broadcast.jl:427 [inlined]
 [4] combine_axes at ./broadcast.jl:422 [inlined]
 [5] instantiate at ./broadcast.jl:266 [inlined]
 [6] materialize at ./broadcast.jl:748 [inlined]
 [7] broadcast(::typeof(+), ::Array{Int64,1}, ::Array{Int64,2}) at ./broadcast.jl:702
 [8] top-level scope at none:0

julia> map(+, [1,2,3], [2 2; 2 0])
3-element Array{Int64,1}:
 3
 4
like image 27
gdmsl Avatar answered Nov 08 '22 16:11

gdmsl