Computergestützte Mathematik zur Analysis¶

Vorlesung vom 13.10.2022

Copyright: Prof. Dr. Rüdiger W. Braun

Arithmetik¶

Numerisch¶

In [1]:
4*3 + 5
Out[1]:
17
In [2]:
4*(3+5)
Out[2]:
32
In [3]:
(3+5)/4
Out[3]:
2.0
In [4]:
(3+5)/5
Out[4]:
1.6
In [6]:
9/5
Out[6]:
1.8
In [5]:
2**10
Out[5]:
1024
In [7]:
(1/3)*3
Out[7]:
1.0
In [8]:
(1/3)**100 * 3**100
Out[8]:
0.9999999999999944

Symbolisch¶

In [9]:
from sympy import *
In [10]:
S(4)*3 + 5
Out[10]:
$\displaystyle 17$
In [11]:
(S(3)+5)/4
Out[11]:
$\displaystyle 2$
In [12]:
(S(3)+5)/5
Out[12]:
$\displaystyle \frac{8}{5}$
In [13]:
Rational(8,5)
Out[13]:
$\displaystyle \frac{8}{5}$
In [14]:
S("8/5")
Out[14]:
$\displaystyle \frac{8}{5}$
In [15]:
S(8/5)
Out[15]:
$\displaystyle 1.6$
In [16]:
(S(1)/3)**100 * 3**100
Out[16]:
$\displaystyle 1$
In [17]:
(S(1)/3)**100
Out[17]:
$\displaystyle \frac{1}{515377520732011331036461129765621272702107522001}$

Funktionen¶

Sympy kennt alle elementaren und einige spezielle Funktionen

In [18]:
pi
Out[18]:
$\displaystyle \pi$
In [19]:
sin(pi/6)
Out[19]:
$\displaystyle \frac{1}{2}$
In [23]:
cos(pi/4)
Out[23]:
$\displaystyle \frac{\sqrt{2}}{2}$
In [21]:
sqrt(128)
Out[21]:
$\displaystyle 8 \sqrt{2}$
In [24]:
_**2
Out[24]:
$\displaystyle \frac{1}{2}$
In [25]:
atan(1)
Out[25]:
$\displaystyle \frac{\pi}{4}$
In [26]:
atan(S(1)/2)
Out[26]:
$\displaystyle \operatorname{atan}{\left(\frac{1}{2} \right)}$
In [27]:
atan(1/2)
Out[27]:
$\displaystyle 0.463647609000806$
In [28]:
atan(1/sqrt(3))
Out[28]:
$\displaystyle \frac{\pi}{6}$
In [29]:
exp(2)
Out[29]:
$\displaystyle e^{2}$
In [30]:
log(1/exp(3))
Out[30]:
$\displaystyle -3$
In [31]:
log(e)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Input In [31], in <cell line: 1>()
----> 1 log(e)

NameError: name 'e' is not defined
In [32]:
log(E)
Out[32]:
$\displaystyle 1$
In [33]:
factorial(5)
Out[33]:
$\displaystyle 120$
In [34]:
factorial(120)
Out[34]:
$\displaystyle 6689502913449127057588118054090372586752746333138029810295671352301633557244962989366874165271984981308157637893214090552534408589408121859898481114389650005964960521256960000000000000000000000000000$
In [35]:
factorial(240/7)
Out[35]:
$\displaystyle 8.12939431021931 \cdot 10^{38}$

Beliebig genaue Fließkommazahlen¶

In [36]:
pi
Out[36]:
$\displaystyle \pi$
In [37]:
N(pi)
Out[37]:
$\displaystyle 3.14159265358979$
In [38]:
N(pi, 100)
Out[38]:
$\displaystyle 3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117068$
In [39]:
N(1/3, 100)
Out[39]:
$\displaystyle 0.333333333333333314829616256247390992939472198486328125$
In [40]:
N("1/3", 100)
Out[40]:
$\displaystyle 0.3333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333$
In [41]:
N(Rational(2,3), 100)
Out[41]:
$\displaystyle 0.6666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666667$

Sympyfizierungen¶

In [42]:
type(3)
Out[42]:
int
In [43]:
type(S(3))
Out[43]:
sympy.core.numbers.Integer
In [44]:
type(1/3)
Out[44]:
float
In [45]:
type(N(S(1)/3, 200))
Out[45]:
sympy.core.numbers.Float
In [46]:
type(S("8/5"))
Out[46]:
sympy.core.numbers.Rational
In [47]:
type(pi)
Out[47]:
sympy.core.numbers.Pi

Man muss manchmal den Typ eines Objekts wissen, um das Ergebnis zu verstehen. Man legt den Typ in der Regel aber implizit fest.

Exceptions¶

In [49]:
1/0
---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
Input In [49], in <cell line: 1>()
----> 1 1/0

ZeroDivisionError: division by zero
In [50]:
11.**500
---------------------------------------------------------------------------
OverflowError                             Traceback (most recent call last)
Input In [50], in <cell line: 1>()
----> 1 11.**500

OverflowError: (34, 'Result too large')
In [51]:
11**500
Out[51]:
49698419673122668962869431655245562316047646973098768083125762994096192244203916170987083962253749489388688113714575561907503512837449875405039140440330009424543851341473661479835156198793475230001956315493079590327474913873798690064481533085354154735423096767027003741096784021844432989065936384717922119162319032031586803190801094405732340227101653875034599110559882607618553240212695425973141414122679894203387024067292317607709706656265698204172143249895717068799024075072923130694160791083167647986127226561792230001
In [52]:
N(11.)**500
Out[52]:
$\displaystyle 4.96984196731227 \cdot 10^{520}$
In [53]:
N(11., 550)**500
Out[53]:
$\displaystyle 49698419673122668962869431655245562316047646973098768083125762994096192244203916170987083962253749489388688113714575561907503512837449875405039140440330009424543851341473661479835156198793475230001956315493079590327474913873798690064481533085354154735423096767027003741096784021844432989065936384717922119162319032031586803190801094405732340227101653875034599110559882607618553240212695425973141414122679894203387024067292317607709706656265698204172143249895717068799024075072923130694160791083167647986127226561792230001.0$

Variablen¶

In [54]:
a = 5
In [55]:
a
Out[55]:
5
In [56]:
b
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Input In [56], in <cell line: 1>()
----> 1 b

NameError: name 'b' is not defined
In [57]:
b = a
In [58]:
b
Out[58]:
5
In [59]:
a = 19
In [60]:
b
Out[60]:
5

Symbole¶

In [61]:
x = S('x')
x
Out[61]:
$\displaystyle x$
In [62]:
type(x)
Out[62]:
sympy.core.symbol.Symbol
In [63]:
f = 5*x + 13
f
Out[63]:
$\displaystyle 5 x + 13$
In [64]:
x = 0
In [65]:
f
Out[65]:
$\displaystyle 5 x + 13$
In [67]:
x = S('x')
In [68]:
sin(x)**2 + cos(x)**2
Out[68]:
$\displaystyle \sin^{2}{\left(x \right)} + \cos^{2}{\left(x \right)}$

Simplify

In [69]:
s = sin(x)**2 + cos(x)**2
simplify(s)
Out[69]:
$\displaystyle 1$
In [71]:
s = sqrt(x**2)
s
Out[71]:
$\displaystyle \sqrt{x^{2}}$
In [72]:
simplify(s)
Out[72]:
$\displaystyle \sqrt{x^{2}}$

sympy hat Recht und Sie haben Unrecht 🙂

In [73]:
x = -2
x == sqrt(x**2)
Out[73]:
False
In [74]:
x, sqrt(x**2)
Out[74]:
(-2, 2)