Lektion 9

Vektoren und Matrizen

In [4]:
from sympy import *
init_printing()
In [5]:
v = Matrix([1,-1,1])
v
Out[5]:
$$\left[\begin{matrix}1\\-1\\1\end{matrix}\right]$$
In [6]:
u = Matrix(1,3,[1,2,3])
u
Out[6]:
$$\left[\begin{matrix}1 & 2 & 3\end{matrix}\right]$$
In [7]:
w = Matrix([[4,5,6]])
w
Out[7]:
$$\left[\begin{matrix}4 & 5 & 6\end{matrix}\right]$$
In [8]:
v*u
Out[8]:
$$\left[\begin{matrix}1 & 2 & 3\\-1 & -2 & -3\\1 & 2 & 3\end{matrix}\right]$$
In [9]:
u*v
Out[9]:
$$\left[\begin{matrix}2\end{matrix}\right]$$
In [10]:
xs = symbols('x:3')
xs
Out[10]:
$$\left ( x_{0}, \quad x_{1}, \quad x_{2}\right )$$
In [11]:
x = Matrix(xs)
x
Out[11]:
$$\left[\begin{matrix}x_{0}\\x_{1}\\x_{2}\end{matrix}\right]$$

transponieren

In [12]:
x.T*x
Out[12]:
$$\left[\begin{matrix}x_{0}^{2} + x_{1}^{2} + x_{2}^{2}\end{matrix}\right]$$
In [13]:
A = Matrix([[1,2],[3,4]])
A
Out[13]:
$$\left[\begin{matrix}1 & 2\\3 & 4\end{matrix}\right]$$
In [14]:
A = Matrix(3,3,range(9))
A
Out[14]:
$$\left[\begin{matrix}0 & 1 & 2\\3 & 4 & 5\\6 & 7 & 8\end{matrix}\right]$$
In [15]:
A[1,1]
Out[15]:
$$4$$

Slicing von Matrizen

In [16]:
A[:,1]
Out[16]:
$$\left[\begin{matrix}1\\4\\7\end{matrix}\right]$$
In [17]:
A[0:2,1:3]
Out[17]:
$$\left[\begin{matrix}1 & 2\\4 & 5\end{matrix}\right]$$
In [18]:
A*v
Out[18]:
$$\left[\begin{matrix}1\\4\\7\end{matrix}\right]$$
In [19]:
A*u
---------------------------------------------------------------------------
ShapeError                                Traceback (most recent call last)
<ipython-input-19-49c5415e54c8> in <module>()
----> 1 A*u

/local/schaedle/miniconda3/envs/py36/lib/python3.6/site-packages/sympy/core/decorators.py in binary_op_wrapper(self, other)
    130                     else:
    131                         return f(self)
--> 132             return func(self, other)
    133         return binary_op_wrapper
    134     return priority_decorator

/local/schaedle/miniconda3/envs/py36/lib/python3.6/site-packages/sympy/matrices/common.py in __mul__(self, other)
   2006             if self.shape[1] != other.shape[0]:
   2007                 raise ShapeError("Matrix size mismatch: %s * %s." % (
-> 2008                     self.shape, other.shape))
   2009 
   2010         # honest sympy matrices defer to their class's routine

ShapeError: Matrix size mismatch: (3, 3) * (1, 3).
In [20]:
A*x
Out[20]:
$$\left[\begin{matrix}x_{1} + 2 x_{2}\\3 x_{0} + 4 x_{1} + 5 x_{2}\\6 x_{0} + 7 x_{1} + 8 x_{2}\end{matrix}\right]$$
In [21]:
B = Matrix(3,3,[0,1,0,1,0,0,0,0,1])
B, A
Out[21]:
$$\left ( \left[\begin{matrix}0 & 1 & 0\\1 & 0 & 0\\0 & 0 & 1\end{matrix}\right], \quad \left[\begin{matrix}0 & 1 & 2\\3 & 4 & 5\\6 & 7 & 8\end{matrix}\right]\right )$$
In [22]:
A*B
Out[22]:
$$\left[\begin{matrix}1 & 0 & 2\\4 & 3 & 5\\7 & 6 & 8\end{matrix}\right]$$
In [23]:
B*A
Out[23]:
$$\left[\begin{matrix}3 & 4 & 5\\0 & 1 & 2\\6 & 7 & 8\end{matrix}\right]$$

Identitaet

In [24]:
eye(4)
Out[24]:
$$\left[\begin{matrix}1 & 0 & 0 & 0\\0 & 1 & 0 & 0\\0 & 0 & 1 & 0\\0 & 0 & 0 & 1\end{matrix}\right]$$
In [25]:
C = zeros(4)
C
Out[25]:
$$\left[\begin{matrix}0 & 0 & 0 & 0\\0 & 0 & 0 & 0\\0 & 0 & 0 & 0\\0 & 0 & 0 & 0\end{matrix}\right]$$
In [26]:
C[1,1] = 1
C
Out[26]:
$$\left[\begin{matrix}0 & 0 & 0 & 0\\0 & 1 & 0 & 0\\0 & 0 & 0 & 0\\0 & 0 & 0 & 0\end{matrix}\right]$$

Lineare Algebra

Determinate

In [27]:
A.det()
Out[27]:
$$0$$
In [28]:
A[1,1] = 5
In [29]:
A.det()
Out[29]:
$$-12$$
In [30]:
Ainv = A**(-1)
Ainv
Out[30]:
$$\left[\begin{matrix}- \frac{5}{12} & - \frac{1}{2} & \frac{5}{12}\\- \frac{1}{2} & 1 & - \frac{1}{2}\\\frac{3}{4} & - \frac{1}{2} & \frac{1}{4}\end{matrix}\right]$$
In [31]:
A*Ainv
Out[31]:
$$\left[\begin{matrix}1 & 0 & 0\\0 & 1 & 0\\0 & 0 & 1\end{matrix}\right]$$
In [32]:
Ainv*A
Out[32]:
$$\left[\begin{matrix}1 & 0 & 0\\0 & 1 & 0\\0 & 0 & 1\end{matrix}\right]$$
In [33]:
A, A.T
Out[33]:
$$\left ( \left[\begin{matrix}0 & 1 & 2\\3 & 5 & 5\\6 & 7 & 8\end{matrix}\right], \quad \left[\begin{matrix}0 & 3 & 6\\1 & 5 & 7\\2 & 5 & 8\end{matrix}\right]\right )$$
In [34]:
A[::-1,0]
Out[34]:
$$\left[\begin{matrix}6\\3\\0\end{matrix}\right]$$
In [35]:
D = diag(1,2,3)
D
Out[35]:
$$\left[\begin{matrix}1 & 0 & 0\\0 & 2 & 0\\0 & 0 & 3\end{matrix}\right]$$

Matrizen zusammenfuegen

In [36]:
AB = Matrix([A,B])
AB
Out[36]:
$$\left[\begin{matrix}0 & 1 & 2\\3 & 5 & 5\\6 & 7 & 8\\0 & 1 & 0\\1 & 0 & 0\\0 & 0 & 1\end{matrix}\right]$$
In [37]:
AB.reshape(1,len(AB))
Out[37]:
$$\left[\begin{array}{cccccccccccccccccc}0 & 1 & 2 & 3 & 5 & 5 & 6 & 7 & 8 & 0 & 1 & 0 & 1 & 0 & 0 & 0 & 0 & 1\end{array}\right]$$

Matrizen mit Iterator

In [38]:
def element(i,j):
    return 1/(i+j+1)
In [39]:
H = Matrix(3,3,element) # Hilbertmatrix
H
Out[39]:
$$\left[\begin{matrix}1 & \frac{1}{2} & \frac{1}{3}\\\frac{1}{2} & \frac{1}{3} & \frac{1}{4}\\\frac{1}{3} & \frac{1}{4} & \frac{1}{5}\end{matrix}\right]$$

charakteristisches Polynom

In [40]:
H.charpoly()
Out[40]:
$$\operatorname{PurePoly}{\left( \lambda^{3} - \frac{23 \lambda^{2}}{15} + \frac{127 \lambda}{720} - \frac{1}{2160}, \lambda, domain=\mathbb{Q} \right)}$$
In [41]:
H.det()
Out[41]:
$$\frac{1}{2160}$$

Kern und Rang

In [42]:
H.nullspace()
H.rank()
Out[42]:
$$3$$
In [43]:
CS = H.columnspace()
RS = H.rowspace()
CS
Out[43]:
$$\left [ \left[\begin{matrix}1\\\frac{1}{2}\\\frac{1}{3}\end{matrix}\right], \quad \left[\begin{matrix}\frac{1}{2}\\\frac{1}{3}\\\frac{1}{4}\end{matrix}\right], \quad \left[\begin{matrix}\frac{1}{3}\\\frac{1}{4}\\\frac{1}{5}\end{matrix}\right]\right ]$$

Gram Schmidt Orthogonalisierung

In [44]:
CSo = GramSchmidt(CS) # Orthogonalisierung
In [45]:
[CSo[i].T*CSo[j] for i in range(3) for j in range(3)]
Out[45]:
$$\left [ \left[\begin{matrix}\frac{49}{36}\end{matrix}\right], \quad \left[\begin{matrix}0\end{matrix}\right], \quad \left[\begin{matrix}0\end{matrix}\right], \quad \left[\begin{matrix}0\end{matrix}\right], \quad \left[\begin{matrix}\frac{73}{7056}\end{matrix}\right], \quad \left[\begin{matrix}0\end{matrix}\right], \quad \left[\begin{matrix}0\end{matrix}\right], \quad \left[\begin{matrix}0\end{matrix}\right], \quad \left[\begin{matrix}\frac{1}{65700}\end{matrix}\right]\right ]$$

Matrixinverse

In [46]:
Hinv = H.inv()
Hinv
Out[46]:
$$\left[\begin{matrix}9 & -36 & 30\\-36 & 192 & -180\\30 & -180 & 180\end{matrix}\right]$$
In [47]:
Hinv2 = H.inv(method="LU")
Hinv2
Out[47]:
$$\left[\begin{matrix}9 & -36 & 30\\-36 & 192 & -180\\30 & -180 & 180\end{matrix}\right]$$
In [48]:
A = Matrix([[1,-1,2],[0,0,-1],[0,2,-1]])
A
Out[48]:
$$\left[\begin{matrix}1 & -1 & 2\\0 & 0 & -1\\0 & 2 & -1\end{matrix}\right]$$
In [49]:
A, v
Out[49]:
$$\left ( \left[\begin{matrix}1 & -1 & 2\\0 & 0 & -1\\0 & 2 & -1\end{matrix}\right], \quad \left[\begin{matrix}1\\-1\\1\end{matrix}\right]\right )$$

Loesung linearer Gleichungssysteme

Gausselimination

In [50]:
x = A.LUsolve(v)
x, A*x -v
Out[50]:
$$\left ( \left[\begin{matrix}0\\1\\1\end{matrix}\right], \quad \left[\begin{matrix}0\\0\\0\end{matrix}\right]\right )$$

Loesung mit 'solve' und 'linsolve'

In [51]:
A = Matrix(3,3,range(9))
A[0,0] = 3
x = Matrix(3,1,xs)
A,v
Out[51]:
$$\left ( \left[\begin{matrix}3 & 1 & 2\\3 & 4 & 5\\6 & 7 & 8\end{matrix}\right], \quad \left[\begin{matrix}1\\-1\\1\end{matrix}\right]\right )$$
In [52]:
Eq(A*x,v)
Out[52]:
$$\left[\begin{matrix}3 x_{0} + x_{1} + 2 x_{2}\\3 x_{0} + 4 x_{1} + 5 x_{2}\\6 x_{0} + 7 x_{1} + 8 x_{2}\end{matrix}\right] = \left[\begin{matrix}1\\-1\\1\end{matrix}\right]$$
In [53]:
solve(Eq(A*x,v),x)
Out[53]:
$$\left \{ x_{0} : \frac{4}{3}, \quad x_{1} : \frac{5}{3}, \quad x_{2} : - \frac{7}{3}\right \}$$
In [54]:
solve(Eq(A*x,v))
Out[54]:
$$\left \{ x_{0} : \frac{4}{3}, \quad x_{1} : \frac{5}{3}, \quad x_{2} : - \frac{7}{3}\right \}$$
In [55]:
A.solve(v)
Out[55]:
$$\left[\begin{matrix}\frac{4}{3}\\\frac{5}{3}\\- \frac{7}{3}\end{matrix}\right]$$
In [56]:
linsolve((A,v),xs)
Out[56]:
$$\left\{\left ( \frac{4}{3}, \quad \frac{5}{3}, \quad - \frac{7}{3}\right )\right\}$$
In [57]:
a = symbols("a")
A[0,1] = a
In [58]:
xx = A.solve(v)
xx
Out[58]:
$$\left[\begin{matrix}- \frac{- 15 a + 24}{18 a - 45} + \frac{81 a - 324}{\left(- 9 a + 36\right) \left(18 a - 45\right)} - \frac{- 3 a \left(18 a - 45\right) - \left(- 15 a + 24\right) \left(18 a - 63\right)}{\left(- 9 a + 36\right) \left(18 a - 45\right)}\\- \frac{- 108 a + 432}{\left(- 3 a + 12\right) \left(18 a - 45\right)} + \frac{- 54 a + 216}{\left(- 3 a + 12\right) \left(18 a - 45\right)} + \frac{81 a - 324}{\left(- 3 a + 12\right) \left(18 a - 45\right)}\\\frac{- 9 a + 36}{18 a - 45} - \frac{18 a - 63}{18 a - 45} - \frac{9}{18 a - 45}\end{matrix}\right]$$
In [59]:
[xi.cancel() for xi in xx]
Out[59]:
$$\left [ \frac{13 a - 25}{6 a - 15}, \quad - \frac{5}{2 a - 5}, \quad - \frac{3 a - 10}{2 a - 5}\right ]$$
In [60]:
linsolve((A,v),xs)
Out[60]:
$$\left\{\left ( \frac{13 a - 25}{3 \left(2 a - 5\right)}, \quad - \frac{5}{2 a - 5}, \quad \frac{- 3 a + 10}{2 a - 5}\right )\right\}$$
In [61]:
x = Matrix(3,1,xs)
solve(Eq(A*x,v),xs)
Out[61]:
$$\left \{ x_{0} : \frac{13 a - 25}{3 \left(2 a - 5\right)}, \quad x_{1} : - \frac{5}{2 a - 5}, \quad x_{2} : \frac{- 3 a + 10}{2 a - 5}\right \}$$

Gausselimination II

In [62]:
L,U,perm = A.LUdecomposition() # Zeile 2 und 3 muessen vertauscht werden
L, U, perm
P = eye(A.shape[0]).permuteBkwd(perm)
P, L, U
Out[62]:
$$\left ( \left[\begin{matrix}1 & 0 & 0\\0 & 1 & 0\\0 & 0 & 1\end{matrix}\right], \quad \left[\begin{matrix}1 & 0 & 0\\1 & 1 & 0\\2 & \frac{- 2 a + 7}{- a + 4} & 1\end{matrix}\right], \quad \left[\begin{matrix}3 & a & 2\\0 & - a + 4 & 3\\0 & 0 & - \frac{3 \left(- 2 a + 7\right)}{- a + 4} + 4\end{matrix}\right]\right )$$
In [63]:
L,U,perm = H.LUdecomposition()
P = eye(H.shape[0]).permuteBkwd(perm)
P*H - L*U
Out[63]:
$$\left[\begin{matrix}0 & 0 & 0\\0 & 0 & 0\\0 & 0 & 0\end{matrix}\right]$$
In [64]:
def element(i,j,n):
    return (-1)**(i+j)*(i+j+1)*binomial(n+i,n-j-1)*binomial(n+j,n-i-1)*binomial(i+j,i)**2

Hi = Matrix(3,3,lambda i,j : element(i,j,3))
Hi
Out[64]:
$$\left[\begin{matrix}9 & -36 & 30\\-36 & 192 & -180\\30 & -180 & 180\end{matrix}\right]$$
In [65]:
H*Hi
Out[65]:
$$\left[\begin{matrix}1 & 0 & 0\\0 & 1 & 0\\0 & 0 & 1\end{matrix}\right]$$
In [66]:
Hnp = matrix2numpy(H)
Hnp
Out[66]:
array([[1, 1/2, 1/3],
       [1/2, 1/3, 1/4],
       [1/3, 1/4, 1/5]], dtype=object)
In [67]:
sin(H)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-67-6ae5d6c80eff> in <module>()
----> 1 sin(H)

/local/schaedle/miniconda3/envs/py36/lib/python3.6/site-packages/sympy/core/function.py in __new__(cls, *args, **options)
    425 
    426         evaluate = options.get('evaluate', global_evaluate[0])
--> 427         result = super(Function, cls).__new__(cls, *args, **options)
    428         if not evaluate or not isinstance(result, cls):
    429             return result

/local/schaedle/miniconda3/envs/py36/lib/python3.6/site-packages/sympy/core/function.py in __new__(cls, *args, **options)
    248 
    249         if evaluate:
--> 250             evaluated = cls.eval(*args)
    251             if evaluated is not None:
    252                 return evaluated

/local/schaedle/miniconda3/envs/py36/lib/python3.6/site-packages/sympy/functions/elementary/trigonometric.py in eval(cls, arg)
    285                                 Max(sin(min), sin(max)))
    286 
--> 287         if arg.could_extract_minus_sign():
    288             return -cls(-arg)
    289 

/local/schaedle/miniconda3/envs/py36/lib/python3.6/site-packages/sympy/matrices/matrices.py in __getattr__(self, attr)
   1841         else:
   1842             raise AttributeError(
-> 1843                 "%s has no attribute %s." % (self.__class__.__name__, attr))
   1844 
   1845     def __len__(self):

AttributeError: ImmutableDenseMatrix has no attribute could_extract_minus_sign.
In [68]:
H.applyfunc(sin)
Out[68]:
$$\left[\begin{matrix}\sin{\left (1 \right )} & \sin{\left (\frac{1}{2} \right )} & \sin{\left (\frac{1}{3} \right )}\\\sin{\left (\frac{1}{2} \right )} & \sin{\left (\frac{1}{3} \right )} & \sin{\left (\frac{1}{4} \right )}\\\sin{\left (\frac{1}{3} \right )} & \sin{\left (\frac{1}{4} \right )} & \sin{\left (\frac{1}{5} \right )}\end{matrix}\right]$$
In [69]:
M = randMatrix(2,3)
M
Out[69]:
$$\left[\begin{matrix}66 & 85 & 39\\48 & 53 & 21\end{matrix}\right]$$

Kopieren von Matrizen

In [70]:
BB = B
In [71]:
BB,B
Out[71]:
$$\left ( \left[\begin{matrix}0 & 1 & 0\\1 & 0 & 0\\0 & 0 & 1\end{matrix}\right], \quad \left[\begin{matrix}0 & 1 & 0\\1 & 0 & 0\\0 & 0 & 1\end{matrix}\right]\right )$$
In [72]:
BB[0,0]=9
In [73]:
BB,B
Out[73]:
$$\left ( \left[\begin{matrix}9 & 1 & 0\\1 & 0 & 0\\0 & 0 & 1\end{matrix}\right], \quad \left[\begin{matrix}9 & 1 & 0\\1 & 0 & 0\\0 & 0 & 1\end{matrix}\right]\right )$$
In [74]:
BBB = B.copy()
In [75]:
BBB[0,0] = 99
In [76]:
BBB,B
Out[76]:
$$\left ( \left[\begin{matrix}99 & 1 & 0\\1 & 0 & 0\\0 & 0 & 1\end{matrix}\right], \quad \left[\begin{matrix}9 & 1 & 0\\1 & 0 & 0\\0 & 0 & 1\end{matrix}\right]\right )$$