In [1]:
from sympy import *
init_printing()
import numpy as np
import matplotlib.pyplot as plt
interaktiv
In [2]:
#%matplotlib notebook
für den Druck
In [3]:
%matplotlib inline
Blöcke werden durch Einrückung festgelegt
In [4]:
summe = 0
for j in range(10):
summe = summe + j
summe
Out[4]:
Probe
In [5]:
np.arange(10).sum()
Out[5]:
In [6]:
12 % 3 # modulo
Out[6]:
In [7]:
for a in range(7):
if a % 3 == 0:
print(a, 'ist durch 3 teilbar')
elif a % 3 == 1:
print(a, 'lässt Rest 1')
else:
print(a, 'lässt Rest 2')
Pascalsches Dreieck
In [8]:
N = 6
In [9]:
b = {}
for n in range(1, N+1):
b[(n,1)] = 1
for j in range(2,n):
b[(n,j)] = b[(n-1,j-1)] + b[(n-1,j)]
b[(n,n)] = 1
In [10]:
for n in range(1,N+1):
print([b[(n,j)] for j in range(1,n+1)])
In [11]:
for j in range(1, 6):
wert = sin(2*pi/j)
print(wert)
In [12]:
from IPython.display import display, Image
In [13]:
for j in range(1, 11):
wert = sin(2*pi/j)
display((2*pi/j, wert))
In [14]:
x = Symbol('x')
y = Symbol('y')
In [15]:
h = (x**2+y**2)**2 + 3*x**2*y - y**3
xn = np.linspace(-1, 1, 200)
yn = xn
hn = lambdify((x,y), h)
X, Y = np.meshgrid(xn, yn)
In [16]:
fig = plt.figure()
for j in range(9):
fig.add_subplot(331+j)
plt.contour(X, Y, hn(X, Y), [.001*(j-4)])
plt.axis('equal')
if j % 3 != 0:
plt.yticks([])
if j <= 5:
plt.xticks([])
else:
plt.xticks([-1.0, 0.0, 1.0])
fig.savefig('9plots.png') # pdf geht auch
In [17]:
Image('9plots.png') # kann pdf nicht anzeigen
Out[17]:
In [18]:
x = Symbol('x')
xn = np.arange(6)
zn = 1j + xn
x, xn, zn
Out[18]:
In [19]:
def f(x):
return x**3 + 2*x + 15
In [20]:
f(0)
Out[20]:
In [21]:
f(0.)
Out[21]:
In [22]:
f(0+0j)
Out[22]:
In [23]:
f(2*x)
Out[23]:
In [24]:
f(xn)
Out[24]:
Duck typing:
“When I see a bird that walks like a duck and swims like a duck and quacks like a duck, I call that bird a duck.”
– James Whitcomb Riley
In [25]:
def g(x):
return sin(x) + f(x)
In [26]:
g(-2*x)
Out[26]:
In [27]:
g(pi)
Out[27]:
In [28]:
g(3.1415)
Out[28]:
In [29]:
# g(xn) zeigt einen AttributeError
In [30]:
def abss(x):
if x > 0:
return x
else:
return -x
In [31]:
abss(-5)
Out[31]:
In [32]:
# abss(x) # TypeError
In [33]:
xp = Symbol('x_+', positive=True)
abss(xp)
Out[33]:
In [34]:
x = Symbol('x')
In [35]:
a = (x**4-3*x**2+5)*exp(x**2)*sin(x)
a
Out[35]:
In [36]:
da = diff(a, x, 3)
da
Out[36]:
In [37]:
da.expand()
Out[37]:
In [38]:
da.expand().collect(exp(x**2))
Out[38]:
In [39]:
rcollect(collect(da.expand(), exp(x**2)), cos(x), sin(x))
Out[39]:
In [40]:
x = Symbol('x')
y = Symbol('y')
In [41]:
a = cos(x+y)
In [42]:
A = expand(a, trig=True)
A
Out[42]:
In [43]:
trigsimp(A)
Out[43]:
In [44]:
c = tan(x)**2 + 1
c
Out[44]:
In [45]:
c.trigsimp()
Out[45]:
In [46]:
d = tan(3*x)
d.simplify()
Out[46]:
In [47]:
d.expand(trig=True)
Out[47]:
In [48]:
d.expand(trig=True).ratsimp()
Out[48]:
In [49]:
b = sin(x) + sin(y)
b
Out[49]:
In [50]:
b.trigsimp(method='fu')
Out[50]:
Wie kompliziert ist dieser Ausdruck?
In [51]:
b.count_ops(visual=True)
Out[51]:
In [52]:
def my_measure(expr):
opc = expr.count_ops(visual=True)
# print(opc) # zur Fehlersuche
wert = {}
wert['ADD'] = 1
wert['SIN'] = 1
wert['MUL'] = -100
return opc.subs(wert)
In [53]:
my_measure(b)
Out[53]:
In [54]:
# b.trigsimp(method='fu', measure=my_measure) # TypeError
In [55]:
def my_measure(expr):
opc = expr.count_ops(visual=True)
wert = {}
wert['ADD'] = 1
wert['SIN'] = 1
wert['MUL'] = -100
wert['COS'] = 1
wert['DIV'] = 1
wert['SUB'] = 1
return opc.subs(wert)
In [56]:
b.trigsimp(method='fu', measure=my_measure)
Out[56]:
In [57]:
e = tan(x) + cot(y)
e.simplify()
Out[57]:
In [58]:
def my_measure(expr):
opc = expr.count_ops(visual=True)
# print(opc) # zur Fehlersuche
wert = {}
wert['ADD'] = 1
wert['SIN'] = 1
wert['COS'] = 1
wert['TAN'] = 100
wert['COT'] = 100
return opc.subs(wert)
In [59]:
# e.trigsimp(method='fu', measure=my_measure) # TypeError
In [60]:
def my_measure(expr):
opc = expr.count_ops(visual=True)
wert = {}
wert['ADD'] = 1
wert['SIN'] = 1
wert['COS'] = 1
wert['TAN'] = 100
wert['COT'] = 100
wert['DIV'] = 1
return opc.subs(wert)
In [61]:
e1 = e.trigsimp(method='fu', measure=my_measure)
e1
Out[61]:
In [62]:
e2 = e1.ratsimp()
e2
Out[62]:
In [63]:
e2.trigsimp()
Out[63]:
In [64]:
num, den = numer(e2), denom(e2)
num1 = num.trigsimp()
num1/den
Out[64]:
In [65]:
f = x**Rational(1,3)
In [66]:
plot(f, (x, -1, 1));
In [67]:
z = f.subs(x, -1)
z
Out[67]:
In [68]:
expand(z, complex=True)
Out[68]:
In [69]:
expand(exp(pi*I/3), complex=True)
Out[69]:
In [70]:
g = sign(x)*abs(x)**Rational(1,3)
g
Out[70]:
In [71]:
plot(g, (x,-1,1));