How to perform mathematical operations on array elements in python ?

Published: August 02, 2019

DMCA.com Protection Status

Examples of how to perform mathematical operations on array elements ("element-wise operations") in python:

Add a number to all the elements of an array

Let's consider the following array:

\begin{equation}
A = \left( \begin{array}{ccc}
0 & 1 & 2 \\
3 & 4 & 5 \\
6 & 7 & 8
\end{array}\right)
\end{equation}

>>> import numpy as np
>>> A = np.arange(9).reshape(3,3)
>>> A
array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]])

to add a constant number, a solution is to do:

>>> A + 1
array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])

or

>>> B = np.ones(9).reshape(3,3)
>>> B
array([[ 1.,  1.,  1.],
       [ 1.,  1.,  1.],
       [ 1.,  1.,  1.]])
>>> A + B
array([[ 1.,  2.,  3.],
       [ 4.,  5.,  6.],
       [ 7.,  8.,  9.]])

Another example:

>>> B = np.arange(10,19).reshape(3,3)
>>> B
array([[10, 11, 12],
       [13, 14, 15],
       [16, 17, 18]])
>>> A + B
array([[10, 12, 14],
       [16, 18, 20],
       [22, 24, 26]])

Subtract a number to all the elements of an array

Example with a subtraction:

>>> import numpy as np
>>> A = np.arange(9).reshape(3,3)

to subtract a number to all the elements of an array, a solution is to do:

>>> A - 1
array([[-1,  0,  1],
       [ 2,  3,  4],
       [ 5,  6,  7]])

or

>>> B = np.ones(9).reshape(3,3)
>>> A - B
array([[-1.,  0.,  1.],
       [ 2.,  3.,  4.],
       [ 5.,  6.,  7.]])

Another example

>>> B = np.arange(10,19).reshape(3,3)
>>> B
array([[10, 11, 12],
       [13, 14, 15],
       [16, 17, 18]])
>>> A - B
array([[-10, -10, -10],
       [-10, -10, -10],
       [-10, -10, -10]])

Multiply a number to all the elements of an array

>>> A = np.arange(9).reshape(3,3)
>>> A
array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]])
>>> A * 2
array([[ 0,  2,  4],
       [ 6,  8, 10],
       [12, 14, 16]])

Multiply array elements by another array elements

Note: arrays with same size

>>> A = np.arange(9).reshape(3,3)
>>> A
array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]])
>>> B = np.arange(10,19).reshape(3,3)
>>> B
array([[10, 11, 12],
       [13, 14, 15],
       [16, 17, 18]])
>>> A * B
array([[  0,  11,  24],
       [ 39,  56,  75],
       [ 96, 119, 144]])

Square number of each array elements

>>> A = np.arange(9).reshape(3,3)
>>> A
array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]])
>>> A ** 2
array([[ 0,  1,  4],
       [ 9, 16, 25],
       [36, 49, 64]])

Root square number of each array elements

To get the root square of each array elements, a solution is to use the numpy function sqrt()

>>> A = np.arange(9).reshape(3,3)
>>> A
array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]])
>>> A ** 2
array([[ 0,  1,  4],
       [ 9, 16, 25],
       [36, 49, 64]])
>>> np.sqrt(A**2)
array([[ 0.,  1.,  2.],
       [ 3.,  4.,  5.],
       [ 6.,  7.,  8.]])

Using a python function

>>> A = np.arange(9).reshape(3,3)
>>> A
array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]])
>>> def my_custom_function(x):
...     return x**2 + 1
... 
>>> my_custom_function(A)
array([[ 1,  2,  5],
       [10, 17, 26],
       [37, 50, 65]])

Note: to use well know functions such as sinus, cosinus, etc do not use the math module but numpy (numpy Mathematical functions):

>>> import math
>>> def my_custom_function(x):
...     return math.sin(x)
... 
>>> my_custom_function(A)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 2, in my_custom_function
TypeError: only length-1 arrays can be converted to Python scalars

just replace math.sin(x) by np.sin(x)

>>> def my_custom_function(x):
...     return np.sin(x)
... 
>>> my_custom_function(A)
array([[ 0.        ,  0.84147098,  0.90929743],
       [ 0.14112001, -0.7568025 , -0.95892427],
       [-0.2794155 ,  0.6569866 ,  0.98935825]])

Another example

>>> np.sin(A)
array([[ 0.        ,  0.84147098,  0.90929743],
       [ 0.14112001, -0.7568025 , -0.95892427],
       [-0.2794155 ,  0.6569866 ,  0.98935825]])

Element-wise matrix product

>>> import numpy as np
>>> A = np.arange(4).reshape(2,2)
>>> A = np.array([A[:],A[:]*2,A[:]*3])
>>> A
array([[[0, 1],
        [2, 3]],

       [[0, 2],
        [4, 6]],

       [[0, 3],
        [6, 9]]])
>>> B = np.array((4,6))
>>> B
array([4, 6])
>>> B @ A
array([[12, 22],
       [24, 44],
       [36, 66]])

Another example

>>> A = np.arange(3).reshape(3,1)
>>> A
array([[0],
       [1],
       [2]])
>>> B = np.arange(3).reshape(1,3)
>>> B
array([[0, 1, 2]])
>>> B @ A
array([[5]])

Numpy multiply function (rows)

>>> A = np.arange(9).reshape(3,3)
>>> A
array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]])
>>> B = np.arange(3)
>>> B
array([0, 1, 2])
>>> np.multiply(A,B)
array([[ 0,  1,  4],
       [ 0,  4, 10],
       [ 0,  7, 16]])

Numpy multiply function (columns)

>>> C = B[:,np.newaxis]
>>> C
array([[0],
       [1],
       [2]])
>>> np.multiply(A,C)
array([[ 0,  0,  0],
       [ 3,  4,  5],
       [12, 14, 16]])

References