In [1]:
import matplotlib.pyplot as plt
import numpy as np
from sympy import *
init_printing()
x,y,z = symbols('x y z')
%matplotlib notebook

Lektion 10

Lineare Algebra II

Matrixplots

In [2]:
H = Matrix(20,20,lambda i,j : 1/(i+j+1))
In [3]:
HH = matrix2numpy(H)
plt.figure()
plt.imshow(HH.astype(float))
plt.colorbar();
In [4]:
plt.figure()
plt.imshow(np.log(HH.astype(float)))
plt.colorbar();
In [5]:
H = Matrix(10,10,lambda i,j : 1/(i+j+1))
HH = matrix2numpy(H)
plt.figure()
plt.imshow(np.log(HH.astype(float)))
plt.colorbar();
In [6]:
Hinv = H**(-1)
HHinv = matrix2numpy(Hinv)
plt.figure()
plt.imshow(np.log(HHinv.astype(float)))
plt.colorbar()
/local/schaedle/conda/envs/py36/lib/python3.6/site-packages/ipykernel_launcher.py:4: RuntimeWarning: invalid value encountered in log
  after removing the cwd from sys.path.
Out[6]:
<matplotlib.colorbar.Colorbar at 0x7fcd70785ba8>
In [7]:
fig = plt.figure(figsize=(8,3))
fig.add_subplot(121)
plt.imshow(np.log(np.abs(HHinv.astype(float))))
plt.colorbar()
fig.add_subplot(122)
plt.imshow(np.sign(HHinv.astype(float)),cmap='hot')
plt.colorbar();

Eigenwerte, Eigenvektoren, Jordannormalform

In [8]:
A = Matrix(3,3,[-4,-2,-3,5,3,3,5,2,4])
A
Out[8]:
$$\left[\begin{matrix}-4 & -2 & -3\\5 & 3 & 3\\5 & 2 & 4\end{matrix}\right]$$
In [9]:
A.eigenvals()
Out[9]:
$$\left \{ 1 : 3\right \}$$
In [10]:
A.eigenvects()
Out[10]:
$$\left [ \left ( 1, \quad 3, \quad \left [ \left[\begin{matrix}- \frac{2}{5}\\1\\0\end{matrix}\right], \quad \left[\begin{matrix}- \frac{3}{5}\\0\\1\end{matrix}\right]\right ]\right )\right ]$$
In [11]:
A.diagonalize()
---------------------------------------------------------------------------
MatrixError                               Traceback (most recent call last)
<ipython-input-11-842d3ad24c22> in <module>()
----> 1 A.diagonalize()

/local/schaedle/conda/envs/py36/lib/python3.6/site-packages/sympy/matrices/matrices.py in diagonalize(self, reals_only, sort, normalize)
   1073 
   1074         if not self.is_diagonalizable(reals_only=reals_only, clear_cache=False):
-> 1075             raise MatrixError("Matrix is not diagonalizable")
   1076 
   1077         eigenvecs = self._cache_eigenvects

MatrixError: Matrix is not diagonalizable
In [12]:
T,J = A.jordan_form()
T,J
Out[12]:
$$\left ( \left[\begin{matrix}-5 & 1 & - \frac{2}{5}\\5 & 0 & 1\\5 & 0 & 0\end{matrix}\right], \quad \left[\begin{matrix}1 & 1 & 0\\0 & 1 & 0\\0 & 0 & 1\end{matrix}\right]\right )$$
In [13]:
T*J*T.inv() == A
Out[13]:
True
In [18]:
v = T[:,2]
v, A*v
Out[18]:
$$\left ( \left[\begin{matrix}- \frac{2}{5}\\1\\0\end{matrix}\right], \quad \left[\begin{matrix}- \frac{2}{5}\\1\\0\end{matrix}\right]\right )$$

Berechung des Rangs

In [19]:
M = Matrix(3,3,range(1,10))
M
Out[19]:
$$\left[\begin{matrix}1 & 2 & 3\\4 & 5 & 6\\7 & 8 & 9\end{matrix}\right]$$
In [21]:
M.rank()
Out[21]:
$$2$$
In [22]:
M = Matrix(3,2,[2*x+2,2*y-2,2*x+2,-2*y+2,y-1,x+1])
M
Out[22]:
$$\left[\begin{matrix}2 x + 2 & 2 y - 2\\2 x + 2 & - 2 y + 2\\y - 1 & x + 1\end{matrix}\right]$$
In [23]:
M.rank()
Out[23]:
$$2$$
In [24]:
M.row_op(1,lambda r,j: r-M[0,j])
M1 = M.copy()
M1
Out[24]:
$$\left[\begin{matrix}2 x + 2 & 2 y - 2\\0 & - 4 y + 4\\y - 1 & x + 1\end{matrix}\right]$$
In [27]:
M1.row_op(0,lambda r,j: r*(y-1))    # falls y != 1
M2 = M1.copy()
M2
Out[27]:
$$\left[\begin{matrix}\left(2 x + 2\right) \left(y - 1\right)^{3} & \left(y - 1\right)^{3} \left(2 y - 2\right)\\0 & - 4 y + 4\\y - 1 & x + 1\end{matrix}\right]$$
In [28]:
M2.row_op(2,lambda r,j: r*(2*x+2))  # falls x != -1
M3 = M2.copy()
M3
Out[28]:
$$\left[\begin{matrix}\left(2 x + 2\right) \left(y - 1\right)^{3} & \left(y - 1\right)^{3} \left(2 y - 2\right)\\0 & - 4 y + 4\\\left(2 x + 2\right) \left(y - 1\right) & \left(x + 1\right) \left(2 x + 2\right)\end{matrix}\right]$$
In [29]:
M3.row_op(2,lambda r,j: r-M3[0,j])
M4 = M3.copy()
M4
Out[29]:
$$\left[\begin{matrix}\left(2 x + 2\right) \left(y - 1\right)^{3} & \left(y - 1\right)^{3} \left(2 y - 2\right)\\0 & - 4 y + 4\\- \left(2 x + 2\right) \left(y - 1\right)^{3} + \left(2 x + 2\right) \left(y - 1\right) & \left(x + 1\right) \left(2 x + 2\right) - \left(y - 1\right)^{3} \left(2 y - 2\right)\end{matrix}\right]$$
In [26]:
M4.expand()
Out[26]:
$$\left[\begin{matrix}2 x y - 2 x + 2 y - 2 & 2 y^{2} - 4 y + 2\\0 & - 4 y + 4\\0 & 2 x^{2} + 4 x - 2 y^{2} + 4 y\end{matrix}\right]$$
In [27]:
factor(M4) # Rang 2 falls x != -1 und y!=1
Out[27]:
$$\left[\begin{matrix}2 \left(x + 1\right) \left(y - 1\right) & 2 \left(y - 1\right)^{2}\\0 & - 4 \left(y - 1\right)\\0 & 2 \left(x + y\right) \left(x - y + 2\right)\end{matrix}\right]$$
In [28]:
Mx = M.copy()
Mx = Mx.subs(x,-1)
M, Mx  # Rang 2 falls x = -1 und y!=1
Out[28]:
$$\left ( \left[\begin{matrix}2 x + 2 & 2 y - 2\\0 & - 4 y + 4\\y - 1 & x + 1\end{matrix}\right], \quad \left[\begin{matrix}0 & 2 y - 2\\0 & - 4 y + 4\\y - 1 & 0\end{matrix}\right]\right )$$
In [29]:
My = M.copy()
My = My.subs(y,1)
M, My   # Rang 2 falls x != -1 und y=1
Out[29]:
$$\left ( \left[\begin{matrix}2 x + 2 & 2 y - 2\\0 & - 4 y + 4\\y - 1 & x + 1\end{matrix}\right], \quad \left[\begin{matrix}2 x + 2 & 0\\0 & 0\\0 & x + 1\end{matrix}\right]\right )$$
In [30]:
Mxy = M.copy()
M, Mxy.subs({x:-1,y:1})
Out[30]:
$$\left ( \left[\begin{matrix}2 x + 2 & 2 y - 2\\0 & - 4 y + 4\\y - 1 & x + 1\end{matrix}\right], \quad \left[\begin{matrix}0 & 0\\0 & 0\\0 & 0\end{matrix}\right]\right )$$

Normen

In [31]:
v = Matrix(1,3,[1,2,3])
v
Out[31]:
$$\left[\begin{matrix}1 & 2 & 3\end{matrix}\right]$$
In [32]:
v.norm()
Out[32]:
$$\sqrt{14}$$
In [33]:
v.norm(1)
Out[33]:
$$6$$
In [34]:
v.norm(oo)
Out[34]:
$$3$$
In [35]:
A, A.norm() # Frobenius Norm
Out[35]:
$$\left ( \left[\begin{matrix}-4 & -2 & -3\\5 & 3 & 3\\5 & 2 & 4\end{matrix}\right], \quad 3 \sqrt{13}\right )$$
In [36]:
sqrt(trace(A*A.T))
Out[36]:
$$3 \sqrt{13}$$
In [37]:
A.norm(2)
Out[37]:
$$\sqrt{\sqrt{3363} + 58}$$
In [38]:
(A*A.T).eigenvals()
Out[38]:
$$\left \{ 1 : 1, \quad - \sqrt{3363} + 58 : 1, \quad \sqrt{3363} + 58 : 1\right \}$$

Kreuzprodukt

In [39]:
w = Matrix(1,3,[1,-2,1])
v,w
Out[39]:
$$\left ( \left[\begin{matrix}1 & 2 & 3\end{matrix}\right], \quad \left[\begin{matrix}1 & -2 & 1\end{matrix}\right]\right )$$
In [40]:
z = v.cross(w)
z
Out[40]:
$$\left[\begin{matrix}8 & 2 & -4\end{matrix}\right]$$
In [41]:
w.cross(v)
Out[41]:
$$\left[\begin{matrix}-8 & -2 & 4\end{matrix}\right]$$

Reihenentwicklung (Taylor)

In [42]:
tr = tan(x).series(x,0,6)
tr.removeO()
Out[42]:
$$\frac{2 x^{5}}{15} + \frac{x^{3}}{3} + x$$
In [45]:
ts = {}
for k in range(3,10,2):
    ts[k] = (x/(1+x**2)).series(x,0,k).removeO()

for tp in ts:
    print(tp)
3
5
7
9
In [46]:
fig = plt.figure()
ax = fig.gca()
xn = np.linspace(-1,1)
for tp in ts:
    ax.plot(xn,lambdify(x,ts[tp])(xn),label='Taylorpolynom Grad {0}'.format(tp))
    
ax.plot(xn,np.tan(xn),label='tan')
plt.legend()
plt.legend(loc=4)
fig.show();
In [47]:
ar={}
for n in [4,20,60]:
    ar[n]= series(atan(x),x,0,n).removeO()
ar
Out[47]:
$$\left \{ 4 : - \frac{x^{3}}{3} + x, \quad 20 : - \frac{x^{19}}{19} + \frac{x^{17}}{17} - \frac{x^{15}}{15} + \frac{x^{13}}{13} - \frac{x^{11}}{11} + \frac{x^{9}}{9} - \frac{x^{7}}{7} + \frac{x^{5}}{5} - \frac{x^{3}}{3} + x, \quad 60 : - \frac{x^{59}}{59} + \frac{x^{57}}{57} - \frac{x^{55}}{55} + \frac{x^{53}}{53} - \frac{x^{51}}{51} + \frac{x^{49}}{49} - \frac{x^{47}}{47} + \frac{x^{45}}{45} - \frac{x^{43}}{43} + \frac{x^{41}}{41} - \frac{x^{39}}{39} + \frac{x^{37}}{37} - \frac{x^{35}}{35} + \frac{x^{33}}{33} - \frac{x^{31}}{31} + \frac{x^{29}}{29} - \frac{x^{27}}{27} + \frac{x^{25}}{25} - \frac{x^{23}}{23} + \frac{x^{21}}{21} - \frac{x^{19}}{19} + \frac{x^{17}}{17} - \frac{x^{15}}{15} + \frac{x^{13}}{13} - \frac{x^{11}}{11} + \frac{x^{9}}{9} - \frac{x^{7}}{7} + \frac{x^{5}}{5} - \frac{x^{3}}{3} + x\right \}$$
In [48]:
xn = np.linspace(-2,3,100)
fig2 = plt.figure()
ax = fig2.gca()
for tp in ar:
    ax.plot(xn,lambdify(x,ar[tp])(xn), \
            label='Taylorpolynom Grad {0}'.format(tp))
    
ax.plot(xn,np.arctan(xn),label='arctan')

ax.set_ylim(-2,2)
plt.legend()
plt.legend(loc=4)
fig2.show()
In [49]:
fig = plt.figure()
ax = fig.gca()
xn = np.linspace(-2,3,1000)
ax.plot(xn,lambdify(x,ar[60])(xn))
ax.plot(xn,np.arctan(xn))
ax.set_ylim(-2,2)
Out[49]:
$$\left ( -2, \quad 2\right )$$
In [50]:
series(atan(x),x,oo,12)
Out[50]:
$$\frac{1}{11 x^{11}} - \frac{1}{9 x^{9}} + \frac{1}{7 x^{7}} - \frac{1}{5 x^{5}} + \frac{1}{3 x^{3}} - \frac{1}{x} + \frac{\pi}{2} + \mathcal{O}\left(\frac{1}{x^{12}}; x\rightarrow \infty\right)$$
In [51]:
ai = series(atan(x),x,oo,12).removeO()
In [53]:
xn2 = np.linspace(1/2,3,500)
ax.plot(xn2,lambdify(x,ai)(xn2))
Out[53]:
[<matplotlib.lines.Line2D at 0x7f97d90f9be0>]
In [54]:
a1 = series(atan(x),x,1,12).removeO()
In [55]:
xn1 = np.linspace(-2,3)
ax.plot(xn1,lambdify(x,a1)(xn1))
Out[55]:
[<matplotlib.lines.Line2D at 0x7f97d9029470>]
In [56]:
series(ln(x)**2/(1+x)**2,x,+oo)
Out[56]:
$$- \frac{4}{x^{5}} \log^{2}{\left (\frac{1}{x} \right )} + \frac{3}{x^{4}} \log^{2}{\left (\frac{1}{x} \right )} - \frac{2}{x^{3}} \log^{2}{\left (\frac{1}{x} \right )} + \frac{1}{x^{2}} \log^{2}{\left (\frac{1}{x} \right )} + \mathcal{O}\left(\frac{1}{x^{6}} \log^{2}{\left (\frac{1}{x} \right )}; x\rightarrow \infty\right)$$
In [57]:
f = 1-cos(x**2) 
g = x*(x-sin(x))
f.series(x,0,10), g.series(x,0,10)
Out[57]:
$$\left ( \frac{x^{4}}{2} - \frac{x^{8}}{24} + \mathcal{O}\left(x^{10}\right), \quad \frac{x^{4}}{6} - \frac{x^{6}}{120} + \frac{x^{8}}{5040} + \mathcal{O}\left(x^{10}\right)\right )$$
In [58]:
p = f.series(x,0,10).removeO()
q = g.series(x,0,10).removeO()
p,q
Out[58]:
$$\left ( - \frac{x^{8}}{24} + \frac{x^{4}}{2}, \quad \frac{x^{8}}{5040} - \frac{x^{6}}{120} + \frac{x^{4}}{6}\right )$$
In [59]:
b = cancel(p/q)
b
Out[59]:
$$- \frac{210 x^{4} - 2520}{x^{4} - 42 x^{2} + 840}$$
In [60]:
b.subs(x,0)
Out[60]:
$$3$$
In [61]:
limit(f/g,x,0)
Out[61]:
$$3$$