Mathematisches Praktikum

Transcription

Mathematisches Praktikum
Mathematisches Praktikum
Teil 1:
Prof. Angela Kunoth / Roland Pabel
[email protected]
Teil 2:
Prof. Norbert Köckler
Übungsbetrieb
Übungen: Di 16-18 Uhr
Übungszettel: ab nächste Woche
Maximal 2 Studenten in einer Gruppe
Keine Ausgabe von Musterlösungen
Präsentation der Ergebnisse
Scheinkriterien:
Regelmäßige Anwesenheit
50% der Punkte von den Übungsaufgaben ( in beiden Teilen )
Intro
Slide 2
Wissenschaftliches Rechnen
1. Problemformulierung
•
Verstehen, Optimieren, Vorhersagen von Naturphänomenen
2. Modellierung mittels eines math./phys. Modell
•
Interaktionen abstrahieren und mittels ODEs / PDEs /
Integralgleichungen modellieren
3. Mathematische Fragen
•
Lösungsbegriff, Lösbarkeit (Existenz & Eindeutigkeit), Lösungsraum
•
Abhängigkeit der Lösung von den Rand- und Anfangswerten
4. Simulierung
•
Diskretisierung → Diskretisierungsfehler
•
Numerische Verfahren → Konsistenz, Konvergenz
5. Validierung
Intro
•
Verifikation der Exaktheit des Modells
•
Ggf. Problemformulierung abändern
Slide 3
Eigener Wissensstand
1. Studium
Semesterzahl? VD?
2. Mathe/Numerik
Vorlesungen?
3. Programmieren:
Newb -> 1 2 3 4 5 <- Pro
C, C++
1 2 3 4 5
Makefiles, CMake,...
1 2 3 4 5
awk, sed, grep, ...
4. Visualisieren:
Newb -> 1 2 3 4 5 <- Pro
Gnuplot
1 2 3 4 5
MatLab, Maple, Mathematica, ...
1 2 3 4 5
MuPAD
1 2 3 4 5
5. Publizieren:
Intro
1 2 3 4 5
Newb -> 1 2 3 4 5 <- Pro
LaTeX, BibTex
1 2 3 4 5
DVI, PostScript (PS), PDF
1 2 3 4 5
Slide 4
Inhalt des Praktikums
•
Implementation eines Programms zum ...
•
Laden,
•
Transformieren in Multiskalendarstellung,
•
Filtern und
•
Darstellen
eines Signals in C++ .
Lernziele des Praktikums
•
•
•
Multiskalenansätze in der
Signalverarbeitung
Betriebssystemunabhängiges
Programmieren mit C++ und der STL
Moderne Programmiertechniken (OO,
Design Patterns)
I. Programming
Topics:
Compiling
GCC command line usage
Makefiles
CMake
Programming Techniques:
STL
Design Patterns
GCC – Gnu Compiler Collection
Started as a GNU Project by Richard Stallman in 1985
Languages: C, C++, Java, Ada, Objective-C, Objective-C++,
Fortran, ...
Architectures: IA-32, x86_64, ARM, IA-64, ...
Debugging: GDB – GNU Debugger
Profiling: GProf – GNU Profiler
The Definite Guide to GCC, Second Edition
Programming / GCC
Slide 8
GCC – Overview
Human-readable
source code
compiler
machine-executable
binary code
#include<iostream>
int main() {
std::cout <<”hello”;
}
Programming / GCC
Slide 9
GCC – Overview (II)
Human-readable
source code
compiler
machine-executable
binary code
Two-Step Process:
Compiling (a source file):
lexical analysis, preprocessing, parsing, semantic analysis, code
generation, and code optimization
Linking (a library):
takes one or more objects generated by compilers and
assembles them into a single executable program
Programming / GCC
Slide 10
GCC – Command Line Usage
g++ ­Wall ­pg ­fno­strength­reduce ­g myprog.c ­o myprog
Options:
<file>
­p
­g
­pg
­o <file>
file to compile
enable profiling with prof
enable debugging
enable profiling with gprof (!-p -g!)
place output into file
Flags:
­f<flag>
Warnings:
­W<warning>
Programming / GCC
Slide 11
GCC – Useful Options
­###
­­help
­v
­Ox
­g0
Displays the programs and arguments that would be invoked as
the compiler executes with the specified command-line, but does
not actually execute them.
Display basic usage information
Displays the programs and arguments invoked as
the compiler executes them
enable optimization of level x = 1,2,3
disable debugging
­dumpmachine
­dumpspecs
­dumpversion
Programming / GCC
Displays the compiler's target CPU
Displays GCC's default spec strings
Displays the compiler's version number
Slide 12
GCC – Useful Flags & Warnings
­Wall
­Wundef
enable (almost) all warnings
display warning whenever an undefined identifier is
evaluated in a #if preprocessor directive
­Wsign­compare display a warning when comparing signed and unsigned
values
­Wconversion
display warning if automatics conversions would change
in the absence of a prototype
­Wpointer­arith display warning if anything depends on the size of a
function type of void
­funsafe­math­optimizations
enable additional loop optimizations for floating point
arithmetic - dangerous!
­funroll­loops unroll all loops where the number of executions is known
at compile time (can make things faster)
­fpic
generate Position-Independent-Code for use in a
shared library
Programming / GCC
Slide 13
GCC – Building a Binary (I)
main.cpp
#include ”prog.h”
int main(int argc, char** argv) {
my_function();
}
prog.cpp
#include <iostream>
void my_function() {
std::cout << ”hello” << std::endl;
}
prog.h
void my_function();
Programming / GCC
Application
Programming
Interface
Slide 14
GCC – Building a Binary (II) : Compiling
main.cpp
g++ main.cpp ­o main.o
main.o
prog.cpp
g++ prog.cpp ­o prog.o
prog.o
prog.h
Programming / GCC
Slide 15
GCC – Building a Binary (III) : Linking
main.o
Application Binary Interface
g++ main.o prog.o ­o my_prog
my_prog
prog.o
Programming / GCC
Slide 16
GCC – Compiling with a Makefile (I)
make...
... is a utility for automatically building large applications
... tracks file changes and minimizes recompilations
... is controlled by Makefiles
Problems:
Not designed with Cross-Platform in mind
Syntax can get complicated quickly
Programming / Makefile
Slide 17
GCC – Compiling with a Makefile (II)
Makefile
# Compiler, Compile Flags, Linker Flags
CC
= g++
CFLAGS
= ­Wall ­O2
LFLAGS = # default target
all:
my_prog
# Compile Rules
prog.o:
prog.cpp prog.h
$(CC) $(CFLAGS) ­c prog.cpp
main.o:
main.cpp prog.h
$(CC) $(CFLAGS) ­c main.cpp
# Link Rules
my_prog: main.o prog.o
$(CC) $(LDFLAGS) main.o prog.o ­o my_prog
Programming / Makefile
Slide 18
GCC – Compiling with a Makefile (III)
Programming / Makefile
Slide 19
Compiling with CMake (I)
Open Source
Cross-Platform (Linux, Windows, ...)
Target Build Systems: Makefiles, Microsoft Visual Studio,
Apple's Xcode, ...
Advantages:
Simple syntax
Out-Of-Source Builds
Automatic dependency generation
GUI (Windows, Linux)
http://www.cmake.org
Mastering CMake: A Cross-Plattform Build System
Programming / CMake
Slide 20
Compiling with CMake (II)
Process Structure:
Source Files
cmake
Source Files
make
Program
CMakeLists.txt
Programming / CMake
CMakeLists.txt
Makefile
Slide 21
Compiling with CMake (III)
CMakeLists.txt
# Project Name, required
PROJECT( my_prog )
# List of all source files
SET( MY_SRC_FILES main.cpp prog.cpp )
# set compile options
SET( CMAKE_CXX_FLAGS ”­O2 ­Wall” )
# add target for my program
ADD_EXECUTABLE( my_prog ${MY_SRC_FILES} )
Programming / CMake
Slide 22
Compiling with CMake (IV)
Programming / CMake
Slide 23
Why not autoconf / automake?
automake/autoconf ...
... generates Makefiles like CMake does.
... generates a configure shell script, designed to run on
almost any shell.
... not easy to understand, maintain and extend in
comparison to CMake.
... is not Cross-Platform; difficult to run on anything other
than Unix, especially Windows.
... does not support dependent options (one option depends
on some other property or selection)
Programming / autoconf/automake
Slide 24
The Standard Template Library
Part of the standard library which ships with C++
Separation of data and algorithms:
Containers → store a number of objects of a kind
Iterators → traverse a family of objects independent of the
container used; iterators are generalized pointers
Algorithms → work on sets of elements as a whole or
individual elements
Containers ↔ Iterators ↔ Algorithms
Programming / STL
Slide 25
STL – Containers
Containers:
Sequential containers:
ordered sets, every element has a unique positions
vectors, lists (doubly linked), deques (double-ended queue)
Associative containers:
sorted sets, position is determined by a sorting criteria
sets (sort by uniquely by value), maps (sort uniquely by key),
multisets (allow duplicates), multimaps (allow key
repetition)
Programming / STL
Slide 26
STL – Sequential Containers: Vector
Vector:
Replacement for C-style array
Random-access iterators (i.e. direct access by index,
arithmetic offset)
Appending and removing of elements at the end of optimal
speed
Appending and removing in the middle or beginning is not
optimal due to moving of neighboring elements
using namespace std;
vector<int> vint; // vector of integers
for( int j = 0; j <= 5; ++j ) {
vint.push_back(j);
} // vint = { 0,1,2,3,4,5 }
v[3] = ­3; // vint = { 0,1,2,­3,4,5 }
Programming / STL
Slide 27
STL – Sequential Containers: Lists
Lists:
Doubly linked list
Linear time for element access by traversing the list
Appending and removing of elements of optimal speed
Bi-directional Iterators
using namespace std;
list<char> lch; // vector of integers
for( int c = 'a'; c <= 'z'; ++c ) {
lch.push_back(c);
} // lch = { a,b,c,d,...,z }
lch.pop_front(); // lch = { b,c,d,...,z }
Programming / STL
Slide 28
STL – Sequential Containers: Deque
Deques:
Dynamic array that can grow in both directions
Linear time for element access by traversing the list
Appending and removing of elements at the beginning and the
end of optimal speed
Random-Access Iterators
using namespace std;
deque<int> dint; // deque of integers
for( int j = 0; j <= 5; ++j ) {
dint.push_front(j);
dint.push_back(j);
} // dint = { 5,4,3,2,1,0,0,1,2,3,4,5 }
Programming / STL
Slide 29
STL – Associative Container: Set
Sets:
Stores elements that are orderable by a relation operator ('<')
Elements are equal if a < b AND b < a
Bi-directional Iterators (forward & backward)
using namespace std;
set<int> sint; // set of integers
for( int j = 0; j < 6; ++j ) { sint.insert(j); }
for( int j = 0; j < 6; ++j ) { sint.insert(j); }
// sint = {0, 1, 2, 3, 4, 5}
cout << sint.count(4); // 1
set<int>::iterator it = sint.find(4);
sint.erase(it); // delete
cout << sint.count(4); // 0
Programming / STL
Slide 30
STL – Associative Container: Map
Map:
Store elements (values) that are indexed with a key which is
comparable with a relation operator ('<')
Elements are equal if a.key < b.key AND b.key < a.key
Searching has complexity O(log N)
Bi-directional Iterators (forward & backward)
using namespace std;
map<string, int> my_map;
typedef map<string, int>::value_type my_els;
my_map.insert( my_els( ”ABC”, 123 ) );
my_map.insert( my_els( ”DEF”, 456 ) );
my_map.insert( my_els( ”GHI”, 123 ) ); // will fail
map<string, int>::iterator it = my_map.begin();
cout << (*it).first; // ”123”
cout << (*it).second; // ”ABC”
Programming / STL
Slide 31
STL – Algorithms
Algorithms for
searching, finding, swapping, copying, modifying, ...
Algorithms work on containers (or ranges thereof) by using
Iterators to access elements
#include <algorithm>
vector<int> vint;
// ... vint = 2,5,4,1,6,3
vector<int>::iterator pos;
pos = min_element( vint.begin(), vint.end() ); // *pos = 1
// sort ascending
sort( vint.begin(), vint.end() );
// reverse from 2nd to 2nd last one
reserse( vint.begin()+1, vint.end()­1 );
Programming / STL
Slide 32
Design Patterns
A Design Pattern is a general technique used to solve a
class of (related) problems
Design Patterns serve a specific purpose
Knowing of them is like knowing a trick in
a mathematical proof
Holub on Patterns
Design Patterns: Elements of Reusable
Object-Oriented Software
Programming / Design Patterns
Slide 33
Design Patterns Examples
Creational Patterns:
Abstract Factory (create an object knowing only the interface)
Singleton (constrain the number of instances of a class)
Structural Patterns:
Adapter (make a class appear to support another interface)
Decorator (attach/remove features of an object at runtime)
Facade (provide a single interface for a complete subsystem)
Behavioral Patterns:
Iterator (access elements sequentially)
Command (encapsulate a unit of work into an object)
Visitor (add features to a set of elements)
Programming / Design Patterns
Slide 34
Design Pattern: Iterator
Purpose:
access the elements of an aggregate object sequentially
without exposing how the aggregation is implemented
✔
✗
✗
Promotes reuse by hiding implementation
A client may modify the elements potentially damaging the
aggregate (e.g. change the key in a sorted map)
Iterators often become undefined when elements are stored
in the container and new memory must be allocated
iterator it = container.begin();
++it; // traverse to next element
­­it; // traverse to previous element
it += 3; // random access iterator: traverse 3 elements
it2 = it + 5;
int distance = it2 ­ it;
Programming / Design Patterns
Slide 35