```python
from sympy import *
```

Copyright - Michael Corley

# **Printing and graphing functions**
*   This tutorial should be useful for those looking for a Mathematica or Matlab-like tool in Python.
*   This tutorial is designed to bring you up to speed as quickly as possible on how to use the tool, not to teach you what functions are.
> *SymPy is a Python library for symbolic mathematics. It aims to become a full-featured computer algebra system (CAS) while keeping the code as simple as possible in order to be comprehensible and easily extensible. SymPy is written entirely in Python.*


---








**Let** *f(x) = e^x* and *g(x) = sqrt(x-1)*
*   Lets work through how to initiate these functions






```python
x = Symbol('x')
f = Function('f')
g = Function('g')(x)

class f(Function):
  @classmethod
  def eval(cls, x):
    return exp(x)

class g(Function):
  @classmethod
  def eval(cls, x):
    return sqrt(x-1)
```

**First:** sympy allows you to print symbolic functions
*   You can either print it directly, or you can use the **latex()** function to output in Latex


```python
print(g(f(x)))
print(f(g(x)))

print('\n Latex output:')
print(latex(f(g(x))))
```

    sqrt(exp(x) - 1)
    exp(sqrt(x - 1))
    
     Latex output:
    e^{\sqrt{x - 1}}


**Second:** sympy allows you to graph functions automatically, but is build on commonly used Python libraries.
*   Normally this would required that you input some values for the function
*   You would have to write custom code to do this




```python
plot(f(g(x)))
```


```python
plot(g(f(x)))
```



---



# Solving for variables and simplifying expressions

Solving algebraic expressions using **solve()**
*   Simplfication is automatically set to true
*   **solve()** works with linear and non-linear equations




```python
z = Symbol('z')
t = Symbol('t')
equation = Equality(3*z-t,5)

#solve for z
print('z=')
print(solve(equation,z))

#solve for t
print('t=')
print(solve(equation,t))
```

    z=
    [t/3 + 5/3]
    t=
    [3*z - 5]


Non-linear function


```python
x = Symbol('x')
y = Symbol('y')
z = Symbol('z')

#solve for x
print('x=')
print(solve(z*x**2-y**2, x))

print('y=')
print(solve(z*x**2-y**2, y))
```

    x=
    [-y*sqrt(1/z), y*sqrt(1/z)]
    y=
    [-x*sqrt(z), x*sqrt(z)]


---
**Integrals:** probability density function example
$p(x) = \frac{\sqrt{2} e^{- \frac{0.5 \left(- \mu + x\right)^{2}}{s^{2}}}}{2 \sqrt{\pi} s}$





```python
mu = Symbol('mu')
s = Symbol('s')
x = Symbol('x')
pr = Function('pr')

#probability density function
class pr(Function):
  @classmethod
  def eval(cls, x, mu, s):
    return (1/(s*sqrt(2*pi)))*exp((-1/2)*((x-mu)/s)**2)

print(latex(pr(x, mu, s)))
```

    \frac{\sqrt{2} e^{- \frac{0.5 \left(- \mu + x\right)^{2}}{s^{2}}}}{2 \sqrt{\pi} s}


Plot the probability density function


```python
plot(pr(x,0,1)) #standard normal distribution has a mean of 0 and a sdev of 1
```

p(a≤x≤b) = ∫f(x)dx between b an a. What is the probability that x falls between 1 and 4?
*  **Solution per Excel: 0.158624**
* N() will make the numerical solution display
* integrate() can be used to evaluate definate and indefinate integrals


```python
a = 1
b = 4
print(N(integrate(pr(x, 0, 1), (x, a, b))))
```

    0.158623582689624


**Statistics:** probability density function
* The Normal() function can be used to define a normal distribution.
* The P() function can be used to compute the probability.



```python
from sympy.stats import *
Z=Normal('Z', 0, 1)
print(N(P(Z<4)-P(Z<1)))
```

    0.158623582689624


# Resources
* Latest official documentation: https://docs.sympy.org/latest/index.html
* Unofficial Tutorial: https://www.tutorialspoint.com/sympy/




10a730161fcb6d0d59c7e5cc1d85c46284a7b959
ipynb
Jupyter Notebook
Lecture1-SymPy.ipynb
ikicker/FinTech-Python-package-tutorials
906b99e6cf14612a84dce392747993fdf47b608d
MIT
Lecture1-SymPy.ipynb
ikicker/FinTech-Python-package-tutorials
906b99e6cf14612a84dce392747993fdf47b608d
MIT
Lecture1-SymPy.ipynb
ikicker/FinTech-Python-package-tutorials
906b99e6cf14612a84dce392747993fdf47b608d
MIT
Qwen/Qwen-72B
1. YES
2. YES