<jupyter_start><jupyter_text>sympy Since we want to work interactively with `sympy`, we will import the complete module. Note that this polutes the namespace, and is not recommended in general.<jupyter_code>from sympy import *<jupyter_output><empty_output><jupyter_text>Enable pretty printing in this notebook.<jupyter_code>init_printing()<jupyter_output><empty_output><jupyter_text>Expression manipulation Define a number of symbols to work with, as well as an example expression.<jupyter_code>x, y, a, b, c = symbols('x y a b c')
expr = (a*x**2 - b*y**2 + 5)/(c*x + y)<jupyter_output><empty_output><jupyter_text>Check the expression's type.<jupyter_code>expr.func<jupyter_output><empty_output><jupyter_text>Although the expression was defined as a divisino, it is represented as a multiplicatino by `sympy`.  The `args` attribute of an expressions stores the operands of the top-level operator.<jupyter_code>expr.args<jupyter_output><empty_output><jupyter_text>Although the first factor appears to be a division, it is in fact a power.  The denominator of this expression would be given by:<jupyter_code>expr.args[0].func
expr.args[0].args[0]<jupyter_output><empty_output><jupyter_text>The expression $\frac{1}{a x + b}$ can alternatively be defined as follows, which highlights the internal representation of expressions.<jupyter_code>expr = Pow(Add(Mul(a, x), b), -1)
pprint(expr)
expr.args
expr.args[0].args[0]<jupyter_output><empty_output><jupyter_text>This may be a bit surprising when you look at the mathematical representation of the expression, but the order of the terms is different from its rendering on the screen.<jupyter_code>expr.args[0].args<jupyter_output><empty_output><jupyter_text>Since the addition operation is commutative, this makes no difference mathematically.<jupyter_code>expr.args[0].args[1].args
expr = x**2 + 2*a*x + y**2
expr2 = expr.subs(y, a)
expr2<jupyter_output><empty_output><jupyter_text>Most expression manipulation algorithms can be called as functions, or as methods on expressions.<jupyter_code>factor(expr2)
expr2.factor()
x, y = symbols('x y', positive=True)
(log(x) + log(y)).simplify()<jupyter_output><empty_output><jupyter_text>Calculus  Series expansion<jupyter_code>x, a = symbols('x a')
expr = sin(a*x)/x
expr2 = series(expr, x, 0, n=7)
expr2<jupyter_output><empty_output><jupyter_text>A term of a specific order in a given variable can be selected easily.<jupyter_code>expr2.taylor_term(2, x)<jupyter_output><empty_output><jupyter_text>When the order is unimportant, or when the expression should be used to define a function, the order term can be removed.<jupyter_code>expr2.removeO()<jupyter_output><empty_output><jupyter_text>Adding two series deals with the order correctly.<jupyter_code>s1 = series(sin(x), x, 0, n=7)
s2 = series(cos(x), x, 0, n=4)
s1 + s2<jupyter_output><empty_output><jupyter_text>Derivatives and integrals<jupyter_code>expr = a*x**2 + b*x + c
expr.diff(x)
expr.integrate(x)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11062688568555146, 'fraction_numerical': 0.012068387529332886, 'mean_word_length': 7.501424501424501, 'pattern_counts': {'":': 0, '<': 61, '<?xml version=': 0, '>': 61, 'https://': 0, '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': '15', 'dir': 'jupyter-structured-clean-dedup', 'id': '280580', 'license': 'CC-BY-4.0', 'n_tokens_mistral': 1038, 'n_tokens_neox': 1013, 'n_words': 284, 'path': 'source-code/sympy/sympy.ipynb', 'repo_name': 'gjbex/Scientific-Python', 'size': '2983'}