fork(1) download
  1. import numpy as np
  2.  
  3. def run():
  4. # create a column vector
  5. col_vec = np.array([[1], [2]])
  6. print "column vector"
  7. print col_vec
  8.  
  9. # create a row vector
  10. row_vec = np.array([[1, 2]])
  11. print "row vector"
  12. print row_vec
  13.  
  14. # create a matrix
  15. mat = np.array([[1, 2], [3, 4]])
  16. print "matrix"
  17. print mat
  18.  
  19. # inspect dimensions
  20. print "row vector dimensions", row_vec.ndim
  21. shape = row_vec.shape
  22. print "row vector rows", shape[0], "columns", shape[1]
  23.  
  24. print "matrix dimensions", mat.ndim
  25. shape = mat.shape
  26. print "matrix rows", shape[0], "columns", shape[1]
  27.  
  28. # transpose
  29. vec_t = row_vec.transpose() # or row_vec.T
  30. print "transposed vector"
  31. print vec_t
  32.  
  33. mat_t = mat.transpose() # or mat.T
  34. print "transposed matrix"
  35. print mat_t
  36.  
  37. a = np.array([[2], [-4], [1]])
  38. b = np.array([[2], [1], [-2]])
  39.  
  40. # addition
  41. print "a + b"
  42. print a + b
  43.  
  44. # subtraction
  45. print "a - b"
  46. print a - b
  47.  
  48. # scalar multiplication
  49. print "1.2 * a"
  50. print 1.2 * a
  51.  
  52. # element wise multiplication
  53. print "a * b"
  54. print a * b
  55.  
  56. # vector scalar product
  57. print "a . b"
  58. print np.dot(a.transpose(), b)
  59.  
  60. # vector cross product
  61. print "a x b"
  62. print np.cross(a, b, axis=0) # or np.cross(a.T, b.T).T
  63.  
  64. identity = np.array([[1, 0], [0, 1]])
  65.  
  66. # matrix vector product
  67. print "identity . col_vec"
  68. print np.dot(identity, col_vec)
  69.  
  70. # matrix product
  71. print "identity . mat"
  72. print np.dot(identity, mat)
  73.  
  74.  
  75.  
Success #stdin #stdout 0.1s 23944KB
stdin
Standard input is empty
stdout
Standard output is empty