import math
3) math.sqrt(
1.7320508075688772
Symbolic computation deals with symbols, representing them exactly, instead of numerical approximations (floating point).
We will start with the following borrowed tutorial to introduce the concepts of SymPy. Devito uses SymPy heavily and builds upon it in its DSL.
\(\sqrt(8) = 2\sqrt(2)\), but it’s hard to see that here
SymPy can even simplify symbolic computations
Note that simply adding two symbols creates an expression. Now let’s play around with it.
Note that expr - x
was not x + 2y -x
\(\displaystyle x^{2} + 2 x y\)
\(\displaystyle e^{x} \sin{\left(x \right)} + e^{x} \cos{\left(x \right)}\)
Solve \(x^2 - 2 = 0\) using sympy.solve
More symbols. Exercise: fix the following piece of code
# NBVAL_SKIP
# The following piece of code is supposed to fail as it is
# The exercise is to fix the code
expr2 = x + 2*y +3*z
NameError: name 'z' is not defined
Solve \(x + 2*y + 3*z\) for \(x\)
Difference between symbol name and python variable name
NameError: name 'z' is not defined
Symbol names can be more than one character long
What happens when I print expr now? Does it print 3?
How do we get 3?
Suppose we want to ask whether \((x + 1)^2 = x^2 + 2x + 1\)
Write a function that takes two expressions as input, and returns a tuple of two booleans. The first if they are equal symbolically, and the second if they are equal mathematically.
\(\displaystyle x^{2} + 3 x - \frac{1}{2}\)
\(\displaystyle 3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117068\)
\(\displaystyle 0.0874989834394464\)
Write a function that takes a symbolic expression (like pi), and determines the first place where 789 appears. Tip: Use the string representation of the number. Python starts counting at 0, but the decimal point offsets this
\(\displaystyle f{\left(x \right)}\)
\(\displaystyle f{\left(x \right)} - 2 \frac{d}{d x} f{\left(x \right)} + \frac{d^{2}}{d x^{2}} f{\left(x \right)} = \sin{\left(x \right)}\)
\(\displaystyle - f{\left(x - \frac{1}{2} \right)} + f{\left(x + \frac{1}{2} \right)}\)
\(\displaystyle - \frac{2 f{\left(x \right)}}{h^{2}} + \frac{f{\left(- h + x \right)}}{h^{2}} + \frac{f{\left(h + x \right)}}{h^{2}}\)
Now that we have seen some relevant features of vanilla SymPy, let’s move on to Devito, which could be seen as SymPy finite differences on steroids!