Linear algebra
Elementwise operations are not always what we expect. We have to distinguish between a two-dimensional array and the interpretation of it as a matrix. For example, the product of two matrices is not the element-wise product, but the product of the rows of the first matrix with the columns of the second matrix. This is why there are special operators for linear algebra.
np.arange(4) * np.arange(4) # Elementwise operation: array([0, 1, 4, 9])
np.dot(np.arange(4), np.arange(4)) # Scalar product: 14
np.outer(np.arange(4), np.arange(4)) # Outer product: array([[0, 0, 0, 0],
# [0, 1, 2, 3],
# [0, 2, 4, 6],
# [0, 3, 6, 9]])
The matrix multiplication is performed with @
:
A = np.arange(9).reshape(3,3) # array([[0, 1, 2],
# [3, 4, 5],
# [6, 7, 8]])
A * A # Elementwise operation: array([[ 0, 1, 4],
# [ 9, 16, 25],
# [36, 49, 64]])
A @ A # Matrix multiplication: array([[ 15, 18, 21],
# [ 42, 54, 66],
# [ 69, 90, 111]])
The norm of a vector can be calculated with np.linalg.norm
:
np.linalg.norm(np.arange(3))
Eigenvalues and eigenvectors as well as the determinant of a matrix can be calculated with np.linalg.eig
:
A = np.array([[2,1], [1,2]])
np.linalg.eig(A).eigenvalues # array([3., 1.])
np.linalg.eig(A).eigenvectors # array([[ 0.70710678, -0.70710678],
# [ 0.70710678, 0.70710678]])
np.linalg.det(A) # 2.9999999999999996