Skip to content
Related Articles

Related Articles

Improve Article

Broadcasting a function over collections in Julia – broadcast() and broadcast!() Methods

  • Last Updated : 01 Apr, 2020

The broadcast() is an inbuilt function in julia which is used to broadcast the function f over the specified arrays, tuples or collections.

Syntax: broadcast(f, As…)

Parameters:

  • f: Specified function.
  • As: Specified arrays, tuples or collections.

Returns: It returns the results of the process of broadcasting the function over the specified arrays, tuples or collections.

Example:






# Julia program to illustrate 
# the use of broadcast() method
   
# Getting the results of the process
# of broadcasting the function over 
# the specified arrays, tuples or collections
A = [1 3; 5 7; 9 11; 13 15; 17 19]
B = [2, 4, 6, 8, 10]
println(broadcast(+, A, B))
broadcast(-, A, B)

Output:

broadcast!()

The broadcast!() is an inbuilt function in julia which is same as broadcast() function but stores the result of broadcast(f, As…) in the specified dest array.

Syntax: broadcast!(f, dest, As…)

Parameters:

  • f: Specified function.
  • dest: Specified destination array.
  • As: Specified arrays, tuples or collections.

Returns: It returns the results of the process of broadcasting the function over the specified arrays, tuples or collections.

Example:




# Julia program to illustrate 
# the use of broadcast !() method
   
# Getting the results of the process
# of broadcasting the function over 
# the specified arrays, tuples or collections
A = [1; 2]; 
B = [3; 4];
broadcast !(+, B, A)
println(B)
println(A)
broadcast !(+, B, A, (5, 10))
println(B)
println(A)

Output:




My Personal Notes arrow_drop_up
Recommended Articles
Page :