Arithmetic Operators
The following are the various Arithmetic operators
- Addition :: +
- Subtraction :: –
- Multiplication :: *
- Division :: /
- Floor Division :: //
- Modulo operation/Remainder Operation :: %.
- Exponential operation/power operation :: **.
Note
- The result of division operator(/) is always float.
- But floor division operator(//) can return either integer and float values.
- If both arguments are of type int, then floor division operator returns int value only.
- If atleast one argument is float type then it returns float type only.
Example
Python
print(f"10/2 value :: {10/2}")
print(f"10.0/2value :: {10.0/2}")
print(f"10//2 value :: {10//2}")
print(f"10.0//2 value :: {10.0//2}")
Output
PowerShell
10/2 value :: 5.0
10.0/2value :: 5.0
10//2 value :: 5
10.0//2 value :: 5.0
Arithmetic operators for Numpy arrays with scalar:
- scalar means constant numeric value.
- All arithmetic operators are applicable for Numpy arrays with scalar.
- All these operations will be performed at element level.
1-D Array
Example
Python
import numpy as np
a = np.array([10,20,30,40])
print(f"a+2 value is :: {a+2}")
print(f"a-2 value is :: {a-2}")
print(f"a*2 value is :: {a*2}")
print(f"a**2 value is :: {a**2}")
print(f"a/2 value is :: {a/2}")
print(f"a//2 value is :: {a//2}")
Output
PowerShell
a+2 value is :: [12 22 32 42]
a-2 value is :: [ 8 18 28 38]
a*2 value is :: [20 40 60 80]
a**2 value is :: [ 100 400 900 1600]
a/2 value is :: [ 5. 10. 15. 20.]
a//2 value is :: [ 5 10 15 20]
2-D Array
Example
Python
In [191]:
a = np.array([[10,20,30],[40,50,60]])
a
Output
PowerShell
array([[10, 20, 30],
[40, 50, 60]])
Example
Python
print(f"a value is ::\n {a}")
print(f"a+2 value is :: \n {a+2}")
print(f"a-2 value is :: \n {a-2}")
print(f"a*2 value is :: \n {a*2}")
print(f"a**2 value is ::\n {a**2}")
print(f"a/2 value is :: \n {a/2}")
print(f"a//2 value is ::\n {a//2}")
Output
PowerShell
a value is ::
[[10 20 30]
[40 50 60]]
a+2 value is ::
[[12 22 32]
[42 52 62]]
a-2 value is ::
[[ 8 18 28]
[38 48 58]]
a*2 value is ::
[[ 20 40 60]
[ 80 100 120]]
a**2 value is ::
[[ 100 400 900]
[1600 2500 3600]]
a/2 value is ::
[[ 5. 10. 15.]
[20. 25. 30.]]
a//2 value is ::
[[ 5 10 15]
[20 25 30]]
ZeroDivisionError
- In python Anything by zero including zero/zero also results in : ZeroDivisionError.
- But in numpy there is no ZeroDivisionError.
- 10/0 ==> Infinity(inf).
- 0/0 ==> undefined(nan—>not a number).
Example
Python
# normal Python
print(f"The value of 10/0 :: {10/0}")
print(f"The value of 0/0 :: {0/0}")
Output
PowerShell
---------------------------------------------------------------------------
ZeroDivisionError Traceback (most recent call last)
<ipython-input-193-9d387af29aeb> in <module>
1 # normal Python
----> 2 print(f"The value of 10/0 :: {10/0}")
3 print(f"The value of 0/0 :: {0/0}")
ZeroDivisionError: division by zero
Example
Python
# numpy arrays
a = np.arange(6)
print(f"The value of a/0 :: {a/0}")
Output
PowerShell
The value of a/0 :: [nan inf inf inf inf inf]
<ipython-input-194-58ff1c7748d1>:3: RuntimeWarning: divide by zero encountere d in true_divide
print(f"The value of a/0 :: {a/0}")
<ipython-input-194-58ff1c7748d1>:3: RuntimeWarning: invalid value encountered in true_divide
print(f"The value of a/0 :: {a/0}")
Arithmetic operators for Arrays with Arrays (Arrays with Arrays)
To perform arithmetic operators between numpy arrays, compulsory both arrays should have.
- same dimension,
- same shape and
- same size,
otherwise we will get error.
1-D arrays
Example
Python
import numpy as np
a = np.array([1,2,3,4])
b = np.array([10,20,30,40])
print(f"Dimension of a : {a.ndim}, size of a :{a.shape} and shape of a : {a.shape}")
print(f"Dimension of b : {b.ndim}, size of a :{b.shape} and shape of a : {b.shape}")
print(f"a array :: {a} and b array :: {b}")
print(f"a+b value is :: {a+b}")
print(f"a-b value is :: {a-b}")
print(f"a*b value is :: {a*b}")
print(f"a**b value is :: {a**b}")
print(f"a/b value is :: {a/b}")
print(f"a//b value is :: {a//b}")
Output
PowerShell
Dimension of a : 1, size of a :(4,) and shape of a : (4,)
Dimension of b : 1, size of a :(4,) and shape of a : (4,)
a array :: [1 2 3 4] and b array :: [10 20 30 40]
a+b value is :: [11 22 33 44]
a-b value is :: [ -9 -18 -27 -36]
a*b value is :: [ 10 40 90 160]
a**b value is :: [ 1 1048576 -1010140999 0]
a/b value is :: [0.1 0.1 0.1 0.1]
a//b value is :: [0 0 0 0]
2-D arrays
Example
Python
a = np.array([[1,2],[3,4]])
b = np.array([[5,6],[7,8]])
print(f"Dimension of a : {a.ndim}, size of a :{a.shape} and shape of a : {a.shape}")
print(f"Dimension of b : {b.ndim}, size of a :{b.shape} and shape of a : {b.shape}")
print(f"a array :: \n {a} ")
print(f"b array :: \n {b} ")
print(f"a+b value is :: \n {a+b}")
print(f"a-b value is :: \n {a-b}")
print(f"a*b value is :: \n {a*b}")
print(f"a**b value is :: \n {a**b}")
print(f"a/b value is :: \n {a/b}")
print(f"a//b value is :: \n {a//b}")
Output
PowerShell
Dimension of a : 2, size of a :(2, 2) and shape of a : (2, 2)
Dimension of b : 2, size of a :(2, 2) and shape of a : (2, 2
a array ::
[[1 2]
[3 4]]
b array ::
[[5 6]
[7 8]]
a+b value is
[[ 6 8]
[10 12]]
a-b value is
[[-4 -4]
[-4 -4]]
a*b value is ::
[[ 5 12]
[21 32]]
a**b value is ::
[[ 1 64]
[ 2187 65536]]
a/b value is ::
[[0.2 0.33333333]
[0.42857143 0.5 ]]
a//b value is ::
[[0 0]
[0 0]]
Example
Python
a = np.array([10,20,30,40])
b = np.array([10,20,30,40,50])
print(f"Dimension of a : {a.ndim}, size of a :{a.shape} and shape of a : {a.shape}")
print(f"Dimension of b : {b.ndim}, size of a :{b.shape} and shape of a : {b.shape}")
print(f"a+b value is :: \n {a+b}")
Output
PowerShell
Dimension of a : 1, size of a :(4,) and shape of a : (4,)
Dimension of b : 1, size of a :(5,) and shape of a : (5,)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-197-5e5693a9e518> in <module>
3 print(f"Dimension of a : {a.ndim}, size of a :{a.shape} and shape of a : {a.shape}")
4 print(f"Dimension of b : {b.ndim}, size of a :{b.shape} and shape of a : {b.shape}")
----> 5 print(f"a+b value is :: \n {a+b}")
ValueError: operands could not be broadcast together with shapes (4,) (5,)
Equivalent function for arithmetic operators in numpy
- a+b ==> np.add(a,b).
- a-b ==> np.subtract(a,b).
- a*b ==> np.multiply(a,b).
- a/b ==> np.divide(a,b.
- a//b ==> np.floor_divide(a,b).
- a%b ==> np.mod(a,b).
- a ** b ==> np.power(a,b).
Note
To use these functions both arrays should be in
- same dimension,
- same size and
- same shape
Example
Python
# Using the functions to perform arithmetic operations
import numpy as np
a = np.array([10,20,30,40])
b = np.array([1,2,3,4])
print(f"Dimension of a : {a.ndim}, size of a :{a.shape} and shape of a : {a.shape}")
print(f"Dimension of b : {b.ndim}, size of a :{b.shape} and shape of a : {b.shape}")
print(f"a array :: {a} and b array :: {b}")
print(f"a+b value is :: { np.add(a,b)}")
print(f"a-b value is :: {np.subtract(a,b)}")
print(f"a*b value is :: {np.multiply(a,b)}")
print(f"a/b value is :: {np.divide(a,b)}")
print(f"a//b value is :: {np.floor_divide(a,b)}")
print(f"a%b value is :: {np.mod(a,b)}")
print(f"a**b value is :: {np.power(a,b)}")
Output
PowerShell
Dimension of a : 1, size of a :(4,) and shape of a : (4,)
Dimension of b : 1, size of a :(4,) and shape of a : (4,)
a array :: [10 20 30 40] and b array :: [1 2 3 4]
a+b value is :: [11 22 33 44]
a-b value is :: [ 9 18 27 36]
a*b value is :: [ 10 40 90 160]
a/b value is :: [10. 10. 10. 10.]
a//b value is :: [10 10 10 10]
a%b value is :: [0 0 0 0]
a**b value is :: [ 10 400 27000 2560000]
Universal Functions(ufunc)
- he functions which operates element by element on whole array are called.
- Universal functions (ufunc).
- All the above functions are ufunc.
- np.dot() :: Matrix Multiplication/Dot product.
- np.multiply() :: Element multiplication.