Final: WIP: Clean up task 3.1 a little

This commit is contained in:
2022-03-29 19:15:33 +02:00
parent b7a7a08798
commit 7f12a5cfe9

View File

@ -606,7 +606,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 114, "execution_count": 118,
"metadata": { "metadata": {
"deletable": false, "deletable": false,
"nbgrader": { "nbgrader": {
@ -632,22 +632,19 @@
" positions = atomic_positions(n)\n", " positions = atomic_positions(n)\n",
" \n", " \n",
" infty = 1e3\n", " infty = 1e3\n",
" epsilon = 1e-5\n", " epsilon = 1e-3\n",
" x = np.arange(-infty, infty, epsilon)\n", " x = np.arange(-infty, infty, epsilon)\n",
" \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", " def V(x):\n",
" value = np.zeros(x.shape)\n", " ret = np.zeros(x.shape)\n",
" for x_i in positions:\n", " for x_i in positions:\n",
" value += -1./(np.abs(x - x_i) + 0.001)\n", " ret += -1./(np.abs(x - x_i) + 0.001)\n",
" return value\n", " return ret\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", " # Instead of using a loop, one could vectorize the problem by calculating all sum\n",
" #V = lambda x: np.sum( -1/( np.abs(x - np.tile(positions, (len(x), 1)).T) + 0.001 ), axis=0 )\n", " # elements in a len(x) by len(positions) matrix and then summing along the rows.\n",
" # OMG IT EXISTS\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", " #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", " integrand = lambda x: atomic_basis(x, positions[i], sigma)*V(x)*atomic_basis(x, positions[j], sigma)\n",
" return integrate(integrand, x)\n", " return integrate(integrand, x)\n",
"\n", "\n",
@ -663,7 +660,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 115, "execution_count": 119,
"metadata": { "metadata": {
"deletable": false, "deletable": false,
"nbgrader": { "nbgrader": {
@ -683,13 +680,12 @@
"name": "stdout", "name": "stdout",
"output_type": "stream", "output_type": "stream",
"text": [ "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": [ "source": [
"#print(hopping(1, 0, 10), hopping(0, 1, 10))\n", "print(hopping(1, 0, 10), hopping(0, 1, 10))\n",
"%timeit hopping(1, 0, 10)\n",
"\n", "\n",
"# YOUR CODE HERE\n", "# YOUR CODE HERE\n",
"#raise NotImplementedError()" "#raise NotImplementedError()"