Guest User

Untitled

a guest
Aug 16th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. import numpy as np
  2. import numpy.linalg as npl
  3.  
  4. # Use double brackets to ensure proper vector representation
  5. x = np.array([1,2,3,4])
  6. print(x)
  7. print(x.T)
  8. z = np.array([[1,2,3,4]])
  9. print(z)
  10. print(z.T)
  11.  
  12. y = np.array([[1],
  13. [2],
  14. [3],
  15. [4]])
  16.  
  17. # NumPy will do what it thinks you want it to...
  18. # works...
  19. npl.norm(x)**2
  20. np.dot(x, x)
  21.  
  22. npl.norm(z)**2
  23. # doesn't work...why?
  24. np.dot(z, z)
  25. # works
  26. np.dot(z, z.T)
  27.  
  28. # identity matrix instantiation ...
  29. x = np.identity(10)
  30.  
  31. # additional properties of matrices
  32. x = np.arange(100).reshape(10,10)
  33. y = np.arange(100, 200).reshape(10,10)
  34. z = np.arange(200, 300).reshape(10, 10)
  35.  
  36. # 1
  37. (x + y == y + x).sum() == x.ravel().shape[0]
  38.  
  39. # 2
  40. (x + (y + z) == (x + y) + z).sum() == y.ravel().shape[0]
  41.  
  42. # 3
  43. (x.dot(y.dot(z)) == np.matmul((np.matmul(x, y)), z)).sum() == z.ravel().shape[0]
  44.  
  45. # 4
  46. x = x.reshape(20, 5)
  47. y = y.reshape(5, 20)
  48. z = z.reshape(5, 20)
  49.  
  50. (np.matmul(x, (y + z)) == x.dot(y) + x.dot(z)).sum() == x.shape[0]**2
  51.  
  52. # 5
  53. x = x.reshape(50,2)
  54. y = y.reshape(2, 50)
  55. z = z.reshape(2, 50)
  56.  
  57. ((y + z).dot(x) == y.dot(x) + z.dot(x)).sum() == x.shape[1]**2
  58.  
  59. # 6
  60. x = x.reshape(20, 5)
  61. a = 5
  62. b = 10
  63.  
  64. ((a + b)*x == (a*x + b*x)).sum() == x.shape[0] * x.shape[1]
  65.  
  66. # 7
  67. x = x.reshape(20, 5)
  68. y = y.reshape(20, 5)
  69. a = 5
  70.  
  71. (a*(x + y) == (a*x + a*y)).sum() == y.flatten().shape[0]
  72.  
  73. # 8
  74. x = x.reshape(20, 5)
  75. y = y.reshape(5, 20)
  76. a = 5
  77.  
  78. (x.dot(a*y) == a*(np.matmul(x, y))).sum() == x.shape[0]**2
  79.  
  80. # Eigendecomposition
  81. x = np.diag(np.arange(11))
  82. eig_vals, eig_vecs = np.linalg.eig(x)
Add Comment
Please, Sign In to add comment