Guest User

Untitled

a guest
Oct 21st, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.03 KB | None | 0 0
  1. import sys
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4.  
  5. def cbrt(n):
  6. x = np.ones((n, n))
  7. x = np.triu(x, 1) # ones strictly above the diagonal
  8. x = x[:, ::-1] # mirrored left-right
  9.  
  10. W, V = np.linalg.eig(x)
  11. W = np.cbrt(W)
  12. z = V.dot(np.diag(W)).dot(V.T)
  13.  
  14. return z
  15.  
  16.  
  17. def keypoints(z):
  18. n = len(z)
  19. m = n // 2
  20. s, q, k = z[m, 0], z[m, m//2], z[m, m-2]
  21. return s, q, k
  22.  
  23.  
  24. do_plot = True
  25. if do_plot:
  26. n = 1000
  27. z = cbrt(n)
  28.  
  29. l = z[range(n), range(n)]
  30. plt.suptitle("diagonal of cuberoot matrix, n=%d" % n)
  31. plt.plot(range(len(l)), l)
  32. plt.savefig("cbrt-diag.pdf")
  33. plt.show()
  34.  
  35. zm = z[:, ::-1]
  36. plt.suptitle("antidiagonals of cuberoot matrix, n=%d" % n)
  37. legend = []
  38. plt.gca().set_color_cycle(['red', 'orange', 'yellow', 'green', 'blue', 'purple', 'black'])
  39. for offset in range(-3, 4):
  40. v = np.diagonal(zm, offset)
  41. plt.plot(range(len(v)), v)
  42. legend.append("offset %+d" % offset)
  43. plt.xlabel("n")
  44. plt.legend(legend)
  45. plt.savefig("cbrt-antidiag.pdf")
  46. plt.show()
  47.  
  48. l = z[n//2, :]
  49. plt.suptitle("middle row of cuberoot matrix, n=%d" % n)
  50. plt.plot(range(len(l)), l)
  51. plt.savefig("cbrt-middle.pdf")
  52. plt.show()
  53.  
  54.  
  55. three_dee = True
  56. if three_dee:
  57. from mpl_toolkits.mplot3d import Axes3D
  58. from matplotlib import cm
  59.  
  60. n = 20
  61. z = cbrt(n)
  62.  
  63. fig = plt.figure()
  64. fig.suptitle("cuberoot matrix, n=%d" % n)
  65. ax = fig.gca(projection='3d')
  66.  
  67. x, y = np.meshgrid(range(n), range(n))
  68.  
  69. surf = ax.plot_surface(x, y, z, cmap=cm.coolwarm,
  70. linewidth=0, antialiased=False)
  71. fig.colorbar(surf, shrink=0.5, aspect=5)
  72. plt.savefig("cbrt-surface.pdf")
  73. plt.show()
  74.  
  75.  
  76. ss=[]
  77. for n in range(20, 400, 20):
  78. z = cbrt(n)
  79. s, q, k = keypoints(z)
  80. ss.append((n,s,q,k))
  81.  
  82. ss = np.array(ss)
  83.  
  84. plt.plot(ss[:, 0], ss[:, 1])
  85. plt.plot(ss[:, 0], ss[:, 2])
  86. plt.plot(ss[:, 0], ss[:, 3])
  87. plt.suptitle("values of key matrix elements")
  88. plt.xlabel("n")
  89. plt.legend(["edge", "quarter", "middle"])
  90. plt.savefig("cbrt-chart.pdf")
  91. plt.show()
Add Comment
Please, Sign In to add comment