Lektion 8


In [1]:
from sympy import *
init_printing()
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

Matrixplots


In [2]:
i = Symbol('i')
j = Symbol('j')

In [3]:
def hilbert_element(i,j):
    return 1/(i+j+1)

hilbert_element(i,j)


Out[3]:
$$\frac{1}{i + j + 1}$$

In [4]:
hilbert = Matrix(20, 20, hilbert_element)

In [5]:
# plt.imshow(hilbert)  # TypeError

In [6]:
plt.imshow(np.array(hilbert).astype(float), interpolation='none')
plt.colorbar();



In [7]:
plt.imshow(np.log(np.array(hilbert).astype(float)), 
           interpolation='none')
plt.colorbar();



In [8]:
hilbert = Matrix(60, 60, hilbert_element)

In [9]:
plt.imshow(np.log(np.array(hilbert).astype(float)), 
           interpolation='none')
plt.colorbar();



In [10]:
inv_hilbert = hilbert**(-1)

In [11]:
plt.imshow(np.log(np.array(inv_hilbert).astype(float)), 
           interpolation='none')
plt.colorbar();


/home/braun/miniconda3/envs/compana16/lib/python3.5/site-packages/ipykernel/__main__.py:1: RuntimeWarning: invalid value encountered in log
  if __name__ == '__main__':

Was ist das Problem?


In [14]:
fig = plt.figure(figsize=(12, 5))
fig.add_subplot(121)
plt.imshow(np.log(abs(np.array(inv_hilbert).astype(float))), 
           interpolation='none')
plt.colorbar()
fig.add_subplot(122)
plt.imshow(np.sign(np.array(inv_hilbert).astype(float)), 
           interpolation='none', cmap=plt.cm.hot)
plt.colorbar();


Als Ersatz für 3D-Plot:


In [15]:
x = Symbol('x')
y = Symbol('y')
f = cos(sqrt(x**2+y**2))
fn = lambdify((x,y), f, 'numpy')

In [16]:
x = np.linspace(-2*np.pi, 2*np.pi, 150)
y = x
X, Y = np.meshgrid(x, y)
W = fn(X, Y)
plt.imshow(W, origin='lower', cmap=plt.cm.coolwarm)
plt.colorbar();



In [17]:
extent = (x[0], x[-1], y[0], y[-1])
extent


Out[17]:
$$\left ( -6.28318530718, \quad 6.28318530718, \quad -6.28318530718, \quad 6.28318530718\right )$$

In [18]:
W[W<0] = -W[W<0]  # abs(W) wäre einfacher

In [19]:
plt.imshow(W, origin='lower', cmap=plt.cm.hot, extent=extent)
plt.colorbar();


Lineare Gleichungssysteme


In [20]:
A = Matrix(3, 3, range(1,10))
A


Out[20]:
$$\left[\begin{matrix}1 & 2 & 3\\4 & 5 & 6\\7 & 8 & 9\end{matrix}\right]$$

In [21]:
b = Matrix([6, 15, 24])
b


Out[21]:
$$\left[\begin{matrix}6\\15\\24\end{matrix}\right]$$

Wir wollen $Ax=b$ lösen.


In [22]:
x = Matrix([Symbol('x_'+str(j)) for j in [1,2,3]])
x


Out[22]:
$$\left[\begin{matrix}x_{1}\\x_{2}\\x_{3}\end{matrix}\right]$$

In [23]:
glg = Eq(A*x, b)
glg


Out[23]:
$$\left[\begin{matrix}x_{1} + 2 x_{2} + 3 x_{3}\\4 x_{1} + 5 x_{2} + 6 x_{3}\\7 x_{1} + 8 x_{2} + 9 x_{3}\end{matrix}\right] = \left[\begin{matrix}6\\15\\24\end{matrix}\right]$$

In [24]:
Lsg = solve(glg)
Lsg


Out[24]:
$$\left [ \left \{ x_{1} : x_{3}, \quad x_{2} : - 2 x_{3} + 3\right \}\right ]$$

Probe:


In [25]:
glg.subs(Lsg[0])


Out[25]:
$$\mathrm{True}$$

konkrete Lösung


In [26]:
x.subs(Lsg[0]).subs(x[2], 0)


Out[26]:
$$\left[\begin{matrix}0\\3\\0\end{matrix}\right]$$

andere konkrete Lösung


In [27]:
x.subs(Lsg[0]).subs(x[2], 1)


Out[27]:
$$\left[\begin{matrix}1\\1\\1\end{matrix}\right]$$

In [28]:
solve(A*x, x)  # Kern von A


Out[28]:
$$\left \{ x_{1} : x_{3}, \quad x_{2} : - 2 x_{3}\right \}$$

In [29]:
solve(A*x, Matrix([0,0,1]))  # unlösbar


Out[29]:
$$\left [ \right ]$$

Veränderliche und unveränderliche Matrizen


In [30]:
B = A

In [31]:
B[0,0] = 999
B


Out[31]:
$$\left[\begin{matrix}999 & 2 & 3\\4 & 5 & 6\\7 & 8 & 9\end{matrix}\right]$$

In [32]:
A


Out[32]:
$$\left[\begin{matrix}999 & 2 & 3\\4 & 5 & 6\\7 & 8 & 9\end{matrix}\right]$$

In [33]:
B = A.copy()
B[0,0] = 777
B


Out[33]:
$$\left[\begin{matrix}777 & 2 & 3\\4 & 5 & 6\\7 & 8 & 9\end{matrix}\right]$$

In [34]:
A


Out[34]:
$$\left[\begin{matrix}999 & 2 & 3\\4 & 5 & 6\\7 & 8 & 9\end{matrix}\right]$$

In [35]:
C = A[0:3, 0:3]
C


Out[35]:
$$\left[\begin{matrix}999 & 2 & 3\\4 & 5 & 6\\7 & 8 & 9\end{matrix}\right]$$

In [36]:
C[1,1] = 555
C


Out[36]:
$$\left[\begin{matrix}999 & 2 & 3\\4 & 555 & 6\\7 & 8 & 9\end{matrix}\right]$$

In [37]:
A


Out[37]:
$$\left[\begin{matrix}999 & 2 & 3\\4 & 5 & 6\\7 & 8 & 9\end{matrix}\right]$$

In [38]:
type(A)


Out[38]:
sympy.matrices.dense.MutableDenseMatrix

In [39]:
Ai = simplify(A)
type(Ai)


Out[39]:
sympy.matrices.immutable.ImmutableMatrix

In [43]:
# Ai[0,0] = 3 # TypeError, denn Ai ist ImmutableMatrix

In [44]:
B = Matrix(Ai)
B[0,0] = 333
A, B


Out[44]:
$$\left ( \left[\begin{matrix}999 & 2 & 3\\4 & 5 & 6\\7 & 8 & 9\end{matrix}\right], \quad \left[\begin{matrix}333 & 2 & 3\\4 & 5 & 6\\7 & 8 & 9\end{matrix}\right]\right )$$

In [45]:
type(Matrix(B))


Out[45]:
sympy.matrices.dense.MutableDenseMatrix

Parameterabhängige Matrizen


In [46]:
a = Symbol('a')
A = Matrix(3,3,range(1,10))
A[0,0] = 1 + a
A


Out[46]:
$$\left[\begin{matrix}a + 1 & 2 & 3\\4 & 5 & 6\\7 & 8 & 9\end{matrix}\right]$$

In [47]:
glg = Eq(A*x, b)
glg


Out[47]:
$$\left[\begin{matrix}x_{1} \left(a + 1\right) + 2 x_{2} + 3 x_{3}\\4 x_{1} + 5 x_{2} + 6 x_{3}\\7 x_{1} + 8 x_{2} + 9 x_{3}\end{matrix}\right] = \left[\begin{matrix}6\\15\\24\end{matrix}\right]$$

In [48]:
solve(glg, x)


Out[48]:
$$\left \{ x_{1} : 0, \quad x_{2} : 3, \quad x_{3} : 0\right \}$$

In [49]:
glg = Eq(A*x, Matrix([0,0,1]))
glg


Out[49]:
$$\left[\begin{matrix}x_{1} \left(a + 1\right) + 2 x_{2} + 3 x_{3}\\4 x_{1} + 5 x_{2} + 6 x_{3}\\7 x_{1} + 8 x_{2} + 9 x_{3}\end{matrix}\right] = \left[\begin{matrix}0\\0\\1\end{matrix}\right]$$

In [50]:
solve(glg, x)


Out[50]:
$$\left \{ x_{1} : \frac{1}{a}, \quad x_{2} : 2 - \frac{2}{a}, \quad x_{3} : - \frac{5}{3} + \frac{1}{a}\right \}$$

In [51]:
A.det()


Out[51]:
$$- 3 a$$

Gauß-Elimination zu Fuß


In [52]:
L, U, R = A.LUdecomposition()
L, U, R


Out[52]:
$$\left ( \left[\begin{matrix}1 & 0 & 0\\\frac{4}{a + 1} & 1 & 0\\\frac{7}{a + 1} & \frac{8 - \frac{14}{a + 1}}{5 - \frac{8}{a + 1}} & 1\end{matrix}\right], \quad \left[\begin{matrix}a + 1 & 2 & 3\\0 & 5 - \frac{8}{a + 1} & 6 - \frac{12}{a + 1}\\0 & 0 & 9 - \frac{21}{a + 1} - \frac{\left(6 - \frac{12}{a + 1}\right) \left(8 - \frac{14}{a + 1}\right)}{5 - \frac{8}{a + 1}}\end{matrix}\right], \quad \left [ \right ]\right )$$

In [53]:
L*U == A  # aber nur, weil R=[]


Out[53]:
True

In [54]:
A = Matrix(3,3,range(1,10))
A[0,0] = 1+a
b = Matrix([0,0,1])
A1 = A.row_join(b)
A1


Out[54]:
$$\left[\begin{matrix}a + 1 & 2 & 3 & 0\\4 & 5 & 6 & 0\\7 & 8 & 9 & 1\end{matrix}\right]$$

In [55]:
def func1(b, j):
    return b/A1[0,0]
A1.row_op(0, func1)
A1


Out[55]:
$$\left[\begin{matrix}1 & \frac{2}{a + 1} & \frac{3}{a + 1} & 0\\4 & 5 & 6 & 0\\7 & 8 & 9 & 1\end{matrix}\right]$$

In [56]:
def func2(b, j):
    return b - A[1,0]*A1[0,j]
A1.row_op(1, func2)
def func3(b, j):
    return b - A[2,0]*A1[0,j]
A1.row_op(2, func3)
A1


Out[56]:
$$\left[\begin{matrix}1 & \frac{2}{a + 1} & \frac{3}{a + 1} & 0\\0 & 5 - \frac{8}{a + 1} & 6 - \frac{12}{a + 1} & 0\\0 & 8 - \frac{14}{a + 1} & 9 - \frac{21}{a + 1} & 1\end{matrix}\right]$$

Das ist aber sehr fehleranfällig


In [57]:
def mult_zeile(A, zeilennr, faktor):
    B = Matrix(A)
    def func(b, j):
        return b*faktor
    B.row_op(zeilennr, func)
    return B

In [58]:
B = A.copy()
mult_zeile(B, 2, -3)


Out[58]:
$$\left[\begin{matrix}a + 1 & 2 & 3\\4 & 5 & 6\\-21 & -24 & -27\end{matrix}\right]$$

In [59]:
def add_zeile(A, quell_nr, ziel_nr, faktor):
    B = Matrix(A)  
    def func(b, j):
        return b + B[quell_nr,j]*faktor
    B.row_op(ziel_nr, func)
    return B

In [60]:
A1


Out[60]:
$$\left[\begin{matrix}1 & \frac{2}{a + 1} & \frac{3}{a + 1} & 0\\0 & 5 - \frac{8}{a + 1} & 6 - \frac{12}{a + 1} & 0\\0 & 8 - \frac{14}{a + 1} & 9 - \frac{21}{a + 1} & 1\end{matrix}\right]$$

In [61]:
A2 = mult_zeile(A1, 1, 1/A1[1,1])
A2


Out[61]:
$$\left[\begin{matrix}1 & \frac{2}{a + 1} & \frac{3}{a + 1} & 0\\0 & 1 & \frac{6 - \frac{12}{a + 1}}{5 - \frac{8}{a + 1}} & 0\\0 & 8 - \frac{14}{a + 1} & 9 - \frac{21}{a + 1} & 1\end{matrix}\right]$$

In [62]:
A3 = add_zeile(A2, 1, 2, -A2[2,1])
A3 = simplify(A3)
A3


Out[62]:
$$\left[\begin{matrix}1 & \frac{2}{a + 1} & \frac{3}{a + 1} & 0\\0 & 1 & \frac{6 a - 6}{5 a - 3} & 0\\0 & 0 & - \frac{3 a}{5 a - 3} & 1\end{matrix}\right]$$

In [63]:
A4 = mult_zeile(A3, 2, 1/A3[2,2])
A4


Out[63]:
$$\left[\begin{matrix}1 & \frac{2}{a + 1} & \frac{3}{a + 1} & 0\\0 & 1 & \frac{6 a - 6}{5 a - 3} & 0\\0 & 0 & 1 & - \frac{5 a - 3}{3 a}\end{matrix}\right]$$

In [64]:
A5 = add_zeile(A4, 2, 1, -A4[1,2])
A5


Out[64]:
$$\left[\begin{matrix}1 & \frac{2}{a + 1} & \frac{3}{a + 1} & 0\\0 & 1 & 0 & \frac{1}{a} \left(2 a - 2\right)\\0 & 0 & 1 & - \frac{5 a - 3}{3 a}\end{matrix}\right]$$

In [65]:
A6 = add_zeile(A5, 1, 0, -A5[0,1])
A7 = add_zeile(A6, 2, 0, -A6[0,2])
A7


Out[65]:
$$\left[\begin{matrix}1 & 0 & 0 & - \frac{4 a - 4}{a \left(a + 1\right)} + \frac{5 a - 3}{a \left(a + 1\right)}\\0 & 1 & 0 & \frac{1}{a} \left(2 a - 2\right)\\0 & 0 & 1 & - \frac{5 a - 3}{3 a}\end{matrix}\right]$$

In [66]:
y = simplify(A7[0:3, 3])
y


Out[66]:
$$\left[\begin{matrix}\frac{1}{a}\\2 - \frac{2}{a}\\- \frac{5}{3} + \frac{1}{a}\end{matrix}\right]$$

In [67]:
simplify(A*y) == b


Out[67]:
True

Eigenwerte


In [68]:
A = Matrix(3, 3, range(1,10))
A.eigenvals()


Out[68]:
$$\left \{ 0 : 1, \quad \frac{15}{2} + \frac{3 \sqrt{33}}{2} : 1, \quad - \frac{3 \sqrt{33}}{2} + \frac{15}{2} : 1\right \}$$

In [69]:
A.eigenvects()


Out[69]:
$$\left [ \left ( 0, \quad 1, \quad \left [ \left[\begin{matrix}1\\-2\\1\end{matrix}\right]\right ]\right ), \quad \left ( \frac{15}{2} + \frac{3 \sqrt{33}}{2}, \quad 1, \quad \left [ \left[\begin{matrix}\frac{- \frac{24}{- \frac{3 \sqrt{33}}{2} - \frac{13}{2}} + 12}{\left(- \frac{3 \sqrt{33}}{2} - \frac{13}{2}\right) \left(- \frac{9 \sqrt{33}}{8} - \frac{33}{8}\right)} - \frac{3}{- \frac{3 \sqrt{33}}{2} - \frac{13}{2}}\\- \frac{- \frac{12}{- \frac{3 \sqrt{33}}{2} - \frac{13}{2}} + 6}{- \frac{9 \sqrt{33}}{8} - \frac{33}{8}}\\1\end{matrix}\right]\right ]\right ), \quad \left ( - \frac{3 \sqrt{33}}{2} + \frac{15}{2}, \quad 1, \quad \left [ \left[\begin{matrix}- \frac{3}{- \frac{13}{2} + \frac{3 \sqrt{33}}{2}} + \frac{- \frac{24}{- \frac{13}{2} + \frac{3 \sqrt{33}}{2}} + 12}{\left(- \frac{13}{2} + \frac{3 \sqrt{33}}{2}\right) \left(- \frac{33}{8} + \frac{9 \sqrt{33}}{8}\right)}\\- \frac{- \frac{12}{- \frac{13}{2} + \frac{3 \sqrt{33}}{2}} + 6}{- \frac{33}{8} + \frac{9 \sqrt{33}}{8}}\\1\end{matrix}\right]\right ]\right )\right ]$$

In [70]:
A.eigenvects(simplify=True)


Out[70]:
$$\left [ \left ( 0, \quad 1, \quad \left [ \left[\begin{matrix}1\\-2\\1\end{matrix}\right]\right ]\right ), \quad \left ( \frac{15}{2} + \frac{3 \sqrt{33}}{2}, \quad 1, \quad \left [ \left[\begin{matrix}\frac{\frac{256}{13 + 3 \sqrt{33}} + 64}{\left(11 + 3 \sqrt{33}\right) \left(13 + 3 \sqrt{33}\right)} + \frac{6}{13 + 3 \sqrt{33}}\\\frac{\frac{64}{13 + 3 \sqrt{33}} + 16}{11 + 3 \sqrt{33}}\\1\end{matrix}\right]\right ]\right ), \quad \left ( - \frac{3 \sqrt{33}}{2} + \frac{15}{2}, \quad 1, \quad \left [ \left[\begin{matrix}\frac{6}{- 3 \sqrt{33} + 13} + \frac{\frac{256}{- 3 \sqrt{33} + 13} + 64}{\left(- 3 \sqrt{33} + 11\right) \left(- 3 \sqrt{33} + 13\right)}\\\frac{\frac{64}{- 3 \sqrt{33} + 13} + 16}{- 3 \sqrt{33} + 11}\\1\end{matrix}\right]\right ]\right )\right ]$$

In [71]:
T, J = A.diagonalize()
T = simplify(T)
T, J


Out[71]:
$$\left ( \left[\begin{matrix}1 & - \frac{1}{2} + \frac{3 \sqrt{33}}{22} & - \frac{3 \sqrt{33}}{22} - \frac{1}{2}\\-2 & \frac{1}{4} + \frac{3 \sqrt{33}}{44} & - \frac{3 \sqrt{33}}{44} + \frac{1}{4}\\1 & 1 & 1\end{matrix}\right], \quad \left[\begin{matrix}0 & 0 & 0\\0 & \frac{15}{2} + \frac{3 \sqrt{33}}{2} & 0\\0 & 0 & - \frac{3 \sqrt{33}}{2} + \frac{15}{2}\end{matrix}\right]\right )$$

In [72]:
simplify(T * J * T**(-1))


Out[72]:
$$\left[\begin{matrix}1 & 2 & 3\\4 & 5 & 6\\7 & 8 & 9\end{matrix}\right]$$