Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- import numpy.linalg as npl
- # Use double brackets to ensure proper vector representation
- x = np.array([1,2,3,4])
- print(x)
- print(x.T)
- z = np.array([[1,2,3,4]])
- print(z)
- print(z.T)
- y = np.array([[1],
- [2],
- [3],
- [4]])
- # NumPy will do what it thinks you want it to...
- # works...
- npl.norm(x)**2
- np.dot(x, x)
- npl.norm(z)**2
- # doesn't work...why?
- np.dot(z, z)
- # works
- np.dot(z, z.T)
- # identity matrix instantiation ...
- x = np.identity(10)
- # additional properties of matrices
- x = np.arange(100).reshape(10,10)
- y = np.arange(100, 200).reshape(10,10)
- z = np.arange(200, 300).reshape(10, 10)
- # 1
- (x + y == y + x).sum() == x.ravel().shape[0]
- # 2
- (x + (y + z) == (x + y) + z).sum() == y.ravel().shape[0]
- # 3
- (x.dot(y.dot(z)) == np.matmul((np.matmul(x, y)), z)).sum() == z.ravel().shape[0]
- # 4
- x = x.reshape(20, 5)
- y = y.reshape(5, 20)
- z = z.reshape(5, 20)
- (np.matmul(x, (y + z)) == x.dot(y) + x.dot(z)).sum() == x.shape[0]**2
- # 5
- x = x.reshape(50,2)
- y = y.reshape(2, 50)
- z = z.reshape(2, 50)
- ((y + z).dot(x) == y.dot(x) + z.dot(x)).sum() == x.shape[1]**2
- # 6
- x = x.reshape(20, 5)
- a = 5
- b = 10
- ((a + b)*x == (a*x + b*x)).sum() == x.shape[0] * x.shape[1]
- # 7
- x = x.reshape(20, 5)
- y = y.reshape(20, 5)
- a = 5
- (a*(x + y) == (a*x + a*y)).sum() == y.flatten().shape[0]
- # 8
- x = x.reshape(20, 5)
- y = y.reshape(5, 20)
- a = 5
- (x.dot(a*y) == a*(np.matmul(x, y))).sum() == x.shape[0]**2
- # Eigendecomposition
- x = np.diag(np.arange(11))
- eig_vals, eig_vecs = np.linalg.eig(x)
Add Comment
Please, Sign In to add comment