|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "#### Linear Algebra\n", |
| 8 | + "Work in progress. Basic linear algebra to be included here." |
| 9 | + ] |
| 10 | + }, |
| 11 | + { |
| 12 | + "cell_type": "markdown", |
| 13 | + "metadata": {}, |
| 14 | + "source": [ |
| 15 | + "#### Simple Array Operations" |
| 16 | + ] |
| 17 | + }, |
| 18 | + { |
| 19 | + "cell_type": "code", |
| 20 | + "execution_count": 1, |
| 21 | + "metadata": {}, |
| 22 | + "outputs": [], |
| 23 | + "source": [ |
| 24 | + "import numpy as np" |
| 25 | + ] |
| 26 | + }, |
| 27 | + { |
| 28 | + "cell_type": "code", |
| 29 | + "execution_count": 2, |
| 30 | + "metadata": {}, |
| 31 | + "outputs": [ |
| 32 | + { |
| 33 | + "data": { |
| 34 | + "text/plain": [ |
| 35 | + "array([[1., 2.],\n", |
| 36 | + " [3., 4.]])" |
| 37 | + ] |
| 38 | + }, |
| 39 | + "execution_count": 2, |
| 40 | + "metadata": {}, |
| 41 | + "output_type": "execute_result" |
| 42 | + } |
| 43 | + ], |
| 44 | + "source": [ |
| 45 | + "a = np.array([[1.0, 2.0], [3.0, 4.0]])\n", |
| 46 | + "a" |
| 47 | + ] |
| 48 | + }, |
| 49 | + { |
| 50 | + "cell_type": "code", |
| 51 | + "execution_count": 3, |
| 52 | + "metadata": {}, |
| 53 | + "outputs": [ |
| 54 | + { |
| 55 | + "data": { |
| 56 | + "text/plain": [ |
| 57 | + "array([[1., 3.],\n", |
| 58 | + " [2., 4.]])" |
| 59 | + ] |
| 60 | + }, |
| 61 | + "execution_count": 3, |
| 62 | + "metadata": {}, |
| 63 | + "output_type": "execute_result" |
| 64 | + } |
| 65 | + ], |
| 66 | + "source": [ |
| 67 | + "a.transpose()" |
| 68 | + ] |
| 69 | + }, |
| 70 | + { |
| 71 | + "cell_type": "code", |
| 72 | + "execution_count": 4, |
| 73 | + "metadata": {}, |
| 74 | + "outputs": [ |
| 75 | + { |
| 76 | + "data": { |
| 77 | + "text/plain": [ |
| 78 | + "array([[1., 3.],\n", |
| 79 | + " [2., 4.]])" |
| 80 | + ] |
| 81 | + }, |
| 82 | + "execution_count": 4, |
| 83 | + "metadata": {}, |
| 84 | + "output_type": "execute_result" |
| 85 | + } |
| 86 | + ], |
| 87 | + "source": [ |
| 88 | + "a.T" |
| 89 | + ] |
| 90 | + }, |
| 91 | + { |
| 92 | + "cell_type": "code", |
| 93 | + "execution_count": 12, |
| 94 | + "metadata": {}, |
| 95 | + "outputs": [ |
| 96 | + { |
| 97 | + "data": { |
| 98 | + "text/plain": [ |
| 99 | + "array([[ 0.6, -0.7],\n", |
| 100 | + " [-0.2, 0.4]])" |
| 101 | + ] |
| 102 | + }, |
| 103 | + "execution_count": 12, |
| 104 | + "metadata": {}, |
| 105 | + "output_type": "execute_result" |
| 106 | + } |
| 107 | + ], |
| 108 | + "source": [ |
| 109 | + "np.linalg.inv(a)" |
| 110 | + ] |
| 111 | + }, |
| 112 | + { |
| 113 | + "cell_type": "code", |
| 114 | + "execution_count": 13, |
| 115 | + "metadata": {}, |
| 116 | + "outputs": [], |
| 117 | + "source": [ |
| 118 | + "from numpy.linalg import inv\n", |
| 119 | + "a = np.array([[4,7], [2, 6]])\n", |
| 120 | + "ainv = inv(a)" |
| 121 | + ] |
| 122 | + }, |
| 123 | + { |
| 124 | + "cell_type": "markdown", |
| 125 | + "metadata": {}, |
| 126 | + "source": [ |
| 127 | + "" |
| 128 | + ] |
| 129 | + }, |
| 130 | + { |
| 131 | + "cell_type": "code", |
| 132 | + "execution_count": 14, |
| 133 | + "metadata": {}, |
| 134 | + "outputs": [ |
| 135 | + { |
| 136 | + "data": { |
| 137 | + "text/plain": [ |
| 138 | + "array([[ 0.6, -0.7],\n", |
| 139 | + " [-0.2, 0.4]])" |
| 140 | + ] |
| 141 | + }, |
| 142 | + "execution_count": 14, |
| 143 | + "metadata": {}, |
| 144 | + "output_type": "execute_result" |
| 145 | + } |
| 146 | + ], |
| 147 | + "source": [ |
| 148 | + "ainv" |
| 149 | + ] |
| 150 | + }, |
| 151 | + { |
| 152 | + "cell_type": "code", |
| 153 | + "execution_count": 15, |
| 154 | + "metadata": {}, |
| 155 | + "outputs": [ |
| 156 | + { |
| 157 | + "data": { |
| 158 | + "text/plain": [ |
| 159 | + "array([[1., 0.],\n", |
| 160 | + " [0., 1.]])" |
| 161 | + ] |
| 162 | + }, |
| 163 | + "execution_count": 15, |
| 164 | + "metadata": {}, |
| 165 | + "output_type": "execute_result" |
| 166 | + } |
| 167 | + ], |
| 168 | + "source": [ |
| 169 | + "u = np.eye(2) # unit 2x2 matrix; \"eye\" represents \"I\"\n", |
| 170 | + "u" |
| 171 | + ] |
| 172 | + }, |
| 173 | + { |
| 174 | + "cell_type": "code", |
| 175 | + "execution_count": 16, |
| 176 | + "metadata": {}, |
| 177 | + "outputs": [ |
| 178 | + { |
| 179 | + "data": { |
| 180 | + "text/plain": [ |
| 181 | + "array([[-1., 0.],\n", |
| 182 | + " [ 0., -1.]])" |
| 183 | + ] |
| 184 | + }, |
| 185 | + "execution_count": 16, |
| 186 | + "metadata": {}, |
| 187 | + "output_type": "execute_result" |
| 188 | + } |
| 189 | + ], |
| 190 | + "source": [ |
| 191 | + "j = np.array([[0.0, -1.0], [1.0, 0.0]])\n", |
| 192 | + "j @ j # matrix product" |
| 193 | + ] |
| 194 | + }, |
| 195 | + { |
| 196 | + "cell_type": "code", |
| 197 | + "execution_count": 23, |
| 198 | + "metadata": {}, |
| 199 | + "outputs": [ |
| 200 | + { |
| 201 | + "name": "stdout", |
| 202 | + "output_type": "stream", |
| 203 | + "text": [ |
| 204 | + "[[3 1]\n", |
| 205 | + " [4 5]]\n" |
| 206 | + ] |
| 207 | + }, |
| 208 | + { |
| 209 | + "data": { |
| 210 | + "text/plain": [ |
| 211 | + "8" |
| 212 | + ] |
| 213 | + }, |
| 214 | + "execution_count": 23, |
| 215 | + "metadata": {}, |
| 216 | + "output_type": "execute_result" |
| 217 | + } |
| 218 | + ], |
| 219 | + "source": [ |
| 220 | + "a = np.array([[3,1],\n", |
| 221 | + " [4,5]])\n", |
| 222 | + "print(a)\n", |
| 223 | + "np.trace(a) # trace" |
| 224 | + ] |
| 225 | + }, |
| 226 | + { |
| 227 | + "cell_type": "code", |
| 228 | + "execution_count": 24, |
| 229 | + "metadata": {}, |
| 230 | + "outputs": [ |
| 231 | + { |
| 232 | + "data": { |
| 233 | + "text/plain": [ |
| 234 | + "array([[3, 1],\n", |
| 235 | + " [4, 5]])" |
| 236 | + ] |
| 237 | + }, |
| 238 | + "execution_count": 24, |
| 239 | + "metadata": {}, |
| 240 | + "output_type": "execute_result" |
| 241 | + } |
| 242 | + ], |
| 243 | + "source": [ |
| 244 | + "a" |
| 245 | + ] |
| 246 | + }, |
| 247 | + { |
| 248 | + "cell_type": "code", |
| 249 | + "execution_count": 25, |
| 250 | + "metadata": {}, |
| 251 | + "outputs": [ |
| 252 | + { |
| 253 | + "data": { |
| 254 | + "text/plain": [ |
| 255 | + "array([[1.63636364],\n", |
| 256 | + " [0.09090909]])" |
| 257 | + ] |
| 258 | + }, |
| 259 | + "execution_count": 25, |
| 260 | + "metadata": {}, |
| 261 | + "output_type": "execute_result" |
| 262 | + } |
| 263 | + ], |
| 264 | + "source": [ |
| 265 | + "y = np.array([[5.], [7.]])\n", |
| 266 | + "np.linalg.solve(a, y)" |
| 267 | + ] |
| 268 | + }, |
| 269 | + { |
| 270 | + "cell_type": "code", |
| 271 | + "execution_count": 26, |
| 272 | + "metadata": {}, |
| 273 | + "outputs": [ |
| 274 | + { |
| 275 | + "data": { |
| 276 | + "text/plain": [ |
| 277 | + "array([2., 3.])" |
| 278 | + ] |
| 279 | + }, |
| 280 | + "execution_count": 26, |
| 281 | + "metadata": {}, |
| 282 | + "output_type": "execute_result" |
| 283 | + } |
| 284 | + ], |
| 285 | + "source": [ |
| 286 | + "a = np.array([[3,1], [1,2]])\n", |
| 287 | + "b = np.array([9,8])\n", |
| 288 | + "x = np.linalg.solve(a, b)\n", |
| 289 | + "x" |
| 290 | + ] |
| 291 | + }, |
| 292 | + { |
| 293 | + "cell_type": "code", |
| 294 | + "execution_count": 30, |
| 295 | + "metadata": {}, |
| 296 | + "outputs": [ |
| 297 | + { |
| 298 | + "data": { |
| 299 | + "text/plain": [ |
| 300 | + "True" |
| 301 | + ] |
| 302 | + }, |
| 303 | + "execution_count": 30, |
| 304 | + "metadata": {}, |
| 305 | + "output_type": "execute_result" |
| 306 | + } |
| 307 | + ], |
| 308 | + "source": [ |
| 309 | + "np.allclose(np.dot(a, x), b)" |
| 310 | + ] |
| 311 | + }, |
| 312 | + { |
| 313 | + "cell_type": "markdown", |
| 314 | + "metadata": {}, |
| 315 | + "source": [ |
| 316 | + "https://en.wikipedia.org/wiki/Linear_equation\n", |
| 317 | + " \n", |
| 318 | + "# One Varaiable:\n", |
| 319 | + " aX+b= 0\n", |
| 320 | + " \n", |
| 321 | + " x = -b/a\n", |
| 322 | + "\n", |
| 323 | + "# Two Variables\n", |
| 324 | + " \n", |
| 325 | + " ax+bx+c=0\n", |
| 326 | + " \n", |
| 327 | + " x(a+b)+c=0\n", |
| 328 | + " 1/x = -((a+b)+c)\n" |
| 329 | + ] |
| 330 | + } |
| 331 | + ], |
| 332 | + "metadata": { |
| 333 | + "kernelspec": { |
| 334 | + "display_name": "Python 3", |
| 335 | + "language": "python", |
| 336 | + "name": "python3" |
| 337 | + }, |
| 338 | + "language_info": { |
| 339 | + "codemirror_mode": { |
| 340 | + "name": "ipython", |
| 341 | + "version": 3 |
| 342 | + }, |
| 343 | + "file_extension": ".py", |
| 344 | + "mimetype": "text/x-python", |
| 345 | + "name": "python", |
| 346 | + "nbconvert_exporter": "python", |
| 347 | + "pygments_lexer": "ipython3", |
| 348 | + "version": "3.6.3" |
| 349 | + } |
| 350 | + }, |
| 351 | + "nbformat": 4, |
| 352 | + "nbformat_minor": 2 |
| 353 | +} |
0 commit comments