import addutils.toc ; addutils.toc.js(ipy_notebook=True)
Symbolic computation (also called computer algebra) emphasizes exact computation where mathematical expressions and objects are represented by algebraic formulas, and not numbers. SymPy is the python module suitable for symbolic computation. See Sympy Modules Reference.
from addutils import css_notebook
from sympy import *
import numpy as np
import bokeh.plotting as bk
from bokeh.plotting import output_notebook
init_printing(use_latex='mathjax')
css_notebook()
a, b, c = symbols('a, b, c')
exp1 = pi*( a*b*b+2*b*a*b )**c
exp1
We can add assumpions to symbols
k = symbols('k', real=True, positive=True)
k > 0
Rational numbers
r1 = Rational(5,6)
r2 = Rational(7,3)
r1+r2
The expression may be calculated by substituting numerical values to given variables (in this case --> a,b):
exp1.evalf(subs={a:6e-3, b:3, c:2})
We can combine expressions and Numpy arrays
b_vec = np.arange(0, 10, 0.1)
y_vec = np.array([exp1.evalf(subs={a:6e-3, b:bb, c:2}) for bb in b_vec])
y_vec = np.float64(y_vec)
fig = bk.figure(title=None)
fig.line(b_vec, y_vec)
bk.show(fig)
However, this kind of numerical evolution can be very slow, and there is a much more efficient way to do it: Use the function lambdify
to "compile" a Sympy expression into a function that is much more efficient to evaluate numerically:
f = lambdify([b], exp1, 'numpy')
y_vec = f(b_vec)
Algebraically solve equations or systems of equations (expr = 0)
x, y, z = symbols("x y z")
e = (x+y)*40-(z-x)/0.5
e
solve(e)
exp2 = (x+1)*sin(x+2)*tanh(x+3)
exp2
expand(exp2)
The expand
function takes a number of keywords arguments which we can tell the functions what kind of expansions we want to have performed. For example, to expand trigonometric expressions, use the trig=True
keyword argument (see help(sym.expand)
for more info):
expand(exp2, trig=True)
The opposite a product expansion is of course factoring. The factor an expression in SymPy use the factor
function:
factor(x**3 + 6 * x**2 + 11*x + 6)
The simplify
tries to simplify an expression into a nice looking expression, using various techniques. More specific alternatives to the simplify
functions also exists: trigsimp
, powsimp
, logcombine
, etc.
The basic usages of these functions are as follows:
simplify(sin(x)**2 + cos(x)**2)
simplify(sin(x)/cos(x))
exp3 = 1/((a+1)*(a+2))
exp3
apart(exp3)
exp4 = 1/(a+2) + 1/(a+3)
exp4
together(exp4)
exp2
diff(exp2)
exp4
integrate(exp3, a)
integrate(exp(-x**2), (x, -oo, oo))
n = Symbol("n")
exp5 = 1/n**2
exp5
Sum(exp5, (n, 1, oo))
Sum(exp5, (n, 1, oo)).evalf()
Product(exp5, (n, 1, oo))
limit(1/x, x, 0, dir="+")
limit(1/x, x, 0, dir="-")
By default it expands the expression around $x=0$, but we can expand around any value of $x$ by explicitly include a value in the function call. It's possible to specify to which order the series expansion should be carried out:
exp6 = exp(x)
series(exp6, x)
series(exp(x), x, 1, 10)
m11, m12, m21, m22 = symbols("m11, m12, m21, m22")
b1, b2 = symbols("b1, b2")
A = Matrix([[m11, m12],[m21, m22]])
A
b = Matrix([[b1], [b2]])
b
A * b
A.det()
A.inv()
exp7 = x**4 - x**2 - a
exp7
solve(exp7,x)
Visit www.add-for.com for more tutorials and updates.
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.