Lektion 8

Vereinfachungen (simplify)

In [1]:
from sympy import *
init_printing()
x,y,z,a,b,c = symbols('x y z a b c')
In [2]:
f = sin(x)**2 + cos(x)**2
f
Out[2]:
$$\sin^{2}{\left (x \right )} + \cos^{2}{\left (x \right )}$$
In [3]:
simplify(f)
Out[3]:
$$1$$
In [4]:
p= (x**3 + x**2 +x +1) / (x**2 +2* x +1)
p
Out[4]:
$$\frac{x^{3} + x^{2} + x + 1}{x^{2} + 2 x + 1}$$
In [5]:
simplify(p)
Out[5]:
$$\frac{x^{2} + 1}{x + 1}$$
In [6]:
g = exp((x-1)**2+log(c*exp(y**2)-exp(4*x))-(x+1)**2)
g
Out[6]:
$$\left(c e^{y^{2}} - e^{4 x}\right) e^{\left(x - 1\right)^{2} - \left(x + 1\right)^{2}}$$
In [7]:
simplify(g)
Out[7]:
$$c e^{- 4 x + y^{2}} - 1$$
In [8]:
q = simplify( x**2+2*x+1 )
q
Out[8]:
$$x^{2} + 2 x + 1$$

faktorisieren (factor)

In [9]:
h = factor(q)
h
Out[9]:
$$\left(x + 1\right)^{2}$$

ausmultiplizieren (expand)

In [10]:
q = expand(h)
q
Out[10]:
$$x^{2} + 2 x + 1$$
In [11]:
f = (x+1)*(x-2)-(x-1)*x+2
f
Out[11]:
$$- x \left(x - 1\right) + \left(x - 2\right) \left(x + 1\right) + 2$$
In [12]:
expand(f)
Out[12]:
$$0$$
In [13]:
f = (x**2-y**2)/(x+y)**2
f
Out[13]:
$$\frac{x^{2} - y^{2}}{\left(x + y\right)^{2}}$$
In [14]:
factor(f)
Out[14]:
$$\frac{x - y}{x + y}$$
In [15]:
expand(f)
Out[15]:
$$\frac{x^{2}}{x^{2} + 2 x y + y^{2}} - \frac{y^{2}}{x^{2} + 2 x y + y^{2}}$$

"cancel" bringt rationale Ausdruecke in gekuerzte Standardform

In [16]:
cancel(f)
Out[16]:
$$\frac{x - y}{x + y}$$
In [17]:
g = (exp(-x)-exp(x))**3
g
Out[17]:
$$\left(- e^{x} + e^{- x}\right)^{3}$$
In [18]:
f = expand(g)
f
Out[18]:
$$- e^{3 x} + 3 e^{x} - 3 e^{- x} + e^{- 3 x}$$
In [19]:
factor(f)
Out[19]:
$$- \left(e^{x} - 1\right)^{3} \left(e^{x} + 1\right)^{3} e^{- 3 x}$$
In [20]:
f = integrate(x**2 * (exp(x)+exp(-x)),x)
f
Out[20]:
$$\left(- x^{2} - 2 x - 2\right) e^{- x} + \left(x^{2} - 2 x + 2\right) e^{x}$$
In [21]:
simplify(f)
Out[21]:
$$\left(- x^{2} - 2 x + \left(x^{2} - 2 x + 2\right) e^{2 x} - 2\right) e^{- x}$$
In [22]:
g = factor(f)
g
Out[22]:
$$\left(x^{2} e^{2 x} - x^{2} - 2 x e^{2 x} - 2 x + 2 e^{2 x} - 2\right) e^{- x}$$
In [23]:
h = expand(g)
h
Out[23]:
$$x^{2} e^{x} - x^{2} e^{- x} - 2 x e^{x} - 2 x e^{- x} + 2 e^{x} - 2 e^{- x}$$

Zusammenfassen (collect)

In [24]:
collect(h,x)
Out[24]:
$$x^{2} \left(e^{x} - e^{- x}\right) + x \left(- 2 e^{x} - 2 e^{- x}\right) + 2 e^{x} - 2 e^{- x}$$
In [25]:
collect(h,exp(x))
Out[25]:
$$\left(- x^{2} - 2 x - 2\right) e^{- x} + \left(x^{2} - 2 x + 2\right) e^{x}$$
In [26]:
collect(h,exp(x),exact=True)
Out[26]:
$$- x^{2} e^{- x} - 2 x e^{- x} + \left(x^{2} - 2 x + 2\right) e^{x} - 2 e^{- x}$$
In [27]:
collect(h,x**2,exact=True)
Out[27]:
$$x^{2} \left(e^{x} - e^{- x}\right) - 2 x e^{x} - 2 x e^{- x} + 2 e^{x} - 2 e^{- x}$$
In [28]:
k = 1+x/(x -2/(x-4/(8-x))) # Kettenbruch
k
Out[28]:
$$\frac{x}{x - \frac{2}{x - \frac{4}{- x + 8}}} + 1$$
In [29]:
cancel(k)
Out[29]:
$$\frac{2 x^{3} - 16 x^{2} + 6 x + 16}{x^{3} - 8 x^{2} + 2 x + 16}$$
In [30]:
simplify(k)
Out[30]:
$$\frac{x}{x - \frac{2}{x + \frac{4}{x - 8}}} + 1$$
In [31]:
factor(k) # das gleiche wie cancel
Out[31]:
$$\frac{2 \left(x^{3} - 8 x^{2} + 3 x + 8\right)}{x^{3} - 8 x^{2} + 2 x + 16}$$

Partialbruchzerlegung

In [32]:
h = apart(k) #Partialbruchzerlegung
h
Out[32]:
$$\frac{2 \left(x - 8\right)}{x^{3} - 8 x^{2} + 2 x + 16} + 2$$
In [33]:
together(h)
Out[33]:
$$\frac{2 \left(x^{3} - 8 x^{2} + 3 x + 8\right)}{x^{3} - 8 x^{2} + 2 x + 16}$$
In [34]:
f = log(y/x)-log(y)+log(x)
f
Out[34]:
$$\log{\left (x \right )} - \log{\left (y \right )} + \log{\left (\frac{y}{x} \right )}$$

Vereinfachung unter Annahmen (assumptions)

In [35]:
simplify(f)
Out[35]:
$$\log{\left (x \right )} - \log{\left (y \right )} + \log{\left (\frac{y}{x} \right )}$$
In [36]:
x,y = symbols('x y',positive=True)
f = log(y/x)-log(y)+log(x)
simplify(f)
Out[36]:
$$0$$
In [37]:
x,y = symbols('x y')
x.assumptions0, y.assumptions0
Out[37]:
({'commutative': True}, {'commutative': True})
In [38]:
f = sin(x)**4 - 2*sin(x)**2*cos(x)**2 + cos(x)**4
f
Out[38]:
$$\sin^{4}{\left (x \right )} - 2 \sin^{2}{\left (x \right )} \cos^{2}{\left (x \right )} + \cos^{4}{\left (x \right )}$$
In [39]:
simplify(f)
Out[39]:
$$\frac{1}{2} \cos{\left (4 x \right )} + \frac{1}{2}$$

trigsimp und powsimp

In [40]:
trigsimp(f)
Out[40]:
$$\frac{1}{2} \cos{\left (4 x \right )} + \frac{1}{2}$$
In [41]:
simplify(sinh(x)**2+cosh(x)**2), trigsimp(sinh(x)**2+cosh(x)**2)
Out[41]:
$$\left ( \cosh{\left (2 x \right )}, \quad \cosh{\left (2 x \right )}\right )$$
In [42]:
simplify(cos(x+y))
Out[42]:
$$\cos{\left (x + y \right )}$$
In [43]:
trigsimp(cos(x+y))
Out[43]:
$$\cos{\left (x + y \right )}$$
In [44]:
expand(cos(x+y))
Out[44]:
$$\cos{\left (x + y \right )}$$
In [45]:
expand_trig(cos(x+y))
Out[45]:
$$- \sin{\left (x \right )} \sin{\left (y \right )} + \cos{\left (x \right )} \cos{\left (y \right )}$$
In [46]:
expand_trig(sinh(x+y))
Out[46]:
$$\sinh{\left (x \right )} \cosh{\left (y \right )} + \sinh{\left (y \right )} \cosh{\left (x \right )}$$
In [47]:
f = expand_trig(tan(4*x))
f
Out[47]:
$$\frac{- 4 \tan^{3}{\left (x \right )} + 4 \tan{\left (x \right )}}{\tan^{4}{\left (x \right )} - 6 \tan^{2}{\left (x \right )} + 1}$$
In [48]:
trigsimp(f)
Out[48]:
$$\tan{\left (4 x \right )}$$
In [49]:
simplify(x**a*x)
Out[49]:
$$x^{a + 1}$$
In [50]:
powsimp(x**a*x**b)
Out[50]:
$$x^{a + b}$$
In [51]:
trigsimp(x**a*x*sin(x)/cos(x))
Out[51]:
$$x x^{a} \tan{\left (x \right )}$$
In [52]:
simplify(x**a*x*sin(x)/cos(x))
Out[52]:
$$x^{a + 1} \tan{\left (x \right )}$$
In [53]:
powsimp(x**a*x*sin(x)/cos(x))
Out[53]:
$$\frac{x^{a + 1} \sin{\left (x \right )}}{\cos{\left (x \right )}}$$
In [54]:
powsimp(x**a*y**a) # Uebungen
Out[54]:
$$x^{a} y^{a}$$
In [55]:
powsimp((x**a)**b)
Out[55]:
$$\left(x^{a}\right)^{b}$$

Umformungen (rewrite)

In [56]:
sin(2*x).rewrite(cot)
Out[56]:
$$\frac{2 \cot{\left (x \right )}}{\cot^{2}{\left (x \right )} + 1}$$
In [57]:
sin(2*x).rewrite(cos)
Out[57]:
$$\cos{\left (2 x - \frac{\pi}{2} \right )}$$
In [58]:
sin(2*x).rewrite(exp)
Out[58]:
$$- \frac{i}{2} \left(e^{2 i x} - e^{- 2 i x}\right)$$
In [59]:
cot(x+1).rewrite(tan)
Out[59]:
$$\frac{1}{\tan{\left (x + 1 \right )}}$$
In [60]:
tan(x).rewrite(sin)
Out[60]:
$$\frac{2 \sin^{2}{\left (x \right )}}{\sin{\left (2 x \right )}}$$
In [61]:
besselj(Rational(1,2),x).rewrite(sin)
Out[61]:
$$J_{\frac{1}{2}}\left(x\right)$$
In [62]:
plot(besselj(Rational(1,2),x)-sin(x)*sqrt(2/pi/x),(x,0,2));
<matplotlib.figure.Figure at 0x7f9a1a8656a0>

gamma(4)

In [63]:
gamma(5)
Out[63]:
$$24$$
In [64]:
f = gamma(x)*gamma(x+Rational(1,2)) -2**(1-2*x)*sqrt(pi)*gamma(2*x)
f
Out[64]:
$$- 2^{- 2 x + 1} \sqrt{\pi} \Gamma{\left(2 x \right)} + \Gamma{\left(x \right)} \Gamma{\left(x + \frac{1}{2} \right)}$$
In [65]:
simplify(f)
Out[65]:
$$0$$

Anwendungsbeispiel Kettenbruch (nicht klausurrelevant)

In [66]:
n=9
def PadeSqrtB(n):
    qb = [1]
    pb = [0]
    for ii in range(1,n+1):
        qb.append( 1 + (ii+1) % 2 )
        pb.append(z/2)
    return qb,pb
In [67]:
qb, pb = PadeSqrtB(n)
display(qb)
display(pb)
$$\left [ 1, \quad 1, \quad 2, \quad 1, \quad 2, \quad 1, \quad 2, \quad 1, \quad 2, \quad 1\right ]$$
$$\left [ 0, \quad \frac{z}{2}, \quad \frac{z}{2}, \quad \frac{z}{2}, \quad \frac{z}{2}, \quad \frac{z}{2}, \quad \frac{z}{2}, \quad \frac{z}{2}, \quad \frac{z}{2}, \quad \frac{z}{2}\right ]$$
In [68]:
Ab = {-1 : 1,
    0 : qb[0]}
Bb = {-1: 0,
    0 : 1}
Rb = {}

for i in range(1,n+1):
  Ab[i] = simplify(qb[i]*Ab[i-1]+pb[i]*Ab[i-2]);
  Bb[i] = simplify(qb[i]*Bb[i-1]+pb[i]*Bb[i-2]);
  Rb[i] = cancel(Ab[i]/Bb[i]);
In [69]:
fg = plot(sqrt(1+z),Rb[1],Rb[3],Rb[5],(z,-1,10),show=False)
fg[0].line_color='red'
fg[1].line_color='blue'
fg[2].line_color='cyan'
fg[3].line_color='orange'
fg.show()
In [70]:
h = symbols('h:{0:d}'.format(int(n/2+2)))
k = symbols('k:{0:d}'.format(int(n/2+2)))
In [71]:
qd = {0 : 0}
pd = {}
nh = int((n+1)/2)
for i in range(1, nh+1):
  qd.update({ 2*i-1: k[i]*(1+z)})
  qd.update({ 2*i : h[i]})

for i in range(1,n+2):
  pd.update({i :1})

Ad= {-1 : 1,
    0 : qd[0],
    1 : qd[1]*qd[0]+pd[1]
  }

Bd = {-1 : 0,
    0 : 1,
    1 : qd[1]
  }

Rd = {}
for i in range(2, n+2):
  Ad[i] = factor(qd[i]*Ad[i-1]+pd[i]*Ad[i-2])
  Bd[i] = factor(qd[i]*Bd[i-1]+pd[i]*Bd[i-2])
  Rd[i] = cancel(Bd[i]/Ad[i])  
In [72]:
m = 5; # ungerade Zahl kleiner als n
eq1 = collect(denom(Rd[m+1])-denom(Rb[m]),z,factor).as_poly(z).all_coeffs()
eq2 = collect(numer(Rd[m+1])-numer(Rb[m]),z,factor).as_poly(z).all_coeffs()
eq1.extend(eq2)
solve(eq1)
Out[72]:
$$\left [ \left \{ h_{1} : \frac{18}{35}, \quad h_{2} : \frac{64}{45}, \quad h_{3} : \frac{256}{63}, \quad k_{1} : \frac{1}{6}, \quad k_{2} : \frac{175}{192}, \quad k_{3} : \frac{567}{256}\right \}\right ]$$