Difference between revisions of "EGR 103/Concept List Fall 2019"
Jump to navigation
Jump to search
Line 207: | Line 207: | ||
</div> | </div> | ||
</div> | </div> | ||
+ | |||
+ | === Lecture 9 === | ||
+ | * Different number systems convey information in different ways. | ||
+ | * "One billion dollars!" may not mean the same thing to different people: [https://en.wikipedia.org/wiki/Long_and_short_scales 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<math>\approx 1.798\times 10^{308}</math>. | ||
+ | *** The smallest normal number (full precision) is 2**(-1022)<math>\approx 2.225\times 10^{-308}</math>. | ||
+ | *** 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 | ||
+ | * List-building roundoff demonstration | ||
+ | <div class="mw-collapsible mw-collapsed"> | ||
+ | <source lang=python> | ||
+ | # Roundoff Demo | ||
+ | </source> | ||
+ | <div class="mw-collapsible-content"> | ||
+ | <source lang=python> | ||
+ | |||
+ | 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-') | ||
+ | </source> | ||
+ | </div> | ||
+ | </div> | ||
+ | * Exponential demo program | ||
+ | <div class="mw-collapsible mw-collapsed"> | ||
+ | <source lang=python> | ||
+ | # Exponential demo | ||
+ | </source> | ||
+ | <div class="mw-collapsible-content"> | ||
+ | <source lang=python> | ||
+ | #%% | ||
+ | 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-') | ||
+ | </source> | ||
+ | </div> | ||
+ | </div> | ||
+ | |||
+ | |||
== Labs == | == Labs == | ||
Line 220: | Line 304: | ||
** Work on lab every day - at least logging in, changing directories, using (emacs, latex, dvipdf, evince) | ** Work on lab every day - at least logging in, changing directories, using (emacs, latex, dvipdf, evince) | ||
** Work a little at a time to help with debugging | ** Work a little at a time to help with debugging | ||
+ | |||
+ | <!-- | ||
+ | |||
+ | * Exponential approximation demonstration | ||
+ | <div class="mw-collapsible mw-collapsed"> | ||
+ | <source lang=python> | ||
+ | # Fibonacci program from class | ||
+ | </source> | ||
+ | <div class="mw-collapsible-content"> | ||
+ | <source lang=python> | ||
+ | # code | ||
+ | </source> | ||
+ | </div> | ||
+ | </div> | ||
+ | --> |
Revision as of 20:56, 24 September 2018
This page will be used to keep track of the commands and major concepts for each lab in EGR 103.
Contents
Lectures
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
- Use a Duke container with Spyder: Containers - note - these do not have access to your Duke files. More on how to connect that later.
- 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
Lecture 3
- 7 Steps for finding prime numbers
- prime program -- includes intro to input(), if tree, for loop, print(), remainder %
Lecture 4
- Function definitions
- Positional and key word arguments (kwargs)
- Default values
- Returns tuples -- can be received by a single item or a tuple of the right size
- Aquarium
Lecture 5
- print() and format specifications: link
- Main components are width, precision, and type; sometimes initial +
- e and f can print integers as floats; d cannot print floats
- relational and logical operators - how they work on item, string, list, tuple
- if trees
- while loops
- for loops
- NOTE: in the case of
for y in x
- If the entries of x are changed, or really if x is changed in a way that its location in memory remained unchanged, y will iterate over the changed entries. If x is changed so that a copy first has to be made (for example, it is set equal to a slice of itself), then y will iterate over the original entries. Note the differences between:
x = [1, 2, 3, 4, 5]
for y in x:
print(x, y)
x[4] = [0]
print(x, y)
and:
x = [1, 2, 3, 4, 5]
for y in x:
print(x, y)
x = x[:-1]
print(x, y)
- counting characters program
Expand
# letter_typing.py from class:
- Question in class: does Python have ++ or -- operators; it does not. You need x += 1 or x -= 1
Lecture 7
- Distinction between == and i
- Iterable types and how they work (list, tuple, string
- Things that do not change memory address of a list (and therefore change what a loop iterates over):
- +, .append(), .extend(), .insert(), .remove(), .pop(), .sort(), .reverse(), .clear()
- Things that do change memory address of a list:
- Total replacement, replacement by self-slice, making a copy
- Singularity functions
- Unit step \(u(t)\)
- See Singularity Functions for way too much information - more in lab!
- Singularity_Functions#Alternate_Names_for, Singularity_Functions#Building_a_Mystery_Signal, and Singularity_Functions#Accumulated_Differences might be particularly helpful!
Lecture 8
- Be sure to use the resources available to you, especially the Think Python links in the Class Schedule!
- When checking for valid inputs, sometimes the order of asking the question can be important. If you want a single non-negative integer, you first need to ask if you have an integer before you ask if something is non-negative. The right way to go is something like:
if not isinstance(n, int) or n<1:
because if n is not an integer, the logic will be true without having to ask the second question. This is a good thing, because if n is, for example, a list, asking the question n<1 would yield an error.
- We built up a program whose end goal was to compare and contrast the time it takes different methods of calculating Fibonacci numbers to run.
- The plt.plot(x, y) command has some near cousins:
- plt.semilogy(x, y) will plot with a regular scale for x and a base-10 log scale for y
- plt.semilogx(x, y) will plot with a base-10 log scale for x and a regular scale for y
- plt.loglog(x, y) will plot with base-10 log scales in both directions
- Fibonacci comparison program
Expand
# Fibonacci program from class
Lecture 9
- Different number systems convey information in different ways.
- "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
- List-building roundoff demonstration
Expand
# Roundoff Demo
- Exponential demo program
Expand
# Exponential demo
Labs
- Lab 1
- Unix commands: pwd, cd, ls, mkdir, wget, tar, cp, latex, dvipdf, evince, xeyes
- Other concepts: MobaXterm, XQuartz, ssh
- Windows permissions were covered, but were only needed during this one lab.
- Mounting CIFS drives was covered, but will not be needed for lab 1.
- Three parts of lab:
- Once only ever: creating EGR 103 folder, setting Windows permissions
- Once per lab: creating lab1 folder, wget-ting files, tar expansion, duplicate lab skeleton
- Doing work: changing to lab1 folder; using emacs, latex, dvipsd, and evince correctly
- Work on lab every day - at least logging in, changing directories, using (emacs, latex, dvipdf, evince)
- Work a little at a time to help with debugging