Mathematical operations can be completed using NumPy arrays. Scalars can be added and subtracted from arrays and arrays can be added and
subtracted from each other: In [1]: a = np.array([1, 2, 3])
b = a + 2
print(b)
In [2]: a = np.array([1, 2, 3]) b = np.array([2, 4, 6]) c = a + b print(c) Scalar MultiplicationNumPy arrays can be multiplied and divided by scalar integers and floats: In [3]: a = np.array([1,2,3]) b = 3*a print(b) In [4]: a = np.array([10,20,30]) b = a/2 print(b) Array MultiplicationNumPy array can be multiplied by each other using matrix multiplication. These matrix multiplication methods include element-wise multiplication, the dot product, and the cross product. Element-wise MultiplicationThe standard multiplication sign in Python In [5]: a = np.array([1, 2, 3]) b = np.array([4, 5, 6]) a * b Dot ProductIn [6]: a = np.array([1, 2, 3]) b = np.array([4, 5, 6]) np.dot(a,b) Cross ProductIn [7]: a = np.array([1, 2, 3]) b = np.array([4, 5, 6]) np.cross(a, b) Exponents and Logarithmsnp.exp()NumPy's In [8]: a = np.array([1, 2, 3]) np.exp(a) Out[8]: array([ 2.71828183, 7.3890561 , 20.08553692]) LogarithmsNumPy has three logarithmic functions.
TrigonometryNumPy also contains all of the standard trigonometry functions which operate on arrays.
In [12]: import numpy as np np.set_printoptions(4) a = np.array([0, np.pi/4, np.pi/3, np.pi/2]) print(np.sin(a)) print(np.cos(a)) print(np.tan(a)) print(f"Sides 3 and 4, hypotenuse {np.hypot(3,4)}") [0. 0.7071 0.866 1. ] [1.0000e+00 7.0711e-01 5.0000e-01 6.1232e-17] [0.0000e+00 1.0000e+00 1.7321e+00 1.6331e+16] Sides 3 and 4, hypotenuse 5.0 NumPy contains functions to convert arrays of angles between degrees and radians.
In [13]: a = np.array([np.pi,2*np.pi]) np.rad2deg(a) In [14]: a = np.array([0,90, 180, 270]) np.deg2rad(a) Out[14]: array([0. , 1.5708, 3.1416, 4.7124]) Can you multiply two NumPy arrays?multiply() function is used when we want to compute the multiplication of two array. It returns the product of arr1 and arr2, element-wise.
How do you multiply multiple arrays in Python?You can use np. multiply to multiply two same-sized arrays together. This computes something called the Hadamard product. In the Hadamard product, the two inputs have the same shape, and the output contains the element-wise product of each of the input values.
How do you multiply two arrays together?C = A . * B multiplies arrays A and B by multiplying corresponding elements. The sizes of A and B must be the same or be compatible. If the sizes of A and B are compatible, then the two arrays implicitly expand to match each other.
How does Python multiply arrays?NumPy array can be multiplied by each other using matrix multiplication. These matrix multiplication methods include element-wise multiplication, the dot product, and the cross product.
|