summaryrefslogtreecommitdiff
path: root/tutorials/module_1/arrays.ipynb
diff options
context:
space:
mode:
Diffstat (limited to 'tutorials/module_1/arrays.ipynb')
-rw-r--r--tutorials/module_1/arrays.ipynb252
1 files changed, 194 insertions, 58 deletions
diff --git a/tutorials/module_1/arrays.ipynb b/tutorials/module_1/arrays.ipynb
index 7fe777d..f0ad435 100644
--- a/tutorials/module_1/arrays.ipynb
+++ b/tutorials/module_1/arrays.ipynb
@@ -2,10 +2,10 @@
"cells": [
{
"cell_type": "markdown",
- "id": "22b60740-09ee-4205-ab11-206bbef80709",
+ "id": "7caa19c6-3283-43a2-8730-711610242dc8",
"metadata": {},
"source": [
- "# matrixArrays\n",
+ "# Arrays\n",
"\n",
"In computer programming, an array is a structure for storing and retrieving data. We often talk about an array as if it were a grid in space, with each cell storing one element of the data. For instance, if each element of the data were a number, we might visualize a “one-dimensional” array like a list:\n",
"\n",
@@ -24,26 +24,48 @@
"- From [Numpy documentation](https://numpy.org/doc/2.2/user/absolute_beginners.html)\n",
"![Mathworks 3-D array](../figures/multi-dimensional-array.gif)\n",
"\n",
- "If the load on this block changes over time, then we may want to add a 4th dimension i.e. additional sets of 3-D arrays for each time increment. As you can see - the more dimensions we add, the more complicated of a problem we have to solve. It is possible to increase the number of dimensions to the n-th order. This course we will not be going beyond dimensional analysis.\n",
- "\n",
- "---\n",
- "\n",
+ "If the load on this block changes over time, then we may want to add a 4th dimension i.e. additional sets of 3-D arrays for each time increment. As you can see - the more dimensions we add, the more complicated of a problem we have to solve. It is possible to increase the number of dimensions to the n-th order. This course we will not be going beyond dimensional analysis."
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "08cef772-93f4-4cd9-8d0e-746a2a9be4de",
+ "metadata": {},
+ "source": [
"# Numpy - the python's array library\n",
"\n",
"In this tutorial we will be introducing arrays and we will be using the numpy library. Arrays, lists, vectors, matrices, sets - You might've heard of them before, they all store data. In programming, an array is a variable that can hold more than one value at a time. We will be using the Numpy python library to create arrays. Since we already have installed Numpy previously, we can start using the package.\n",
"\n",
- "Before importing our first package, let's as ourselves *what is a package?* A package can be thought of as pre-written python code that we can re-use. This means the for every script that we write in python we need to tell it to use a certain package. We call this importing a package.\n",
+ "Before importing our first package, let's as ourselves *what is a package?* A package can be thought of as pre-written python code that we can re-use. This means the for every script that we write in python we need to tell it to use a certain package. We call this *importing* a package.\n",
"\n",
"## Importing Numpy\n",
- "When using packages in python, we need to let it know what package we will be using. This is called importing. To import numpy we need to declare it a the start of a script as follows:\n",
- "```python\n",
- "import numpy as np\n",
- "```\n",
+ "When using packages in python, we need to let it know what package we will be using. This is called importing. To import numpy we need to declare it a the start of a script as follows:"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 12,
+ "id": "8badcbc9-5cde-46d6-9508-7e6209761d0b",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "import numpy as np"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "dfd1a02a-01f6-4ccb-b5bc-23a82dfe40ce",
+ "metadata": {},
+ "source": [
"- `import` - calls for a library to use, in our case it is Numpy.\n",
- "- `as` - gives the library an alias in your script. It's common convention in Python programming to make the code shorter and more readable. We will be using *np* as it's a standard using in many projects.\n",
- "\n",
- "---\n",
- "\n",
+ "- `as` - gives the library an alias in your script. It's common convention in Python programming to make the code shorter and more readable. We will be using *np* as it's a standard using in many projects."
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "51c5a7f9-291f-4415-a986-9102238859e3",
+ "metadata": {},
+ "source": [
"# Creating arrays\n",
"Now that we have imported the library we can create a one dimensional array or *vector* with three elements.\n",
"```python\n",
@@ -51,16 +73,27 @@
"```\n",
"\n",
"\n",
- "To create a *matrix* we can nest the arrays to create a two dimensional array. This is done as follows.\n",
- "\n",
- "```python\n",
+ "To create a *matrix* we can nest the arrays to create a two dimensional array. This is done as follows."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 13,
+ "id": "44b9d41f-0acf-4820-9f47-371f14e4f6b8",
+ "metadata": {},
+ "outputs": [],
+ "source": [
"matrix = np.array([[1,2,3],\n",
"\t\t\t\t [4,5,6],\n",
- "\t\t\t\t [7,8,9]])\n",
- "```\n",
- "\n",
- "*Note: for every array we nest, we get a new dimension in our data structure.*\n",
- "\n"
+ "\t\t\t\t [7,8,9]])"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "bbdea9cb-af6c-49d0-b530-da297ec18163",
+ "metadata": {},
+ "source": [
+ "*Note: for every array we nest, we get a new dimension in our data structure.*"
]
},
{
@@ -86,7 +119,7 @@
},
{
"cell_type": "code",
- "execution_count": 1,
+ "execution_count": 5,
"id": "7f2bcfe9-6e3d-424d-a08f-520682b3bdb5",
"metadata": {},
"outputs": [
@@ -111,44 +144,125 @@
},
{
"cell_type": "markdown",
- "id": "715d8fcd-8c8c-4f72-a8bd-7e369eb916a8",
+ "id": "9dbcd4e9-506d-4119-88f6-3fe83fd6356e",
"metadata": {},
"source": [
"## Numpy array creation functions\n",
"Numpy comes with some built-in function that we can use to create arrays quickly. Here are a couple of functions that are commonly used in python.\n",
"\n",
"### np.arange\n",
- "The `np.arange()` function returns an array with evenly spaced values within a specified range. It is similar to the built-in `range()` function in Python but returns a Numpy array instead of a list. The parameters for this function are the start value (inclusive), the stop value (exclusive), and the step size. If the step size is not provided, it defaults to 1.\n",
- "\n",
- "```python\n",
- ">>> np.arange(4)\n",
- "array([0. , 1., 2., 3. ])\n",
- "```\n",
- "\n",
+ "The `np.arange()` function returns an array with evenly spaced values within a specified range. It is similar to the built-in `range()` function in Python but returns a Numpy array instead of a list. The parameters for this function are the start value (inclusive), the stop value (exclusive), and the step size. If the step size is not provided, it defaults to 1."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 7,
+ "id": "dabff758-4003-4f64-bd9e-1a6c8d1f6b70",
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "array([0, 1, 2, 3])"
+ ]
+ },
+ "execution_count": 7,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "np.arange(4)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "3a51ab81-1d38-425d-9f0e-e01b07632240",
+ "metadata": {},
+ "source": [
"In this example, `np.arange(4)` generates an array starting from 0 and ending before 4, with a step size of 1.\n",
"\n",
"### np.linspace\n",
- "The `np.linspace()` function returns an array of evenly spaced values over a specified range. Unlike `np.arange()`, which uses a step size to define the spacing between elements, `np.linspace()` uses the number of values you want to generate and calculates the spacing automatically. It accepts three parameters: the start value, the stop value, and the number of samples.\n",
- "\n",
- "```python\n",
- ">>> np.linspace(1., 4., 6)\n",
- "array([1. , 1.6, 2.2, 2.8, 3.4, 4. ])\n",
- "```\n",
- "\n",
+ "The `np.linspace()` function returns an array of evenly spaced values over a specified range. Unlike `np.arange()`, which uses a step size to define the spacing between elements, `np.linspace()` uses the number of values you want to generate and calculates the spacing automatically. It accepts three parameters: the start value, the stop value, and the number of samples."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 6,
+ "id": "6ade3b6b-ea5c-4894-aff5-6e24ae43b9fe",
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "array([1. , 1.6, 2.2, 2.8, 3.4, 4. ])"
+ ]
+ },
+ "execution_count": 6,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "np.linspace(1., 4., 6)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "8ce82fb1-d843-4203-acc2-0ff3327ebd34",
+ "metadata": {},
+ "source": [
"In this example, `np.linspace(1., 4., 6)` generates 6 evenly spaced values between 1. and 4., including both endpoints.\n",
"\n",
- "Try this and see what happens:\n",
- "\n",
- "```python\n",
- "x = np.linspace(0,100,101)\n",
+ "Try this and see what happens:"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 5,
+ "id": "73b23a4a-3482-4b50-905c-92d0fe06acd9",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "[ 0. 0.06235951 0.12447629 0.18610855 0.24701637 0.30696269\n",
+ " 0.36571416 0.42304208 0.47872332 0.53254113 0.58428602 0.63375658\n",
+ " 0.68076024 0.72511405 0.76664535 0.80519249 0.84060542 0.8727463\n",
+ " 0.90149002 0.92672468 0.94835208 0.96628801 0.98046268 0.9908209\n",
+ " 0.99732234 0.99994172 0.99866883 0.99350862 0.98448118 0.97162165\n",
+ " 0.95498009 0.93462127 0.91062444 0.88308301 0.85210418 0.81780853\n",
+ " 0.78032958 0.73981319 0.69641708 0.65031016 0.60167192 0.55069167\n",
+ " 0.49756786 0.44250727 0.38572421 0.32743973 0.26788069 0.20727893\n",
+ " 0.14587033 0.08389393 0.02159098 -0.04079602 -0.10302422 -0.1648514\n",
+ " -0.22603689 -0.28634254 -0.3455336 -0.40337967 -0.45965558 -0.51414229\n",
+ " -0.5666277 -0.61690752 -0.66478602 -0.71007685 -0.75260371 -0.79220105\n",
+ " -0.82871476 -0.8620027 -0.89193529 -0.91839603 -0.94128192 -0.96050387\n",
+ " -0.97598706 -0.98767123 -0.99551089 -0.99947552 -0.9995497 -0.99573314\n",
+ " -0.98804069 -0.97650229 -0.96116286 -0.94208211 -0.91933431 -0.893008\n",
+ " -0.86320566 -0.83004329 -0.79364999 -0.7541674 -0.71174922 -0.66656056\n",
+ " -0.61877731 -0.56858548 -0.51618043 -0.46176614 -0.40555444 -0.34776411\n",
+ " -0.28862012 -0.22835267 -0.16719636 -0.10538923 -0.04317189]\n"
+ ]
+ }
+ ],
+ "source": [
+ "x = np.linspace(0,6.24,101)\n",
"y = np.sin(x)\n",
- "```\n",
"\n",
+ "print(y)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "82fc9e4b-6aaf-4753-b988-4350eebbee0f",
+ "metadata": {},
+ "source": [
"### Other useful functions\n",
"- `np.zeros()`\n",
"- `np.ones()`\n",
- "- `np.eye()` \n",
- "\n"
+ "- `np.eye()` "
]
},
{
@@ -162,7 +276,7 @@
},
{
"cell_type": "code",
- "execution_count": 2,
+ "execution_count": 8,
"id": "0c5de840-6256-4de2-9845-1c80a12c062d",
"metadata": {},
"outputs": [
@@ -181,7 +295,7 @@
},
{
"cell_type": "markdown",
- "id": "ae9aae08-b05b-4385-aa13-20dc1f8b88f2",
+ "id": "81c71011-1e20-413f-be61-9ef07a8d022a",
"metadata": {},
"source": [
"## Working with Arrays\n",
@@ -192,14 +306,37 @@
"\n",
"Python uses **zero-based indexing**, meaning the first element is at position **0** rather than **1**. This approach is common in many programming languages. For example, in a list with five elements, the first element is at index `0`, followed by elements at indices `1`, `2`, `3`, and `4`.\n",
"\n",
- "Here's an example of data from a rocket test stand where thrust was recorded as a function of time.\n",
- "\n",
- "```python\n",
- "thrust_lbf = np.array(0.603355, 2.019083, 2.808092, 4.054973, 1.136618, 0.943668)\n",
- "\n",
- ">>> thrust_lbs[3]\n",
- "```\n",
+ "Here's an example of data from a rocket test stand where thrust was recorded as a function of time."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 4,
+ "id": "bc405b0a-9914-400c-9fcf-f81ba058bed9",
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "np.float64(4.054973)"
+ ]
+ },
+ "execution_count": 4,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "thrust_lbf = np.array([0.603355, 2.019083, 2.808092, 4.054973, 1.136618, 0.943668])\n",
"\n",
+ "thrust_lbf[3]"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "d621cef0-440b-453d-b3a5-7ad6f1eca0e1",
+ "metadata": {},
+ "source": [
"Due to the nature of zero-based indexing. If we want to call the value `4.054973` that will be the 3rd index. \n",
"\n",
"### Operations on arrays\n",
@@ -217,8 +354,7 @@
"#### Combining arrays\n",
"- Concatenation: `np.concatenate((arr1, arr2), axis=0)`\n",
"- Stacking: `np.vstack()`, `np.hstack()`\n",
- "- Splitting: `np.split()`\n",
- "\n"
+ "- Splitting: `np.split()`"
]
},
{
@@ -266,7 +402,7 @@
},
{
"cell_type": "code",
- "execution_count": 3,
+ "execution_count": 5,
"id": "fc0244f6-d7f2-499a-a929-7f080d981b3e",
"metadata": {},
"outputs": [