<a href="https://colab.research.google.com/github/ricklon/jupytercalculus/blob/master/basic_calculus_sympy.ipynb" target="_parent"></a>

# Basic Calculus: Derivatives, Integrals, Limits
from https://docs.sympy.org/latest/tutorial/basic_operations.html

Intiating symbol printing 


```python
from sympy import *
import sympy as sp
init_printing()
x, y, z = symbols('x y z')

```


```python
print(sp.__version__)
```

    1.1.1



```python
from IPython.display import HTML, Math
display(HTML(""))
Math(r"e^\alpha")
```








$$e^\alpha$$



# Derivatives
To take derivatives, use the diff function.


```python
figure = diff(cos(x), x)
display(figure)

```


```python
figure = diff(exp(x**2), x)
print(figure)
display(figure)
```

diff can take multiple derivatives at once. To take multiple derivatives, pass the variable as many times as you wish to differentiate, or pass a number after the variable. For example, both of the following find the third derivative of x^4.


```python
diff(cos(x), x)
```

You can also take derivatives with respect to many variables at once. Just pass each derivative in order, using the same syntax as for single variable derivatives. 


```python
diff(x**4, x, x, x)

```

Evaluate a derivative, https://stackoverflow.com/a/44274950


```python
import sympy as sym
import math


def f(x,y):
    return x**2 + x*y**2


x, y = sym.symbols('x y')

def fprime(x,y):
    return sym.diff(f(x,y),x)

print(fprime(x,y)) #This works.

DerivativeOfF = sym.lambdify((x,y),fprime(x,y),"numpy")

print(DerivativeOfF(1,1))
```

    2*x + y**2
    3


Partial Derivatives, https://stackoverflow.com/a/30791631


```python
from sympy import symbols, diff
x, y, z = symbols('x y z', real=True)
f = 4*x*y + x*sin(z) + x**3 + z**8*y
diff(f, x)
```

# Integrals
To compute an integral, use the integrate function. There are two kinds of integrals, definite and indefinite. To compute an indefinite integral, that is, an antiderivative, or primitive, just pass the variable after the expression.


```python
integrate(cos(x), x)
```

∞ in SymPy is oo (that’s the lowercase letter “oh” twice). This is because oo looks like ∞, and is easy to type.
To compute a definite integral, pass the argument (integration_variable, lower_limit, upper_limit). 


```python
integrate(exp(-x), (x, 0, oo))
```

As with indefinite integrals, you can pass multiple limit tuples to perform a multiple integral. 


```python
integrate(exp(-x**2 - y**2), (x, -oo, oo), (y, -oo, oo))
```


```python
#If integrate is unable to compute an integral, it returns an unevaluated Integral object.
```


```python
expr = integrate(x**x, x)
print(expr)
expr
```

As with Derivative, you can create an unevaluated integral using Integral. To later evaluate this integral, call doit.


```python
expr = Integral(log(x)**2, x)
expr
```


```python
expr.doit()
```

integrate uses powerful algorithms that are always improving to compute both definite and indefinite integrals, including heuristic pattern matching type algorithms, a partial implementation of the Risch algorithm, and an algorithm using Meijer G-functions that is useful for computing integrals in terms of special functions, especially definite integrals. Here is a sampling of some of the power of integrate.


```python
integ = Integral((x**4 + x**2*exp(x) - x**2 - 2*x*exp(x) - 2*x - exp(x))*exp(x)/((x - 1)**2*(x + 1)**2*(exp(x) + 1)), x)

#integ

integ.doit()
```


```python
integ = Integral(sin(x**2), x)

#integ

integ.doit()
```


```python
integ = Integral(x**y*exp(-x), (x, 0, oo))

#integ


integ.doit()
```

# Limits
SymPy can compute symbolic limits with the limit function.


```python
limit(sin(x)/x, x, 0)

```

Like Derivative and Integral, limit has an unevaluated counterpart, Limit. To evaluate it, use doit.


```python
expr = Limit((cos(x) - 1)/x, x, 0)
expr
#expr.doit()
```


```python
To evaluate a limit at one side only, pass '+' or '-' as a third argument to limit.
```


```python
expr = limit(1/x, x, 0, '+')
expr
```


```python
expr = limit(1/x, x, 0, '-')
expr
```

# Series Expansion¶

SymPy can compute asymptotic series expansions of functions around a point. To compute the expansion of f(x)
around the point x=x0 terms of order x^n , use f(x).series(x, x0, n). x0 and n can be omitted, in which case the defaults x0=0 and n=6 will be used.




```python
expr = exp(sin(x))
expr.series(x, 0, 4)
```


```python
 x + x**3 + x**6 + O(x**4)
```

If you do not want the order term, use the removeO method.


```python
expr.series(x, 0, 4).removeO()
```

The O notation supports arbitrary limit points (other than 0):




```python
exp(x - 6).series(x, x0=6)

```

ff814ab7fc614a9e408b45d7bfbd23277bdcc5c2
ipynb
Jupyter Notebook
basic_calculus_sympy.ipynb
ricklon/jupytercalculus
2c8a4d6b94881eedfe3d3ed54bdebfb5847ba611
Apache-2.0
basic_calculus_sympy.ipynb
ricklon/jupytercalculus
2c8a4d6b94881eedfe3d3ed54bdebfb5847ba611
Apache-2.0
basic_calculus_sympy.ipynb
ricklon/jupytercalculus
2c8a4d6b94881eedfe3d3ed54bdebfb5847ba611
Apache-2.0
Qwen/Qwen-72B
1. YES
2. YES