Mathematical Operations on Arrays in Julia
With the help of Mathematical Operations, we can perform addition, subtraction, multiplication, division, and many more to compute the result between two matrices or arrays. Mathematical operations are a very crucial part of any high-level programming language. In the world of Julia to compete with languages like python and java, Julia is also enabled with the same functionality but have different syntax.
Mathematical Operations :
Addition: This operation helps to add two arrays.
[1 2 3] + [4 5 10] = [5 7 13]
Subtration: This operation helps to subtract two arrays.
[1 2 3] – [4 5 10] = [-3 -3 -7]
Multiplication: This operation helps to multiply two arrays.
[1 2 3] * [4; 5; 10] = [44]
Division: This operation helps to divide two arrays.
[1 2 3] / [4; 5; 10] = [3.1428…]
Addition Operation
We can add two arrays with the help of + operator.
Example of 1D array :
A = [ 1 2 3 ]
B = [ 4 5 10 ]
gfg = A + B
print (gfg)
|
Output:
Example of 2D array:
A = [ 1 2 ; - 1 - 2 ]
B = [ 4 5 ; 10 12 ]
gfg = A + B
print (gfg)
|
Output:
Example of 3D array:
A = cat([ 1 2 3 ], [ - 1 - 2 - 3 ], [ 2 1 4 ], dims = 3 )
B = cat([ 4 5 2 ], [ 10 12 - 5 ], [ - 1 2 1 ], dims = 3 )
gfg = A + B
print (gfg)
|
Output:
Subtraction operation
We can subtract two arrays with the help of – operator.
Example of 1D array:
A = [ 1 2 3 ]
B = [ 4 5 10 ]
gfg = A - B
print (gfg)
|
Output:
Example of 2D array:
A = [ 1 2 ; - 1 - 2 ]
B = [ 4 5 ; 10 12 ]
gfg = A - B
print (gfg)
|
Output:
Example of 3D array:
A = cat([ 1 2 3 ], [ - 1 - 2 - 3 ], [ 2 1 4 ], dims = 3 )
B = cat([ 4 5 2 ], [ 10 12 - 5 ], [ - 1 2 1 ], dims = 3 )
gfg = A - B
print (gfg)
|
Output:
Multiplication operation
We can multiply two arrays with the help of * operator.
Example of 1D array:
A = [ 1 2 3 ]
B = [ 4 ; 5 ; 10 ]
gfg = A * B
print (gfg)
|
Output:
Example of 2D array:
A = [ 1 2 ; - 1 - 2 ]
B = [ 4 5 ; 10 12 ]
gfg = A * B
print (gfg)
|
Output:
Example of 3D array:
A = cat([ 1 2 3 ], [ - 1 - 2 - 3 ], [ 2 1 4 ], dims = 3 )
B = cat([ 4 5 2 ], [ 10 12 - 5 ], [ - 1 2 1 ], dims = 3 )
gfg = A * B
print (gfg)
|
Output:
Division operation
We can division two arrays with the help of / operator.
Example of 1D array:
B = [ 1 2 3 ]
A = [ 4 5 10 ]
gfg = A / B
print (gfg)
|
Output:
Example of 2D array:
B = [ 1 2 ; 1 2 ]
A = [ 4 5 ; 10 12 ]
gfg = B / A
print (gfg)
|
Output:
Example of 3D array:
A = cat([ 1 2 3 ], [ - 1 - 2 - 3 ], [ 2 1 4 ], dims = 3 )
B = cat([ 4 5 2 ], [ 10 12 - 5 ], [ - 1 2 1 ], dims = 3 )
gfg = B / A
print (gfg)
|
Output:
Last Updated :
26 May, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...