Vorlesung vom 27.10.2022
© 2022 Prof. Dr. Rüdiger W. Braun
from sympy import *
x = S('x')
f = 5*x + 13
f
f.subs(x, 0)
x = 10
f
f.subs(x, 10)
x
10
x = S('x')
f.subs(x, 10)
x = S('x')
y = S('y')
(x+y)**2
a = (x+y)**2
a.expand()
c = (x+y)*(x-y)
c.expand()
d = (4 + 4*x + x**2)**3
expand(d)
factor(d)
alpha = (x**4 + 20*x**3 + 150*x**2 + 500*x + 625) / (x+5)**2
alpha
expand(alpha)
expand(alpha, denom=True)
cancel(alpha)
factor(alpha)
beta = cos(2*x+y)
expand(beta)
expand(beta, trig=True)
delta = (x*y)**(2+x)
expand(delta)
expand(delta, power_exp=True, force=True)
Statt force = True
verwendet man, wo möglich, besser Annahmen.
Die folgende Umformung ist korrekt, weil $2$ positiv ist.
expand((2*y)**x)
expand((2*y)**x)
expand((2*y)**x, power_base=False)
eta = cos(2*(x+1)**2+pi)
expand(eta)
expand(eta, deep=False)
eta.subs(x+1, y).expand(trig=True).subs(y, x+1)
f(x)
: Funktion f
angewandt auf Objekt x
x.m()
: Methode m
des Objekts x
angewandt auf leeres Argument
Das Bedürfnis nach objektorientierter Programmierung stammt nicht aus der Mathematik.
Speziell in sympy funktioniert oft beides
c.expand()
expand(c)
p = 1279
p
1279
p.is_prime
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) Input In [49], in <cell line: 1>() ----> 1 p.is_prime AttributeError: 'int' object has no attribute 'is_prime'
S(p).is_prime
True
isprime(p)
True
m = 2**p - 1
m
10407932194664399081925240327364085538615262247266704805319112350403608059673360298012239441732324184842421613954281007791383566248323464908139906605677320762924129509389220345773183349661583550472959420547689811211693677147548478866962501384438260291732348885311160828538416585028255604666224831890918801847068222203140521026698435488732958028878050869736186900714720710555703168729087
isprime(m)
True
?isprime
Also arbeitet isprime
probabilistisch. In der Zahlentheorie ist das nicht akzeptabel. Die Funktion isprime
von http://sagemath.org hat einen Schalter proof=True
g = (x**2-y**2)/((x-y)**2)
g
g.cancel()
g.simplify()
g.ratsimp()
h = x*x**y
h.simplify()
h.powsimp()
%%timeit
h.simplify()
4.99 ms ± 392 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
%%timeit
h.powsimp()
394 µs ± 38 µs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)
exp(log(x))
log(exp(x))
Da hat er recht: $\exp$ ist nicht injektiv
exp(2*pi*I)
1j**2
(-1+0j)
I**2
Wir kommen später zu den komplexen Zahlen zurück.
f = (sin(3*x)+cos(x)) / ((sin(2*x)**2 - cos(x)**2)*(sin(2*x)-cos(x)))
f
f.simplify()
f.expand(trig=True)
d = f.expand(trig=true, numer=True)
d
d.factor()
a = cos(pi*x) / (2*x-1)
a
a.subs(x, 1)
res = a.subs(x, Rational(1,2))
res
Not a Number
a.limit(x, Rational(1,2))
b = 1/(1-x)
b.limit(x,1)
b.limit(x,1,dir='-')
b.limit(x,1,dir='+-')
print(_)
zoo
exp(x).limit(x,-oo)
b*exp(x)
(b*exp(x)).limit(x,oo)
n = Symbol('n')
c = factorial(n)*exp(n)/n**n/sqrt(n)
c
L = Limit(c, n, oo) # träger Operator
L
L.doit()
Stirlingsche Formel