|
| 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": 4, |
| 40 | + "metadata": {}, |
| 41 | + "outputs": [ |
| 42 | + { |
| 43 | + "data": {}, |
| 44 | + "metadata": {}, |
| 45 | + "output_type": "display_data" |
| 46 | + }, |
| 47 | + { |
| 48 | + "data": { |
| 49 | + "image/png": "iVBORw0KGgoAAAANSUhEUgAAAaQAAAFoCAMAAAAvuH/IAAAANlBMVEUAAAAzMzNNTU1oaGh8\nfHyMjIyampqnp6eysrLHx8fQ0NDZ2dnh4eHp6enr6+vw8PD/AAD////YlKJyAAAACXBIWXMA\nABJ0AAASdAHeZh94AAAISUlEQVR4nO3d7VajyBpAYYbo2Np2K/d/s52YLyBgqKKqqG32++Os\n2LOnqPFZIB090nRO9dNsvQHn/ogEGJEAIxJgRAKMSIARCTAiASYJ0t/R3PzB9CTNtjhm3kwk\nQCYSIBMJkIkEyEQCZCIBMpEAmUiALAqpPUzvtUg1Io1eXj8uuvcSi1WRiQTIYpDa8WuRKkTq\nf0m6Iv23n8VrOFETeCa1t687z6Q8WQzSVUekIplIgCwGyctd4SwWaeLGQaRcWQzS5V2Gtvda\npHxZFNL8FN17icWqyEQCZCIBMpEAmUiATCRAJhIgEwmQiQTIRAJkIgEykQCZSIBMJEAmEiAT\nCZCJBMhEAmQiATKRAJlIgEwkQCYSIBMJkIkEyEQCZCIBssRITt7xTKo2EwmQiQTIRAJkIgEy\nkQCZSIBMJEAmEiATCZCJBMhEAmQiATKRAJlIgEwkQCYSIBMJkIkEyEQCZCIBMpEAmUiATCRA\nJhIgEwmQiQTIRAJkIgGyKKT+b9VvB79iv+jeSyxWRRaDNHg+RTv4R0X3XmKxKjKRAFkMUh9n\naCRSlmw10vVLks/0yz9hSD4/qWC2Fmn0QdG9l1isiiwSqZ35qOjeSyxWRRaH1A5fiVQh0uix\n2j4urkKk9nxL5zP9ymRRZ9L8FN17icWqyEQCZCIBMpEAmUiATKRCWbMfkTY95rKsOU7waiKV\nz+5DibR5dv9kEmnjbC9094In0pbZ8SQSqWKkpjle7kJXE6lYdr4F9+5uo2PezU43C90iI5G2\nyC63c90iI5HKZ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plot without title" |
| 52 | + ] |
| 53 | + }, |
| 54 | + "metadata": {}, |
| 55 | + "output_type": "display_data" |
| 56 | + } |
| 57 | + ], |
| 58 | + "source": [ |
| 59 | + "library(ggplot2)\n", |
| 60 | + "library(NISTunits) # Libary to work netween degrees and radians\n", |
| 61 | + "library(repr)\n", |
| 62 | + "options(repr.plot.width=3.5, repr.plot.height=3) # Set the initial plot area dimensions\n", |
| 63 | + "\n", |
| 64 | + "## data frame for origin and end of vector\n", |
| 65 | + "v = data.frame(x = c(0,2), y = c(0,1))\n", |
| 66 | + "\n", |
| 67 | + "## Element-wise multiply the vector by 2\n", |
| 68 | + "w = 2 * v\n", |
| 69 | + "\n", |
| 70 | + "## Plot the vector\n", |
| 71 | + "ggplot() + \n", |
| 72 | + " geom_line(data = v, aes(x,y), arrow = arrow(length=unit(0.30,\"cm\")), color = 'red', size = 1) + \n", |
| 73 | + " geom_line(data = w, aes(x,y), arrow = arrow(length=unit(0.30,\"cm\")), size = 0.5) +\n", |
| 74 | + " xlim(-5,5) + ylim(-5,5)" |
| 75 | + ] |
| 76 | + }, |
| 77 | + { |
| 78 | + "cell_type": "markdown", |
| 79 | + "metadata": {}, |
| 80 | + "source": [ |
| 81 | + "The same approach is taken for scalar division.\n", |
| 82 | + "\n", |
| 83 | + "Try it for yourself - use the cell below to calculate a new vector named **b** based on the following definition:\n", |
| 84 | + "\n", |
| 85 | + "\\begin{equation}\\vec{b} = \\frac{\\vec{v}}{2}\\end{equation}" |
| 86 | + ] |
| 87 | + }, |
| 88 | + { |
| 89 | + "cell_type": "code", |
| 90 | + "execution_count": 6, |
| 91 | + "metadata": {}, |
| 92 | + "outputs": [ |
| 93 | + { |
| 94 | + "name": "stdout", |
| 95 | + "output_type": "stream", |
| 96 | + "text": [ |
| 97 | + " x y\n", |
| 98 | + "2 1 0.5\n" |
| 99 | + ] |
| 100 | + }, |
| 101 | + { |
| 102 | + "data": {}, |
| 103 | + "metadata": {}, |
| 104 | + "output_type": "display_data" |
| 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IBMpEAmUiATCRAJhIg\nEwmQiQTIRAJkIgEykQCZSIAsCWl8iTh/eXv5LA0puOm1KkSKzFr4tubNUpD68LZIDSKNX5Iu\nSF4urvykXR3TI6lCloJ00RGpShaHFD7PiVQlSzmSfLqrnKUieQnTilkK0vlZz8vF1cmSkJan\n6t5rLNZEJhIgEwmQiQTIRAJkIgEykQBZZqRw8n4snnW1hre2vJpIgNVEAqwmEmA1kQCr+TtY\nASMSYEQCjEiAEQkwmZGWf148dbW5lTMs1czGhhu+abmRgpurdj9ZYeX3YbREUxsLVpjfm0gr\nV+Mh/ePnxVcuufb5KVyqlY0Nt3zTMiPN/7z4qiWvV05dpQxS3pek0cqXKXAkZf1eZCJvdmPD\nLXsrcApe5HtxfSd+mRY3FqxQFunbnxdPW2367zeElGtjwQrEp7t+eqsdpGwbG27ZW36kjCcO\nwblpOycO+TY23PJNK/CJw/mBs5z2nL6Elas1u7Fb9uZnd4ARCTAiAUYkwIgEGJEAIxJgRAKM\nSIARCTAiAUYkwIgEmEdC2nVvw/DWbe+9j+h5JKSPbjMM24MUbB4JaXjuXl+6p3vvIn4eCinD\n/6C7yzwW0kvXvdx7DwkjEmAeC6nfbHy6a3z2Jw6v3fO9dxE/j4T0dQq+6T7uvY/oeSSk45vZ\n3b33ET2PhIQdkQAjEmBEAoxIgBEJMCIBRiTAiASYP1RNorvl+QeLAAAAAElFTkSuQmCC", |
| 109 | + "text/plain": [ |
| 110 | + "plot without title" |
| 111 | + ] |
| 112 | + }, |
| 113 | + "metadata": {}, |
| 114 | + "output_type": "display_data" |
| 115 | + } |
| 116 | + ], |
| 117 | + "source": [ |
| 118 | + "b = v / 2\n", |
| 119 | + "print(b[2,])\n", |
| 120 | + "\n", |
| 121 | + "# Plot b\n", |
| 122 | + "## Plot the vector\n", |
| 123 | + "ggplot() + \n", |
| 124 | + " geom_line(data = v, aes(x,y), arrow = arrow(length=unit(0.30,\"cm\")), color = 'red') + \n", |
| 125 | + " geom_line(data = b, aes(x,y), arrow = arrow(length=unit(0.30,\"cm\")), size = 1) +\n", |
| 126 | + " xlim(-5,5) + ylim(-5,5)" |
| 127 | + ] |
| 128 | + }, |
| 129 | + { |
| 130 | + "cell_type": "markdown", |
| 131 | + "metadata": {}, |
| 132 | + "source": [ |
| 133 | + "## Dot Product Multiplication\n", |
| 134 | + "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", |
| 135 | + "\n", |
| 136 | + "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", |
| 137 | + "\n", |
| 138 | + "\\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", |
| 139 | + "\n", |
| 140 | + "So for our vectors **v** (2,1) and **s** (-3,2), our calculation looks like this:\n", |
| 141 | + "\n", |
| 142 | + "\\begin{equation} \\vec{v} \\cdot \\vec{s} = (2 \\cdot -3) + (1 \\cdot 2) = -6 + 2 = -4\\end{equation}\n", |
| 143 | + "\n", |
| 144 | + "So the dot product, or scalar product, of **v** • **s** is **-4**.\n", |
| 145 | + "\n", |
| 146 | + "*****\n", |
| 147 | + "**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", |
| 148 | + "****\n", |
| 149 | + "\n", |
| 150 | + "Execute the code in the cell below to see the result:" |
| 151 | + ] |
| 152 | + }, |
| 153 | + { |
| 154 | + "cell_type": "code", |
| 155 | + "execution_count": 19, |
| 156 | + "metadata": {}, |
| 157 | + "outputs": [ |
| 158 | + { |
| 159 | + "data": { |
| 160 | + "text/html": [ |
| 161 | + "-4" |
| 162 | + ], |
| 163 | + "text/latex": [ |
| 164 | + "-4" |
| 165 | + ], |
| 166 | + "text/markdown": [ |
| 167 | + "-4" |
| 168 | + ], |
| 169 | + "text/plain": [ |
| 170 | + "[1] -4" |
| 171 | + ] |
| 172 | + }, |
| 173 | + "metadata": {}, |
| 174 | + "output_type": "display_data" |
| 175 | + } |
| 176 | + ], |
| 177 | + "source": [ |
| 178 | + "## Another data frame s\n", |
| 179 | + "s = data.frame(x = c(0,-3), y = c(0,2))\n", |
| 180 | + "\n", |
| 181 | + "dot_v_s = sum(as.matrix(v) %*% as.matrix(t(s)))\n", |
| 182 | + "dot_v_s" |
| 183 | + ] |
| 184 | + }, |
| 185 | + { |
| 186 | + "cell_type": "markdown", |
| 187 | + "metadata": {}, |
| 188 | + "source": [ |
| 189 | + "### The Cosine Rule\n", |
| 190 | + "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", |
| 191 | + "\n", |
| 192 | + "$$ \\vec{v} \\cdot \\vec{s} = \\|\\vec{v} \\|\\|\\vec{s}\\| \\cos (\\theta) $$ \n", |
| 193 | + "\n", |
| 194 | + "Which we can rearrange as:\n", |
| 195 | + "\n", |
| 196 | + "$$ \\cos(\\theta) = \\frac{\\vec{v} \\cdot \\vec{s}}{\\|\\vec{v} \\|\\|\\vec{s}\\|} $$\n", |
| 197 | + "\n", |
| 198 | + "So for our vectors **v** (2,1) and **s** (-3,2), our calculation looks like this:\n", |
| 199 | + "\n", |
| 200 | + "$$ \\cos(\\theta) = \\frac{(2 \\cdot-3) + (-3 \\cdot 2)}{\\sqrt{2^{2} + 1^{2}} \\times \\sqrt{-3^{2} + 2^{2}}} $$\n", |
| 201 | + "\n", |
| 202 | + "So:\n", |
| 203 | + "\n", |
| 204 | + "$$\\cos(\\theta) = \\frac{-4}{8.0622577483}$$\n", |
| 205 | + "\n", |
| 206 | + "Which calculates to:\n", |
| 207 | + "\n", |
| 208 | + "$$\\cos(\\theta) = -0.496138938357 $$\n", |
| 209 | + "\n", |
| 210 | + "So:\n", |
| 211 | + "\n", |
| 212 | + "$$\\theta \\approx 119.74 $$\n", |
| 213 | + "\n", |
| 214 | + "Here's that calculation in R:" |
| 215 | + ] |
| 216 | + }, |
| 217 | + { |
| 218 | + "cell_type": "code", |
| 219 | + "execution_count": 26, |
| 220 | + "metadata": {}, |
| 221 | + "outputs": [ |
| 222 | + { |
| 223 | + "data": { |
| 224 | + "text/html": [ |
| 225 | + "119.744898585964" |
| 226 | + ], |
| 227 | + "text/latex": [ |
| 228 | + "119.744898585964" |
| 229 | + ], |
| 230 | + "text/markdown": [ |
| 231 | + "119.744898585964" |
| 232 | + ], |
| 233 | + "text/plain": [ |
| 234 | + "[1] 119.7449" |
| 235 | + ] |
| 236 | + }, |
| 237 | + "metadata": {}, |
| 238 | + "output_type": "display_data" |
| 239 | + } |
| 240 | + ], |
| 241 | + "source": [ |
| 242 | + "norm_v = norm(v, '2')\n", |
| 243 | + "norm_s = norm(s, '2')\n", |
| 244 | + "\n", |
| 245 | + "cos_v_s = dot_v_s/(norm_v*norm_s)\n", |
| 246 | + "NISTradianTOdeg(acos(cos_v_s))" |
| 247 | + ] |
| 248 | + }, |
| 249 | + { |
| 250 | + "cell_type": "markdown", |
| 251 | + "metadata": { |
| 252 | + "collapsed": true |
| 253 | + }, |
| 254 | + "source": [ |
| 255 | + "## Cross Product Multiplication\n", |
| 256 | + "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", |
| 257 | + "\n", |
| 258 | + "For example, let's suppose we have the following vectors:\n", |
| 259 | + "\n", |
| 260 | + "\\begin{equation}\\vec{p} = \\begin{bmatrix}2 \\\\ 3 \\\\ 1 \\end{bmatrix}\\;\\; \\vec{q} = \\begin{bmatrix}1 \\\\ 2 \\\\ -2 \\end{bmatrix}\\end{equation}\n", |
| 261 | + "\n", |
| 262 | + "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 (r<sub>1</sub>, r<sub>2</sub>, and r<sub>3</sub>). The values for these components are calculated like this:\n", |
| 263 | + "\n", |
| 264 | + "\\begin{equation}r_{1} = p_{2}q_{3} - p_{3}q_{2}\\end{equation}\n", |
| 265 | + "\\begin{equation}r_{2} = p_{3}q_{1} - p_{1}q_{3}\\end{equation}\n", |
| 266 | + "\\begin{equation}r_{3} = p_{1}q_{2} - p_{2}q_{1}\\end{equation}\n", |
| 267 | + "\n", |
| 268 | + "So in our case:\n", |
| 269 | + "\n", |
| 270 | + "\\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", |
| 271 | + "\n", |
| 272 | + "In R you can use the **tcrossprod** function to compute the outer cross product of two vector arrays:" |
| 273 | + ] |
| 274 | + }, |
| 275 | + { |
| 276 | + "cell_type": "code", |
| 277 | + "execution_count": 32, |
| 278 | + "metadata": {}, |
| 279 | + "outputs": [ |
| 280 | + { |
| 281 | + "data": { |
| 282 | + "text/html": [ |
| 283 | + "<table>\n", |
| 284 | + "<tbody>\n", |
| 285 | + "\t<tr><td>6</td></tr>\n", |
| 286 | + "</tbody>\n", |
| 287 | + "</table>\n" |
| 288 | + ], |
| 289 | + "text/latex": [ |
| 290 | + "\\begin{tabular}{l}\n", |
| 291 | + "\t 6\\\\\n", |
| 292 | + "\\end{tabular}\n" |
| 293 | + ], |
| 294 | + "text/markdown": [ |
| 295 | + "\n", |
| 296 | + "| 6 | \n", |
| 297 | + "\n", |
| 298 | + "\n" |
| 299 | + ], |
| 300 | + "text/plain": [ |
| 301 | + " [,1]\n", |
| 302 | + "[1,] 6 " |
| 303 | + ] |
| 304 | + }, |
| 305 | + "metadata": {}, |
| 306 | + "output_type": "display_data" |
| 307 | + }, |
| 308 | + { |
| 309 | + "data": { |
| 310 | + "text/html": [ |
| 311 | + "<table>\n", |
| 312 | + "<tbody>\n", |
| 313 | + "\t<tr><td>2 </td><td>4 </td><td>-4</td></tr>\n", |
| 314 | + "\t<tr><td>3 </td><td>6 </td><td>-6</td></tr>\n", |
| 315 | + "\t<tr><td>1 </td><td>2 </td><td>-2</td></tr>\n", |
| 316 | + "</tbody>\n", |
| 317 | + "</table>\n" |
| 318 | + ], |
| 319 | + "text/latex": [ |
| 320 | + "\\begin{tabular}{lll}\n", |
| 321 | + "\t 2 & 4 & -4\\\\\n", |
| 322 | + "\t 3 & 6 & -6\\\\\n", |
| 323 | + "\t 1 & 2 & -2\\\\\n", |
| 324 | + "\\end{tabular}\n" |
| 325 | + ], |
| 326 | + "text/markdown": [ |
| 327 | + "\n", |
| 328 | + "| 2 | 4 | -4 | \n", |
| 329 | + "| 3 | 6 | -6 | \n", |
| 330 | + "| 1 | 2 | -2 | \n", |
| 331 | + "\n", |
| 332 | + "\n" |
| 333 | + ], |
| 334 | + "text/plain": [ |
| 335 | + " [,1] [,2] [,3]\n", |
| 336 | + "[1,] 2 4 -4 \n", |
| 337 | + "[2,] 3 6 -6 \n", |
| 338 | + "[3,] 1 2 -2 " |
| 339 | + ] |
| 340 | + }, |
| 341 | + "metadata": {}, |
| 342 | + "output_type": "display_data" |
| 343 | + } |
| 344 | + ], |
| 345 | + "source": [ |
| 346 | + "## Define the two vectors\n", |
| 347 | + "p = c(2,3,1)\n", |
| 348 | + "q = c(1,2,-2)\n", |
| 349 | + "\n", |
| 350 | + "## Outer cross product of two vectors\n", |
| 351 | + "tcrossprod(p,q)" |
| 352 | + ] |
| 353 | + } |
| 354 | + ], |
| 355 | + "metadata": { |
| 356 | + "kernelspec": { |
| 357 | + "display_name": "R", |
| 358 | + "language": "R", |
| 359 | + "name": "ir" |
| 360 | + }, |
| 361 | + "language_info": { |
| 362 | + "codemirror_mode": "r", |
| 363 | + "file_extension": ".r", |
| 364 | + "mimetype": "text/x-r-source", |
| 365 | + "name": "R", |
| 366 | + "pygments_lexer": "r", |
| 367 | + "version": "3.5.0" |
| 368 | + } |
| 369 | + }, |
| 370 | + "nbformat": 4, |
| 371 | + "nbformat_minor": 2 |
| 372 | +} |
0 commit comments