Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
scalars and vectors
  • Loading branch information
j-m-dean committed May 25, 2020
commit a44ccb4b59fbd67f980468adef48e5b84f09fa2c
216 changes: 215 additions & 1 deletion content/data_work/linear_algebra.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,221 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"# Linear algebra"
"**Prerequisites:**\n",
"- Functions\n",
"- Collections\n",
"- NumPy arrays"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Linear algebra\n",
"\n",
"Linear algebra is wide bearing branch of mathematics that is still actively researched today. It is the fundamental basis for many chemical applications, for example solving the Schrodinger equation, and is the bedrock of computer science. \n",
"\n",
"There are multiple linear algebra courses run throughout a mathematics degree, and therefore it is impossible to cover significant proportions of linear algebra in a tutorial such as this. The purpose of this tutorial is to introduce the following key concepts:\n",
"\n",
"- Scalars and vectors\n",
"- The cross and dot product of vectors\n",
"- Matrices\n",
"- Notation\n",
"- Determinants\n",
"- Bases\n",
"- Inverse transformations\n",
"- Active and passive transformations\n",
"- Diagonalisation\n",
"- Eigenvectors and eigenvalues\n",
"- Change of variables\n",
"\n",
"This notebook will not be exhaustive when it comes to the concepts above as our aim is to help facilitate your use of python for chemistry problems. For more information on the above there are a plethora of resources based on undergraduate and postgraduate mathematics courses on the web for a more indepth discussion. \n",
"\n",
"Whilst many undergraduate chemistry degree courses attempt to steer clear of mathematics as far as possible, we believe that gaining knowledge about these fundamental concepts will not only allow you to become a better programmer, but allow you to tackle the more difficult chemistry problems as well. \n",
"\n",
"Due to there being a lot of content to cover, there will be a longer problem notebook that will test your knowledge of linear algebra after this. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Scalars and vectors"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Scalar values are values that consist of just a magnitude. These are properties such as the distance travelled by an atom, the mass of compound, or speed of a particle in a mass spectrometer. \n",
"\n",
"A vector is a property that consists of both a magnitude and a direction. This may be the displacement of an atom, the weight of a compound, or the velocity of a particle. \n",
"\n",
"This differentiation is better described with an example: A person drives to the shop from their house. The person's house is at point A, the shop is at point B, and the road connecting points A to B is circular and defined by ACBD.\n",
"\n",
"<img src=\"../images/circle_road.pdf\" alt=\"drawing\" width=\"200\"/>\n",
"\n",
"The distance travelled by the person is simple to calculate. They must either got from A to C to B or, alternatively, A to D to B. Travelling a semi-circle of radius ten on each of the paths to the shop. Using the equation for an arc,\n",
"\n",
"$$ d = r \\theta ,$$ \n",
"\n",
"we deduce they travel 10 $\\pi$. \n",
"\n",
"But the displacement of the person when arriving at the shop can be determined by thinking of this circle sitting on a coordinate system, as if we are plotting a graph. We conveniently set the origin to be point A, and therefore point A has coordinates (0,0). Point B has coordinates (20,0). This consequently makes the coordinates of point C (10,10) and the coordinates of point D (10,-10) as the radius of the circle is 10. \n",
"\n",
"<img src=\"../images/road_config.pdf\" alt=\"drawing\" width=\"400\"/>\n",
"\n",
"The grid lines on the graph above are seperated for each value of one in both the x and y directions. It is common practice in linear algebra to define a movement of one in the x direction by **i**, which defines a vector (1,0), and a movement of one in the y direction by __j__, which defines a vector (0,1). As described in the figure below.\n",
"\n",
"<img src=\"../images/ij.pdf\" alt=\"drawing\" width=\"400\"/>\n",
"\n",
"We can write the coordinates defined earlier as (x,y), as vectors such that:\n",
"\n",
"$$ \\text{A}: \\quad \\mathbf{a}= 0 \\; \\mathbf{i} + 0 \\; \\mathbf{j} $$\n",
"$$ \\text{B}: \\quad \\mathbf{b}= 20 \\; \\mathbf{i} + 0 \\; \\mathbf{j} $$\n",
"$$ \\text{C}: \\quad \\mathbf{c}= 10 \\; \\mathbf{i} + 10 \\; \\mathbf{j} $$\n",
"$$ \\text{D}: \\quad\\mathbf{d}= 10 \\; \\mathbf{i} -10 \\; \\mathbf{j} $$\n",
"\n",
"Here we have defined the vector to go from the origin to point B, as $\\mathbf{b}$. This is because it is conventional to label vectors as lowercase and in bold font. Therefore, the displacement to go from home, at A, to the shop, at B, is defined by:\n",
"\n",
"$$ \\mathbf{b} - \\mathbf{a} = 20 \\; \\mathbf{i} + 0 \\; \\mathbf{j} $$\n",
"\n",
"Which you may recognise as merely being the vector $\\mathbf{b}$. This is due to the fact that the position A is the origin. \n",
"\n",
"We can represent the vectors above as NumPy arrays such that:"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"ExecuteTime": {
"end_time": "2020-05-25T13:07:31.767073Z",
"start_time": "2020-05-25T13:07:31.760699Z"
}
},
"outputs": [],
"source": [
"import numpy as np\n",
"\n",
"a = np.array([0,0])\n",
"\n",
"b = np.array([20,0])\n",
"\n",
"c = np.array([10,10])\n",
"\n",
"d = np.array([10,-10])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now image the another person travelling to the shop. This individual's house is located at C. The distance this individual has to travel is again calculated using the equation for an arc expressed before\n",
"\n",
"$$ d = r \\theta = 10 \\frac{\\pi}{2} = 5\\pi$$ \n",
"\n",
"and the displacement of this individual to go from their house is given by $\\mathbf{c} - \\mathbf{b}$. This can now be calculated using python with the following code:"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"ExecuteTime": {
"end_time": "2020-05-25T13:11:19.606817Z",
"start_time": "2020-05-25T13:11:19.603133Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The displacement of this individual to go to the shop is [-10 10]\n"
]
}
],
"source": [
"displacement = c - b \n",
"\n",
"print(\"The displacement of this individual to go to the shop is\", displacement)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Recall that the array [-10 10] is equivalent to \n",
"\n",
"$$ -10 \\; \\mathbf{i} + 10 \\; \\mathbf{j}$$\n",
"\n",
"**Exercise:** Using the NumPy arrays defined above, calculate the displacement of an individual travelling from point D to the shop, and the displacement between the points C and D. In addition, calculate the distances traversed when travelling between these points on the circular path. "
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"ExecuteTime": {
"end_time": "2020-05-25T13:16:11.425912Z",
"start_time": "2020-05-25T13:16:11.421021Z"
}
},
"outputs": [],
"source": [
"### Write your code here"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In the above example, we described movement on a two-dimensional plane. Often, problems in chemistry are three-dimensional. Similarly to earlier, in three dimensions we describe the coordinates (x,y,z) as \n",
"\n",
"$$ x \\; \\mathbf{i} + y \\; \\mathbf{j} + z \\; \\mathbf{k} ,$$\n",
"\n",
"where **k** describes the vector associated with a movement of magntiude one in the z direction. Let us consider a problem similar to the one we have already discussed: travelling on a sphere. Consider a sphere of radius ten with associated the following associated points\n",
"\n",
"$$ \\text{A}: \\quad \\mathbf{a}= -10 \\; \\mathbf{i} + 0 \\; \\mathbf{j} + 0 \\; \\mathbf{k}$$\n",
"$$ \\text{B}: \\quad \\mathbf{b}= 10 \\; \\mathbf{i} + 0 \\; \\mathbf{j} + 0 \\; \\mathbf{k}$$\n",
"$$ \\text{C}: \\quad \\mathbf{c}= 0 \\; \\mathbf{i} + 10 \\; \\mathbf{j} + 0 \\; \\mathbf{k} $$\n",
"$$ \\text{D}: \\quad\\mathbf{d}= 0 \\; \\mathbf{i} -10 \\; \\mathbf{j} + 0 \\; \\mathbf{k}$$\n",
"$$ \\text{E}: \\quad\\mathbf{e}= 0 \\; \\mathbf{i} + 0 \\; \\mathbf{j} + 10 \\; \\mathbf{k}$$\n",
"$$ \\text{F}: \\quad\\mathbf{f}= 0 \\; \\mathbf{i} + 0 \\; \\mathbf{j} - 10 \\; \\mathbf{k}$$\n",
"\n",
"Here the origin is at the centre of the sphere. The system defined here is:\n",
"\n",
"<img src=\"../images/sphere.pdf\" alt=\"drawing\" width=\"600\"/>\n",
"\n",
"Consider traversing the sphere between the points along the shortest path. This again can be described using the equation for an arc. To traverse between adjacent points requires moving a distance of 5 $\\pi$, whilst to traverse between points that are opposite one another requires moving a distance of 10 $\\pi$ along the surface of the sphere. \n",
"\n",
"The displacement to go from position A to position B is:\n",
"\n",
"$$ \\mathbf{b} - \\mathbf{a} = 20 \\; \\mathbf{i} + 0 \\; \\mathbf{j} + 0 \\; \\mathbf{k} $$\n",
"\n",
"**Exercise:** Determine the displacement to when someone travels from point D to point F. "
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"ExecuteTime": {
"end_time": "2020-05-25T14:36:24.737277Z",
"start_time": "2020-05-25T14:36:24.734381Z"
}
},
"outputs": [],
"source": [
"### Write your code here"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Dot and cross product of vectors"
]
},
{
Expand Down
Binary file added content/images/circle_road.pdf
Binary file not shown.
Binary file added content/images/ij.pdf
Binary file not shown.
Binary file added content/images/road_config.pdf
Binary file not shown.
Binary file added content/images/sphere.pdf
Binary file not shown.