From bc5aebc7934a65883835509c81586a3420629dd9 Mon Sep 17 00:00:00 2001 From: Jan Grewe Date: Sat, 8 Oct 2016 16:53:21 +0200 Subject: [PATCH] [translation] programming chapter first part translated 1/6 --- programming/lecture/programming.tex | 227 ++++++++++++++-------------- 1 file changed, 113 insertions(+), 114 deletions(-) diff --git a/programming/lecture/programming.tex b/programming/lecture/programming.tex index c4159ae..baba408 100644 --- a/programming/lecture/programming.tex +++ b/programming/lecture/programming.tex @@ -1,23 +1,23 @@ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -\chapter{Programmierung in \matlab} +\chapter{Programming in \matlab} -\section{Variablen und Datentypen} +\section{Variables and data types} -\subsection{Variablen} +\subsection{Variables} -Eine \determ{Variable} ist ein Zeiger auf eine Stelle im -Speicher. Dieser Zeiger hat einen Namen, den Variablennamen, und einen -\determ{Datentyp} (Abbildung \ref{variablefig}). Im Speicher wird der -Wert der Variablen bin\"ar als eine Folge von \determ[Bit]{Bits} (0 -oder 1) gespeichert. Wird auf den Wert der Variable zugegriffen, wird -dieses Bitmuster je nach Datentyp interpretiert. Das Beispiel in -Abbildung \ref{variablefig} zeigt, dass das gleiche Bitmuster im einen -Fall als 8-Bit Integer Datentyp zur Zahl 38 interpretiert wird und im -anderen Fall als Character zum kaufm\"annischen ``und'' ausgewertet -wird. In \matlab{} sind Datentypen nicht von sehr zentraler -Bedeutung. Wir werden uns dennoch sp\"ater etwas genauer mit ihnen -befassen. +A \enterm{variable} is a pointer to a certain place in the computer's +memory. This pointer is characterized by its name, the variable's +name, and the \enterm{data type} (figure~\ref{variablefig}). In the +computer's memory the value of the variable is stored in binary form +that is as a sequence of zeros and ones (\enterm[Bit]{Bits}). When the +variable is read from the memory, this binary pattern is interpreted +according to the data type. The example shown in +figure~\ref{variablefig} shows that the very same bit pattern is either +interpreted as a 8-bit integer type (numeric value 38) or as a +ampersand (&) character. In \matlab{} data types are of only minor +importance but there are occasions where it becomes important to know +the type of a variable and we will come back to them later on. \begin{figure} \centering @@ -29,19 +29,20 @@ befassen. \includegraphics[width=.8\textwidth]{variableB} \label{variable:b} \end{subfigure} - \titlecaption{Variablen.}{Variablen sind Zeiger auf eine Adresse - im Speicher, die einen Namen und einen Datentypen beinhalten. Im - Speicher ist der Wert der Variable bin\"ar gespeichert. Abh\"angig - vom Datentyp wird dieses Bitmuster unterschiedlich - interpretiert.}\label{variablefig} + \titlecaption{Variables.}{Variables are point to a memory + address. They further are described by their name and + data type. The variable's value is stored as a pattern of binary + values (0 or 1). When reading the variable this pattern is + interpreted according to the variable's + data type.}\label{variablefig} \end{figure} -\subsection{Erzeugen von Variablen} -In \matlab{} kann eine Variable auf der Kommandozeile, in einem Skript -oder einer Funktion an beliebiger Stelle erzeugt werden. Listing -\ref{varListing1} zeigt drei Beispiele: -\begin{lstlisting}[label=varListing1, caption={Erzeugen von Variablen.}] +\subsection{Creating variables} +In \matlab{} variables can be created at any time on the command line +or any place in a script or function. Listing~\ref{varListing1} shows +three different possibilities: +\begin{lstlisting}[label=varListing1, caption={Creating variables.}] >> x = 38 x = 38 @@ -55,22 +56,22 @@ z = A \end{lstlisting} -Die Zeile 1 kann etwa so gelesen werden:''Erzeuge eine Variable mit -dem Namen \varcode{x} und weise ihr den Wert 38 zu''. Das -Gleichheitszeichen ist der sogenannte -\codeterm{Zuweisungsoperator}. Zeile 5 definiert eine Variable \varcode{y}, der -ein leerer Wert zugewiesen wird. Da \matlab{}, wenn nicht anders -angegeben, immer den \codeterm{double} Datentypen benutzt, haben beide -Variablen diesen Datentyp. In Zeile 9 wird der Variablen \varcode{z} der Buchstabe -``A'' zugewiesen. \varcode{z} ist nicht ein Flie{\ss}kommazahl von Typ \codeterm{double}, -sondern ein \codeterm{character} (Zeichen). - -Der Datentyp einer Variable kann mit \code{class()} abgefragt werden. -Eine Liste aller definierten Variablen gibt \code{who} -zur\"uck. Detailliertere Informationen \"uber Variablen zeigt -\code{whos} an. - -\begin{lstlisting}[label=varListing2, caption={Erfragen des Datentyps einer Variable, Listen aller definierten Variablen.}] +Line 1 can be read like: ``create a variable with the name \varcode{x} +and assign the value 38''. The equal sign is the so called +\codeterm{assignment operator}. Line 5 defines a variable \varcode{y} +and assigns an empty value. If not explicitly specified \matlab{} +variables will have the \codeterm{double} (a numeric data type, see +below) data type. In line 9, however, we create a variable \varcode{z} +and assign the character ``A'' to it. Accordingly, \varcode{z} does +not have the numeric \codeterm{double} data type but is of the type +\codeterm{character}. + +The actual data type of a variable can be found out with the +\code{class()} function. \code{who} prints a list of all defined +variables and \code{whos} provides detailed information +(listing~\ref{varListing2}). + +\begin{lstlisting}[label=varListing2, caption={Requesting information about defined variables and their types.}] >>class(x) ans = double @@ -88,32 +89,30 @@ x y z z 1x1 2 char \end{lstlisting} -\begin{important}[Namen von Variablen] - Bei der Namensgebung ist zu beachten, dass \matlab{} auf Gro{\ss}- - und Kleinschreibung achtet und ein Variablenname mit einem - alphabetischen Zeichen beginnen muss. Umlaute, Sonder- und - Leerzeichen sind in Variablennamen nicht erlaubt. +\begin{important}[Naming conventions] + There are a few rules regarding the variable names. \matlab{} is + case-sensitive, i.e. \code{x} and \code{X} are two different + names. Names must begin with an alphabetic character. German (or + other) umlauts, special characters and spaces are forbidden. \end{important} -\subsection{Arbeiten mit Variablen} - -Nat\"urlich kann mit den Variablen auch gearbeitet, bzw. gerechnet -werden. \matlab{} kennt alle normalen -\codeterm[Operator!arithmetischer]{arithmetischen Operatoren} wie -\code[Operator!arithmetischer!1add@+]{+}, -\code[Operator!arithmetischer!2sub@-]{-}, -\code[Operator!arithmetischer!3mul@*]{*} und -\code[Operator!arithmetischer!4div@/]{/}. Die Potenz wird \"uber das -Dachsymbol \code[Operator!arithmetischer!5pow@\^{}]{\^{}} -dargestellt. Listing \ref{varListing3} zeigt, wie sie benutzt werden. +\subsection{Working with variables} +We can certainly work, i.e. do calculations, with variables. \matlab{} +knows all basic \codeterm[Operator!arithmetic]{arithmetic operators} +such as \code[Operator!arithmetic!1add@+]{+}, +\code[Operator!arithmetic!2sub@-]{-}, +\code[Operator!arithmetic!3mul@*]{*} and +\code[Operator!arithmetic!4div@/]{/}. The power is denoted by the +\code[Operator!arithmetic!5pow@\^{}]{\^{}}. Listing~\ref{varListing3} +show their use. \pagebreak[4] -\begin{lstlisting}[label=varListing3, caption={Rechnen mit Variablen.}] +\begin{lstlisting}[label=varListing3, caption={Working with variables.}] >> x = 1; >> x + 10 ans = 11 ->> x % x wurde nicht veraendert +>> x % x has not changed! ans = 1 @@ -131,83 +130,83 @@ z = z = 15 ->> clear z % loesche die Variable z +>> clear z % deleting a variable \end{lstlisting} -Beachtenswert ist in Zeilen 2 und 6, dass mit dem Inhalt einer -Variablen gerechnet werden kann, ohne dass dadurch ihr Wert -ver\"andert wird. Wenn der Wert einer Variablen ver\"andert werden -soll, dann muss der neue Wert explizit einer Variablen zugewiesen werden -(mit dem \code[Operator!Zuweisung!=]{=} Zuweisungsoperator, -z.B. Zeilen 14 und 18). Zeile 23 zeigt wie eine einzelne Variable -gel\"oscht wird. - +Note: in lines 2 and 6 the values of the variables have been used +without changing their values. Whenever the value of a variable should +change, the \code[Operator!Assignment!=]{=} operator has to be used +(lines 14 and 18). Line 23, finally shows how to delete a variable. -\subsection{Datentypen} +\subsection{Data types} -Der Datentyp bestimmt, wie die im Speicher abgelegten Bitmuster -interpretiert werden. Die wichtigsten Datentpyen sind: +As mentioned above, the data type associated with a variable defines how the stored bit pattern is interpreted. The major data types are: \begin{itemize} -\item \codeterm{integer}: Ganze Zahlen. Hier gibt es mehrere - Unterarten, die wir in \matlab{} (meist) ignorieren k\"onnen. -\item \codeterm{double}: Flie{\ss}kommazahlen. Im Gegensatz zu den reelen Zahlen, die durch diesen Datentyp dargestellt werden, sind sie abz\"ahlbar. -\item \codeterm{complex}: Komplexe Zahlen. -\item \codeterm{logical}: Boolesche Werte, die als wahr - (\code{true}) oder falsch (\code{false}) interpretiert werden. -\item \codeterm{char}: ASCII Zeichen +\item \codeterm{integer}: Integer numbers. There are several subtypes + which, for most use-cases, can be ignored when working in \matlab{}. +\item \codeterm{double}: Floating point numbers. In contrast to the + real numbers that are represented with this data type the number of + numeric values that can be represented is limited (countable?). +\item \codeterm{complex}: Complex numbers having a real and imaginary + part. +\item \codeterm{logical}: Boolean values that can be evaluated to + \code{true} or \code{false}. +\item \codeterm{char}: ASCII characters. \end{itemize} -Unter den numerischen Datentypen gibt es verschiedene Arten mit -unterschiedlichem Speicherbedarf und Wertebreich (siehe -Tabelle~\ref{dtypestab}). +There is a variety of numeric data types that require different memory +demands and ranges of representable values (table~\ref{dtypestab}). \begin{table}[t] \centering - \titlecaption{Grundlegende numerische Datentypen und ihr Wertebereich.}{} + \titlecaption{Numeric data types and their ranges.}{} \label{dtypestab} \begin{tabular}{llcl}\hline - Datentyp & Speicherbedarf & Wertebereich & Beispiel \erh \\ \hline - \code{double} & 64 bit & $\approx -10^{308}$ bis $\approx 10^{308} $& Flie{\ss}kommazahlen.\erb\\ - \code{int} & 64 bit & $-2^{31}$ bis $2^{31}-1$ & Ganzzahlige Werte \\ - \code{int16} & 16 bit & $-2^{15}$ bis $2^{15}-1$ & Digitalisierte Spannungen. \\ - \code{uint8} & 8 bit & $0$ bis $255$ & Digitalisierte Imaging Daten. \\ \hline + Data type & memory demand & range & example \erh \\ \hline + \code{double} & 64 bit & $\approx -10^{308}$ to $\approx 10^{308} + $& Floating point numbers.\erb\\ \code{int} & 64 bit & $-2^{31}$ + to $2^{31}-1$ & Integer values. \\ \code{int16} & 16 bit & + $-2^{15}$ to $2^{15}-1$ & Digitizes measurements. \\ \code{uint8} + & 8 bit & $0$ bis $255$ & Digitized intensities of colors in + images. \\ \hline \end{tabular} \end{table} -\matlab{} arbeitet meist mit dem \codeterm{double} Datentyp wenn numerische -Daten gespeichert werden. Dennoch lohnt es sich, sich ein wenig mit -den Datentypen auseinanderzusetzen (Box \ref{daqbox}). - -\begin{ibox}[t]{\label{daqbox}Digitalisierung von Messdaten} - Szenario: Die elektrische Aktivit\"at (z.B. die Membranspannung) - einer Nervenzelle wird gemessen. Die gemessene Spannung wird mittels - Messkarte digitalisiert und auf dem Computer f\"ur weitere Analysen - gespeichert. Typischerweise k\"onnen mit solchen Messkarten - Spannungen im Bereich $\pm 10$\,V gemessen werden. Die Aufl\"osung - der Analog-Digitalwandler betr\"agt heutzutage meistens 16 bit. Das - heisst, dass der gesamte Spannungsbereich in $2^{16}$ Schritte - eingeteilt ist. Die gemessenene Spannung wird auf digitalisierte - Werte abgebildet.\vspace{0.25cm} +By default \matlab{} uses the \codeterm{double} data type whenever +numerical values have to be stored. Nevertheless there are use-cases +in which different data types are better suited. Box~\ref{daqbox} +exemplifies such a case. + +\begin{ibox}[t]{\label{daqbox}Digitizing measurements} + + Scenario: The electric activity (e.g. the membrane potential) of a + nerve cell is recorded. The measurements are digitized and stored on + the hard disk of a computer for later analysis. This is done using a + Data Acquisition system (DAQ) that converts the analog measurements + into computer digestible digital format. Typically these systems + have a working range of $\pm 10$\,V. This range is usually resolved + with a precision of 16 bit. This means that the full potential range + is mapped onto $2^{16}$ digital values.\vspace{0.25cm} \begin{minipage}{0.5\textwidth} \includegraphics[width=0.9\columnwidth]{data_acquisition} \end{minipage} \begin{minipage}{0.5\textwidth} - Um die Spannung auf den \code{int16} Datentyp abzubilden: - \[ y = x \cdot 2^{16}/20\] mit $x$ der gemessenen Spannung und $y$ - dem digitalisierten Wert bei einem Spannungsbereich von - $\pm10$\,V. Das ergibt ganzzahlige Werte zwischen $-2^{15}=-32768$ - und $2^{15}-1 = 32767$. - - Durch Umkehrung kann der digitalisierte Wert wieder - in eine Spannung zur\"uckgewandelt werden: + Mapping of the potential range onto a \code{int16} data type: + \[ y = x \cdot 2^{16}/20\] with $x$ being the measured potential and $y$ + the digitized value at a potential range of $\pm10$\,V and a + resolution of 16 bit. Resulting values are integer numbers in the + range $-2^{15}=-32768$ to $2^{15}-1 = 32767$. + + The measured potential can be calculated from the digitized value + by inverting the equation: \[ x = y \cdot 20/2^{16} \] \end{minipage}\vspace{0.25cm} - Um Speicherplatz zu sparen ist es sinnvoll, die gemessenen Daten als - \code{int16} anstelle der \code{double} Werte im Rechner abzulegen. Die - Daten als Spannungswerte, also als Flie{\ss}kommawerte, - abzulegen ben\"otigt den 4-fachen Speicherplatz (8 statt 2 Bytes) - und bietet keine zus\"atzliche Information. + In this context it is most efficient to store the measured values as + \code{int16} instead of \code{double} numbers. Storing floating + point numbers requires four times more memory (8 instead of 2 + \codeterm{Byte}, 64 instead of 16 bit) and offers no additional + information. \end{ibox}