Broadcasting in Julia

By using Ref(), it protects the object inside it from being iterated over. Supoose we define the following function fun. See the examples below that demonstrate how broadcasting works: fun(a,x)=a.-x Case 1: fun.([1,2], [3,4]) Note that without Ref(), both arguments of function fun are interated over from broadcasting. This implies that each element within each argument will be computed element-wise. ([1-3, 2-4]) 2-element Vector{Int64}: -2 -2 Case 2: fun.([1,2], Ref([3,4])) Note that Ref() protects [3,4]....

May 11, 2023 · 1 min · 185 words · Me

Map and broadcast

Map map(f, c...) applies function f to each element of collection c that enter as the the second argument of the map function. Below two lines of codes produce same output. This means that map also acts as a for loop. map(x->x+2, 1:10) [ x + 2 for x=1:10] Output: 10-element Vector{Int64}: 3 4 5 6 7 8 9 10 11 12 The function that enters the first argument of map could take multiple arguments; it could be more than one argument....

April 5, 2023 · 2 min · 262 words · Me