├── README.md
├── DAT256x.zip
├── LICENSE
├── R
├── Module01
│ ├── 01-03-Systems of Equations.ipynb
│ ├── 01-02-Linear Equations.ipynb
│ ├── 01-05-Polynomials.ipynb
│ ├── 01-06-Factorization.ipynb
│ └── 01-01-Introduction to Equations.ipynb
├── Module03
│ ├── 03-03-Matrices.ipynb
│ └── 03-02-Vector Multiplication.ipynb
└── Module02
│ ├── 02-05-Multivariate Functions and Partial Derivatives.ipynb
│ ├── 02-01-Rate of Change.ipynb
│ └── 02-06-Integration.ipynb
└── Python
├── Module01
├── 01-03-Systems of Equations.ipynb
├── 01-05-Polynomials.ipynb
├── 01-02-Linear Equations.ipynb
├── 01-01-Introduction to Equations.ipynb
└── 01-06-Factorization.ipynb
├── Module02
├── 02-05-Multivariate Functions and Partial Derivatives.ipynb
├── 02-01-Rate of Change.ipynb
└── 02-06-Integration.ipynb
└── Module03
├── 03-03-Matrices.ipynb
└── 03-02-Vector Multiplication.ipynb
/README.md:
--------------------------------------------------------------------------------
1 | # Essential-Math
2 | Lab files for Essential Math course
3 |
--------------------------------------------------------------------------------
/DAT256x.zip:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/edx/Essential-Math/HEAD/DAT256x.zip
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2018 Microsoft
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/R/Module01/01-03-Systems of Equations.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Systems of Equations\n",
8 | "Imagine you are at a casino, and you have a mixture of £10 and £25 chips. You know that you have a total of 16 chips, and you also know that the total value of chips you have is £250. Is this enough information to determine how many of each denomination of chip you have?\n",
9 | "\n",
10 | "Well, we can express each of the facts that we have as an equation. The first equation deals with the total number of chips - we know that this is 16, and that it is the number of £10 chips (which we'll call ***x*** ) added to the number of £25 chips (***y***).\n",
11 | "\n",
12 | "The second equation deals with the total value of the chips (£250), and we know that this is made up of ***x*** chips worth £10 and ***y*** chips worth £25.\n",
13 | "\n",
14 | "Here are the equations\n",
15 | "\n",
16 | "\\begin{equation}x + y = 16 \\end{equation}\n",
17 | "\\begin{equation}10x + 25y = 250 \\end{equation}\n",
18 | "\n",
19 | "Taken together, these equations form a *system of equations* that will enable us to determine how many of each chip denomination we have.\n",
20 | "\n",
21 | "## Graphing Lines to Find the Intersection Point\n",
22 | "One approach is to determine all possible values for x and y in each equation and plot them.\n",
23 | "\n",
24 | "A collection of 16 chips could be made up of 16 £10 chips and no £25 chips, no £10 chips and 16 £25 chips, or any combination between these.\n",
25 | "\n",
26 | "Similarly, a total of £250 could be made up of 25 £10 chips and no £25 chips, no £10 chips and 10 £25 chips, or a combination in between.\n",
27 | "\n",
28 | "Let's plot each of these ranges of values as lines on a graph:"
29 | ]
30 | },
31 | {
32 | "cell_type": "code",
33 | "execution_count": null,
34 | "metadata": {},
35 | "outputs": [],
36 | "source": [
37 | "library(ggplot2)\n",
38 | "library(repr)\n",
39 | "options(repr.plot.width=4, repr.plot.height=4)\n",
40 | "\n",
41 | "## Create a data frames with the extreems of the possible values of chips\n",
42 | "chips = data.frame(x = c(16,0), y = c(0,16))\n",
43 | "\n",
44 | "## A second data frame with the extreems of the values of the chips\n",
45 | "values = data.frame(x = c(25,0), y = c(0,10))\n",
46 | "\n",
47 | "ggplot() + geom_line(data = chips, aes(x,y), color = 'blue', size = 1) +\n",
48 | " geom_line(data = values, aes(x,y), color = 'orange', size = 1)"
49 | ]
50 | },
51 | {
52 | "cell_type": "markdown",
53 | "metadata": {},
54 | "source": [
55 | "Looking at the graph, you can see that there is only a single combination of £10 and £25 chips that is on both the line for all possible combinations of 16 chips and the line for all possible combinations of £250. The point where the line intersects is (10, 6); or put another way, there are ten £10 chips and six £25 chips.\n",
56 | "\n",
57 | "### Solving a System of Equations with Elimination\n",
58 | "You can also solve a system of equations mathematically. Let's take a look at our two equations:\n",
59 | "\n",
60 | "\\begin{equation}x + y = 16 \\end{equation}\n",
61 | "\\begin{equation}10x + 25y = 250 \\end{equation}\n",
62 | "\n",
63 | "We can combine these equations to eliminate one of the variable terms and solve the resulting equation to find the value of one of the variables. Let's start by combining the equations and eliminating the x term.\n",
64 | "\n",
65 | "We can combine the equations by adding them together, but first, we need to manipulate one of the equations so that adding them will eliminate the x term. The first equation includes the term ***x***, and the second includes the term ***10x***, so if we multiply the first equation by -10, the two x terms will cancel each other out. So here are the equations with the first one multiplied by -10:\n",
66 | "\n",
67 | "\\begin{equation}-10(x + y) = -10(16) \\end{equation}\n",
68 | "\\begin{equation}10x + 25y = 250 \\end{equation}\n",
69 | "\n",
70 | "After we apply the multiplication to all of the terms in the first equation, the system of equations look like this:\n",
71 | "\n",
72 | "\\begin{equation}-10x + -10y = -160 \\end{equation}\n",
73 | "\\begin{equation}10x + 25y = 250 \\end{equation}\n",
74 | "\n",
75 | "Now we can combine the equations by adding them. The ***-10x*** and ***10x*** cancel one another, leaving us with a single equation like this:\n",
76 | "\n",
77 | "\\begin{equation}15y = 90 \\end{equation}\n",
78 | "\n",
79 | "We can isolate ***y*** by dividing both sides by 15:\n",
80 | "\n",
81 | "\\begin{equation}y = \\frac{90}{15} \\end{equation}\n",
82 | "\n",
83 | "So now we have a value for ***y***:\n",
84 | "\n",
85 | "\\begin{equation}y = 6 \\end{equation}\n",
86 | "\n",
87 | "So how does that help us? Well, now we have a value for ***y*** that satisfies both equations. We can simply use it in either of the equations to determine the value of ***x***. Let's use the first one:\n",
88 | "\n",
89 | "\\begin{equation}x + 6 = 16 \\end{equation}\n",
90 | "\n",
91 | "When we work through this equation, we get a value for ***x***:\n",
92 | "\n",
93 | "\\begin{equation}x = 10 \\end{equation}\n",
94 | "\n",
95 | "So now we've calculated values for ***x*** and ***y***, and we find, just as we did with the graphical intersection method, that there are ten £10 chips and six £25 chips.\n",
96 | "\n",
97 | "You can run the following R code to verify that the equations are both true with an ***x*** value of 10 and a ***y*** value of 6."
98 | ]
99 | },
100 | {
101 | "cell_type": "code",
102 | "execution_count": null,
103 | "metadata": {},
104 | "outputs": [],
105 | "source": [
106 | "x = 10\n",
107 | "y = 6\n",
108 | "print((x + y == 16) & ((10*x) + (25*y) == 250))"
109 | ]
110 | }
111 | ],
112 | "metadata": {
113 | "kernelspec": {
114 | "display_name": "R",
115 | "language": "R",
116 | "name": "ir"
117 | },
118 | "language_info": {
119 | "codemirror_mode": "r",
120 | "file_extension": ".r",
121 | "mimetype": "text/x-r-source",
122 | "name": "R",
123 | "pygments_lexer": "r",
124 | "version": "3.5.0"
125 | }
126 | },
127 | "nbformat": 4,
128 | "nbformat_minor": 2
129 | }
130 |
--------------------------------------------------------------------------------
/Python/Module01/01-03-Systems of Equations.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Systems of Equations\n",
8 | "Imagine you are at a casino, and you have a mixture of £10 and £25 chips. You know that you have a total of 16 chips, and you also know that the total value of chips you have is £250. Is this enough information to determine how many of each denomination of chip you have?\n",
9 | "\n",
10 | "Well, we can express each of the facts that we have as an equation. The first equation deals with the total number of chips - we know that this is 16, and that it is the number of £10 chips (which we'll call ***x*** ) added to the number of £25 chips (***y***).\n",
11 | "\n",
12 | "The second equation deals with the total value of the chips (£250), and we know that this is made up of ***x*** chips worth £10 and ***y*** chips worth £25.\n",
13 | "\n",
14 | "Here are the equations\n",
15 | "\n",
16 | "\\begin{equation}x + y = 16 \\end{equation}\n",
17 | "\\begin{equation}10x + 25y = 250 \\end{equation}\n",
18 | "\n",
19 | "Taken together, these equations form a *system of equations* that will enable us to determine how many of each chip denomination we have.\n",
20 | "\n",
21 | "## Graphing Lines to Find the Intersection Point\n",
22 | "One approach is to determine all possible values for x and y in each equation and plot them.\n",
23 | "\n",
24 | "A collection of 16 chips could be made up of 16 £10 chips and no £25 chips, no £10 chips and 16 £25 chips, or any combination between these.\n",
25 | "\n",
26 | "Similarly, a total of £250 could be made up of 25 £10 chips and no £25 chips, no £10 chips and 10 £25 chips, or a combination in between.\n",
27 | "\n",
28 | "Let's plot each of these ranges of values as lines on a graph:"
29 | ]
30 | },
31 | {
32 | "cell_type": "code",
33 | "execution_count": null,
34 | "metadata": {},
35 | "outputs": [],
36 | "source": [
37 | "%matplotlib inline\n",
38 | "from matplotlib import pyplot as plt\n",
39 | "\n",
40 | "# Get the extremes for number of chips\n",
41 | "chipsAll10s = [16, 0]\n",
42 | "chipsAll25s = [0, 16]\n",
43 | "\n",
44 | "# Get the extremes for values\n",
45 | "valueAll10s = [25,0]\n",
46 | "valueAll25s = [0,10]\n",
47 | "\n",
48 | "# Plot the lines\n",
49 | "plt.plot(chipsAll10s,chipsAll25s, color='blue')\n",
50 | "plt.plot(valueAll10s, valueAll25s, color=\"orange\")\n",
51 | "plt.xlabel('x (£10 chips)')\n",
52 | "plt.ylabel('y (£25 chips)')\n",
53 | "plt.grid()\n",
54 | "\n",
55 | "plt.show()"
56 | ]
57 | },
58 | {
59 | "cell_type": "markdown",
60 | "metadata": {},
61 | "source": [
62 | "Looking at the graph, you can see that there is only a single combination of £10 and £25 chips that is on both the line for all possible combinations of 16 chips and the line for all possible combinations of £250. The point where the line intersects is (10, 6); or put another way, there are ten £10 chips and six £25 chips.\n",
63 | "\n",
64 | "### Solving a System of Equations with Elimination\n",
65 | "You can also solve a system of equations mathematically. Let's take a look at our two equations:\n",
66 | "\n",
67 | "\\begin{equation}x + y = 16 \\end{equation}\n",
68 | "\\begin{equation}10x + 25y = 250 \\end{equation}\n",
69 | "\n",
70 | "We can combine these equations to eliminate one of the variable terms and solve the resulting equation to find the value of one of the variables. Let's start by combining the equations and eliminating the x term.\n",
71 | "\n",
72 | "We can combine the equations by adding them together, but first, we need to manipulate one of the equations so that adding them will eliminate the x term. The first equation includes the term ***x***, and the second includes the term ***10x***, so if we multiply the first equation by -10, the two x terms will cancel each other out. So here are the equations with the first one multiplied by -10:\n",
73 | "\n",
74 | "\\begin{equation}-10(x + y) = -10(16) \\end{equation}\n",
75 | "\\begin{equation}10x + 25y = 250 \\end{equation}\n",
76 | "\n",
77 | "After we apply the multiplication to all of the terms in the first equation, the system of equations look like this:\n",
78 | "\n",
79 | "\\begin{equation}-10x + -10y = -160 \\end{equation}\n",
80 | "\\begin{equation}10x + 25y = 250 \\end{equation}\n",
81 | "\n",
82 | "Now we can combine the equations by adding them. The ***-10x*** and ***10x*** cancel one another, leaving us with a single equation like this:\n",
83 | "\n",
84 | "\\begin{equation}15y = 90 \\end{equation}\n",
85 | "\n",
86 | "We can isolate ***y*** by dividing both sides by 15:\n",
87 | "\n",
88 | "\\begin{equation}y = \\frac{90}{15} \\end{equation}\n",
89 | "\n",
90 | "So now we have a value for ***y***:\n",
91 | "\n",
92 | "\\begin{equation}y = 6 \\end{equation}\n",
93 | "\n",
94 | "So how does that help us? Well, now we have a value for ***y*** that satisfies both equations. We can simply use it in either of the equations to determine the value of ***x***. Let's use the first one:\n",
95 | "\n",
96 | "\\begin{equation}x + 6 = 16 \\end{equation}\n",
97 | "\n",
98 | "When we work through this equation, we get a value for ***x***:\n",
99 | "\n",
100 | "\\begin{equation}x = 10 \\end{equation}\n",
101 | "\n",
102 | "So now we've calculated values for ***x*** and ***y***, and we find, just as we did with the graphical intersection method, that there are ten £10 chips and six £25 chips.\n",
103 | "\n",
104 | "You can run the following Python code to verify that the equations are both true with an ***x*** value of 10 and a ***y*** value of 6."
105 | ]
106 | },
107 | {
108 | "cell_type": "code",
109 | "execution_count": null,
110 | "metadata": {},
111 | "outputs": [],
112 | "source": [
113 | "x = 10\n",
114 | "y = 6\n",
115 | "print ((x + y == 16) & ((10*x) + (25*y) == 250))"
116 | ]
117 | }
118 | ],
119 | "metadata": {
120 | "kernelspec": {
121 | "display_name": "Python 3.6",
122 | "language": "python",
123 | "name": "python36"
124 | },
125 | "language_info": {
126 | "codemirror_mode": {
127 | "name": "ipython",
128 | "version": 3
129 | },
130 | "file_extension": ".py",
131 | "mimetype": "text/x-python",
132 | "name": "python",
133 | "nbconvert_exporter": "python",
134 | "pygments_lexer": "ipython3",
135 | "version": "3.6.3"
136 | }
137 | },
138 | "nbformat": 4,
139 | "nbformat_minor": 2
140 | }
141 |
--------------------------------------------------------------------------------
/R/Module03/03-03-Matrices.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Introduction to Matrices\n",
8 | "In general terms, a matrix is an array of numbers that are arranged into rows and columns.\n",
9 | "\n",
10 | "## Matrices and Matrix Notation\n",
11 | "A matrix arranges numbers into rows and columns, like this:\n",
12 | "\n",
13 | "\\begin{equation}A = \\begin{bmatrix}\n",
14 | " 1 & 2 & 3 \\\\\n",
15 | " 4 & 5 & 6\n",
16 | " \\end{bmatrix}\n",
17 | "\\end{equation}\n",
18 | "\n",
19 | "Note that matrices are generally named as a capital letter. We refer to the *elements* of the matrix using the lower case equivalent with a subscript row and column indicator, like this:\n",
20 | "\n",
21 | "\\begin{equation}A = \\begin{bmatrix}\n",
22 | " a_{1,1} & a_{1,2} & a_{1,3} \\\\\n",
23 | " a_{2,1} & a_{2,2} & a_{2,3}\n",
24 | " \\end{bmatrix}\n",
25 | "\\end{equation}\n",
26 | "\n",
27 | "In R a matrix is a 2-dimensional numeric array. R arrays are stored in **column major order**, meaning the values in each column are stored sequentially. Thus, the R **matrix** converts a numeric vector to a 2-dimensional matrix like this:"
28 | ]
29 | },
30 | {
31 | "cell_type": "code",
32 | "execution_count": null,
33 | "metadata": {},
34 | "outputs": [],
35 | "source": [
36 | "A = matrix(c(1,4,2,5,3,6), nrow = 2, ncol =3)\n",
37 | "A"
38 | ]
39 | },
40 | {
41 | "cell_type": "markdown",
42 | "metadata": {},
43 | "source": [
44 | "R also has an **array** object which can have more than two dimensions. For example, a 2-dimensional numeric array can be created like this:"
45 | ]
46 | },
47 | {
48 | "cell_type": "code",
49 | "execution_count": null,
50 | "metadata": {},
51 | "outputs": [],
52 | "source": [
53 | "B = array(c(1,4,2,5,3,6,7,8,9,1,2,5), c(3,4))\n",
54 | "B"
55 | ]
56 | },
57 | {
58 | "cell_type": "markdown",
59 | "metadata": {},
60 | "source": [
61 | "## Matrix Operations\n",
62 | "Matrices support common arithmetic operations.\n",
63 | "\n",
64 | "### Adding Matrices\n",
65 | "To add two matrices of the same size together, just add the corresponding elements in each matrix:\n",
66 | "\n",
67 | "\\begin{equation}\\begin{bmatrix}1 & 2 & 3 \\\\4 & 5 & 6\\end{bmatrix}+ \\begin{bmatrix}6 & 5 & 4 \\\\3 & 2 & 1\\end{bmatrix} = \\begin{bmatrix}7 & 7 & 7 \\\\7 & 7 & 7\\end{bmatrix}\\end{equation}\n",
68 | "\n",
69 | "In this example, we're adding two matrices (let's call them ***A*** and ***B***). Each matrix has two rows of three columns (so we describe them as 2x3 matrices). Adding these will create a new matrix of the same dimensions with the values a1,1 + b1,1, a1,2 + b1,2, a1,3 + b1,3,a2,1 + b2,1, a2,2 + b2,2, and a2,3 + b2,3. In this instance, each pair of corresponding elements(1 and 6, 2, and 5, 3 and 4, etc.) adds up to 7.\n",
70 | "\n",
71 | "Let's try that with R:"
72 | ]
73 | },
74 | {
75 | "cell_type": "code",
76 | "execution_count": null,
77 | "metadata": {},
78 | "outputs": [],
79 | "source": [
80 | "## Create the second matrix\n",
81 | "B = matrix(c(6,3,5,2,4,1), nrow = 2, ncol = 3)\n",
82 | "A + B"
83 | ]
84 | },
85 | {
86 | "cell_type": "markdown",
87 | "metadata": {},
88 | "source": [
89 | "### Subtracting Matrices\n",
90 | "Matrix subtraction works similarly to matrix addition:\n",
91 | "\n",
92 | "\\begin{equation}\\begin{bmatrix}1 & 2 & 3 \\\\4 & 5 & 6\\end{bmatrix}- \\begin{bmatrix}6 & 5 & 4 \\\\3 & 2 & 1\\end{bmatrix} = \\begin{bmatrix}-5 & -3 & -1 \\\\1 & 3 & 5\\end{bmatrix}\\end{equation}\n",
93 | "\n",
94 | "Here's the R code to do this:"
95 | ]
96 | },
97 | {
98 | "cell_type": "code",
99 | "execution_count": null,
100 | "metadata": {},
101 | "outputs": [],
102 | "source": [
103 | "A - B"
104 | ]
105 | },
106 | {
107 | "cell_type": "markdown",
108 | "metadata": {},
109 | "source": [
110 | "#### Conformability\n",
111 | "In the previous examples, we were able to add and subtract the matrices, because the *operands* (the matrices we are operating on) are ***conformable*** for the specific operation (in this case, addition or subtraction). To be conformable for addition and subtraction, the operands must have the same number of rows and columns. There are different conformability requirements for other operations, such as multiplication; which we'll explore later."
112 | ]
113 | },
114 | {
115 | "cell_type": "markdown",
116 | "metadata": {},
117 | "source": [
118 | "### Negative Matrices\n",
119 | "The negative of a matrix, is just a matrix with the sign of each element reversed:\n",
120 | "\n",
121 | "\\begin{equation}C = \\begin{bmatrix}-5 & -3 & -1 \\\\1 & 3 & 5\\end{bmatrix}\\end{equation}\n",
122 | "\n",
123 | "\\begin{equation}-C = \\begin{bmatrix}5 & 3 & 1 \\\\-1 & -3 & -5\\end{bmatrix}\\end{equation}\n",
124 | "\n",
125 | "Let's see that with R:"
126 | ]
127 | },
128 | {
129 | "cell_type": "code",
130 | "execution_count": null,
131 | "metadata": {},
132 | "outputs": [],
133 | "source": [
134 | "C = matrix(c(-5,-3,-1,1,3,5), ncol = 3, nrow = 2)\n",
135 | "\n",
136 | "C\n",
137 | "-C"
138 | ]
139 | },
140 | {
141 | "cell_type": "markdown",
142 | "metadata": {},
143 | "source": [
144 | "### Matrix Transposition\n",
145 | "You can *transpose* a matrix, that is switch the orientation of its rows and columns. You indicate this with a superscript **T**, like this:\n",
146 | "\n",
147 | "\\begin{equation}\\begin{bmatrix}1 & 2 & 3 \\\\4 & 5 & 6\\end{bmatrix}^{T} = \\begin{bmatrix}1 & 4\\\\2 & 5\\\\3 & 6 \\end{bmatrix}\\end{equation}\n",
148 | "\n",
149 | "The `t` function in R creates the transpose of a matrix:"
150 | ]
151 | },
152 | {
153 | "cell_type": "code",
154 | "execution_count": null,
155 | "metadata": {},
156 | "outputs": [],
157 | "source": [
158 | "A\n",
159 | "t(A)"
160 | ]
161 | }
162 | ],
163 | "metadata": {
164 | "kernelspec": {
165 | "display_name": "R",
166 | "language": "R",
167 | "name": "ir"
168 | },
169 | "language_info": {
170 | "codemirror_mode": "r",
171 | "file_extension": ".r",
172 | "mimetype": "text/x-r-source",
173 | "name": "R",
174 | "pygments_lexer": "r",
175 | "version": "3.5.0"
176 | }
177 | },
178 | "nbformat": 4,
179 | "nbformat_minor": 2
180 | }
181 |
--------------------------------------------------------------------------------
/R/Module02/02-05-Multivariate Functions and Partial Derivatives.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Partial Derivatives\n",
8 | "Until now, we've considered derivatives of functions that operate on a single variable. How do we take the derivatives of a function like the following?\n",
9 | "\n",
10 | "$$f(x,y) = x^2 + y^2$$\n",
11 | "\n",
12 | "We can take a derivative of the changes in the function with respect to either x or y. We call these derivatives with respect to one variable partial derivatives. Let's give this a try by taking the derivative of $f(x,y)$ with respect to ***x***. We write this partial derivative as follows.\n",
13 | "\n",
14 | "$$\\frac{\\partial f(x,y)}{\\partial x} = \\frac{\\partial (x^2 + y^2)}{\\partial x}$$\n",
15 | "\n",
16 | "Just as ordinary derivatives give us a way to compute the rate of change of a function, partial derivatives give us a way to compute the rate of change of a function of many variables with respect to one of those variables.\n",
17 | "\n",
18 | "Since $f(x,y)$ is the sum of several simpler functions we need to take the partial derivative of each of these and sum the result. The first two parts are easy.\n",
19 | "\n",
20 | "$$\\frac{\\partial x^2}{\\partial x} = 2x$$\n",
21 | "\n",
22 | "Notice that we are following the usual rules of differentiation for any function of ***x*** here. \n",
23 | "\n",
24 | "Now we need to take the partial derivative of the last part of $f(x,y)$, which does not depend on ***x*** at all. In these care we get the following.\n",
25 | "\n",
26 | "$$\\frac{\\partial y^2}{\\partial x} = 0$$\n",
27 | "\n",
28 | "Now we can add up the parts to get the complete partial derivative of $f(x,y)$.\n",
29 | "\n",
30 | "$$\\frac{\\partial f(x,y)}{\\partial x} = 2x + 0 = 2x$$"
31 | ]
32 | },
33 | {
34 | "cell_type": "markdown",
35 | "metadata": {},
36 | "source": [
37 | "We can also take the partial derivative of $f(x,y)$ with respect to ***y***. The process proceeds in the following manner.\n",
38 | "\n",
39 | "$$\\frac{\\partial f(x,y)}{\\partial y} = 0 + 2y = 2y$$"
40 | ]
41 | },
42 | {
43 | "cell_type": "markdown",
44 | "metadata": {
45 | "collapsed": true
46 | },
47 | "source": [
48 | "## Computing a Gradient\n",
49 | "\n",
50 | "At this point, you may well ask what is the point of computing partial derivatives? Yes, they are a nifty math trick, but what are they good for? It turns out that partial derivatives are important if you want to find the analog of the slope for multi-dimensional surfaces. We call this quantity the **gradient**. \n",
51 | "\n",
52 | "Recall that you can find minimum and maximum of curves using derivatives. In the same way, you can find the minimum and maximum of surfaces by following the gradient and finding the points were the gradient is zero in all directions. "
53 | ]
54 | },
55 | {
56 | "cell_type": "markdown",
57 | "metadata": {},
58 | "source": [
59 | "You have already examined the partial derivatives of the function, $f(x,y) = x^2 + y^2$. These partial derivatives are:\n",
60 | "\n",
61 | "$$\\frac{\\partial f(x,y)}{\\partial x} = 2x \\\\\n",
62 | "\\frac{\\partial f(x,y)}{\\partial y} = 2y$$\n",
63 | "\n",
64 | "In this case, the gradient is a 2-dimensional vector of the change of the function in the $x$ direction and the change in the function in the $y$ direction. This vector can be written as follows:\n",
65 | "\n",
66 | "$$grad(f(x,y)) = \\vec{g(x,y)} = \\begin{bmatrix}\\frac{\\partial f(x,y)}{\\partial x} \\\\ \\frac{\\partial f(x,y)}{\\partial y} \\end{bmatrix} = \\begin{bmatrix}2x \\\\ 2y \\end{bmatrix} $$"
67 | ]
68 | },
69 | {
70 | "cell_type": "markdown",
71 | "metadata": {},
72 | "source": [
73 | "## Plotting the Gradient\n",
74 | "\n",
75 | "A plot will help you get feel for the meaning of the gradient. The code below plots the gradient of the function $f(x,y) = x^2 + y^2$ along with contours of the value of the function. Run this code and examine the plot. "
76 | ]
77 | },
78 | {
79 | "cell_type": "code",
80 | "execution_count": null,
81 | "metadata": {
82 | "scrolled": false
83 | },
84 | "outputs": [],
85 | "source": [
86 | "library(ggplot2)\n",
87 | "library(repr)\n",
88 | "options(repr.plot.width=5, repr.plot.height=5) # Set the initial plot area dimensions\n",
89 | "\n",
90 | "## Create data frame from grid with out {0,0}\n",
91 | "df = data.frame(expand.grid(x=c(seq(-5,-1), seq(1,5)), y=c(seq(-5,-1), seq(1,5))))\n",
92 | "df$dx = 2 * df$x # compute x derivative\n",
93 | "df$dy = 2 * df$y # compute y derivative\n",
94 | "df$dx_scale = 0.1 * df$dx # scale to get desired line length\n",
95 | "df$dy_scale = 0.1 * df$dy # scale to get desired line length\n",
96 | "\n",
97 | "## Data frame with values to contour\n",
98 | "df2 = data.frame(expand.grid(x=seq(-5,5,by=0.1), y=seq(-5,5,by=0.1)))\n",
99 | "df2$mag = sqrt(df2$x^2 + df2$y^2)\n",
100 | "\n",
101 | "## Plot countours of function and gradient as arrows\n",
102 | "ggplot() + \n",
103 | " geom_contour(data = df2, aes(x, y, z = mag)) +\n",
104 | " geom_segment(data = df, aes(x = x, y = y, xend = x - dx_scale, yend = y - dy_scale),\n",
105 | " size = 1, arrow = arrow(length = unit(0.2,\"cm\")))"
106 | ]
107 | },
108 | {
109 | "cell_type": "markdown",
110 | "metadata": {
111 | "collapsed": true
112 | },
113 | "source": [
114 | "Notice the following properties of this plot. \n",
115 | "- The arrows in the plot point in the direction of the gradient.\n",
116 | "- The length of the arrows is proportional to the value of the gradient. The length of the arrows and the **gradient decreases as function gets closer to the minimum**. If this is the case everywhere, you can say that a function is **convex**. It is always much easier to find minimum of convex functions. \n",
117 | "- The **direction of the gradient is always perpendicular to the contours**. This is an important property of multivariate functions. \n",
118 | "\n",
119 | "## Using the gradient\n",
120 | "\n",
121 | "So, what is all this good for? Say that you want to find the minimum of the function $f(x,y) = x^2 + y^2$. It is easy to see that the minimum of this function is at $x = 0$ and $y = 0$. But, what if you did not know this solution? Then you could do the following:\n",
122 | "\n",
123 | "1. Take some starting guess.\n",
124 | "2. Compute the gradient.\n",
125 | "3. take a small step in the direction of the gradient.\n",
126 | "4. Determine if the gradient is close to zero. If so, then stop, since the gradient will be zero at the minimum.\n",
127 | "5. Repeat steps 2, 3 and 4. \n",
128 | "\n",
129 | "The algorithm outlined above is called the **gradient decent method**. It is the basis of many real-world minimization algorithms. "
130 | ]
131 | }
132 | ],
133 | "metadata": {
134 | "anaconda-cloud": {},
135 | "kernelspec": {
136 | "display_name": "R",
137 | "language": "R",
138 | "name": "ir"
139 | },
140 | "language_info": {
141 | "codemirror_mode": "r",
142 | "file_extension": ".r",
143 | "mimetype": "text/x-r-source",
144 | "name": "R",
145 | "pygments_lexer": "r",
146 | "version": "3.5.0"
147 | }
148 | },
149 | "nbformat": 4,
150 | "nbformat_minor": 1
151 | }
152 |
--------------------------------------------------------------------------------
/Python/Module02/02-05-Multivariate Functions and Partial Derivatives.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Partial Derivatives\n",
8 | "Until now, we've considered derivatives of functions that operate on a single variable. How do we take the derivatives of a function like the following?\n",
9 | "\n",
10 | "$$f(x,y) = x^2 + y^2$$\n",
11 | "\n",
12 | "We can take a derivative of the changes in the function with respect to either x or y. We call these derivatives with respect to one variable partial derivatives. Let's give this a try by taking the derivative of $f(x,y)$ with respect to ***x***. We write this partial derivative as follows.\n",
13 | "\n",
14 | "$$\\frac{\\partial f(x,y)}{\\partial x} = \\frac{\\partial (x^2 + y^2)}{\\partial x}$$\n",
15 | "\n",
16 | "Just as ordinary derivatives give us a way to compute the rate of change of a function, partial derivatives give us a way to compute the rate of change of a function of many variables with respect to one of those variables.\n",
17 | "\n",
18 | "Since $f(x,y)$ is the sum of several simpler functions we need to take the partial derivative of each of these and sum the result. The first two parts are easy.\n",
19 | "\n",
20 | "$$\\frac{\\partial x^2}{\\partial x} = 2x$$\n",
21 | "\n",
22 | "Notice that we are following the usual rules of differentiation for any function of ***x*** here. \n",
23 | "\n",
24 | "Now we need to take the partial derivative of the last part of $f(x,y)$, which does not depend on ***x*** at all. In these care we get the following.\n",
25 | "\n",
26 | "$$\\frac{\\partial y^2}{\\partial x} = 0$$\n",
27 | "\n",
28 | "Now we can add up the parts to get the complete partail derivative of $f(x,y)$.\n",
29 | "\n",
30 | "$$\\frac{\\partial f(x,y)}{\\partial x} = 2x + 0 = 2x$$"
31 | ]
32 | },
33 | {
34 | "cell_type": "markdown",
35 | "metadata": {},
36 | "source": [
37 | "We can also take the partial derivative of $f(x,y)$ with respect to ***y***. The process proceeds in the following manner.\n",
38 | "\n",
39 | "$$\\frac{\\partial f(x,y)}{\\partial y} = 0 + 2y = 2y$$"
40 | ]
41 | },
42 | {
43 | "cell_type": "markdown",
44 | "metadata": {
45 | "collapsed": true
46 | },
47 | "source": [
48 | "## Computing a Gradient\n",
49 | "\n",
50 | "At this point, you may well ask what is the point of computing partial derivatives? Yes, they are a nifty math trick, but what are they good for? It turns out that partial derivatives are important if you want to find the analog of the slope for multi-dimensonal surfaces. We call this quantity the **gradient**. \n",
51 | "\n",
52 | "Recall that you can find minimum and maximum of curves using derivatives. In the same way, you can find the minimum and maximum of surfaces by following the gradiennt and finding the points were the gradient is zero in all directions. "
53 | ]
54 | },
55 | {
56 | "cell_type": "markdown",
57 | "metadata": {},
58 | "source": [
59 | "You have already examined the partial derivatives of the function, $f(x,y) = x^2 + y^2$. These partial derivatives are:\n",
60 | "\n",
61 | "$$\\frac{\\partial f(x,y)}{\\partial x} = 2x \\\\\n",
62 | "\\frac{\\partial f(x,y)}{\\partial y} = 2y$$\n",
63 | "\n",
64 | "In this case, the gradient is a 2-dimensional vector of the change of the function in the $x$ direction and the change in the function in the $y$ direction. This vector can be written as follows:\n",
65 | "\n",
66 | "$$grad(f(x,y)) = \\vec{g(x,y)} = \\begin{bmatrix}\\frac{\\partial f(x,y)}{\\partial x} \\\\ \\frac{\\partial f(x,y)}{\\partial y} \\end{bmatrix} = \\begin{bmatrix}2x \\\\ 2y \\end{bmatrix} $$"
67 | ]
68 | },
69 | {
70 | "cell_type": "markdown",
71 | "metadata": {},
72 | "source": [
73 | "## Plotting the Gradient\n",
74 | "\n",
75 | "A plot will help you get feel for the meaning of the gradient. The code below plots the gradient of the function $f(x,y) = x^2 + y^2$ along with contours of the value of the function. Run this code and examine the plot. "
76 | ]
77 | },
78 | {
79 | "cell_type": "code",
80 | "execution_count": null,
81 | "metadata": {
82 | "scrolled": false
83 | },
84 | "outputs": [],
85 | "source": [
86 | "%matplotlib inline\n",
87 | "import matplotlib.pyplot as plt\n",
88 | "import numpy as np\n",
89 | "import math\n",
90 | "\n",
91 | "## Create a uniform grid\n",
92 | "el = np.arange(-5,6)\n",
93 | "nx, ny = np.meshgrid(el, el, sparse=False, indexing='ij')\n",
94 | "\n",
95 | "## flatten the gird to 1-d and compute the value of the function z\n",
96 | "x_coord = []\n",
97 | "y_coord = []\n",
98 | "z = []\n",
99 | "for i in range(11): \n",
100 | " for j in range(11):\n",
101 | " x_coord.append(float(-nx[i,j]))\n",
102 | " y_coord.append(float(-ny[i,j])) \n",
103 | " z.append(nx[i,j]**2 + ny[i,j]**2)\n",
104 | "\n",
105 | "## perform vector arithmetic to get the x and y gradients \n",
106 | "x_grad = [-2 * x for x in x_coord]\n",
107 | "y_grad = [-2 * y for y in y_coord] \n",
108 | "\n",
109 | "## Plot the arrows using width for gradient\n",
110 | "plt.xlim(-5.5,5.5)\n",
111 | "plt.ylim(-5.5,5.5)\n",
112 | "for x, y, xg, yg in zip(list(x_coord), list(y_coord), list(x_grad), list(y_grad)):\n",
113 | " if x != 0.0 or y != 0.0: ## Avoid the zero divide when scaling the arrow\n",
114 | " l = math.sqrt(xg**2 + yg**2)/2.0\n",
115 | " plt.quiver(x, y, xg, yg, width = l, units = 'dots')\n",
116 | "\n",
117 | "## Plot the countours of the function surface\n",
118 | "z = np.array(z).reshape(11,11) \n",
119 | "plt.contour(el, el, z) "
120 | ]
121 | },
122 | {
123 | "cell_type": "markdown",
124 | "metadata": {
125 | "collapsed": true
126 | },
127 | "source": [
128 | "Notice the following properties of this plot. \n",
129 | "- The arrows in the plot point in the direction of the gradient.\n",
130 | "- The width of the arrows is proportional to the value of the gradient. The width of the arrows and the **gradient decreases as function gets closer to the minimum**. If this is the case everywhere, you can say that a function is **convex**. It is always much easier to find minimum of convex functions. \n",
131 | "- The **direction of the gradient is always perpendicular to the contours**. This is an important property of multivariate functions. \n",
132 | "\n",
133 | "## Using the gradient\n",
134 | "\n",
135 | "So, what is all this good for? Say that you want to find the minimum of the function $f(x,y) = x^2 + y^2$. It is easy to see that the minimum of this function is at $x = 0$ and $y = 0$. But, what if you did not know this solution? Then you could do the following:\n",
136 | "\n",
137 | "1. Take some starting guess.\n",
138 | "2. Compute the gradient.\n",
139 | "3. take a small step in the direction of the gradient.\n",
140 | "4. Determine if the gradient is close to zero. If so, then stop, since the gradient will be zero at the minimum.\n",
141 | "5. Repeate steps 2, 3 and 4. \n",
142 | "\n",
143 | "The algorithm outlined above is called the **gradient decent method**. It is the basis of many real-world minimization algorithms. "
144 | ]
145 | }
146 | ],
147 | "metadata": {
148 | "anaconda-cloud": {},
149 | "kernelspec": {
150 | "display_name": "Python 3",
151 | "language": "python",
152 | "name": "python3"
153 | },
154 | "language_info": {
155 | "codemirror_mode": {
156 | "name": "ipython",
157 | "version": 3
158 | },
159 | "file_extension": ".py",
160 | "mimetype": "text/x-python",
161 | "name": "python",
162 | "nbconvert_exporter": "python",
163 | "pygments_lexer": "ipython3",
164 | "version": "3.5.4"
165 | }
166 | },
167 | "nbformat": 4,
168 | "nbformat_minor": 1
169 | }
170 |
--------------------------------------------------------------------------------
/Python/Module03/03-03-Matrices.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Introduction to Matrices\n",
8 | "In general terms, a matrix is an array of numbers that are arranged into rows and columns.\n",
9 | "\n",
10 | "## Matrices and Matrix Notation\n",
11 | "A matrix arranges numbers into rows and columns, like this:\n",
12 | "\n",
13 | "\\begin{equation}A = \\begin{bmatrix}\n",
14 | " 1 & 2 & 3 \\\\\n",
15 | " 4 & 5 & 6\n",
16 | " \\end{bmatrix}\n",
17 | "\\end{equation}\n",
18 | "\n",
19 | "Note that matrices are generally named as a capital letter. We refer to the *elements* of the matrix using the lower case equivalent with a subscript row and column indicator, like this:\n",
20 | "\n",
21 | "\\begin{equation}A = \\begin{bmatrix}\n",
22 | " a_{1,1} & a_{1,2} & a_{1,3} \\\\\n",
23 | " a_{2,1} & a_{2,2} & a_{2,3}\n",
24 | " \\end{bmatrix}\n",
25 | "\\end{equation}\n",
26 | "\n",
27 | "In Python, you can define a matrix as a 2-dimensional *numpy.**array***, like this:"
28 | ]
29 | },
30 | {
31 | "cell_type": "code",
32 | "execution_count": 1,
33 | "metadata": {},
34 | "outputs": [
35 | {
36 | "name": "stdout",
37 | "output_type": "stream",
38 | "text": [
39 | "[[1 2 3]\n",
40 | " [4 5 6]]\n"
41 | ]
42 | }
43 | ],
44 | "source": [
45 | "import numpy as np\n",
46 | "\n",
47 | "A = np.array([[1,2,3],\n",
48 | " [4,5,6]])\n",
49 | "print (A)"
50 | ]
51 | },
52 | {
53 | "cell_type": "markdown",
54 | "metadata": {},
55 | "source": [
56 | "You can also use the *numpy.**matrix*** type, which is a specialist subclass of ***array***:"
57 | ]
58 | },
59 | {
60 | "cell_type": "code",
61 | "execution_count": 2,
62 | "metadata": {},
63 | "outputs": [
64 | {
65 | "name": "stdout",
66 | "output_type": "stream",
67 | "text": [
68 | "[[1 2 3]\n",
69 | " [4 5 6]]\n"
70 | ]
71 | }
72 | ],
73 | "source": [
74 | "import numpy as np\n",
75 | "\n",
76 | "M = np.matrix([[1,2,3],\n",
77 | " [4,5,6]])\n",
78 | "print (M)"
79 | ]
80 | },
81 | {
82 | "cell_type": "markdown",
83 | "metadata": {},
84 | "source": [
85 | "There are some differences in behavior between ***array*** and ***matrix*** types - particularly with regards to multiplication (which we'll explore later). You can use either, but most experienced Python programmers who need to work with both vectors and matrices tend to prefer the ***array*** type for consistency."
86 | ]
87 | },
88 | {
89 | "cell_type": "markdown",
90 | "metadata": {},
91 | "source": [
92 | "## Matrix Operations\n",
93 | "Matrices support common arithmetic operations.\n",
94 | "\n",
95 | "### Adding Matrices\n",
96 | "To add two matrices of the same size together, just add the corresponding elements in each matrix:\n",
97 | "\n",
98 | "\\begin{equation}\\begin{bmatrix}1 & 2 & 3 \\\\4 & 5 & 6\\end{bmatrix}+ \\begin{bmatrix}6 & 5 & 4 \\\\3 & 2 & 1\\end{bmatrix} = \\begin{bmatrix}7 & 7 & 7 \\\\7 & 7 & 7\\end{bmatrix}\\end{equation}\n",
99 | "\n",
100 | "In this example, we're adding two matrices (let's call them ***A*** and ***B***). Each matrix has two rows of three columns (so we describe them as 2x3 matrices). Adding these will create a new matrix of the same dimensions with the values a1,1 + b1,1, a1,2 + b1,2, a1,3 + b1,3,a2,1 + b2,1, a2,2 + b2,2, and a2,3 + b2,3. In this instance, each pair of corresponding elements(1 and 6, 2, and 5, 3 and 4, etc.) adds up to 7.\n",
101 | "\n",
102 | "Let's try that with Python:"
103 | ]
104 | },
105 | {
106 | "cell_type": "code",
107 | "execution_count": 3,
108 | "metadata": {},
109 | "outputs": [
110 | {
111 | "name": "stdout",
112 | "output_type": "stream",
113 | "text": [
114 | "[[7 7 7]\n",
115 | " [7 7 7]]\n"
116 | ]
117 | }
118 | ],
119 | "source": [
120 | "import numpy as np\n",
121 | "\n",
122 | "A = np.array([[1,2,3],\n",
123 | " [4,5,6]])\n",
124 | "B = np.array([[6,5,4],\n",
125 | " [3,2,1]])\n",
126 | "print(A + B)"
127 | ]
128 | },
129 | {
130 | "cell_type": "markdown",
131 | "metadata": {},
132 | "source": [
133 | "### Subtracting Matrices\n",
134 | "Matrix subtraction works similarly to matrix addition:\n",
135 | "\n",
136 | "\\begin{equation}\\begin{bmatrix}1 & 2 & 3 \\\\4 & 5 & 6\\end{bmatrix}- \\begin{bmatrix}6 & 5 & 4 \\\\3 & 2 & 1\\end{bmatrix} = \\begin{bmatrix}-5 & -3 & -1 \\\\1 & 3 & 5\\end{bmatrix}\\end{equation}\n",
137 | "\n",
138 | "Here's the Python code to do this:"
139 | ]
140 | },
141 | {
142 | "cell_type": "code",
143 | "execution_count": 4,
144 | "metadata": {},
145 | "outputs": [
146 | {
147 | "name": "stdout",
148 | "output_type": "stream",
149 | "text": [
150 | "[[-5 -3 -1]\n",
151 | " [ 1 3 5]]\n"
152 | ]
153 | }
154 | ],
155 | "source": [
156 | "import numpy as np\n",
157 | "\n",
158 | "A = np.array([[1,2,3],\n",
159 | " [4,5,6]])\n",
160 | "B = np.array([[6,5,4],\n",
161 | " [3,2,1]])\n",
162 | "print (A - B)"
163 | ]
164 | },
165 | {
166 | "cell_type": "markdown",
167 | "metadata": {},
168 | "source": [
169 | "#### Conformability\n",
170 | "In the previous examples, we were able to add and subtract the matrices, because the *operands* (the matrices we are operating on) are ***conformable*** for the specific operation (in this case, addition or subtraction). To be conformable for addition and subtraction, the operands must have the same number of rows and columns. There are different conformability requirements for other operations, such as multiplication; which we'll explore later."
171 | ]
172 | },
173 | {
174 | "cell_type": "markdown",
175 | "metadata": {},
176 | "source": [
177 | "### Negative Matrices\n",
178 | "The nagative of a matrix, is just a matrix with the sign of each element reversed:\n",
179 | "\n",
180 | "\\begin{equation}C = \\begin{bmatrix}-5 & -3 & -1 \\\\1 & 3 & 5\\end{bmatrix}\\end{equation}\n",
181 | "\n",
182 | "\\begin{equation}-C = \\begin{bmatrix}5 & 3 & 1 \\\\-1 & -3 & -5\\end{bmatrix}\\end{equation}\n",
183 | "\n",
184 | "Let's see that with Python:"
185 | ]
186 | },
187 | {
188 | "cell_type": "code",
189 | "execution_count": 5,
190 | "metadata": {},
191 | "outputs": [
192 | {
193 | "name": "stdout",
194 | "output_type": "stream",
195 | "text": [
196 | "[[-5 -3 -1]\n",
197 | " [ 1 3 5]]\n",
198 | "[[ 5 3 1]\n",
199 | " [-1 -3 -5]]\n"
200 | ]
201 | }
202 | ],
203 | "source": [
204 | "import numpy as np\n",
205 | "\n",
206 | "C = np.array([[-5,-3,-1],\n",
207 | " [1,3,5]])\n",
208 | "print (C)\n",
209 | "print (-C)"
210 | ]
211 | },
212 | {
213 | "cell_type": "markdown",
214 | "metadata": {},
215 | "source": [
216 | "### Matrix Transposition\n",
217 | "You can *transpose* a matrix, that is switch the orientation of its rows and columns. You indicate this with a superscript **T**, like this:\n",
218 | "\n",
219 | "\\begin{equation}\\begin{bmatrix}1 & 2 & 3 \\\\4 & 5 & 6\\end{bmatrix}^{T} = \\begin{bmatrix}1 & 4\\\\2 & 5\\\\3 & 6 \\end{bmatrix}\\end{equation}\n",
220 | "\n",
221 | "In Python, both *numpy.**array*** and *numpy.**matrix*** have a **T** function:"
222 | ]
223 | },
224 | {
225 | "cell_type": "code",
226 | "execution_count": 6,
227 | "metadata": {},
228 | "outputs": [
229 | {
230 | "name": "stdout",
231 | "output_type": "stream",
232 | "text": [
233 | "[[1 4]\n",
234 | " [2 5]\n",
235 | " [3 6]]\n"
236 | ]
237 | }
238 | ],
239 | "source": [
240 | "import numpy as np\n",
241 | "\n",
242 | "A = np.array([[1,2,3],\n",
243 | " [4,5,6]])\n",
244 | "print(A.T)"
245 | ]
246 | }
247 | ],
248 | "metadata": {
249 | "kernelspec": {
250 | "display_name": "Python 3",
251 | "language": "python",
252 | "name": "python3"
253 | },
254 | "language_info": {
255 | "codemirror_mode": {
256 | "name": "ipython",
257 | "version": 3
258 | },
259 | "file_extension": ".py",
260 | "mimetype": "text/x-python",
261 | "name": "python",
262 | "nbconvert_exporter": "python",
263 | "pygments_lexer": "ipython3",
264 | "version": "3.6.4"
265 | }
266 | },
267 | "nbformat": 4,
268 | "nbformat_minor": 2
269 | }
270 |
--------------------------------------------------------------------------------
/R/Module02/02-01-Rate of Change.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Rate of Change\n",
8 | "Functions are often visualized as a line on a graph, and this line shows how the value returned by the function changes based on changes in the input value.\n",
9 | "\n",
10 | "## Linear Rate of Change\n",
11 | "\n",
12 | "For example, imagine a function that returns the number of meters traveled by a cyclist based on the number of seconds that the cyclist has been cycling.\n",
13 | "\n",
14 | "Here is such a function:\n",
15 | "\n",
16 | "\\begin{equation}q(x) = 2x + 1\\end{equation}\n",
17 | "\n",
18 | "We can plot the output for this function for a period of 10 seconds like this:"
19 | ]
20 | },
21 | {
22 | "cell_type": "code",
23 | "execution_count": null,
24 | "metadata": {},
25 | "outputs": [],
26 | "source": [
27 | "q = function(x){2*x + 1}\n",
28 | " \n",
29 | "## Construct the data frame.\n",
30 | "df = data.frame(x = seq(0,10))\n",
31 | "df$y = q(df$x) ## Call g(x) with the vector df2$x\n",
32 | "\n",
33 | "## Make the plot\n",
34 | "library(ggplot2)\n",
35 | "library(repr)\n",
36 | "options(repr.plot.width=4, repr.plot.height=3)\n",
37 | "ggplot(df, aes(x,y)) + geom_line(color = 'green', size = 1) +\n",
38 | " scale_x_continuous(breaks = seq(0,10)) +\n",
39 | " scale_y_continuous(breaks = seq(0,22, by = 2)) +\n",
40 | " xlab('Seconds') + ylab('Meters')"
41 | ]
42 | },
43 | {
44 | "cell_type": "markdown",
45 | "metadata": {},
46 | "source": [
47 | "It's clear from the graph that ***q*** is a *linear* function that describes a slope in which distance increases at a constant rate over time. In other words, the cyclist is traveling at a constant speed.\n",
48 | "\n",
49 | "But what speed?\n",
50 | "\n",
51 | "Speed, or more technically, velocity is a measure of change - it measures how the distance traveled changes over time (which is why we typically express it as a unit of distance per a unit of time, like *miles-per-hour* or *meters-per-second*). So we're looking for a way to measure the change in the line created by the function.\n",
52 | "\n",
53 | "The change in values along the line define its *slope*, which we know from a previous lesson is represented like this:\n",
54 | "\n",
55 | "\\begin{equation}m = \\frac{\\Delta{y}}{\\Delta{x}} \\end{equation}\n",
56 | "\n",
57 | "We can calculate the slope of our function like this:\n",
58 | "\n",
59 | "\\begin{equation}m = \\frac{q(x)_{2} - q(x)_{1}}{x_{2} - x_{1}} \\end{equation}\n",
60 | "\n",
61 | "So we just need two ordered pairs of ***x*** and ***q(x)*** values from our line to apply this equation.\n",
62 | "\n",
63 | "- After 1 second, ***x*** is 1 and ***q***(1) = **3**.\n",
64 | "- After 10 seconds, ***x*** is 10 and ***q***(10) = 21.\n",
65 | "\n",
66 | "So we can measure the rate of change like this:\n",
67 | "\n",
68 | "\\begin{equation}m = \\frac{21 - 3}{10 - 1} \\end{equation}\n",
69 | "\n",
70 | "This is the same as:\n",
71 | "\n",
72 | "\\begin{equation}m = \\frac{18}{9} \\end{equation}\n",
73 | "\n",
74 | "Which simplifies to:\n",
75 | "\n",
76 | "\\begin{equation}m = \\frac{2}{1} \\end{equation}\n",
77 | "\n",
78 | "So our rate of change is 2/1 or put another way, the cyclist is traveling at 2 meters-per-second."
79 | ]
80 | },
81 | {
82 | "cell_type": "markdown",
83 | "metadata": {},
84 | "source": [
85 | "## Average Rate of Change\n",
86 | "OK, let's look at another function that calculates distance traveled for a given number of seconds:\n",
87 | "\n",
88 | "\\begin{equation}r(x) = x^{2} + x\\end{equation}\n",
89 | "\n",
90 | "Let's take a look at that using R and the ggplot2 package:"
91 | ]
92 | },
93 | {
94 | "cell_type": "code",
95 | "execution_count": null,
96 | "metadata": {},
97 | "outputs": [],
98 | "source": [
99 | "r = function(x){x^2 + x}\n",
100 | " \n",
101 | "## Construct the data frame.\n",
102 | "df2 = data.frame(x = seq(0,10))\n",
103 | "df2$y = r(df2$x) ## Call g(x) with the vector df2$x\n",
104 | "\n",
105 | "## Make the plot\n",
106 | "library(ggplot2)\n",
107 | "library(repr)\n",
108 | "options(repr.plot.width=4, repr.plot.height=3)\n",
109 | "ggplot(df2, aes(x,y)) + geom_line(color = 'green', size = 1) +\n",
110 | " scale_x_continuous(breaks = seq(0,10)) +\n",
111 | " scale_y_continuous(breaks = seq(0,120,20)) +\n",
112 | " xlab('Seconds') + ylab('Meters')"
113 | ]
114 | },
115 | {
116 | "cell_type": "markdown",
117 | "metadata": {},
118 | "source": [
119 | "This time, the function is not linear. It's actually a quadratic function, and the line from 0 seconds to 10 seconds shows an exponential increase; in other words, the cyclist is *accelerating*.\n",
120 | "\n",
121 | "Technically, acceleration itself is a measure of change in velocity over time; and velocity, as we've already discussed, is a measure of change in distance over time. So measuring acceleration is pretty complex, and requires *differential calculus*, which we're going to cover shortly. In fact, even just measuring the velocity at a single point in time requires differential calculus; but we can use algebraic methods to calculate an *average* rate of velocity for a given period shown in the graph.\n",
122 | "\n",
123 | "First, we need to define a *secant* line that joins two points in our exponential arc to create a straight slope. For example, a secant line for the entire 10 second time span would join the following two points:\n",
124 | "\n",
125 | "- 0, ***r***(0)\n",
126 | "- 10, ***r***(10)\n",
127 | "\n",
128 | "Run the following R code to visualize this line:"
129 | ]
130 | },
131 | {
132 | "cell_type": "code",
133 | "execution_count": null,
134 | "metadata": {
135 | "scrolled": false
136 | },
137 | "outputs": [],
138 | "source": [
139 | "## Create a data frame with the end points.\n",
140 | "df3 = df2[c(1,nrow(df2)),]\n",
141 | "\n",
142 | "## Make the plot\n",
143 | "library(ggplot2)\n",
144 | "library(repr)\n",
145 | "options(repr.plot.width=4, repr.plot.height=3)\n",
146 | "ggplot(df2, aes(x,y)) + geom_line(color = 'green', size = 1) +\n",
147 | " geom_line(data = df3, aes(x,y), color = 'magenta', size = 1)\n",
148 | " scale_x_continuous(breaks = seq(0,10)) +\n",
149 | " scale_y_continuous(breaks = seq(0,120,20)) +\n",
150 | " xlab('Seconds') + ylab('Meters')"
151 | ]
152 | },
153 | {
154 | "cell_type": "markdown",
155 | "metadata": {},
156 | "source": [
157 | "Now, because the secant line is straight, we can apply the slope formula we used for a linear function to calculate the average velocity for the 10 second period:\n",
158 | "\n",
159 | "- At 0 seconds, ***x*** is 0 and ***r***(0) = **0**.\n",
160 | "- At 10 seconds, ***x*** is 10 and ***r***(10) = 110.\n",
161 | "\n",
162 | "So we can measure the rate of change like this:\n",
163 | "\n",
164 | "\\begin{equation}m = \\frac{110 - 0}{10 - 0} \\end{equation}\n",
165 | "\n",
166 | "This is the same as:\n",
167 | "\n",
168 | "\\begin{equation}m = \\frac{110}{10} \\end{equation}\n",
169 | "\n",
170 | "Which simplifies to:\n",
171 | "\n",
172 | "\\begin{equation}m = \\frac{11}{1} \\end{equation}\n",
173 | "\n",
174 | "So our rate of change is 11/1 or put another way, the cyclist is traveling at an average velocity of 11 meters-per-second over the 10-second period.\n",
175 | "\n",
176 | "Of course, we can measure the average velocity between any two points on the exponential line. Use the following R code to show the secant line for the period between 2 and 7 seconds, and calculate the average velocity for that period"
177 | ]
178 | },
179 | {
180 | "cell_type": "code",
181 | "execution_count": null,
182 | "metadata": {},
183 | "outputs": [],
184 | "source": [
185 | "## Create a data frame with the end points.\n",
186 | "df4 = data.frame(x = c(2,7))\n",
187 | "df4$y = r(df4$x)\n",
188 | "\n",
189 | "## Make the plot\n",
190 | "library(ggplot2)\n",
191 | "library(repr)\n",
192 | "options(repr.plot.width=4, repr.plot.height=3)\n",
193 | "ggplot(df2, aes(x,y)) + geom_line(color = 'green', size = 1) +\n",
194 | " geom_line(data = df4, aes(x,y), color = 'magenta', size = 1) +\n",
195 | " scale_x_continuous(breaks = seq(0,10)) +\n",
196 | " scale_y_continuous(breaks = seq(0,120,20)) +\n",
197 | " xlab('Seconds') + ylab('Meters')"
198 | ]
199 | }
200 | ],
201 | "metadata": {
202 | "kernelspec": {
203 | "display_name": "R",
204 | "language": "R",
205 | "name": "ir"
206 | },
207 | "language_info": {
208 | "codemirror_mode": "r",
209 | "file_extension": ".r",
210 | "mimetype": "text/x-r-source",
211 | "name": "R",
212 | "pygments_lexer": "r",
213 | "version": "3.5.0"
214 | }
215 | },
216 | "nbformat": 4,
217 | "nbformat_minor": 2
218 | }
219 |
--------------------------------------------------------------------------------
/Python/Module03/03-02-Vector Multiplication.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Vector Multiplication\n",
8 | "Vector multiplication can be performed in three ways:\n",
9 | "\n",
10 | "- Scalar Multiplication\n",
11 | "- Dot Product Multiplication\n",
12 | "- Cross Product Multiplication\n",
13 | "\n",
14 | "## Scalar Multiplication\n",
15 | "Let's start with *scalar* multiplication - in other words, multiplying a vector by a single numeric value.\n",
16 | "\n",
17 | "Suppose I want to multiply my vector by 2, which I could write like this:\n",
18 | "\n",
19 | "\\begin{equation} \\vec{w} = 2\\vec{v}\\end{equation}\n",
20 | "\n",
21 | "Note that the result of this calculation is a new vector named **w**. So how would we calculate this?\n",
22 | "Recall that **v** is defined like this:\n",
23 | "\n",
24 | "\\begin{equation}\\vec{v} = \\begin{bmatrix}2 \\\\ 1 \\end{bmatrix}\\end{equation}\n",
25 | "\n",
26 | "To calculate 2v, we simply need to apply the operation to each dimension value in the vector matrix, like this:\n",
27 | "\n",
28 | "\\begin{equation}\\vec{w} = \\begin{bmatrix}2 \\cdot 2 \\\\ 2 \\cdot 1 \\end{bmatrix}\\end{equation}\n",
29 | "\n",
30 | "Which gives us the following result:\n",
31 | "\n",
32 | "\\begin{equation}\\vec{w} = \\begin{bmatrix}2 \\cdot 2 \\\\ 2 \\cdot 1 \\end{bmatrix} = \\begin{bmatrix}4 \\\\ 2 \\end{bmatrix}\\end{equation}\n",
33 | "\n",
34 | "In Python, you can apply these sort of matrix operations directly to numpy arrays, so we can simply calculate **w** like this:"
35 | ]
36 | },
37 | {
38 | "cell_type": "code",
39 | "execution_count": null,
40 | "metadata": {},
41 | "outputs": [],
42 | "source": [
43 | "%matplotlib inline\n",
44 | "\n",
45 | "import numpy as np\n",
46 | "import matplotlib.pyplot as plt\n",
47 | "import math\n",
48 | "\n",
49 | "v = np.array([2,1])\n",
50 | "\n",
51 | "w = 2 * v\n",
52 | "print(w)\n",
53 | "\n",
54 | "# Plot w\n",
55 | "origin = [0], [0]\n",
56 | "plt.grid()\n",
57 | "plt.ticklabel_format(style='sci', axis='both', scilimits=(0,0))\n",
58 | "plt.quiver(*origin, *w, scale=10)\n",
59 | "plt.show()"
60 | ]
61 | },
62 | {
63 | "cell_type": "markdown",
64 | "metadata": {},
65 | "source": [
66 | "The same approach is taken for scalar division.\n",
67 | "\n",
68 | "Try it for yourself - use the cell below to calculate a new vector named **b** based on the following definition:\n",
69 | "\n",
70 | "\\begin{equation}\\vec{b} = \\frac{\\vec{v}}{2}\\end{equation}"
71 | ]
72 | },
73 | {
74 | "cell_type": "code",
75 | "execution_count": null,
76 | "metadata": {},
77 | "outputs": [],
78 | "source": [
79 | "b = v / 2\n",
80 | "print(b)\n",
81 | "\n",
82 | "# Plot b\n",
83 | "origin = [0], [0]\n",
84 | "plt.axis('equal')\n",
85 | "plt.grid()\n",
86 | "plt.ticklabel_format(style='sci', axis='both', scilimits=(0,0))\n",
87 | "plt.quiver(*origin, *b, scale=10)\n",
88 | "plt.show()"
89 | ]
90 | },
91 | {
92 | "cell_type": "markdown",
93 | "metadata": {},
94 | "source": [
95 | "## Dot Product Multiplication\n",
96 | "So we've seen how to multiply a vector by a scalar. How about multiplying two vectors together? There are actually two ways to do this depending on whether you want the result to be a *scalar product* (in other words, a number) or a *vector product* (a vector).\n",
97 | "\n",
98 | "To get a scalar product, we calculate the *dot product*. This takes a similar approach to multiplying a vector by a scalar, except that it multiplies each component pair of the vectors and sums the results. To indicate that we are performing a dot product operation, we use the • operator:\n",
99 | "\n",
100 | "\\begin{equation} \\vec{v} \\cdot \\vec{s} = (v_{1} \\cdot s_{1}) + (v_{2} \\cdot s_{2}) ... + \\; (v_{n} \\cdot s_{n})\\end{equation}\n",
101 | "\n",
102 | "So for our vectors **v** (2,1) and **s** (-3,2), our calculation looks like this:\n",
103 | "\n",
104 | "\\begin{equation} \\vec{v} \\cdot \\vec{s} = (2 \\cdot -3) + (1 \\cdot 2) = -6 + 2 = -4\\end{equation}\n",
105 | "\n",
106 | "So the dot product, or scalar product, of **v** • **s** is **-4**.\n",
107 | "\n",
108 | "In Python, you can use the *numpy.**dot*** function to calculate the dot product of two vector arrays:"
109 | ]
110 | },
111 | {
112 | "cell_type": "code",
113 | "execution_count": null,
114 | "metadata": {},
115 | "outputs": [],
116 | "source": [
117 | "import numpy as np\n",
118 | "\n",
119 | "v = np.array([2,1])\n",
120 | "s = np.array([-3,2])\n",
121 | "d = np.dot(v,s)\n",
122 | "print (d)"
123 | ]
124 | },
125 | {
126 | "cell_type": "markdown",
127 | "metadata": {},
128 | "source": [
129 | "In Python 3.5 and later, you can also use the **@** operator to calculate the dot product:"
130 | ]
131 | },
132 | {
133 | "cell_type": "code",
134 | "execution_count": null,
135 | "metadata": {},
136 | "outputs": [],
137 | "source": [
138 | "import numpy as np\n",
139 | "\n",
140 | "v = np.array([2,1])\n",
141 | "s = np.array([-3,2])\n",
142 | "d = v @ s\n",
143 | "print (d)"
144 | ]
145 | },
146 | {
147 | "cell_type": "markdown",
148 | "metadata": {},
149 | "source": [
150 | "### The Cosine Rule\n",
151 | "An useful property of vector dot product multiplication is that we can use it to calculate the cosine of the angle between two vectors. We could write the dot products as:\n",
152 | "\n",
153 | "$$ \\vec{v} \\cdot \\vec{s} = \\|\\vec{v} \\|\\|\\vec{s}\\| \\cos (\\theta) $$ \n",
154 | "\n",
155 | "Which we can rearrange as:\n",
156 | "\n",
157 | "$$ \\cos(\\theta) = \\frac{\\vec{v} \\cdot \\vec{s}}{\\|\\vec{v} \\|\\|\\vec{s}\\|} $$\n",
158 | "\n",
159 | "So for our vectors **v** (2,1) and **s** (-3,2), our calculation looks like this:\n",
160 | "\n",
161 | "$$ \\cos(\\theta) = \\frac{(2 \\cdot-3) + (1 \\cdot 2)}{\\sqrt{2^{2} + 1^{2}} \\times \\sqrt{-3^{2} + 2^{2}}} $$\n",
162 | "\n",
163 | "So:\n",
164 | "\n",
165 | "$$\\cos(\\theta) = \\frac{-4}{8.0622577483}$$\n",
166 | "\n",
167 | "Which calculates to:\n",
168 | "\n",
169 | "$$\\cos(\\theta) = -0.496138938357 $$\n",
170 | "\n",
171 | "So:\n",
172 | "\n",
173 | "$$\\theta \\approx 119.74 $$\n",
174 | "\n",
175 | "Here's that calculation in Python:"
176 | ]
177 | },
178 | {
179 | "cell_type": "code",
180 | "execution_count": null,
181 | "metadata": {},
182 | "outputs": [],
183 | "source": [
184 | "import math\n",
185 | "import numpy as np\n",
186 | "\n",
187 | "# define our vectors\n",
188 | "v = np.array([2,1])\n",
189 | "s = np.array([-3,2])\n",
190 | "\n",
191 | "# get the magnitudes\n",
192 | "vMag = np.linalg.norm(v)\n",
193 | "sMag = np.linalg.norm(s)\n",
194 | "\n",
195 | "# calculate the cosine of theta\n",
196 | "cos = (v @ s) / (vMag * sMag)\n",
197 | "\n",
198 | "# so theta (in degrees) is:\n",
199 | "theta = math.degrees(math.acos(cos))\n",
200 | "\n",
201 | "print(theta)\n"
202 | ]
203 | },
204 | {
205 | "cell_type": "markdown",
206 | "metadata": {
207 | "collapsed": true
208 | },
209 | "source": [
210 | "## Cross Product Multiplication\n",
211 | "To get the *vector product* of multipying two vectors together, you must calculate the *cross product*. The result of this is a new vector that is at right angles to both the other vectors in 3D Euclidean space. This means that the cross-product only really makes sense when working with vectors that contain three components.\n",
212 | "\n",
213 | "For example, let's suppose we have the following vectors:\n",
214 | "\n",
215 | "\\begin{equation}\\vec{p} = \\begin{bmatrix}2 \\\\ 3 \\\\ 1 \\end{bmatrix}\\;\\; \\vec{q} = \\begin{bmatrix}1 \\\\ 2 \\\\ -2 \\end{bmatrix}\\end{equation}\n",
216 | "\n",
217 | "To calculate the cross product of these vectors, written as **p** x **q**, we need to create a new vector (let's call it **r**) with three components (r1, r2, and r3). The values for these components are calculated like this:\n",
218 | "\n",
219 | "\\begin{equation}r_{1} = p_{2}q_{3} - p_{3}q_{2}\\end{equation}\n",
220 | "\\begin{equation}r_{2} = p_{3}q_{1} - p_{1}q_{3}\\end{equation}\n",
221 | "\\begin{equation}r_{3} = p_{1}q_{2} - p_{2}q_{1}\\end{equation}\n",
222 | "\n",
223 | "So in our case:\n",
224 | "\n",
225 | "\\begin{equation}\\vec{r} = \\vec{p} \\times \\vec{q} = \\begin{bmatrix}(3 \\cdot -2) - (1 \\cdot 2) \\\\ (1 \\cdot 1) - (2 \\cdot -2) \\\\ (2 \\cdot 2) - (3 \\cdot 1) \\end{bmatrix} = \\begin{bmatrix}-6 - 2 \\\\ 1 - -4 \\\\ 4 - 3 \\end{bmatrix} = \\begin{bmatrix}-8 \\\\ 5 \\\\ 1 \\end{bmatrix}\\end{equation}\n",
226 | "\n",
227 | "In Python, you can use the *numpy.**cross*** function to calculate the cross product of two vector arrays:"
228 | ]
229 | },
230 | {
231 | "cell_type": "code",
232 | "execution_count": null,
233 | "metadata": {},
234 | "outputs": [],
235 | "source": [
236 | "import numpy as np\n",
237 | "\n",
238 | "p = np.array([2,3,1])\n",
239 | "q = np.array([1,2,-2])\n",
240 | "r = np.cross(p,q)\n",
241 | "print (r)"
242 | ]
243 | }
244 | ],
245 | "metadata": {
246 | "kernelspec": {
247 | "display_name": "Python 3",
248 | "language": "python",
249 | "name": "python3"
250 | },
251 | "language_info": {
252 | "codemirror_mode": {
253 | "name": "ipython",
254 | "version": 3
255 | },
256 | "file_extension": ".py",
257 | "mimetype": "text/x-python",
258 | "name": "python",
259 | "nbconvert_exporter": "python",
260 | "pygments_lexer": "ipython3",
261 | "version": "3.7.3"
262 | }
263 | },
264 | "nbformat": 4,
265 | "nbformat_minor": 2
266 | }
267 |
--------------------------------------------------------------------------------
/R/Module03/03-02-Vector Multiplication.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Vector Multiplication\n",
8 | "Vector multiplication can be performed in three ways:\n",
9 | "\n",
10 | "- Scalar Multiplication\n",
11 | "- Dot Product Multiplication\n",
12 | "- Cross Product Multiplication\n",
13 | "\n",
14 | "## Scalar Multiplication\n",
15 | "Let's start with *scalar* multiplication - in other words, multiplying a vector by a single numeric value.\n",
16 | "\n",
17 | "Suppose I want to multiply my vector by 2, which I could write like this:\n",
18 | "\n",
19 | "\\begin{equation} \\vec{w} = 2\\vec{v}\\end{equation}\n",
20 | "\n",
21 | "Note that the result of this calculation is a new vector named **w**. So how would we calculate this?\n",
22 | "Recall that **v** is defined like this:\n",
23 | "\n",
24 | "\\begin{equation}\\vec{v} = \\begin{bmatrix}2 \\\\ 1 \\end{bmatrix}\\end{equation}\n",
25 | "\n",
26 | "To calculate 2v, we simply need to apply the operation to each dimension value in the vector matrix, like this:\n",
27 | "\n",
28 | "\\begin{equation}\\vec{w} = \\begin{bmatrix}2 \\cdot 2 \\\\ 2 \\cdot 1 \\end{bmatrix}\\end{equation}\n",
29 | "\n",
30 | "Which gives us the following result:\n",
31 | "\n",
32 | "\\begin{equation}\\vec{w} = \\begin{bmatrix}2 \\cdot 2 \\\\ 2 \\cdot 1 \\end{bmatrix} = \\begin{bmatrix}4 \\\\ 2 \\end{bmatrix}\\end{equation}\n",
33 | "\n",
34 | "In r, you can apply these sort of matrix operations directly to arrays and numeric columns in data frames, so we can simply calculate **w** like this:"
35 | ]
36 | },
37 | {
38 | "cell_type": "code",
39 | "execution_count": null,
40 | "metadata": {
41 | "collapsed": true
42 | },
43 | "outputs": [],
44 | "source": [
45 | "library(ggplot2)\n",
46 | "library(NISTunits) # Libary to work netween degrees and radians\n",
47 | "library(repr)\n",
48 | "options(repr.plot.width=3.5, repr.plot.height=3) # Set the initial plot area dimensions\n",
49 | "\n",
50 | "## data frame for origin and end of vector\n",
51 | "v = data.frame(x = c(0,2), y = c(0,1))\n",
52 | "\n",
53 | "## Element-wise multiply the vector by 2\n",
54 | "w = 2 * v\n",
55 | "\n",
56 | "## Plot the vector\n",
57 | "ggplot() + \n",
58 | " geom_line(data = v, aes(x,y), arrow = arrow(length=unit(0.30,\"cm\")), color = 'red', size = 1) + \n",
59 | " geom_line(data = w, aes(x,y), arrow = arrow(length=unit(0.30,\"cm\")), size = 0.5) +\n",
60 | " xlim(-5,5) + ylim(-5,5)"
61 | ]
62 | },
63 | {
64 | "cell_type": "markdown",
65 | "metadata": {},
66 | "source": [
67 | "The same approach is taken for scalar division.\n",
68 | "\n",
69 | "Try it for yourself - use the cell below to calculate a new vector named **b** based on the following definition:\n",
70 | "\n",
71 | "\\begin{equation}\\vec{b} = \\frac{\\vec{v}}{2}\\end{equation}"
72 | ]
73 | },
74 | {
75 | "cell_type": "code",
76 | "execution_count": null,
77 | "metadata": {
78 | "collapsed": true
79 | },
80 | "outputs": [],
81 | "source": [
82 | "b = v / 2\n",
83 | "print(b[2,])\n",
84 | "\n",
85 | "# Plot b\n",
86 | "## Plot the vector\n",
87 | "ggplot() + \n",
88 | " geom_line(data = v, aes(x,y), arrow = arrow(length=unit(0.30,\"cm\")), color = 'red') + \n",
89 | " geom_line(data = b, aes(x,y), arrow = arrow(length=unit(0.30,\"cm\")), size = 1) +\n",
90 | " xlim(-5,5) + ylim(-5,5)"
91 | ]
92 | },
93 | {
94 | "cell_type": "markdown",
95 | "metadata": {},
96 | "source": [
97 | "## Dot Product Multiplication\n",
98 | "So we've seen how to multiply a vector by a scalar. How about multiplying two vectors together? There are actually two ways to do this depending on whether you want the result to be a *scalar product* (in other words, a number) or a *vector product* (a vector).\n",
99 | "\n",
100 | "To get a scalar product, we calculate the *dot product*. This takes a similar approach to multiplying a vector by a scalar, except that it multiplies each component pair of the vectors and sums the results. To indicate that we are performing a dot product operation, we use the • operator:\n",
101 | "\n",
102 | "\\begin{equation} \\vec{v} \\cdot \\vec{s} = (v_{1} \\cdot s_{1}) + (v_{2} \\cdot s_{2}) ... + \\; (v_{n} \\cdot s_{n})\\end{equation}\n",
103 | "\n",
104 | "So for our vectors **v** (2,1) and **s** (-3,2), our calculation looks like this:\n",
105 | "\n",
106 | "\\begin{equation} \\vec{v} \\cdot \\vec{s} = (2 \\cdot -3) + (1 \\cdot 2) = -6 + 2 = -4\\end{equation}\n",
107 | "\n",
108 | "So the dot product, or scalar product, of **v** • **s** is **-4**.\n",
109 | "\n",
110 | "*****\n",
111 | "**Note:** In R you can take a dot project as the sum of the matrix multiplication of one vector by the transpose of the other using the **%*%** operator. The multiplication gives an array with one non-zero element and the sum gives the scalar result. \n",
112 | "****\n",
113 | "\n",
114 | "Execute the code in the cell below to see the result:"
115 | ]
116 | },
117 | {
118 | "cell_type": "code",
119 | "execution_count": null,
120 | "metadata": {
121 | "collapsed": true
122 | },
123 | "outputs": [],
124 | "source": [
125 | "## Another data frame s\n",
126 | "s = data.frame(x = c(0,-3), y = c(0,2))\n",
127 | "\n",
128 | "dot_v_s = sum(as.matrix(v) %*% as.matrix(t(s)))\n",
129 | "dot_v_s"
130 | ]
131 | },
132 | {
133 | "cell_type": "markdown",
134 | "metadata": {},
135 | "source": [
136 | "### The Cosine Rule\n",
137 | "An useful property of vector dot product multiplication is that we can use it to calculate the cosine of the angle between two vectors. We could write the dot products as:\n",
138 | "\n",
139 | "$$ \\vec{v} \\cdot \\vec{s} = \\|\\vec{v} \\|\\|\\vec{s}\\| \\cos (\\theta) $$ \n",
140 | "\n",
141 | "Which we can rearrange as:\n",
142 | "\n",
143 | "$$ \\cos(\\theta) = \\frac{\\vec{v} \\cdot \\vec{s}}{\\|\\vec{v} \\|\\|\\vec{s}\\|} $$\n",
144 | "\n",
145 | "So for our vectors **v** (2,1) and **s** (-3,2), our calculation looks like this:\n",
146 | "\n",
147 | "$$ \\cos(\\theta) = \\frac{(2 \\cdot-3) + (1 \\cdot 2)}{\\sqrt{2^{2} + 1^{2}} \\times \\sqrt{-3^{2} + 2^{2}}} $$\n",
148 | "\n",
149 | "So:\n",
150 | "\n",
151 | "$$\\cos(\\theta) = \\frac{-4}{8.0622577483}$$\n",
152 | "\n",
153 | "Which calculates to:\n",
154 | "\n",
155 | "$$\\cos(\\theta) = -0.496138938357 $$\n",
156 | "\n",
157 | "So:\n",
158 | "\n",
159 | "$$\\theta \\approx 119.74 $$\n",
160 | "\n",
161 | "Here's that calculation in R:"
162 | ]
163 | },
164 | {
165 | "cell_type": "code",
166 | "execution_count": null,
167 | "metadata": {
168 | "collapsed": true
169 | },
170 | "outputs": [],
171 | "source": [
172 | "norm_v = norm(v, '2')\n",
173 | "norm_s = norm(s, '2')\n",
174 | "\n",
175 | "cos_v_s = dot_v_s/(norm_v*norm_s)\n",
176 | "NISTradianTOdeg(acos(cos_v_s))"
177 | ]
178 | },
179 | {
180 | "cell_type": "markdown",
181 | "metadata": {
182 | "collapsed": true
183 | },
184 | "source": [
185 | "## Cross Product Multiplication\n",
186 | "To get the *vector product* of multiplying two vectors together, you must calculate the *cross product*. The result of this is a new vector that is at right angles to both the other vectors in 3D Euclidean space. This means that the cross-product only really makes sense when working with vectors that contain three components.\n",
187 | "\n",
188 | "For example, let's suppose we have the following vectors:\n",
189 | "\n",
190 | "\\begin{equation}\\vec{p} = \\begin{bmatrix}2 \\\\ 3 \\\\ 1 \\end{bmatrix}\\;\\; \\vec{q} = \\begin{bmatrix}1 \\\\ 2 \\\\ -2 \\end{bmatrix}\\end{equation}\n",
191 | "\n",
192 | "To calculate the cross product of these vectors, written as **p** x **q**, we need to create a new vector (let's call it **r**) with three components (r1, r2, and r3). The values for these components are calculated like this:\n",
193 | "\n",
194 | "\\begin{equation}r_{1} = p_{2}q_{3} - p_{3}q_{2}\\end{equation}\n",
195 | "\\begin{equation}r_{2} = p_{3}q_{1} - p_{1}q_{3}\\end{equation}\n",
196 | "\\begin{equation}r_{3} = p_{1}q_{2} - p_{2}q_{1}\\end{equation}\n",
197 | "\n",
198 | "So in our case:\n",
199 | "\n",
200 | "\\begin{equation}\\vec{r} = \\vec{p} \\times \\vec{q} = \\begin{bmatrix}(3 \\cdot -2) - (1 \\cdot 2) \\\\ (1 \\cdot 1) - (2 \\cdot -2) \\\\ (2 \\cdot 2) - (3 \\cdot 1) \\end{bmatrix} = \\begin{bmatrix}-6 - 2 \\\\ 1 - -4 \\\\ 4 - 3 \\end{bmatrix} = \\begin{bmatrix}-8 \\\\ 5 \\\\ 1 \\end{bmatrix}\\end{equation}\n",
201 | "\n",
202 | "In R you can use the **tcrossprod** function to compute the outer cross product of two vector arrays:"
203 | ]
204 | },
205 | {
206 | "cell_type": "code",
207 | "execution_count": null,
208 | "metadata": {
209 | "collapsed": true
210 | },
211 | "outputs": [],
212 | "source": [
213 | "## Need the cross function from the pracma package\n",
214 | "install.packages('pracma')\n",
215 | "library(pracma)\n",
216 | "\n",
217 | "## Define the two vectors\n",
218 | "p = c(2,3,1)\n",
219 | "q = c(1,2,-2)\n",
220 | "\n",
221 | "## Outer cross product of two vectors\n",
222 | "cat('\\nCross product of vectors')\n",
223 | "cross(p,q)"
224 | ]
225 | }
226 | ],
227 | "metadata": {
228 | "kernelspec": {
229 | "display_name": "R",
230 | "language": "R",
231 | "name": "ir"
232 | },
233 | "language_info": {
234 | "codemirror_mode": "r",
235 | "file_extension": ".r",
236 | "mimetype": "text/x-r-source",
237 | "name": "R",
238 | "pygments_lexer": "r",
239 | "version": "3.5.0"
240 | }
241 | },
242 | "nbformat": 4,
243 | "nbformat_minor": 2
244 | }
245 |
--------------------------------------------------------------------------------
/Python/Module02/02-01-Rate of Change.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Rate of Change\n",
8 | "Functions are often visualized as a line on a graph, and this line shows how the value returned by the function changes based on changes in the input value.\n",
9 | "\n",
10 | "## Linear Rate of Change\n",
11 | "\n",
12 | "For example, imagine a function that returns the number of meters travelled by a cyclist based on the number of seconds that the cyclist has been cycling.\n",
13 | "\n",
14 | "Here is such a function:\n",
15 | "\n",
16 | "\\begin{equation}q(x) = 2x + 1\\end{equation}\n",
17 | "\n",
18 | "We can plot the output for this function for a period of 10 seconds like this:"
19 | ]
20 | },
21 | {
22 | "cell_type": "code",
23 | "execution_count": null,
24 | "metadata": {},
25 | "outputs": [],
26 | "source": [
27 | "%matplotlib inline\n",
28 | "\n",
29 | "def q(x):\n",
30 | " return 2*x + 1\n",
31 | "\n",
32 | "# Plot the function\n",
33 | "import numpy as np\n",
34 | "from matplotlib import pyplot as plt\n",
35 | "\n",
36 | "# Create an array of x values from 0 to 10\n",
37 | "x = np.array(range(0, 11))\n",
38 | "\n",
39 | "# Set up the graph\n",
40 | "plt.xlabel('Seconds')\n",
41 | "plt.ylabel('Meters')\n",
42 | "plt.xticks(range(0,11, 1))\n",
43 | "plt.yticks(range(0, 22, 1))\n",
44 | "plt.grid()\n",
45 | "\n",
46 | "# Plot x against q(x)\n",
47 | "plt.plot(x,q(x), color='green')\n",
48 | "\n",
49 | "plt.show()"
50 | ]
51 | },
52 | {
53 | "cell_type": "markdown",
54 | "metadata": {},
55 | "source": [
56 | "It's clear from the graph that ***q*** is a *linear* function that describes a slope in which distance increases at a constant rate over time. In other words, the cyclist is travelling at a constant speed.\n",
57 | "\n",
58 | "But what speed?\n",
59 | "\n",
60 | "Speed, or more technically, velocity is a measure of change - it measures how the distance travelled changes over time (which is why we typically express it as a unit of distance per a unit of time, like *miles-per-hour* or *meters-per-second*). So we're looking for a way to measure the change in the line created by the function.\n",
61 | "\n",
62 | "The change in values along the line define its *slope*, which we know from a previous lesson is represented like this:\n",
63 | "\n",
64 | "\\begin{equation}m = \\frac{\\Delta{y}}{\\Delta{x}} \\end{equation}\n",
65 | "\n",
66 | "We can calculate the slope of our function like this:\n",
67 | "\n",
68 | "\\begin{equation}m = \\frac{q(x)_{2} - q(x)_{1}}{x_{2} - x_{1}} \\end{equation}\n",
69 | "\n",
70 | "So we just need two ordered pairs of ***x*** and ***q(x)*** values from our line to apply this equation.\n",
71 | "\n",
72 | "- After 1 second, ***x*** is 1 and ***q***(1) = **3**.\n",
73 | "- After 10 seconds, ***x*** is 10 and ***q***(10) = 21.\n",
74 | "\n",
75 | "So we can meassure the rate of change like this:\n",
76 | "\n",
77 | "\\begin{equation}m = \\frac{21 - 3}{10 - 1} \\end{equation}\n",
78 | "\n",
79 | "This is the same as:\n",
80 | "\n",
81 | "\\begin{equation}m = \\frac{18}{9} \\end{equation}\n",
82 | "\n",
83 | "Which simplifies to:\n",
84 | "\n",
85 | "\\begin{equation}m = \\frac{2}{1} \\end{equation}\n",
86 | "\n",
87 | "So our rate of change is 2/1 or put another way, the cyclist is travelling at 2 meters-per-second."
88 | ]
89 | },
90 | {
91 | "cell_type": "markdown",
92 | "metadata": {},
93 | "source": [
94 | "## Average Rate of Change\n",
95 | "OK, let's look at another function that calculates distance travelled for a given number of seconds:\n",
96 | "\n",
97 | "\\begin{equation}r(x) = x^{2} + x\\end{equation}\n",
98 | "\n",
99 | "Let's take a look at that using Python:"
100 | ]
101 | },
102 | {
103 | "cell_type": "code",
104 | "execution_count": null,
105 | "metadata": {
106 | "collapsed": true
107 | },
108 | "outputs": [],
109 | "source": [
110 | "%matplotlib inline\n",
111 | "\n",
112 | "def r(x):\n",
113 | " return x**2 + x\n",
114 | "\n",
115 | "# Plot the function\n",
116 | "import numpy as np\n",
117 | "from matplotlib import pyplot as plt\n",
118 | "\n",
119 | "# Create an array of x values from 0 to 10\n",
120 | "x = np.array(range(0, 11))\n",
121 | "\n",
122 | "# Set up the graph\n",
123 | "plt.xlabel('Seconds')\n",
124 | "plt.ylabel('Meters')\n",
125 | "plt.grid()\n",
126 | "\n",
127 | "# Plot x against r(x)\n",
128 | "plt.plot(x,r(x), color='green')\n",
129 | "\n",
130 | "plt.show()"
131 | ]
132 | },
133 | {
134 | "cell_type": "markdown",
135 | "metadata": {},
136 | "source": [
137 | "This time, the function is not linear. It's actually a quadratic function, and the line from 0 seconds to 10 seconds shows an exponential increase; in other words, the cyclist is *accelerating*.\n",
138 | "\n",
139 | "Technically, acceleration itself is a measure of change in velocity over time; and velocity, as we've already discussed, is a measure of change in distance over time. So measuring accelleration is pretty complex, and requires *differential calculus*, which we're going to cover shortly. In fact, even just measuring the velocity at a single point in time requires differential calculus; but we can use algebraic methods to calculate an *average* rate of velocity for a given period shown in the graph.\n",
140 | "\n",
141 | "First, we need to define a *secant* line that joins two points in our exponential arc to create a straight slope. For example, a secant line for the entire 10 second time span would join the following two points:\n",
142 | "\n",
143 | "- 0, ***r***(0)\n",
144 | "- 10, ***r***(10)\n",
145 | "\n",
146 | "Run the following Python code to visualize this line:"
147 | ]
148 | },
149 | {
150 | "cell_type": "code",
151 | "execution_count": null,
152 | "metadata": {
153 | "collapsed": true
154 | },
155 | "outputs": [],
156 | "source": [
157 | "%matplotlib inline\n",
158 | "\n",
159 | "def r(x):\n",
160 | " return (x)**2 + x\n",
161 | "\n",
162 | "# Plot the function\n",
163 | "import numpy as np\n",
164 | "from matplotlib import pyplot as plt\n",
165 | "\n",
166 | "# Create an array of x values from 0 to 10\n",
167 | "x = np.array(range(0, 11))\n",
168 | "\n",
169 | "# Create an array for the secant line\n",
170 | "s = np.array([0,10])\n",
171 | "\n",
172 | "# Set up the graph\n",
173 | "plt.xlabel('Seconds')\n",
174 | "plt.ylabel('Meters')\n",
175 | "plt.grid()\n",
176 | "\n",
177 | "# Plot x against r(x)\n",
178 | "plt.plot(x,r(x), color='green')\n",
179 | "\n",
180 | "# Plot the secant line\n",
181 | "plt.plot(s,r(s), color='magenta')\n",
182 | "\n",
183 | "plt.show()"
184 | ]
185 | },
186 | {
187 | "cell_type": "markdown",
188 | "metadata": {},
189 | "source": [
190 | "Now, because the secant line is straight, we can apply the slope formula we used for a linear function to calculate the average velocity for the 10 second period:\n",
191 | "\n",
192 | "- At 0 seconds, ***x*** is 0 and ***r***(0) = **0**.\n",
193 | "- At 10 seconds, ***x*** is 10 and ***r***(10) = 110.\n",
194 | "\n",
195 | "So we can meassure the rate of change like this:\n",
196 | "\n",
197 | "\\begin{equation}m = \\frac{110 - 0}{10 - 0} \\end{equation}\n",
198 | "\n",
199 | "This is the same as:\n",
200 | "\n",
201 | "\\begin{equation}m = \\frac{110}{10} \\end{equation}\n",
202 | "\n",
203 | "Which simplifies to:\n",
204 | "\n",
205 | "\\begin{equation}m = \\frac{11}{1} \\end{equation}\n",
206 | "\n",
207 | "So our rate of change is 11/1 or put another way, the cyclist is travelling at an average velocity of 11 meters-per-second over the 10-second period.\n",
208 | "\n",
209 | "Of course, we can measure the average velocity between any two points on the exponential line. Use the following Python code to show the secant line for the period between 2 and 7 seconds, and calculate the average velocity for that period"
210 | ]
211 | },
212 | {
213 | "cell_type": "code",
214 | "execution_count": null,
215 | "metadata": {
216 | "collapsed": true
217 | },
218 | "outputs": [],
219 | "source": [
220 | "%matplotlib inline\n",
221 | "\n",
222 | "def r(x):\n",
223 | " return x**2 + x\n",
224 | "\n",
225 | "# Plot the function\n",
226 | "import numpy as np\n",
227 | "from matplotlib import pyplot as plt\n",
228 | "\n",
229 | "# Create an array of x values from 0 to 10\n",
230 | "x = np.array(range(0, 11))\n",
231 | "\n",
232 | "# Create an array for the secant line\n",
233 | "s = np.array([2,7])\n",
234 | "\n",
235 | "# Calculate rate of change\n",
236 | "x1 = s[0]\n",
237 | "x2 = s[-1]\n",
238 | "y1 = r(x1)\n",
239 | "y2 = r(x2)\n",
240 | "a = (y2 - y1)/(x2 - x1)\n",
241 | "\n",
242 | "\n",
243 | "# Set up the graph\n",
244 | "plt.xlabel('Seconds')\n",
245 | "plt.ylabel('Meters')\n",
246 | "plt.grid()\n",
247 | "\n",
248 | "# Plot x against r(x)\n",
249 | "plt.plot(x,r(x), color='green')\n",
250 | "\n",
251 | "# Plot the secant line\n",
252 | "plt.plot(s,r(s), color='magenta')\n",
253 | "\n",
254 | "plt.annotate('Average Velocity =' + str(a) + ' m/s',((x2+x1)/2, (y2+y1)/2))\n",
255 | "\n",
256 | "plt.show()\n",
257 | "\n"
258 | ]
259 | }
260 | ],
261 | "metadata": {
262 | "kernelspec": {
263 | "display_name": "Python 3.6",
264 | "language": "python",
265 | "name": "python36"
266 | },
267 | "language_info": {
268 | "codemirror_mode": {
269 | "name": "ipython",
270 | "version": 3
271 | },
272 | "file_extension": ".py",
273 | "mimetype": "text/x-python",
274 | "name": "python",
275 | "nbconvert_exporter": "python",
276 | "pygments_lexer": "ipython3",
277 | "version": "3.6.3"
278 | }
279 | },
280 | "nbformat": 4,
281 | "nbformat_minor": 2
282 | }
283 |
--------------------------------------------------------------------------------
/R/Module02/02-06-Integration.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Introduction to Integration\n",
8 | "\n",
9 | "Integrals are the inverses of derivatives. More importantly, using integration provides a way to compute the area under the curve of most any function. There are many applications for integration. For example, if you need to compute a probability of some occurrence between limits (which we'll discuss later in this course), then you will use an integral.\n",
10 | "\n",
11 | "Let's start with a simple function:\n",
12 | "\n",
13 | "$$f(x)=x$$\n",
14 | "\n",
15 | "We can plot this function as a line. Run the code below to plot the function for the range 0 to 10:"
16 | ]
17 | },
18 | {
19 | "cell_type": "code",
20 | "execution_count": null,
21 | "metadata": {},
22 | "outputs": [],
23 | "source": [
24 | "library(ggplot2)\n",
25 | "library(repr)\n",
26 | "options(repr.plot.width=3.5, repr.plot.height=3) # Set the initial plot area dimensions\n",
27 | "\n",
28 | "## Define the function\n",
29 | "f = function(x) x\n",
30 | "\n",
31 | "df1 = data.frame(x = seq(0,10))\n",
32 | "df1$f = f(df1$x)\n",
33 | "\n",
34 | "ggplot(df1, aes(x,f)) +\n",
35 | " geom_line(color = 'magenta', size = 0.8) +\n",
36 | " ylab('f(x)') + \n",
37 | " xlab('x')"
38 | ]
39 | },
40 | {
41 | "cell_type": "markdown",
42 | "metadata": {},
43 | "source": [
44 | "## Performing Integration\n",
45 | "\n",
46 | "The *integral* of a function is the area under it - in this case, the area under the purple diagonal line down to the x-axis.\n",
47 | "\n",
48 | "So how do you find the integral of a function? well, for our simple function $f(x) = x$, the formula for an integral is written as follows:\n",
49 | "\n",
50 | "$$\\int f(x)\\;dx$$\n",
51 | "\n",
52 | "The $\\int$ symbol shows that this formula is an integral. The $dx$ indicates that the integration is with respect to the $x$ variable. Note that since $f(x) = x$, we could also write this integral formula as $\\int x\\;dx$\n",
53 | "\n",
54 | "So, what is the integral of $x\\ dx$? To answer this question, we need the *anti-derivative* of ***f*** - in other words we need to find a function which has a derivative matching the output of $f$, which is just $x$. Using the power rule in reverse, a function that has the derivative $x$ would be $\\frac{1}{2} x^2$\n",
55 | "\n",
56 | "So, the *unbound* integral formula for $f$ with respect to $x$ can be written as:\n",
57 | "\n",
58 | "$$\\int f(x)\\;dx = \\frac{1}{2} x^2$$"
59 | ]
60 | },
61 | {
62 | "cell_type": "markdown",
63 | "metadata": {},
64 | "source": [
65 | "## Integration between Limits\n",
66 | "\n",
67 | "Now that we have the unbound integral formula, we can use it to find the integral between specific start and end points. Let's suppose we want to find the area under the function between the $x$ values 0 and 2. In other words, the *integral* of $f$ for the range 0 to 2 with respect to $x$.\n",
68 | "\n",
69 | "Run the following code to re-plot the function and show the area we're interested in:"
70 | ]
71 | },
72 | {
73 | "cell_type": "code",
74 | "execution_count": null,
75 | "metadata": {},
76 | "outputs": [],
77 | "source": [
78 | "## Data frame for shaded area polygon\n",
79 | "shade = data.frame(x=c(0,2,2), y=c(0,2,0))\n",
80 | "\n",
81 | "ggplot(df1, aes(x,f)) +\n",
82 | " geom_line(color = 'magenta', size = 0.8) +\n",
83 | " geom_polygon(data = shade, aes(x = x, y = y), fill = 'orange') +\n",
84 | " ylab('f(x)') + \n",
85 | " xlab('x')"
86 | ]
87 | },
88 | {
89 | "cell_type": "markdown",
90 | "metadata": {},
91 | "source": [
92 | "We call the start and end point the **limits** of the integral. The lower limit is placed as a subscript of the integral sign. The upper limit is placed as a superscript of the integral sign. Using this notation the integral of $f(x)$ from $0$ to $2$ is written as follows:\n",
93 | "\n",
94 | "$$\\int_0^2 f(x)\\;dx$$\n",
95 | "\n",
96 | "The integral is evaluated by subtracting the value of the integrand at the lower limit from the integrand at the upper limit; and since we know the formula based on our anti-derivative function, the integral can be evaluated in the following manner.\n",
97 | "\n",
98 | "$$\\int_0^2 f(x)\\;dx\\\\ = \\frac{1}{2} x^2\\ \\big|_0^2\\\\\n",
99 | "= \\frac{1}{2} 2^2 - \\frac{1}{2} 0^2\\\\\n",
100 | "= \\frac{4}{2} - \\frac{0}{2} x^2\\\\ = 2$$"
101 | ]
102 | },
103 | {
104 | "cell_type": "markdown",
105 | "metadata": {},
106 | "source": [
107 | "Execute the code in the cell below and verify that the result returned are approximately correct. This code uses the R **integrate** function to numerically approximate the integral of a one dimensional function. "
108 | ]
109 | },
110 | {
111 | "cell_type": "code",
112 | "execution_count": null,
113 | "metadata": {},
114 | "outputs": [],
115 | "source": [
116 | "integrate(f, 0, 2)$value"
117 | ]
118 | },
119 | {
120 | "cell_type": "markdown",
121 | "metadata": {},
122 | "source": [
123 | "## Another Integral\n",
124 | "\n",
125 | "Here is another example for a slightly more complex function. What is the area under the curve of the function $3x^2 + 2x + 1$ between $0$ and $3$?\n",
126 | "\n",
127 | "let's look at that function and the area in question:"
128 | ]
129 | },
130 | {
131 | "cell_type": "code",
132 | "execution_count": null,
133 | "metadata": {},
134 | "outputs": [],
135 | "source": [
136 | "## Define the function\n",
137 | "f = function(x) 3 * x^2 + 2 * x + 1\n",
138 | "\n",
139 | "## Data frame of function values\n",
140 | "df2 = data.frame(x = seq(0,10))\n",
141 | "df2$f = f(df2$x)\n",
142 | "\n",
143 | "## Data frame for shaded area polygon\n",
144 | "shade2 = data.frame(x = c(seq(0,3), 3, 0))\n",
145 | "shade2$y = c(f(shade2$x[1:4]), 0, 0)\n",
146 | "\n",
147 | "ggplot(df2, aes(x,f)) +\n",
148 | " geom_line(color = 'magenta', size = 0.8) +\n",
149 | " geom_polygon(data = shade2, aes(x = x, y = y), fill = 'orange') +\n",
150 | " ylab('f(x)') + \n",
151 | " xlab('x')"
152 | ]
153 | },
154 | {
155 | "cell_type": "markdown",
156 | "metadata": {},
157 | "source": [
158 | "We can evaluate this integral just as before, this time using function:\n",
159 | "\n",
160 | "$$\\int_0^3 3x^2 + 2x + 1\\;dx$$ \n",
161 | "\n",
162 | "We can calculate the anti-derivative of $3x^2 + 2x + 1\\;dx$ as $\\frac{3}{3} x^3 + \\frac{2}{2} x^2 + x$, so:\n",
163 | "\n",
164 | "$$\\int_0^3= \\frac{3}{3} x^3 + \\frac{2}{2} x^2 + x\\ \\big|_0^3\\\\\n",
165 | "= \\frac{3}{3} 3^3 + \\frac{2}{2} 3^2 + 3 - \\frac{3}{3} 0^3 - \\frac{2}{2} 0^2 + 0\\\\\n",
166 | "= 27 + 9 + 3 + 0 + 0 + 0\\\\\n",
167 | "= 39$$\n",
168 | "\n",
169 | "Now, execute the code in the cell below to verify the result:"
170 | ]
171 | },
172 | {
173 | "cell_type": "code",
174 | "execution_count": null,
175 | "metadata": {},
176 | "outputs": [],
177 | "source": [
178 | "integral = integrate(f, 0, 3)\n",
179 | "integral$value"
180 | ]
181 | },
182 | {
183 | "cell_type": "markdown",
184 | "metadata": {},
185 | "source": [
186 | "The result return by the R **integrate** function is approximate - the function actually returns an estimated integral (*i* in this case) and also a measure of absolute error (*e*). Run the following code to display the absolute error:"
187 | ]
188 | },
189 | {
190 | "cell_type": "code",
191 | "execution_count": null,
192 | "metadata": {},
193 | "outputs": [],
194 | "source": [
195 | "integral$abs.error"
196 | ]
197 | },
198 | {
199 | "cell_type": "markdown",
200 | "metadata": {},
201 | "source": [
202 | "The absolute error in this case is extremely small (around $4.3 \\times 10^{-13}$).\n",
203 | "\n",
204 | "## Infinite limits\n",
205 | "\n",
206 | "In many cases the limits of an integral can be $+/- \\infty$. Perhaps suprisingly, this situation is not really a problem if the function being integrated converges to $0$ at the infinite limit. \n",
207 | "\n",
208 | "Here is an example. The function $e^{-5x} \\rightarrow 0$ as $x \\rightarrow \\infty$. Therefore, the integral of this function from some limit to $\\infty$. This integral can be written as follows:\n",
209 | "\n",
210 | "$$\\int^{\\infty}_0 e^{-5x} dx$$\n",
211 | "\n",
212 | "The code in the cell below computes this integral numerically. **Inf** is used to represent infinity in R. "
213 | ]
214 | },
215 | {
216 | "cell_type": "code",
217 | "execution_count": null,
218 | "metadata": {},
219 | "outputs": [],
220 | "source": [
221 | "integrate(function(x) exp(-5*x), 0, Inf)"
222 | ]
223 | },
224 | {
225 | "cell_type": "markdown",
226 | "metadata": {},
227 | "source": [
228 | "This integral converges to a small number with a much smaller error estimate. \n",
229 | "\n",
230 | "Here is another example that illustrates why having infinite integration limits is so useful. When computing probabilities it is often necessary to have infinite limits. Don't worry too much about the details of probability theory. This is covered in a later lesson. \n",
231 | "\n",
232 | "A Normal distribution with zero mean and a standard deviation of $1$ has the following density function:\n",
233 | "\n",
234 | "$$\\frac{1}{\\sqrt{2 \\pi}} e^{\\frac{-x^2}{2}}$$\n",
235 | "\n",
236 | "It makes sense that the integral of this probability density function from $-\\infty$ to $\\infty$ must be $1.0$. In other words the probability of a Normally distributed event occurring at all possible values must be $1.0$. \n",
237 | "\n",
238 | "The code in the cell below computes the following integral:\n",
239 | "\n",
240 | "$$\\int_{-\\infty}^{\\infty} \\frac{1}{\\sqrt{2 \\pi}} e^{\\frac{-x^2}{2}} dx$$\n",
241 | "\n",
242 | "Execute this code and verify that the result is approximately $1.0$."
243 | ]
244 | },
245 | {
246 | "cell_type": "code",
247 | "execution_count": null,
248 | "metadata": {},
249 | "outputs": [],
250 | "source": [
251 | "integrate(function(x) (1/sqrt(2 * pi)) * exp(-x^2/2.0), -Inf, Inf)"
252 | ]
253 | }
254 | ],
255 | "metadata": {
256 | "anaconda-cloud": {},
257 | "kernelspec": {
258 | "display_name": "R",
259 | "language": "R",
260 | "name": "ir"
261 | },
262 | "language_info": {
263 | "codemirror_mode": "r",
264 | "file_extension": ".r",
265 | "mimetype": "text/x-r-source",
266 | "name": "R",
267 | "pygments_lexer": "r",
268 | "version": "3.5.0"
269 | }
270 | },
271 | "nbformat": 4,
272 | "nbformat_minor": 1
273 | }
274 |
--------------------------------------------------------------------------------
/Python/Module02/02-06-Integration.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Introduction to Integration\n",
8 | "\n",
9 | "Integrals are the inverses of derivatives. More importantly, using integration provides a way to compute the area under the curve of most any function. There are many applications for integration. For example, if you need to compute a probability of some occurance between limits (which we'll discuss later in this course), then you will use an integral.\n",
10 | "\n",
11 | "Let's start with a simple function:\n",
12 | "\n",
13 | "$$f(x)=x$$\n",
14 | "\n",
15 | "We can plot this function as a line. Run the code below to plot the function for the range 0 to 10:"
16 | ]
17 | },
18 | {
19 | "cell_type": "code",
20 | "execution_count": null,
21 | "metadata": {},
22 | "outputs": [],
23 | "source": [
24 | "import numpy as np\n",
25 | "from matplotlib import pyplot as plt\n",
26 | "%matplotlib inline\n",
27 | "\n",
28 | "# Define function f\n",
29 | "def f(x):\n",
30 | " return x\n",
31 | "\n",
32 | "# Create an array of x values from 0 to 10\n",
33 | "x = range(0, 11)\n",
34 | "\n",
35 | "# Get the corresponding y values from the function\n",
36 | "y = [f(a) for a in x]\n",
37 | "\n",
38 | "# Set up the plot\n",
39 | "plt.xlabel('x')\n",
40 | "plt.ylabel('f(x)')\n",
41 | "plt.grid()\n",
42 | "\n",
43 | "# Plot x against f(x)\n",
44 | "plt.plot(x,y, color='purple')\n",
45 | "\n",
46 | "plt.show()"
47 | ]
48 | },
49 | {
50 | "cell_type": "markdown",
51 | "metadata": {},
52 | "source": [
53 | "## Performing Integration\n",
54 | "\n",
55 | "The *integral* of a function is the area under it - in this case, the area under the purple diagonal line down to the x-axis.\n",
56 | "\n",
57 | "So how do you find the integral of a function? well, for our simple function $f(x) = x$, the formula for an integral is written as follows:\n",
58 | "\n",
59 | "$$\\int f(x)\\;dx$$\n",
60 | "\n",
61 | "The $\\int$ symbol shows that this formula is an integral. The $dx$ indicates that the integration is with respect to the $x$ variable. Note that since $f(x) = x$, we could also write this integral formula as $\\int x\\;dx$\n",
62 | "\n",
63 | "So, what is the integral of $x\\ dx$? To answer this question, we need the *antiderivative* of ***f*** - in other words we need to find a function which has a derivative matching the output of $f$, which is just $x$. Using the power rule in reverse, a function that has the derivative $x$ would be $\\frac{1}{2} x^2$\n",
64 | "\n",
65 | "So, the *unbound* integral formula for $f$ with respect to $x$ can be written as:\n",
66 | "\n",
67 | "$$\\int f(x)\\;dx = \\frac{1}{2} x^2$$"
68 | ]
69 | },
70 | {
71 | "cell_type": "markdown",
72 | "metadata": {},
73 | "source": [
74 | "## Integration between Limits\n",
75 | "\n",
76 | "Now that we have the unbound integral formula, we can use it to find the integral between specific start and end points. Let's suppose we want to find the area under the function between the $x$ values 0 and 2. In other words, the *integral* of $f$ for the range 0 to 2 with respect to $x$.\n",
77 | "\n",
78 | "Run the following code to re-plot the function and show the area we're interested in:"
79 | ]
80 | },
81 | {
82 | "cell_type": "code",
83 | "execution_count": null,
84 | "metadata": {},
85 | "outputs": [],
86 | "source": [
87 | "# Set up the plot\n",
88 | "plt.xlabel('x')\n",
89 | "plt.ylabel('f(x)')\n",
90 | "plt.grid()\n",
91 | "\n",
92 | "# Plot x against f(x)\n",
93 | "plt.plot(x,y, color='purple')\n",
94 | "\n",
95 | "# show area for integral\n",
96 | "section = np.arange(0, 2, 1/20)\n",
97 | "plt.fill_between(section,f(section), color='orange')\n",
98 | "\n",
99 | "plt.show()"
100 | ]
101 | },
102 | {
103 | "cell_type": "markdown",
104 | "metadata": {},
105 | "source": [
106 | "We call the start and end point the **limits** of the integral. The lower limit is placed as a subscript of the integral sign. The upper limit is placed as a superscript of the integral sign. Using this notation the integral of $f(x)$ from $0$ to $2$ is written as follows:\n",
107 | "\n",
108 | "$$\\int_0^2 f(x)\\;dx$$\n",
109 | "\n",
110 | "The integral is evaluated by subtracting the value of the integrand at the lower limit from the integrand at the upper limit; and since we know the formula based on our antiderivative function, the integral can be evaluated in the following manner.\n",
111 | "\n",
112 | "$$\\int_0^2 f(x)\\;dx\\\\ = \\frac{1}{2} x^2\\ \\big|_0^2\\\\\n",
113 | "= \\frac{1}{2} 2^2 - \\frac{1}{2} 0^2\\\\\n",
114 | "= \\frac{4}{2} - \\frac{0}{2} x^2\\\\ = 2$$"
115 | ]
116 | },
117 | {
118 | "cell_type": "markdown",
119 | "metadata": {},
120 | "source": [
121 | "Execute the code in the cell below and verify that the result returned by the ***scipy.integrate.quad*** function in Python is approximately the same as we computed analytically. "
122 | ]
123 | },
124 | {
125 | "cell_type": "code",
126 | "execution_count": null,
127 | "metadata": {},
128 | "outputs": [],
129 | "source": [
130 | "import scipy.integrate as integrate\n",
131 | "i, e = integrate.quad(lambda x: f(x), 0, 2)\n",
132 | "print (i)"
133 | ]
134 | },
135 | {
136 | "cell_type": "markdown",
137 | "metadata": {},
138 | "source": [
139 | "## Another Integral\n",
140 | "\n",
141 | "Here is another example for a slightly more complex function. What is the area under the curve of the function $3x^2 + 2x + 1$ between $0$ and $3$?\n",
142 | "\n",
143 | "let's look at that function and the area in question:"
144 | ]
145 | },
146 | {
147 | "cell_type": "code",
148 | "execution_count": null,
149 | "metadata": {},
150 | "outputs": [],
151 | "source": [
152 | "import numpy as np\n",
153 | "from matplotlib import pyplot as plt\n",
154 | "from matplotlib.patches import Polygon\n",
155 | "%matplotlib inline\n",
156 | "\n",
157 | "# Define function g\n",
158 | "def g(x):\n",
159 | " return 3 * x**2 + 2 * x + 1\n",
160 | "\n",
161 | "# Create an array of x values from 0 to 10\n",
162 | "x = range(0, 11)\n",
163 | "\n",
164 | "# Get the corresponding y values from the function\n",
165 | "y = [g(a) for a in x]\n",
166 | "\n",
167 | "# Set up the plot\n",
168 | "fig, ax = plt.subplots()\n",
169 | "plt.xlabel('x')\n",
170 | "plt.ylabel('f(x)')\n",
171 | "plt.grid()\n",
172 | "\n",
173 | "# Plot x against g(x)\n",
174 | "plt.plot(x,y, color='purple')\n",
175 | "\n",
176 | "# Make the shaded region\n",
177 | "ix = np.linspace(0, 3)\n",
178 | "iy = g(ix)\n",
179 | "verts = [(0, 0)] + list(zip(ix, iy)) + [(3, 0)]\n",
180 | "poly = Polygon(verts, facecolor='orange')\n",
181 | "ax.add_patch(poly)\n",
182 | "\n",
183 | "plt.show()"
184 | ]
185 | },
186 | {
187 | "cell_type": "markdown",
188 | "metadata": {},
189 | "source": [
190 | "We can evaluate this integral just as before, this time using function:\n",
191 | "\n",
192 | "$$\\int_0^3 3x^2 + 2x + 1\\;dx$$ \n",
193 | "\n",
194 | "We can calculate the antiderivative of $3x^2 + 2x + 1\\;dx$ as $\\frac{3}{3} x^3 + \\frac{2}{2} x^2 + x$, so:\n",
195 | "\n",
196 | "$$\\int_0^3= \\frac{3}{3} x^3 + \\frac{2}{2} x^2 + x\\ \\big|_0^3\\\\\n",
197 | "= \\frac{3}{3} 3^3 + \\frac{2}{2} 3^2 + 3 - \\frac{3}{3} 0^3 - \\frac{2}{2} 0^2 + 0\\\\\n",
198 | "= 27 + 9 + 3 + 0 + 0 + 0\\\\\n",
199 | "= 39$$\n",
200 | "\n",
201 | "Now, execute the code in the cell below to verify the result:"
202 | ]
203 | },
204 | {
205 | "cell_type": "code",
206 | "execution_count": null,
207 | "metadata": {},
208 | "outputs": [],
209 | "source": [
210 | "i, e = integrate.quad(lambda x: 3 * x**2 + 2 * x + 1, 0, 3)\n",
211 | "print(i)"
212 | ]
213 | },
214 | {
215 | "cell_type": "markdown",
216 | "metadata": {},
217 | "source": [
218 | "Note that the result from the ***scipy.integrate.quad*** function is approximate - the function actually returns an estimated integral (*i* in this case) and also a measure of absolute error (*e*). Run the following code to see what the absolute error was in this case:"
219 | ]
220 | },
221 | {
222 | "cell_type": "code",
223 | "execution_count": null,
224 | "metadata": {},
225 | "outputs": [],
226 | "source": [
227 | "print(e)"
228 | ]
229 | },
230 | {
231 | "cell_type": "markdown",
232 | "metadata": {},
233 | "source": [
234 | "The absolute error in this case is extremely small (around $4.3 \\times 10^{-13}$).\n",
235 | "\n",
236 | "## Infinite limits\n",
237 | "\n",
238 | "In many cases the limits of an integral can be $+/- \\infty$. Perhaps suprisingly, this situation is not really a problem if the function being integrated converges to $0$ at the infinite limit. \n",
239 | "\n",
240 | "Here is an example. The function $e^{-5x} \\rightarrow 0$ as $x \\rightarrow \\infty$. Therefore, the integral of this function from some limit to $\\infty$. This integral can be written as follows:\n",
241 | "\n",
242 | "$$\\int^{\\infty}_0 e^{-5x} dx$$\n",
243 | "\n",
244 | "The code in the cell below computes this integral numerically."
245 | ]
246 | },
247 | {
248 | "cell_type": "code",
249 | "execution_count": null,
250 | "metadata": {},
251 | "outputs": [],
252 | "source": [
253 | "import numpy as np\n",
254 | "i, e = integrate.quad(lambda x: np.exp(-x*5), 0, np.inf)\n",
255 | "\n",
256 | "print('Integral: ' + str(i))\n",
257 | "print('Absolute Error: ' + str(e))"
258 | ]
259 | },
260 | {
261 | "cell_type": "markdown",
262 | "metadata": {},
263 | "source": [
264 | "This integral converges to a small number with a much smaller error estimate. \n",
265 | "\n",
266 | "Here is another example that illustrates why having infinite integration limits is so useful. When computing probabilities it is often necessary to have infinite limits. Don't worry too much about the details of probability theory. This is covered in a later lesson. \n",
267 | "\n",
268 | "A Normal distribution with zero mean and a standard deviation of $1$ has the following density function:\n",
269 | "\n",
270 | "$$\\frac{1}{2 \\pi} e^{\\frac{-x^2}{\\sqrt(2 \\pi)}}$$\n",
271 | "\n",
272 | "It makes sense that the integral of this probability density function from $-\\infty$ to $\\infty$ must be $1.0$. In other words the probability of a Normally distributed event ocurring at all possible values must be $1.0$. \n",
273 | "\n",
274 | "The code in the cell below computes the following integral:\n",
275 | "\n",
276 | "$$\\int_{-\\infty}^{\\infty} \\frac{1}{2 \\pi} e^{\\frac{-x^2}{\\sqrt(2 \\pi)}} dx$$\n",
277 | "\n",
278 | "Execute this code and verify that the result is approximately $1.0$."
279 | ]
280 | },
281 | {
282 | "cell_type": "code",
283 | "execution_count": null,
284 | "metadata": {},
285 | "outputs": [],
286 | "source": [
287 | "import numpy as np\n",
288 | "norms = lambda x: np.exp(-x**2/2.0)/np.sqrt(2.0 * 3.14159)\n",
289 | "i, e = integrate.quad(norms, -np.inf, np.inf)\n",
290 | "\n",
291 | "print('Integral: ' + str(i))\n",
292 | "print('Absolute Error: ' + str(e))"
293 | ]
294 | },
295 | {
296 | "cell_type": "code",
297 | "execution_count": null,
298 | "metadata": {},
299 | "outputs": [],
300 | "source": []
301 | }
302 | ],
303 | "metadata": {
304 | "anaconda-cloud": {},
305 | "kernelspec": {
306 | "display_name": "Python 3",
307 | "language": "python",
308 | "name": "python3"
309 | },
310 | "language_info": {
311 | "codemirror_mode": {
312 | "name": "ipython",
313 | "version": 3
314 | },
315 | "file_extension": ".py",
316 | "mimetype": "text/x-python",
317 | "name": "python",
318 | "nbconvert_exporter": "python",
319 | "pygments_lexer": "ipython3",
320 | "version": "3.5.4"
321 | }
322 | },
323 | "nbformat": 4,
324 | "nbformat_minor": 1
325 | }
326 |
--------------------------------------------------------------------------------
/R/Module01/01-02-Linear Equations.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Linear Equations\n",
8 | "The equations in the previous lab included one variable, for which you solved the equation to find its value. Now let's look at equations with multiple variables. For reasons that will become apparent, equations with two variables are known as linear equations.\n",
9 | "\n",
10 | "## Solving a Linear Equation\n",
11 | "Consider the following equation:\n",
12 | "\n",
13 | "\\begin{equation}2y + 3 = 3x - 1 \\end{equation}\n",
14 | "\n",
15 | "This equation includes two different variables, **x** and **y**. These variables depend on one another; the value of x is determined in part by the value of y and vice-versa; so we can't solve the equation and find absolute values for both x and y. However, we *can* solve the equation for one of the variables and obtain a result that describes a relative relationship between the variables.\n",
16 | "\n",
17 | "For example, let's solve this equation for y. First, we'll get rid of the constant on the right by adding 1 to both sides:\n",
18 | "\n",
19 | "\\begin{equation}2y + 4 = 3x \\end{equation}\n",
20 | "\n",
21 | "Then we'll use the same technique to move the constant on the left to the right to isolate the y term by subtracting 4 from both sides:\n",
22 | "\n",
23 | "\\begin{equation}2y = 3x - 4 \\end{equation}\n",
24 | "\n",
25 | "Now we can deal with the coefficient for y by dividing both sides by 2:\n",
26 | "\n",
27 | "\\begin{equation}y = \\frac{3x - 4}{2} \\end{equation}\n",
28 | "\n",
29 | "Our equation is now solved. We've isolated **y** and defined it as 3x-4/2\n",
30 | "\n",
31 | "While we can't express **y** as a particular value, we can calculate it for any value of **x**. For example, if **x** has a value of 6, then **y** can be calculated as:\n",
32 | "\n",
33 | "\\begin{equation}y = \\frac{3\\cdot6 - 4}{2} \\end{equation}\n",
34 | "\n",
35 | "This gives the result 14/2 which can be simplified to 7.\n",
36 | "\n",
37 | "You can view the values of **y** for a range of **x** values by applying the equation to them using the following R code:"
38 | ]
39 | },
40 | {
41 | "cell_type": "code",
42 | "execution_count": null,
43 | "metadata": {},
44 | "outputs": [],
45 | "source": [
46 | "# Create a dataframe with an x column containing values from -10 to 10\n",
47 | "df = data.frame(x = seq(-10, 10))\n",
48 | "\n",
49 | "# Add a y column by applying the solved equation to x\n",
50 | "df$y = (3*df$x - 4) / 2\n",
51 | "\n",
52 | "#Display the dataframe\n",
53 | "df"
54 | ]
55 | },
56 | {
57 | "cell_type": "markdown",
58 | "metadata": {},
59 | "source": [
60 | "We can also plot these values to visualize the relationship between x and y as a line. For this reason, equations that describe a relative relationship between two variables are known as *linear equations*:"
61 | ]
62 | },
63 | {
64 | "cell_type": "code",
65 | "execution_count": null,
66 | "metadata": {},
67 | "outputs": [],
68 | "source": [
69 | "library(ggplot2)\n",
70 | "library(repr)\n",
71 | "options(repr.plot.width=4, repr.plot.height=4)\n",
72 | "ggplot(df, aes(x,y)) + geom_point() + geom_line(color = 'blue')"
73 | ]
74 | },
75 | {
76 | "cell_type": "markdown",
77 | "metadata": {},
78 | "source": [
79 | "In a linear equation, a valid solution is described by an ordered pair of x and y values. For example, valid solutions to the linear equation above include:\n",
80 | "- (-10, -17)\n",
81 | "- (0, -2)\n",
82 | "- (9, 11.5)\n",
83 | "\n",
84 | "The cool thing about linear equations is that we can plot the points for some specific ordered pair solutions to create the line, and then interpolate the x value for any y value (or vice-versa) along the line."
85 | ]
86 | },
87 | {
88 | "cell_type": "markdown",
89 | "metadata": {},
90 | "source": [
91 | "## Intercepts\n",
92 | "When we use a linear equation to plot a line, we can easily see where the line intersects the X and Y axes of the plot. These points are known as *intercepts*. The *x-intercept* is where the line intersects the X (horizontal) axis, and the *y-intercept* is where the line intersects the Y (horizontal) axis.\n",
93 | "\n",
94 | "Let's take a look at the line from our linear equation with the X and Y axis shown through the origin (0,0)."
95 | ]
96 | },
97 | {
98 | "cell_type": "code",
99 | "execution_count": null,
100 | "metadata": {},
101 | "outputs": [],
102 | "source": [
103 | "ggplot(df, aes(x,y)) + geom_point() + geom_line(color = 'blue') +\n",
104 | " geom_hline(yintercept=0) + geom_vline(xintercept=0)"
105 | ]
106 | },
107 | {
108 | "cell_type": "markdown",
109 | "metadata": {},
110 | "source": [
111 | "The x-intercept is the point where the line crosses the X axis, and at this point, the **y** value is always 0. Similarly, the y-intercept is where the line crosses the Y axis, at which point the **x** value is 0. So to find the intercepts, we need to solve the equation for **x** when **y** is 0.\n",
112 | "\n",
113 | "For the x-intercept, our equation looks like this:\n",
114 | "\n",
115 | "\\begin{equation}0 = \\frac{3x - 4}{2} \\end{equation}\n",
116 | "\n",
117 | "Which can be reversed to make it look more familar with the x expression on the left:\n",
118 | "\n",
119 | "\\begin{equation}\\frac{3x - 4}{2} = 0 \\end{equation}\n",
120 | "\n",
121 | "We can multiply both sides by 2 to get rid of the fraction:\n",
122 | "\n",
123 | "\\begin{equation}3x - 4 = 0 \\end{equation}\n",
124 | "\n",
125 | "Then we can add 4 to both sides to get rid of the constant on the left:\n",
126 | "\n",
127 | "\\begin{equation}3x = 4 \\end{equation}\n",
128 | "\n",
129 | "And finally we can divide both sides by 3 to get the value for x:\n",
130 | "\n",
131 | "\\begin{equation}x = \\frac{4}{3} \\end{equation}\n",
132 | "\n",
133 | "Which simplifies to:\n",
134 | "\n",
135 | "\\begin{equation}x = 1\\frac{1}{3} \\end{equation}\n",
136 | "\n",
137 | "So the x-intercept is 11/3 (approximately 1.333).\n",
138 | "\n",
139 | "To get the y-intercept, we solve the equation for y when x is 0:\n",
140 | "\n",
141 | "\\begin{equation}y = \\frac{3\\cdot0 - 4}{2} \\end{equation}\n",
142 | "\n",
143 | "Since 3 x 0 is 0, this can be simplified to:\n",
144 | "\n",
145 | "\\begin{equation}y = \\frac{-4}{2} \\end{equation}\n",
146 | "\n",
147 | "-4 divided by 2 is -2, so:\n",
148 | "\n",
149 | "\\begin{equation}y = -2 \\end{equation}\n",
150 | "\n",
151 | "This gives us our y-intercept, so we can plot both intercepts on the graph:"
152 | ]
153 | },
154 | {
155 | "cell_type": "code",
156 | "execution_count": null,
157 | "metadata": {},
158 | "outputs": [],
159 | "source": [
160 | "ggplot(df, aes(x,y)) + geom_line(color = 'blue') +\n",
161 | " geom_hline(yintercept=0) + geom_vline(xintercept=0) +\n",
162 | " annotate(\"text\", x = 3, y = -2, label = \"y-intercept\")+\n",
163 | " annotate(\"text\", x = 5, y = 1, label = \"x-intercept\")"
164 | ]
165 | },
166 | {
167 | "cell_type": "markdown",
168 | "metadata": {},
169 | "source": [
170 | "The ability to calculate the intercepts for a linear equation is useful, because you can calculate only these two points and then draw a straight line through them to create the entire line for the equation."
171 | ]
172 | },
173 | {
174 | "cell_type": "markdown",
175 | "metadata": {},
176 | "source": [
177 | "## Slope\n",
178 | "It's clear from the graph that the line from our linear equation describes a slope in which values increase as we travel up and to the right along the line. It can be useful to quantify the slope in terms of how much **x** increases (or decreases) for a given change in **y**. In the notation for this, we use the greek letter Δ (*delta*) to represent change:\n",
179 | "\n",
180 | "\\begin{equation}slope = \\frac{\\Delta{y}}{\\Delta{x}} \\end{equation}\n",
181 | "\n",
182 | "Sometimes slope is represented by the variable ***m***, and the equation is written as:\n",
183 | "\n",
184 | "\\begin{equation}m = \\frac{y_{2} - y_{1}}{x_{2} - x_{1}} \\end{equation}\n",
185 | "\n",
186 | "Although this form of the equation is a little more verbose, it gives us a clue as to how we calculate slope. What we need is any two ordered pairs of x,y values for the line - for example, we know that our line passes through the following two points:\n",
187 | "- (0,-2)\n",
188 | "- (6,7)\n",
189 | "\n",
190 | "We can take the x and y values from the first pair, and label them x1 and y1; and then take the x and y values from the second point and label them x2 and y2. Then we can plug those into our slope equation:\n",
191 | "\n",
192 | "\\begin{equation}m = \\frac{7 - -2}{6 - 0} \\end{equation}\n",
193 | "\n",
194 | "This is the same as:\n",
195 | "\n",
196 | "\\begin{equation}m = \\frac{7 + 2}{6 - 0} \\end{equation}\n",
197 | "\n",
198 | "That gives us the result 9/6 which is 11/2 or 1.5 .\n",
199 | "\n",
200 | "So what does that actually mean? Well, it tells us that for every change of **1** in x, **y** changes by 11/2 or 1.5. So if we start from any point on the line and move one unit to the right (along the X axis), we'll need to move 1.5 units up (along the Y axis) to get back to the line.\n",
201 | "\n",
202 | "You can plot the slope onto the original line with the following R code to verify it fits:"
203 | ]
204 | },
205 | {
206 | "cell_type": "code",
207 | "execution_count": null,
208 | "metadata": {},
209 | "outputs": [],
210 | "source": [
211 | "line = data.frame(x = c(0,1.5), y = c(-2,0))\n",
212 | "ggplot() + geom_line(data = df, aes(x,y),color = 'blue') +\n",
213 | " geom_hline(yintercept=0) + geom_vline(xintercept=0) +\n",
214 | " geom_line(data = line, aes(x,y), color = 'red', size = 3)"
215 | ]
216 | },
217 | {
218 | "cell_type": "markdown",
219 | "metadata": {},
220 | "source": [
221 | "### Slope-Intercept Form\n",
222 | "One of the great things about algebraic expressions is that you can write the same equation in multiple ways, or *forms*. The *slope-intercept form* is a specific way of writing a 2-variable linear equation so that the equation definition includes the slope and y-intercept. The generalized slope-intercept form looks like this:\n",
223 | "\n",
224 | "\\begin{equation}y = mx + b \\end{equation}\n",
225 | "\n",
226 | "In this notation, ***m*** is the slope and ***b*** is the y-intercept.\n",
227 | "\n",
228 | "For example, let's look at the solved linear equation we've been working with so far in this section:\n",
229 | "\n",
230 | "\\begin{equation}y = \\frac{3x - 4}{2} \\end{equation}\n",
231 | "\n",
232 | "Now that we know the slope and y-intercept for the line that this equation defines, we can rewrite the equation as:\n",
233 | "\n",
234 | "\\begin{equation}y = 1\\frac{1}{2}x + -2 \\end{equation}\n",
235 | "\n",
236 | "You can see intuitively that this is true. In our original form of the equation, to find y we multiply x by three, subtract 4, and divide by two - in other words, x is half of 3x - 4; which is 1.5x - 2. So these equations are equivalent, but the slope-intercept form has the advantages of being simpler, and including two key pieces of information we need to plot the line represented by the equation. We know the y-intercept that the line passes through (0, -2), and we know the slope of the line (for every x, we add 1.5 to y.\n",
237 | "\n",
238 | "Let's recreate our set of test x and y values using the slope-intercept form of the equation, and plot them to prove that this describes the same line:"
239 | ]
240 | },
241 | {
242 | "cell_type": "code",
243 | "execution_count": null,
244 | "metadata": {},
245 | "outputs": [],
246 | "source": [
247 | "## Make a data frame with the x values\n",
248 | "df = data.frame(x = seq(-10,10))\n",
249 | "\n",
250 | "## Add the y values using the formula y = mx + b\n",
251 | "m = 1.5\n",
252 | "b = -2\n",
253 | "df$y = m * df$x + b\n",
254 | "\n",
255 | "## Plot the result\n",
256 | "ggplot() + geom_line(data = df, aes(x,y),color = 'blue') +\n",
257 | " geom_hline(yintercept=0) + geom_vline(xintercept=0) +\n",
258 | " geom_line(data = line, aes(x,y), color = 'red', size = 3) +\n",
259 | " annotate(\"text\", x = 3, y = -2, label = \"y-intercept\")"
260 | ]
261 | }
262 | ],
263 | "metadata": {
264 | "kernelspec": {
265 | "display_name": "R",
266 | "language": "R",
267 | "name": "ir"
268 | },
269 | "language_info": {
270 | "codemirror_mode": "r",
271 | "file_extension": ".r",
272 | "mimetype": "text/x-r-source",
273 | "name": "R",
274 | "pygments_lexer": "r",
275 | "version": "3.5.0"
276 | }
277 | },
278 | "nbformat": 4,
279 | "nbformat_minor": 2
280 | }
281 |
--------------------------------------------------------------------------------
/R/Module01/01-05-Polynomials.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Polynomials\n",
8 | "Some of the equations we've looked at so far include expressions that are actually *polynomials*; but what *is* a polynomial, and why should you care?\n",
9 | "\n",
10 | "A polynomial is an algebraic expression containing one or more *terms* that each meet some specific criteria. Specifically:\n",
11 | "- Each term can contain:\n",
12 | " - Numeric values that are coefficients or constants (for example 2, -5, 1/7)\n",
13 | " - Variables (for example, x, y)\n",
14 | " - Non-negative integer exponents (for example 2, 64)\n",
15 | "- The terms can be combined using arithmetic operations - but **not** division by a variable.\n",
16 | "\n",
17 | "For example, the following expression is a polynomial:\n",
18 | "\n",
19 | "\\begin{equation}12x^{3} + 2x - 16 \\end{equation}\n",
20 | "\n",
21 | "When identifying the terms in a polynomial, it's important to correctly interpret the arithmetic addition and subtraction operators as the sign for the term that follows. For example, the polynomial above contains the following three terms:\n",
22 | "- 12x3\n",
23 | "- 2x\n",
24 | "- -16\n",
25 | "\n",
26 | "The terms themselves include:\n",
27 | "- Two coefficients(12 and 2) and a constant (-16)\n",
28 | "- A variable (x)\n",
29 | "- An exponent (3)\n",
30 | "\n",
31 | "A polynomial that contains three terms is also known as a *trinomial*. Similarly, a polynomial with two terms is known as a *binomial* and a polynomial with only one term is known as a *monomial*.\n",
32 | "\n",
33 | "So why do we care? Well, polynomials have some useful properties that make them easy to work with. for example, if you multiply, add, or subtract a polynomial, the result is always another polynomial.\n",
34 | "\n",
35 | "## Standard Form for Polynomials\n",
36 | "Technically, you can write the terms of a polynomial in any order; but the *standard form* for a polynomial is to start with the highest *degree* first and constants last. The degree of a term is the highest order (exponent) in the term, and the highest order in a polynomial determines the degree of the polynomial itself.\n",
37 | "\n",
38 | "For example, consider the following expression:\n",
39 | "\\begin{equation}3x + 4xy^{2} - 3 + x^{3} \\end{equation}\n",
40 | "\n",
41 | "To express this as a polynomial in the standard form, we need to re-order the terms like this:\n",
42 | "\n",
43 | "\\begin{equation}x^{3} + 4xy^{2} + 3x - 3 \\end{equation}\n",
44 | "\n",
45 | "## Simplifying Polynomials\n",
46 | "We saw previously how you can simplify an equation by combining *like terms*. You can simplify polynomials in the same way.\n",
47 | "\n",
48 | "For example, look at the following polynomial:\n",
49 | "\n",
50 | "\\begin{equation}x^{3} + 2x^{3} - 3x - x + 8 - 3 \\end{equation}\n",
51 | "\n",
52 | "In this case, we can combine x3 and 2x3 by adding them to make 3x3. Then we can add -3x and -x (which is really just a shorthand way to say -1x) to get -4x, and then add 8 and -3 to get 5. Our simplified polynomial then looks like this:\n",
53 | "\n",
54 | "\\begin{equation}3x^{3} - 4x + 5 \\end{equation}\n",
55 | "\n",
56 | "We can use R to compare the original and simplified polynomials to check them - using an arbitrary random value for ***x***:"
57 | ]
58 | },
59 | {
60 | "cell_type": "code",
61 | "execution_count": null,
62 | "metadata": {},
63 | "outputs": [],
64 | "source": [
65 | "x = sample.int(100, 1)\n",
66 | "\n",
67 | "(x^3 + 2*x^3 - 3*x - x + 8 - 3) == (3*x^3 - 4*x + 5)"
68 | ]
69 | },
70 | {
71 | "cell_type": "markdown",
72 | "metadata": {},
73 | "source": [
74 | "## Adding Polynomials\n",
75 | "When you add two polynomials, the result is a polynomial. Here's an example:\n",
76 | "\n",
77 | "\\begin{equation}(3x^{3} - 4x + 5) + (2x^{3} + 3x^{2} - 2x + 2) \\end{equation}\n",
78 | "\n",
79 | "because this is an addition operation, you can simply add all of the like terms from both polynomials. To make this clear, let's first put the like terms together:\n",
80 | "\n",
81 | "\\begin{equation}3x^{3} + 2x^{3} + 3x^{2} - 4x -2x + 5 + 2 \\end{equation}\n",
82 | "\n",
83 | "This simplifies to:\n",
84 | "\n",
85 | "\\begin{equation}5x^{3} + 3x^{2} - 6x + 7 \\end{equation}\n",
86 | "\n",
87 | "We can verify this with R:"
88 | ]
89 | },
90 | {
91 | "cell_type": "code",
92 | "execution_count": null,
93 | "metadata": {},
94 | "outputs": [],
95 | "source": [
96 | "x = sample.int(100, 1)\n",
97 | "\n",
98 | "(3*x^3 - 4*x + 5) + (2*x^3 + 3*x^2 - 2*x + 2) == 5*x^3 + 3*x^2 - 6*x + 7"
99 | ]
100 | },
101 | {
102 | "cell_type": "markdown",
103 | "metadata": {},
104 | "source": [
105 | "## Subtracting Polynomials\n",
106 | "Subtracting polynomials is similar to adding them but you need to take into account that one of the polynomials is a negative.\n",
107 | "\n",
108 | "Consider this expression:\n",
109 | "\n",
110 | "\\begin{equation}(2x^{2} - 4x + 5) - (x^{2} - 2x + 2) \\end{equation}\n",
111 | "\n",
112 | "The key to performing this calculation is to realize that the subtraction of the second polynomial is really an expression that adds -1(x2 - 2x + 2); so you can use the distributive property to multiply each of the terms in the polynomial by -1 (which in effect simply reverses the sign for each term). So our expression becomes:\n",
113 | "\n",
114 | "\\begin{equation}(2x^{2} - 4x + 5) + (-x^{2} + 2x - 2) \\end{equation}\n",
115 | "\n",
116 | "Which we can solve as an addition problem. First place the like terms together:\n",
117 | "\n",
118 | "\\begin{equation}2x^{2} + -x^{2} + -4x + 2x + 5 + -2 \\end{equation}\n",
119 | "\n",
120 | "Which simplifies to:\n",
121 | "\n",
122 | "\\begin{equation}x^{2} - 2x + 3 \\end{equation}\n",
123 | "\n",
124 | "Let's check that with R:"
125 | ]
126 | },
127 | {
128 | "cell_type": "code",
129 | "execution_count": null,
130 | "metadata": {},
131 | "outputs": [],
132 | "source": [
133 | "x = sample.int(100, 1)\n",
134 | "\n",
135 | "(2*x^2 - 4*x + 5) - (x^2 - 2*x + 2) == x^2 - 2*x + 3"
136 | ]
137 | },
138 | {
139 | "cell_type": "markdown",
140 | "metadata": {},
141 | "source": [
142 | "## Multiplying Polynomials\n",
143 | "To multiply two polynomials, you need to perform the following two steps:\n",
144 | "1. Multiply each term in the first polynomial by each term in the second polynomial.\n",
145 | "2. Add the results of the multiplication operations, combining like terms where possible.\n",
146 | "\n",
147 | "For example, consider this expression:\n",
148 | "\n",
149 | "\\begin{equation}(x^{4} + 2)(2x^{2} + 3x - 3) \\end{equation}\n",
150 | "\n",
151 | "Let's do the first step and multiply each term in the first polynomial by each term in the second polynomial. The first term in the first polynomial is x4, and the first term in the second polynomial is 2x2, so multiplying these gives us 2x6. Then we can multiply the first term in the first polynomial (x4) by the second term in the second polynomial (3x), which gives us 3x5, and so on until we've multiplied all of the terms in the first polynomial by all of the terms in the second polynomial, which results in this:\n",
152 | "\n",
153 | "\\begin{equation}2x^{6} + 3x^{5} - 3x^{4} + 4x^{2} + 6x - 6 \\end{equation}\n",
154 | "\n",
155 | "We can verify a match between this result and the original expression this with the following R code:"
156 | ]
157 | },
158 | {
159 | "cell_type": "code",
160 | "execution_count": null,
161 | "metadata": {},
162 | "outputs": [],
163 | "source": [
164 | "x = sample.int(100, 1)\n",
165 | "\n",
166 | "(x^4 + 2)*(2*x^2 + 3*x - 3) == 2*x^6 + 3*x^5 - 3*x^4 + 4*x^2 + 6*x - 6"
167 | ]
168 | },
169 | {
170 | "cell_type": "markdown",
171 | "metadata": {},
172 | "source": [
173 | "## Dividing Polynomials\n",
174 | "When you need to divide one polynomial by another, there are two approaches you can take depending on the number of terms in the divisor (the expression you're dividing by).\n",
175 | "\n",
176 | "### Dividing Polynomials Using Simplification\n",
177 | "In the simplest case, division of a polynomial by a monomial, the operation is really just simplification of a fraction.\n",
178 | "\n",
179 | "For example, consider the following expression:\n",
180 | "\n",
181 | "\\begin{equation}(4x + 6x^{2}) \\div 2x \\end{equation}\n",
182 | "\n",
183 | "This can also be written as:\n",
184 | "\n",
185 | "\\begin{equation}\\frac{4x + 6x^{2}}{2x} \\end{equation}\n",
186 | "\n",
187 | "One approach to simplifying this fraction is to split it it into a separate fraction for each term in the dividend (the expression we're dividing), like this:\n",
188 | "\n",
189 | "\\begin{equation}\\frac{4x}{2x} + \\frac{6x^{2}}{2x}\\end{equation}\n",
190 | "\n",
191 | "Then we can simplify each fraction and add the results. For the first fraction, 2x goes into 4x twice, so the fraction simplifies to 2; and for the second, 6x2 is 2x multiplied by 3x. So our answer is 2 + 3x:\n",
192 | "\n",
193 | "\\begin{equation}2 + 3x\\end{equation}\n",
194 | "\n",
195 | "Let's use R to compare the original fraction with the simplified result for an arbitrary value of ***x***:"
196 | ]
197 | },
198 | {
199 | "cell_type": "code",
200 | "execution_count": null,
201 | "metadata": {},
202 | "outputs": [],
203 | "source": [
204 | "x = sample.int(100, 1)\n",
205 | "\n",
206 | "(4*x + 6*x^2) / (2*x) == 2 + 3*x"
207 | ]
208 | },
209 | {
210 | "cell_type": "markdown",
211 | "metadata": {},
212 | "source": [
213 | "### Dividing Polynomials Using Long Division\n",
214 | "Things get a little more complicated for divisors with more than one term.\n",
215 | "\n",
216 | "Suppose we have the following expression:\n",
217 | "\\begin{equation}(x^{2} + 2x - 3) \\div (x - 2) \\end{equation}\n",
218 | "\n",
219 | "Another way of writing this is to use the long-division format, like this:\n",
220 | "\\begin{equation} x - 2 |\\overline{x^{2} + 2x - 3} \\end{equation}\n",
221 | "\n",
222 | "We begin long-division by dividing the highest order divisor into the highest order dividend - so in this case we divide x into x2. X goes into x2 x times, so we put an x on top and then multiply it through the divisor:\n",
223 | "\\begin{equation} \\;\\;\\;\\;x \\end{equation}\n",
224 | "\\begin{equation}x - 2 |\\overline{x^{2} + 2x - 3} \\end{equation}\n",
225 | "\\begin{equation} \\;x^{2} -2x \\end{equation}\n",
226 | "\n",
227 | "Now we'll subtract the remaining dividend, and then carry down the -3 that we haven't used to see what's left:\n",
228 | "\\begin{equation} \\;\\;\\;\\;x \\end{equation}\n",
229 | "\\begin{equation}x - 2 |\\overline{x^{2} + 2x - 3} \\end{equation}\n",
230 | "\\begin{equation}- (x^{2} -2x) \\end{equation}\n",
231 | "\\begin{equation}\\;\\;\\;\\;\\;\\overline{\\;\\;\\;\\;\\;\\;\\;\\;\\;\\;4x -3} \\end{equation}\n",
232 | "\n",
233 | "OK, now we'll divide our highest order divisor into the highest order of the remaining dividend. In this case, x goes into 4x four times, so we'll add a 4 to the top line, multiply it through the divisor, and subtract the remaining dividend:\n",
234 | "\\begin{equation} \\;\\;\\;\\;\\;\\;\\;\\;x + 4 \\end{equation}\n",
235 | "\\begin{equation}x - 2 |\\overline{x^{2} + 2x - 3} \\end{equation}\n",
236 | "\\begin{equation}- (x^{2} -2x) \\end{equation}\n",
237 | "\\begin{equation}\\;\\;\\;\\;\\;\\overline{\\;\\;\\;\\;\\;\\;\\;\\;\\;\\;4x -3} \\end{equation}\n",
238 | "\\begin{equation}- (\\;\\;\\;\\;\\;\\;\\;\\;\\;\\;\\;\\;4x -8) \\end{equation}\n",
239 | "\\begin{equation}\\;\\;\\;\\;\\;\\overline{\\;\\;\\;\\;\\;\\;\\;\\;\\;\\;\\;\\;\\;\\;\\;\\;5} \\end{equation}\n",
240 | "\n",
241 | "We're now left with just 5, which we can't divide further by x - 2; so that's our remainder, which we'll add as a fraction.\n",
242 | "\n",
243 | "The solution to our division problem is:\n",
244 | "\n",
245 | "\\begin{equation}x + 4 + \\frac{5}{x-2} \\end{equation}\n",
246 | "\n",
247 | "Once again, we can use R to check our answer:"
248 | ]
249 | },
250 | {
251 | "cell_type": "code",
252 | "execution_count": null,
253 | "metadata": {},
254 | "outputs": [],
255 | "source": [
256 | "x = sample.int(100, 1)\n",
257 | "\n",
258 | "(x^2 + 2*x -3)/(x-2) == x + 4 + (5/(x-2))\n",
259 | " "
260 | ]
261 | }
262 | ],
263 | "metadata": {
264 | "kernelspec": {
265 | "display_name": "R",
266 | "language": "R",
267 | "name": "ir"
268 | },
269 | "language_info": {
270 | "codemirror_mode": "r",
271 | "file_extension": ".r",
272 | "mimetype": "text/x-r-source",
273 | "name": "R",
274 | "pygments_lexer": "r",
275 | "version": "3.5.0"
276 | }
277 | },
278 | "nbformat": 4,
279 | "nbformat_minor": 2
280 | }
281 |
--------------------------------------------------------------------------------
/Python/Module01/01-05-Polynomials.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Polynomials\n",
8 | "Some of the equations we've looked at so far include expressions that are actually *polynomials*; but what *is* a polynomial, and why should you care?\n",
9 | "\n",
10 | "A polynomial is an algebraic expression containing one or more *terms* that each meet some specific criteria. Specifically:\n",
11 | "- Each term can contain:\n",
12 | " - Numeric values that are coefficients or constants (for example 2, -5, 1/7)\n",
13 | " - Variables (for example, x, y)\n",
14 | " - Non-negative integer exponents (for example 2, 64)\n",
15 | "- The terms can be combined using arithmetic operations - but **not** division by a variable.\n",
16 | "\n",
17 | "For example, the following expression is a polynomial:\n",
18 | "\n",
19 | "\\begin{equation}12x^{3} + 2x - 16 \\end{equation}\n",
20 | "\n",
21 | "When identifying the terms in a polynomial, it's important to correctly interpret the arithmetic addition and subtraction operators as the sign for the term that follows. For example, the polynomial above contains the following three terms:\n",
22 | "- 12x3\n",
23 | "- 2x\n",
24 | "- -16\n",
25 | "\n",
26 | "The terms themselves include:\n",
27 | "- Two coefficients(12 and 2) and a constant (-16)\n",
28 | "- A variable (x)\n",
29 | "- An exponent (3)\n",
30 | "\n",
31 | "A polynomial that contains three terms is also known as a *trinomial*. Similarly, a polynomial with two terms is known as a *binomial* and a polynomial with only one term is known as a *monomial*.\n",
32 | "\n",
33 | "So why do we care? Well, polynomials have some useful properties that make them easy to work with. for example, if you multiply, add, or subtract a polynomial, the result is always another polynomial.\n",
34 | "\n",
35 | "## Standard Form for Polynomials\n",
36 | "Techbnically, you can write the terms of a polynomial in any order; but the *standard form* for a polynomial is to start with the highest *degree* first and constants last. The degree of a term is the highest order (exponent) in the term, and the highest order in a polynomial determines the degree of the polynomial itself.\n",
37 | "\n",
38 | "For example, consider the following expression:\n",
39 | "\\begin{equation}3x + 4xy^{2} - 3 + x^{3} \\end{equation}\n",
40 | "\n",
41 | "To express this as a polynomial in the standard form, we need to re-order the terms like this:\n",
42 | "\n",
43 | "\\begin{equation}x^{3} + 4xy^{2} + 3x - 3 \\end{equation}\n",
44 | "\n",
45 | "## Simplifying Polynomials\n",
46 | "We saw previously how you can simplify an equation by combining *like terms*. You can simplify polynomials in the same way.\n",
47 | "\n",
48 | "For example, look at the following polynomial:\n",
49 | "\n",
50 | "\\begin{equation}x^{3} + 2x^{3} - 3x - x + 8 - 3 \\end{equation}\n",
51 | "\n",
52 | "In this case, we can combine x3 and 2x3 by adding them to make 3x3. Then we can add -3x and -x (which is really just a shorthand way to say -1x) to get -4x, and then add 8 and -3 to get 5. Our simplified polynomial then looks like this:\n",
53 | "\n",
54 | "\\begin{equation}3x^{3} - 4x + 5 \\end{equation}\n",
55 | "\n",
56 | "We can use Python to compare the original and simplified polynomials to check them - using an arbitrary random value for ***x***:"
57 | ]
58 | },
59 | {
60 | "cell_type": "code",
61 | "execution_count": null,
62 | "metadata": {
63 | "collapsed": true
64 | },
65 | "outputs": [],
66 | "source": [
67 | "from random import randint\n",
68 | "x = randint(1,100)\n",
69 | "\n",
70 | "(x**3 + 2*x**3 - 3*x - x + 8 - 3) == (3*x**3 - 4*x + 5)"
71 | ]
72 | },
73 | {
74 | "cell_type": "markdown",
75 | "metadata": {},
76 | "source": [
77 | "## Adding Polynomials\n",
78 | "When you add two polynomials, the result is a polynomial. Here's an example:\n",
79 | "\n",
80 | "\\begin{equation}(3x^{3} - 4x + 5) + (2x^{3} + 3x^{2} - 2x + 2) \\end{equation}\n",
81 | "\n",
82 | "because this is an addition operation, you can simply add all of the like terms from both polynomials. To make this clear, let's first put the like terms together:\n",
83 | "\n",
84 | "\\begin{equation}3x^{3} + 2x^{3} + 3x^{2} - 4x -2x + 5 + 2 \\end{equation}\n",
85 | "\n",
86 | "This simplifies to:\n",
87 | "\n",
88 | "\\begin{equation}5x^{3} + 3x^{2} - 6x + 7 \\end{equation}\n",
89 | "\n",
90 | "We can verify this with Python:"
91 | ]
92 | },
93 | {
94 | "cell_type": "code",
95 | "execution_count": null,
96 | "metadata": {
97 | "collapsed": true
98 | },
99 | "outputs": [],
100 | "source": [
101 | "from random import randint\n",
102 | "x = randint(1,100)\n",
103 | "\n",
104 | "\n",
105 | "(3*x**3 - 4*x + 5) + (2*x**3 + 3*x**2 - 2*x + 2) == 5*x**3 + 3*x**2 - 6*x + 7"
106 | ]
107 | },
108 | {
109 | "cell_type": "markdown",
110 | "metadata": {},
111 | "source": [
112 | "## Subtracting Polynomials\n",
113 | "Subtracting polynomials is similar to adding them but you need to take into account that one of the polynomials is a negative.\n",
114 | "\n",
115 | "Consider this expression:\n",
116 | "\n",
117 | "\\begin{equation}(2x^{2} - 4x + 5) - (x^{2} - 2x + 2) \\end{equation}\n",
118 | "\n",
119 | "The key to performing this calculation is to realize that the subtraction of the second polynomial is really an expression that adds -1(x2 - 2x + 2); so you can use the distributive property to multiply each of the terms in the polynomial by -1 (which in effect simply reverses the sign for each term). So our expression becomes:\n",
120 | "\n",
121 | "\\begin{equation}(2x^{2} - 4x + 5) + (-x^{2} + 2x - 2) \\end{equation}\n",
122 | "\n",
123 | "Which we can solve as an addition problem. First place the like terms together:\n",
124 | "\n",
125 | "\\begin{equation}2x^{2} + -x^{2} + -4x + 2x + 5 + -2 \\end{equation}\n",
126 | "\n",
127 | "Which simplifies to:\n",
128 | "\n",
129 | "\\begin{equation}x^{2} - 2x + 3 \\end{equation}\n",
130 | "\n",
131 | "Let's check that with Python:"
132 | ]
133 | },
134 | {
135 | "cell_type": "code",
136 | "execution_count": null,
137 | "metadata": {
138 | "collapsed": true
139 | },
140 | "outputs": [],
141 | "source": [
142 | "from random import randint\n",
143 | "x = randint(1,100)\n",
144 | "\n",
145 | "(2*x**2 - 4*x + 5) - (x**2 - 2*x + 2) == x**2 - 2*x + 3"
146 | ]
147 | },
148 | {
149 | "cell_type": "markdown",
150 | "metadata": {},
151 | "source": [
152 | "## Multiplying Polynomials\n",
153 | "To multiply two polynomials, you need to perform the following two steps:\n",
154 | "1. Multiply each term in the first polynomial by each term in the second polynomial.\n",
155 | "2. Add the results of the multiplication operations, combining like terms where possible.\n",
156 | "\n",
157 | "For example, consider this expression:\n",
158 | "\n",
159 | "\\begin{equation}(x^{4} + 2)(2x^{2} + 3x - 3) \\end{equation}\n",
160 | "\n",
161 | "Let's do the first step and multiply each term in the first polynomial by each term in the second polynomial. The first term in the first polynomial is x4, and the first term in the second polynomial is 2x2, so multiplying these gives us 2x6. Then we can multiply the first term in the first polynomial (x4) by the second term in the second polynomial (3x), which gives us 3x5, and so on until we've multipled all of the terms in the first polynomial by all of the terms in the second polynomial, which results in this:\n",
162 | "\n",
163 | "\\begin{equation}2x^{6} + 3x^{5} - 3x^{4} + 4x^{2} + 6x - 6 \\end{equation}\n",
164 | "\n",
165 | "We can verify a match between this result and the original expression this with the following Python code:"
166 | ]
167 | },
168 | {
169 | "cell_type": "code",
170 | "execution_count": null,
171 | "metadata": {
172 | "collapsed": true
173 | },
174 | "outputs": [],
175 | "source": [
176 | "from random import randint\n",
177 | "x = randint(1,100)\n",
178 | "\n",
179 | "(x**4 + 2)*(2*x**2 + 3*x - 3) == 2*x**6 + 3*x**5 - 3*x**4 + 4*x**2 + 6*x - 6"
180 | ]
181 | },
182 | {
183 | "cell_type": "markdown",
184 | "metadata": {},
185 | "source": [
186 | "## Dividing Polynomials\n",
187 | "When you need to divide one polynomial by another, there are two approaches you can take depending on the number of terms in the divisor (the expression you're dividing by).\n",
188 | "\n",
189 | "### Dividing Polynomials Using Simplification\n",
190 | "In the simplest case, division of a polynomial by a monomial, the operation is really just simplification of a fraction.\n",
191 | "\n",
192 | "For example, consider the following expression:\n",
193 | "\n",
194 | "\\begin{equation}(4x + 6x^{2}) \\div 2x \\end{equation}\n",
195 | "\n",
196 | "This can also be written as:\n",
197 | "\n",
198 | "\\begin{equation}\\frac{4x + 6x^{2}}{2x} \\end{equation}\n",
199 | "\n",
200 | "One approach to simplifying this fraction is to split it it into a separate fraction for each term in the dividend (the expression we're dividing), like this:\n",
201 | "\n",
202 | "\\begin{equation}\\frac{4x}{2x} + \\frac{6x^{2}}{2x}\\end{equation}\n",
203 | "\n",
204 | "Then we can simplify each fraction and add the results. For the first fraction, 2x goes into 4x twice, so the fraction simplifies to 2; and for the second, 6x2 is 2x mutliplied by 3x. So our answer is 2 + 3x:\n",
205 | "\n",
206 | "\\begin{equation}2 + 3x\\end{equation}\n",
207 | "\n",
208 | "Let's use Python to compare the original fraction with the simplified result for an arbitrary value of ***x***:"
209 | ]
210 | },
211 | {
212 | "cell_type": "code",
213 | "execution_count": null,
214 | "metadata": {
215 | "collapsed": true
216 | },
217 | "outputs": [],
218 | "source": [
219 | "from random import randint\n",
220 | "x = randint(1,100)\n",
221 | "\n",
222 | "(4*x + 6*x**2) / (2*x) == 2 + 3*x"
223 | ]
224 | },
225 | {
226 | "cell_type": "markdown",
227 | "metadata": {},
228 | "source": [
229 | "### Dividing Polynomials Using Long Division\n",
230 | "Things get a little more complicated for divisors with more than one term.\n",
231 | "\n",
232 | "Suppose we have the following expression:\n",
233 | "\\begin{equation}(x^{2} + 2x - 3) \\div (x - 2) \\end{equation}\n",
234 | "\n",
235 | "Another way of writing this is to use the long-division format, like this:\n",
236 | "\\begin{equation} x - 2 |\\overline{x^{2} + 2x - 3} \\end{equation}\n",
237 | "\n",
238 | "We begin long-division by dividing the highest order divisor into the highest order dividend - so in this case we divide x into x2. X goes into x2 x times, so we put an x on top and then multiply it through the divisor:\n",
239 | "\\begin{equation} \\;\\;\\;\\;x \\end{equation}\n",
240 | "\\begin{equation}x - 2 |\\overline{x^{2} + 2x - 3} \\end{equation}\n",
241 | "\\begin{equation} \\;x^{2} -2x \\end{equation}\n",
242 | "\n",
243 | "Now we'll subtract the remaining dividend, and then carry down the -3 that we haven't used to see what's left:\n",
244 | "\\begin{equation} \\;\\;\\;\\;x \\end{equation}\n",
245 | "\\begin{equation}x - 2 |\\overline{x^{2} + 2x - 3} \\end{equation}\n",
246 | "\\begin{equation}- (x^{2} -2x) \\end{equation}\n",
247 | "\\begin{equation}\\;\\;\\;\\;\\;\\overline{\\;\\;\\;\\;\\;\\;\\;\\;\\;\\;4x -3} \\end{equation}\n",
248 | "\n",
249 | "OK, now we'll divide our highest order divisor into the highest order of the remaining dividend. In this case, x goes into 4x four times, so we'll add a 4 to the top line, multiply it through the divisor, and subtract the remaining dividend:\n",
250 | "\\begin{equation} \\;\\;\\;\\;\\;\\;\\;\\;x + 4 \\end{equation}\n",
251 | "\\begin{equation}x - 2 |\\overline{x^{2} + 2x - 3} \\end{equation}\n",
252 | "\\begin{equation}- (x^{2} -2x) \\end{equation}\n",
253 | "\\begin{equation}\\;\\;\\;\\;\\;\\overline{\\;\\;\\;\\;\\;\\;\\;\\;\\;\\;4x -3} \\end{equation}\n",
254 | "\\begin{equation}- (\\;\\;\\;\\;\\;\\;\\;\\;\\;\\;\\;\\;4x -8) \\end{equation}\n",
255 | "\\begin{equation}\\;\\;\\;\\;\\;\\overline{\\;\\;\\;\\;\\;\\;\\;\\;\\;\\;\\;\\;\\;\\;\\;\\;5} \\end{equation}\n",
256 | "\n",
257 | "We're now left with just 5, which we can't divide further by x - 2; so that's our remainder, which we'll add as a fraction.\n",
258 | "\n",
259 | "The solution to our division problem is:\n",
260 | "\n",
261 | "\\begin{equation}x + 4 + \\frac{5}{x-2} \\end{equation}\n",
262 | "\n",
263 | "Once again, we can use Python to check our answer:"
264 | ]
265 | },
266 | {
267 | "cell_type": "code",
268 | "execution_count": null,
269 | "metadata": {
270 | "collapsed": true
271 | },
272 | "outputs": [],
273 | "source": [
274 | "from random import randint\n",
275 | "x = randint(3,100)\n",
276 | "\n",
277 | "(x**2 + 2*x -3)/(x-2) == x + 4 + (5/(x-2))\n",
278 | " "
279 | ]
280 | }
281 | ],
282 | "metadata": {
283 | "kernelspec": {
284 | "display_name": "Python 3.6",
285 | "language": "python",
286 | "name": "python36"
287 | },
288 | "language_info": {
289 | "codemirror_mode": {
290 | "name": "ipython",
291 | "version": 3
292 | },
293 | "file_extension": ".py",
294 | "mimetype": "text/x-python",
295 | "name": "python",
296 | "nbconvert_exporter": "python",
297 | "pygments_lexer": "ipython3",
298 | "version": "3.6.3"
299 | }
300 | },
301 | "nbformat": 4,
302 | "nbformat_minor": 2
303 | }
304 |
--------------------------------------------------------------------------------
/Python/Module01/01-02-Linear Equations.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Linear Equations\n",
8 | "The equations in the previous lab included one variable, for which you solved the equation to find its value. Now let's look at equations with multiple variables. For reasons that will become apparent, equations with two variables are known as linear equations.\n",
9 | "\n",
10 | "## Solving a Linear Equation\n",
11 | "Consider the following equation:\n",
12 | "\n",
13 | "\\begin{equation}2y + 3 = 3x - 1 \\end{equation}\n",
14 | "\n",
15 | "This equation includes two different variables, **x** and **y**. These variables depend on one another; the value of x is determined in part by the value of y and vice-versa; so we can't solve the equation and find absolute values for both x and y. However, we *can* solve the equation for one of the variables and obtain a result that describes a relative relationship between the variables.\n",
16 | "\n",
17 | "For example, let's solve this equation for y. First, we'll get rid of the constant on the right by adding 1 to both sides:\n",
18 | "\n",
19 | "\\begin{equation}2y + 4 = 3x \\end{equation}\n",
20 | "\n",
21 | "Then we'll use the same technique to move the constant on the left to the right to isolate the y term by subtracting 4 from both sides:\n",
22 | "\n",
23 | "\\begin{equation}2y = 3x - 4 \\end{equation}\n",
24 | "\n",
25 | "Now we can deal with the coefficient for y by dividing both sides by 2:\n",
26 | "\n",
27 | "\\begin{equation}y = \\frac{3x - 4}{2} \\end{equation}\n",
28 | "\n",
29 | "Our equation is now solved. We've isolated **y** and defined it as 3x-4/2\n",
30 | "\n",
31 | "While we can't express **y** as a particular value, we can calculate it for any value of **x**. For example, if **x** has a value of 6, then **y** can be calculated as:\n",
32 | "\n",
33 | "\\begin{equation}y = \\frac{3\\cdot6 - 4}{2} \\end{equation}\n",
34 | "\n",
35 | "This gives the result 14/2 which can be simplified to 7.\n",
36 | "\n",
37 | "You can view the values of **y** for a range of **x** values by applying the equation to them using the following Python code:"
38 | ]
39 | },
40 | {
41 | "cell_type": "code",
42 | "execution_count": null,
43 | "metadata": {},
44 | "outputs": [],
45 | "source": [
46 | "import pandas as pd\n",
47 | "\n",
48 | "# Create a dataframe with an x column containing values from -10 to 10\n",
49 | "df = pd.DataFrame ({'x': range(-10, 11)})\n",
50 | "\n",
51 | "# Add a y column by applying the solved equation to x\n",
52 | "df['y'] = (3*df['x'] - 4) / 2\n",
53 | "\n",
54 | "#Display the dataframe\n",
55 | "df"
56 | ]
57 | },
58 | {
59 | "cell_type": "markdown",
60 | "metadata": {},
61 | "source": [
62 | "We can also plot these values to visualize the relationship between x and y as a line. For this reason, equations that describe a relative relationship between two variables are known as *linear equations*:"
63 | ]
64 | },
65 | {
66 | "cell_type": "code",
67 | "execution_count": null,
68 | "metadata": {},
69 | "outputs": [],
70 | "source": [
71 | "%matplotlib inline\n",
72 | "from matplotlib import pyplot as plt\n",
73 | "\n",
74 | "plt.plot(df.x, df.y, color=\"grey\", marker = \"o\")\n",
75 | "plt.xlabel('x')\n",
76 | "plt.ylabel('y')\n",
77 | "plt.grid()\n",
78 | "plt.show()"
79 | ]
80 | },
81 | {
82 | "cell_type": "markdown",
83 | "metadata": {},
84 | "source": [
85 | "In a linear equation, a valid solution is described by an ordered pair of x and y values. For example, valid solutions to the linear equation above include:\n",
86 | "- (-10, -17)\n",
87 | "- (0, -2)\n",
88 | "- (9, 11.5)\n",
89 | "\n",
90 | "The cool thing about linear equations is that we can plot the points for some specific ordered pair solutions to create the line, and then interpolate the x value for any y value (or vice-versa) along the line."
91 | ]
92 | },
93 | {
94 | "cell_type": "markdown",
95 | "metadata": {},
96 | "source": [
97 | "## Intercepts\n",
98 | "When we use a linear equation to plot a line, we can easily see where the line intersects the X and Y axes of the plot. These points are known as *intercepts*. The *x-intercept* is where the line intersects the X (horizontal) axis, and the *y-intercept* is where the line intersects the Y (vertical) axis.\n",
99 | "\n",
100 | "Let's take a look at the line from our linear equation with the X and Y axis shown through the origin (0,0)."
101 | ]
102 | },
103 | {
104 | "cell_type": "code",
105 | "execution_count": null,
106 | "metadata": {},
107 | "outputs": [],
108 | "source": [
109 | "plt.plot(df.x, df.y, color=\"grey\")\n",
110 | "plt.xlabel('x')\n",
111 | "plt.ylabel('y')\n",
112 | "plt.grid()\n",
113 | "\n",
114 | "## add axis lines for 0,0\n",
115 | "plt.axhline()\n",
116 | "plt.axvline()\n",
117 | "plt.show()"
118 | ]
119 | },
120 | {
121 | "cell_type": "markdown",
122 | "metadata": {},
123 | "source": [
124 | "The x-intercept is the point where the line crosses the X axis, and at this point, the **y** value is always 0. Similarly, the y-intercept is where the line crosses the Y axis, at which point the **x** value is 0. So to find the intercepts, we need to solve the equation for **x** when **y** is 0.\n",
125 | "\n",
126 | "For the x-intercept, our equation looks like this:\n",
127 | "\n",
128 | "\\begin{equation}0 = \\frac{3x - 4}{2} \\end{equation}\n",
129 | "\n",
130 | "Which can be reversed to make it look more familar with the x expression on the left:\n",
131 | "\n",
132 | "\\begin{equation}\\frac{3x - 4}{2} = 0 \\end{equation}\n",
133 | "\n",
134 | "We can multiply both sides by 2 to get rid of the fraction:\n",
135 | "\n",
136 | "\\begin{equation}3x - 4 = 0 \\end{equation}\n",
137 | "\n",
138 | "Then we can add 4 to both sides to get rid of the constant on the left:\n",
139 | "\n",
140 | "\\begin{equation}3x = 4 \\end{equation}\n",
141 | "\n",
142 | "And finally we can divide both sides by 3 to get the value for x:\n",
143 | "\n",
144 | "\\begin{equation}x = \\frac{4}{3} \\end{equation}\n",
145 | "\n",
146 | "Which simplifies to:\n",
147 | "\n",
148 | "\\begin{equation}x = 1\\frac{1}{3} \\end{equation}\n",
149 | "\n",
150 | "So the x-intercept is 11/3 (approximately 1.333).\n",
151 | "\n",
152 | "To get the y-intercept, we solve the equation for y when x is 0:\n",
153 | "\n",
154 | "\\begin{equation}y = \\frac{3\\cdot0 - 4}{2} \\end{equation}\n",
155 | "\n",
156 | "Since 3 x 0 is 0, this can be simplified to:\n",
157 | "\n",
158 | "\\begin{equation}y = \\frac{-4}{2} \\end{equation}\n",
159 | "\n",
160 | "-4 divided by 2 is -2, so:\n",
161 | "\n",
162 | "\\begin{equation}y = -2 \\end{equation}\n",
163 | "\n",
164 | "This gives us our y-intercept, so we can plot both intercepts on the graph:"
165 | ]
166 | },
167 | {
168 | "cell_type": "code",
169 | "execution_count": null,
170 | "metadata": {},
171 | "outputs": [],
172 | "source": [
173 | "plt.plot(df.x, df.y, color=\"grey\")\n",
174 | "plt.xlabel('x')\n",
175 | "plt.ylabel('y')\n",
176 | "plt.grid()\n",
177 | "\n",
178 | "## add axis lines for 0,0\n",
179 | "plt.axhline()\n",
180 | "plt.axvline()\n",
181 | "plt.annotate('x-intercept',(1.333, 0))\n",
182 | "plt.annotate('y-intercept',(0,-2))\n",
183 | "plt.show()"
184 | ]
185 | },
186 | {
187 | "cell_type": "markdown",
188 | "metadata": {},
189 | "source": [
190 | "The ability to calculate the intercepts for a linear equation is useful, because you can calculate only these two points and then draw a straight line through them to create the entire line for the equation."
191 | ]
192 | },
193 | {
194 | "cell_type": "markdown",
195 | "metadata": {},
196 | "source": [
197 | "## Slope\n",
198 | "It's clear from the graph that the line from our linear equation describes a slope in which values increase as we travel up and to the right along the line. It can be useful to quantify the slope in terms of how much **x** increases (or decreases) for a given change in **y**. In the notation for this, we use the greek letter Δ (*delta*) to represent change:\n",
199 | "\n",
200 | "\\begin{equation}slope = \\frac{\\Delta{y}}{\\Delta{x}} \\end{equation}\n",
201 | "\n",
202 | "Sometimes slope is represented by the variable ***m***, and the equation is written as:\n",
203 | "\n",
204 | "\\begin{equation}m = \\frac{y_{2} - y_{1}}{x_{2} - x_{1}} \\end{equation}\n",
205 | "\n",
206 | "Although this form of the equation is a little more verbose, it gives us a clue as to how we calculate slope. What we need is any two ordered pairs of x,y values for the line - for example, we know that our line passes through the following two points:\n",
207 | "- (0,-2)\n",
208 | "- (6,7)\n",
209 | "\n",
210 | "We can take the x and y values from the first pair, and label them x1 and y1; and then take the x and y values from the second point and label them x2 and y2. Then we can plug those into our slope equation:\n",
211 | "\n",
212 | "\\begin{equation}m = \\frac{7 - -2}{6 - 0} \\end{equation}\n",
213 | "\n",
214 | "This is the same as:\n",
215 | "\n",
216 | "\\begin{equation}m = \\frac{7 + 2}{6 - 0} \\end{equation}\n",
217 | "\n",
218 | "That gives us the result 9/6 which is 11/2 or 1.5 .\n",
219 | "\n",
220 | "So what does that actually mean? Well, it tells us that for every change of **1** in x, **y** changes by 11/2 or 1.5. So if we start from any point on the line and move one unit to the right (along the X axis), we'll need to move 1.5 units up (along the Y axis) to get back to the line.\n",
221 | "\n",
222 | "You can plot the slope onto the original line with the following Python code to verify it fits:"
223 | ]
224 | },
225 | {
226 | "cell_type": "code",
227 | "execution_count": null,
228 | "metadata": {},
229 | "outputs": [],
230 | "source": [
231 | "plt.plot(df.x, df.y, color=\"grey\")\n",
232 | "plt.xlabel('x')\n",
233 | "plt.ylabel('y')\n",
234 | "plt.grid()\n",
235 | "plt.axhline()\n",
236 | "plt.axvline()\n",
237 | "\n",
238 | "# set the slope\n",
239 | "m = 1.5\n",
240 | "\n",
241 | "# get the y-intercept\n",
242 | "yInt = -2\n",
243 | "\n",
244 | "# plot the slope from the y-intercept for 1x\n",
245 | "mx = [0, 1]\n",
246 | "my = [yInt, yInt + m]\n",
247 | "plt.plot(mx,my, color='red', lw=5)\n",
248 | "\n",
249 | "plt.show()"
250 | ]
251 | },
252 | {
253 | "cell_type": "markdown",
254 | "metadata": {},
255 | "source": [
256 | "### Slope-Intercept Form\n",
257 | "One of the great things about algebraic expressions is that you can write the same equation in multiple ways, or *forms*. The *slope-intercept form* is a specific way of writing a 2-variable linear equation so that the equation definition includes the slope and y-intercept. The generalised slope-intercept form looks like this:\n",
258 | "\n",
259 | "\\begin{equation}y = mx + b \\end{equation}\n",
260 | "\n",
261 | "In this notation, ***m*** is the slope and ***b*** is the y-intercept.\n",
262 | "\n",
263 | "For example, let's look at the solved linear equation we've been working with so far in this section:\n",
264 | "\n",
265 | "\\begin{equation}y = \\frac{3x - 4}{2} \\end{equation}\n",
266 | "\n",
267 | "Now that we know the slope and y-intercept for the line that this equation defines, we can rewrite the equation as:\n",
268 | "\n",
269 | "\\begin{equation}y = 1\\frac{1}{2}x + -2 \\end{equation}\n",
270 | "\n",
271 | "You can see intuitively that this is true. In our original form of the equation, to find y we multiply x by three, subtract 4, and divide by two - in other words, y is half of 3x - 4; which is 1.5x - 2. So these equations are equivalent, but the slope-intercept form has the advantages of being simpler, and including two key pieces of information we need to plot the line represented by the equation. We know the y-intecept that the line passes through (0, -2), and we know the slope of the line (for every x, we add 1.5 to y.\n",
272 | "\n",
273 | "Let's recreate our set of test x and y values using the slope-intercept form of the equation, and plot them to prove that this describes the same line:"
274 | ]
275 | },
276 | {
277 | "cell_type": "code",
278 | "execution_count": null,
279 | "metadata": {},
280 | "outputs": [],
281 | "source": [
282 | "%matplotlib inline\n",
283 | "\n",
284 | "import pandas as pd\n",
285 | "from matplotlib import pyplot as plt\n",
286 | "\n",
287 | "# Create a dataframe with an x column containing values from -10 to 10\n",
288 | "df = pd.DataFrame ({'x': range(-10, 11)})\n",
289 | "\n",
290 | "# Define slope and y-intercept\n",
291 | "m = 1.5\n",
292 | "yInt = -2\n",
293 | "\n",
294 | "# Add a y column by applying the slope-intercept equation to x\n",
295 | "df['y'] = m*df['x'] + yInt\n",
296 | "\n",
297 | "# Plot the line\n",
298 | "from matplotlib import pyplot as plt\n",
299 | "\n",
300 | "plt.plot(df.x, df.y, color=\"grey\")\n",
301 | "plt.xlabel('x')\n",
302 | "plt.ylabel('y')\n",
303 | "plt.grid()\n",
304 | "plt.axhline()\n",
305 | "plt.axvline()\n",
306 | "\n",
307 | "# label the y-intercept\n",
308 | "plt.annotate('y-intercept',(0,yInt))\n",
309 | "\n",
310 | "# plot the slope from the y-intercept for 1x\n",
311 | "mx = [0, 1]\n",
312 | "my = [yInt, yInt + m]\n",
313 | "plt.plot(mx,my, color='red', lw=5)\n",
314 | "\n",
315 | "plt.show()"
316 | ]
317 | }
318 | ],
319 | "metadata": {
320 | "kernelspec": {
321 | "display_name": "Python 3",
322 | "language": "python",
323 | "name": "python3"
324 | },
325 | "language_info": {
326 | "codemirror_mode": {
327 | "name": "ipython",
328 | "version": 3
329 | },
330 | "file_extension": ".py",
331 | "mimetype": "text/x-python",
332 | "name": "python",
333 | "nbconvert_exporter": "python",
334 | "pygments_lexer": "ipython3",
335 | "version": "3.6.4"
336 | }
337 | },
338 | "nbformat": 4,
339 | "nbformat_minor": 2
340 | }
341 |
--------------------------------------------------------------------------------
/R/Module01/01-06-Factorization.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Factorization\n",
8 | "Factorization is the process of restating an expression as the *product* of two expressions (in other words, expressions multiplied together).\n",
9 | "\n",
10 | "For example, you can make the value **16** by performing the following multiplications of integer numbers:\n",
11 | "- 1 x 16\n",
12 | "- 2 x 8\n",
13 | "- 4 x 4\n",
14 | "\n",
15 | "Another way of saying this is that 1, 2, 4, 8, and 16 are all factors of 16.\n",
16 | "\n",
17 | "## Factors of Polynomial Expressions\n",
18 | "We can apply the same logic to polynomial expressions. For example, consider the following monomial expression:\n",
19 | "\n",
20 | "\\begin{equation}-6x^{2}y^{3} \\end{equation}\n",
21 | "\n",
22 | "You can get this value by performing the following multiplication:\n",
23 | "\n",
24 | "\\begin{equation}(2xy^{2})(-3xy) \\end{equation}\n",
25 | "\n",
26 | "Run the following R code to test this with arbitrary ***x*** and ***y*** values:"
27 | ]
28 | },
29 | {
30 | "cell_type": "code",
31 | "execution_count": null,
32 | "metadata": {},
33 | "outputs": [],
34 | "source": [
35 | "x = sample.int(100, 1)\n",
36 | "y = sample.int(100, 1)\n",
37 | "\n",
38 | "(2*x*y^2)*(-3*x*y) == -6*x^2*y^3"
39 | ]
40 | },
41 | {
42 | "cell_type": "markdown",
43 | "metadata": {},
44 | "source": [
45 | "So, we can say that **2xy2** and **-3xy** are both factors of **-6x2y3**.\n",
46 | "\n",
47 | "This also applies to polynomials with more than one term. For example, consider the following expression:\n",
48 | "\n",
49 | "\\begin{equation}(x + 2)(2x^{2} - 3y + 2) = 2x^{3} + 4x^{2} - 3xy + 2x - 6y + 4 \\end{equation}\n",
50 | "\n",
51 | "Based on this, **x+2** and **2x2 - 3y + 2** are both factors of **2x3 + 4x2 - 3xy + 2x - 6y + 4**.\n",
52 | "\n",
53 | "(and if you don't believe me, you can try this with random values for x and y with the following R code):"
54 | ]
55 | },
56 | {
57 | "cell_type": "code",
58 | "execution_count": null,
59 | "metadata": {},
60 | "outputs": [],
61 | "source": [
62 | "x = sample.int(100, 1)\n",
63 | "y = sample.int(100, 1)\n",
64 | "\n",
65 | "(x + 2)*(2*x^2 - 3*y + 2) == 2*x^3 + 4*x^2 - 3*x*y + 2*x - 6*y + 4"
66 | ]
67 | },
68 | {
69 | "cell_type": "markdown",
70 | "metadata": {},
71 | "source": [
72 | "## Greatest Common Factor\n",
73 | "Of course, these may not be the only factors of **-6x2y3**, just as 8 and 2 are not the only factors of 16.\n",
74 | "\n",
75 | "Additionally, 2 and 8 aren't just factors of 16; they're factors of other numbers too - for example, they're both factors of 24 (because 2 x 12 = 24 and 8 x 3 = 24). Which leads us to the question, what is the highest number that is a factor of both 16 and 24? Well, let's look at all the numbers that multiply evenly into 12 and all the numbers that multiply evenly into 24:\n",
76 | "\n",
77 | "| 16 | 24 |\n",
78 | "|--------|--------|\n",
79 | "| 1 x 16 | 1 x 24 |\n",
80 | "| 2 x **8** | 2 x 12 |\n",
81 | "| - | 3 x **8** |\n",
82 | "| 4 x 4 | 4 x 6 |\n",
83 | "\n",
84 | "The highest value that is a multiple of both 16 and 24 is **8**, so 8 is the *Greatest Common Factor* (or GCF) of 16 and 24.\n",
85 | "\n",
86 | "OK, let's apply that logic to the following expressions:\n",
87 | "\n",
88 | "\\begin{equation}15x^{2}y\\;\\;\\;\\;\\;\\;\\;\\;9xy^{3}\\end{equation}\n",
89 | "\n",
90 | "So what's the greatest common factor of these two expressions?\n",
91 | "\n",
92 | "It helps to break the expressions into their constituent components. Let's deal with the coefficients first; we have 15 and 9. The highest value that divides evenly into both of these is **3** (3 x 5 = 15 and 3 x 3 = 9).\n",
93 | "\n",
94 | "Now let's look at the ***x*** terms; we have x2 and x. The highest value that divides evenly into both is these is **x** (*x* goes into *x* once and into *x*2 *x* times).\n",
95 | "\n",
96 | "Finally, for our ***y*** terms, we have y and y3. The highest value that divides evenly into both is these is **y** (*y* goes into *y* once and into *y*3 *y•y* times).\n",
97 | "\n",
98 | "Putting all of that together, the GCF of both of our expression is:\n",
99 | "\n",
100 | "\\begin{equation}3xy\\end{equation}\n",
101 | "\n",
102 | "An easy shortcut to identifying the GCF of an expression that includes variables with exponentials is that it will always consist of:\n",
103 | "- The *largest* numeric factor of the numeric coefficients in the polynomial expressions (in this case 3)\n",
104 | "- The *smallest* exponential of each variable (in this case, x and y, which technically are x1 and y1.\n",
105 | "\n",
106 | "You can check your answer by dividing the original expressions by the GCF to find the coefficient expressions for the GCF (in other words, how many times the GCF divides into the original expression). The result, when multiplied by the GCF will always produce the original expression. So in this case, we need to perform the following divisions:\n",
107 | "\n",
108 | "\\begin{equation}\\frac{15x^{2}y}{3xy}\\;\\;\\;\\;\\;\\;\\;\\;\\frac{9xy^{3}}{3xy}\\end{equation}\n",
109 | "\n",
110 | "These fractions simplify to **5x** and **3y2**, giving us the following calculations to prove our factorization:\n",
111 | "\n",
112 | "\\begin{equation}3xy(5x) = 15x^{2}y\\end{equation}\n",
113 | "\\begin{equation}3xy(3y^{2}) = 9xy^{3}\\end{equation}\n",
114 | "\n",
115 | "Let's try both of those in R:"
116 | ]
117 | },
118 | {
119 | "cell_type": "code",
120 | "execution_count": null,
121 | "metadata": {},
122 | "outputs": [],
123 | "source": [
124 | "x = sample.int(100, 1)\n",
125 | "y = sample.int(100, 1)\n",
126 | "\n",
127 | "print((3*x*y)*(5*x) == 15*x^2*y)\n",
128 | "print((3*x*y)*(3*y^2) == 9*x*y^3)"
129 | ]
130 | },
131 | {
132 | "cell_type": "markdown",
133 | "metadata": {},
134 | "source": [
135 | "## Distributing Factors\n",
136 | "Let's look at another example. Here is a binomial expression:\n",
137 | "\n",
138 | "\\begin{equation}6x + 15y \\end{equation}\n",
139 | "\n",
140 | "To factor this, we need to find an expression that divides equally into both of these expressions. In this case, we can use **3** to factor the coefficients, because 3 • 2x = 6x and 3• 5y = 15y, so we can write our original expression as:\n",
141 | "\n",
142 | "\\begin{equation}6x + 15y = 3(2x) + 3(5y) \\end{equation}\n",
143 | "\n",
144 | "Now, remember the distributive property? It enables us to multiply each term of an expression by the same factor to calculate the product of the expression multiplied by the factor. We can *factor-out* the common factor in this expression to distribute it like this:\n",
145 | "\n",
146 | "\\begin{equation}6x + 15y = 3(2x) + 3(5y) = \\mathbf{3(2x + 5y)} \\end{equation}\n",
147 | "\n",
148 | "Let's prove to ourselves that these all evaluate to the same thing:"
149 | ]
150 | },
151 | {
152 | "cell_type": "code",
153 | "execution_count": null,
154 | "metadata": {},
155 | "outputs": [],
156 | "source": [
157 | "x = sample.int(100, 1)\n",
158 | "y = sample.int(100, 1)\n",
159 | "\n",
160 | "((6*x + 15*y) == (3*(2*x) + 3*(5*y))) & ((3*(2*x) + 3*(5*y)) == (3*(2*x + 5*y)))"
161 | ]
162 | },
163 | {
164 | "cell_type": "markdown",
165 | "metadata": {},
166 | "source": [
167 | "For something a little more complex, let's return to our previous example. Suppose we want to add our original 15x2y and 9xy3 expressions:\n",
168 | "\n",
169 | "\\begin{equation}15x^{2}y + 9xy^{3}\\end{equation}\n",
170 | "\n",
171 | "We've already calculated the common factor, so we know that:\n",
172 | "\n",
173 | "\\begin{equation}3xy(5x) = 15x^{2}y\\end{equation}\n",
174 | "\\begin{equation}3xy(3y^{2}) = 9xy^{3}\\end{equation}\n",
175 | "\n",
176 | "Now we can factor-out the common factor to produce a single expression:\n",
177 | "\n",
178 | "\\begin{equation}15x^{2}y + 9xy^{3} = \\mathbf{3xy(5x + 3y^{2})}\\end{equation}\n",
179 | "\n",
180 | "And here's the R test code:"
181 | ]
182 | },
183 | {
184 | "cell_type": "code",
185 | "execution_count": null,
186 | "metadata": {},
187 | "outputs": [],
188 | "source": [
189 | "x = sample.int(100, 1)\n",
190 | "y = sample.int(100, 1)\n",
191 | "\n",
192 | "(15*x^2*y + 9*x*y^3) == (3*x*y*(5*x + 3*y^2))"
193 | ]
194 | },
195 | {
196 | "cell_type": "markdown",
197 | "metadata": {},
198 | "source": [
199 | "So you might be wondering what's so great about being able to distribute the common factor like this. The answer is that it can often be useful to apply a common factor to multiple terms in order to solve seemingly complex problems.\n",
200 | "\n",
201 | "For example, consider this:\n",
202 | "\n",
203 | "\\begin{equation}x^{2} + y^{2} + z^{2} = 127\\end{equation}\n",
204 | "\n",
205 | "Now solve this equation:\n",
206 | "\n",
207 | "\\begin{equation}a = 5x^{2} + 5y^{2} + 5z^{2}\\end{equation}\n",
208 | "\n",
209 | "At first glance, this seems tricky because there are three unknown variables, and even though we know that their squares add up to 127, we don't know their individual values. However, we can distribute the common factor and apply what we *do* know. Let's restate the problem like this:\n",
210 | "\n",
211 | "\\begin{equation}a = 5(x^{2} + y^{2} + z^{2})\\end{equation}\n",
212 | "\n",
213 | "Now it becomes easier to solve, because we know that the expression in parenthesis is equal to 127, so actually our equation is:\n",
214 | "\n",
215 | "\\begin{equation}a = 5(127)\\end{equation}\n",
216 | "\n",
217 | "So ***a*** is 5 times 127, which is 635"
218 | ]
219 | },
220 | {
221 | "cell_type": "markdown",
222 | "metadata": {},
223 | "source": [
224 | "## Formulae for Factoring Squares\n",
225 | "There are some useful ways that you can employ factoring to deal with expressions that contain squared values (that is, values with an exponential of 2).\n",
226 | "\n",
227 | "### Differences of Squares\n",
228 | "Consider the following expression:\n",
229 | "\n",
230 | "\\begin{equation}x^{2} - 9\\end{equation}\n",
231 | "\n",
232 | "The constant *9* is 32, so we could rewrite this as:\n",
233 | "\n",
234 | "\\begin{equation}x^{2} - 3^{2}\\end{equation}\n",
235 | "\n",
236 | "Whenever you need to subtract one squared term from another, you can use an approach called the *difference of squares*, whereby we can factor *a2 - b2* as *(a - b)(a + b)*; so we can rewrite the expression as:\n",
237 | "\n",
238 | "\\begin{equation}(x - 3)(x + 3)\\end{equation}\n",
239 | "\n",
240 | "Run the code below to check this:"
241 | ]
242 | },
243 | {
244 | "cell_type": "code",
245 | "execution_count": null,
246 | "metadata": {},
247 | "outputs": [],
248 | "source": [
249 | "x = sample.int(100, 1)\n",
250 | "\n",
251 | "(x^2 - 9) == (x - 3)*(x + 3)"
252 | ]
253 | },
254 | {
255 | "cell_type": "markdown",
256 | "metadata": {},
257 | "source": [
258 | "### Perfect Squares\n",
259 | "A *perfect square* is a number multiplied by itself, for example 3 multiplied by 3 is 9, so 9 is a perfect square.\n",
260 | "\n",
261 | "When working with equations, the ability to factor between polynomial expressions and binomial perfect square expressions can be a useful tool. For example, consider this expression:\n",
262 | "\n",
263 | "\\begin{equation}x^{2} + 10x + 25\\end{equation}\n",
264 | "\n",
265 | "We can use 5 as a common factor to rewrite this as:\n",
266 | "\n",
267 | "\\begin{equation}(x + 5)(x + 5)\\end{equation}\n",
268 | "\n",
269 | "So what happened here?\n",
270 | "\n",
271 | "Well, first we found a common factor for our coefficients: 5 goes into 10 twice and into 25 five times (in other words, squared). Then we just expressed this factoring as a multiplication of two identical binomials *(x + 5)(x + 5)*.\n",
272 | "\n",
273 | "Remember the rule for multiplication of polynomials is to multiple each term in the first polynomial by each term in the second polynomial and then add the results; so you can do this to verify the factorization:\n",
274 | "\n",
275 | "- x • x = x2\n",
276 | "- x • 5 = 5x\n",
277 | "- 5 • x = 5x\n",
278 | "- 5 • 5 = 25\n",
279 | "\n",
280 | "When you combine the two 5x terms we get back to our original expression of x2 + 10x + 25.\n",
281 | "\n",
282 | "Now we have an expression multiplied by itself; in other words, a perfect square. We can therefore rewrite this as:\n",
283 | "\n",
284 | "\\begin{equation}(x + 5)^{2}\\end{equation}\n",
285 | "\n",
286 | "Factorization of perfect squares is a useful technique, as you'll see when we start to tackle quadratic equations in the next section. In fact, it's so useful that it's worth memorizing its formula:\n",
287 | "\n",
288 | "\\begin{equation}(a + b)^{2} = a^{2} + b^{2}+ 2ab \\end{equation}\n",
289 | "\n",
290 | "In our example, the *a* terms is ***x*** and the *b* terms is ***5***, and in standard form, our equation *x2 + 10x + 25* is actually *a2 + 2ab + b2*. The operations are all additions, so the order isn't actually important!\n",
291 | "\n",
292 | "Run the following code with random values for *a* and *b* to verify that the formula works:"
293 | ]
294 | },
295 | {
296 | "cell_type": "code",
297 | "execution_count": null,
298 | "metadata": {},
299 | "outputs": [],
300 | "source": [
301 | "a = sample.int(100, 1)\n",
302 | "b = sample.int(100, 1)\n",
303 | "\n",
304 | "a^2 + b^2 + (2*a*b) == (a + b)^2"
305 | ]
306 | }
307 | ],
308 | "metadata": {
309 | "kernelspec": {
310 | "display_name": "R",
311 | "language": "R",
312 | "name": "ir"
313 | },
314 | "language_info": {
315 | "codemirror_mode": "r",
316 | "file_extension": ".r",
317 | "mimetype": "text/x-r-source",
318 | "name": "R",
319 | "pygments_lexer": "r",
320 | "version": "3.5.0"
321 | }
322 | },
323 | "nbformat": 4,
324 | "nbformat_minor": 2
325 | }
326 |
--------------------------------------------------------------------------------
/R/Module01/01-01-Introduction to Equations.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {
6 | "collapsed": true
7 | },
8 | "source": [
9 | "# Getting Started with Equations\n",
10 | "Equations are calculations in which one or more variables represent unknown values. In this notebook, you'll learn some fundamental techniques for solving simple equations.\n",
11 | "\n",
12 | "## One Step Equations\n",
13 | "Consider the following equation:\n",
14 | "\n",
15 | "\\begin{equation}x + 16 = -25\\end{equation}\n",
16 | "\n",
17 | "The challenge here is to find the value for **x**, and to do this we need to *isolate the variable*. In this case, we need to get **x** onto one side of the \"=\" sign, and all of the other values onto the other side. To accomplish this we'll follow these rules:\n",
18 | "1. Use opposite operations to cancel out the values we don't want on one side. In this case, the left side of the equation includes an addition of 16, so we'll cancel that out by subtracting 16 and the left side of the equation becomes **x + 16 - 16**.\n",
19 | "2. Whatever you do to one side, you must also do to the other side. In this case, we subtracted 16 from the left side, so we must also subtract 16 from the right side of the equation, which becomes **-25 - 16**.\n",
20 | "Our equation now looks like this:\n",
21 | "\n",
22 | "\\begin{equation}x + 16 - 16 = -25 - 16\\end{equation}\n",
23 | "\n",
24 | "Now we can calculate the values on both side. On the left side, 16 - 16 is 0, so we're left with:\n",
25 | "\n",
26 | "\\begin{equation}x = -25 - 16\\end{equation}\n",
27 | "\n",
28 | "Which yields the result **-41**. Our equation is now solved, as you can see here:\n",
29 | "\n",
30 | "\\begin{equation}x = -41\\end{equation}\n",
31 | "\n",
32 | "It's always good practice to verify your result by plugging the variable value you've calculated into the original equation and ensuring that it holds true. We can easily do that by using some simple R code.\n",
33 | "\n",
34 | "To verify the equation using R code, place the cursor in the following cell and then click the ►| button in the toolbar."
35 | ]
36 | },
37 | {
38 | "cell_type": "code",
39 | "execution_count": null,
40 | "metadata": {},
41 | "outputs": [],
42 | "source": [
43 | "x = -41\n",
44 | "x + 16 == -25"
45 | ]
46 | },
47 | {
48 | "cell_type": "markdown",
49 | "metadata": {},
50 | "source": [
51 | "## Two-Step Equations\n",
52 | "The previous example was fairly simple - you could probably work it out in your head. So what about something a little more complex?\n",
53 | "\n",
54 | "\\begin{equation}3x - 2 = 10 \\end{equation}\n",
55 | "\n",
56 | "As before, we need to isolate the variable **x**, but this time we'll do it in two steps. The first thing we'll do is to cancel out the *constants*. A constant is any number that stands on its own, so in this case the 2 that we're subtracting on the left side is a constant. We'll use an opposite operation to cancel it out on the left side, so since the current operation is to subtract 2, we'll add 2; and of course whatever we do on the left side we also need to do on the right side, so after the first step, our equation looks like this:\n",
57 | "\n",
58 | "\\begin{equation}3x - 2 + 2 = 10 + 2 \\end{equation}\n",
59 | "\n",
60 | "Now the -2 and +2 on the left cancel one another out, and on the right side, 10 + 2 is 12; so the equation is now:\n",
61 | "\n",
62 | "\\begin{equation}3x = 12 \\end{equation}\n",
63 | "\n",
64 | "OK, time for step two - we need to deal with the *coefficients* - a coefficient is a number that is applied to a variable. In this case, our expression on the left is 3x, which means x multiplied by 3; so we can apply the opposite operation to cancel it out as long as we do the same to the other side, like this:\n",
65 | "\n",
66 | "\\begin{equation}\\frac{3x}{3} = \\frac{12}{3} \\end{equation}\n",
67 | "\n",
68 | "3x ÷ 3 is x, so we've now isolated the variable\n",
69 | "\n",
70 | "\\begin{equation}x = \\frac{12}{3} \\end{equation}\n",
71 | "\n",
72 | "And we can calculate the result as 12/3 which is **4**:\n",
73 | "\n",
74 | "\\begin{equation}x = 4 \\end{equation}\n",
75 | "\n",
76 | "Let's verify that result using R:"
77 | ]
78 | },
79 | {
80 | "cell_type": "code",
81 | "execution_count": null,
82 | "metadata": {},
83 | "outputs": [],
84 | "source": [
85 | "x = 4\n",
86 | "3*x - 2 == 10"
87 | ]
88 | },
89 | {
90 | "cell_type": "markdown",
91 | "metadata": {},
92 | "source": [
93 | "## Combining Like Terms\n",
94 | "Like terms are elements of an expression that relate to the same variable or constant (with the same *order* or *exponential*, which we'll discuss later). For example, consider the following equation:\n",
95 | "\n",
96 | "\\begin{equation}\\textbf{5x} + 1 \\textbf{- 2x} = 22 \\end{equation}\n",
97 | "\n",
98 | "In this equation, the left side includes the terms **5x** and **- 2x**, both of which represent the variable **x** multiplied by a coefficient. Note that we include the sign (+ or -) in front of the value.\n",
99 | "\n",
100 | "We can rewrite the equation to combine these like terms:\n",
101 | "\n",
102 | "\\begin{equation}\\textbf{5x - 2x} + 1 = 22 \\end{equation}\n",
103 | "\n",
104 | "We can then simply perform the necessary operations on the like terms to consolidate them into a single term:\n",
105 | "\n",
106 | "\\begin{equation}\\textbf{3x} + 1 = 22 \\end{equation}\n",
107 | "\n",
108 | "Now, we can solve this like any other two-step equation. First we'll remove the constants from the left side - in this case, there's a constant expression that adds 1, so we'll use the opposite operation to remove it and do the same on the other side:\n",
109 | "\n",
110 | "\\begin{equation}3x + 1 - 1 = 22 - 1 \\end{equation}\n",
111 | "\n",
112 | "That gives us:\n",
113 | "\n",
114 | "\\begin{equation}3x = 21 \\end{equation}\n",
115 | "\n",
116 | "Then we'll deal with the coefficients - in this case x is multiplied by 3, so we'll divide by 3 on both sides to remove that:\n",
117 | "\n",
118 | "\\begin{equation}\\frac{3x}{3} = \\frac{21}{3} \\end{equation}\n",
119 | "\n",
120 | "This give us our answer:\n",
121 | "\n",
122 | "\\begin{equation}x = 7 \\end{equation}"
123 | ]
124 | },
125 | {
126 | "cell_type": "code",
127 | "execution_count": null,
128 | "metadata": {},
129 | "outputs": [],
130 | "source": [
131 | "x = 7\n",
132 | "5*x + 1 - 2*x == 22"
133 | ]
134 | },
135 | {
136 | "cell_type": "markdown",
137 | "metadata": {},
138 | "source": [
139 | "## Working with Fractions\n",
140 | "Some of the steps in solving the equations above have involved working wth fractions - which in themselves are actually just division operations. Let's take a look at an example of an equation in which our variable is defined as a fraction:\n",
141 | "\n",
142 | "\\begin{equation}\\frac{x}{3} + 1 = 16 \\end{equation}\n",
143 | "\n",
144 | "We follow the same approach as before, first removing the constants from the left side - so we'll subtract 1 from both sides.\n",
145 | "\n",
146 | "\\begin{equation}\\frac{x}{3} = 15 \\end{equation}\n",
147 | "\n",
148 | "Now we need to deal with the fraction on the left so that we're left with just **x**. The fraction is x/3 which is another way of saying *x divided by 3*, so we can apply the opposite operation to both sides. In this case, we need to multiply both sides by the denominator under our variable, which is 3. To make it easier to work with a term that contains fractions, we can express whole numbers as fractions with a denominator of 1; so on the left, we can express 3 as 3/1 and multiply it with x/3. Note that the notation for multiplication is a **•** symbol rather than the standard *x* multiplication operator (which would cause confusion with the variable **x**) or the asterisk symbol used by most programming languages.\n",
149 | "\n",
150 | "\\begin{equation}\\frac{3}{1} \\cdot \\frac{x}{3} = 15 \\cdot 3 \\end{equation}\n",
151 | "\n",
152 | "This gives us the following result:\n",
153 | "\n",
154 | "\\begin{equation}x = 45 \\end{equation}\n",
155 | "\n",
156 | "Let's verify that with some R code:"
157 | ]
158 | },
159 | {
160 | "cell_type": "code",
161 | "execution_count": null,
162 | "metadata": {},
163 | "outputs": [],
164 | "source": [
165 | "x = 45\n",
166 | "x/3 + 1 == 16"
167 | ]
168 | },
169 | {
170 | "cell_type": "markdown",
171 | "metadata": {},
172 | "source": [
173 | "Let's look at another example, in which the variable is a whole number, but its coefficient is a fraction:\n",
174 | "\n",
175 | "\\begin{equation}\\frac{2}{5}x + 1 = 11 \\end{equation}\n",
176 | "\n",
177 | "As usual, we'll start by removing the constants from the variable expression; so in this case we need to subtract 1 from both sides:\n",
178 | "\n",
179 | "\\begin{equation}\\frac{2}{5}x = 10 \\end{equation}\n",
180 | "\n",
181 | "Now we need to cancel out the fraction. The expression equates to two-fifths times x, so the opposite operation is to divide by 2/5; but a simpler way to do this with a fraction is to multiply it by its *reciprocal*, which is just the inverse of the fraction, in this case 5/2. Of course, we need to do this to both sides:\n",
182 | "\n",
183 | "\\begin{equation}\\frac{5}{2} \\cdot \\frac{2}{5}x = \\frac{10}{1} \\cdot \\frac{5}{2} \\end{equation}\n",
184 | "\n",
185 | "That gives us the following result:\n",
186 | "\n",
187 | "\\begin{equation}x = \\frac{50}{2} \\end{equation}\n",
188 | "\n",
189 | "Which we can simplify to:\n",
190 | "\n",
191 | "\\begin{equation}x = 25 \\end{equation}\n",
192 | "\n",
193 | "We can confirm that with R:"
194 | ]
195 | },
196 | {
197 | "cell_type": "code",
198 | "execution_count": null,
199 | "metadata": {},
200 | "outputs": [],
201 | "source": [
202 | "x = 25\n",
203 | "2/5 * x + 1 ==11"
204 | ]
205 | },
206 | {
207 | "cell_type": "markdown",
208 | "metadata": {},
209 | "source": [
210 | "## Equations with Variables on Both Sides\n",
211 | "So far, all of our equations have had a variable term on only one side. However, variable terms can exist on both sides. \n",
212 | "\n",
213 | "Consider this equation:\n",
214 | "\n",
215 | "\\begin{equation}3x + 2 = 5x - 1 \\end{equation}\n",
216 | "\n",
217 | "This time, we have terms that include **x** on both sides. Let's take exactly the same approach to solving this kind of equation as we did for the previous examples. First, let's deal with the constants by adding 1 to both sides. That gets rid of the -1 on the right:\n",
218 | "\n",
219 | "\\begin{equation}3x + 3 = 5x \\end{equation}\n",
220 | "\n",
221 | "Now we can eliminate the variable expression from one side by subtracting 3x from both sides. That gets rid of the 3x on the left:\n",
222 | "\n",
223 | "\\begin{equation}3 = 2x \\end{equation}\n",
224 | "\n",
225 | "Next, we can deal with the coefficient by dividing both sides by 2:\n",
226 | "\n",
227 | "\\begin{equation}\\frac{3}{2} = x \\end{equation}\n",
228 | "\n",
229 | "Now we've isolated x. It looks a little strange because we usually have the variable on the left side, so if it makes you more comfortable you can simply reverse the equation:\n",
230 | "\n",
231 | "\\begin{equation}x = \\frac{3}{2} \\end{equation}\n",
232 | "\n",
233 | "Finally, this answer is correct as it is; but 3/2 is an improper fraction. We can simplify it to:\n",
234 | "\n",
235 | "\\begin{equation}x = 1\\frac{1}{2} \\end{equation}\n",
236 | "\n",
237 | "So x is 11/2 (which is of course 1.5 in decimal notation).\n",
238 | "Let's check it in R:"
239 | ]
240 | },
241 | {
242 | "cell_type": "code",
243 | "execution_count": null,
244 | "metadata": {},
245 | "outputs": [],
246 | "source": [
247 | "x = 1.5\n",
248 | "3*x + 2 == 5*x -1"
249 | ]
250 | },
251 | {
252 | "cell_type": "markdown",
253 | "metadata": {},
254 | "source": [
255 | "## Using the Distributive Property\n",
256 | "The distributive property is a mathematical law that enables us to distribute the same operation to terms within parenthesis. For example, consider the following equation:\n",
257 | "\n",
258 | "\\begin{equation}\\textbf{4(x + 2)} + \\textbf{3(x - 2)} = 16 \\end{equation}\n",
259 | "\n",
260 | "The equation includes two operations in parenthesis: **4(*x* + 2)** and **3(*x* - 2)**. Each of these operations consists of a constant by which the contents of the parenthesis must be multiplied: for example, 4 times (*x* + 2). The distributive property means that we can achieve the same result by multiplying each term in the parenthesis and adding the results, so for the first parenthetical operation, we can multiply 4 by *x* and add it to 4 times +2; and for the second parenthetical operation, we can calculate 3 times *x* + 3 times -2). Note that the constants in the parenthesis include the sign (+ or -) that proceed them:\n",
261 | "\n",
262 | "\\begin{equation}4x + 8 + 3x - 6 = 16 \\end{equation}\n",
263 | "\n",
264 | "Now we can group our like terms:\n",
265 | "\n",
266 | "\\begin{equation}7x + 2 = 16 \\end{equation}\n",
267 | "\n",
268 | "Then we move the constant to the other side:\n",
269 | "\n",
270 | "\\begin{equation}7x = 14 \\end{equation}\n",
271 | "\n",
272 | "And now we can deal with the coefficient:\n",
273 | "\n",
274 | "\\begin{equation}\\frac{7x}{7} = \\frac{14}{7} \\end{equation}\n",
275 | "\n",
276 | "Which gives us our answer:\n",
277 | "\n",
278 | "\\begin{equation}x = 2 \\end{equation}\n",
279 | "\n",
280 | "Here's the original equation with the calculated value for *x* in R:"
281 | ]
282 | },
283 | {
284 | "cell_type": "code",
285 | "execution_count": null,
286 | "metadata": {},
287 | "outputs": [],
288 | "source": [
289 | "x = 2\n",
290 | "4*(x + 2) + 3*(x - 2) == 16"
291 | ]
292 | }
293 | ],
294 | "metadata": {
295 | "kernelspec": {
296 | "display_name": "R",
297 | "language": "R",
298 | "name": "ir"
299 | },
300 | "language_info": {
301 | "codemirror_mode": "r",
302 | "file_extension": ".r",
303 | "mimetype": "text/x-r-source",
304 | "name": "R",
305 | "pygments_lexer": "r",
306 | "version": "3.5.0"
307 | }
308 | },
309 | "nbformat": 4,
310 | "nbformat_minor": 2
311 | }
312 |
--------------------------------------------------------------------------------
/Python/Module01/01-01-Introduction to Equations.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {
6 | "collapsed": true
7 | },
8 | "source": [
9 | "# Getting Started with Equations\n",
10 | "Equations are calculations in which one or more variables represent unknown values. In this notebook, you'll learn some fundamental techniques for solving simple equations.\n",
11 | "\n",
12 | "## One Step Equations\n",
13 | "Consider the following equation:\n",
14 | "\n",
15 | "\\begin{equation}x + 16 = -25\\end{equation}\n",
16 | "\n",
17 | "The challenge here is to find the value for **x**, and to do this we need to *isolate the variable*. In this case, we need to get **x** onto one side of the \"=\" sign, and all of the other values onto the other side. To accomplish this we'll follow these rules:\n",
18 | "1. Use opposite operations to cancel out the values we don't want on one side. In this case, the left side of the equation includes an addition of 16, so we'll cancel that out by subtracting 16 and the left side of the equation becomes **x + 16 - 16**.\n",
19 | "2. Whatever you do to one side, you must also do to the other side. In this case, we subtracted 16 from the left side, so we must also subtract 16 from the right side of the equation, which becomes **-25 - 16**.\n",
20 | "Our equation now looks like this:\n",
21 | "\n",
22 | "\\begin{equation}x + 16 - 16 = -25 - 16\\end{equation}\n",
23 | "\n",
24 | "Now we can calculate the values on both side. On the left side, 16 - 16 is 0, so we're left with:\n",
25 | "\n",
26 | "\\begin{equation}x = -25 - 16\\end{equation}\n",
27 | "\n",
28 | "Which yields the result **-41**. Our equation is now solved, as you can see here:\n",
29 | "\n",
30 | "\\begin{equation}x = -41\\end{equation}\n",
31 | "\n",
32 | "It's always good practice to verify your result by plugging the variable value you've calculated into the original equation and ensuring that it holds true. We can easily do that by using some simple Python code.\n",
33 | "\n",
34 | "To verify the equation using Python code, place the cursor in the following cell and then click the ►| button in the toolbar."
35 | ]
36 | },
37 | {
38 | "cell_type": "code",
39 | "execution_count": null,
40 | "metadata": {
41 | "collapsed": true
42 | },
43 | "outputs": [],
44 | "source": [
45 | "x = -41\n",
46 | "x + 16 == -25"
47 | ]
48 | },
49 | {
50 | "cell_type": "markdown",
51 | "metadata": {},
52 | "source": [
53 | "## Two-Step Equations\n",
54 | "The previous example was fairly simple - you could probably work it out in your head. So what about something a little more complex?\n",
55 | "\n",
56 | "\\begin{equation}3x - 2 = 10 \\end{equation}\n",
57 | "\n",
58 | "As before, we need to isolate the variable **x**, but this time we'll do it in two steps. The first thing we'll do is to cancel out the *constants*. A constant is any number that stands on its own, so in this case the 2 that we're subtracting on the left side is a constant. We'll use an opposite operation to cancel it out on the left side, so since the current operation is to subtract 2, we'll add 2; and of course whatever we do on the left side we also need to do on the right side, so after the first step, our equation looks like this:\n",
59 | "\n",
60 | "\\begin{equation}3x - 2 + 2 = 10 + 2 \\end{equation}\n",
61 | "\n",
62 | "Now the -2 and +2 on the left cancel one another out, and on the right side, 10 + 2 is 12; so the equation is now:\n",
63 | "\n",
64 | "\\begin{equation}3x = 12 \\end{equation}\n",
65 | "\n",
66 | "OK, time for step two - we need to deal with the *coefficients* - a coefficient is a number that is applied to a variable. In this case, our expression on the left is 3x, which means x multiplied by 3; so we can apply the opposite operation to cancel it out as long as we do the same to the other side, like this:\n",
67 | "\n",
68 | "\\begin{equation}\\frac{3x}{3} = \\frac{12}{3} \\end{equation}\n",
69 | "\n",
70 | "3x ÷ 3 is x, so we've now isolated the variable\n",
71 | "\n",
72 | "\\begin{equation}x = \\frac{12}{3} \\end{equation}\n",
73 | "\n",
74 | "And we can calculate the result as 12/3 which is **4**:\n",
75 | "\n",
76 | "\\begin{equation}x = 4 \\end{equation}\n",
77 | "\n",
78 | "Let's verify that result using Python:"
79 | ]
80 | },
81 | {
82 | "cell_type": "code",
83 | "execution_count": null,
84 | "metadata": {
85 | "collapsed": true
86 | },
87 | "outputs": [],
88 | "source": [
89 | "x = 4\n",
90 | "3*x - 2 == 10"
91 | ]
92 | },
93 | {
94 | "cell_type": "markdown",
95 | "metadata": {},
96 | "source": [
97 | "## Combining Like Terms\n",
98 | "Like terms are elements of an expression that relate to the same variable or constant (with the same *order* or *exponential*, which we'll discuss later). For example, consider the following equation:\n",
99 | "\n",
100 | "\\begin{equation}\\textbf{5x} + 1 \\textbf{- 2x} = 22 \\end{equation}\n",
101 | "\n",
102 | "In this equation, the left side includes the terms **5x** and **- 2x**, both of which represent the variable **x** multiplied by a coefficent. Note that we include the sign (+ or -) in front of the value.\n",
103 | "\n",
104 | "We can rewrite the equation to combine these like terms:\n",
105 | "\n",
106 | "\\begin{equation}\\textbf{5x - 2x} + 1 = 22 \\end{equation}\n",
107 | "\n",
108 | "We can then simply perform the necessary operations on the like terms to consolidate them into a single term:\n",
109 | "\n",
110 | "\\begin{equation}\\textbf{3x} + 1 = 22 \\end{equation}\n",
111 | "\n",
112 | "Now, we can solve this like any other two-step equation. First we'll remove the constants from the left side - in this case, there's a constant expression that adds 1, so we'll use the opposite operation to remove it and do the same on the other side:\n",
113 | "\n",
114 | "\\begin{equation}3x + 1 - 1 = 22 - 1 \\end{equation}\n",
115 | "\n",
116 | "That gives us:\n",
117 | "\n",
118 | "\\begin{equation}3x = 21 \\end{equation}\n",
119 | "\n",
120 | "Then we'll deal with the coefficients - in this case x is multiplied by 3, so we'll divide by 3 on boths sides to remove that:\n",
121 | "\n",
122 | "\\begin{equation}\\frac{3x}{3} = \\frac{21}{3} \\end{equation}\n",
123 | "\n",
124 | "This give us our answer:\n",
125 | "\n",
126 | "\\begin{equation}x = 7 \\end{equation}"
127 | ]
128 | },
129 | {
130 | "cell_type": "code",
131 | "execution_count": null,
132 | "metadata": {
133 | "collapsed": true
134 | },
135 | "outputs": [],
136 | "source": [
137 | "x = 7\n",
138 | "5*x + 1 - 2*x == 22"
139 | ]
140 | },
141 | {
142 | "cell_type": "markdown",
143 | "metadata": {},
144 | "source": [
145 | "## Working with Fractions\n",
146 | "Some of the steps in solving the equations above have involved working wth fractions - which in themselves are actually just division operations. Let's take a look at an example of an equation in which our variable is defined as a fraction:\n",
147 | "\n",
148 | "\\begin{equation}\\frac{x}{3} + 1 = 16 \\end{equation}\n",
149 | "\n",
150 | "We follow the same approach as before, first removing the constants from the left side - so we'll subtract 1 from both sides.\n",
151 | "\n",
152 | "\\begin{equation}\\frac{x}{3} = 15 \\end{equation}\n",
153 | "\n",
154 | "Now we need to deal with the fraction on the left so that we're left with just **x**. The fraction is x/3 which is another way of saying *x divided by 3*, so we can apply the opposite operation to both sides. In this case, we need to multiply both sides by the denominator under our variable, which is 3. To make it easier to work with a term that contains fractions, we can express whole numbers as fractions with a denominator of 1; so on the left, we can express 3 as 3/1 and multiply it with x/3. Note that the notation for mutiplication is a **•** symbol rather than the standard *x* multiplication operator (which would cause confusion with the variable **x**) or the asterisk symbol used by most programming languages.\n",
155 | "\n",
156 | "\\begin{equation}\\frac{3}{1} \\cdot \\frac{x}{3} = 15 \\cdot 3 \\end{equation}\n",
157 | "\n",
158 | "This gives us the following result:\n",
159 | "\n",
160 | "\\begin{equation}x = 45 \\end{equation}\n",
161 | "\n",
162 | "Let's verify that with some Python code:"
163 | ]
164 | },
165 | {
166 | "cell_type": "code",
167 | "execution_count": null,
168 | "metadata": {
169 | "collapsed": true
170 | },
171 | "outputs": [],
172 | "source": [
173 | "x = 45\n",
174 | "x/3 + 1 == 16"
175 | ]
176 | },
177 | {
178 | "cell_type": "markdown",
179 | "metadata": {},
180 | "source": [
181 | "Let's look at another example, in which the variable is a whole number, but its coefficient is a fraction:\n",
182 | "\n",
183 | "\\begin{equation}\\frac{2}{5}x + 1 = 11 \\end{equation}\n",
184 | "\n",
185 | "As usual, we'll start by removing the constants from the variable expression; so in this case we need to subtract 1 from both sides:\n",
186 | "\n",
187 | "\\begin{equation}\\frac{2}{5}x = 10 \\end{equation}\n",
188 | "\n",
189 | "Now we need to cancel out the fraction. The expression equates to two-fifths times x, so the opposite operation is to divide by 2/5; but a simpler way to do this with a fraction is to multiply it by its *reciprocal*, which is just the inverse of the fraction, in this case 5/2. Of course, we need to do this to both sides:\n",
190 | "\n",
191 | "\\begin{equation}\\frac{5}{2} \\cdot \\frac{2}{5}x = \\frac{10}{1} \\cdot \\frac{5}{2} \\end{equation}\n",
192 | "\n",
193 | "That gives us the following result:\n",
194 | "\n",
195 | "\\begin{equation}x = \\frac{50}{2} \\end{equation}\n",
196 | "\n",
197 | "Which we can simplify to:\n",
198 | "\n",
199 | "\\begin{equation}x = 25 \\end{equation}\n",
200 | "\n",
201 | "We can confirm that with Python:"
202 | ]
203 | },
204 | {
205 | "cell_type": "code",
206 | "execution_count": null,
207 | "metadata": {
208 | "collapsed": true
209 | },
210 | "outputs": [],
211 | "source": [
212 | "x = 25\n",
213 | "2/5 * x + 1 ==11"
214 | ]
215 | },
216 | {
217 | "cell_type": "markdown",
218 | "metadata": {},
219 | "source": [
220 | "## Equations with Variables on Both Sides\n",
221 | "So far, all of our equations have had a variable term on only one side. However, variable terms can exist on both sides. \n",
222 | "\n",
223 | "Consider this equation:\n",
224 | "\n",
225 | "\\begin{equation}3x + 2 = 5x - 1 \\end{equation}\n",
226 | "\n",
227 | "This time, we have terms that include **x** on both sides. Let's take exactly the same approach to solving this kind of equation as we did for the previous examples. First, let's deal with the constants by adding 1 to both sides. That gets rid of the -1 on the right:\n",
228 | "\n",
229 | "\\begin{equation}3x + 3 = 5x \\end{equation}\n",
230 | "\n",
231 | "Now we can eliminate the variable expression from one side by subtracting 3x from both sides. That gets rid of the 3x on the left:\n",
232 | "\n",
233 | "\\begin{equation}3 = 2x \\end{equation}\n",
234 | "\n",
235 | "Next, we can deal with the coefficient by dividing both sides by 2:\n",
236 | "\n",
237 | "\\begin{equation}\\frac{3}{2} = x \\end{equation}\n",
238 | "\n",
239 | "Now we've isolated x. It looks a little strange because we usually have the variable on the left side, so if it makes you more comfortable you can simply reverse the equation:\n",
240 | "\n",
241 | "\\begin{equation}x = \\frac{3}{2} \\end{equation}\n",
242 | "\n",
243 | "Finally, this answer is correct as it is; but 3/2 is an improper fraction. We can simplify it to:\n",
244 | "\n",
245 | "\\begin{equation}x = 1\\frac{1}{2} \\end{equation}\n",
246 | "\n",
247 | "So x is 11/2 (which is of course 1.5 in decimal notation).\n",
248 | "Let's check it in Python:"
249 | ]
250 | },
251 | {
252 | "cell_type": "code",
253 | "execution_count": null,
254 | "metadata": {
255 | "collapsed": true
256 | },
257 | "outputs": [],
258 | "source": [
259 | "x = 1.5\n",
260 | "3*x + 2 == 5*x -1"
261 | ]
262 | },
263 | {
264 | "cell_type": "markdown",
265 | "metadata": {},
266 | "source": [
267 | "## Using the Distributive Property\n",
268 | "The distributive property is a mathematical law that enables us to distribute the same operation to terms within parenthesis. For example, consider the following equation:\n",
269 | "\n",
270 | "\\begin{equation}\\textbf{4(x + 2)} + \\textbf{3(x - 2)} = 16 \\end{equation}\n",
271 | "\n",
272 | "The equation includes two operations in parenthesis: **4(*x* + 2)** and **3(*x* - 2)**. Each of these operations consists of a constant by which the contents of the parenthesis must be multipled: for example, 4 times (*x* + 2). The distributive property means that we can achieve the same result by multiplying each term in the parenthesis and adding the results, so for the first parenthetical operation, we can multiply 4 by *x* and add it to 4 times +2; and for the second parenthetical operation, we can calculate 3 times *x* + 3 times -2). Note that the constants in the parenthesis include the sign (+ or -) that preceed them:\n",
273 | "\n",
274 | "\\begin{equation}4x + 8 + 3x - 6 = 16 \\end{equation}\n",
275 | "\n",
276 | "Now we can group our like terms:\n",
277 | "\n",
278 | "\\begin{equation}7x + 2 = 16 \\end{equation}\n",
279 | "\n",
280 | "Then we move the constant to the other side:\n",
281 | "\n",
282 | "\\begin{equation}7x = 14 \\end{equation}\n",
283 | "\n",
284 | "And now we can deal with the coefficient:\n",
285 | "\n",
286 | "\\begin{equation}\\frac{7x}{7} = \\frac{14}{7} \\end{equation}\n",
287 | "\n",
288 | "Which gives us our anwer:\n",
289 | "\n",
290 | "\\begin{equation}x = 2 \\end{equation}\n",
291 | "\n",
292 | "Here's the original equation with the calculated value for *x* in Python:"
293 | ]
294 | },
295 | {
296 | "cell_type": "code",
297 | "execution_count": null,
298 | "metadata": {
299 | "collapsed": true
300 | },
301 | "outputs": [],
302 | "source": [
303 | "x = 2\n",
304 | "4*(x + 2) + 3*(x - 2) == 16"
305 | ]
306 | }
307 | ],
308 | "metadata": {
309 | "kernelspec": {
310 | "display_name": "Python 3",
311 | "language": "python",
312 | "name": "python3"
313 | },
314 | "language_info": {
315 | "codemirror_mode": {
316 | "name": "ipython",
317 | "version": 3
318 | },
319 | "file_extension": ".py",
320 | "mimetype": "text/x-python",
321 | "name": "python",
322 | "nbconvert_exporter": "python",
323 | "pygments_lexer": "ipython3",
324 | "version": "3.6.4"
325 | }
326 | },
327 | "nbformat": 4,
328 | "nbformat_minor": 2
329 | }
330 |
--------------------------------------------------------------------------------
/Python/Module01/01-06-Factorization.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Factorization\n",
8 | "Factorization is the process of restating an expression as the *product* of two expressions (in other words, expressions multiplied together).\n",
9 | "\n",
10 | "For example, you can make the value **16** by performing the following multiplications of integer numbers:\n",
11 | "- 1 x 16\n",
12 | "- 2 x 8\n",
13 | "- 4 x 4\n",
14 | "\n",
15 | "Another way of saying this is that 1, 2, 4, 8, and 16 are all factors of 16.\n",
16 | "\n",
17 | "## Factors of Polynomial Expressions\n",
18 | "We can apply the same logic to polynomial expressions. For example, consider the following monomial expression:\n",
19 | "\n",
20 | "\\begin{equation}-6x^{2}y^{3} \\end{equation}\n",
21 | "\n",
22 | "You can get this value by performing the following multiplication:\n",
23 | "\n",
24 | "\\begin{equation}(2xy^{2})(-3xy) \\end{equation}\n",
25 | "\n",
26 | "Run the following Python code to test this with arbitrary ***x*** and ***y*** values:"
27 | ]
28 | },
29 | {
30 | "cell_type": "code",
31 | "execution_count": null,
32 | "metadata": {
33 | "collapsed": true
34 | },
35 | "outputs": [],
36 | "source": [
37 | "from random import randint\n",
38 | "x = randint(1,100)\n",
39 | "y = randint(1,100)\n",
40 | "\n",
41 | "(2*x*y**2)*(-3*x*y) == -6*x**2*y**3"
42 | ]
43 | },
44 | {
45 | "cell_type": "markdown",
46 | "metadata": {},
47 | "source": [
48 | "So, we can say that **2xy2** and **-3xy** are both factors of **-6x2y3**.\n",
49 | "\n",
50 | "This also applies to polynomials with more than one term. For example, consider the following expression:\n",
51 | "\n",
52 | "\\begin{equation}(x + 2)(2x^{2} - 3y + 2) = 2x^{3} + 4x^{2} - 3xy + 2x - 6y + 4 \\end{equation}\n",
53 | "\n",
54 | "Based on this, **x+2** and **2x2 - 3y + 2** are both factors of **2x3 + 4x2 - 3xy + 2x - 6y + 4**.\n",
55 | "\n",
56 | "(and if you don't believe me, you can try this with random values for x and y with the following Python code):"
57 | ]
58 | },
59 | {
60 | "cell_type": "code",
61 | "execution_count": null,
62 | "metadata": {
63 | "collapsed": true
64 | },
65 | "outputs": [],
66 | "source": [
67 | "from random import randint\n",
68 | "x = randint(1,100)\n",
69 | "y = randint(1,100)\n",
70 | "\n",
71 | "(x + 2)*(2*x**2 - 3*y + 2) == 2*x**3 + 4*x**2 - 3*x*y + 2*x - 6*y + 4"
72 | ]
73 | },
74 | {
75 | "cell_type": "markdown",
76 | "metadata": {},
77 | "source": [
78 | "## Greatest Common Factor\n",
79 | "Of course, these may not be the only factors of **-6x2y3**, just as 8 and 2 are not the only factors of 16.\n",
80 | "\n",
81 | "Additionally, 2 and 8 aren't just factors of 16; they're factors of other numbers too - for example, they're both factors of 24 (because 2 x 12 = 24 and 8 x 3 = 24). Which leads us to the question, what is the highest number that is a factor of both 16 and 24? Well, let's look at all the numbers that multiply evenly into 12 and all the numbers that multiply evenly into 24:\n",
82 | "\n",
83 | "| 16 | 24 |\n",
84 | "|--------|--------|\n",
85 | "| 1 x 16 | 1 x 24 |\n",
86 | "| 2 x **8** | 2 x 12 |\n",
87 | "| | 3 x **8** |\n",
88 | "| 4 x 4 | 4 x 6 |\n",
89 | "\n",
90 | "The highest value that is a multiple of both 16 and 24 is **8**, so 8 is the *Greatest Common Factor* (or GCF) of 16 and 24.\n",
91 | "\n",
92 | "OK, let's apply that logic to the following expressions:\n",
93 | "\n",
94 | "\\begin{equation}15x^{2}y\\;\\;\\;\\;\\;\\;\\;\\;9xy^{3}\\end{equation}\n",
95 | "\n",
96 | "So what's the greatest common factor of these two expressions?\n",
97 | "\n",
98 | "It helps to break the expressions into their consitituent components. Let's deal with the coefficients first; we have 15 and 9. The highest value that divides evenly into both of these is **3** (3 x 5 = 15 and 3 x 3 = 9).\n",
99 | "\n",
100 | "Now let's look at the ***x*** terms; we have x2 and x. The highest value that divides evenly into both is these is **x** (*x* goes into *x* once and into *x*2 *x* times).\n",
101 | "\n",
102 | "Finally, for our ***y*** terms, we have y and y3. The highest value that divides evenly into both is these is **y** (*y* goes into *y* once and into *y*3 *y•y* times).\n",
103 | "\n",
104 | "Putting all of that together, the GCF of both of our expression is:\n",
105 | "\n",
106 | "\\begin{equation}3xy\\end{equation}\n",
107 | "\n",
108 | "An easy shortcut to identifying the GCF of an expression that includes variables with exponentials is that it will always consist of:\n",
109 | "- The *largest* numeric factor of the numeric coefficients in the polynomial expressions (in this case 3)\n",
110 | "- The *smallest* exponential of each variable (in this case, x and y, which technically are x1 and y1.\n",
111 | "\n",
112 | "You can check your answer by dividing the original expressions by the GCF to find the coefficent expressions for the GCF (in other words, how many times the GCF divides into the original expression). The result, when multiplied by the GCF will always produce the original expression. So in this case, we need to perform the following divisions:\n",
113 | "\n",
114 | "\\begin{equation}\\frac{15x^{2}y}{3xy}\\;\\;\\;\\;\\;\\;\\;\\;\\frac{9xy^{3}}{3xy}\\end{equation}\n",
115 | "\n",
116 | "These fractions simplify to **5x** and **3y2**, giving us the following calculations to prove our factorization:\n",
117 | "\n",
118 | "\\begin{equation}3xy(5x) = 15x^{2}y\\end{equation}\n",
119 | "\\begin{equation}3xy(3y^{2}) = 9xy^{3}\\end{equation}\n",
120 | "\n",
121 | "Let's try both of those in Python:"
122 | ]
123 | },
124 | {
125 | "cell_type": "code",
126 | "execution_count": null,
127 | "metadata": {
128 | "collapsed": true
129 | },
130 | "outputs": [],
131 | "source": [
132 | "from random import randint\n",
133 | "x = randint(1,100)\n",
134 | "y = randint(1,100)\n",
135 | "\n",
136 | "print((3*x*y)*(5*x) == 15*x**2*y)\n",
137 | "print((3*x*y)*(3*y**2) == 9*x*y**3)"
138 | ]
139 | },
140 | {
141 | "cell_type": "markdown",
142 | "metadata": {},
143 | "source": [
144 | "## Distributing Factors\n",
145 | "Let's look at another example. Here is a binomial expression:\n",
146 | "\n",
147 | "\\begin{equation}6x + 15y \\end{equation}\n",
148 | "\n",
149 | "To factor this, we need to find an expression that divides equally into both of these expressions. In this case, we can use **3** to factor the coefficents, because 3 • 2x = 6x and 3• 5y = 15y, so we can write our original expression as:\n",
150 | "\n",
151 | "\\begin{equation}6x + 15y = 3(2x) + 3(5y) \\end{equation}\n",
152 | "\n",
153 | "Now, remember the distributive property? It enables us to multiply each term of an expression by the same factor to calculate the product of the expression multiplied by the factor. We can *factor-out* the common factor in this expression to distribute it like this:\n",
154 | "\n",
155 | "\\begin{equation}6x + 15y = 3(2x) + 3(5y) = \\mathbf{3(2x + 5y)} \\end{equation}\n",
156 | "\n",
157 | "Let's prove to ourselves that these all evaluate to the same thing:"
158 | ]
159 | },
160 | {
161 | "cell_type": "code",
162 | "execution_count": null,
163 | "metadata": {
164 | "collapsed": true
165 | },
166 | "outputs": [],
167 | "source": [
168 | "from random import randint\n",
169 | "x = randint(1,100)\n",
170 | "y = randint(1,100)\n",
171 | "\n",
172 | "(6*x + 15*y) == (3*(2*x) + 3*(5*y)) == (3*(2*x + 5*y))"
173 | ]
174 | },
175 | {
176 | "cell_type": "markdown",
177 | "metadata": {},
178 | "source": [
179 | "For something a little more complex, let's return to our previous example. Suppose we want to add our original 15x2y and 9xy3 expressions:\n",
180 | "\n",
181 | "\\begin{equation}15x^{2}y + 9xy^{3}\\end{equation}\n",
182 | "\n",
183 | "We've already calculated the common factor, so we know that:\n",
184 | "\n",
185 | "\\begin{equation}3xy(5x) = 15x^{2}y\\end{equation}\n",
186 | "\\begin{equation}3xy(3y^{2}) = 9xy^{3}\\end{equation}\n",
187 | "\n",
188 | "Now we can factor-out the common factor to produce a single expression:\n",
189 | "\n",
190 | "\\begin{equation}15x^{2}y + 9xy^{3} = \\mathbf{3xy(5x + 3y^{2})}\\end{equation}\n",
191 | "\n",
192 | "And here's the Python test code:"
193 | ]
194 | },
195 | {
196 | "cell_type": "code",
197 | "execution_count": null,
198 | "metadata": {
199 | "collapsed": true
200 | },
201 | "outputs": [],
202 | "source": [
203 | "from random import randint\n",
204 | "x = randint(1,100)\n",
205 | "y = randint(1,100)\n",
206 | "\n",
207 | "(15*x**2*y + 9*x*y**3) == (3*x*y*(5*x + 3*y**2))"
208 | ]
209 | },
210 | {
211 | "cell_type": "markdown",
212 | "metadata": {},
213 | "source": [
214 | "So you might be wondering what's so great about being able to distribute the common factor like this. The answer is that it can often be useful to apply a common factor to multiple terms in order to solve seemingly complex problems.\n",
215 | "\n",
216 | "For example, consider this:\n",
217 | "\n",
218 | "\\begin{equation}x^{2} + y^{2} + z^{2} = 127\\end{equation}\n",
219 | "\n",
220 | "Now solve this equation:\n",
221 | "\n",
222 | "\\begin{equation}a = 5x^{2} + 5y^{2} + 5z^{2}\\end{equation}\n",
223 | "\n",
224 | "At first glance, this seems tricky because there are three unknown variables, and even though we know that their squares add up to 127, we don't know their individual values. However, we can distribute the common factor and apply what we *do* know. Let's restate the problem like this:\n",
225 | "\n",
226 | "\\begin{equation}a = 5(x^{2} + y^{2} + z^{2})\\end{equation}\n",
227 | "\n",
228 | "Now it becomes easier to solve, because we know that the expression in parenthesis is equal to 127, so actually our equation is:\n",
229 | "\n",
230 | "\\begin{equation}a = 5(127)\\end{equation}\n",
231 | "\n",
232 | "So ***a*** is 5 times 127, which is 635"
233 | ]
234 | },
235 | {
236 | "cell_type": "markdown",
237 | "metadata": {},
238 | "source": [
239 | "## Formulae for Factoring Squares\n",
240 | "There are some useful ways that you can employ factoring to deal with expressions that contain squared values (that is, values with an exponential of 2).\n",
241 | "\n",
242 | "### Differences of Squares\n",
243 | "Consider the following expression:\n",
244 | "\n",
245 | "\\begin{equation}x^{2} - 9\\end{equation}\n",
246 | "\n",
247 | "The constant *9* is 32, so we could rewrite this as:\n",
248 | "\n",
249 | "\\begin{equation}x^{2} - 3^{2}\\end{equation}\n",
250 | "\n",
251 | "Whenever you need to subtract one squared term from another, you can use an approach called the *difference of squares*, whereby we can factor *a2 - b2* as *(a - b)(a + b)*; so we can rewrite the expression as:\n",
252 | "\n",
253 | "\\begin{equation}(x - 3)(x + 3)\\end{equation}\n",
254 | "\n",
255 | "Run the code below to check this:"
256 | ]
257 | },
258 | {
259 | "cell_type": "code",
260 | "execution_count": null,
261 | "metadata": {
262 | "collapsed": true
263 | },
264 | "outputs": [],
265 | "source": [
266 | "from random import randint\n",
267 | "x = randint(1,100)\n",
268 | "\n",
269 | "(x**2 - 9) == (x - 3)*(x + 3)"
270 | ]
271 | },
272 | {
273 | "cell_type": "markdown",
274 | "metadata": {},
275 | "source": [
276 | "### Perfect Squares\n",
277 | "A *perfect square* is a number multiplied by itself, for example 3 multipled by 3 is 9, so 9 is a perfect square.\n",
278 | "\n",
279 | "When working with equations, the ability to factor between polynomial expressions and binomial perfect square expressions can be a useful tool. For example, consider this expression:\n",
280 | "\n",
281 | "\\begin{equation}x^{2} + 10x + 25\\end{equation}\n",
282 | "\n",
283 | "We can use 5 as a common factor to rewrite this as:\n",
284 | "\n",
285 | "\\begin{equation}(x + 5)(x + 5)\\end{equation}\n",
286 | "\n",
287 | "So what happened here?\n",
288 | "\n",
289 | "Well, first we found a common factor for our coefficients: 5 goes into 10 twice and into 25 five times (in other words, squared). Then we just expressed this factoring as a multiplication of two identical binomials *(x + 5)(x + 5)*.\n",
290 | "\n",
291 | "Remember the rule for multiplication of polynomials is to multiple each term in the first polynomial by each term in the second polynomial and then add the results; so you can do this to verify the factorization:\n",
292 | "\n",
293 | "- x • x = x2\n",
294 | "- x • 5 = 5x\n",
295 | "- 5 • x = 5x\n",
296 | "- 5 • 5 = 25\n",
297 | "\n",
298 | "When you combine the two 5x terms we get back to our original expression of x2 + 10x + 25.\n",
299 | "\n",
300 | "Now we have an expression multipled by itself; in other words, a perfect square. We can therefore rewrite this as:\n",
301 | "\n",
302 | "\\begin{equation}(x + 5)^{2}\\end{equation}\n",
303 | "\n",
304 | "Factorization of perfect squares is a useful technique, as you'll see when we start to tackle quadratic equations in the next section. In fact, it's so useful that it's worth memorizing its formula:\n",
305 | "\n",
306 | "\\begin{equation}(a + b)^{2} = a^{2} + b^{2}+ 2ab \\end{equation}\n",
307 | "\n",
308 | "In our example, the *a* terms is ***x*** and the *b* terms is ***5***, and in standard form, our equation *x2 + 10x + 25* is actually *a2 + 2ab + b2*. The operations are all additions, so the order isn't actually important!\n",
309 | "\n",
310 | "Run the following code with random values for *a* and *b* to verify that the formula works:"
311 | ]
312 | },
313 | {
314 | "cell_type": "code",
315 | "execution_count": null,
316 | "metadata": {},
317 | "outputs": [],
318 | "source": [
319 | "from random import randint\n",
320 | "a = randint(1,100)\n",
321 | "b = randint(1,100)\n",
322 | "\n",
323 | "a**2 + b**2 + (2*a*b) == (a + b)**2"
324 | ]
325 | }
326 | ],
327 | "metadata": {
328 | "kernelspec": {
329 | "display_name": "Python 3",
330 | "language": "python",
331 | "name": "python3"
332 | },
333 | "language_info": {
334 | "codemirror_mode": {
335 | "name": "ipython",
336 | "version": 3
337 | },
338 | "file_extension": ".py",
339 | "mimetype": "text/x-python",
340 | "name": "python",
341 | "nbconvert_exporter": "python",
342 | "pygments_lexer": "ipython3",
343 | "version": "3.6.4"
344 | }
345 | },
346 | "nbformat": 4,
347 | "nbformat_minor": 2
348 | }
349 |
--------------------------------------------------------------------------------