Beruflich Dokumente
Kultur Dokumente
Axel Arnold
Institut für Computerphysik
Universität Stuttgart
Wintersemester 2011/12
Was ist Programmieren?
Idee
Algorithmus
Programm
Fragestellung
• Wer wird die Klausur bestehen?
• Wieviele Studenten haben nur einen Vornamen?
• Wessen Matrikelnummer ist eine Primzahl?
Hilfe zu Python
• offizielle Homepage
http://www.python.org
• Einsteigerkurs „A Byte of Python“
http://swaroopch.com/notes/Python (englisch)
http://abop-german.berlios.de (deutsch)
• mit Programmiererfahrung „Dive into Python“
http://diveintopython.net
Nächster Befehl
Voriger Befehl
1 2 3 4
1 2 5 10 17
2 2 2
c = 5 =⇒ c = 25, a + b = 2 8 13 20
3 18 25
4 32
Wenn wahr
wahr
Nächster Schritt
Bedingung
Wenn falsch
falsch
15 15
b
b
10 10
5 5
00 5 10 15 20 00 5 10 15 20
a a
15 15
b
10 10
5 5
00 5 10 15 20 00 5 10 15 20
a a
wahr
Nächster Befehl
Voriger Befehl
Bedingung
falsch
• Führt den Block solange aus, wie die Bedingung wahr ist
• Block wird nicht ausgeführt, wenn Bedingung sofort verletzt ist:
>>> a = 6
>>> while a < 5:
... a = a + 1
... print "erhoehe a um eins"
>>> print a
6
Nächster Befehl
Schleifenblock
Voriger Befehl Bedingung
falsch
continue break
Befehl
Nächster Befehl
Voriger Befehl
Effizienz
• im Schnitt N/2 Durchläufe mit N/2 Vergleichen
=⇒ Laufzeit O(N 2 )
• Auch Worst Case N − 1 + N − 2 + ... + 1 = O(N 2 )
• Kein zusätzlicher Speicherbedarf
0 1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 17 18 19
20 21 22 23 24 25 26 27 28 29
30 31 32 33 34 35 36 37 38 39
40 41 42 43 44 45 46 47 48 49
Schritt 1
Schritt 2
Schritt 3
Schritt 4
Unterprogramm/Prozedur/Funktion
Effizienz
• im Schnitt log2 N-mal aufteilen, dabei N Elemente einordnen
=⇒ Laufzeit O(N log N)
• Aber Worst Case N − 1 + N − 2 + ... + 1 = O(N 2 )
import sys
line = sys.stdin.readline()
sys.stderr.write("don’t know what to do with %s\n" % line)
for i in range(10):
sys.stdout.write("%d, " % i)
sys.stdout.write("\n)
Ausgabe:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
http://www.icp.uni-stuttgart.de A. Arnold Computergrundlagen 55/88
Ein-/Ausgabe mittels raw_input
>>> passwd = raw_input("enter password to continue: ")
enter a password to continue: secret
>>> control = input("please repeat the password: ")
please repeat the password: passwd
>>> if passwd == control: print "both are the same!"
both are the same!
• Bis jetzt haben wir einen Teil der Basisfunktionalität von Python
gesehen.
• Weitere Funktionen sind in Module ausgelagert
• Manche sind nicht Teil von Python und müssen erst
nachinstalliert werden
• Die Benutzung eines installierten Moduls muss per import
angekündigt werden („Modul laden“)
• Hilfe: help(modul), alle Funktionen: dir(modul)
import sys
sys.stdout.write("running %s\n" % sys.argv[0])
line = sys.stdin.readline()
sys.stderr.write("some error message\n")
root.mainloop()
[[7, 8, 9],
[0, 1, 2]]]
• numpy.cross: Vektorkreuzprodukt
• numpy.linalg.det, .trace: Determinante und Spur
• numpy.linalg.norm, .cond: Norm und Kondition
• numpy.linalg.eig: Eigenwerte und -vektoren
• numpy.linalg.inv: Inverse einer Matrix berechnen
• numpy.linalg.cholesky, .qr, .svd: Matrixzerlegungen
• numpy.linalg.solve(A, b): Lösen von Ax = b
• Und so weiter...
http://www.icp.uni-stuttgart.de A. Arnold Computergrundlagen 78/88
Implementation
import numpy as np
def backsubstitute(A, b):
rows = b.shape[0] # length of the problem
x = np.zeros(rows) # solution, same size as b
for i in range(1, rows + 1): # looping the rows reversel
row = rows - i
x[row] = b[row] - np.dot(A[row,row+1:], x[row+1:])
x[row] /= A[row,row]
return x
A = np.array([[1, 2, 3], [0, 4, 5], [0, 0, 6]])
b = np.array([1, 2, 3])
print backsubstitute(A, b), np.linalg.solve(A, b)
Ausgabe
[-0.25 -0.125 0.5 ] [-0.25 -0.125 0.5 ]
• Arithmetischer Durchschnitt
Xlen(z)
hzi = zi /len(z)
i=1
• Varianz
σ(z) = (z − hzi)2
>>> zz = np.random.normal(0,1,100000)
>>> werte, rand = np.histogram(zz, bins=100, normed=True)
... 30000
25000
x = np.arange(0, 2*np.pi, 0.01) 20000
y = np.sin(x) 15000
5000
pyplot.text(1, -0.5, "sin(2∗pi∗x)")
0 10 5 0 5 10
pyplot.show()
5
• Oder mehrere plot-Befehle 0.5
10
• Mehrere Graphen in einem Bild
mit Hilfe von add_subplot 1.0 4 3 2 1 0 1 2 3 4 15 4 3 2 1 0 1 2 3 4
... 0.2
0.5 0.0 0.5 0.5
0.0
0.5
0.00.0 0.2 0.4 0.6 0.8 1.0
bild = pyplot.figure() 1.0 Oberflaeche