Basic Math for Arrays¶

In [1]:
%pip install numpy
import numpy as np
Requirement already satisfied: numpy in /home/samrat/Documents/numpy-torch-tutorials/venv/lib/python3.8/site-packages (1.24.4)
Note: you may need to restart the kernel to use updated packages.
In [4]:
foo = np.random.randint(low=0, high=10, size=(2,2))
bar = np.random.randint(low=0, high=10, size=(2,2))

print(foo)
print(bar)
[[7 4]
 [9 5]]
[[4 0]
 [8 9]]
In [5]:
foo + bar
Out[5]:
array([[11,  4],
       [17, 14]])
In [6]:
foo - bar
Out[6]:
array([[ 3,  4],
       [ 1, -4]])
In [7]:
foo * bar
Out[7]:
array([[28,  0],
       [72, 45]])
In [8]:
foo / bar
/tmp/ipykernel_3278/486367609.py:1: RuntimeWarning: divide by zero encountered in divide
  foo / bar
Out[8]:
array([[1.75      ,        inf],
       [1.125     , 0.55555556]])
In [9]:
foo @ bar
Out[9]:
array([[60, 36],
       [76, 45]])
In [10]:
foo + 5
Out[10]:
array([[12,  9],
       [14, 10]])