+{"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"name":"NumpyAndLaTeX.ipynb","provenance":[],"collapsed_sections":[]},"kernelspec":{"name":"python3","display_name":"Python 3"}},"cells":[{"cell_type":"code","metadata":{"id":"eAqYe_o7Eaqh"},"source":["\"\"\"\n","Introduction to Numpy and LaTeX\n","(Created for UBC Engineering Physics 259)\n","\n","@authors: Alexandra Tully, Tim Child\n","@date: 30 July 2020\n","\"\"\";"],"execution_count":null,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"l_aeQLcDOqm9"},"source":["## Numpy Arrays\n","\n","There are several great libraries in Python for working with data, most notably Numpy and Pandas. Here we will look at the most popular and a common starting place, Numpy. (Pandas builds on top of Numpy and they work together very well).\n","\n","The Numpy package provides many of the same functions as found in Matlab, plus many many more. \n","\n","To use Numpy, the numpy package must first be imported into the script using an import statement. It is common to import numpy like this: `import numpy as np`. The as `np` part just makes it quicker to type when using later. As with all import statements, they need only be run once, but it doesn't hurt to run them multiple times.\n","\n","e.g.\n","```\n","import numpy as np\n","xdata = np.array([1,2,3,4,5,6,7,8])\n","```\n","To create a numpy array from scratch. Or\n","```\n","xdata = np.linspace(0, 100, 1000) # Creates a 1D array with 1000 values from 0 -> 100\n","ydata = np.sin(xdata) # Creates a 1D array with sin(x) evaluated at each xdata value\n","```\n","\n","The Numpy package has extremely good documentation which can be found [here](https://numpy.org/doc/stable/user/absolute_beginners.html). \n","\n","Additionally, any question you might have that doesn't have an obvious answer in the documentation has 99.9% of the time, been asked on StackOverflow (https://stackoverflow.com/) already, and the answers are usually extremely helpful. (Seriously, the effort people have put into answering questions about all things Python is incredible). "]},{"cell_type":"markdown","metadata":{"id":"4mPEszZWOxsl"},"source":["### Viewing data in arrays\n","\n","It's often useful to get a quick idea of what values are in an array, even before plotting. A couple of ways are:\n","\n","1. Leave the array as the last line of a cell and run it\n"]},{"cell_type":"code","metadata":{"id":"zClDtECxVJws","colab":{"base_uri":"https://localhost:8080/","height":34},"outputId":"4e08e71b-8617-4507-df3e-657b1c3da20a"},"source":["import numpy as np\n","x = np.linspace(-3, 7, 11)\n","x"],"execution_count":null,"outputs":[{"output_type":"execute_result","data":{"text/plain":["array([-3., -2., -1., 0., 1., 2., 3., 4., 5., 6., 7.])"]},"metadata":{"tags":[]},"execution_count":7}]},{"cell_type":"markdown","metadata":{"id":"n4u0U4gyVhjb"},"source":["2. Print the array anywhere in the cell"]},{"cell_type":"code","metadata":{"id":"BZxvcrVzVltB","colab":{"base_uri":"https://localhost:8080/","height":51},"outputId":"ed47c940-f1d6-4061-be82-6c579477e727"},"source":["y = x**2 # Double star means 'to power of'\n","print(y)\n","y = y*3 \n","print(y)"],"execution_count":null,"outputs":[{"output_type":"stream","text":["[ 9. 4. 1. 0. 1. 4. 9. 16. 25. 36. 49.]\n","[ 27. 12. 3. 0. 3. 12. 27. 48. 75. 108. 147.]\n"],"name":"stdout"}]},{"cell_type":"markdown","metadata":{"id":"x_5QskQLVHqV"},"source":["### Retrieving Specific Values\n","\n","It's often useful to find values, or where values are in arrays of data. \n","\n","Here I'll go over just a few examples, if you still have questions, StackOverflow is your friend!\n"]},{"cell_type":"code","metadata":{"id":"PJdPVvYPazAU","colab":{"base_uri":"https://localhost:8080/","height":51},"outputId":"b62f8077-c7e2-44b5-ef6f-60706ffb973d"},"source":["# Making some sample data\n","x = np.linspace(-10, 10, 1000) \n","y = 2.1*x**2 -17.6*x + 14.3 \n","\n","# Now I want to find the minimum y value and store it in a variable called 'y_min'\n","y_min = np.min(y) \n","\n","# If I want to find the index of the array at which it is minimum, then I would argmin instead\n","y_min_index = np.argmin(y) \n","print(f'The index at y_min is = {y_min_index}') # remember f' ' makes a format string\n","\n","# Then I could use that index to find out what the x value was at the minimum y value\n","print(f'y has a minimum value of {y_min:.2f} at x = {x[y_min_index]:.2f}')\n"],"execution_count":null,"outputs":[{"output_type":"stream","text":["The index at y_min is = 709\n","y has a minimum value of -22.58 at x = 4.19\n"],"name":"stdout"}]},{"cell_type":"markdown","metadata":{"id":"iAg9EOTPO3Qy"},"source":["## LaTeX / Equations in Python\n","\n","LaTeX is a widely used formatting scripting language called LaTeX (or Tex) which is superior to render mathematical expressions and is used widely. It's definitely worth learning ASAP.\n","\n","To insert an expression wrap the LaTeX formatted equation in either `$` signs for inline equations, or `$$` for full line equations\n","\n","e.g. If you enter\n","\n","`$$\\alpha^2+\\beta^2=c^2$$`\n","\n","This is the result: \n","$$\\alpha^2+\\beta^2=c^2$$\n","\n","Single `$` signs allow you to put equations in the $\\alpha^2+\\beta^2=c^2$ middle of sentences.\n","\n","Go to https://www.codecogs.com/latex/eqneditor.php or https://www.latex4technics.com/ for help with writing LaTeX equations.\n","\n","Here is an example of a more complicated formula,\n","\n","`$$P_\\lambda = \\frac{2 \\pi h c^2}{\\lambda^5 \\left(e^{\\left(\\frac{h c}{\\lambda k T}\\right)} - 1\\right)}$$`\n","$$P_\\lambda = \\frac{2 \\pi h c^2}{\\lambda^5 \\left(e^{\\left(\\frac{h c}{\\lambda k T}\\right)} - 1\\right)}$$"]},{"cell_type":"code","metadata":{"id":"ivF5qLyKPdVC"},"source":[""],"execution_count":null,"outputs":[]}]}
0 commit comments