<jupyter_start><jupyter_text>1. SymbolsPython variables and SymPy Variables<jupyter_code>a = 'stuff' # Python variable<jupyter_output><empty_output><jupyter_text>The `symbols` function creates a SymPy Variable `x` which is assigned to python variable `x`.<jupyter_code>x = symbols('x')
x<jupyter_output><empty_output><jupyter_text>The following works too, but not recommended. The python variable `crazy` is assigned to the SymPy variable `unrelated`. So, its always a good idea to name your variables same as your symbols.<jupyter_code>crazy = symbols('unrelated')
crazy<jupyter_output><empty_output><jupyter_text>2. Immutability<jupyter_code>x, y = symbols('x y')
a = x + y + 1
a
x = 3<jupyter_output><empty_output><jupyter_text>What is "`a`" now?<jupyter_code>a<jupyter_output><empty_output><jupyter_text>Now, how do I substitute x with 3?<jupyter_code>??<jupyter_output><empty_output><jupyter_text>What is `a` now?<jupyter_code>a<jupyter_output><empty_output><jupyter_text>SymPy expressions are all immutable. They never change in-place. So, whenever you do `subs`, `simplify`, etc it creates a new expression.  3. Equals signChecking mathematical inequality.<jupyter_code>x = symbols('x')
a = (x + 1)**2
a
b = x**2 + 2*x + 1<jupyter_output><empty_output><jupyter_text>Now, lets check if `a` and `b` are equal or not. What do you think will be the output of the following?<jupyter_code>a == b
simplify(a - b)<jupyter_output><empty_output><jupyter_text>That's the difference between Structural and Mathematical Equality.  Representing a symbolic equality, like say: $$x^2 = y$$<jupyter_code>eq = Eq(x**2, y)
eq.lhs
eq.rhs
solveset(eq, x)<jupyter_output><empty_output><jupyter_text>4. Fractions<jupyter_code># In Python 2
acos(1/2)<jupyter_output><empty_output><jupyter_text>The arc cosine of Half is π/3, isn't it?<jupyter_code># Python 2
1/2
# Python 3
from __future__ import division
1/2
acos(1/2)<jupyter_output><empty_output><jupyter_text>We are looking for symbolic results, right?<jupyter_code>Rational(1, 2)
acos(Rational(1, 2))
from fractions import Fraction
Fraction(1, 2)
acos(Fraction(1, 2))
type(sympify(Fraction(1, 2)))<jupyter_output><empty_output><jupyter_text>5. Logical Operator (XOR)<jupyter_code>x^2
sympify('x^2')<jupyter_output><empty_output><jupyter_text>If you are doing logic, and **don't** want to convert the `XOR` to power operator:<jupyter_code>sympify('x^2', convert_xor=False)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12505125051250512, 'fraction_numerical': 0.016400164001640016, 'mean_word_length': 8.070631970260223, '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': '359403', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 959, 'n_tokens_neox': 921, 'n_words': 223, 'path': 'tutorial_exercises/Gotchas.ipynb', 'repo_name': 'gvvynplaine/scipy-2016-tutorial', 'size': '2439'}