Vorlesung vom 01.12.2022
© 2022 Prof. Dr. Rüdiger W. Braun
from sympy import *
init_printing()
Wenn identische Daten an mehreren Stellen gehalten werden, dann
ist eine Form von DRY. Die besondere Gefahr ist, dass Änderungen am Input zu Änderungen am Output führen, die eine neuerliche Kopieroperation erorderlich machen, welche aber vergessen wird
Bei der Klausur ist copy-and-paste unzulässig. Wir löschen alle Ausgaben und lassen das Skript laufen. Eingaben, die sich nur durch Kopieren einer Ausgabe erklären lassen, werden nicht berücksichtigt.
x = Symbol('x')
f = 1/(1-x**2)**Rational(3,2)
I1 = Integral(f, x)
I1
F = I1.doit()
F
F.diff(x).simplify()
Nur der zweite Fall interessiert uns
print(F)
Piecewise((-I*x/sqrt(x**2 - 1), Abs(x**2) > 1), (x/sqrt(1 - x**2), True))
F.subs(Abs(x**2)>1, False)
g = (x-2.4)**2*sqrt(x)*0.1875
g
intervall = (x, 0, 4)
plot(g, intervall);
Integral(g, intervall).doit()
Integral(g, intervall).n()
Moral: Wenn ich symbolisch integrieren will, darf ich keine Fließkommazahlen verwenden
Rational(pi.n())
Rational(0.1875)
g = 3*(x-Rational(12,5))**2*sqrt(x)/16
Integral(g, intervall).doit()
Integral(g, intervall).doit().n()
Ist natürlich trotzdem ein Bug
n, m = symbols('n m')
S1 = Sum(n, (n,1,m))
S1
sum
mit kleinem s ist nicht symbolisch
S1.doit()
a = (-1)**n * x**(2*n+1) / factorial(2*n+1)
S2 = Sum(a, (n,0,oo))
S2
S2.doit()
q = S('q')
S3 = Sum(q**n, (n,0,oo))
S3
S3.doit()
S3.doit().subs(Abs(q)<1, True)
S4 = Sum(1/n**2, (n, 1, m))
S4
S4.doit()
S4.subs(m, oo).doit()
Sum(1/n**54, (n, 1, oo)).doit()
Sum(1/n**3, (n, 1, oo)).doit()
f = zeta(Rational(1,2) + I*x)
f
plot(abs(f), (x, -1, 26));
s = Rational(1,2) + 15*I
S5 = Sum(1/n**s, (n, 1, oo))
S5
S5.doit() # TypeError, die komplexe Reihe divergiert
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Input In [39], in <cell line: 1>() ----> 1 S5.doit() File ~\anaconda3\lib\site-packages\sympy\concrete\summations.py:234, in Sum.doit(self, **hints) 232 if reps: 233 undo = {v: k for k, v in reps.items()} --> 234 did = self.xreplace(reps).doit(**hints) 235 if isinstance(did, tuple): # when separate=True 236 did = tuple([i.xreplace(undo) for i in did]) File ~\anaconda3\lib\site-packages\sympy\concrete\summations.py:263, in Sum.doit(self, **hints) 261 if newf is None: 262 if f == self.function: --> 263 zeta_function = self.eval_zeta_function(f, (i, a, b)) 264 if zeta_function is not None: 265 return zeta_function File ~\anaconda3\lib\site-packages\sympy\concrete\summations.py:293, in Sum.eval_zeta_function(self, f, limits) 291 s = result[z] 292 q = result[y] / result[w] + a --> 293 return Piecewise((coeff * zeta(s, q), And(q > 0, s > 1)), (self, True)) File ~\anaconda3\lib\site-packages\sympy\core\decorators.py:236, in _SympifyWrapper.make_wrapped.<locals>._func(self, other) 234 if not isinstance(other, expectedcls): 235 return retval --> 236 return func(self, other) File ~\anaconda3\lib\site-packages\sympy\core\expr.py:365, in Expr.__gt__(self, other) 362 @sympify_return([('other', 'Expr')], NotImplemented) 363 def __gt__(self, other): 364 from .relational import StrictGreaterThan --> 365 return StrictGreaterThan(self, other) File ~\anaconda3\lib\site-packages\sympy\core\relational.py:834, in _Inequality.__new__(cls, lhs, rhs, **options) 832 for me in (lhs, rhs): 833 if me.is_extended_real is False: --> 834 raise TypeError("Invalid comparison of non-real %s" % me) 835 if me is S.NaN: 836 raise TypeError("Invalid NaN comparison") TypeError: Invalid comparison of non-real 1/2 + 15*I
Strings
"Text" == 'Text'
True
netto = 2.72
f"Bruttopreis = {netto}€ plus {netto*0.16}€ Mehrwertsteuer"
'Bruttopreis = 2.72€ plus 0.43520000000000003€ Mehrwertsteuer'
f"Bruttopreis = {netto}€ plus {netto*0.16:.2f}€ Mehrwertsteuer"
'Bruttopreis = 2.72€ plus 0.44€ Mehrwertsteuer'
.2f (2 Nachkommastellen)
8.2f (2 Nachkommastellen, insgesamt 8 Zeichen inkl. eventueller Leerzeichen)
r = Symbol('r', positive=True)
phi = Symbol('phi', real=True)
x = r*cos(phi)
y = r*sin(phi)
x, y
r == simplify(sqrt(x**2+y**2))
True
phi == atan(y/x)
False
ers1 = {r:1, phi: pi/3}
x1 = x.subs(ers1)
y1 = y.subs(ers1)
x1, y1
atan(y1/x1)
ers2 = {r:1, phi: 4*pi/3}
x2 = x.subs(ers2)
y2 = y.subs(ers2)
x2, y2
atan(y2/x2)
atan2(y1, x1)
atan2(y2, x2)
x, y = symbols('x y')
I1 = Integral(x**Rational(2,3), (x,-8,8))
I1
I1.doit()
Das Problem geschickt zurückgespielt 😉
Was ist $(-8)^\frac23$ ?
(-8)**Rational(2,3)
((-8)**Rational(2,3)).expand(complex=True)
(((-8)**Rational(1,3))**2).expand(complex=True)
((-8)**2)**Rational(1,3)
glg = Eq(x**3, (-8)**2)
solveset(glg, x)
p = exp(log(-8)*Rational(2,3))
p
expand(p, complex=True)
s = Symbol('s')
q = exp(log(-8+I*s)*Rational(2,3))
q
q.limit(s, 0, dir='+')
q.limit(s, 0, dir='+').expand(complex=True)
q.limit(s, 0, dir='-')
q.limit(s, 0, dir='-').expand(complex=True)
Die Funktion $e^{\frac23i\phi}$ ist halt nicht $2\pi$-periodisch
phi = S('phi')
h = exp(2*I*phi/3)
plot(re(h), im(h), (phi, -pi, pi));