diff --git a/Lectures/L16_FunctProgContinued/A2Examples.py b/Lectures/L16_FunctProgContinued/A2Examples.py new file mode 100644 index 0000000000000000000000000000000000000000..fce8b597cf0fe71bf1ccd6dbec350af508485423 --- /dev/null +++ b/Lectures/L16_FunctProgContinued/A2Examples.py @@ -0,0 +1,31 @@ +from CurveADT import * +from Data import * +from Load import * +from Plot import * + +X = range(11) +Y = list(map(lambda x: x**2, X)) + +c1 = CurveT(X, Y, 1) + +print(c1.eval(6.5)) + +PlotCurve(c1, 50) + +c2 = CurveT(X, Y, 2) + +print(c2.eval(6.5)) + +PlotCurve(c2, 5) +#PlotCurve(c, 50) will look strange because of edge effects + +Load('glass.csv') + +c3 = Data.getC(9) +PlotCurve(c3, 50) +print(Data.eval(18, 34)) + +c4 = Data.slice(18, 1) +PlotCurve(c4, 50) +print(c4.eval(34)) + diff --git a/Lectures/L16_FunctProgContinued/FunctProgContinued.pdf b/Lectures/L16_FunctProgContinued/FunctProgContinued.pdf index 9c0d87dad5c09617064b5e04bce0b6cc0875056d..5e810b4ce072563c2853788d594520b07a27e20b 100644 Binary files a/Lectures/L16_FunctProgContinued/FunctProgContinued.pdf and b/Lectures/L16_FunctProgContinued/FunctProgContinued.pdf differ diff --git a/Lectures/L16_FunctProgContinued/FunctProgContinued.tex b/Lectures/L16_FunctProgContinued/FunctProgContinued.tex index 9d03c985cfbf1b73f06960ea8bfe22283a57c9a9..74170c51ae49901004cf0d7558c9f6fbb0a2325b 100755 --- a/Lectures/L16_FunctProgContinued/FunctProgContinued.tex +++ b/Lectures/L16_FunctProgContinued/FunctProgContinued.tex @@ -56,7 +56,7 @@ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -\begin{frame} +\begin{frame}[fragile] \frametitle{Administrative Details} \ifDraft @@ -69,12 +69,20 @@ TBD \item Part 1: February 12, 2018 \item Partner Files: February 18, 2018 \item Part 2: March 2, 2018 +\item For floats remember to use pytests + \href{https://docs.pytest.org/en/latest/builtin.html#pytest.approx}{approx} +\item Consider using + \href{https://docs.scipy.org/doc/scipy/reference/generated/scipy.interpolate.interp1d.html} + {interp1d} - the implementation does not have to follow the spec, it just has + to match the specified behaviour +\item A2 demo \end{itemize} \item Midterm exam \begin{itemize} \item Wednesday, February 28, 7:00 pm \item 90 minute duration +\item Next week's tutorial will cover a sample midterm \end{itemize} \end{itemize} } @@ -84,16 +92,21 @@ TBD %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -\begin{frame} +\begin{frame}[fragile] \frametitle{HW: Remove Everything but Uppercase} Write a function \texttt{removeNonUpperCase(st)} that takes a string \texttt{st} and returns the string that results by removing all non upper case letters\\ ~\newline -\structure<1>{How would you build the sequence of \texttt{['A', 'B', ..., 'Z']}?}\newline -\uncover<2->{\texttt{[chr(i) for i in range(ord('A'),ord('Z')+1)]}} - +\structure<1>{How would you build the sequence of \texttt{['A','B',...,'Z']}?}\newline +\uncover<2->{\texttt{[chr(i) for i in range(\structure<2>{?, ?})]}}\newline +\uncover<3->{\texttt{[chr(i) for i in range(ord('A'),ord('Z')+1)]}} +~\\ +\begin{verbatim} +def removeNonUpperCase(st): + return [c for c in st if (c in upCase)] +\end{verbatim} \end{frame} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% @@ -151,15 +164,16 @@ list(map(add3, [1, 5, 3, 1, 6])) \bi \item \texttt{lamda} followed by list of arguments: expression -\item \structure{Write code to add \texttt{'!'} to every string in a list of +\item \structure<1>{Write code to add \texttt{'!'} to every string in a list of strings} \ei - +\uncover<1>{\structure<1>{\texttt{list(map(?, ["BIFF","BANG","POW"]))}}}\\ +\uncover<2>{\texttt{list(map(lambda s: s+'!', ["BIFF","BANG","POW"]))}} \end{frame} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -\begin{frame} +\begin{frame}[fragile] \frametitle{Introduce Partial Functions} \begin{itemize} @@ -172,9 +186,12 @@ list(map(add3, [1, 5, 3, 1, 6])) \item $\mbox{rep}: t \times int \rightarrow [t]$ \ei \item What if we could partially evaluate rep? - \end{itemize} +\begin{verbatim} +def rep(x, n): + return [x for i in range(n)] +\end{verbatim} \end{frame} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% @@ -224,9 +241,12 @@ map (mapSqr, [[1,2], [3, 4, 5, 6], [7, 8]]) \end{verbatim} \bi -\item \structure{What is the type of \texttt{mapSqr}?} +\item \structure<1>{What is the type of \texttt{mapSqr}?}\newline +\uncover<2->{$\mbox{map}: (a \rightarrow b) \times [a] \rightarrow [b]$}\newline +\uncover<3->{$\mbox{map}: (a \rightarrow b) \times [[int]] \rightarrow [[int]]$}\newline +\uncover<4->{$\mbox{map}: ([int] \rightarrow [int]) \times [[int]] \rightarrow [[int]]$}\newline -\item \structure{Can you write a function of this type for \texttt{mapSqr}?} +\item \structure<4>{Can you write a function of this type for \texttt{mapSqr}?} \ei \end{frame} @@ -255,11 +275,19 @@ map (mapSqr, [[1,2], [3, 4, 5, 6], [7, 8]]) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -\begin{frame} +\begin{frame}[fragile] \frametitle{Filter Example} -Write a filter that takes a list of numbers and returns only the even numbers. -%\texttt{filter(even, range(1, 11))} +\structure<1>{Write a filter that takes a list of numbers from 1 to 10 and + returns only the even numbers.} + +\begin{verbatim} +def even(x): + return x%2==0 +\end{verbatim} + +\texttt{filter(func, seq)}\\ +\uncover<2>{\texttt{filter(even, range(1, 11))}} \end{frame} @@ -276,6 +304,7 @@ Write a filter that takes a list of numbers and returns only the even numbers. element \item Repeats until completely reduced \item \texttt{accum} is the initial value for the accumulator +\item In Python 3 need \texttt{from functools import reduce} \item \structure{How would you use reduce to calculate the sum of the numbers from 1 to 100?} \item \structure{How about the product?} @@ -285,6 +314,18 @@ Write a filter that takes a list of numbers and returns only the even numbers. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \begin{frame} +\frametitle{Understanding Reduce for Sum} + +\bi +\item \texttt{reduce(lambda x, y: x+y, range(1, 5), 0)} +\item \texttt{reduce(lambda x, y: x+y, [1, 2, 3, 4, 5], 0)} +\item \texttt{((((0+1)+2)+3)+4)+5} +\ei +\end{frame} + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +\begin{frame}[fragile] \frametitle{Standard Deviation} Write a function \texttt{stdDev(A)} that takes a list of numbers @@ -292,54 +333,77 @@ Write a function \texttt{stdDev(A)} that takes a list of numbers $$\sigma = \sqrt{\frac{\sum_{i=0}^{|A|-1} (A_i - \mu)^2}{|A|}}$$ +\begin{verbatim} +from math import sqrt +from functools import reduce +def stdDev(A): + add = lambda x, y: x+y + N = float(len(A)) + mu = reduce(add, A, 0)/N + tot = reduce(add, [(a - mu)**2 for a in A], 0) + return sqrt(tot/N) + +print(stdDev([13, 23, 12, 44, 55])) +\end{verbatim} \end{frame} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -\begin{frame} +\begin{frame}[fragile] \frametitle{And Series} -Write a function \texttt{forall(A)} that takes a list of boolean values -\texttt{A} and returns $\forall ( i:\mathbb{N} | i \in [0 ..|A|-1] : A_i)$ +\structure<1>{Write a function \texttt{forall(A)} that takes a list of boolean values +\texttt{A} and returns $\forall ( i:\mathbb{N} | i \in [0 ..|A|-1] : A_i)$} + +\begin{verbatim} +def forall(A): + return reduce(?, A, ?) + +print(forall([True, True, True, True])) +print(forall([True, True, True, False])) +\end{verbatim} +\uncover<2->{\texttt{reduce(lambda x, y: x and y, A, True)}} \end{frame} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -\begin{frame} +\begin{frame}[fragile] \frametitle{Contains} -Write a function \texttt{contains(x, xs)} that takes a value \texttt{x} and a -list of values \texttt{xs} and returns $\exists ( y | y \in xs : y = x)$ +\structure<1>{Write a function \texttt{contains(x, xs)} that takes a value \texttt{x} and a +list of values \texttt{xs} and returns $\exists ( y | y \in xs : y = x)$} -\end{frame} +\begin{verbatim} +def contains(x, xs): + return reduce(func?, seq?, False) -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +adjectives = ["lazy", "grouchy", "scheming"] -\begin{frame} -\frametitle{Index Set} +print(contains(4, [3, 4, 5, 6, 8])) +print(contains("happy", adjectives)) -Write a function \texttt{indexSet(x, B)} that takes a value \texttt{x} and a -list of values \texttt{B} and returns a list of indices where \texttt{B[i] = - x}.\\ -~\newline -As a first step, how would you say this mathematically? +\end{verbatim} + +\uncover<2->{\texttt{reduce(lambda a, b: a or b, [y==x for y in xs], False)}} \end{frame} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \begin{frame} -\frametitle{Index Set} +\frametitle{Homework: Index Set} Write a function \texttt{indexSet(x, B)} that takes a value \texttt{x} and a list of values \texttt{B} and returns a list of indices where \texttt{B[i] = x}.\\ +~\newline +\structure<1>{As a first step, how would you say this mathematically?}\\ -$$\mbox{indexSet}(x, B) \equiv \{i : \mathbb{N} | i \in [0 .. |B|-1] \wedge B_i = -x : i\}$$ +\uncover<2->{$$\mbox{indexSet}(x, B) \equiv \{i : \mathbb{N} | i \in [0 .. |B|-1] \wedge B_i = +x : i\}$$}\\ -How could you use \texttt{indexSet} to calculate \texttt{rank(x, A)}? +\uncover<3->{How could you use \texttt{indexSet} to calculate \texttt{rank(x, A)}?}\\ \end{frame} diff --git a/Lectures/L16_FunctProgContinued/FunctProgContinued.vrb b/Lectures/L16_FunctProgContinued/FunctProgContinued.vrb index b0738fc8cb5a5f0bb60ae7142ed996e1705cbc57..403da55b2fa691735972d847a483671d08881cee 100644 --- a/Lectures/L16_FunctProgContinued/FunctProgContinued.vrb +++ b/Lectures/L16_FunctProgContinued/FunctProgContinued.vrb @@ -1,15 +1,18 @@ -\frametitle{Another Example with Map} +\frametitle{Contains} -Write a map that takes a 2D list of numbers and returns a 2D list where all -elements are squared. +\structure<1>{Write a function \texttt{contains(x, xs)} that takes a value \texttt{x} and a +list of values \texttt{xs} and returns $\exists ( y | y \in xs : y = x)$} \begin{verbatim} -map (mapSqr, [[1,2], [3, 4, 5, 6], [7, 8]]) -\end{verbatim} +def contains(x, xs): + return reduce(func?, seq?, False) + +adjectives = ["lazy", "grouchy", "scheming"] -\bi -\item \structure{What is the type of \texttt{mapSqr}?} +print(contains(4, [3, 4, 5, 6, 8])) +print(contains("happy", adjectives)) + +\end{verbatim} -\item \structure{Can you write a function of this type for \texttt{mapSqr}?} -\ei +\uncover<2->{\texttt{reduce(lambda a, b: a or b, [y==x for y in xs], False)}} diff --git a/Lectures/L16_FunctProgContinued/examples.py b/Lectures/L16_FunctProgContinued/examples.py index 6bddba30bcd3097dbf097c08bacbf98486658d58..6c24d4ee7dde968e207ea6c87995e47b37840657 100644 --- a/Lectures/L16_FunctProgContinued/examples.py +++ b/Lectures/L16_FunctProgContinued/examples.py @@ -25,8 +25,6 @@ L7 = list(map(lambda s: s+'!', ["BIFF", "BANG", "POW"])) # Partial Functions -from functools import partial - def power(base, exponent): return base ** exponent @@ -60,10 +58,16 @@ def grt3(x): L8 = filter(grt3, [1,5,3,2,1,6,4,3,2,1]) L9 = filter(lambda x: x==3, [1,5,3,2,1,6,4,3,2,1]) + +def even(x): + return x%2==0 + L10 = filter(even, range(1, 11)) # Reduce +from functools import reduce + reduce(lambda x, y: x+y, range(1, 101), 0) reduce(lambda x, y: x*y, range(1, 101), 1) @@ -77,23 +81,25 @@ def stdDev(A): tot = reduce(add, [(a - mu)**2 for a in A], 0) return sqrt(tot/N) -print stdDev([13, 23, 12, 44, 55]) +print(stdDev([13, 23, 12, 44, 55])) # And Series def forall(A): return reduce(lambda x, y: x and y, A, True) -print forall([True, True, True, True]) -print forall([True, True, True, False]) +print(forall([True, True, True, True])) +print(forall([True, True, True, False])) # Contains def contains(x, xs): return reduce(lambda a, b: a or b, [y==x for y in xs], False) -print contains(4, [3, 4, 5, 6, 8]) -print contains("happy", adjectives) +adjectives = ["lazy", "grouchy", "scheming"] + +print(contains(4, [3, 4, 5, 6, 8])) +print(contains("happy", adjectives)) def indexSet(x, B): return [i for i in range(len(B)) if B[i]==x] diff --git a/Lectures/L16_FunctProgContinued/examplesPartial.py b/Lectures/L16_FunctProgContinued/examplesPartial.py index 42fcfaf3ff199cbf7643491b9cfa08be653958e7..f3b2b3540ef2bdaa1c80a2134e3a107bd271039c 100644 --- a/Lectures/L16_FunctProgContinued/examplesPartial.py +++ b/Lectures/L16_FunctProgContinued/examplesPartial.py @@ -60,9 +60,11 @@ L10 = filter(func?, seq?) # Reduce -reduce(?) +from functools import reduce -reduce(?) +reduce(func, seq, accum) + +reduce(func, seq, accum) # Standard Deviation @@ -70,19 +72,19 @@ from math import sqrt def stdDev(A): add = lambda x, y: x+y N = float(len(A)) - mu = reduce(? - tot = reduce(? + mu = reduce(func, seq, accum) + tot = reduce(func, seq, accum) return sqrt(tot/N) -print stdDev([13, 23, 12, 44, 55]) +print(stdDev([13, 23, 12, 44, 55])) # And Series def forall(A): - return reduce(?) + return reduce(func, seq, accum) -print forall([True, True, True, True]) -print forall([True, True, True, False]) +print(forall([True, True, True, True])) +print(forall([True, True, True, False])) # Contains @@ -91,8 +93,8 @@ def contains(x, xs): adjectives = ["lazy", "grouchy", "scheming"] -print contains(4, [3, 4, 5, 6, 8]) -print contains("happy", adjectives) +print(contains(4, [3, 4, 5, 6, 8])) +print(contains("happy", adjectives)) def indexSet(x, B): return [?]