Open In App

Mapping over Matrices in Julia

Julia is termed to be a fresh approach to Parallel Computing. Julia is a high-level and dynamic programming language for interactive use. 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.

Julia Command Shell

As every programming language has its environment to develop dynamic models. Apparently Julia also features an interactive command shell REPL (REPL stands for read-eval-print-loop).



Example: Basic function
Input:

Julia> a(x) = 2x^3+1; f(x, y) = 1 + a(x)y
Julia> println(“Hello World!”, “ I’m “, f(2, 1), “Welcome to Julia!”)

Here, We’ve declared a(x) where x is the parameter of the function f(x, y). We have allocated an operation  (2x^3+1) to a(x). Hence ” ^ ” operator performs multiplication. Now there is a function f(x, y) which gives us a result by performing a specific operation declared when parameter values are passed.



Output:

Hello World! I’m 18 Welcome to Julia!

Mapping over Matrices

Mapping the matrices in Julia is nothing but performing a function or an operation to each and every element in the matrix.

For example, Let us create a matrix in Julia and understand the terms in a lucid way.

julia> variable = [1 2 3 4 5 6]
1×6 Array{Int64, 2}:
1  2  3  4  5  6

Here, we have created a 1×6 matrix(consists of 1 row and 6 columns). We can also create matrices of different dimensions and with varied range of datatypes(i.e float, int, double) without specifying the type while declaring the value.

In the above picture, a matrix of order 3×4 of the integer data type is created.

Performing the map function:

In the above picture, the code is executed in the Julia command line where I have created a matrix of order 3×3.

We have performed the map function in the above picture to the matrix c. Here x is a variable which stores each and every value present in the matrix c.

Performing the map function over two matrices:

In the above-explained examples, we have seen operations performed on a single matrix(i.e, c). Now let us understand the mapping technique by performing the operations on two different matrices.

julia> f = [1 2 3; 1 2 3; 1 2 3]
3×3 Array{Int64, 2}:
 1  2  3
 1  2  3
 1  2  3

Created a matrix ‘ f ‘of order 3×3.

julia> c = [2 1 1; 1 1 2; 2 1 2]
3×3 Array{Int64, 2}:
 2  1  1
 1  1  2
 2  1  2

Created a matrix ‘ c ‘of order 3×3.

julia> map(x -> f*x*2, c)
3×3 Array{Array{Int64, 2}, 2}:
 [4 8 12; 4 8 12; 4 8 12]  [2 4 6; 2 4 6; 2 4 6]  [2 4 6; 2 4 6; 2 4 6]
 [2 4 6; 2 4 6; 2 4 6]     [2 4 6; 2 4 6; 2 4 6]  [4 8 12; 4 8 12; 4 8 12]
 [4 8 12; 4 8 12; 4 8 12]  [2 4 6; 2 4 6; 2 4 6]  [4 8 12; 4 8 12; 4 8 12]


The final executed output image.

Conclusion:

Hence, we now got to know what is a mapping function in Julia and what operations are performed and the executed using this function in Julia. We also executed the function to map over the matrices.

Article Tags :