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
1=== modified file 'tex/17.tex'
2--- tex/17.tex 2011-10-17 12:35:24 +0000
3+++ tex/17.tex 2011-10-18 10:09:24 +0000
4@@ -1,7 +1,4 @@
5-% Used in algorithms
6-\newcommand{\afor}{\textbf{for} {}}
7-\newcommand{\aif}{\textbf{if} {}}
8-\newcommand{\aelse}{\textbf{else} {}}
9+% Define a sequence, used in algorithms and text
10 \newcommand{\seq}[1]{\left\langle{#1}\right\rangle}
11
12 % Boldface letters
13@@ -1283,13 +1280,17 @@
14 setting $z = y_n$. Algorithm~\ref{ufl:alg:program} shows such a program.
15
16 \begin{algorithm}
17-\afor $i = 1, \ldots, m$:\\
18-\tab $ y_i = t_i = \mbox{terminal expression}$ \\
19-\afor $i = m+1, \ldots, n$:\\
20-\tab $ y_i = f_i(\seq{y_j}_{j\in\mI_i})$ \\
21-$z = y_n$
22 \caption{Program to compute an expression $z$.}
23 \label{ufl:alg:program}
24+\begin{algorithmic}[1]
25+\For{$i \gets 1, \ldots, m$}
26+\State $y_i := t_i = \mbox{terminal expression}$
27+\EndFor
28+\For{$i \gets m+1, \ldots, n$}
29+\State $y_i := f_i(\seq{y_j}_{j\in\mI_i})$
30+\EndFor
31+\State $z := y_n$
32+\end{algorithmic}
33 \end{algorithm}
34
35 Each terminal expression $t_i$ is a literal constant or input argument to
36@@ -1734,16 +1735,20 @@
37 This extension gives Algorithm~\ref{ufl:alg:forwardad}.
38
39 \begin{algorithm}
40-\afor $i = 1, \ldots, m$:\\
41-\tab $y_i = t_i$ \\
42-\tab $\frac{d y_i}{d v} = \frac{d t_i}{d v}$ \\
43-\afor $i = m+1, \ldots, n$:\\
44-\tab $y_i = f_i(\seq{y_j}_{j\in\mI_i})$ \\
45-\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}$ \\
46-$z = y_n$ \\
47-$\frac{d z}{d v} = \frac{d y_n}{d v}$
48 \caption{Forward mode AD on Algorithm~\ref{ufl:alg:program}.}
49 \label{ufl:alg:forwardad}
50+\begin{algorithmic}[1]
51+\For{$i \gets 1, \ldots, m$}
52+\State $y_i := t_i$
53+\State $\frac{d y_i}{d v} := \frac{d t_i}{d v}$
54+\EndFor
55+\For{$i \gets m+1, \ldots, n$}
56+\State $y_i := f_i(\seq{y_j}_{j\in\mI_i})$
57+\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}$
58+\EndFor
59+\State $z := y_n$
60+\State $\frac{d z}{d v} := \frac{d y_n}{d v}$
61+\end{algorithmic}
62 \end{algorithm}
63
64 This way of extending a program to simultaneously compute the expression
65@@ -1754,13 +1759,17 @@
66 Algorithm~\ref{ufl:alg:forwardadprogram}, which is isomorphic to
67 Algorithm~\ref{ufl:alg:program} (they have exactly the same structure).
68 \begin{algorithm}
69-\afor $i = 1, \ldots, \hat m$:\\
70-\tab $\hat y_i = \hat t_i$ \\
71-\afor $i = \hat m + 1, \ldots, \hat n$:\\
72-\tab $\hat y_i = \hat f_i(\seq{\hat y_j}_{j\in\hat\mI_i})$ \\
73-$\frac{d z}{d v} = \hat y_{\hat n}$
74 \caption{Program to compute $\frac{d z}{d v}$ produced by forward mode AD}
75 \label{ufl:alg:forwardadprogram}
76+\begin{algorithmic}[1]
77+\For{$i \gets 1, \ldots, \hat m$}
78+\State $\hat y_i := \hat t_i$
79+\EndFor
80+\For{$i \gets \hat m + 1, \ldots, \hat n$}
81+\State $\hat y_i := \hat f_i(\seq{\hat y_j}_{j\in\hat\mI_i})$
82+\EndFor
83+\State $\frac{d z}{d v} := \hat y_{\hat n}$
84+\end{algorithmic}
85 \end{algorithm}
86
87 Since the program in Algorithm~\ref{ufl:alg:program} can be
88@@ -2025,14 +2034,15 @@
89 is somewhat different from the patterns used in a statically typed
90 language such as C++.
91
92-One way to implement type based operation selection is to use a type
93-switch, or a sequence of if-tests such as this:
94+One way to implement type based operation selection is to use a
95+\emph{type switch}, which is a sequence of if-tests as shown here:
96 \begin{python}
97-if isinstance(expression, IntValue):
98- result = int_operation(expression)
99-elif isinstance(expression, Sum):
100- result = sum_operation(expression)
101-# etc.
102+def operation(expression):
103+ if isinstance(expression, IntValue):
104+ return int_operation(expression)
105+ elif isinstance(expression, Sum):
106+ return sum_operation(expression)
107+ # etc.
108 \end{python}
109 There are several problems with this approach, one of which is
110 efficiency when there are many types to check. A type based function
111@@ -2053,14 +2063,7 @@
112 the closest superclass handler function is used instead. Note that
113 the \emp{MultiFunction} implementation is specialized to types in the
114 \emp{Expr} class hierarchy. The declaration and use of a multifunction
115-is illustrated in Figure~\ref{ufl:fig:examplefunction}. Note that
116-\emp{argument} and \emp{sum} will handle instances of the exact types
117-\emp{Argument} and \emp{Sum}, while \emp{terminal} and \emp{operator}
118-will handle the types \emp{SpatialCoordinate} and \emp{Product} since
119-they have no specific handlers.
120-
121-\begin{figure}
122-\bwfig
123+is illustrated in this example code:
124 \begin{python}
125 class ExampleFunction(MultiFunction):
126 def __init__(self):
127@@ -2088,9 +2091,11 @@
128 print m(x[0] + x[1])
129 print m(x[0] * x[1])
130 \end{python}
131-\caption{Example declaration and use of a multifunction.}
132-\label{ufl:fig:examplefunction}
133-\end{figure}
134+Note that \emp{argument} and \emp{sum} will handle instances of the exact types
135+\emp{Argument} and \emp{Sum}, while \emp{terminal} and \emp{operator}
136+will handle the types \emp{SpatialCoordinate} and \emp{Product} since
137+they have no specific handlers.
138+
139 %------------------------------------------------------------
140 \subsection{Implementing expression transformations}
141 \label{ufl:sec:transformer}

Subscribers

People subscribed via source and target branches