Python Arrays Mathematical Operations

It is possible to perform various mathematical operations like addition, subtraction, division, etc. on the elements of an array. Also, the functions of 'math' module can be applied to the elements of the array. For example, to add the value 5 to every element of an array, we can write:

arr = array([10, 20, 30.5, -40])

arr = arr+5

CopiedCopy Code

from numpy import * 
arr = array([10, 20, 30.5, -40])
arr = arr+5
print(arr)

In the same way, we can perform addition, subtraction, multiplication, division etc. operations on two different arrays as:

arr1 = array([10, 20, 30.5, -40])

arr2 = array([1, 2, 3, 4])

arr3 = arr1 - arr2

CopiedCopy Code

from numpy import * 
arr1 = array([10, 20, 30.5, -40]) 
arr2 = array([1, 2, 3, 4]) 

CopiedCopy Code

arr3 = arr1 - arr2
print(arr3)

So, to add two arrays 'a' and 'b', we can simply write a+b. Similarly, to divide one array 'a' with another array 'b', we can write a/b. These kinds of operations are called vectorized operations since the entire array (or vector) is processed just like a variable.

Vectorized operations

Vectorized operations are important because of two reasons:

Vectorized operations are faster. Adding two arrays in the form of a+b is much faster than taking the corresponding elements of both the arrays and then adding them.

Vectorized operations are syntactically clearer. To add two arrays, we can simply write a+b. This is clearer than using loops and iterating through all the elements of the arrays while adding them.

Vectorized operations provide compact code.

We can apply mathematical functions like sin(), cos(), sqrt(), exp(), abs(), etc. on the elements of the array. All these functions are re-defined in numpy module.

Function Meaning
sin(arr) Calculates sine value of each element in the array 'arr'.
cos(arr) Calculates cosine value of each element in the array 'arr'.
tan(arr) Calculates tangent value of each element in the array 'arr'.
arcsin(arr) Calculates sine inverse value of each element in the array 'arr'.
arcos(arr) Calculates cosine inverse value of each element in the array 'arr'.
arctan(arr) Calculates tangent inverse value of each element in the array 'arr'.
log(arr) Calculates natural logarithmic value of each element in the array 'arr'.
abs(arr) Calculates absolute value of each element in the array 'arr'.
sqrt(arr) Calculates square root value of each element in the array 'arr'.
power(arr, n) Returns power value of each element in the array 'arr' when raised to the power of 'n'.
exp(arr) Calculates exponentiation value of each element in the array 'arr'.
sum(arr) Returns sum of all the elements in the array 'arr'.
prod(arr) Returns product of all the elements in the array 'arr'.
min(arr) Returns smallest element in the array 'arr'.
max(arr) Returns biggest element in the array 'arr'.
mean(arr) Returns mean value (average) of all elements in the array 'arr'.
median(arr) Returns median value of all elements in the array 'arr'.
var(arr) Returns variance of all elements in the array'arr'.
cov(arr) Returns covariance of all elements in the array 'arr'.
std(arr) Gives standard deviation of elements in the array'arr'.
argmin(arr) Gives index of the smallest element in the array. Counting starts from 0.
argmax(arr) Gives index of the biggest element in the array. Counting starts from 0.
unique(arr) Gives an array that contains unique elements of the array 'arr'.
sort(arr) Gives an array with sorted elements of the array 'arr' in ascending order.
concatenate([a,b]) Returns an array after joining a, b arrays. The arrays a, b should be entered as elements of a list.

arr = array([10, 20, 30.5, -40])

arr = sin(arr)

[-0.54402111 0.91294525 -0.79312724 -0.74511316]

Observe that the sin() function has worked on each element of the array and displayed the sine values by taking them as angles in radians. So, the first value in the output -0.54402111 is nothing but the value of sin(10) where 10 is taken as angle value in radians.

CopiedCopy Code

from numpy import * 
arr = array([10, 20, 30.5, -40,-80,25]) 
print("Original array:", arr) 
#do arithmetic operations on the elements of the array 
print("After adding 5:", arr+5) 
print("After subtracting 5:", arr-5) 
print("After multiplying with 5:", arr*5) 
print("After dividing with 5:", arr/5) 
print("After modulus with 5:", arr%5)
print("Expression value:", (arr+5)**2-10)
print("Absolute value of each element in the array ",abs(arr))
print("product of all the elements in the array ",prod(arr))
print("array that contains unique elements ",unique(arr))
print("Sin values:", sin(arr)) 
print("Cos values:", cos(arr))
print("Tan values:", tan(arr)) 
print("Biggest element:", max(arr)) 
print("Smallest element:", min(arr)) 
print("Sum of all elements:", sum(arr)) 
print("Average of all elements:", mean(arr))
print("sorted elements of the array in ascending order ",sort(arr))
print("Index of Minimum Value : ",argmin(arr))
print("Index of Maximum Value : ",argmax(arr))
print("Square Root of Array : ",sqrt(arr))
print("median : ",median(arr))