Commit 4a5ebc4e authored by Éamonn Murray's avatar Éamonn Murray

Add some timing comparison

parent a9d1363f
......@@ -7,7 +7,8 @@
"outputs": [],
"source": [
"import numpy as np\n",
"import scipy.linalg"
"import scipy.linalg\n",
"import matplotlib.pyplot as plt"
]
},
{
......@@ -322,6 +323,45 @@
"scipy.linalg.eigvals(rand_mat)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"diag_timing_np = []\n",
"diag_timing_sp = []\n",
"size = np.arange(10, 2000, 400)\n",
"for s in size:\n",
" print(\"Size = %dx%d, NumPy, SciPy timings:\" % (s, s))\n",
" m_rand = np.random.rand(s, s)\n",
" \n",
" timer_out = %timeit -r 3 -o np.linalg.eigvals(m_rand)\n",
" diag_timing_np.append(timer_out.average)\n",
" \n",
" timer_out = %timeit -r 3 -o scipy.linalg.eigvals(m_rand)\n",
" diag_timing_sp.append(timer_out.average)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Plot our timing results\n",
"plt.plot(size, np.array(diag_timing_np), \"bo\", label=\"NumPy\")\n",
"plt.plot(size, np.array(diag_timing_sp), \"r+\", label=\"SciPy\")\n",
"scale = diag_timing_np[-1] / size[-1]**3\n",
"plt.plot(size, scale * size**3, 'g-', label=\"N^3\")\n",
"plt.xlabel(\"Matrix Size\")\n",
"plt.ylabel(\"Time to find eigenvalues (s)\")\n",
"plt.legend()\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
......@@ -345,7 +385,7 @@
"\n",
"The file [`CH4-dynmat`](CH4-dynmat) contains output from a calculation of the dynamical matrix of a methane molecule using [Density Functional Perutbation Theory](https://journals.aps.org/rmp/abstract/10.1103/RevModPhys.73.515) (which you will likely learn about in a later course). This is a quantum mechanical calculation of the derivative of the energy of a system with respect to displacements of pairs of atoms in each of the Cartesian directions.\n",
"\n",
"The eigenvalues of this matrix give the vibrational frequencies. Let's try to parse this file to a numpy array and diagonalize it to find the predicted vibrational frequencies of methane.\n",
"The eigenvalues of this matrix give the vibrational frequencies squared. Let's try to parse this file to a numpy array and diagonalize it to find the predicted vibrational frequencies of methane.\n",
"\n",
"The layout of the file is not amenable to being imported directly. You'll often find that if you want to process the output from some code one of the trickiest things is to get it in a format you can operate on. There are many recent initiatives such as [NetCDF](https://www.unidata.ucar.edu/software/netcdf/) and [HDF5](https://support.hdfgroup.org/HDF5/doc/index.html) which try to solve this, and are now incorporated into many scientific codes, but it's good to be able to work without them.\n",
"\n",
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment