Table of Contents
- Introduction
- NumPy Addition
+
- NumPy Subtraction
-
- NumPy Negation
-
- NumPy Multiplication
*
- NumPy Division
/
- NumPy Floor Division
//
- NumPy Power
**
- NumPy Modulus/Remainder
%
- Compound ufunc Operations
- Commonly Used Mathematical Functions
Introduction
NumPy’s ufuncs make use of Python’s built-in arithmetic operators such as +
, -
, *
and /
and are therefore very convenient to implement. By using the ufuncs, an operation on the array is equivalent to the same operation applied to each and every element.
- unary ufuncs: operate on a single input.
- binary ufuncs: operate on two inputs.
The following table lists the basic arithmetic operators in NumPy.
Operator | ufunc | Type | Description | Output |
---|---|---|---|---|
+ |
np.add(a,b) |
binary | Addition | a+b |
- |
np.subtract(a,b) |
binary | Subtraction | a-b |
- |
np.negative(a) |
unary | Unary negation | -a |
* |
np.multiply(a,b) |
binary | Multiplication | a*b |
/ |
np.divide(a,b) |
binary | Division | a/b |
/ |
np.reciprocal(a) |
unary | Reciprocal | 1/a |
// |
np.floor_divide(a,b) |
binary | Floor division | a//b |
** |
np.power(a,b) |
binary | Exponentiation | a**b |
% |
np.mod(a,b) |
binary | Modulus/remainder | a%b |
Note that the arithmetic operators (first column) are simply convenient wrappers for the corresponding NumPy ufuncs. For example, the +
operator is a wrapper for the np.add
function.
We will present examples of the above arithmetic operations in the following sections. First, let’s create two arrays using the numpy.arange
function.
1import numpy as np
2a= np.arange(11,20)
3b= np.arange(1,10)
4print(a)
5print(b)
[11 12 13 14 15 16 17 18 19]
[1 2 3 4 5 6 7 8 9]
NumPy Addition +
Example
NumPy addition with+
.
1print(a+2)
2print(a+b)
[13 14 15 16 17 18 19 20 21]
[12 14 16 18 20 22 24 26 28]
The same operations could have been accomplished using np.add
.
1print(np.add(a,2))
2print(np.add(a,b))
[13 14 15 16 17 18 19 20 21]
[12 14 16 18 20 22 24 26 28]
NumPy Subtraction -
Example
NumPy subtraction with-
.
1print(a-2)
2print(a-b)
[ 9 10 11 12 13 14 15 16 17]
[10 10 10 10 10 10 10 10 10]
ADVERTISEMENT
NumPy Negation -
Example
NumPy negation with-
.
1print(-a)
2print(-b)
3print(--a) # applied twice
[-11 -12 -13 -14 -15 -16 -17 -18 -19]
[-1 -2 -3 -4 -5 -6 -7 -8 -9]
[11 12 13 14 15 16 17 18 19]
NumPy Multiplication *
Example
NumPy multiplication with*
.
1print(a*2)
2print(a*b)
[22 24 26 28 30 32 34 36 38]
[ 11 24 39 56 75 96 119 144 171]
In the first example, each element of array a
is multiplied by 2
. In the second example, each element of array a
is multiplied by the corresponding element in array b
.
Both inputs in a binary ufunc can be either a scalar or an array.
Example
NumPy multiplication with*
.
1print(2*a)
[22 24 26 28 30 32 34 36 38]
In the above example, the number 2
is multiplied by each element of array a
. This is the same as the previous example when each element of array a
is multiplied by 2
.
NumPy Division /
Example
NumPy division with/
.
1print(a/2)
2print(a/b)
[5.5 6. 6.5 7. 7.5 8. 8.5 9. 9.5]
[11. 6. 4.33333333 3.5 3. 2.66666667
2.42857143 2.25 2.11111111]
NumPy Floor Division //
Example
NumPy floor division with//
.
1print(a//2)
2print(a//b)
[5 6 6 7 7 8 8 9 9]
[11 6 4 3 3 2 2 2 2]
Compare the current outputs with the outputs from the previous example. The numbers here are simply truncated at the decimal point to return the largest possible integer.
NumPy Power **
Example
NumPy power with**
.
1print(a**2)
2print(a**b)
[121 144 169 196 225 256 289 324 361]
[ 11 144 2197 38416 759375 16777216
410338673 -1864941312 565150579]
a**2
is not the same as 2**a
.
In fact a**2
is equivalent to
$$[{11}^2~ {12}^2~ {13}^2~ {14}^2~ {15}^2~ {16}^2~ {17}^2~ {18}^2~ {19}^2]$$
whereas 2**a
is equivalent to
$$[2^{11}~ 2^{12}~ 2^{13}~ 2^{14}~ 2^{15}~ 2^{16}~ 2^{17}~ 2^{18}~ 2^{19}]$$
The following example computes 2**a
.
1print(2**a)
[ 2048 4096 8192 16384 32768 65536 131072 262144 524288]
ADVERTISEMENT
NumPy Modulus/Remainder %
The %
operator computes the remainder when the first input a
is divided by the second input b
. The numpy.mod
ufunc can also take floats as arguments.
Example
NumPy Modulus/Remainder with%
.
1print(a%2)
2print(a%2.3)
[1 0 1 0 1 0 1 0 1]
[1.8 0.5 1.5 0.2 1.2 2.2 0.9 1.9 0.6]
For the first element of the second example, 11 % 2.3 = 1.8
since the remainder of $\frac{11}{2.3}$ can be computed as:
$$ 11 - 4\times 2.3 = 1.8 $$
In fact, the quotient 4.0
above can be obtained using floor division: 11//2.3=4.0
.
Compound ufunc Operations
In case you encounter several operators in a single line, just follow the PEDMAS rule.
Example
Compund operations with ufuncs.1print(a-b*2)
2print(a*b-2)
[9 8 7 6 5 4 3 2 1]
[ 9 22 37 54 73 94 117 142 169]
Whenever possible, always use parentheses to precisely define the order of operations you desire.
Commonly Used Mathematical Functions
Other commonly used mathematical functions include:
ufunc | Type | Description | Math Expression |
---|---|---|---|
np.sqrt(a) |
unary | Square root | $\sqrt{a}$ |
np.square(a) |
unary | Square | $a^2$ |
np.cbrt(a) |
unary | Cube root | $ \sqrt[3]{a}$ |
np.abs(a) |
unary | Absolute value | $| a |$ |