import numpy as np
Math operations
add(x1, x2, /[, out, where, casting, order, …]) Add arguments element-wise.
subtract(x1, x2, /[, out, where, casting, …])对应元素相
Subtract arguments, element-wise.
Multiply arguments element-wise.
Returns a true division of the inputs, element-wise.
Logarithm of the sum of exponentiations of the inputs.
Logarithm of the sum of exponentiations of the inputs in base-2.
Returns a true division of the inputs, element-wise.
Return the largest integer smaller or equal to the division of the inputs.
Numerical negative, element-wise.
Numerical positive, element-wise.
First array elements raised to powers from second array, element-wise.
Return element-wise remainder of division.
Return element-wise remainder of division.
Return the element-wise remainder of division.
Return element-wise quotient and remainder simultaneously.
Calculate the absolute value element-wise.
Compute the absolute values element-wise.
Round elements of the array to the nearest integer.
Returns an element-wise indication of the sign of a number.
Compute the Heaviside step function.
Return the complex conjugate, element-wise.
Calculate the exponential of all elements in the input array.
Calculate 2**p for all p in the input array.
Natural logarithm, element-wise.
ufunc is super convenient because it does not need loops to things that are very intuitive to us, such as operations element-to-element.
#np.arange(start, stop, step)
a = np.arange(10,15,2)
a
array([10, 12, 14])
b= np.array([5,6,7])
b
array([5, 6, 7])
a/b
array([2., 2., 2.])
a*b
array([50, 72, 98])
It is addition with broadcasting/sharing.
np.add(1,4)
5
# We can also do this.
1+4
5
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])
a+b
array([[ 0, 2, 4], [ 3, 5, 7], [ 6, 8, 10]])
np.add(a,b)
array([[ 0, 2, 4], [ 3, 5, 7], [ 6, 8, 10]])
np.subtract(1,4)
-3
# We can also do this.
1-4
-3
# But the magic is the following
a - b
array([[0, 0, 0], [3, 3, 3], [6, 6, 6]])
np.subtract(a,b)
array([[0, 0, 0], [3, 3, 3], [6, 6, 6]])
np.subtract?
2*4
8
np.multiply(2,4)
8
# Remember what a is?
a
array([[0, 1, 2], [3, 4, 5], [6, 7, 8]])
# Remember what b is?
b
array([0, 1, 2])
# it seems that the first column of b got shared by the first column of a, and so on.
a*b
array([[ 0, 1, 4], [ 0, 4, 10], [ 0, 7, 16]])
a/(b+1)
array([[0. , 0.5 , 0.66666667], [3. , 2. , 1.66666667], [6. , 3.5 , 2.66666667]])