Vorlesung vom 03.11.2022
© 2022 Prof. Dr. Rüdiger W. Braun
from sympy import *
x = S('x')
n = S('n')
f = x**n
f.diff(x)
f.diff(x).powsimp()
f.diff(x,x,x)
f.diff(x,x,x).powsimp().factor()
f.diff(x,7).powsimp().factor()
f.diff(x,0)
#f.diff() # Fehler: ValueError
g = sin(x**2)*cos(x**2)
g.diff().trigsimp()
Unterschied:
a = (cos(pi*x))/(2*x-1)
a
n = numer(a) # numer ist keine Methode
d = denom(a)
n.diff(x)
n1 = n.diff(x).subs(x, Rational(1,2))
n1
d1 = d.diff(x).subs(x, Rational(1,2))
d1
l = n1/d1
l
l == a.limit(x, Rational(1,2))
True
a = Symbol('a')
g = cos(a*x**2)
dg4 = g.diff(x, 4)
dg4
dg4.expand().collect(g)
f = exp(-a**2*x**2)
d10 = diff(f, x, 10)
d10
#d10.limit(x, oo) # NotImplementedError
aplus = Symbol('a', positive=True)
aplus
aplus == a
False
a == S('a')
True
fplus = f.subs(a, aplus)
fplus
fplus.diff(x, 10).limit(x, oo)
aplus._assumptions
{'positive': True, 'infinite': False, 'extended_nonzero': True, 'imaginary': False, 'finite': True, 'nonnegative': True, 'extended_nonnegative': True, 'commutative': True, 'extended_positive': True, 'nonpositive': False, 'extended_negative': False, 'zero': False, 'negative': False, 'nonzero': True, 'extended_nonpositive': False, 'extended_real': True, 'complex': True, 'hermitian': True, 'real': True, 'rational': None, 'integer': None, 'even': None, 'irrational': None, 'algebraic': None, 'prime': None, 'composite': None, 'odd': None, 'polar': None}
Dreiwertige Logik
True
False
None
g = x**7*cos(x)
I1 = Integral(g, x) # Warum darf diese Variable nicht I heißen?
I1
G = I1.doit()
G
G.collect(cos(x)).collect(sin(x))
G.collect([cos(x), sin(x)])
n = Symbol('n')
f = x**n
I2 = Integral(f, x)
I2
F = I2.doit()
F
F.diff(x).simplify()
m = Symbol('m', nonnegative=True)
I3 = I2.subs(n, m)
I3.doit()
delta = Symbol('delta', nonzero=True)
k = -1 + delta
k
I4 = I2.subs(n, k)
I4
I4.doit().subs(delta, k+1)
Warum klappt das nicht?
h = 1/(1+x**4)
I5 = Integral(h, x)
I5
H = I5.doit()
H
H.diff(x) == h
False
H.diff(x)
H.diff(x).ratsimp()
I6 = Integral(h, (x,-oo,oo))
I6
I6.doit()
H.limit(x, oo) - H.limit(x, -oo)
k = sqrt(exp(x)+4)
J = Integral(k, (x,0,1))
J
J_Wert = J.doit()
J_Wert
Das konnte er vor 6 Jahren noch nicht. Probe:
K = Integral(k, x).doit()
K
K.subs(x,1) - K.subs(x, 0) == J_Wert
True
K.diff(x) == k
False
K.diff(x).simplify()
Numerische Probe:
J_Wert.n()
J.n()
Das sind wirklich verschiedene Rechenwege:
%%timeit
J.doit().n()
1.12 s ± 77 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
%%timeit
J.n()
5.47 ms ± 578 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
1 == 0
False
1 != 0
True
1 <= 2
True
Die anderen Vergleichsoperatoren sind <, > und >=
Für Symbole sind die binären logischen Operatoren &
für "und" sowie |
für "oder". Die Verneinung ist ~
y = S('y')
A = x | y
A
(~A).simplify()
A.subs(x, 2<3)
A.subs(y, 3<0)
F
print(F)
Piecewise((x**(n + 1)/(n + 1), Ne(n, -1)), (log(x), True))
# ?Piecewise
g = Piecewise((0, x<-1), (1, (x>=-1) & (x<=1)), (0, x>1))
g
print(g)
Piecewise((0, x < -1), (1, x <= 1), (0, True))
g.subs(x, -2)
g.subs(x, Rational(1,2))
Die Bedingungen werden von links nach rechts (bzw. von oben nach unten) abgearbeitet. Bei der ersten zutreffenden wird ausgwertet.