Difference between revisions of "EGR 103/Concept List Fall 2019"
Jump to navigation
Jump to search
\(
y=e^x=\sum_{n=0}^{\infty}\frac{x^n}{n!}
\)
\(
\begin{align}
y_{init}&=1\\
y_{new}&=y_{old}+\frac{x^n}{n!}
\end{align}
\)
Line 19: | Line 19: | ||
* Some commands are only available by importing from modules; <code>import numpy as np</code> will bring in all the functions of the numpy module. Access these commands by typing np.VALUE or np.FUNCTION (for example, np.pi or np.cos(2)) | * Some commands are only available by importing from modules; <code>import numpy as np</code> will bring in all the functions of the numpy module. Access these commands by typing np.VALUE or np.FUNCTION (for example, np.pi or np.cos(2)) | ||
− | == Lecture 3 == | + | == Lecture 3 - Variable Types == |
* Python has several different variable types, each with their own purpose and operators. | * Python has several different variable types, each with their own purpose and operators. | ||
* Main ones this lecture: int, float, string, tuple, list. | * Main ones this lecture: int, float, string, tuple, list. | ||
− | == Lecture 4 == | + | == Lecture 4 - Dictionaries and Functions == |
− | * Brief discussion of | + | * Brief discussion of dictionaries, how to build, and how to access. |
* Two main types of function - lambda functions and defined functions | * Two main types of function - lambda functions and defined functions | ||
* Lambda functions are one line of code; can have multiple inputs but only one expression. | * Lambda functions are one line of code; can have multiple inputs but only one expression. | ||
Line 37: | Line 37: | ||
**** Function will create a dictionary of keyword and value pairs | **** Function will create a dictionary of keyword and value pairs | ||
− | == Lecture 5 == | + | == Lecture 5 - Format, Logic, Decisions, and Loops == |
* Creating formatted strings using {} and .format() ([https://www.python.org/dev/peps/pep-3101/#format-strings format strings], [https://www.python.org/dev/peps/pep-3101/#standard-format-specifiers 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. | * Creating formatted strings using {} and .format() ([https://www.python.org/dev/peps/pep-3101/#format-strings format strings], [https://www.python.org/dev/peps/pep-3101/#standard-format-specifiers 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. | ||
* Basics of decisions using if...elif...else | * Basics of decisions using if...elif...else | ||
Line 74: | Line 74: | ||
</div> | </div> | ||
− | == Lecture 6 == | + | == Lecture 6 - TPIR Example with Decisions and Loops == |
* The Price Is Right - Clock Game: | * The Price Is Right - Clock Game: | ||
<div class="mw-collapsible mw-collapsed"> | <div class="mw-collapsible mw-collapsed"> | ||
Line 122: | Line 122: | ||
</div> | </div> | ||
− | == Lecture 7 == | + | == Lecture 7 - Input Checking == |
* Robust programming | * Robust programming | ||
* isinstance to check type | * isinstance to check type | ||
Line 177: | Line 177: | ||
*Note: my_string.isdigit() works like check_for_int() above | *Note: my_string.isdigit() works like check_for_int() above | ||
− | == Lecture 8 == | + | == Lecture 8 - Taylor Series and Iterative Solutions == |
* Taylor series fundamentals | * Taylor series fundamentals | ||
* Maclaurin series approximation for exponential uses Chapra 4.2 to compute terms in an infinite sum. | * Maclaurin series approximation for exponential uses Chapra 4.2 to compute terms in an infinite sum. | ||
Line 200: | Line 200: | ||
* See Python version of Fig. 4.2 and modified version of 4.2 in the Resources section of Sakai page under Chapra Pythonified | * 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 == | + | == Lecture 9 - Binary and Floating Point Numbers == |
* Different number systems convey information in different ways. | * Different number systems convey information in different ways. | ||
** Roman Numerals | ** Roman Numerals | ||
Line 218: | Line 218: | ||
*** (x+x)/x is inf | *** (x+x)/x is inf | ||
*** x/x + x/x is 2.0 | *** x/x + x/x is 2.0 | ||
− | + | ||
+ | == Lecture 10 - Numerical Issues == | ||
* List-building roundoff demonstration | * List-building roundoff demonstration | ||
<div class="mw-collapsible mw-collapsed"> | <div class="mw-collapsible mw-collapsed"> | ||
Line 285: | Line 286: | ||
</div> | </div> | ||
</div> | </div> | ||
− | --> | + | * List comprehensions: info in [http://greenteapress.com/thinkpython2/html/thinkpython2020.html#sec224 Think Python] and Section 7.1.1 in Punch & Enbody |
+ | ** Basic idea: make a list of [EXPRESSION for VARIABLE in ITERABLE if LOGIC] | ||
+ | ** Even numbers 0 through 10: [k for k in range(11) if k%2==0] | ||
+ | ** Non-vowels in a word: [letter for letter in word if not letter in 'aeiouAEIOU'] | ||
+ | ** Square roots of nonnegative numbers in a list of numbers: [sqrt{n} for n in nums if n>=0] | ||
+ | ** Print all three character codes with each character unique: | ||
+ | <div class="mw-collapsible mw-collapsed"> | ||
+ | <source lang=python> | ||
+ | # unique_codes.py | ||
+ | </source> | ||
+ | <div class="mw-collapsible-content"> | ||
+ | <source lang=python> | ||
+ | letters = 'howdy' | ||
+ | count = 0 | ||
+ | for l1 in letters: | ||
+ | #print('{}'.format(l1)) | ||
+ | for l2 in [k for k in letters if k not in [l1]]: | ||
+ | #print('{} {}'.format(l1, l2)) | ||
+ | for l3 in [m for m in letters if m not in [l1, l2]]: | ||
+ | count += 1 | ||
+ | print('{:4.0f} {} {} {}'.format(count, l1, l2, l3)) | ||
+ | </source> | ||
+ | </div> | ||
+ | </div> | ||
+ | ::* Can also replace letters with whatever should comprise three element code; letters='hello' will make all three letter words out of h, e, l, and o (24 choices) and letters=['1', '2', '3', '4, '5'] will make all three-digit codes | ||
Line 578: | Line 603: | ||
=== Lecture 10 === | === Lecture 10 === | ||
− | + | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
* Iterative solutions - where next approximation or guess is based on previous guess and some algorithm | * Iterative solutions - where next approximation or guess is based on previous guess and some algorithm | ||
** Series method for finding exponential <math>e^c</math>:<center><math> | ** Series method for finding exponential <math>e^c</math>:<center><math> |
Revision as of 16:36, 15 February 2019
This page will be used to keep track of the commands and major concepts for each lecture in EGR 103.
Contents
- 1 Lecture 1 - Introduction
- 2 Lecture 2 - Programs and Programming
- 3 Lecture 3 - Variable Types
- 4 Lecture 4 - Dictionaries and Functions
- 5 Lecture 5 - Format, Logic, Decisions, and Loops
- 6 Lecture 6 - TPIR Example with Decisions and Loops
- 7 Lecture 7 - Input Checking
- 8 Lecture 8 - Taylor Series and Iterative Solutions
- 9 Lecture 9 - Binary and Floating Point Numbers
- 10 Lecture 10 - Numerical Issues
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
- Piazza page: Piazza 103L page; message board for questions
Lecture 2 - Programs and Programming
- To play with Python:
- Install it on your machine or a public machine: Download
- Quick tour of Python
- Editing window, variable explorer, and console
- Variable explorer is your friend
- From Dewey - programming language typically have ability to work with input, output, math, conditional execution, and repetition
- Hilton and Bracy Seven Steps
- Class work developing algorithm for program to determine if a number is prime
- Inputs in Python using input() command - always grab strings
- Convert strings containing integer characters to integers using int()
- Some commands are only available by importing from modules;
import numpy as np
will bring in all the functions of the numpy module. Access these commands by typing np.VALUE or np.FUNCTION (for example, np.pi or np.cos(2))
Lecture 3 - Variable Types
- Python has several different variable types, each with their own purpose and operators.
- Main ones this lecture: int, float, string, tuple, list.
Lecture 4 - Dictionaries and Functions
- Brief discussion of dictionaries, how to build, and how to access.
- Two main types of function - lambda functions and defined functions
- Lambda functions are one line of code; can have multiple inputs but only one expression.
- c = lambda a,b: np.sqrt(a**2 + b**2)
- Defined functions can be multiple lines of code and have multiple outputs.
- Four different types of argument:
- Required (listed first)
- Named with defaults (second)
- Additional positional arguments ("*args") (third)
- Function will create a tuple containing these items in order
- Additional keyword arguments ("**kwargs") (last)
- Function will create a dictionary of keyword and value pairs
- Four different types of argument:
Lecture 5 - Format, Logic, Decisions, and Loops
- 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.
- Basics of decisions using if...elif...else
- Basics of loops using for and while
- Building a program to count the number of numbers, vowels, consonants, and other characters in a phrase
# letter_counter.py from class:
def counter(phrase):
counts = [0, 0, 0, 0]
nums = "0123456789"
vowels = "aeiou"
cons = "bcdfghjklmnpqrstvwxyz"
for k in phrase.lower():
#print(k)
if k in nums:
# print('{:s} is a number!'.format(k))
counts[0] += 1
elif k in vowels:
counts[1] += 1
elif k in cons:
counts[2] += 1
else:
counts[3] += 1
return counts
c = counter("Hello! Go 2022! East Campus Rocks!")
print(c)
Lecture 6 - TPIR Example with Decisions and Loops
- The Price Is Right - Clock Game:
# tpir.py from class:
# -*- coding: utf-8 -*-
"""
The Price Is Right - Clock Game
"""
import numpy as np
import time
def create_price(low, high):
return np.random.randint(low, high+1)
def get_guess():
guess = int(input('Guess: '))
return guess
def check_guess(new_guess, price):
if new_guess > price:
print('${:0.0f} is too high - Lower!'.format(new_guess))
elif new_guess < price:
print('Higher!')
else:
print('You win!')
price = create_price(100, 1000)
# print(price)
new_guess = price + 1
start_time = time.clock()
while new_guess != price and (time.clock()-start_time)<30:
new_guess = get_guess()
# print(new_guess)
check_guess(new_guess, price)
if new_guess != price:
print('You lose :( '))
Lecture 7 - Input Checking
- Robust programming
- isinstance to check type
# validator.py from class:
def check_for_int(x):
'''
returns true if string contains an valid int
returns false otherwise
'''
for k in x:
if k not in '0123456789':
return False
return True
def get_good():
x = input('Integer between 0 and 10: ')
bad = True
''' not quite
while bad:
if check_for_int(x) is False:
pass
elif int(x)>=0 and int(x)<=10:
bad = False
return int(x)
'''
while bad:
if check_for_int(x) is False:
print('Wrong type of input')
x = input('INTEGER between 0 and 10: ')
elif int(x) < 0 or int(x) > 10:
print('Invalid value')
x = input('Integer BETWEEN 0 and 10: ')
else:
bad = False
return int(x)
if __name__ == "__main__":
check_for_int('1.1')
y = get_good()
print(y)
- Note: my_string.isdigit() works like check_for_int() above
Lecture 8 - Taylor Series and Iterative Solutions
- 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 - Binary and Floating Point Numbers
- Different number systems convey information in different ways.
- Roman Numerals
- Chinese Numbers
- Ndebe Igbo Numbers
- "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 10 - Numerical Issues
- List-building roundoff demonstration
# Roundoff Demo
import numpy as np
import matplotlib.pyplot as plt
start = 10;
delta = 0.1;
finish = 100;
k = 0
val_c = [start];
val_a = [start];
while val_c[-1]+delta <= finish:
val_c += [val_c[-1] + delta];
k = k + 1
val_a += [start + k*delta];
array_c = np.array(val_c)
array_a = np.array(val_a)
diffs = [val_c[k]-val_a[k] for k in range(len(val_a))]
plt.figure(1)
plt.clf()
#plt.plot(array_c - array_a, 'k-')
plt.plot(diffs, 'k-')
- Exponential demo program
# Exponential demo
#%%
import numpy as np
import matplotlib.pyplot as plt
#%%
def exp_calc(x, n):
return (1 + (x/n))**n
#%% Shows that apprxomation gets better as n increases
print(np.exp(1))
print(exp_calc(1, 1))
print(exp_calc(1, 10))
print(exp_calc(1, 100))
print(exp_calc(1, 1000))
print(exp_calc(1, 10000))
print(exp_calc(1, 100000))
#%% Shows that once n is too big, problems happen
n = np.logspace(0, 18, 1e3);
plt.figure(1)
plt.clf()
plt.semilogx(n, exp_calc(1, n), 'b-')
- List comprehensions: info in Think Python and Section 7.1.1 in Punch & Enbody
- Basic idea: make a list of [EXPRESSION for VARIABLE in ITERABLE if LOGIC]
- Even numbers 0 through 10: [k for k in range(11) if k%2==0]
- Non-vowels in a word: [letter for letter in word if not letter in 'aeiouAEIOU']
- Square roots of nonnegative numbers in a list of numbers: [sqrt{n} for n in nums if n>=0]
- Print all three character codes with each character unique:
# unique_codes.py
letters = 'howdy'
count = 0
for l1 in letters:
#print('{}'.format(l1))
for l2 in [k for k in letters if k not in [l1]]:
#print('{} {}'.format(l1, l2))
for l3 in [m for m in letters if m not in [l1, l2]]:
count += 1
print('{:4.0f} {} {} {}'.format(count, l1, l2, l3))
- Can also replace letters with whatever should comprise three element code; letters='hello' will make all three letter words out of h, e, l, and o (24 choices) and letters=['1', '2', '3', '4, '5'] will make all three-digit codes