english design pattern
This commit is contained in:
parent
c0dd59beb8
commit
6587725f50
@ -1,165 +1,170 @@
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
\chapter{Design pattern}
|
||||
\chapter{Design patterns}
|
||||
|
||||
\selectlanguage{ngerman}
|
||||
|
||||
Beim Programmieren sind sich viel Codes in ihrer Grundstruktur sehr
|
||||
\"ahnlich. Viele Konstrukte kommen in den verschiedensten Kontexten
|
||||
immer wieder in \"ahnlicher Weise vor. In diesem Kapitel stellen wir
|
||||
einige dieser ``Design pattern'' zusammen.
|
||||
Many code fragments are variations of some basic patterns. These
|
||||
patterns are used in many different variations in many different
|
||||
contexts. In this chapter we summarize a few of these \enterm[design
|
||||
pattern]{design patterns}.
|
||||
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
\section{for Schleifen \"uber Vektoren}
|
||||
Grundlegend ist das Iterieren \"uber den Inhalt eines Vektors mit einer \code{for}-Schleife:
|
||||
\begin{lstlisting}[caption={\varcode{for}-Schleife mit Indexen \"uber einen Vektor}]
|
||||
x = [2:3:20]; % irgendein Vektor
|
||||
for i=1:length(x) % Mit der for-Schleife "loopen" wir ueber den Vektor
|
||||
i % das ist der Index, der die Elemente des Vektors indiziert.
|
||||
x(i) % das ist der Wert des i-ten Elements des Vektors x.
|
||||
a = x(i); % die Variable a bekommt den Wert des i-ten Elements des Vektors x zugewiesen.
|
||||
% Benutze den Wert:
|
||||
\section{Looping over vector elements}
|
||||
Iterating over vector elements by means of a \mcode{for}-loop is a very commen pattern:
|
||||
\begin{lstlisting}[caption={\varcode{for}-loop for accessing vector elements by indices}]
|
||||
x = [2:3:20]; % Some vector.
|
||||
for i=1:length(x) % For loop over the indices of the vector.
|
||||
i % This is the index (an integer number)
|
||||
% to be used for accessing vector elements.
|
||||
x(i) % This is the value of the i-th element of the vector.
|
||||
a = x(i); % The value of the i-th vector element
|
||||
% is assigned to the variable a.
|
||||
% Use the value of the i-th vector element by passing it
|
||||
% as an argument to a function:
|
||||
do_something( x(i) );
|
||||
end
|
||||
\end{lstlisting}
|
||||
|
||||
Wenn in der Schleife das Ergebnis in einen Vektor gespeichert werden soll,
|
||||
sollten wir vor der Schleife schon einen Vektor f\"ur die Ergebnisse
|
||||
erstellen:
|
||||
\begin{lstlisting}[caption={\varcode{for}-Schleife zum Schreiben eines Vektors}]
|
||||
x = [1.2 2.3 2.6 3.1]; % irgendein Vektor
|
||||
y = zeros(length(x),1); % Platz fuer die Ergebnisse, genauso viele wie Loops der Schleife
|
||||
If the result of the computation within the loop are single numbers
|
||||
that are to be stored in a vector, you should create this vector with
|
||||
the right size before the loop:
|
||||
\begin{lstlisting}[caption={\varcode{for}-loop for writing a vector}]
|
||||
x = [1.2 2.3 2.6 3.1]; % Some vector.
|
||||
% Create a vector for the results, as long as the number of loops:
|
||||
y = zeros(length(x),1);
|
||||
for i=1:length(x)
|
||||
% Schreibe den Rueckgabewert der Funktion get_something an die i-te
|
||||
% Stelle von y:
|
||||
% Write the result of the computation at
|
||||
% the i-th position in the y vector:
|
||||
y(i) = get_something( x(i) );
|
||||
end
|
||||
% jetzt koennen wir den Ergebnisvektor weiter bearbeiten:
|
||||
mean(y)
|
||||
% Now the result vector can be further processed:
|
||||
mean(y);
|
||||
\end{lstlisting}
|
||||
|
||||
Die Berechnungen in der Schleife k\"onnen statt einer Zahl auch einen Vektor
|
||||
zur\"uckgeben. Wenn die L\"ange diese Vektors bekannt ist, dann kann vorher
|
||||
eine entsprechend gro{\ss}e Matrix angelegt werden:
|
||||
\begin{lstlisting}[caption={\varcode{for}-Schleife zum Schreiben von Zeilen einer Matrix}]
|
||||
x = [2:3:20]; % irgendein Vektor
|
||||
y = zeros(length(x),10); % Platz fuer die Ergebnisse
|
||||
The computation within the loop could also result in a vector of some
|
||||
length and not just a single number. If the length of this vector
|
||||
(here 10) is known beforehand, then you should create a matrix of
|
||||
appropriate size for storing the results:
|
||||
\begin{lstlisting}[caption={\varcode{for}-loop for writing rows of a matrix}]
|
||||
x = [2:3:20]; % Some vector.
|
||||
% Create space for results -
|
||||
% as many rows as loops, as many columns as needed:
|
||||
y = zeros(length(x), 10);
|
||||
for i=1:length(x)
|
||||
% Schreibe den Rueckgabewert der Funktion get_something - jetzt ein
|
||||
% Vektor mit 10 Elementen - in die i-te
|
||||
% Zeile von y:
|
||||
y(i,:) = get_something( x(i) );
|
||||
% Write the return value of the function get_something - now a
|
||||
% column vector with 10 elements - into the i-th row of the matrix y:
|
||||
y(i, :) = get_something(x(i));
|
||||
end
|
||||
% jetzt koennen wir die Ergebnismatrix weiter bearbeiten:
|
||||
% Process the results stored in matrix y:
|
||||
mean(y, 1)
|
||||
\end{lstlisting}
|
||||
|
||||
Alternativ k\"onnen die in der Schleife erzeugten Vektoren zu einem
|
||||
einzigen, durchgehenden Vektor zusammengestellt werden:
|
||||
\begin{lstlisting}[caption={\varcode{for}-Schleife zum Aneinanderh\"angen von Vektoren}]
|
||||
x = [2:3:20]; % irgendein Vektor
|
||||
y = []; % Leerer Vektor fuer die Ergebnisse
|
||||
Another possibility is that the result vectors (here of unknown size)
|
||||
need to be combined into a single large vector:
|
||||
\begin{lstlisting}[caption={\varcode{for}-loop for appending vectors}]
|
||||
x = [2:3:20]; % Some vector.
|
||||
y = []; % Empty vector for storing the results.
|
||||
for i=1:length(x)
|
||||
% Die Funktion get_something gibt einen Vektor zurueck:
|
||||
z = get_something( x(i) );
|
||||
% dessen Inhalt h\"angen wir an den Ergebnissvektor an:
|
||||
% The function get_something() returns a vector of unspecified size:
|
||||
z = get_something(x(i));
|
||||
% The content of z is appended to the result vector y:
|
||||
y = [y; z(:)];
|
||||
% z(:) stellt sicher, das wir auf jeden Fall einen Spaltenvektoren aneinanderreihen.
|
||||
% The z(:) syntax ensures that we append column-vectors.
|
||||
end
|
||||
% jetzt koennen wir dem Ergebnisvektor weiter bearbeiten:
|
||||
% Process the results stored in the vector z:
|
||||
mean(y)
|
||||
\end{lstlisting}
|
||||
|
||||
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
\section{Skalieren und Verschieben nicht nur von Zufallszahlen}
|
||||
Zufallsgeneratoren geben oft nur Zufallszahlen mit festen Mittelwerten
|
||||
und Standardabweichungen (auch Skalierungen) zur\"uck. Multiplikation
|
||||
mit einem Faktor skaliert die Standardabweichung und Addition einer Zahl
|
||||
verschiebt den Mittelwert.
|
||||
\begin{lstlisting}[caption={Skalierung von Zufallszahlen}]
|
||||
% 100 random numbers draw from a Gaussian distribution with mean 0 and standard deviation 1.
|
||||
\section{Scaling and shifting random numbers, zeros, and ones}
|
||||
Random number generators usually return random numbers of a given mean
|
||||
and standard deviation. Multiply those numbers by a factor to change their standard deviation and add a number to shift the mean.
|
||||
\begin{lstlisting}[caption={Scaling and shifting of random numbers}]
|
||||
% 100 random numbers drawn from a normal distribution
|
||||
% with mean 0 and standard deviation 1:
|
||||
x = randn(100, 1);
|
||||
|
||||
% 100 random numbers drawn from a Gaussian distribution with mean 4.8 and standard deviation 2.3.
|
||||
% 100 random numbers drawn from a normal distribution
|
||||
% with mean 4.8 and standard deviation 2.3:
|
||||
mu = 4.8;
|
||||
sigma = 2.3;
|
||||
y = randn(100, 1)*sigma + mu;
|
||||
\end{lstlisting}
|
||||
Das gleiche Prinzip ist manchmal auch sinnvoll f\"ur \code{zeros()} oder \code{ones()}:
|
||||
\begin{lstlisting}[caption={Skalierung von \varcode{zeros()} und \varcode{ones()}}]
|
||||
x = -1:0.01:2; % Vektor mit x-Werten
|
||||
|
||||
The same principle can be useful for in the context of the functions
|
||||
\mcode{zeros()} or \mcode{ones()}:
|
||||
\begin{lstlisting}[caption={Scaling and shifting of \varcode{zeros()} and \varcode{ones()}}]
|
||||
x = -1:0.01:2; % Vector of x-values for plotting
|
||||
plot(x, exp(-x.*x));
|
||||
% Plotte f\"ur die gleichen x-Werte eine Linie mit y=0.8:
|
||||
% Plot for the same x-values a horizontal line with y=0.8:
|
||||
plot(x, zeros(size(x))+0.8);
|
||||
% ... Linie mit y=0.5:
|
||||
% ... or a line with y=0.5:
|
||||
plot(x, ones(size(x))*0.5);
|
||||
\end{lstlisting}
|
||||
|
||||
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
\section{Plotten einer mathematischen Funktion}
|
||||
Eine mathematische Funktion ordnet einem beliebigen $x$-Wert einen
|
||||
$y$-Wert zu. Um eine solche Funktion zeichnen zu k\"onnen, m\"ussen
|
||||
wir eine Wertetabelle aus vielen $x$-Werten und den
|
||||
dazugeh\"origen Funktionswerten $y=f(x)$ erstellen.
|
||||
|
||||
Wir erstellen dazu einen Vektor mit geeigneten $x$-Werten, die von
|
||||
dem kleinsten bis zu dem gr\"o{\ss}ten $x$-Wert laufen, den wir
|
||||
plotten wollen. Die Schrittweite f\"ur die $x$-Werte w\"ahlen wir
|
||||
klein genug, um eine sch\"one glatte Kurve zu bekommen. F\"ur jeden
|
||||
Wert $x_i$ dieses Vektors berechnen wir den entsprechenden
|
||||
Funktionswert und erzeugen damit einen Vektor mit den $y$-Werten. Die
|
||||
Werte des $y$-Vektors k\"onnen dann gegen die Werte des $x$-Vektors
|
||||
geplottet werden.
|
||||
|
||||
Folgende Programme berechnen und plotten die Funktion $f(x)=e^{-x^2}$:
|
||||
\begin{lstlisting}[caption={Plotten einer mathematischen Funktion --- sehr ausf\"uhrlich}]
|
||||
\section{Plotting a mathematical function}
|
||||
A mathematical function $y=f(x)$ assigns to each value of $x$ a single
|
||||
$y$-value. For drawing a function with the computer we need compute a
|
||||
table of values with many $x$-values and the corresponding function
|
||||
values $y=f(x)$.
|
||||
|
||||
We first create a vector with useful $x$-values. They range from the
|
||||
smallest to the largest value we want to plot. We also need to set a
|
||||
step size that is small enough to result in a smooth plot of the
|
||||
function. We then compute for each value $x_i$ of this vector the
|
||||
corresponding function value $y_i$ and store them in a vector. We then
|
||||
can plot the values of the $y$ vector against the ones of the $x$
|
||||
vector.
|
||||
|
||||
The following scripts compute and plot the function $f(x)=e^{-x^2}$:
|
||||
\begin{lstlisting}[caption={Plotting a mathematical function --- very detailed}]
|
||||
xmin = -1.0;
|
||||
xmax = 2.0;
|
||||
dx = 0.01; % Schrittweite
|
||||
x = xmin:dx:xmax; % Vektor mit x-Werten
|
||||
y = exp(-x.*x); % keine for Schleife! '.*' fuer elementweises multiplizieren
|
||||
dx = 0.01; % Step size
|
||||
x = xmin:dx:xmax; % Vector with x-values.
|
||||
y = exp(-x.*x); % No for loop! '.*' for multiplying the vector elements.
|
||||
plot(x, y);
|
||||
\end{lstlisting}
|
||||
|
||||
\begin{lstlisting}[caption={Plotten einer mathematischen Funktion --- k\"urzer}]
|
||||
x = -1:0.01:2; % Vektor mit x-Werten
|
||||
y = exp(-x.*x); % keine for Schleife! '.*' fuer elementweises multiplizieren
|
||||
\begin{lstlisting}[caption={Plotting a mathematical function --- shorter}]
|
||||
x = -1:0.01:2;
|
||||
y = exp(-x.*x);
|
||||
plot(x, y);
|
||||
\end{lstlisting}
|
||||
|
||||
\begin{lstlisting}[caption={Plotten einer mathematischen Funktion --- sehr kompakt}]
|
||||
x = -1:0.01:2; % Vektor mit x-Werten
|
||||
\begin{lstlisting}[caption={Plotting a mathematical function --- compact}]
|
||||
x = -1:0.01:2;
|
||||
plot(x, exp(-x.*x));
|
||||
\end{lstlisting}
|
||||
|
||||
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
\section{Normierung von Histogrammen}
|
||||
Meistens sollten Histogramme normiert werden, damit sie vergleichbar
|
||||
mit anderen Histogrammen oder mit theoretischen
|
||||
Wahrscheinlichkeitsverteilungen werden.
|
||||
|
||||
Die \code{histogram()} Funktion macht das mit den entsprechenden Parametern automatisch:
|
||||
\begin{lstlisting}[caption={Probability-density-function mit der \varcode{histogram()}-Funktion}]
|
||||
x = randn(100, 1); % irgendwelche reellwertige Daten
|
||||
\section{Normalizing histograms}
|
||||
For estimating probabilities or probability densities from histograms
|
||||
we need to normalize them appropriately.
|
||||
|
||||
The \mcode{histogram()} function does this automatically with the appropriate arguments:
|
||||
\begin{lstlisting}[caption={Probability density with the \varcode{histogram()}-function}]
|
||||
x = randn(100, 1); % Some real-valued data.
|
||||
histogram(x, 'Normalization', 'pdf');
|
||||
\end{lstlisting}
|
||||
\begin{lstlisting}[caption={Probability mit der \varcode{histogram()}-Funktion}]
|
||||
x = randi(6, 100, 1); % irgendwelche integer Daten
|
||||
\begin{lstlisting}[caption={Probability mit der \varcode{histogram()}-function}]
|
||||
x = randi(6, 100, 1); % Some integer-valued data.
|
||||
histogram(x, 'Normalization', 'probability');
|
||||
\end{lstlisting}
|
||||
So geht es mit der \code{hist()}-Funktion:
|
||||
\begin{lstlisting}[caption={Probability-density-function mit der \varcode{hist()}- und \varcode{bar()}-Funktion}]
|
||||
x = randn(100, 1); % irgendwelche reellwertige Daten
|
||||
[h, b] = hist(x); % Histogram berechnen
|
||||
h = h/sum(h)/(b(2)-b(1)); % normieren zu einer Wahrscheinlichkeitsdichte
|
||||
bar(b, h); % und plotten.
|
||||
Alternatively one can normalize the histogram data as returned by the
|
||||
\code{hist()}-function manually:
|
||||
\begin{lstlisting}[caption={Probability density with the \varcode{hist()}- and \varcode{bar()}-function}]
|
||||
x = randn(100, 1); % Some real-valued data.
|
||||
[h, b] = hist(x); % Compute histogram.
|
||||
h = h/sum(h)/(b(2)-b(1)); % Normalization to a probability density.
|
||||
bar(b, h); % Plot the probability density.
|
||||
\end{lstlisting}
|
||||
\begin{lstlisting}[caption={Probability mit der \varcode{hist()}- und \varcode{bar()}-Funktion}]
|
||||
x = randi(6, 100, 1); % irgendwelche integer Daten
|
||||
[h, b] = hist(x); % Histogram berechnen
|
||||
h = h/sum(h); % normieren zu Wahrscheinlichkeiten
|
||||
bar(b, h); % und plotten.
|
||||
\begin{lstlisting}[caption={Probability with the \varcode{hist()}- and \varcode{bar()}-function}]
|
||||
x = randi(6, 100, 1); % Some integer-valued data.
|
||||
[h, b] = hist(x); % Compute histogram.
|
||||
h = h/sum(h); % Normalize to probability.
|
||||
bar(b, h); % Plot the probabilities.
|
||||
\end{lstlisting}
|
||||
|
Reference in New Issue
Block a user