Page - 24 - in Programming for Computations β Python - A Gentle Introduction to Numerical Simulations with Python
Image of the Page - 24 -
Text of the Page - 24 -
24 1 TheFirstFewSteps
from sympy import *
x, y = symbols(βx yβ)
z = x*y
print z
whichcauses the symbolic resultx*y to appearon the screen. Note that nonumer-
ical valuewas assigned to anyof the variables in the symbolic computation. Only
the symbolswereused, aswhenyoudo symbolicmathematics byhandonapiece
ofpaper.
SymboliccomputationsinPythonmakeuseof theSymPypackage. Eachsymbol
is representedbyastandardvariable, but thenameof the symbolmust bedeclared
with Symbol(name) for a single symbol, or symbols(name1 name2 ...) for
multiplesymbols.. Thefollowingscriptexample_symbolic.pyisaquickdemon-
strationof someof thebasic symbolicoperations that are supported inPython.
from sympy import *
x = Symbol(βxβ)
y = Symbol(βyβ)
print 2*x + 3*x - y # Algebraic computation
print diff(x**2, x) # Differentiates x**2 wrt. x
print integrate(cos(x), x) # Integrates cos(x) wrt. x
print simplify((x**2 + x**3)/x**2) # Simplifies expression
print limit(sin(x)/x, x, 0) # Finds limit of sin(x)/x as x->0
print solve(5*x - 15, x) # Solves 5*x = 15
Other symbolic calculations like Taylor series expansion, linear algebra (with
matrix and vector operations), and (some) differential equation solving are also
possible.
Symbolic computations are also readily accessible through the (partly) free on-
line toolWolframAlpha4,which applies thevery advancedMathematica5 package
assymbolicengine. ThedisadvantagewithWolframAlphacomparedto theSymPy
package is that the results cannot automatically be imported into your code and
used for further analysis. On theotherhand,WolframAlphahas the advantage that
it displaysmanyadditionalmathematical results related to thegivenproblem. For
example, ifwe type2x + 3x - y inWolframAlpha, it not only simplifies the ex-
pression to 5x - y, but it also makes plots of the function f.x;y/ D 5x y,
solves the equation 5x y D 0, and calculates the integral R R.5x Cy/dxdy.
ThecommercialProversioncanalso showastep-by-stepof the analytical compu-
tations in theproblem. Youare strongly encouraged to try out these commands in
WolframAlpha:
diff(x^2, x)ordiff(x**2, x)
integrate(cos(x), x)
simplify((x**2 + x**3)/x**2)
4http://www.wolframalpha.com
5http://en.wikipedia.org/wiki/Mathematica
back to the
book Programming for Computations β Python - A Gentle Introduction to Numerical Simulations with Python"
Programming for Computations β Python
A Gentle Introduction to Numerical Simulations with Python
- Title
- Programming for Computations β Python
- Subtitle
- A Gentle Introduction to Numerical Simulations with Python
- Authors
- Svein Linge
- Hans Petter Langtangen
- Publisher
- Springer Open
- Date
- 2016
- Language
- English
- License
- CC BY-NC 4.0
- ISBN
- 978-3-319-32428-9
- Size
- 17.8 x 25.4 cm
- Pages
- 248
- Keywords
- Programmiersprache, Informatik, programming language, functional, imperative, object-oriented, reflective
- Category
- Informatik