Web-Books
in the Austria-Forum
Austria-Forum
Web-Books
Informatik
Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition
Page - 112 -
  • User
  • Version
    • full version
    • text only version
  • Language
    • Deutsch - German
    • English

Page - 112 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition

Image of the Page - 112 -

Image of the Page - 112 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition

Text of the Page - 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.
back to the  book Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition"
Programming for Computations – Python A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition
Title
Programming for Computations – Python
Subtitle
A Gentle Introduction to Numerical Simulations with Python 3.6
Volume
Second Edition
Authors
Svein Linge
Hans Petter Langtangen
Publisher
Springer Open
Date
2020
Language
English
License
CC BY 4.0
ISBN
978-3-319-32428-9
Size
17.8 x 25.4 cm
Pages
356
Keywords
Programmiersprache, Informatik, programming language, functional, imperative, object-oriented, reflective
Category
Informatik
Web-Books
Library
Privacy
Imprint
Austria-Forum
Austria-Forum
Web-Books
Programming for Computations – Python