Skip to content
Related Articles

Related Articles

Improve Article

Broadcasting across arrays in Julia

  • Last Updated : 16 Jul, 2021

Julia is a high performance, dynamic programming language that has a high-level syntax. It is free for everyone to use and easy to learn. Julia is used generally for data analysis and visualization purposes.

A grid that contains objects in multiple dimensions is called a Multi-dimensional Array. It can contain any type of object but for most computational purposes Int or Float type objects must be present. It is comparatively easier to implement Arrays in Julia than other computational languages. Arrays can be written in the code in an understandable manner and the compiler takes care of the implementation which is performance and time-efficient. In Julia, a custom array type can also be defined by inheriting from AbstractArray.

Broadcasting across Arrays

Sometimes it is very useful to apply operations on only some elements of an array, like adding a vector of two elements to the array. But it can be inefficient if it is done by increasing the size of the vector to the array’s size, especially for arrays with a large number of elements and more dimensions. 

Julia




# Creating two arrays with random elements
a = rand(2, 1);
b = rand(2, 4);
 
# Repeat function repeats the elements,
# specified number of times.
# Adding vector 'a' to 'b' by increasing the size of 'a'
repeat(a, 1, 4) + b

Output: 



To tackle this problem, Julia provides the broadcast() function which increases the size of a singleton dimension array to the array we want to operate on and applies the function we provide, without using any extra memory. Hence, for large arrays, memory is not wasted. The function is valid not only for arrays but other structures too.

The arguments we can pass in the broadcast() function are as follows:

  1. Operator/function
  2. Array to be added
  3. Array to be added on

Applying the function on a 1D array: 

Julia




# Creating two 1D arrays of different sizes
A = [1];
B = [5,6,7,8];
 
# Adding 'A' to 'B' by increasing it's size
# We use the addition operator in the broadcast function
broadcast(+, A, B)

Applying the function on a 2D array:

Julia






# Applying the broadcast function
# to perform addition on a 2D array
A = [1 2];  
B = [5 6; 7 8; 9 10; 11 12]; 
broadcast(+, A, B)

Applying the function on a 3D array:

Julia




# Applying the broadcast function
# to perform addition on a 3D array
A = [2 3]; 
B = cat([4 5;6 7], [8 9;10 11],
                   [12 13;14 15], dims = 3);  
broadcast(+, A, B)

Using dot operator

We can use the dot operator (.+, .-, .*, .=) to implement the same operations we did with the broadcast() function

Using the dot operator on a 1D array:

Julia




# Equating all elements of the 1D array to the element '7'
[1, 2, 3, 4].= [7]

  

Using the dot operator on a 2D array:



Julia




# Implementing addition, multiplication and
# subtraction operations to broadcast to a 2D array
[1,2].+[1 2 3; 4 5 6]  
[1,2].*[1 2 3; 4 5 6]  
[1,2].-[1 2 3; 4 5 6]

Using the dot operator on a 3D array:

Julia




# Implementing addition operator to broadcast to a 3D array
cat([4 5; 6 7] , [8 9; 10 11] , [12 13; 14 15] , dims = 3).+[1, 2]

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.

Julia




# Broadcasting to create strings with additional elements to a 1D array
string.(1:3, ".", ["ONE", "TWO", "THREE"])

Julia




# Broadcasting to convert elements
# of a 2D array to the type Float32
convert.(Float32, [1 2 3; 4 5 6])

Julia




#  Broadcasting to calculate celing
# of the decimal elements in a 3D array
ceil.((UInt8,), cat([4 5; 6 7] ,
                    [8 9; 10 11] ,
                    [12 13; 14 15] , dims = 3))




My Personal Notes arrow_drop_up
Recommended Articles
Page :