<jupyter_start><jupyter_text>SymPy Tutorial*<NAME>* *Last Updated: 2020-04-14*This notebook contains the examples from the [SymPy Tutorial](https://docs.sympy.org/latest/tutorial/index.html).  Simplification<jupyter_code>from sympy import *
x, y, z = symbols('x y z')
init_printing(use_unicode=True)<jupyter_output><empty_output><jupyter_text>simplify<jupyter_code>simplify(sin(x)**2 + cos(x)**2)
simplify((x**3 + x**2 - x - 1)/(x**2 + 2*x + 1))
simplify(gamma(x)/gamma(x - 2))
simplify(x**2 + 2*x + 1)<jupyter_output><empty_output><jupyter_text>Polynomial/Rational Function Simplification  expand<jupyter_code>expand((x + 1)**2)
expand((x + 2)*(x - 3))
expand((x + 1)*(x - 2) - (x -1)*x)<jupyter_output><empty_output><jupyter_text>factor<jupyter_code>factor(x**3 - x**2 + x - 1)
factor(x**2*z + 4*x*y*z + 4*y**2*z)
factor_list(x**2*z + 4*x*y*z + 4*y**2*z)
expand((cos(x) + sin(x))**2)
factor(sin(x)**2 + 2*cos(x)*sin(x) + cos(x)**2)<jupyter_output><empty_output><jupyter_text>collect<jupyter_code>expr = x*y + x - 3 + 2*x**2 - z*x**2 + x**3
expr
collected_expr = collect(expr, x)
collected_expr
collected_expr.coeff(x, 2)<jupyter_output><empty_output><jupyter_text>cancel<jupyter_code>cancel((x**2 + 2*x + 1)/(x**2 + x))
expr = 1/x + (3*x/2 - 2)/(x - 4)
expr
cancel(expr)
expr = (x*y**2 - 2*x*y*z + x*z**2 + y**2 - 2*y*z + z**2)/(x**2 - 1)
expr
cancel(expr)
factor(expr)<jupyter_output><empty_output><jupyter_text>apart<jupyter_code>expr = (4*x**3 + 21*x**2 + 10*x + 12)/(x**4 + 5*x**3 + 5*x**2 + 4*x)
expr
apart(expr)<jupyter_output><empty_output><jupyter_text>Trigonometric Simplification<jupyter_code>acos(x)
cos(acos(x))
asin(1)<jupyter_output><empty_output><jupyter_text>trigsimp<jupyter_code>trigsimp(sin(x)**2 + cos(x)**2)
trigsimp(sin(x)**4 - 2*cos(x)**2*sin(x)**2 + cos(x)**4)
trigsimp(sin(x)*tan(x)/sec(x))
trigsimp(cosh(x)**2 + sinh(x)**2)
trigsimp(sinh(x)/tanh(x))<jupyter_output><empty_output><jupyter_text>expand_trig<jupyter_code>expand_trig(sin(x + y))
expand_trig(tan(2*x))
trigsimp(sin(x)*cos(x) + sin(y)*cos(x))<jupyter_output><empty_output><jupyter_text>Powers<jupyter_code>x, y = symbols('x y', positive=True)
a, b = symbols('a b', real=True)
z, t, c = symbols('z t c')
sqrt(x) == x**Rational(1, 2)<jupyter_output><empty_output><jupyter_text>powsimp<jupyter_code>powsimp(x**a*x**b)
powsimp(x**a*y**a)
powsimp(t**c*z**c)
powsimp(t**c*z**c, force=True)
(z*t)**2
sqrt(x*y)
powsimp(z**2*t**2)
powsimp(sqrt(x)*sqrt(y))<jupyter_output><empty_output><jupyter_text>expand_power_exp / expand_power_base<jupyter_code>expand_power_exp(x**(a + b))
expand_power_base((x*y)**a)
expand_power_base((z*t)**c)
expand_power_base((z*t)**c, force=True)
x**2*x**3
expand_power_exp(x**5)<jupyter_output><empty_output><jupyter_text>powdenest<jupyter_code>powdenest((x**a)**b)
powdenest((z**a)**b)
powdenest((z**a)**b, force=True)<jupyter_output><empty_output><jupyter_text>Exponentials and logarithms<jupyter_code>ln(x)
x, y = symbols('x y', positive=True)
n = symbols('n', real=True)<jupyter_output><empty_output><jupyter_text>expand_log<jupyter_code>expand_log(log(x*y))
expand_log(log(x/y))
expand_log(log(x**2))
expand_log(log(x**n))
expand_log(log(z*t))
expand_log(log(z**2))
expand_log(log(z**2), force=True)<jupyter_output><empty_output><jupyter_text>logcombine<jupyter_code>logcombine(log(x) + log(y))
logcombine(n*log(x))
logcombine(n*log(z))
logcombine(n*log(z), force=True)<jupyter_output><empty_output><jupyter_text>Special Functions<jupyter_code>x, y, z = symbols('x y z')
k, m, n = symbols('k m n')
factorial(n)
binomial(n, k)
gamma(z)
hyper([1, 2], [3], z)
tan(x).rewrite(sin)<jupyter_output><empty_output><jupyter_text>rewrite<jupyter_code>factorial(x).rewrite(gamma)<jupyter_output><empty_output><jupyter_text>expand_func<jupyter_code>expand_func(gamma(x + 3))<jupyter_output><empty_output><jupyter_text>hyperexpand<jupyter_code>hyperexpand(hyper([1, 1], [2], z))
expr = meijerg([[1], [1]], [[1], []], -z)
expr
hyperexpand(expr)<jupyter_output><empty_output><jupyter_text>combsimp<jupyter_code>n, k = symbols('n k', integer = True)
combsimp(factorial(n)/factorial(n -3))
combsimp(binomial(n+1, k+1)/binomial(n, k))<jupyter_output><empty_output><jupyter_text>gammasimp<jupyter_code>gammasimp(gamma(x)*gamma(1 - x))<jupyter_output><empty_output><jupyter_text>Example: Continued Fractions<jupyter_code>def list_to_frac(l):
    expr = Integer(0)
    for i in reversed(l[1:]):
        expr += i
        expr = 1/expr
    return l[0] + expr
list_to_frac([x, y, z])
list_to_frac([1, 2, 3, 4])
syms = symbols('a0:5')
syms
a0, a1, a2, a3, a4 = syms
frac = list_to_frac(syms)
frac
frac = cancel(frac)
frac
l = []
frac = apart(frac, a0)
frac
l.append(a0)
frac = 1/(frac - a0)
frac
frac = apart(frac, a1)
frac
l.append(a1)
frac = 1/(frac - a1)
frac = apart(frac, a2)
frac
l.append(a2)
frac = 1/(frac - a2)
frac = apart(frac, a3)
frac
l.append(a3)
frac = 1/(frac - a3)
frac = apart(frac, a4)
frac
l.append(a4)
list_to_frac(l)
import random
l = list(symbols('a0:5'))
random.shuffle(l)
orig_frac = frac = cancel(list_to_frac(l))
del l
frac
l = [a1]
frac = apart(frac, a1)
frac = 1/(frac - a1)
frac
l.append(a3)
frac = apart(frac, a3)
frac = 1/(frac - a3)
frac
l.append(a4)
frac = apart(frac, a4)
frac = 1/(frac - a4)
frac
l.append(a0)
frac = apart(frac, a0)
frac = 1/(frac - a0)
frac
l.append(a2)
new_frac = cancel(list_to_frac(l))
new_frac
new_frac == orig_frac<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.20530415430267063, 'fraction_numerical': 0.03115727002967359, 'mean_word_length': 8.395470383275262, 'pattern_counts': {'":': 0, '<': 98, '<?xml version=': 0, '>': 98, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '24', 'dir': 'jupyter-structured-clean-dedup', 'id': '473512', 'license': 'MIT', 'n_tokens_mistral': 2614, 'n_tokens_neox': 2500, 'n_words': 349, 'path': 'notes/docs.sympy.org/tutorial/simplification.ipynb', 'repo_name': 'agryman/acmpy', 'size': '5398'}