diff --git a/Final/Final - Tight-binding propagation method.ipynb b/Final/Final - Tight-binding propagation method.ipynb index 7432e96..f476ce7 100644 --- a/Final/Final - Tight-binding propagation method.ipynb +++ b/Final/Final - Tight-binding propagation method.ipynb @@ -606,7 +606,7 @@ }, { "cell_type": "code", - "execution_count": 114, + "execution_count": 118, "metadata": { "deletable": false, "nbgrader": { @@ -632,22 +632,19 @@ " positions = atomic_positions(n)\n", " \n", " infty = 1e3\n", - " epsilon = 1e-5\n", + " epsilon = 1e-3\n", " x = np.arange(-infty, infty, epsilon)\n", " \n", - " # V is calculated by creating a matrix len(x) by len(positions) over which\n", - " # we sum each row to create a matching value V(x) for each element of x.\n", - " #V = lambda x: np.sum(-1/(np.abs(x - positions)) + 0.001, axis=1)\n", - " #V = lambda x: np.sum( -1/( x - np.tile(positions, (len(x), 1)).T) + 0.001 ), axis=0 )\n", " def V(x):\n", - " value = np.zeros(x.shape)\n", + " ret = np.zeros(x.shape)\n", " for x_i in positions:\n", - " value += -1./(np.abs(x - x_i) + 0.001)\n", - " return value\n", - " #V = lambda x: np.sum( -1/( np.abs(np.tile(x, (len(positions), 1)) - np.tile(positions, (len(x), 1)).T) + 0.001 ), axis=0 )\n", - " #V = lambda x: np.sum( -1/( np.abs(x - np.tile(positions, (len(x), 1)).T) + 0.001 ), axis=0 )\n", - " # OMG IT EXISTS\n", + " ret += -1./(np.abs(x - x_i) + 0.001)\n", + " return ret\n", + " # Instead of using a loop, one could vectorize the problem by calculating all sum\n", + " # elements in a len(x) by len(positions) matrix and then summing along the rows.\n", + " # In testing I found that this was slower than using the loop, so I commented it out.\n", " #V = lambda x: np.sum( -1/( np.abs(np.subtract.outer(x, positions)) + 0.001 ), axis=1 )\n", + " \n", " integrand = lambda x: atomic_basis(x, positions[i], sigma)*V(x)*atomic_basis(x, positions[j], sigma)\n", " return integrate(integrand, x)\n", "\n", @@ -663,7 +660,7 @@ }, { "cell_type": "code", - "execution_count": 115, + "execution_count": 119, "metadata": { "deletable": false, "nbgrader": { @@ -683,13 +680,12 @@ "name": "stdout", "output_type": "stream", "text": [ - "21.9 s ± 35.9 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n" + "-0.13881260449985544 -0.13881260449985544\n" ] } ], "source": [ - "#print(hopping(1, 0, 10), hopping(0, 1, 10))\n", - "%timeit hopping(1, 0, 10)\n", + "print(hopping(1, 0, 10), hopping(0, 1, 10))\n", "\n", "# YOUR CODE HERE\n", "#raise NotImplementedError()"