{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "*This notebook contains material for CBE 20258 Numerical and Statistical Analysis taught at the University of Notre Dame. (c) Professors Alexander Dowling, Ryan McClarren, and Yamil Colón. This collection of notebooks [cbe-xx258](https://ndcbe.github.io/cbe-xx258) is available [on Github](https://github.com/ndcbe/cbe-xx258).*\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "< [1.5 List, Dictionaries, and Enumeration](https://ndcbe.github.io/cbe-xx258/01.05-Lists-Dictionaries-Enumerate.html) | [Contents](toc.html) | [1.7 Visualization with matplotlib](https://ndcbe.github.io/cbe-xx258/01.07-Matplotlib.html) >
"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "MElKNIbuqnWJ",
"nbpages": {
"level": 1,
"link": "[1.6 Linear Algebra with Numpy and Scipy](https://ndcbe.github.io/cbe-xx258/01.06-NumPy.html#1.6-Linear-Algebra-with-Numpy-and-Scipy)",
"section": "1.6 Linear Algebra with Numpy and Scipy"
}
},
"source": [
"# 1.6 Linear Algebra with Numpy and Scipy\n",
"\n",
"**Reference**: Chapter 1 of *Computational Nuclear Engineering and Radiological Science Using Python*, R. McClarren (2018)"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "OS9TuD7EhSZQ",
"nbpages": {
"level": 2,
"link": "[1.6.1 Learning Objectives](https://ndcbe.github.io/cbe-xx258/01.06-NumPy.html#1.6.1-Learning-Objectives)",
"section": "1.6.1 Learning Objectives"
}
},
"source": [
"## 1.6.1 Learning Objectives\n",
"After studying this notebook, completing the activities, and asking questions in class, you should be able to:\n",
"* Create, manipulate, and use NumPy arrays\n",
" * Explain scoping rules for arrays in Python\n",
" * Perform element-wise and matrix operations with arrays\n",
" * Access elements of an array with indices and slices\n",
" * Iterate over elements of an array with for loops"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "qUr_ziKTqnWL",
"nbpages": {
"level": 2,
"link": "[1.6.2 NumPy Arrays](https://ndcbe.github.io/cbe-xx258/01.06-NumPy.html#1.6.2-NumPy-Arrays)",
"section": "1.6.2 NumPy Arrays"
}
},
"source": [
"## 1.6.2 NumPy Arrays"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "U7VGvVepqnWM",
"nbpages": {
"level": 2,
"link": "[1.6.2 NumPy Arrays](https://ndcbe.github.io/cbe-xx258/01.06-NumPy.html#1.6.2-NumPy-Arrays)",
"section": "1.6.2 NumPy Arrays"
}
},
"source": [
"So far, we have discussed lists, which are containers that hold any type of Python object such as strings, floats, integers, other lists, etc.\n",
"\n",
"As engineers, we often want to store floating point numbers in vectors and matrices to perform linear algebra calculations. In this class, we will use **NumPy**. For a less engineering specific tutorial on NumPy, see https://docs.scipy.org/doc/numpy-1.15.0/user/quickstart.html\n",
"\n",
"The basic unit in numpy is a multi-dimensional array:\n",
"* A one-dimensional (1-D) array is a **vector**\n",
"* A 2-D array is a **matrix**\n",
"* A 3-D array is a vector of matrices\n",
"* etc"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 3,
"link": "[1.6.2.1 Getting Started](https://ndcbe.github.io/cbe-xx258/01.06-NumPy.html#1.6.2.1-Getting-Started)",
"section": "1.6.2.1 Getting Started"
}
},
"source": [
"### 1.6.2.1 Getting Started"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 3,
"link": "[1.6.2.1 Getting Started](https://ndcbe.github.io/cbe-xx258/01.06-NumPy.html#1.6.2.1-Getting-Started)",
"section": "1.6.2.1 Getting Started"
}
},
"source": [
"We'll start by loading the numpy module (a.k.a. library)."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": true,
"nbpages": {
"level": 3,
"link": "[1.6.2.1 Getting Started](https://ndcbe.github.io/cbe-xx258/01.06-NumPy.html#1.6.2.1-Getting-Started)",
"section": "1.6.2.1 Getting Started"
}
},
"outputs": [],
"source": [
"# Import the numpy module (a.k.a library) and give it the \"nickname\" np\n",
"# Warning: Do not modify this line. The autograde assumes numpy is loades as 'np'\n",
"import numpy as np"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 3,
"link": "[1.6.2.1 Getting Started](https://ndcbe.github.io/cbe-xx258/01.06-NumPy.html#1.6.2.1-Getting-Started)",
"section": "1.6.2.1 Getting Started"
}
},
"source": [
"We can now write `np.` to call functions in the numpy library (a.k.a. module). You'll see we place a few standard import statements at the top of most notebooks in this class."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 104
},
"colab_type": "code",
"executionInfo": {
"elapsed": 222,
"status": "ok",
"timestamp": 1548343368881,
"user": {
"displayName": "Alexander Dowling",
"photoUrl": "https://lh3.googleusercontent.com/-LChdQ2m5OQE/AAAAAAAAAAI/AAAAAAAAAA0/JeXJe4vQJ7M/s64/photo.jpg",
"userId": "00988067626794866502"
},
"user_tz": 300
},
"id": "g9x9LjJ9qnWN",
"nbpages": {
"level": 3,
"link": "[1.6.2.1 Getting Started](https://ndcbe.github.io/cbe-xx258/01.06-NumPy.html#1.6.2.1-Getting-Started)",
"section": "1.6.2.1 Getting Started"
},
"outputId": "abef1bed-e788-44cf-c253-2e7c1dbd5be4"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The vector [1 2 3 4]\n",
"The matrix\n",
" [[1 2 3]\n",
" [4 5 6]\n",
" [7 8 9]]\n"
]
}
],
"source": [
"# Create a vector\n",
"a_vector = np.array([1,2,3,4])\n",
"\n",
"# Create a matrix\n",
"a_matrix = np.array([(1,2,3),(4,5,6),(7,8,9)])\n",
"\n",
"# Print to the screen\n",
"print(\"The vector\",a_vector)\n",
"print(\"The matrix\\n\",a_matrix)"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "o0GjLpF5qnWR",
"nbpages": {
"level": 3,
"link": "[1.6.2.1 Getting Started](https://ndcbe.github.io/cbe-xx258/01.06-NumPy.html#1.6.2.1-Getting-Started)",
"section": "1.6.2.1 Getting Started"
}
},
"source": [
"Arrays have several **attributes** that you can use to find out information regarding the array you're working with."
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 52
},
"colab_type": "code",
"executionInfo": {
"elapsed": 349,
"status": "ok",
"timestamp": 1548343428032,
"user": {
"displayName": "Alexander Dowling",
"photoUrl": "https://lh3.googleusercontent.com/-LChdQ2m5OQE/AAAAAAAAAAI/AAAAAAAAAA0/JeXJe4vQJ7M/s64/photo.jpg",
"userId": "00988067626794866502"
},
"user_tz": 300
},
"id": "qsg8MLrXqnWS",
"nbpages": {
"level": 3,
"link": "[1.6.2.1 Getting Started](https://ndcbe.github.io/cbe-xx258/01.06-NumPy.html#1.6.2.1-Getting-Started)",
"section": "1.6.2.1 Getting Started"
},
"outputId": "8f9f7c51-3907-4e7b-a0a7-7d34db98d936"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The shape of a_vector is (4,)\n",
"The shape of a_matrix is (3, 3)\n"
]
}
],
"source": [
"#shape tells you the shape\n",
"print(\"The shape of a_vector is \", a_vector.shape)\n",
"print(\"The shape of a_matrix is \", a_matrix.shape)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 52
},
"colab_type": "code",
"executionInfo": {
"elapsed": 235,
"status": "ok",
"timestamp": 1548343447541,
"user": {
"displayName": "Alexander Dowling",
"photoUrl": "https://lh3.googleusercontent.com/-LChdQ2m5OQE/AAAAAAAAAAI/AAAAAAAAAA0/JeXJe4vQJ7M/s64/photo.jpg",
"userId": "00988067626794866502"
},
"user_tz": 300
},
"id": "FJPmLTRgqnWU",
"nbpages": {
"level": 3,
"link": "[1.6.2.1 Getting Started](https://ndcbe.github.io/cbe-xx258/01.06-NumPy.html#1.6.2.1-Getting-Started)",
"section": "1.6.2.1 Getting Started"
},
"outputId": "c6e859e6-4fc4-4c9d-de50-ff83e97835b0"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The dimension of a_vector is 1\n",
"The dimension of a_matrix is 2\n"
]
}
],
"source": [
"#ndim tells you the dimensionality of an array\n",
"print(\"The dimension of a_vector is \", a_vector.ndim)\n",
"print(\"The dimension of a_matrix is \", a_matrix.ndim)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 52
},
"colab_type": "code",
"executionInfo": {
"elapsed": 349,
"status": "ok",
"timestamp": 1548343461935,
"user": {
"displayName": "Alexander Dowling",
"photoUrl": "https://lh3.googleusercontent.com/-LChdQ2m5OQE/AAAAAAAAAAI/AAAAAAAAAA0/JeXJe4vQJ7M/s64/photo.jpg",
"userId": "00988067626794866502"
},
"user_tz": 300
},
"id": "rOL818DSqnWX",
"nbpages": {
"level": 3,
"link": "[1.6.2.1 Getting Started](https://ndcbe.github.io/cbe-xx258/01.06-NumPy.html#1.6.2.1-Getting-Started)",
"section": "1.6.2.1 Getting Started"
},
"outputId": "5bec0c0d-e27a-4ba8-a20c-0ce9146bf8fc"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The size of a_vector is 4 = 4\n",
"The size of a_matrix is 9 = 3 * 3\n"
]
}
],
"source": [
"#size is the total number of elements = \n",
"#the product of the number of elements in each dimension\n",
"print(\"The size of a_vector is \", a_vector.size,\"= \",\n",
" a_vector.shape[0])\n",
"print(\"The size of a_matrix is \", a_matrix.size,\"=\",\n",
" a_matrix.shape[0],\"*\",a_matrix.shape[1])"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 3,
"link": "[1.6.2.1 Getting Started](https://ndcbe.github.io/cbe-xx258/01.06-NumPy.html#1.6.2.1-Getting-Started)",
"section": "1.6.2.1 Getting Started"
}
},
"source": [
"Notice that `a_matrix.shape` is a list. We can access the number of rows with `a_matrix.shape[0]` and the number of columns with `a_matrix.shape[1]`."
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 3,
"link": "[1.6.2.1 Getting Started](https://ndcbe.github.io/cbe-xx258/01.06-NumPy.html#1.6.2.1-Getting-Started)",
"section": "1.6.2.1 Getting Started"
}
},
"source": [
"
"
]
}
],
"metadata": {
"colab": {
"collapsed_sections": [
"xCRFBLk3qnWw",
"0b7S8aRkqnXL",
"hVTWyRgyqnXT",
"9CTmfuN6qnXe",
"kHsSx8OcqnXt",
"hIS0pUx9qnX3",
"Ck5L2vnuqnX-"
],
"name": "L4-NumPy-Matplotlib.ipynb",
"provenance": [],
"version": "0.3.2"
},
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 1
}