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