Vorlesung vom 17.11.2022
© 2022 Prof. Dr. Rüdiger W. Braun
from sympy import *
init_printing()
def f(x):
return x**2 + 3*sin(x) + 15 # Funktionskörper abgesetzt (wie Schleifenkörper)
f(1)
f(1.)
f(pi/2)
y = S('y')
f(y)
Duck typing:
“When I see a bird that walks like a duck and swims like a duck and quacks like a duck, I call that bird a duck.”
– James Whitcomb Riley
def fakultaet(n):
res = 1
for j in range(1, n+1):
res *= j
return res
fakultaet(3)
fakultaet(120) / factorial(120)
sympy | Python | |
---|---|---|
Definition | als Ausdruck f |
def f(x) |
Auswertung | f.subs(x, ...) |
f(x) |
mathematische Manipulationen | ja | nein |
beliebige Argumente | nein | ja |
Nebeneffekte | nein | ja |
Verwandlung Python-Funktion in Ausdruck durch Auswertung
def f(x): # x ist lokal
return cos(x**2)
y = Symbol('y')
f(y)
Verwandlung von Ausdruck in Funktion
zum Zwecke der numerischen Auswertung
p = y**2/2
f = lambdify(y, p)
f
<function _lambdifygenerated(y)>
f(1)
f(pi)
p
plot(p)
<sympy.plotting.plot.Plot at 0x18cc9b3d9a0>
p1 = plot(p, 1 - p, (y,-sqrt(2),sqrt(2)), show=False)
p1.legend = True
p1.show()
Wenn das nicht reicht, verwenden wir matplotlib
x = S('x')
glg = Eq(x**2, 2)
glg
lsg = solve(glg)
lsg
Probe:
lsg_dict = solve({glg}) # diese Form ist besser für die Probe
lsg_dict
lsd = solve(glg, dict=True)
lsd
for l in lsg_dict:
display(glg.subs(l))
Die Fibonacci-Zahlen sind von der Form $f_n = aA^n + bB^n$. Wir bestimmen die Unbekannten.
a, A, b, B = symbols("a A b B")
def g(n):
return a*A**n + b*B**n
gls = set()
for n in range(1,5):
glg = Eq(fibonacci(n), g(n))
gls.add(glg)
gls
lsg = solve(gls)
lsg
g(n).subs(lsg[0])
$n$ ist aus der Schleife herausgesickert. ($n$ has leaked from the loop)
n = S('n')
g(n).subs(lsg[0])
for nn in range(10, 14):
print(fibonacci(nn), g(nn).subs(lsg[0]).expand())
55 55 89 89 144 144 233 233
Probe
for nn in range(1,35):
if fibonacci(nn) != g(nn).subs(lsg[0]):
raise ValueError(f"ungleich für n={nn}")
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) Input In [40], in <cell line: 1>() 1 for nn in range(1,35): 2 if fibonacci(nn) != g(nn).subs(lsg[0]): ----> 3 raise ValueError(f"ungleich für n={nn}") ValueError: ungleich für n=1
for nn in range(1,35):
if fibonacci(nn) != g(nn).subs(lsg[0]).expand():
raise ValueError(f"ungleich für n={nn}")
No news is good news.
Wir prüfen jetzt noch die Rekursionsgleichung $f_{n+2} = f_{n+1} + f_n$
def G(n):
return g(n).subs(lsg[0])
tmp = G(n+2)-G(n+1)-G(n)
tmp
tmp.expand()
p = S('p')
q = S('q')
f = x**2 + p*x + q
glg = Eq(f, 0)
glg
solve(glg)
solve(glg, {x})
Für welche Werte von $p$ und $q$ hat $f$ eine doppelte Nullstelle?
glg2 = Eq(f.diff(x), 0)
glg2
solve({glg, glg2}, {x,q})
Für beliebiges $p$ und $q = \frac{p^2}4$ ist $x=-\frac p2$ doppelte Nullstelle von $f$.
solve
¶glg = Eq(x**4, 16)
solve(glg)
y = Symbol('y', positive=True)
solve(glg.subs(x, y))
auch wenn man es nicht erwartet
I
I**2
exp(I*pi/2)
((I+1)**3).expand()
re(3+4*I)
im(3+4*I)
conjugate(3+4*I)
re(exp(I*x))
x = Symbol('x', real=True)
re(exp(I*x))
b = I*(I**Rational(3,7) - I**Rational(-3,7))
b
$b$ ist reell
b.simplify()
b.expand(complex=True)
glg = Eq(exp(x), E)
solve({glg})
Es gibt aber noch mehr Lösungen
n = Symbol('n', integer=True)
glg.subs(x, 1 + 2*n*pi*I).simplify()
lsg_cplx = solveset(glg)
lsg_cplx
for (j, z) in zip(range(5), lsg_cplx):
display(z)
list(zip([1,2,3], ['a', 'b', 'c']))
[(1, 'a'), (2, 'b'), (3, 'c')]
solveset(glg, domain=Reals)
Andere Wahlen für domain: Naturals, Naturals0, Integers, Complexes (Voreinstellung)