<jupyter_start><jupyter_text>SymPyThis notebook shows some of SymPy's main features. Consult the official documentation at  http://docs.sympy.org/latest/index.html to obtain the whole module reference.SymPy is a Computer Algebra System (CAS) for Python. In other words, you can perform symbolic mathematic operations with it and embed these operations into your Python programs. SymPy is part of the SciPy project. Usually, Sympy is bundled with common Python distribution, such as Anaconda (https://www.continuum.io/anaconda). If it is not bundled with your distribution and you use pip, you can install SymPy with the command "pip install sympy".Most of the shown examples are derived from the free book "Scipy lectures" (http://www.scipy-lectures.org).Once you have installed SymPy on your device, you can load it into your Python script:<jupyter_code># This notebook was tested with Python 3.5 and SymPy 1.0

# Only methods that are used in this notebook are imported. To import all functions, write "from sympy import *".
# Note that some methods, such as "sin", have homonymous counterparts in other packages, such as NumPy.
# If you use other packages, use "import sympy (as ...)" and "sympy." before every SymPy method to avoid conflicts.
from sympy import * #e.g. Derivative, diff, dsolve, Eq, Function, init_printing, integrate, lambdify, Matrix, sin, solve, sqrt, ...
from sympy.abc import x, y, z # Can be anything from a to z. Represents variables.
from sympy.solvers import linsolve # To solve linear equation systems.<jupyter_output><empty_output><jupyter_text>1. Symbolic representation with SymPySymPy works with symbolic mathematic operations. You can store mathematic formulas in variables without the necessity to compute them directly:<jupyter_code>function = sqrt(sin(x)) + 23 # "sqrt" and "sin" were imported from sympy. "x" was imported from sympy.abc.
function<jupyter_output><empty_output><jupyter_text>2. Usage in interactive sessionsIn interactive sessions, like this Jupyter notebook, you can also print your expressions as more readable LATEX or Matjax expressions (depending on your device) after you initialzed this option with "init_printing()":(More information at http://docs.sympy.org/latest/tutorial/printing.html)<jupyter_code>init_printing()
function
oo #Infinite<jupyter_output><empty_output><jupyter_text>3. Simplify or expand mathematical expressions<jupyter_code>expand((x + y)**3)
expand(x + y, complex=True)
expand(cos(x + y), trig=True)
simplify((x + x*y) / x)<jupyter_output><empty_output><jupyter_text>4. Limits of functions<jupyter_code># Limit of sin(x) as x->0
limit(sin(x)/x, x, 0)
# Using oo (which stands for inifinte)
# Limit of x as x->oo
limit(x, x, oo)
limit(x**2, x, -oo)<jupyter_output><empty_output><jupyter_text>5. Differentiation and IntegrationYou can perform calculus operations on your functions, such as differentiation and integration:(More information at http://docs.sympy.org/latest/tutorial/calculus.html)<jupyter_code>function2 = x**2
# First derivative
function2.diff()
# Second derivative using alternative diff()
diff(function2, x, 2)
function3 = sin(x) * 2
# Integration without limits (indefinite)
integrate(function3, x)
# Integration with limits (definite)
integrate(function3, (x, 0, 2))
# Improper integration (using oo as infinite)
integrate(exp(-x**2), (x, -oo, oo))<jupyter_output><empty_output><jupyter_text>5. Series expansionYou can find about the series module at http://docs.sympy.org/latest/modules/series/index.html<jupyter_code># Get the Taylor series at 0 until x^15...
series(cos(x), x, 0, 15)
# ...or at 3 until the error is automatically assumed as small enough
series(cos(x), x, 3)
# Get the Fourier series from -Pi to +Pi
s = fourier_series(x**2, (x, -pi, pi))
s
# Returns the 4 first elements of the fourier series
s.truncate(n=4)<jupyter_output><empty_output><jupyter_text>6. Defining and solving equations/inequalitiesAside of defining functions, you can also define liner and non-linear equations (with "Eq()") and solve them:(More information at http://docs.sympy.org/latest/modules/solvers/solvers.html)<jupyter_code>equation = Eq(x**2, 4)
solve(equation)<jupyter_output><empty_output><jupyter_text>You can access the left hand side and the right hand side of the equation with the following attributes:<jupyter_code>equation.lhs
equation.rhs<jupyter_output><empty_output><jupyter_text>SymPy can also solve inequalities,...<jupyter_code>inequality = x**2 < 10
solve(inequality)<jupyter_output><empty_output><jupyter_text>...linear equation systems...<jupyter_code># Solves the following system of two equations:
# x +  y +  z -1 = 0
# x + 2y + 3z -1 = 0
# Alternative method: linsolve([x + y + z - 1, x + 2*y + 3*z - 1], (x, y, z))
linsolve(Matrix(([1, 1, 1, 1], [1, 2, 3, 1])), (x, y, z))<jupyter_output><empty_output><jupyter_text>...and overdetermined systems<jupyter_code>x1 = Symbol('x1')
x2 = Symbol('x2')
f1 = 3 * x1**2 - 2 * x2**2 - 1
f2 = x1**2 - 2 * x1 + x2**2 + 2 * x2 - 8
print(nsolve((f1, f2), (x1, x2), (-1, 1)))<jupyter_output>Matrix([[-1.19287309935246], [1.27844411169911]])<jupyter_text>7. Ordinary Differential Equations (ODE)ODE can be solved in the following way (More information at http://docs.sympy.org/latest/modules/solvers/ode.html):<jupyter_code>f = Function("f")
diff_equation = Eq(f(x).diff()/f(x), 3/x) # In other words: y'/y = 3/x
diff_result = dsolve(diff_equation, f(x))
diff_result<jupyter_output><empty_output><jupyter_text>It is also possible to classify ODEs wth SymPy:<jupyter_code>classify_ode(diff_equation, f(x))<jupyter_output><empty_output><jupyter_text>For Partial Differental Equations, look here: http://docs.sympy.org/latest/modules/solvers/pde.html 8. Conversion of SymPy representations into Python codeYou can also convert SymPy functions to Python functions to use them directly in your programs:<jupyter_code>result_function = diff_result.rhs
result_function
python_function = lambdify(('C1', x), result_function)
python_function(1, 2) # C1=1, x=2.<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11416735708367855, 'fraction_numerical': 0.021706710853355425, 'mean_word_length': 5.774410774410774, 'pattern_counts': {'":': 1, '<': 61, '<?xml version=': 0, '>': 62, 'https://': 1, 'lorem ipsum': 0, 'www.': 2, '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': '426506', 'license': 'MIT', 'n_tokens_mistral': 2073, 'n_tokens_neox': 1950, 'n_words': 654, 'path': 'python_module_tutorials/SymPy.ipynb', 'repo_name': 'Paulocracy/python_tutorials', 'size': '6035'}