Merge lp:~martinal/fenics-book/algorithm-and-codefigure-changes into lp:fenics-book

Proposed by Martin Sandve Alnæs
Status: Merged
Merged at revision: 1030
Proposed branch: lp:~martinal/fenics-book/algorithm-and-codefigure-changes
Merge into: lp:fenics-book
Diff against target: 141 lines (+45/-40)
1 file modified
tex/17.tex (+45/-40)
To merge this branch: bzr merge lp:~martinal/fenics-book/algorithm-and-codefigure-changes
Reviewer Review Type Date Requested Status
Registry Administrators Pending
Review via email: mp+79666@code.launchpad.net

Description of the change

Fixes algorithms and layout problems with a figure with code in chapter 17 (UFL).

To post a comment you must log in.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'tex/17.tex'
--- tex/17.tex 2011-10-17 12:35:24 +0000
+++ tex/17.tex 2011-10-18 10:09:24 +0000
@@ -1,7 +1,4 @@
1% Used in algorithms1% Define a sequence, used in algorithms and text
2\newcommand{\afor}{\textbf{for} {}}
3\newcommand{\aif}{\textbf{if} {}}
4\newcommand{\aelse}{\textbf{else} {}}
5\newcommand{\seq}[1]{\left\langle{#1}\right\rangle}2\newcommand{\seq}[1]{\left\langle{#1}\right\rangle}
63
7% Boldface letters4% Boldface letters
@@ -1283,13 +1280,17 @@
1283setting $z = y_n$. Algorithm~\ref{ufl:alg:program} shows such a program.1280setting $z = y_n$. Algorithm~\ref{ufl:alg:program} shows such a program.
12841281
1285\begin{algorithm}1282\begin{algorithm}
1286\afor $i = 1, \ldots, m$:\\
1287\tab $ y_i = t_i = \mbox{terminal expression}$ \\
1288\afor $i = m+1, \ldots, n$:\\
1289\tab $ y_i = f_i(\seq{y_j}_{j\in\mI_i})$ \\
1290$z = y_n$
1291\caption{Program to compute an expression $z$.}1283\caption{Program to compute an expression $z$.}
1292\label{ufl:alg:program}1284\label{ufl:alg:program}
1285\begin{algorithmic}[1]
1286\For{$i \gets 1, \ldots, m$}
1287\State $y_i := t_i = \mbox{terminal expression}$
1288\EndFor
1289\For{$i \gets m+1, \ldots, n$}
1290\State $y_i := f_i(\seq{y_j}_{j\in\mI_i})$
1291\EndFor
1292\State $z := y_n$
1293\end{algorithmic}
1293\end{algorithm}1294\end{algorithm}
12941295
1295Each terminal expression $t_i$ is a literal constant or input argument to1296Each terminal expression $t_i$ is a literal constant or input argument to
@@ -1734,16 +1735,20 @@
1734This extension gives Algorithm~\ref{ufl:alg:forwardad}.1735This extension gives Algorithm~\ref{ufl:alg:forwardad}.
17351736
1736\begin{algorithm}1737\begin{algorithm}
1737\afor $i = 1, \ldots, m$:\\
1738\tab $y_i = t_i$ \\
1739\tab $\frac{d y_i}{d v} = \frac{d t_i}{d v}$ \\
1740\afor $i = m+1, \ldots, n$:\\
1741\tab $y_i = f_i(\seq{y_j}_{j\in\mI_i})$ \\
1742\tab $\frac{d y_i}{d v} = \sum_{k\in\mI_i} \frac{\partial f_i}{\partial y_k} \frac{d y_k}{d v}$ \\
1743$z = y_n$ \\
1744$\frac{d z}{d v} = \frac{d y_n}{d v}$
1745\caption{Forward mode AD on Algorithm~\ref{ufl:alg:program}.}1738\caption{Forward mode AD on Algorithm~\ref{ufl:alg:program}.}
1746\label{ufl:alg:forwardad}1739\label{ufl:alg:forwardad}
1740\begin{algorithmic}[1]
1741\For{$i \gets 1, \ldots, m$}
1742\State $y_i := t_i$
1743\State $\frac{d y_i}{d v} := \frac{d t_i}{d v}$
1744\EndFor
1745\For{$i \gets m+1, \ldots, n$}
1746\State $y_i := f_i(\seq{y_j}_{j\in\mI_i})$
1747\State $\frac{d y_i}{d v} := \sum_{k\in\mI_i} \frac{\partial f_i}{\partial y_k} \frac{d y_k}{d v}$
1748\EndFor
1749\State $z := y_n$
1750\State $\frac{d z}{d v} := \frac{d y_n}{d v}$
1751\end{algorithmic}
1747\end{algorithm}1752\end{algorithm}
17481753
1749This way of extending a program to simultaneously compute the expression1754This way of extending a program to simultaneously compute the expression
@@ -1754,13 +1759,17 @@
1754Algorithm~\ref{ufl:alg:forwardadprogram}, which is isomorphic to1759Algorithm~\ref{ufl:alg:forwardadprogram}, which is isomorphic to
1755Algorithm~\ref{ufl:alg:program} (they have exactly the same structure).1760Algorithm~\ref{ufl:alg:program} (they have exactly the same structure).
1756\begin{algorithm}1761\begin{algorithm}
1757\afor $i = 1, \ldots, \hat m$:\\
1758\tab $\hat y_i = \hat t_i$ \\
1759\afor $i = \hat m + 1, \ldots, \hat n$:\\
1760\tab $\hat y_i = \hat f_i(\seq{\hat y_j}_{j\in\hat\mI_i})$ \\
1761$\frac{d z}{d v} = \hat y_{\hat n}$
1762\caption{Program to compute $\frac{d z}{d v}$ produced by forward mode AD}1762\caption{Program to compute $\frac{d z}{d v}$ produced by forward mode AD}
1763\label{ufl:alg:forwardadprogram}1763\label{ufl:alg:forwardadprogram}
1764\begin{algorithmic}[1]
1765\For{$i \gets 1, \ldots, \hat m$}
1766\State $\hat y_i := \hat t_i$
1767\EndFor
1768\For{$i \gets \hat m + 1, \ldots, \hat n$}
1769\State $\hat y_i := \hat f_i(\seq{\hat y_j}_{j\in\hat\mI_i})$
1770\EndFor
1771\State $\frac{d z}{d v} := \hat y_{\hat n}$
1772\end{algorithmic}
1764\end{algorithm}1773\end{algorithm}
17651774
1766Since the program in Algorithm~\ref{ufl:alg:program} can be1775Since the program in Algorithm~\ref{ufl:alg:program} can be
@@ -2025,14 +2034,15 @@
2025is somewhat different from the patterns used in a statically typed2034is somewhat different from the patterns used in a statically typed
2026language such as C++.2035language such as C++.
20272036
2028One way to implement type based operation selection is to use a type2037One way to implement type based operation selection is to use a
2029switch, or a sequence of if-tests such as this:2038\emph{type switch}, which is a sequence of if-tests as shown here:
2030\begin{python}2039\begin{python}
2031if isinstance(expression, IntValue):2040def operation(expression):
2032 result = int_operation(expression)2041 if isinstance(expression, IntValue):
2033elif isinstance(expression, Sum):2042 return int_operation(expression)
2034 result = sum_operation(expression)2043 elif isinstance(expression, Sum):
2035# etc.2044 return sum_operation(expression)
2045 # etc.
2036\end{python}2046\end{python}
2037There are several problems with this approach, one of which is2047There are several problems with this approach, one of which is
2038efficiency when there are many types to check. A type based function2048efficiency when there are many types to check. A type based function
@@ -2053,14 +2063,7 @@
2053the closest superclass handler function is used instead. Note that2063the closest superclass handler function is used instead. Note that
2054the \emp{MultiFunction} implementation is specialized to types in the2064the \emp{MultiFunction} implementation is specialized to types in the
2055\emp{Expr} class hierarchy. The declaration and use of a multifunction2065\emp{Expr} class hierarchy. The declaration and use of a multifunction
2056is illustrated in Figure~\ref{ufl:fig:examplefunction}. Note that2066is illustrated in this example code:
2057\emp{argument} and \emp{sum} will handle instances of the exact types
2058\emp{Argument} and \emp{Sum}, while \emp{terminal} and \emp{operator}
2059will handle the types \emp{SpatialCoordinate} and \emp{Product} since
2060they have no specific handlers.
2061
2062\begin{figure}
2063\bwfig
2064\begin{python}2067\begin{python}
2065class ExampleFunction(MultiFunction):2068class ExampleFunction(MultiFunction):
2066 def __init__(self):2069 def __init__(self):
@@ -2088,9 +2091,11 @@
2088print m(x[0] + x[1])2091print m(x[0] + x[1])
2089print m(x[0] * x[1])2092print m(x[0] * x[1])
2090\end{python}2093\end{python}
2091\caption{Example declaration and use of a multifunction.}2094Note that \emp{argument} and \emp{sum} will handle instances of the exact types
2092\label{ufl:fig:examplefunction}2095\emp{Argument} and \emp{Sum}, while \emp{terminal} and \emp{operator}
2093\end{figure}2096will handle the types \emp{SpatialCoordinate} and \emp{Product} since
2097they have no specific handlers.
2098
2094%------------------------------------------------------------2099%------------------------------------------------------------
2095\subsection{Implementing expression transformations}2100\subsection{Implementing expression transformations}
2096\label{ufl:sec:transformer}2101\label{ufl:sec:transformer}

Subscribers

People subscribed via source and target branches