Rate This Document
Findability
Accuracy
Completeness
Readability

subtract

Description

Subtract arguments, element-wise.

Mandatory Input Parameters

Parameter

Type

Description

x1, x2

array_like

Arrays or scalars to be subtracted from each other. If x1.shape!=x2.shape, they must be broadcastable to a common shape.

Optional Input Parameters

Parameter

Type

Default Value

Description

out

ndarray/ndarray tuple

None

Location where the calculation result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided, a freshly-allocated array is returned.

where

array_like

None

This condition is broadcast over the input. At locations where the condition is true, the out array stores the result. Elsewhere, the out array retains its original value.

**kwargs

-

-

Other keyword arguments. Refer to the NumPy official document Universal functions (ufunc).

Return Value

Type

Description

ndarray/scalar

Difference of x1 and x2, element-wise

Examples

>>> import numpy as np
>>> np.subtract(1.1, 5.5)
-4.4
>>> x1 = np.arange(9.0).reshape((3,3))
>>> x2 = np.arange(3.0)
>>> x1
array([[0., 1., 2.],
       [3., 4., 5.],
       [6., 7., 8.]])
>>> x2
array([0., 1., 2.])
>>> 
>>> np.subtract(x1, x2)
array([[ 0.,  0.,  0.],
       [ 3.,  3.,  3.],
       [ 6.,  6.,  6.]])
>>

# If both x1 and x2 are of the ndarray type, you can use - to replace np.subtract.

>>> x1 = np.arange(0, 4).reshape((2,2))
>>> x1
array([[0, 1],
       [2, 3]])
>>> x2 = np.arange(4, 8).reshape((2,2))
>>> x2
array([[4, 5],
       [6, 7]])
>>> 
>>> x1 - x2
array([[ -4,  -4],
       [ -4,  -4]])
>>>