pytutorial/sympy/intro/README.md
David Rotermund 8ff18384fe
Create README.md
Signed-off-by: David Rotermund <54365609+davrot@users.noreply.github.com>
2024-01-03 19:11:53 +01:00

2 KiB

Symbolic Computation

{:.no_toc}

* TOC {:toc}

Top

Questions to David Rotermund

pip install sympy
Simplification
Calculus
Solvers

Some examples

Derivatives

import sympy

x, y = sympy.symbols("x y")

y = sympy.diff(sympy.sin(x) * sympy.exp(x), x)
print(y) # -> exp(x)*sin(x) + exp(x)*cos(x)

Integrals

import sympy

x, y = sympy.symbols("x y")

y = sympy.integrate(sympy.cos(x), x)
print(y)  # -> sin(x)

(Taylor) Series Expansion

import sympy

x, y, z = sympy.symbols("x y z")
y = sympy.cos(x)
z = y.series(x, 0, 8) # around x = 0 , up order 7

print(z)  # -> 1 - x**2/2 + x**4/24 - x**6/720 + O(x**8)

simplify

import sympy

x, y, z = sympy.symbols("x y z")
y = sympy.simplify(sympy.sin(x) ** 2 + sympy.cos(x) ** 2)

print(y)  # -> 1

Solving Equations Algebraically

Recall from the gotchas section of this tutorial that symbolic equations in SymPy are not represented by = or ==, but by Eq.

import sympy

x, y, z = sympy.symbols("x y z")

z = sympy.Eq(x, y)

Output:

x=y