Broadcasting

A function f can be broadcast over all elements of an array (or matrix) by using the dot notation f.(matrix); for example:

arr = [1.0, 2.0, 3.0] 
sin.(arr) #> 
3-element Array{Float64,1}:#  0.8414709848078965 
#  0.9092974268256817 
#  0.1411200080598672  

Here is another example:

f(x,y) = x + 7y 
f.(pi, arr)  
#> 3-element Array{Float64,1}: 
# 10.141592653589793 
# 17.141592653589793 
# 24.141592653589793 

Broadcasting is very useful in Julia to write compact expressions with arrays and matrices.

Get Julia 1.0 Programming now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.