def testFunc(): "Performs a series of tests to ensure proper functionality" N = 5 eigTol = 1e-8 X = main.construct(N) Y, v = jacobi_rot(X) jacobi_eigenvals = np.zeros(N) # Put the eigenvalues from the diagonal into a 1-D array for i in xrange(N): jacobi_eigenvals[i] = Y[i, i] # Sort list of eigenvalues & eigenvecs in ascending order permute1 = jacobi_eigenvals.argsort() jacobi_eigenvals = jacobi_eigenvals[permute1] jacobi_eigenvecs = v[:, permute1] Eigval_numpy, Eigvec_numpy = np.linalg.eig(X) permute2 = Eigval_numpy.argsort() Eigval_numpy = Eigval_numpy[permute2] Eigvec_numpy = Eigvec_numpy[:, permute2] # Checking that the computed eigenvalues match up with analytical eigenvalues for i in xrange(N): if abs(jacobi_eigenvals[i] - Eigval_numpy[i]) >= eigTol: print "-- Computed eigenvalues differ too much from numpy result, exiting --" sys.exit() else: pass print "Test function passed with tolerance %.1e." % eigTol print np.matmul(np.transpose(jacobi_eigenvecs[:,1]), jacobi_eigenvecs[:, 0]) print np.matmul(np.transpose(Eigvec_numpy[:, 1]), Eigvec_numpy[:, 0]) print return
def num_rotations(): N = np.linspace(5, 200, 10, dtype=int) no_rots = np.zeros(len(N)) for i in range(len(N)): temp, no_rots[i] = jacobi_rot(main.construct(N[i]), keep_counter=True) a = np.polyfit(N, no_rots, deg=2) print a x = np.linspace(5, 1000, 1e5) extrapolation = a[0] * x**2 + a[1] * x + a[2] plt.figure(figsize=(3.8, 3.8)) plt.loglog(N, no_rots, "x", label="Data") plt.loglog(x, extrapolation, label="Etrapolation") main.figsetup(title="Number of itterations needed for N-dim", xlab="N", ylab="No. Rotations", fname="norots") return
def test_construct(self): input = ['a', 'b', 'c'] expected = {'c': {'b': 'a'}} result = construct(input) self.assertEqual(result, expected)
def test_simple_dict(self): simple_dict = {"name": "Boris"} node = construct(simple_dict) self.assertDictEqual(node.get_dict(), simple_dict)
def test_dict_with_subdict(self): simple_dict = {"level1": {"level2": "You Win"}} node = construct(simple_dict) self.assertListEqual(node.get_value("level2"), ["You Win"])
def test_dict_with_list_int_value(self): simple_dict = {"toto": [1, 2, 3]} node = construct(simple_dict) self.assertDictEqual(node.get_dict(), simple_dict)
def test_mutiple_type_same_level(self): simple_dict = {"name": "Boris", "nickname": "hardbass"} node = construct(simple_dict) self.assertDictEqual(node.get_dict(), simple_dict)