EGR 103/Concept List Spring 2020
Appearance
Lecture 1 - Introduction
- Class web page: EGR 103L; assignments, contact info, readings, etc - see slides on Errata/Notes page
- Sakai page: Sakai 103L page; grades, surveys and tests, some assignment submissions
- Pundit page: EGR 103; reference lists
- CampusWire page: CampusWire 103L page; message board for questions - you need to be in the class and have the access code 6393 to subscribe.
Lecture 2 - Programs and Programming
- Seven steps of programming -
- Watch video on Developing an Algorithm
- Watch video on A Seven Step Approach to Solving Programming Problems
Lecture 3 - "Number" Types
- To play with Python:
- Install it on your machine or a public machine: Download
- Quick tour of Python
- Editing window, variable explorer, and console
- Run icon (F5)
- You are not expected to remember any of the specifics about how Python stores things or works with them yet!
- Python is a "typed" language - variables have types
- We will use eight types:
- Focus of the day: int, float, and array
- Basics today, focus a little later: string, list, tuple
- Focus later: dictionary, set
- int: integers; Python can store these perfectly
- float: floating point numbers - "numbers with decimal points" - Python sometimes has problems storing floating point items exactly
- array
- Requires numpy, usually with
import numpy as np - Organizational unit for storing rectangular arrays of numbers
- Requires numpy, usually with
- Math with "Number" types works the way you expect
- ** * / // % + -
- Slices allow us to extract information from a collection or change information in mutable collections
- a[0] is the element in a at the start
- a[3] is the element in a three away from the start
- a[-1] is the last element of a
- A string contains an immutable collection of characters
- Using + with strings concatenates strings
- Using * with strings makes a string with the original repeated
- A tuple contains an immutable collection of other types
- Using + with tuples concatenates tuples
- Using * with tuples makes a tuple with the original repeated
- A list contains an immutable collection of other types
- Using + with lists concatenates lists
- Using * with lists makes a list with the original repeated
Lecture 4 - More on Types
- Relational operators can compare "Number" Types and work the way you expect with True or False as an answer
- < <= == >= > !=
- With arrays, either same size or one is a single value; result will be an array of True and False the same size as the array
- More advanced slices:
- a[:] is all the elements in a because what is really happening is:
- a[start:until] where start is the first index and until is just *past* the last index;
- a[3:7] will return a[3] through a[6] in 4-element array
- a[start:until:increment] will skip indices by increment instead of 1
- To go backwards, a[start:until:-increment] will start at an index and then go backwards until getting at or just past until.
- For 2-D arrays, you can index items with either separate row and column indices or indices separated by commas:
- a[2][3] is the same as a[2, 3]
- Only works for arrays!
Lecture 5 - Printing and Decisions
- Creating formatted strings using {} and .format() (format strings, standard format specifiers) -- focus was on using e or f for type, minimumwidth.precision, and possibly a + in front to force printing + for positive numbers.
- Also - Format Specification Mini-Language
Lecture 6 - Decisions
- Rolling dice
# tpir.py from class:
def roll(num=1, sides=6):
print(num, sides)
dice = np.random.randint(1, sides+1, num)
print(dice)
return dice
- Checking rolls:
# tpir.py from class:
import numpy as np
def eval_hand(dice):
sorted_dice = dice*1
sorted_dice.sort()
if sorted_dice[0] == sorted_dice[-1]:
hand_type = 3
tie_value = [sorted_dice[0]]
elif (sorted_dice[0] == sorted_dice[1]-1 and
sorted_dice[0] == sorted_dice[2]-2):
hand_type = 2
tie_value = [sorted_dice[2]]
elif (sorted_dice[1] == sorted_dice[0] or
sorted_dice[1] == sorted_dice[2]):
hand_type = 1
tie_value = [sorted_dice[1]]
if sorted_dice[1] == sorted_dice[0]:
tie_value += [sorted_dice[2]]
else:
tie_value += [sorted_dice[0]]
else:
hand_type = 0
tie_value = sorted_dice[::-1]
return hand_value, tie_value
Lecture 7 - Loops
- The Price Is Right - Clock Game video demonstration
# tpir.py from class:
import numpy as np
def create_price(low=100, high=1500):
return np.random.randint(low, high+1)
def get_guess():
guess = int(input('Guess: '))
return guess
def check_guess(actual, guess):
if actual > guess:
print('Higher!')
elif actual < guess:
print('Lower!')
if __name__ == '__main__':
the_price = create_price()
the_guess = get_guess()
while the_price != the_guess:
check_guess(the_price, the_guess)
the_guess = get_guess()
if the_price==the_guess:
print('You win!!!!!!!')
else:
print('LOOOOOOOOOOOOOOOSER')
- Getting temperatures:
# get_temps.py from class:
T = int(input('Temp: '))
Tlist = []
while T>=0:
Tlist += [T]
print(Tlist)
T = int(input('Temp: '))
Lecture 8 - Iterative Methods
- Taylor series fundamentals
- Maclaurin series approximation for exponential uses Chapra 4.2 to compute terms in an infinite sum.
- so
- Newton Method for finding square roots uses Chapra 4.2 to iteratively solve using a mathematical map. To find $ y $ where $ y=\sqrt{x} $:
$ \begin{align} y_{init}&=1\\ y_{new}&=\frac{y_{old}+\frac{x}{y_{old}}}{2} \end{align} $ - See Python version of Fig. 4.2 and modified version of 4.2 in the Resources section of Sakai page under Chapra Pythonified
Lecture 9 - Dictionaries and Loading
Lecture 10 - Monte Carlo Methods
- From Wikipedia: Monte Carlo method
- See file in "Programs From Class" folder under Resources on Sakai
Lecture 11 - Binary and Floating Point Numbers
- Different number systems convey information in different ways.
- Roman Numerals
- Chinese Numbers
- Binary Numbers
- We went through how to convert between decimal and binary
- Kibibytes et al
- "One billion dollars!" may not mean the same thing to different people: Long and Short Scales
- Floats (specifically double precision floats) are stored with a sign bit, 52 fractional bits, and 11 exponent bits. The exponent bits form a code:
- 0 (or 00000000000): the number is either 0 or a denormal
- 2047 (or 11111111111): the number is either infinite or not-a-number
- Others: the power of 2 for scientific notation is 2**(code-1023)
- The largest number is thus just *under* 2**1024 (ends up being (2-2**-52)**1024$ \approx 1.798\times 10^{308} $.
- The smallest normal number (full precision) is 2**(-1022)$ \approx 2.225\times 10^{-308} $.
- The smallest denormal number (only one significant binary digit) is 2**(-1022)/2**53 or 5e-324.
- When adding or subtracting, Python can only operate on the common significant digits - meaning the smaller number will lose precision.
- (1+1e-16)-1=0 and (1+1e-15)-1=1.1102230246251565e-15
- Avoid intermediate calculations that cause problems: if x=1.7e308,
- (x+x)/x is inf
- x/x + x/x is 2.0
Lecture 12 - Matrix Operations
- 1D arrays are neither rows nor columns - they are 1D!
- 2D arrays have...two dimensions, one of which might be 1
- Dot products as heart of matrix multiplication
- Inner dimensions must match; outer dimensions equal dimensions of result
- Reformatting linear algebra expressions as matrix equations
- Calculating determinants and inverses
- Shortcuts for determinants of 1x1, 2x2 and 3x3 matrices (see class notes for processes)
$ \begin{align*} \mbox{det}([a])&=a\\ \mbox{det}\left(\begin{bmatrix}a&b\\c&d\end{bmatrix}\right)&=ad-bc\\ \mbox{det}\left(\begin{bmatrix}a&b&c\\d&e&f\\g&h&i\end{bmatrix}\right)&=aei+bfg+cdh-afh-bdi-ceg\\ \end{align*} $
- Don't believe me? Ask Captain Matrix!
- Inverses of matrices:
- Generally, $ \mbox{inv}(A)=\frac{\mbox{cof}(A)^T}{\mbox{det}(A)} $ there the superscript T means transpose...
- And $ \mbox{det}(A)=\sum_{i\mbox{ or }j=0}^{N-1}a_{ij}(-1)^{i+j}M_{ij} $ for some $ j $ or $ i $...
- And $ M_{ij} $ is a minor of $ A $, specifically the determinant of the matrix that remains if you remove the $ i $th row and $ j $th column or, if $ A $ is a 1x1 matrix, 1
- And $ \mbox{cof(A)} $ is a matrix where the $ i,j $ entry $ c_{ij}=(-1)^{i+j}M_{ij} $
- And $ M_{ij} $ is a minor of $ A $, specifically the determinant of the matrix that remains if you remove the $ i $th row and $ j $th column or, if $ A $ is a 1x1 matrix, 1
- And $ \mbox{det}(A)=\sum_{i\mbox{ or }j=0}^{N-1}a_{ij}(-1)^{i+j}M_{ij} $ for some $ j $ or $ i $...
- Good news - for this class, you need to know how to calculate inverses of 1x1 and 2x2 matrices only:
- Generally, $ \mbox{inv}(A)=\frac{\mbox{cof}(A)^T}{\mbox{det}(A)} $ there the superscript T means transpose...
$ \begin{align} \mbox{inv}([a])&=\frac{1}{a}\\ \mbox{inv}\left(\begin{bmatrix}a&b\\c&d\end{bmatrix}\right)&=\frac{\begin{bmatrix}d &-b\\-c &a\end{bmatrix}}{ad-bc} \end{align} $
Lecture 13 - Linear Algebra
- Chapra 11.2.1 for norms
- np.linalg.nrom() in Python
- Chapra 1.2.2 for condition numbers
- np.linalg.cond() in Python
- Note: base-10 logarithm of condition number gives number of digits of precision possibly lost due to system geometry and scaling (top of p. 295 in Chapra)
- Converting equations to a matrix system:
- For a certain circuit, conservation equations learned in upper level classes will yield the following two equations:
$ \begin{align} \frac{v_1-v_s}{R1}+\frac{v_1}{R_2}+\frac{v_1-v_2}{R_3}&=0\\ \frac{v_2-v_1}{R_3}+\frac{v_2}{R_4}=0 \end{align} $
- Assuming $ v_s $ and the $ R_k $ values are known, to write this as a matrix equation, you need to get $ v_1 $ and $ v_2 $ on the left and everything else on the right:
$ \begin{align} \left(\frac{1}{R_1}+\frac{1}{R_2}+\frac{1}{R_3}\right)v_1+\left(-\frac{1}{R_3}\right)v_2&=\frac{v_s}{R_1}\\ \left(-\frac{1}{R_3}\right)v_1+\left(\frac{1}{R_3}+\frac{1}{R_4}\right)v_2&=0 \end{align} $
- Now you can write this as a matrix equation:
Lecture 14 - Linear Algebra with Parameter Sweeps
- See Python:Linear_Algebra#Sweeping_a_Parameter for example code on solving a system of equations when one parameter (either in the coefficient matrix or in the forcing vector or potentially both)