Lektion 1

In [7]:
from sympy import *
init_printing()

Einfache Arithmetik

In [8]:
2+2
Out[8]:
$$4$$
In [9]:
2*3
Out[9]:
$$6$$
In [10]:
2**3
Out[10]:
$$8$$
In [11]:
1/3
Out[11]:
$$0.3333333333333333$$
In [12]:
1/0
---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
<ipython-input-12-9e1622b385b6> in <module>()
----> 1 1/0

ZeroDivisionError: division by zero

Gleitkommazahlen Rechnen mit vielen Nachkommastellen

In [13]:
3*(1/3)
Out[13]:
$$1.0$$
In [14]:
3**100 * (1/3)**100
Out[14]:
$$0.9999999999999944$$
In [15]:
drittel = Rational(1,3)  # rationale Zahl
drittel
Out[15]:
$$\frac{1}{3}$$
In [16]:
3**100 * drittel**100
Out[16]:
$$1$$
In [17]:
3**100
Out[17]:
$$515377520732011331036461129765621272702107522001$$
In [18]:
(1/3)**1000
Out[18]:
$$0.0$$
In [19]:
(1/3)**1000 * 3**1000
---------------------------------------------------------------------------
OverflowError                             Traceback (most recent call last)
<ipython-input-19-25b92f497fe9> in <module>()
----> 1 (1/3)**1000 * 3**1000

OverflowError: int too large to convert to float
In [20]:
pi
Out[20]:
$$\pi$$
In [21]:
N(pi,200)
Out[21]:
$$3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647093844609550582231725359408128481117450284102701938521105559644622948954930382$$
In [22]:
print(N(pi,200))
3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303820
In [23]:
N(1/3,100)
Out[23]:
$$0.333333333333333314829616256247390992939472198486328125$$
In [24]:
N(Rational(1,3),200)
Out[24]:
$$0.33333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333$$
In [25]:
N(Rational(1,3),200)**1000 * 3**1000
Out[25]:
$$1.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004$$

Sympifizierung

In [26]:
3
Out[26]:
$$3$$
In [27]:
S(3)
Out[27]:
$$3$$
In [28]:
type(3)
Out[28]:
int
In [29]:
type(S(3))
Out[29]:
sympy.core.numbers.Integer
In [30]:
type(S(1)/3)
Out[30]:
sympy.core.numbers.Rational
In [31]:
x = S('x')
In [32]:
type(x)
Out[32]:
sympy.core.symbol.Symbol
In [33]:
y = S('y')
y
Out[33]:
$$y$$
In [34]:
f = (x+y)**2
f
Out[34]:
$$\left(x + y\right)^{2}$$
In [35]:
x = 5
In [36]:
f
Out[36]:
$$\left(x + y\right)^{2}$$
In [37]:
x
Out[37]:
$$5$$
In [38]:
f.atoms()
Out[38]:
$$\left\{2, x, y\right\}$$

Einfache Funktionen

In [39]:
sqrt(81)
Out[39]:
$$9$$
In [40]:
sqrt(-81)
Out[40]:
$$9 i$$
In [41]:
sqrt(234.)
Out[41]:
$$15.2970585407784$$
In [42]:
sqrt(9*y**2)
Out[42]:
$$3 \sqrt{y^{2}}$$
In [43]:
factorial(5)
Out[43]:
$$120$$
In [44]:
factorial(170)
Out[44]:
$$7257415615307998967396728211129263114716991681296451376543577798900561843401706157852350749242617459511490991237838520776666022565442753025328900773207510902400430280058295603966612599658257104398558294257568966313439612262571094946806711205568880457193340212661452800000000000000000000000000000000000000000$$
In [45]:
sin(pi)
Out[45]:
$$0$$
In [46]:
cos(pi)
Out[46]:
$$-1$$
In [47]:
tan(pi/2)
Out[47]:
$$\tilde{\infty}$$
In [48]:
print(tan(pi/2))
zoo
In [49]:
?zoo
In [50]:
alpha = Symbol('alpha')
alpha
Out[50]:
$$\alpha$$
In [51]:
exp(1)
Out[51]:
$$e$$
In [52]:
log(exp(1))
Out[52]:
$$1$$
In [53]:
abs(-1)
Out[53]:
$$1$$

Vereinfachungen

In [54]:
x = Symbol("x")
x
Out[54]:
$$x$$
In [55]:
y = Symbol("y")
y
Out[55]:
$$y$$
In [56]:
f = (x-y)*(x+y)
f
Out[56]:
$$\left(x - y\right) \left(x + y\right)$$
In [57]:
f.expand()
Out[57]:
$$x^{2} - y^{2}$$
In [58]:
expand(f)
Out[58]:
$$x^{2} - y^{2}$$
In [59]:
f.expand().factor()
Out[59]:
$$\left(x - y\right) \left(x + y\right)$$
In [60]:
g = (x**2 -y**2)/(x-y)
g
Out[60]:
$$\frac{x^{2} - y^{2}}{x - y}$$
In [61]:
g.ratsimp()
Out[61]:
$$x + y$$
In [62]:
g.simplify()
Out[62]:
$$x + y$$
In [63]:
h = x*x**y
h
Out[63]:
$$x x^{y}$$
In [64]:
h.powsimp()
Out[64]:
$$x^{y + 1}$$
In [65]:
h.powsimp().expand()
Out[65]:
$$x x^{y}$$
In [66]:
f = (sin(2*x)+cos(x))/ (( (sin(2*x)**2)-cos(x)**2) *(sin(2*x)-cos(x)))
f
Out[66]:
$$\frac{\sin{\left (2 x \right )} + \cos{\left (x \right )}}{\left(\sin{\left (2 x \right )} - \cos{\left (x \right )}\right) \left(\sin^{2}{\left (2 x \right )} - \cos^{2}{\left (x \right )}\right)}$$
In [67]:
f.simplify()
Out[67]:
$$\frac{1}{\left(2 \sin{\left (x \right )} - 1\right)^{2} \cos^{2}{\left (x \right )}}$$
In [68]:
f.trigsimp()
Out[68]:
$$\frac{1}{\left(2 \sin{\left (x \right )} - 1\right)^{2} \cos^{2}{\left (x \right )}}$$
In [69]:
f.expand()
Out[69]:
$$\frac{\sin{\left (2 x \right )}}{\sin^{3}{\left (2 x \right )} - \sin^{2}{\left (2 x \right )} \cos{\left (x \right )} - \sin{\left (2 x \right )} \cos^{2}{\left (x \right )} + \cos^{3}{\left (x \right )}} + \frac{\cos{\left (x \right )}}{\sin^{3}{\left (2 x \right )} - \sin^{2}{\left (2 x \right )} \cos{\left (x \right )} - \sin{\left (2 x \right )} \cos^{2}{\left (x \right )} + \cos^{3}{\left (x \right )}}$$
In [70]:
f.expand(numer=True,trig=True).factor()
Out[70]:
$$\frac{\left(2 \sin{\left (x \right )} + 1\right) \cos{\left (x \right )}}{\left(- \sin{\left (2 x \right )} + \cos{\left (x \right )}\right)^{2} \left(\sin{\left (2 x \right )} + \cos{\left (x \right )}\right)}$$
In [71]:
f.expand(gibtsnicht=True)
Out[71]:
$$\frac{\sin{\left (2 x \right )}}{\sin^{3}{\left (2 x \right )} - \sin^{2}{\left (2 x \right )} \cos{\left (x \right )} - \sin{\left (2 x \right )} \cos^{2}{\left (x \right )} + \cos^{3}{\left (x \right )}} + \frac{\cos{\left (x \right )}}{\sin^{3}{\left (2 x \right )} - \sin^{2}{\left (2 x \right )} \cos{\left (x \right )} - \sin{\left (2 x \right )} \cos^{2}{\left (x \right )} + \cos^{3}{\left (x \right )}}$$