In [1]:
from sympy import *
init_printing()
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
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]:
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();
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]:
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();
In [20]:
A = Matrix(3, 3, range(1,10))
A
Out[20]:
In [21]:
b = Matrix([6, 15, 24])
b
Out[21]:
Wir wollen $Ax=b$ lösen.
In [22]:
x = Matrix([Symbol('x_'+str(j)) for j in [1,2,3]])
x
Out[22]:
In [23]:
glg = Eq(A*x, b)
glg
Out[23]:
In [24]:
Lsg = solve(glg)
Lsg
Out[24]:
Probe:
In [25]:
glg.subs(Lsg[0])
Out[25]:
konkrete Lösung
In [26]:
x.subs(Lsg[0]).subs(x[2], 0)
Out[26]:
andere konkrete Lösung
In [27]:
x.subs(Lsg[0]).subs(x[2], 1)
Out[27]:
In [28]:
solve(A*x, x) # Kern von A
Out[28]:
In [29]:
solve(A*x, Matrix([0,0,1])) # unlösbar
Out[29]:
In [30]:
B = A
In [31]:
B[0,0] = 999
B
Out[31]:
In [32]:
A
Out[32]:
In [33]:
B = A.copy()
B[0,0] = 777
B
Out[33]:
In [34]:
A
Out[34]:
In [35]:
C = A[0:3, 0:3]
C
Out[35]:
In [36]:
C[1,1] = 555
C
Out[36]:
In [37]:
A
Out[37]:
In [38]:
type(A)
Out[38]:
In [39]:
Ai = simplify(A)
type(Ai)
Out[39]:
In [43]:
# Ai[0,0] = 3 # TypeError, denn Ai ist ImmutableMatrix
In [44]:
B = Matrix(Ai)
B[0,0] = 333
A, B
Out[44]:
In [45]:
type(Matrix(B))
Out[45]:
In [46]:
a = Symbol('a')
A = Matrix(3,3,range(1,10))
A[0,0] = 1 + a
A
Out[46]:
In [47]:
glg = Eq(A*x, b)
glg
Out[47]:
In [48]:
solve(glg, x)
Out[48]:
In [49]:
glg = Eq(A*x, Matrix([0,0,1]))
glg
Out[49]:
In [50]:
solve(glg, x)
Out[50]:
In [51]:
A.det()
Out[51]:
In [52]:
L, U, R = A.LUdecomposition()
L, U, R
Out[52]:
In [53]:
L*U == A # aber nur, weil R=[]
Out[53]:
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]:
In [55]:
def func1(b, j):
return b/A1[0,0]
A1.row_op(0, func1)
A1
Out[55]:
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]:
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]:
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]:
In [61]:
A2 = mult_zeile(A1, 1, 1/A1[1,1])
A2
Out[61]:
In [62]:
A3 = add_zeile(A2, 1, 2, -A2[2,1])
A3 = simplify(A3)
A3
Out[62]:
In [63]:
A4 = mult_zeile(A3, 2, 1/A3[2,2])
A4
Out[63]:
In [64]:
A5 = add_zeile(A4, 2, 1, -A4[1,2])
A5
Out[64]:
In [65]:
A6 = add_zeile(A5, 1, 0, -A5[0,1])
A7 = add_zeile(A6, 2, 0, -A6[0,2])
A7
Out[65]:
In [66]:
y = simplify(A7[0:3, 3])
y
Out[66]:
In [67]:
simplify(A*y) == b
Out[67]:
In [68]:
A = Matrix(3, 3, range(1,10))
A.eigenvals()
Out[68]:
In [69]:
A.eigenvects()
Out[69]:
In [70]:
A.eigenvects(simplify=True)
Out[70]:
In [71]:
T, J = A.diagonalize()
T = simplify(T)
T, J
Out[71]:
In [72]:
simplify(T * J * T**(-1))
Out[72]: