Seite - 112 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition
Bild der Seite - 112 -
Text der Seite - 112 -
112 5 SomeMorePythonEssentials
5.3.2 SymPy:SomeBasicFunctionality
Thefollowingscriptexample_symbolic.pygivesa quickdemonstrationof some
of the basic symbolicoperations thatare supported in Python.
import sympy as sym
x, y = sym.symbols(’x y’)
print(2*x + 3*x - y) # Algebraic computation
print(sym.diff(x**2, x)) # Differentiates x**2 wrt. x
print(sym.integrate(sym.cos(x), x)) # Integrates cos(x) wrt. x
print(sym.simplify((x**2 + x**3)/x**2)) # Simplifies expression
print(sym.limit(sym.sin(x)/x, x, 0)) # lim of sin(x)/x as x->0
print(sym.solve(5*x - 15, x)) # Solves 5*x = 15
Another useful possibility with sympy, is that sympy expressions may be
converted to lambda functions, which then may be used as “normal” Python
functionsfornumericalcalculations.Anexamplewill illustrate.
Letususesympy toanalyticallyfindthederivativeof thefunctionf(x)=5x3+
2x2−1,and thenmakebothfand its derivative intoPythonfunctions:
import sympy as sym
x = sym.symbols(’x’)
f_expr = 5*x**3 + 2*x**2 - 1 # symbolic expression for f(x)
dfdx_expr = sym.diff(f_expr, x) # compute f’(x) symbolically
# turn symbolic expressions into functions
f = sym.lambdify([x], f_expr) # f = lambda x: 5*x**3 + 2*x**2 - 1
dfdx = sym.lambdify([x], dfdx_expr) # dfdx = lambda x: 15*x**2 + 4*x
print(f(1), dfdx(1)) # call and print, x = 1
Note the arguments to lambdify. The first argument[x] specifies the argument
that the generatedfunctionf (and the functiondfdx) is supposed to take,while the
secondargumentf_expr(anddfdx_expr)specifiestheexpressiontobeevaluated.
Whenexecuted, theprogramprints6 and19,correspondingtof(1)anddfdx(1),
respectively.
Other symbolic calculations for, e.g., Taylor series3 expansion, linear algebra
(with matrix and vector operations), and (some) differential equation solving are
alsopossible.
5.3.3 SymbolicCalculationswithSomeOtherTools
Symbolic computations are also readily accessible through the (partly) free online
tool WolframAlpha,4 which applies the very advanced Mathematica5 package as
symbolic engine. The disadvantage with WolframAlpha compared to the SymPy
3 See, e.g., https://en.wikipedia.org/wiki/Taylor_series.
4 http://www.wolframalpha.com.
5 http://en.wikipedia.org/wiki/Mathematica.
Programming for Computations – Python
A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition
- Titel
- Programming for Computations – Python
- Untertitel
- A Gentle Introduction to Numerical Simulations with Python 3.6
- Band
- Second Edition
- Autoren
- Svein Linge
- Hans Petter Langtangen
- Verlag
- Springer Open
- Datum
- 2020
- Sprache
- englisch
- Lizenz
- CC BY 4.0
- ISBN
- 978-3-319-32428-9
- Abmessungen
- 17.8 x 25.4 cm
- Seiten
- 356
- Schlagwörter
- Programmiersprache, Informatik, programming language, functional, imperative, object-oriented, reflective
- Kategorie
- Informatik