fixed page breaking of code and exercises

This commit is contained in:
2020-12-11 23:22:40 +01:00
parent 0380b7a96f
commit 0e30a01a45
13 changed files with 362 additions and 538 deletions

View File

@@ -59,14 +59,14 @@ every opening parenthesis must be matched by a closing one or every
respective error messages are clear and the editor will point out and
highlight most syntax errors.
\begin{lstlisting}[label=syntaxerror, caption={Unbalanced parenthesis error.}]
\begin{pagelisting}[label=syntaxerror, caption={Unbalanced parenthesis error.}]
>> mean(random_numbers
|
Error: Expression or statement is incorrect--possibly unbalanced (, {, or [.
Did you mean:
>> mean(random_numbers)
\end{lstlisting}
\end{pagelisting}
\subsection{Indexing error}\label{index_error}
Second on the list of common errors are the
@@ -125,7 +125,7 @@ dimensions. The command in line 7 works due to the fact, that matlab
automatically extends the matrix, if you assign values to a range
outside its bounds.
\begin{lstlisting}[label=assignmenterror, caption={Assignment errors.}]
\begin{pagelisting}[label=assignmenterror, caption={Assignment errors.}]
>> a = zeros(1, 100);
>> b = 0:10;
@@ -136,7 +136,7 @@ outside its bounds.
>> size(a)
ans =
110 1
\end{lstlisting}
\end{pagelisting}
\subsection{Dimension mismatch error}
Similarly, some arithmetic operations are only valid if the variables
@@ -154,7 +154,7 @@ in listing\,\ref{dimensionmismatch} does not throw an error but the
result is something else than the expected elementwise multiplication.
% XXX Some arithmetic operations make size constraints, violating them leads to dimension mismatch errors.
\begin{lstlisting}[label=dimensionmismatch, caption={Dimension mismatch errors.}]
\begin{pagelisting}[label=dimensionmismatch, caption={Dimension mismatch errors.}]
>> a = randn(100, 1);
>> b = randn(10, 1);
>> a + b
@@ -171,7 +171,7 @@ result is something else than the expected elementwise multiplication.
>> size(c)
ans =
100 10
\end{lstlisting}
\end{pagelisting}
@@ -239,7 +239,7 @@ but it requires a deep understanding of the applied functions and also
the task at hand.
% XXX Converting a series of spike times into the firing rate as a function of time. Many tasks can be solved with a single line of code. But is this readable?
\begin{lstlisting}[label=easyvscomplicated, caption={One-liner versus readable code.}]
\begin{pagelisting}[label=easyvscomplicated, caption={One-liner versus readable code.}]
% the one-liner
rate = conv(full(sparse(1, round(spike_times/dt), 1, 1, length(time))), kernel, 'same');
@@ -248,7 +248,7 @@ rate = zeros(size(time));
spike_indices = round(spike_times/dt);
rate(spike_indices) = 1;
rate = conv(rate, kernel, 'same');
\end{lstlisting}
\end{pagelisting}
The preferred way depends on several considerations. (i) How deep is
your personal understanding of the programming language? (ii) What
@@ -291,7 +291,7 @@ example given in the \matlab{} help and assume that there is a
function \varcode{rightTriangle()} (listing\,\ref{trianglelisting}).
% XXX Slightly more readable version of the example given in the \matlab{} help system. Note: The variable name for the angles have been capitalized in order to not override the matlab defined functions \code{alpha, beta,} and \code{gamma}.
\begin{lstlisting}[label=trianglelisting, caption={Example function for unit testing.}]
\begin{pagelisting}[label=trianglelisting, caption={Example function for unit testing.}]
function angles = rightTriangle(length_a, length_b)
ALPHA = atand(length_a / length_b);
BETA = atand(length_a / length_b);
@@ -300,7 +300,7 @@ function angles = rightTriangle(length_a, length_b)
angles = [ALPHA BETA GAMMA];
end
\end{lstlisting}
\end{pagelisting}
This function expects two input arguments that are the length of the
sides $a$ and $b$ and assumes a right angle between them. From this
@@ -337,7 +337,7 @@ The test script for the \varcode{rightTriangle()} function
(listing\,\ref{trianglelisting}) may look like in
listing\,\ref{testscript}.
\begin{lstlisting}[label=testscript, caption={Unit test for the \varcode{rightTriangle()} function stored in an m-file testRightTriangle.m}]
\begin{pagelisting}[label=testscript, caption={Unit test for the \varcode{rightTriangle()} function stored in an m-file testRightTriangle.m}]
tolerance = 1e-10;
% preconditions
@@ -373,7 +373,7 @@ angles = rightTriangle(1, 1500);
smallAngle = (pi / 180) * angles(1); % radians
approx = sin(smallAngle);
assert(abs(approx - smallAngle) <= tolerance, 'Problem with small angle approximation')
\end{lstlisting}
\end{pagelisting}
In a test script we can execute any code. The actual test whether or
not the results match our predictions is done using the
@@ -390,16 +390,16 @@ executed. We further define a \varcode{tolerance} variable that is
used when comparing double values (Why might the test on equality of
double values be tricky?).
\begin{lstlisting}[label=runtestlisting, caption={Run the test!}]
\begin{pagelisting}[label=runtestlisting, caption={Run the test!}]
result = runtests('testRightTriangle')
\end{lstlisting}
\end{pagelisting}
During the run, \matlab{} will put out error messages onto the command
line and a summary of the test results is then stored within the
\varcode{result} variable. These can be displayed using the function
\code[table()]{table(result)}.
\begin{lstlisting}[label=testresults, caption={The test results.}, basicstyle=\ttfamily\scriptsize]
\begin{pagelisting}[label=testresults, caption={The test results.}, basicstyle=\ttfamily\scriptsize]
table(result)
ans =
4x6 table
@@ -411,7 +411,7 @@ _________________________________ ______ ______ ___________ ________ _____
'testR.../Test_IsoscelesTriangles' true false false 0.004893 [1x1 struct]
'testR.../Test_30_60_90Triangle' true false false 0.005057 [1x1 struct]
'testR.../Test_SmallAngleApprox' true false false 0.0049 [1x1 struct]
\end{lstlisting}
\end{pagelisting}
So far so good, all tests pass and our function appears to do what it
is supposed to do. But tests are only as good as the programmer who
@@ -479,11 +479,11 @@ stopped in debug mode (listing\,\ref{debuggerlisting}).
\end{figure}
\begin{lstlisting}[label=debuggerlisting, caption={Command line when the program execution was stopped in the debugger.}]
\begin{pagelisting}[label=debuggerlisting, caption={Command line when the program execution was stopped in the debugger.}]
>> simplerandomwalk
6 for run = 1:num_runs
K>>
\end{lstlisting}
\end{pagelisting}
When stopped in the debugger we can view and change the state of the
program at this point, we can also issue commands to try the next